os/security/authorisation/userpromptservice/server/source/upsserver/policychangeevaluator.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/policychangeevaluator.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,321 @@
     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 +* Process RUpsManagement::NotifyPolicyFilesChanged.
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 +*/
    1.26 +
    1.27 +#include "upsserver.h"
    1.28 +#include <ups/upsdbw.h>
    1.29 +#include <e32property.h>
    1.30 +#include "policycache.h"
    1.31 +#include "policychangeevaluator.h"
    1.32 +
    1.33 +namespace UserPromptService
    1.34 +{
    1.35 +inline CUpsSession *CPolicyChangeEvaluator::UpsSession()
    1.36 +	{
    1.37 +	return static_cast<CUpsSession*>(iSession);
    1.38 +	}
    1.39 +
    1.40 +inline CUpsServer *CPolicyChangeEvaluator::UpsServer()
    1.41 +	{
    1.42 +	return static_cast<CUpsServer *>(
    1.43 +		&static_cast<CUpsSession*>(iSession)->iServer);
    1.44 +	}
    1.45 +
    1.46 +CPolicyChangeEvaluator* CPolicyChangeEvaluator::NewLC(RPolicyCacheCountedHandle &aPolicyCacheHandle, CUpsSession* aSession, const RMessage2& aMessage)
    1.47 +	{
    1.48 +	CPolicyChangeEvaluator* self = new(ELeave) CPolicyChangeEvaluator(aPolicyCacheHandle, aSession, aMessage);
    1.49 +	CleanupStack::PushL(self);
    1.50 +	return self;
    1.51 +	}
    1.52 +
    1.53 +CPolicyChangeEvaluator::CPolicyChangeEvaluator(RPolicyCacheCountedHandle &aPolicyCacheHandle, CUpsSession* aSession, const RMessage2& aMessage)
    1.54 +	:	CAsyncRequest(aSession, 0, aMessage),
    1.55 +		iPolicyCacheHandle(aPolicyCacheHandle),
    1.56 +		iUpdateDbHandle(UpsServer()->iDbHandle, this)
    1.57 +	{
    1.58 +	}
    1.59 +
    1.60 +CPolicyChangeEvaluator::~CPolicyChangeEvaluator()
    1.61 +/**
    1.62 +	Normally cleanup should be done when DoCleanup function is called by the framework.
    1.63 +	Sometime later, possibly after our parent CUpsSession has been deleted, this
    1.64 +	destructor will be run. In this case the framework will have cleared our iSession variable
    1.65 +	and we must do NOTHING.
    1.66 +
    1.67 +	Unfortunately there is a special case where this object fails inside ConstructL, when we must do 
    1.68 +	some cleanup. We can detect this be seeing iSession (and hence UpsServer()) is non-NULL.
    1.69 +*/
    1.70 +	{
    1.71 +	CUpsSession *session = UpsSession();
    1.72 +	if(session)
    1.73 +		{
    1.74 +		/*lint -save -e1506*/ // ignore warning about calling a virtual function in a destructor
    1.75 +		DoCleanup();
    1.76 +		/*lint -restore*/
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +
    1.81 +void CPolicyChangeEvaluator::StartUpdate()
    1.82 +	/// Starts evaluating the database view
    1.83 +	{
    1.84 +	iPolicyCacheHandle.NotifyOnRef1(iStatus);
    1.85 +	SetActive();
    1.86 +	}
    1.87 +
    1.88 +void CPolicyChangeEvaluator::ProcessEventL(TPolicyChangeEvent aEvent)
    1.89 +/**
    1.90 +	Process either a DoCancel call or a request completing through RunL
    1.91 +*/
    1.92 +	{
    1.93 +	switch(iState)
    1.94 +		{
    1.95 +		case EWaitForPolicyCacheIdle:
    1.96 +			WaitForPolicyCacheIdleStateL(aEvent);
    1.97 +			return;
    1.98 +			
    1.99 +		case EScanDatabase:
   1.100 +			ScanDatabaseStateL(aEvent);
   1.101 +			return;
   1.102 +BULLSEYE_OFF
   1.103 +		}
   1.104 +BULLSEYE_RESTORE
   1.105 +	
   1.106 +	// Should not get here
   1.107 +	_LIT(KServerPanicState, "UPS-CPolicyChangeEvaluator");
   1.108 +	User::Panic(KServerPanicState, iState);
   1.109 +	}
   1.110 +
   1.111 +void CPolicyChangeEvaluator::DoCleanup()
   1.112 +/// implement CAsyncRequest
   1.113 +	{
   1.114 +	Cancel();
   1.115 +
   1.116 +	iSSPVarray.Close();
   1.117 +	
   1.118 +	delete iUpdateView;
   1.119 +	iUpdateView = 0;
   1.120 +
   1.121 +	iUpdateDbHandle.Close();
   1.122 +
   1.123 +	iPolicyCacheHandle.Release();
   1.124 +	}
   1.125 +
   1.126 +void CPolicyChangeEvaluator::DoCancel()
   1.127 +	/// implement CActive - Cancel
   1.128 +	{
   1.129 +	TRAP_IGNORE(ProcessEventL(ECancel));
   1.130 +	}
   1.131 +
   1.132 +	
   1.133 +void CPolicyChangeEvaluator::RunL()
   1.134 +	/// implement CActive, override CAsyncRequset
   1.135 +	{
   1.136 +	User::LeaveIfError(iStatus.Int());
   1.137 +
   1.138 +	ProcessEventL(EInternalRequestComplete);
   1.139 +	}
   1.140 +
   1.141 +TInt CPolicyChangeEvaluator::RunError(TInt aError)
   1.142 +	{
   1.143 +	iUpdateDbHandle.CloseMaster();
   1.144 +	return CAsyncRequest::RunError(aError);
   1.145 +	}
   1.146 +
   1.147 +void CPolicyChangeEvaluator::DbHandleAboutToBeDeleted()
   1.148 +/**
   1.149 +	Called just before the master database handle is shut.
   1.150 +	Need to cancel and cleanup/delete our view and fail the client request.
   1.151 +*/
   1.152 +	{
   1.153 +	// Make sure our request is cancelled
   1.154 +	Cancel();
   1.155 +
   1.156 +	// Cleanup/delete our view object
   1.157 +	DoCleanup();
   1.158 +	
   1.159 +	// Abort the client view request.
   1.160 +	CompleteAndMarkForDeletion(KErrAbort);
   1.161 +	}
   1.162 +
   1.163 +void CPolicyChangeEvaluator::WaitForPolicyCacheIdleStateL(TPolicyChangeEvent aEvent)
   1.164 +	{
   1.165 +	switch(aEvent)
   1.166 +		{
   1.167 +		case EInternalRequestComplete:
   1.168 +			{
   1.169 +			// Cache is idle so now create the view and walk the database
   1.170 +			CDecisionFilter *filter= CDecisionFilter::NewLC();
   1.171 +			iUpdateView = iUpdateDbHandle->CreateViewL(*filter);
   1.172 +			CleanupStack::PopAndDestroy(filter);
   1.173 +
   1.174 +			// Update state and start evaluating the view
   1.175 +			iState = EScanDatabase;
   1.176 +			iUpdateView->EvaluateView(iStatus);
   1.177 +			SetActive();
   1.178 +			break;
   1.179 +			}
   1.180 +		case ECancel:
   1.181 +			iPolicyCacheHandle.CancelNotifyOnRef1();
   1.182 +			break;
   1.183 +BULLSEYE_OFF
   1.184 +		}
   1.185 +BULLSEYE_RESTORE
   1.186 +
   1.187 +	}
   1.188 +
   1.189 +void CPolicyChangeEvaluator::ScanDatabaseStateL(TPolicyChangeEvent aEvent)
   1.190 +	{
   1.191 +	switch(aEvent)
   1.192 +		{
   1.193 +		case EInternalRequestComplete:
   1.194 +			{
   1.195 +			// Finished evaluating the view
   1.196 +			BuildServerServicePolicyVersionTableL();
   1.197 +			
   1.198 +			// If the server policy cache handle has not already been
   1.199 +			// re-opened, open it.
   1.200 +			if(!UpsServer()->iPolicyCache.IsOpen())
   1.201 +				{
   1.202 +				UpsServer()->iPolicyCache.OpenL();
   1.203 +				}
   1.204 +			// Assign the server handle to ours - this will increment the reference count.
   1.205 +			iPolicyCacheHandle = UpsServer()->iPolicyCache;
   1.206 +
   1.207 +			// Set the property, the value does not matter.
   1.208 +			// We do not need to change the value, setting to the same value still notifies all clients of
   1.209 +			// an update.
   1.210 +			User::LeaveIfError(RProperty::Set(KUpsServerUid, KUpsServiceConfigProperty, 42));
   1.211 +
   1.212 +			// Process the iSSPVarray and delete uneeded database entries
   1.213 +			DeleteUnneededEntriesL();
   1.214 +
   1.215 +			// All done.
   1.216 +			CompleteAndMarkForDeletion(KErrNone);
   1.217 +			break;
   1.218 +			}
   1.219 +		case ECancel:
   1.220 +			iUpdateView->Cancel();
   1.221 +			break;
   1.222 +BULLSEYE_OFF
   1.223 +		}
   1.224 +BULLSEYE_RESTORE
   1.225 +	}
   1.226 +
   1.227 +
   1.228 +
   1.229 +TInt CPolicyChangeEvaluator::OrderServerServicePolicyVersion(const TServerServicePolicyVersion &aLhs,
   1.230 +									 			 						 const TServerServicePolicyVersion &aRhs)
   1.231 +{
   1.232 +	TInt t = aLhs.iServerId - aRhs.iServerId;
   1.233 +	if(t) return t;
   1.234 +	t = aLhs.iServiceId - aRhs.iServiceId;
   1.235 +	if(t) return t;
   1.236 +	t = aLhs.iMajorVersion - aRhs.iMajorVersion;
   1.237 +	return t;
   1.238 +}
   1.239 +
   1.240 +void CPolicyChangeEvaluator::BuildServerServicePolicyVersionTableL()
   1.241 +	{
   1.242 +	TLinearOrder<TServerServicePolicyVersion> linearOrder(OrderServerServicePolicyVersion);
   1.243 +
   1.244 +	while(CDecisionRecord *record = iUpdateView->NextDecisionL())
   1.245 +		{
   1.246 +		TServerServicePolicyVersion ent = { record->iServerSid.iId, 
   1.247 +											record->iServiceId.iUid,
   1.248 +											record->iMajorPolicyVersion };
   1.249 +		delete record;
   1.250 +		record = 0;
   1.251 +		TInt r = iSSPVarray.InsertInOrder(ent, linearOrder);
   1.252 +		if((r != KErrNone) && (r != KErrAlreadyExists))
   1.253 +			{
   1.254 +			User::Leave(r);
   1.255 +			}
   1.256 +		}
   1.257 +
   1.258 +
   1.259 +	}
   1.260 +
   1.261 +static TBool CompareServiceId(const TServiceId* k, const TServiceConfig& t)
   1.262 +	{
   1.263 +	return TUint(k->iUid) == t.iServiceId;
   1.264 +	}
   1.265 +
   1.266 +void CPolicyChangeEvaluator::DeleteUnneededEntriesL()
   1.267 +	{
   1.268 +	TInt count = iSSPVarray.Count();
   1.269 +
   1.270 +	if(count == 0) return; // No database entries!
   1.271 +
   1.272 +	TSecureId serverSid(0); // ID of server currently being processed. 0 is illegal
   1.273 +	RArray<TServiceConfig> serviceConfigArray;
   1.274 +	CleanupClosePushL(serviceConfigArray);
   1.275 +	for(TInt i=0; i<count; ++i)
   1.276 +		{
   1.277 +		const TServerServicePolicyVersion &sspv = iSSPVarray[i];
   1.278 +		TServiceId serviceId = {sspv.iServiceId};
   1.279 +
   1.280 +		// The iSSPVarray is sorted by server/service/version. This means
   1.281 +		// all entries for a single server will be adjacent to each other
   1.282 +		if(serverSid != sspv.iServerId)
   1.283 +			{
   1.284 +			// 0 is not a valid server SID, so this will always run the first time
   1.285 +			// through the loop.
   1.286 +			serviceConfigArray.Close();
   1.287 +			serverSid = sspv.iServerId;
   1.288 +			iPolicyCacheHandle->ServiceConfigL(serverSid, serviceConfigArray);
   1.289 +			}
   1.290 +
   1.291 +		CDecisionFilter *filter = CDecisionFilter::NewLC();
   1.292 +		filter->SetServerSid(serverSid, EEqual);
   1.293 +		filter->SetServiceId(serviceId, EEqual);
   1.294 +
   1.295 +		// Now attempt to lookup the current ServiceId in the serviceconfig for 
   1.296 +		// the current server.
   1.297 +		TInt serviceIndex = serviceConfigArray.Find(serviceId, CompareServiceId);
   1.298 +		if(serviceIndex != KErrNotFound)
   1.299 +			{
   1.300 +			DEBUG_PRINTF5(_L8("i=%d Found server %x service %x at index %d\n"),
   1.301 +							i, sspv.iServerId, sspv.iServiceId, serviceIndex);
   1.302 +			TServiceConfig &serviceConfig = serviceConfigArray[serviceIndex];
   1.303 +			filter->SetMajorPolicyVersion(serviceConfig.iMajorVersion, ENotEqual);
   1.304 +			DEBUG_PRINTF4(_L8("Deleting OLD decisions where server=0x%x service=0x%x MajorVersion!=%d\n"),
   1.305 +							serverSid.iId, serviceId.iUid, serviceConfig.iMajorVersion);
   1.306 +			}
   1.307 +		else
   1.308 +			{
   1.309 +			DEBUG_PRINTF3(_L8("Deleting ALL decisions where server=0x%x service=0x%x MajorVersion==*\n"),
   1.310 +							serverSid.iId, serviceId.iUid);
   1.311 +			}
   1.312 +
   1.313 +		TRAP_IGNORE(iUpdateDbHandle->RemoveDecisionsL(*filter));
   1.314 +		CleanupStack::PopAndDestroy(filter);
   1.315 +			
   1.316 +		};
   1.317 +	CleanupStack::PopAndDestroy(&serviceConfigArray);
   1.318 +
   1.319 +	}
   1.320 +
   1.321 +} // End of namespace UserPromptServer
   1.322 +// End of file
   1.323 +
   1.324 +