os/security/authorisation/userpromptservice/server/source/upsserver/upspolicycachehandle.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/server/source/upsserver/upspolicycachehandle.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,283 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Implements the UPS database handle manager.	See class and function
    1.19 +* definitions for more information.
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +#include <f32file.h>
    1.28 +#include "upscommon.h"
    1.29 +#include "policycache.h"
    1.30 +#include "upspolicycachehandle.h"
    1.31 +
    1.32 +namespace UserPromptService
    1.33 +{
    1.34 +
    1.35 +NONSHARABLE_CLASS(CPolicyCacheContainer) : public CBase
    1.36 +	{
    1.37 +public:
    1.38 +	static CPolicyCacheContainer *NewL(RFs &aFs);
    1.39 +
    1.40 +	void IncrementReferenceCount();
    1.41 +	void DecrementReferenceCount();
    1.42 +	inline CPolicyCache *PolicyCache();
    1.43 +
    1.44 +	TInt ReferenceCount() const;
    1.45 +
    1.46 +	void NotifyOnRef1(TRequestStatus &aStatus);
    1.47 +	void CancelNotifyOnRef1();
    1.48 +
    1.49 +private:
    1.50 +	void ConstructL(RFs &aFs);
    1.51 +	~CPolicyCacheContainer();
    1.52 +	
    1.53 +	TInt iReferenceCount;
    1.54 +	CPolicyCache *iPolicyCache;
    1.55 +	TRequestStatus *iClientRequest;
    1.56 +	};
    1.57 +
    1.58 +inline CPolicyCache *CPolicyCacheContainer::PolicyCache()
    1.59 +	{
    1.60 +	ASSERT(iPolicyCache != 0);
    1.61 +	return iPolicyCache;
    1.62 +	}
    1.63 +
    1.64 +inline TInt CPolicyCacheContainer::ReferenceCount() const
    1.65 +	{
    1.66 +	return iReferenceCount;
    1.67 +	}
    1.68 +
    1.69 +RPolicyCacheCountedHandle::RPolicyCacheCountedHandle(RFs &aFs)
    1.70 +	: iFs(aFs), iContainer(0)
    1.71 +	{
    1.72 +	}
    1.73 +
    1.74 +RPolicyCacheCountedHandle::RPolicyCacheCountedHandle(RPolicyCacheCountedHandle &aPolicyCacheManager)
    1.75 +	: iFs(aPolicyCacheManager.iFs), iContainer(0)
    1.76 +	{
    1.77 +	*this = aPolicyCacheManager;
    1.78 +	}
    1.79 +
    1.80 +RPolicyCacheCountedHandle &RPolicyCacheCountedHandle::operator=(const RPolicyCacheCountedHandle &aRhs)
    1.81 +	{
    1.82 +	if(this == &aRhs) return *this;
    1.83 +	
    1.84 +	Release();
    1.85 +
    1.86 +	if(aRhs.iContainer)
    1.87 +		{
    1.88 +		iContainer = aRhs.iContainer;
    1.89 +		iContainer->IncrementReferenceCount();
    1.90 +		}
    1.91 +	
    1.92 +	return *this;
    1.93 +	}
    1.94 +
    1.95 +RPolicyCacheCountedHandle::~RPolicyCacheCountedHandle()
    1.96 +/**
    1.97 +	Destructor - make sure cache container is released.
    1.98 +*/
    1.99 +	{
   1.100 +	Release();
   1.101 +	}
   1.102 +
   1.103 +void RPolicyCacheCountedHandle::OpenL()
   1.104 +/**
   1.105 +	Create/Open a new policy cache. If this manager is already opened then the existing cache is
   1.106 +	released first (see Release()).
   1.107 +*/
   1.108 +	{
   1.109 +	// First release any existing container/policy cache.
   1.110 +	Release();
   1.111 +
   1.112 +	// Now create a new one
   1.113 +	iContainer = CPolicyCacheContainer::NewL(iFs);
   1.114 +	}
   1.115 +
   1.116 +void RPolicyCacheCountedHandle::Release()
   1.117 +/**
   1.118 +	Release the current policy cache container. If this decreases its reference count to 0, it will be
   1.119 +	deleted.
   1.120 +*/
   1.121 +	{
   1.122 +	if(iContainer)
   1.123 +		{
   1.124 +		iContainer->DecrementReferenceCount();
   1.125 +		iContainer = 0;
   1.126 +		}
   1.127 +	}
   1.128 +
   1.129 +TBool RPolicyCacheCountedHandle::IsOpen() const
   1.130 +	{
   1.131 +	return iContainer != 0;
   1.132 +	}
   1.133 +
   1.134 +CPolicyCache *RPolicyCacheCountedHandle::operator->()
   1.135 +/**
   1.136 +	Returns the CPolicyCache ptr so -> can be used to call policy cache functions.
   1.137 +
   1.138 +	If the class is not already open, then we will attempt to open ourself using OpenL().
   1.139 +
   1.140 +	Note that this operator can leave...
   1.141 +*/
   1.142 +	{
   1.143 +	if(iContainer == 0)
   1.144 +		{
   1.145 +		OpenL();
   1.146 +		ASSERT(iContainer);
   1.147 +		}
   1.148 +	return iContainer->PolicyCache();
   1.149 +	}
   1.150 +
   1.151 +void RPolicyCacheCountedHandle::NotifyOnRef1(TRequestStatus &aStatus)
   1.152 +/**
   1.153 +	Register for notifcation when the underlying CPolicyCacheContainer reference
   1.154 +	count reaches 1, presumably when the client holds the only reference.
   1.155 +
   1.156 +	Only one client is allowed to register against a single CPolicyCacheContainer.
   1.157 +*/
   1.158 +	{
   1.159 +	if((iContainer == 0) || (iContainer->ReferenceCount() <= 1))
   1.160 +		{
   1.161 +		// No container or ref count is 1, so complete now.
   1.162 +		TRequestStatus *rs = &aStatus;
   1.163 +		*rs = KRequestPending;
   1.164 +		User::RequestComplete(rs, KErrNone);
   1.165 +		return;
   1.166 +		}
   1.167 +	iContainer->NotifyOnRef1(aStatus);
   1.168 +	}
   1.169 +
   1.170 +void RPolicyCacheCountedHandle::CancelNotifyOnRef1()
   1.171 +/**
   1.172 +	Cancel the call to NotifyOnRef1. The request will be completed immediately.
   1.173 +*/
   1.174 +	{
   1.175 +	if(!iContainer)
   1.176 +		{
   1.177 +		return;
   1.178 +		}
   1.179 +	iContainer->CancelNotifyOnRef1();
   1.180 +	}
   1.181 +
   1.182 +CPolicyCacheContainer *CPolicyCacheContainer::NewL(RFs &aFs)
   1.183 +/**
   1.184 +	Create a CPolicyCacheContainer containing a CPolicyCache with a reference count of 1.
   1.185 +*/
   1.186 +	{
   1.187 +	CPolicyCacheContainer *self = new(ELeave) CPolicyCacheContainer();
   1.188 +	CleanupStack::PushL(self);
   1.189 +	self->ConstructL(aFs);
   1.190 +	CleanupStack::Pop(self);
   1.191 +	return self;
   1.192 +	}
   1.193 +
   1.194 +void CPolicyCacheContainer::IncrementReferenceCount()
   1.195 +/**
   1.196 +	Increment the reference count.
   1.197 +*/
   1.198 +	{
   1.199 +	ASSERT(iReferenceCount > 0);
   1.200 +	++iReferenceCount;
   1.201 +	}
   1.202 +
   1.203 +void CPolicyCacheContainer::DecrementReferenceCount()
   1.204 +/**
   1.205 +	Decrement the reference count and delete self if it reaches 0.
   1.206 +*/
   1.207 +	{
   1.208 +	--iReferenceCount;
   1.209 +	if(iReferenceCount <= 1)
   1.210 +		{
   1.211 +		if(iClientRequest && iClientRequest->Int() == KRequestPending)
   1.212 +			{
   1.213 +			User::RequestComplete(iClientRequest, KErrNone);
   1.214 +			}
   1.215 +		}
   1.216 +
   1.217 +	if(iReferenceCount <= 0)
   1.218 +		{
   1.219 +		delete this;
   1.220 +		}
   1.221 +	}
   1.222 +
   1.223 +void CPolicyCacheContainer::NotifyOnRef1(TRequestStatus &aStatus)
   1.224 +/**
   1.225 +	Register for notifcation when the reference count reduces to 1, presumably 
   1.226 +	when the client holds the only reference.
   1.227 +
   1.228 +	Only one client is allowed to register against a single CPolicyCacheContainer.
   1.229 +*/
   1.230 +	{
   1.231 +	BULLSEYE_OFF
   1.232 +	if(iClientRequest != 0)
   1.233 +		{
   1.234 +		TRequestStatus *rs = &aStatus;
   1.235 +		*rs = KRequestPending;
   1.236 +		User::RequestComplete(rs, KErrServerBusy);
   1.237 +		return;
   1.238 +		}
   1.239 +	BULLSEYE_RESTORE
   1.240 +
   1.241 +	// Initialise client request
   1.242 +	iClientRequest = &aStatus;
   1.243 +	*iClientRequest = KRequestPending;
   1.244 +
   1.245 +	// Are we already at ref 1?
   1.246 +	if(iReferenceCount <= 1)
   1.247 +		{
   1.248 +		User::RequestComplete(iClientRequest, KErrNone);
   1.249 +		}
   1.250 +	}
   1.251 +
   1.252 +void CPolicyCacheContainer::CancelNotifyOnRef1()
   1.253 +/**
   1.254 +	Cancel the previous call to NotifyOnRef1.
   1.255 +*/
   1.256 +	{
   1.257 +	if((iClientRequest != 0) && (iClientRequest->Int() == KRequestPending))
   1.258 +		{
   1.259 +		User::RequestComplete(iClientRequest, KErrCancel);
   1.260 +		return;
   1.261 +		}
   1.262 +	}	
   1.263 +
   1.264 +void CPolicyCacheContainer::ConstructL(RFs &aFs)
   1.265 +	{
   1.266 +	_LIT(KPolicyDir,"\\private\\10283558\\policies\\");
   1.267 +	iPolicyCache = CPolicyCache::NewL(aFs, KPolicyDir());
   1.268 +	iReferenceCount = 1;
   1.269 +	}
   1.270 +
   1.271 +CPolicyCacheContainer::~CPolicyCacheContainer()
   1.272 +	{
   1.273 +	delete iPolicyCache;
   1.274 +	iPolicyCache = 0;
   1.275 +
   1.276 +	if(iClientRequest && iClientRequest->Int() == KRequestPending)
   1.277 +		{
   1.278 +		User::RequestComplete(iClientRequest, KErrNone);
   1.279 +		iClientRequest = 0;
   1.280 +		}
   1.281 +	}
   1.282 +
   1.283 +} // End of UserPromptService namespace
   1.284 +
   1.285 +// End of file
   1.286 +