persistentstorage/centralrepository/cenrepsrv/srvrepos_noc.h
changeset 0 08ec8eefde2f
child 1 c084286672be
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15  
       
    16 #ifndef SRVREPOS_H
       
    17 #define SRVREPOS_H
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <s32file.h>
       
    21 #ifdef SYMBIAN_BAFL_SYSUTIL
       
    22 #include <bafl/sysutil.h>
       
    23 #endif
       
    24 #include "shrepos.h"
       
    25 #include "setting.h"
       
    26 #include "clientrequest.h"
       
    27 #include "sessnotf.h"
       
    28 #include "operations.h"
       
    29 #include "datatype.h"
       
    30 
       
    31 class MObserver;
       
    32 class CRestoredRepository;
       
    33 
       
    34 class CServerRepository : public CRepositoryTransactor, public MOperationLogic
       
    35 	{
       
    36 public:
       
    37 	void OpenL(TUid aUid, MObserver& aObserver, TBool aFailIfNotFound = ETrue);
       
    38 	void HandleOpenMergeL();
       
    39 	void Close();
       
    40 	void RestoreNotify(const CRestoredRepository& aRstRepos);
       
    41 	TInt RFSRepositoryL();
       
    42 #ifdef SYMBIAN_BAFL_SYSUTIL
       
    43 	static void CheckROMReflashL();
       
    44 #endif
       
    45 	static void RFSAllRepositoriesL();
       
    46 
       
    47 	inline TInt StartTransaction(TInt aMode)
       
    48 		{
       
    49 		return iRepository->StartTransaction(*this, aMode);
       
    50 		}
       
    51 	inline TInt CommitTransaction(TUint32& aKeyInfo)
       
    52 		{
       
    53 		return iRepository->CommitTransaction(*this, aKeyInfo);
       
    54 		}
       
    55 	inline void CancelTransaction() // serves as both rollback and async cancel
       
    56 		{
       
    57 		iRepository->CancelTransaction(*this);
       
    58 		}
       
    59 	void CleanupCancelTransactionPushL();
       
    60 	inline TInt FailTransaction(TInt aError, TUint32 aErrorKey)
       
    61 		{
       
    62 		// returns aError to allow "return FailTransaction(error, errorKey);" - error written once
       
    63 		return iRepository->FailTransaction(*this, aError, aErrorKey);
       
    64 		}
       
    65 
       
    66 	inline void FailAllTransactions()
       
    67 		{
       
    68 		iRepository->FailAllTransactions(NULL);
       
    69 		}
       
    70 	/** must currently be in active Read transaction. Does not fail
       
    71 	transaction here if promotion to read/write failed.
       
    72 	@return KErrNone if promoted, KErrLocked if not
       
    73 	*/
       
    74 	inline TInt AttemptPromoteTransactionToReadWrite()
       
    75 		{
       
    76 		ASSERT(IsInActiveReadTransaction());
       
    77 		return iRepository->AttemptPromoteTransactionToReadWrite(*this);
       
    78 		}
       
    79 
       
    80 	inline TServerSetting* GetPersistentSetting(TUint32 aId)
       
    81 		{
       
    82 		return iRepository->GetSettings().Find(aId);
       
    83 		}
       
    84 	inline TServerSetting* GetTransactionSetting(TUint32 aId)
       
    85 		{
       
    86 		return iTransactionSettings.Find(aId);
       
    87 		}
       
    88 
       
    89 	template <class T> 
       
    90 	TInt TransactionCreateL(TUint32 aKey, const T& aVal, TUint32* aMeta);
       
    91 
       
    92 	template <class T>
       
    93 	TInt TransactionSetL(TUint32 aKey, const T& aVal);
       
    94 
       
    95 	inline TInt TransactionDeleteL(TUint32 aId)
       
    96 		{
       
    97 		// all write operations now done in a transaction
       
    98 		ASSERT(IsInActiveReadWriteTransaction());
       
    99 		TRAPD(err,DeleteSettingL(aId));
       
   100 		if (err==KErrNoMemory)
       
   101 			User::LeaveNoMemory();
       
   102 		return err;
       
   103 		}
       
   104 		
       
   105 	TInt TransactionMoveL(const TClientRequest& aMessage, TUint32& aErrorKey);
       
   106 	TInt TransactionDeleteRangeL(const TClientRequest& aMessage, TUint32& aErrId);
       
   107 
       
   108 	TInt CheckPermissions(RSettingPointerArray& aSettings, const TClientRequest& aMessage, const char* aDiagnostic,TBool aReadPolicy,TUint32& aErrId);
       
   109 	
       
   110 	TInt CheckAccessPolicyBeforeMoving(const TClientRequest& aMessage, const TServerSetting& aSourceSetting, TUint32 aSourceKey, 
       
   111 							const TServerSetting& aTargetSetting, TUint32 aTargetKey, TUint32& aErrorKey);
       
   112 								
       
   113 	TInt CheckMovePermissions(const RSettingPointerArray& aSourceSettings,const TClientRequest& aMessage,TUint aSourceToTarget,TUint32& aErrorKey)
       
   114 		{
       
   115 		TInt error=KErrNone;
       
   116 		for (TInt i=0;i<aSourceSettings.Count() && error==KErrNone;i++)	
       
   117 			{
       
   118 			ASSERT(aSourceSettings[i]);
       
   119 			TServerSetting* sourceSetting = aSourceSettings[i];
       
   120 			TUint32 sourceKey = sourceSetting->Key();
       
   121 			TUint32 targetKey = sourceKey ^ aSourceToTarget;
       
   122 			TServerSetting* targetSetting = GetSetting(targetKey);
       
   123 		
       
   124 			error = CheckAccessPolicyBeforeMoving(aMessage, *sourceSetting, sourceKey, *targetSetting, targetKey, aErrorKey);		
       
   125 			}
       
   126 		return error;
       
   127 		}
       
   128 
       
   129 	inline TInt FindPersistentSettings(TUint32 aPartialId, TUint32 aIdMask, RSettingPointerArray& aMatches) const
       
   130 		{
       
   131 		// guarantees to reset RSettingPointerArray in case of error
       
   132 		return iRepository->GetSettings().Find(aPartialId, aIdMask, aMatches);
       
   133 		}
       
   134 	inline TInt FindTransactionSettings(TUint32 aPartialId, TUint32 aIdMask, RSettingPointerArray& aMatches) const
       
   135 		{
       
   136 		// guarantees to reset RSettingPointerArray in case of error
       
   137 		return iTransactionSettings.Find(aPartialId, aIdMask, aMatches);
       
   138 		}
       
   139 
       
   140 	inline void CommitChangesL(TCentRepLocation aLocation = EPersists) const
       
   141 		{
       
   142 		iRepository->CommitChangesL(aLocation);
       
   143 		}
       
   144 
       
   145 	TInt ResetL(TUint32 aId);
       
   146 	TInt ResetAllL();
       
   147 
       
   148 	inline const TSecurityPolicy& GetReadAccessPolicy(const TServerSetting& aSetting) const
       
   149 		{
       
   150 		return iRepository->GetReadAccessPolicy(aSetting);
       
   151 		}
       
   152 	inline const TSecurityPolicy& GetReadAccessPolicy(TUint32 aId) const
       
   153 		{
       
   154 		return iRepository->GetReadAccessPolicy(aId);
       
   155 		}
       
   156 	inline const TSecurityPolicy& GetDefaultReadAccessPolicy() const
       
   157 		{
       
   158 		return iRepository->GetDefaultReadAccessPolicy();
       
   159 		}
       
   160 
       
   161 	inline const TSecurityPolicy& GetFallbackWriteAccessPolicy(TUint32 aId) const
       
   162 		{
       
   163 		return iRepository->GetFallbackWriteAccessPolicy(aId);
       
   164 		}
       
   165 
       
   166 	inline const TSecurityPolicy& GetWriteAccessPolicy(const TServerSetting& aSetting) const
       
   167 		{
       
   168 		return iRepository->GetWriteAccessPolicy(aSetting);
       
   169 		}
       
   170 	inline const TSecurityPolicy& GetWriteAccessPolicy(TUint32 aId) const
       
   171 		{
       
   172 		return iRepository->GetWriteAccessPolicy(aId);
       
   173 		}
       
   174 	inline const TSecurityPolicy& GetDefaultWriteAccessPolicy() const
       
   175 		{
       
   176 		return iRepository->GetDefaultWriteAccessPolicy();
       
   177 		}
       
   178 
       
   179 	void HandleSWIUpdateL(TUid aUid, TTime aModified, CSessionNotifier& aNotifier);
       
   180 	void HandleSWIDeleteL(TUid aUid, CSessionNotifier& aNotifier);
       
   181 
       
   182 	void StoreRepositoryContentsL(CStreamStore & aStore, TStreamId & aSettingStreamId, TStreamId & aDeletedSettingsStreamId) const;
       
   183 	void StoreRepositorySettingValuesL(CStreamStore& aStore, TStreamId & aSettingStreamId) const;	
       
   184 	void RestoreRepositoryContentsL(CStreamStore& aStore, TStreamId aSettingStreamId, TStreamId aDeletedSettingsStreamId, CRestoredRepository& aRstRepos);
       
   185 	void RestoreRepositorySettingValuesL(CStreamStore& aStore, TStreamId aSettingStreamId, CRestoredRepository& aRstRepos);
       
   186 
       
   187 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
       
   188 	void ResetFromIniFileL(TUint32 aId,CIniFileIn::TIniFileOpenMode aIniFileOpenMode,TBool& aKeyFound);
       
   189 #else	
       
   190 	void ResetFromIniFileL(TUint32 aId,TCentRepLocation aLocation,TBool& aKeyFound);
       
   191 #endif	
       
   192 	
       
   193 	void LoadIniRepL(CIniFileIn::TIniFileOpenMode aMode);
       
   194 	TBool GetMetaFromIni(TUint32 aKey, TUint32& aMeta);
       
   195 	
       
   196 	void RestoreInstallRepositoryL(TUid aUid, CStreamStore& aStore, TStreamId& aSettingStreamId, CRestoredRepository& aRstRepos); 
       
   197 	void BackupInstallRepositoryL(TUid aUid, CStreamStore& aStore, TStreamId& aSettingStreamId); 
       
   198 
       
   199 
       
   200 #ifdef CACHE_OOM_TESTABILITY
       
   201 	TInt SizeiRepository()
       
   202 		{
       
   203 		return iRepository->Size();
       
   204 		}
       
   205 #endif	
       
   206 	
       
   207 	inline MObserver* Notifier()
       
   208 		{
       
   209 		ASSERT(iNotifier);
       
   210 		return iNotifier;
       
   211 		}
       
   212 	
       
   213 	inline void AccessRepositoryL()
       
   214 		{
       
   215 		iRepository = TServerResources::iObserver->AccessL(iUid);
       
   216 		}
       
   217 
       
   218 private:
       
   219 	enum TPersistedRepActions {ECenRepReflash,ECenRepReset};
       
   220 	
       
   221 private:
       
   222 	static void ProcessPersistsRepositoriesL(TPersistedRepActions aRomFlashOrReset);
       
   223 	static void CacheRomVersionL(const TDesC& aFilename, TDesC8& aVersion);
       
   224 	TInt HandleReflashofRepositoryL();
       
   225 
       
   226 	//--------------------virtual functions from MOperation----------------------------
       
   227 public:
       
   228 	void GetSingleMeta(TUint aKey,TUint32& aMeta)
       
   229 		{
       
   230 		TUint32 metaFromIni;
       
   231 		if (GetMetaFromIni(aKey, metaFromIni))
       
   232 			{
       
   233 			aMeta=~KMetaDefaultValue & metaFromIni;
       
   234 			}
       
   235 		else
       
   236 			{
       
   237 			//First check for a matching "range" default
       
   238 			TSettingsDefaultMeta* defaultMeta = iRepository->iSimRep->RangeMetaArray().Find(aKey);
       
   239 			if (defaultMeta)
       
   240 				{
       
   241 				aMeta=defaultMeta->GetDefaultMetadata();
       
   242 				}
       
   243 			else
       
   244 				{
       
   245 				// Range value not found, try for a repository default
       
   246 				aMeta=iRepository->iSimRep->DefaultMeta();
       
   247 				}			
       
   248 			}		
       
   249 		}
       
   250 		
       
   251 	TSettingsAccessPolicy* GetFallbackAccessPolicy(TUint32 aId) const
       
   252 		{
       
   253 		return iRepository->GetFallbackAccessPolicy(aId);		
       
   254 		}	
       
   255 
       
   256 	RSettingsArray& GetWritableSettingList()
       
   257 		{
       
   258 		return iTransactionSettings;
       
   259 		}
       
   260 
       
   261 	TServerSetting* GetSetting(TUint aKey)
       
   262 		{
       
   263 		// try to be most efficient when no transaction changes
       
   264 		if ((iTransactionSettings.Count() > 0) && IsInActiveReadWriteTransaction())
       
   265 			{
       
   266 			TServerSetting* s = GetTransactionSetting(aKey);
       
   267 			if (s)
       
   268 				{
       
   269 				return (s);
       
   270 				}
       
   271 			}
       
   272 		return GetPersistentSetting(aKey);
       
   273 		}
       
   274 
       
   275 	/** Returns pointer array of settings whose keys match the partial key and mask. Combines
       
   276 	settings from the persistent list with those in the transaction, with priority given to the
       
   277 	latter, including settings flagged as deleted eliminating the corresponding entry from the 
       
   278 	persistent settings (plus themselves so the final list has no settings flagged as deleted in it).
       
   279 	Can also call this method when not in a transaction.
       
   280 	In case of error, aMatches may contain entries and must be Reset.
       
   281 	*/
       
   282 	TInt FindSettings(TUint32 aSourcePartialKey,TUint32 aMask,RSettingPointerArray& aOutputArray) const
       
   283 		{
       
   284 		TInt error = FindPersistentSettings(aSourcePartialKey, aMask, aOutputArray);
       
   285 		// try to be most efficient when no transaction changes
       
   286 		if ((iTransactionSettings.Count() > 0) && IsInActiveReadWriteTransaction() && (KErrNone == error))
       
   287 			{
       
   288 			RSettingPointerArray transactionSettings;
       
   289 			error = FindTransactionSettings(aSourcePartialKey, aMask, transactionSettings);
       
   290 			if (error == KErrNone)
       
   291 				{
       
   292 				error = RSettingsArray::Merge(aOutputArray, transactionSettings);
       
   293 				}
       
   294 			transactionSettings.Reset();
       
   295 			}
       
   296 		return error;
       
   297 		}
       
   298 	
       
   299 	//---------------------end of virtual functions-------------------------------------------	
       
   300 #ifndef FOTA_UNIT_TESTING
       
   301 private:
       
   302 #endif
       
   303 	CSharedRepository* iRepository;
       
   304 	MObserver* iNotifier;	
       
   305 	TUid iUid;
       
   306 	};
       
   307 
       
   308 template <class T>
       
   309 TInt CServerRepository::TransactionSetL(TUint32 aKey, const T& aVal)
       
   310 	{
       
   311 	// all write operations now done in a transaction
       
   312 	ASSERT(IsInActiveReadWriteTransaction());
       
   313 	
       
   314 	TRAPD(error,SetSettingL(aKey,aVal));
       
   315 	if (error != KErrNone)
       
   316 		{
       
   317 		FailTransaction(error, aKey);
       
   318 		}
       
   319 	return error;
       
   320 	}
       
   321 	
       
   322 
       
   323 template <class T>
       
   324 TInt CServerRepository::TransactionCreateL(TUint32 aKey, const T& aVal, TUint32* aMeta)
       
   325 	{
       
   326 	// all write operations now done in a transaction
       
   327 	ASSERT(IsInActiveReadWriteTransaction());
       
   328 	
       
   329 	TRAPD(err,CreateSettingL(aKey,aVal,aMeta));
       
   330 	if (err==KErrAlreadyExists)
       
   331 		{
       
   332 		return FailTransaction(KErrAlreadyExists,aKey);
       
   333 		}
       
   334 	User::LeaveIfError(err);
       
   335 	
       
   336 	return KErrNone;
       
   337 	}
       
   338 
       
   339 #endif // SRVREPOS_H
       
   340 
       
   341