authenticationservices/authenticationserver/source/client/asyncresponsedecoder.cpp
changeset 102 deec7e509f66
parent 94 0e6c5a9328b5
child 108 ca9a0fc2f082
equal deleted inserted replaced
94:0e6c5a9328b5 102:deec7e509f66
     1 /*
       
     2 * Copyright (c) 2005-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 * CAsyncResponseDecoder - Auth Client helper class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22 */
       
    23 
       
    24 #include <e32debug.h>
       
    25 #include <s32mem.h>
       
    26 #include "authclient_impl.h"
       
    27 
       
    28 using namespace AuthServer;
       
    29 
       
    30 namespace AuthServer
       
    31 {
       
    32 HBufC8* ExternalizeExpressionL(const CAuthExpression* aExpr);
       
    33 
       
    34 enum {  EPanicBadCmd = 1 };
       
    35 
       
    36 _LIT(KAsyncDecoderBusy, "AuthClient busy");
       
    37 
       
    38 }
       
    39 
       
    40 CAsyncResponseDecoder::CAsyncResponseDecoder(const RAuthClient& aSession) :
       
    41 	CActive(EPriorityStandard), iSession(aSession), 
       
    42 	iBufDes(0,0), iParamsPtr(0,0)
       
    43     {
       
    44 	CActiveScheduler::Add(this);
       
    45 	}
       
    46 
       
    47 CAsyncResponseDecoder::~CAsyncResponseDecoder()
       
    48     {
       
    49     Cleanup();
       
    50     }
       
    51 
       
    52 
       
    53 void CAsyncResponseDecoder::AuthenticateL(
       
    54 						   const CAuthExpression& aExpression,
       
    55 						   TTimeIntervalSeconds   aTimeout,
       
    56 						   TBool                  aClientSpecificKey,
       
    57 						   TUid 				  aClientSid,
       
    58 						   TBool                  aWithString,
       
    59 						   const TDesC& 		  aClientMessage,
       
    60 						   CIdentity*&            aIdentityResult, 
       
    61 						   TRequestStatus&        aClientStatus)
       
    62 	{
       
    63 	__ASSERT_ALWAYS(!IsActive(), User::Panic(AuthServer::KAsyncDecoderBusy, 0));
       
    64 	
       
    65 	iExpression       	     = &aExpression;
       
    66 	iResult            	     = &aIdentityResult;
       
    67 	iClientStatus            = &aClientStatus;
       
    68 	
       
    69 	//Create a flat buffer.
       
    70   	CBufFlat* flatBuffer = CBufFlat::NewL(KDefaultBufferSize);
       
    71   	CleanupStack::PushL(flatBuffer);
       
    72   	
       
    73   	//Initialize the CAuthParams object.
       
    74   	CAuthParams* params = CAuthParams::NewL(aTimeout,
       
    75   											aClientSpecificKey,
       
    76 											aClientSid,
       
    77   											aWithString,
       
    78   											aClientMessage);
       
    79   	CleanupStack::PushL(params);
       
    80   	
       
    81   	//Externalize params.
       
    82   	RBufWriteStream stream(*flatBuffer);
       
    83   	CleanupClosePushL(stream);
       
    84   	params->ExternalizeL(stream);
       
    85   	CleanupStack::PopAndDestroy(2, params);
       
    86   		
       
    87   	iParamsBuffer = HBufC8::NewL(flatBuffer->Size());
       
    88   	iParamsPtr.Set(iParamsBuffer->Des());
       
    89   	flatBuffer->Read(0, iParamsPtr, flatBuffer->Size());
       
    90   	CleanupStack::PopAndDestroy(flatBuffer);
       
    91   
       
    92 	//Externalize the expression.
       
    93   	iExprBuf = ExternalizeExpressionL(iExpression);
       
    94   
       
    95 	// allocate buffer for identity result.
       
    96 	iBuffer = HBufC8::NewL(KDefaultBufferSize);
       
    97 	iBufDes.Set(iBuffer->Des());
       
    98   
       
    99 	iArgs = new (ELeave)TIpcArgs(iExprBuf,
       
   100 								 &iParamsPtr,
       
   101 								 &iBufDes); 
       
   102 	StartCall(EAuthenticate);
       
   103 	}
       
   104 void CAsyncResponseDecoder::RegisterIdentityL(CIdentity*& aIdentity,
       
   105 											 const TDesC& aDescription,  
       
   106 											 TRequestStatus& aClientStatus)
       
   107 	{
       
   108 	__ASSERT_ALWAYS(!IsActive(), User::Panic(KAsyncDecoderBusy, 0));
       
   109 	
       
   110 	iBuffer = HBufC8::NewL(KDefaultBufferSize);
       
   111 	iBufDes.Set(iBuffer->Des());
       
   112 	iResult = &aIdentity;
       
   113 	iDescription = HBufC::NewL(aDescription.Size());
       
   114 	*iDescription = aDescription;
       
   115 	iClientStatus = &aClientStatus;
       
   116 	iArgs = new (ELeave)TIpcArgs(&iBufDes, iDescription);
       
   117   
       
   118 	StartCall(ERegisterIdentity);
       
   119 	}
       
   120 
       
   121 void CAsyncResponseDecoder::StartCall(TAuthServerMessages aCmd)
       
   122 	{
       
   123 	iCmd = aCmd;
       
   124 	iStatus = KRequestPending;
       
   125 	*iClientStatus = KRequestPending;
       
   126 	SetActive();
       
   127 	iSession.CallSessionFunction(iCmd, *iArgs, iStatus);
       
   128 	}
       
   129 
       
   130 void CAsyncResponseDecoder::RunL() 
       
   131 	{
       
   132 	if (iStatus.Int() == KErrNone)
       
   133 		{
       
   134 		RDesReadStream readStream(*iBuffer);
       
   135 		switch (iCmd)
       
   136 			{
       
   137 			case EAuthenticate:
       
   138 				*iResult = CIdentity::InternalizeL(readStream);
       
   139 				break;
       
   140 			case ERegisterIdentity:
       
   141 				*iResult = CIdentity::InternalizeL(readStream);
       
   142 				break;
       
   143 			default:
       
   144 				User::Panic(_L("AuthClient-AsyncResponseDecoder"),
       
   145 							EPanicBadCmd);
       
   146 			}
       
   147 		}
       
   148 	User::RequestComplete(iClientStatus,iStatus.Int());
       
   149 	Cleanup();
       
   150 	}
       
   151 
       
   152 TInt CAsyncResponseDecoder::RunError(TInt aError)
       
   153 	{	
       
   154 	User::RequestComplete(iClientStatus, aError);
       
   155 	Cleanup();    			
       
   156 	return KErrNone;
       
   157 	
       
   158 	}
       
   159 
       
   160 void CAsyncResponseDecoder::DoCancel()
       
   161 	{
       
   162 	switch (iCmd)
       
   163 		{
       
   164 		case EAuthenticate:
       
   165 		case ERegisterIdentity:
       
   166 			User::RequestComplete(iClientStatus, KErrCancel);
       
   167 			break;
       
   168 		default:
       
   169 		    break;
       
   170 		}
       
   171 	Cleanup();
       
   172     }
       
   173 
       
   174 void CAsyncResponseDecoder::Cleanup()
       
   175     {
       
   176 	delete iArgs;
       
   177 	iArgs = 0;
       
   178 	delete iExprBuf;
       
   179 	iExprBuf = 0;
       
   180 	delete iBuffer;
       
   181 	iBuffer = 0;
       
   182     delete iDescription;
       
   183     iDescription = 0;
       
   184     delete iParamsBuffer;
       
   185     iParamsBuffer = 0;
       
   186 	iCmd = ELastService;
       
   187 	iClientStatus = 0;
       
   188     }