os/security/authorisation/userpromptservice/server/source/upsserver/upspolicycachehandle.cpp
First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implements the UPS database handle manager. See class and function
16 * definitions for more information.
25 #include "upscommon.h"
26 #include "policycache.h"
27 #include "upspolicycachehandle.h"
29 namespace UserPromptService
32 NONSHARABLE_CLASS(CPolicyCacheContainer) : public CBase
35 static CPolicyCacheContainer *NewL(RFs &aFs);
37 void IncrementReferenceCount();
38 void DecrementReferenceCount();
39 inline CPolicyCache *PolicyCache();
41 TInt ReferenceCount() const;
43 void NotifyOnRef1(TRequestStatus &aStatus);
44 void CancelNotifyOnRef1();
47 void ConstructL(RFs &aFs);
48 ~CPolicyCacheContainer();
51 CPolicyCache *iPolicyCache;
52 TRequestStatus *iClientRequest;
55 inline CPolicyCache *CPolicyCacheContainer::PolicyCache()
57 ASSERT(iPolicyCache != 0);
61 inline TInt CPolicyCacheContainer::ReferenceCount() const
63 return iReferenceCount;
66 RPolicyCacheCountedHandle::RPolicyCacheCountedHandle(RFs &aFs)
67 : iFs(aFs), iContainer(0)
71 RPolicyCacheCountedHandle::RPolicyCacheCountedHandle(RPolicyCacheCountedHandle &aPolicyCacheManager)
72 : iFs(aPolicyCacheManager.iFs), iContainer(0)
74 *this = aPolicyCacheManager;
77 RPolicyCacheCountedHandle &RPolicyCacheCountedHandle::operator=(const RPolicyCacheCountedHandle &aRhs)
79 if(this == &aRhs) return *this;
85 iContainer = aRhs.iContainer;
86 iContainer->IncrementReferenceCount();
92 RPolicyCacheCountedHandle::~RPolicyCacheCountedHandle()
94 Destructor - make sure cache container is released.
100 void RPolicyCacheCountedHandle::OpenL()
102 Create/Open a new policy cache. If this manager is already opened then the existing cache is
103 released first (see Release()).
106 // First release any existing container/policy cache.
109 // Now create a new one
110 iContainer = CPolicyCacheContainer::NewL(iFs);
113 void RPolicyCacheCountedHandle::Release()
115 Release the current policy cache container. If this decreases its reference count to 0, it will be
121 iContainer->DecrementReferenceCount();
126 TBool RPolicyCacheCountedHandle::IsOpen() const
128 return iContainer != 0;
131 CPolicyCache *RPolicyCacheCountedHandle::operator->()
133 Returns the CPolicyCache ptr so -> can be used to call policy cache functions.
135 If the class is not already open, then we will attempt to open ourself using OpenL().
137 Note that this operator can leave...
145 return iContainer->PolicyCache();
148 void RPolicyCacheCountedHandle::NotifyOnRef1(TRequestStatus &aStatus)
150 Register for notifcation when the underlying CPolicyCacheContainer reference
151 count reaches 1, presumably when the client holds the only reference.
153 Only one client is allowed to register against a single CPolicyCacheContainer.
156 if((iContainer == 0) || (iContainer->ReferenceCount() <= 1))
158 // No container or ref count is 1, so complete now.
159 TRequestStatus *rs = &aStatus;
160 *rs = KRequestPending;
161 User::RequestComplete(rs, KErrNone);
164 iContainer->NotifyOnRef1(aStatus);
167 void RPolicyCacheCountedHandle::CancelNotifyOnRef1()
169 Cancel the call to NotifyOnRef1. The request will be completed immediately.
176 iContainer->CancelNotifyOnRef1();
179 CPolicyCacheContainer *CPolicyCacheContainer::NewL(RFs &aFs)
181 Create a CPolicyCacheContainer containing a CPolicyCache with a reference count of 1.
184 CPolicyCacheContainer *self = new(ELeave) CPolicyCacheContainer();
185 CleanupStack::PushL(self);
186 self->ConstructL(aFs);
187 CleanupStack::Pop(self);
191 void CPolicyCacheContainer::IncrementReferenceCount()
193 Increment the reference count.
196 ASSERT(iReferenceCount > 0);
200 void CPolicyCacheContainer::DecrementReferenceCount()
202 Decrement the reference count and delete self if it reaches 0.
206 if(iReferenceCount <= 1)
208 if(iClientRequest && iClientRequest->Int() == KRequestPending)
210 User::RequestComplete(iClientRequest, KErrNone);
214 if(iReferenceCount <= 0)
220 void CPolicyCacheContainer::NotifyOnRef1(TRequestStatus &aStatus)
222 Register for notifcation when the reference count reduces to 1, presumably
223 when the client holds the only reference.
225 Only one client is allowed to register against a single CPolicyCacheContainer.
229 if(iClientRequest != 0)
231 TRequestStatus *rs = &aStatus;
232 *rs = KRequestPending;
233 User::RequestComplete(rs, KErrServerBusy);
238 // Initialise client request
239 iClientRequest = &aStatus;
240 *iClientRequest = KRequestPending;
242 // Are we already at ref 1?
243 if(iReferenceCount <= 1)
245 User::RequestComplete(iClientRequest, KErrNone);
249 void CPolicyCacheContainer::CancelNotifyOnRef1()
251 Cancel the previous call to NotifyOnRef1.
254 if((iClientRequest != 0) && (iClientRequest->Int() == KRequestPending))
256 User::RequestComplete(iClientRequest, KErrCancel);
261 void CPolicyCacheContainer::ConstructL(RFs &aFs)
263 _LIT(KPolicyDir,"\\private\\10283558\\policies\\");
264 iPolicyCache = CPolicyCache::NewL(aFs, KPolicyDir());
268 CPolicyCacheContainer::~CPolicyCacheContainer()
273 if(iClientRequest && iClientRequest->Int() == KRequestPending)
275 User::RequestComplete(iClientRequest, KErrNone);
280 } // End of UserPromptService namespace