Update contrib.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 #include "heaprepos.h"
20 CHeapRepository* CHeapRepository::NewL(TUid aUid)
22 CHeapRepository* repos=new (ELeave) CHeapRepository(aUid);
26 CHeapRepository::CHeapRepository(TUid aUid)
27 :iSettings(),iUid(aUid),iSinglePolicies(KGranularity)
31 CHeapRepository::~CHeapRepository()
33 iSinglePolicies.ResetAndDestroy();
34 iRangePolicies.Close();
35 iDeletedSettings.Close();
41 Stores the repository in-memory content to the related repository file on drive C.
42 If the operation fails, the in-memory content won't match the content of
43 the repository file (which will be kept as it was before the CommitChangesL() call).
44 In order to keep the consistency, the in-memory repository content is deleted now
45 and restored later, on the next repository operation.
47 TInt CHeapRepository::CommitChanges(RFs& aFs,TUint8 aPersistVersion,const TDesC& aTargetFilePath)
49 TRAPD(error, DoCommitChangesL(aFs,aPersistVersion,aTargetFilePath));
50 if (error != KErrNone)
52 //If the commit fails reset the repository as it is in an inconsistent state
58 void CHeapRepository::SetMetaDataOnRead(TServerSetting& aSetting, TBool aSingleMetaFound)
60 TInt isMetaFlagSet = aSetting.Meta() & KMetaDefaultValue;
63 // No single metadata set for this key
65 // First check for a matching "range" default metadata
67 TSettingsDefaultMeta* defaultMeta = iRangeMeta.Find(aSetting.Key());
71 //sets a default meta data
72 //also sets the flag back to indicate that it is a default setting from ROM
73 //or previous install so it can be replaced later with a new one.
74 aSetting.SetMeta(defaultMeta->GetDefaultMetadata() | KMetaDefaultValue);
76 aSetting.SetMeta(defaultMeta->GetDefaultMetadata());
80 // Range value not found, try for a repository default
82 aSetting.SetMeta(iDefaultMeta | KMetaDefaultValue) ;
84 aSetting.SetMeta(iDefaultMeta) ;
88 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
89 #ifdef CENTREP_CONV_TOOL
90 if (aSingleMetaFound && aSetting.IsClean() && !aSetting.IsIndividualMeta())
91 aSingleMetaFound=EFalse;
93 aSetting.SetIndividualMeta(aSingleMetaFound);
97 TInt CHeapRepository::Create(TServerSetting& aSetting, TSettingsAccessPolicy* &aPolicy, TBool aSingleMetaFound)
99 if(iSettings.Find(aSetting.Key()))
100 return KErrAlreadyExists;
102 SetMetaDataOnRead( aSetting, aSingleMetaFound);
103 aSetting.SetAccessPolicy(aPolicy);
105 return iSettings.OrderedInsert(aSetting);
108 // Comparison relation to allow single policies to be inserted in order
109 TInt CHeapRepository::CompareKeyIds(const TSettingsAccessPolicy& aSinglePolicy, const TSettingsAccessPolicy& aSinglePolicyIndexItem)
111 if(aSinglePolicy.iLowKey==aSinglePolicyIndexItem.iLowKey)
113 return (aSinglePolicy.iLowKey < aSinglePolicyIndexItem.iLowKey)?-1:1;
116 // Identity relation to allow single policy for a given key to be found
117 TBool CHeapRepository::SinglePolicyMatchOnKey(const TSettingsAccessPolicy& aSinglePolicy, const TSettingsAccessPolicy& aSinglePolicyIndexItem)
119 return aSinglePolicy.iLowKey==aSinglePolicyIndexItem.iLowKey;
122 // returns the read security policy used if there is no per-setting policy at aId
123 const TSecurityPolicy& CHeapRepository::GetFallbackReadAccessPolicy(TUint32 aId)
125 return *(GetFallbackAccessPolicy(aId)->GetReadAccessPolicy());
128 // returns the write security policy used if there is no per-setting policy at aId
129 const TSecurityPolicy& CHeapRepository::GetFallbackWriteAccessPolicy(TUint32 aId)
131 return *(GetFallbackAccessPolicy(aId)->GetWriteAccessPolicy());
134 // Get pointer to security policy that applies to a given setting
135 TSettingsAccessPolicy* CHeapRepository::GetFallbackAccessPolicy(TUint32 aId
136 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
141 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
145 // Check for single policy
146 TSettingsAccessPolicy policy(aId);
147 TIdentityRelation<TSettingsAccessPolicy> identity(SinglePolicyMatchOnKey);
148 TInt index = iSinglePolicies.Find(&policy, identity);
149 if(KErrNotFound != index)
150 return iSinglePolicies[index];
151 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
155 // check if the aId falls into any range specified in the ini file
156 TSettingsAccessPolicy* rangePolicy = iRangePolicies.Find(aId);
160 // If no single policy or range policy, return default policy
161 return &iDefaultPolicy;
164 // this function saves idividual meta as well
165 TInt CHeapRepository::ReadSettingSavePolicyL(CIniFileIn& aFile,TServerSetting& aSetting, TSettingsAccessPolicy* &aPolicy, TBool& aSingleMetaFound)
167 TBool singleReadPolicyFound;
168 TBool singleWritePolicyFound;
169 TSecurityPolicy singleReadPolicy;
170 TSecurityPolicy singleWritePolicy;
172 TInt err=aFile.ReadSettingL(aSetting,singleReadPolicy, singleWritePolicy, singleReadPolicyFound, singleWritePolicyFound, aSingleMetaFound);
176 // Set up single policies
177 if(!singleReadPolicyFound)
178 singleReadPolicy=GetDefaultReadAccessPolicy();
179 if(!singleWritePolicyFound)
180 singleWritePolicy=GetDefaultWriteAccessPolicy();
183 if(singleReadPolicyFound || singleWritePolicyFound)
185 aPolicy=new (ELeave) TSettingsAccessPolicy(singleReadPolicy,singleWritePolicy,aSetting.Key());
186 CleanupStack::PushL(aPolicy);
187 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
188 //reused single setting high key and mask to indicate whether read or write has been specified
189 //or they are just a default policy
190 if (singleReadPolicyFound)
192 if (singleWritePolicyFound)
195 TLinearOrder<TSettingsAccessPolicy> order(&CHeapRepository::CompareKeyIds);
196 iSinglePolicies.InsertInOrderL(aPolicy,order);
197 CleanupStack::Pop(aPolicy);
201 // check if the aId falls into any range specified in the ini file
202 // otherwise set policy to default policy
203 TSettingsAccessPolicy* rangePolicy = iRangePolicies.Find(aSetting.Key());
207 aPolicy=&iDefaultPolicy;
214 #ifdef CENTREP_CONV_TOOL
215 void CHeapRepository::DoCommitChangesToIniFileL(RFs& aFs,const TDesC& aOutFileName
216 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
221 CIniFileOut* out = CIniFileOut::NewLC(aFs,aOutFileName);
224 out->WriteOwnerSectionL(iOwner);
225 out->WriteTimeStampL(iTimeStamp);
226 out->WriteMetaDataL(iDefaultMeta, iRangeMeta);
227 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
228 out->WritePlatSecL(GetDefaultAccessPolicy(), iRangePolicies,aCreVersion);
230 out->WritePlatSecL(GetDefaultReadAccessPolicy(), GetDefaultWriteAccessPolicy(), iRangePolicies);
234 out->WriteMainSectionHeaderL();
235 for(TInt i=0; i<iSettings.Count(); i++)
237 const TServerSetting& setting = iSettings[i];
238 if (setting.HasAccessPolicy() && (iSinglePolicies.Find(setting.AccessPolicy()) != KErrNotFound))
240 out->WriteSettingL(setting, *setting.AccessPolicy()
241 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
248 out->WriteSettingL(setting
249 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
257 CleanupStack::PopAndDestroy(out);//out
261 void CHeapRepository::DoCommitChangesL(RFs& aFs,TUint8 aPersistVersion,const TDesC& aTargetFilePath)
263 CCreGenerator::CommitChangesToCreL(aFs,aPersistVersion,*this, aTargetFilePath);
267 The method reloads the repository content from a repository file.
268 The current repository must be emptied (or must be empty already) before the call is made.
269 @param aIniFile A reference to CIniFileIn object, which will be used to load
270 the repository content.
271 @leave System-wide error codes.
272 @leave KErrGeneral It's probably a programmer's error - current CHeapRepository
273 object is partially initialised.
274 @leave KErrCorrupt Corrupted repository file.
276 void CHeapRepository::ReloadContentL(CIniFileIn& aIniFile)
278 // Preconditions - CHeapRepository object should be an empty one.
281 User::Leave(KErrGeneral);
283 TInt err = ReloadContentExceptSettingsL(aIniFile);
284 if(err == KErrCorrupt)
288 CleanupClosePushL(iRangeMeta);
289 CleanupClosePushL(iRangePolicies);
292 TServerSetting setting;
293 TSettingsAccessPolicy* policy;
294 TBool singleMetaFound;
295 TCleanupItem tc(SinglePoliciesCleanup, &iSinglePolicies);
296 CleanupStack::PushL(tc);
297 CleanupClosePushL(iSettings);
298 while((err = ReadSettingSavePolicyL(aIniFile, setting, policy, singleMetaFound)) == KErrNone)
301 if(iSettings.IsDefault())
305 User::LeaveIfError(Create(setting, policy, singleMetaFound));
308 if(err == KErrNotFound)
312 User::LeaveIfError(err);
313 CleanupStack::Pop(4,&iRangeMeta);
317 Resets current repository data - actually all of them, which may be loaded from
318 the related ini file.
319 The iUid data member value is kept as it was at the moment of creation of
320 CHeapRepository object.
322 void CHeapRepository::ResetContent()
326 iTimeStamp = TTime(0);
328 for (TInt i=0;i<iSinglePolicies.Count();i++)
330 delete iSinglePolicies[i];
332 iSinglePolicies.Reset();
333 iRangePolicies.Reset();
334 TSecurityPolicy emptyPolicy=TSecurityPolicy();
335 iDefaultPolicy = TSettingsAccessPolicy(emptyPolicy,emptyPolicy, KUnspecifiedKey);
340 void CHeapRepository::CreateRepositoryFromCreFileL(RFs& aFs,const TDesC& aFilePath )
342 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
344 CCreGenerator::CreateReposFromCreL(aFs,*this, aFilePath,dummyVersion);
346 CCreGenerator::CreateReposFromCreL(aFs,*this, aFilePath);
350 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
351 void CHeapRepository::CreateRepositoryFromCreFileL(RFs& aFs,const TDesC& aFilePath,TUint8& aCreVersion)
353 CCreGenerator::CreateReposFromCreL(aFs,*this, aFilePath,aCreVersion);
357 void CHeapRepository::ExternalizeCre(TUint8 aPersistVersion,RWriteStream& aStream) const
359 CCreGenerator::ExternalizeCre(aPersistVersion,*this, aStream);
363 void CHeapRepository::InternalizeCreL(RReadStream& aStream)
365 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
367 CCreGenerator::InternalizeCreL(*this, aStream,dummyVersion);
369 CCreGenerator::InternalizeCreL(*this, aStream);
374 void CHeapRepository::SinglePoliciesCleanup(TAny *aPtr)
376 static_cast<RPointerArray<TSettingsAccessPolicy>*>(aPtr)->ResetAndDestroy();
379 TInt CHeapRepository::ReloadContentExceptSettingsL(CIniFileIn& aIniFile)
381 // Look for an "owner" section
382 TUint32 uidValue(KNullUid.iUid);
383 TInt err = aIniFile.ReadOwnerSectionL(uidValue);
384 if(err == KErrCorrupt)
388 iOwner.iUid = uidValue;
391 err = aIniFile.ReadTimeStampSectionL(timeStamp);
392 if(err == KErrCorrupt)
396 iTimeStamp=timeStamp;
399 err = aIniFile.ReadDefaultMetaSecSectionL(iDefaultMeta, iRangeMeta);
400 // Even if err == KErrCorrupt, some items might have already been placed in the array
401 CleanupClosePushL(iRangeMeta);
402 if(err == KErrCorrupt)
404 CleanupStack::PopAndDestroy(&iRangeMeta);
408 // Default read/write policies
409 TBool gotDefaultReadPolicy;
410 TBool gotDefaultWritePolicy;
411 TSecurityPolicy defaultReadPolicy;
412 TSecurityPolicy defaultWritePolicy;
413 err = aIniFile.ReadPlatSecSectionL(defaultReadPolicy, gotDefaultReadPolicy,
414 defaultWritePolicy, gotDefaultWritePolicy,
416 // Even if err == KErrCorrupt, some items might have already been placed in the array
417 CleanupClosePushL(iRangePolicies);
418 if(err == KErrCorrupt)
420 CleanupStack::PopAndDestroy(2,&iRangeMeta);
424 iDefaultPolicy = TSettingsAccessPolicy(defaultReadPolicy,defaultWritePolicy, KUnspecifiedKey);
425 #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
426 if (gotDefaultReadPolicy)
427 iDefaultPolicy.iHighKey=1;
428 if (gotDefaultWritePolicy)
429 iDefaultPolicy.iKeyMask=1;
431 CleanupStack::Pop(2,&iRangeMeta);
435 TBool CHeapRepository::IsEmpty()
437 if(iSettings.Count() != 0 || iRangeMeta.Count() != 0 ||
438 iSinglePolicies.Count() != 0 || iRangePolicies.Count() != 0)