os/security/cryptoservices/certificateandkeymgmt/certstore/certificateapps.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1998-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 *
    16 */
    17 
    18 
    19 #include "CFSTokenTypeClient.h"
    20 #include <certificateapps.h>
    21 #include <ct/mcttoken.h>
    22 #include <mctcertapps.h>
    23 #include "mctcertappinterface.h"
    24 
    25 
    26 ////////////////////////////////////////////////////////////////
    27 //	CCertificateAppInfoManager
    28 ////////////////////////////////////////////////////////////////
    29 
    30 EXPORT_C CCertificateAppInfoManager* CCertificateAppInfoManager::NewLC()
    31 	{
    32 	CCertificateAppInfoManager* self = new(ELeave) CCertificateAppInfoManager();
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL();
    35 	return self;
    36 	}
    37 
    38 EXPORT_C CCertificateAppInfoManager* CCertificateAppInfoManager::NewL()
    39 	{
    40 	CCertificateAppInfoManager* self = NewLC();
    41 	CleanupStack::Pop();
    42 	return self;
    43 	}
    44 
    45 // deprecated
    46 EXPORT_C CCertificateAppInfoManager* CCertificateAppInfoManager::NewLC(RFs& /*aFs*/,
    47 																	   TBool /*aOpenedForWrite*/)
    48 	{
    49 	return NewLC();
    50 	}
    51 
    52 // deprecated
    53 EXPORT_C CCertificateAppInfoManager* CCertificateAppInfoManager::NewL(RFs& /*aFs*/,
    54 																	  TBool /*aOpenedForWrite*/)
    55 	{
    56 	return NewL();
    57 	}
    58 
    59 EXPORT_C CCertificateAppInfoManager::~CCertificateAppInfoManager()
    60 	{
    61 	iClients.Close();
    62 	if (iCertAppsIf)
    63 		{
    64 		iCertAppsIf->Release();
    65 		}
    66 	}
    67 
    68 CCertificateAppInfoManager::CCertificateAppInfoManager()
    69 	{
    70 	}
    71 
    72 void CCertificateAppInfoManager::ConstructL()
    73 	{
    74 	// This method is the second phase of the construction process.
    75 	//
    76 	// It will open the cert apps token type, get the token, and 
    77 	// then get the token interface.
    78 	//
    79 	// This class is not an active object but we can safely
    80 	// wait for requests to complete because the filetokens
    81 	// server completes requests immediately
    82 	MCTTokenType* tokenType = CFSTokenTypeClient::NewL(TUid::Uid(KTokenTypeCertApps));
    83 	CleanupReleasePushL(*tokenType);
    84 
    85 	// Now extract all the tokens this token type contains
    86 	RCPointerArray<HBufC> tokenArray;
    87 	CleanupClosePushL(tokenArray);
    88 
    89 	TRequestStatus stat;
    90 	tokenType->List(tokenArray, stat);
    91 	User::WaitForRequest(stat);
    92 
    93 	// make sure we have at least one token, otherwise leave
    94 	User::LeaveIfError(stat.Int());
    95 	__ASSERT_DEBUG(tokenArray.Count(), User::Panic(_L("CCertificateAppInfoManager"), 1));
    96 
    97 	MCTToken* token = NULL;
    98 
    99 	// We assume the 1st token is the one we want
   100 	tokenType->OpenToken(*tokenArray[0], token, stat);
   101 	User::WaitForRequest(stat);
   102 	User::LeaveIfError(stat.Int());
   103 	CleanupReleasePushL(*token);
   104 
   105 	// Now try and get the appropriate token interface
   106 	MCTTokenInterface* tokenIf = NULL;
   107 	token->GetInterface(TUid::Uid(KInterfaceCertApps), tokenIf, stat);
   108 	User::WaitForRequest(stat);
   109 	User::LeaveIfError(stat.Int());
   110 	__ASSERT_DEBUG(tokenIf, User::Panic(_L("CCertificateAppInfoManager"), 1));
   111 
   112 	// now upcast to a certapps interface. This should be a fairly safe cast
   113 	// since we specifically requested for this interface
   114 	iCertAppsIf = static_cast<MCTCertApps*>(tokenIf);
   115 
   116 	// Now we can release the token and the token type and destroy the token
   117 	// array
   118 	token->Release();
   119 	tokenType->Release();
   120 	tokenArray.Close();
   121 
   122 	// Pop the stuff from the cleanup stack - could have done a
   123 	// PopAndDestroy instead of Release()/Close() but thought I'd be 
   124 	// more explicit
   125 	CleanupStack::Pop(3);
   126 
   127 	// Populate the applications array
   128 	iCertAppsIf->ApplicationsL(iClients);
   129 	}
   130 
   131 EXPORT_C void CCertificateAppInfoManager::AddL(const TCertificateAppInfo& aClient)
   132 	{
   133 	// We have to update our cached applications array, but must keep this in
   134 	// sync with the server in the face of leaves and OOM
   135 	User::LeaveIfError(iClients.Append(aClient));
   136 	TRAPD(err, iCertAppsIf->AddL(aClient));
   137 	if (err != KErrNone)
   138 		{
   139 		iClients.Remove(iClients.Count() - 1);
   140 		User::Leave(err);
   141 		}
   142 	}
   143 
   144 EXPORT_C void CCertificateAppInfoManager::RemoveL(const TUid& aUid)
   145 	{
   146 	// We have to update our cached applications array, but must keep this in
   147 	// sync with the server in the face of leaves and OOM
   148 	iCertAppsIf->RemoveL(aUid);
   149 	
   150 	// Count backwards so we don't have to worry about the size changing
   151 	for (TInt i = iClients.Count() - 1 ; i >= 0 ; --i)
   152 		{
   153 		if (iClients[i].Id() == aUid)
   154 			{
   155 			iClients.Remove(i);
   156 			}
   157 		}
   158 	}
   159 
   160 EXPORT_C const TCertificateAppInfo& CCertificateAppInfoManager::ApplicationL(const TUid& aUid, TInt& aIndex) const
   161 	{
   162 	aIndex = KErrNotFound;
   163 	
   164 	for (TInt i = 0 ; i < iClients.Count() ; ++i)
   165 		{
   166 		if (iClients[i].Id() == aUid)
   167 			{
   168 			aIndex = i;
   169 			break;
   170 			}
   171 		}
   172 	
   173 	User::LeaveIfError(aIndex);
   174 	return iClients[aIndex];
   175 	}
   176 
   177 EXPORT_C const RArray<TCertificateAppInfo>& CCertificateAppInfoManager::Applications() const
   178 	{
   179 	return iClients;
   180 	}