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