os/security/authorisation/userpromptservice/policies/test/testpolicyevaluator/source/testpolicyevaluator.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * refpolicyevaluator.cpp
    16 *
    17 */
    18 
    19 
    20 #include "testpolicyevaluator.h"
    21 #include <ecom/implementationproxy.h>
    22 #include <ups/cliententity.h>
    23 #include <ups/fingerprint.h>
    24 #include <ups/upsdb.h>
    25 
    26 using namespace UserPromptService;
    27 
    28 static const TUint KTestPolicyEvaluatorImplementationId = 0x10283698;
    29 
    30 CPolicyEvaluator* CTestPolicyEvaluator::CreatePolicyEvaluatorL()
    31 /**
    32 Factory method that instantiates a new policy evaluator ECOM plug-in.
    33 
    34 @return A pointer to the new reference policy evaluator object.
    35 */
    36 	{
    37 	CTestPolicyEvaluator* self = new (ELeave)CTestPolicyEvaluator();
    38 	CleanupStack::PushL(self);
    39 	self->ConstructL();
    40 	CleanupStack::Pop(self);
    41 	return self;
    42 	}
    43 
    44 static const TImplementationProxy ImplementationTable[] = 
    45 	{
    46 	IMPLEMENTATION_PROXY_ENTRY(KTestPolicyEvaluatorImplementationId, CTestPolicyEvaluator::CreatePolicyEvaluatorL)
    47 	};
    48 
    49 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
    50 /**
    51 Standard ECOM factory
    52 */
    53 	{
    54 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
    55 	return ImplementationTable;
    56 	}	
    57 	
    58 
    59 CTestPolicyEvaluator::CTestPolicyEvaluator()
    60 /**
    61 Constructor
    62 */
    63 	: CPolicyEvaluator()
    64 	{
    65 	CActiveScheduler::Add(this);
    66 	}
    67 	
    68 CTestPolicyEvaluator::~CTestPolicyEvaluator()
    69 /**
    70 Destructor
    71 */
    72 	{
    73 	Deque();
    74 	delete iDigest;
    75 	delete iClientEntity;
    76 	}
    77 
    78 void CTestPolicyEvaluator::ConstructL()
    79 /**
    80 Second phase constructor, creates the message digest
    81 */
    82 	{	
    83 	iDigest = CMessageDigestFactory::NewDigestL(CMessageDigest::EMD5);
    84 	}
    85 
    86 
    87 // From CActive
    88 void CTestPolicyEvaluator::DoCancel()
    89 	{	
    90 	// Logically should Cancel the internal outstanding requst, but
    91 	// currently GenerateFingerprints has already completed it.
    92 
    93 	// And need to complete the clients request
    94 	if (iClientStatus)
    95 		{
    96 		User::RequestComplete(iClientStatus, KErrCancel);
    97 		}
    98 	}
    99 	
   100 TInt CTestPolicyEvaluator::RunError(TInt aError)
   101 	{
   102 	if (iClientStatus)
   103 		{
   104 		User::RequestComplete(iClientStatus, aError);
   105 		}
   106 	return KErrNone;
   107 	}
   108 	
   109 void CTestPolicyEvaluator::RunL()
   110 	{
   111 	// This plug-in doesn't use the opaque data	
   112 	TInt opaqueDataLen = iRequest->OpaqueData().Length();
   113 	if (opaqueDataLen > 0)
   114 		{
   115 		RDebug::Printf("Received %d bytes of opaque data", opaqueDataLen);
   116 		}
   117 		
   118 	SetClientEntityL();
   119 	
   120 	// Create most specific hash first i.e. HASH(destination)
   121 	// N.B. Normally, the destination field should be parsed so that only relevant data is hashed.		
   122 	iDigest->Reset();
   123 	const TDesC& d = iRequest->Destination();
   124 	TPtrC8 p(reinterpret_cast<const TUint8*>(d.Ptr()), d.Length() * 2);
   125 	TPtrC8 h(iDigest->Hash(p));
   126 	
   127 	CFingerprint* f = CFingerprint::NewLC(h, d);
   128 	iFingerprints->AppendL(f);
   129 	CleanupStack::Pop(f);
   130 
   131 	// An empty fingerprint may be used for decisions that apply to
   132 	// all destinations.
   133 	f = CFingerprint::NewLC(KNullDesC8, KNullDesC);
   134 	iFingerprints->AppendL(f);
   135 	CleanupStack::Pop(f);
   136 		
   137 	User::RequestComplete(iClientStatus, KErrNone);
   138 	}
   139 
   140 void CTestPolicyEvaluator::SetClientEntityL()
   141 /**
   142 Sets the name of the entity within the client process that made the request.
   143 This is optional functionality designed to allow permissions to be set for scripts
   144 where the scripting host process can execute a number of different scripts.
   145 */
   146 	{
   147 	// This doesn't really support client entities, so for testing purposes we
   148 	// extract the client entity name from the opaque data.
   149 	_LIT8(KClientEntityTagStart, "<ce>");
   150 	_LIT8(KClientEntityTagEnd, "</ce>");
   151 	TInt startPos;
   152 	TInt endPos;
   153 	if ((startPos = iRequest->OpaqueData().FindF(KClientEntityTagStart)) != KErrNotFound)
   154 		{
   155 		startPos += KClientEntityTagStart().Length();
   156 		if ((endPos = iRequest->OpaqueData().FindF(KClientEntityTagEnd)) != KErrNotFound)	
   157 			{
   158 			if (endPos > startPos)	
   159 				{
   160 				TPtrC8 entityName = iRequest->OpaqueData().Mid(startPos, endPos - startPos);
   161 				iClientEntity = CClientEntity::NewL(entityName);
   162 				*iClientEntityPtr = iClientEntity;			
   163 				}	
   164 			}
   165 		}
   166 	}
   167 	
   168 void CTestPolicyEvaluator::GenerateFingerprints(
   169 	const CPromptRequest& aRequest, const CPolicy& aPolicy, 
   170 	RPointerArray<CFingerprint>& aFingerprints, const CClientEntity*& aClientEntity, 
   171 	const TAny*& aDialogCreatorParams, TRequestStatus& aStatus)
   172 	{
   173 	iRequest = &aRequest;
   174 	iPolicy = &aPolicy;
   175 	iFingerprints = &aFingerprints;		
   176 	iClientEntityPtr = &aClientEntity;	
   177 	aDialogCreatorParams = 0;			
   178 	
   179 	iClientStatus = &aStatus;
   180 	aStatus = KRequestPending;
   181 	
   182 	// Kick off policy evaluator state machine
   183 	iStatus = KRequestPending;
   184 	TRequestStatus* status = &iStatus;
   185 	SetActive();
   186 	User::RequestComplete(status, KErrNone);
   187 	}
   188 
   189 TBool CTestPolicyEvaluator::ForcePromptL(const CDecisionRecord& aDecision, TUint& aNewEvaluatorInfo)
   190 	{
   191 	(void) aDecision;
   192 	aNewEvaluatorInfo = aDecision.iEvaluatorInfo + 10;
   193 	return EFalse;
   194 	}