persistentstorage/dbms/security/SC_Policy.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2004-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 // CPolicyBase, CDbPolicy, CTblPolicy, CPolicyDomain classes
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "SC_Policy.h"
       
    19 
       
    20 namespace DBSC
       
    21 {
       
    22 
       
    23 ///////////////////////////////////////////////////////////////////////////////////////////
       
    24 //CPolicyBase class
       
    25 
       
    26 /**
       
    27 */
       
    28 CPolicyBase::~CPolicyBase()
       
    29 	{
       
    30 	iPolicyCollection.Close();//Collection of R/W/S security policies
       
    31 	}
       
    32 
       
    33 #ifdef __DBDUMP__
       
    34 /**
       
    35 Dumps the content of a CPolicyBase instance to a text file.
       
    36 @param aFile A reference to RFile object, which has to be used for the output.
       
    37 */
       
    38 void CPolicyBase::Dump(RFile& aFile) const
       
    39 	{
       
    40 	DB_INVARIANT();
       
    41 
       
    42 	_LIT8(KClassName, "Class: CPolicyBase. this=%X");
       
    43 	_LIT8(KCount, "Security Policy, Count=%d");
       
    44 	_LIT8(KCrLf, "\r\n");
       
    45 	_LIT8(KPolicyType, "Policy type: ");
       
    46 	_LIT8(KRead,   "Read, ");
       
    47 	_LIT8(KWrite,  "Write, ");
       
    48 	_LIT8(KSchema, "Schema, ");
       
    49 	_LIT8(KPolicyData, "Policy data: ");
       
    50 	_LIT8(KFmt, "%02X ");
       
    51 	TBuf8<100> buf;
       
    52 
       
    53 	buf.Format(KClassName, this);
       
    54 	(void)aFile.Write(buf);
       
    55 	(void)aFile.Write(KCrLf);
       
    56 
       
    57 	TInt cnt = iPolicyCollection.Count();
       
    58 	buf.Format(KCount, TInt32(cnt));
       
    59 	(void)aFile.Write(buf);
       
    60 	(void)aFile.Write(KCrLf);
       
    61 
       
    62 	for(TInt i=0;i<cnt;++i)
       
    63 		{
       
    64 		const TPolicy& policy = iPolicyCollection[i];
       
    65 		(void)aFile.Write(KPolicyType);
       
    66 		switch(policy.iType)
       
    67 			{
       
    68 			case EPTRead:
       
    69 				(void)aFile.Write(KRead);
       
    70 				break;
       
    71 			case EPTWrite:
       
    72 				(void)aFile.Write(KWrite);
       
    73 				break;
       
    74 			case EPTSchema:
       
    75 				(void)aFile.Write(KSchema);
       
    76 				break;
       
    77 			default:
       
    78 				__ASSERT(0);
       
    79 				break;
       
    80 			}
       
    81 		(void)aFile.Write(KPolicyData);
       
    82 		TPtrC8 packet = policy.iData.Package();
       
    83 		TInt len = packet.Length();
       
    84 		for(TInt j=0;j<len;++j)
       
    85 			{
       
    86 			buf.Format(KFmt, packet[j]);
       
    87 			(void)aFile.Write(buf);
       
    88 			}
       
    89 		(void)aFile.Write(KCrLf);
       
    90 		}
       
    91 	}
       
    92 #endif//__DBDUMP__
       
    93 
       
    94 /**
       
    95 Standard phase-two construction method for CPolicyBase instance.
       
    96 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
       
    97        be used to control the access to a database object, controlled by CPolicyBase
       
    98 	   instance.
       
    99 */
       
   100 void CPolicyBase::ConstructL(const CPolicyBase::RPolicyCollection& aPolicyCollection)
       
   101 	{
       
   102 	iPolicyCollection.Reset();
       
   103 	TInt cnt = aPolicyCollection.Count();
       
   104 	for(TInt i=0;i<cnt;++i)
       
   105 		{
       
   106 		__LEAVE_IF_ERROR(iPolicyCollection.Append(aPolicyCollection[i]));
       
   107 		}
       
   108 	DB_INVARIANT();
       
   109 	}
       
   110 
       
   111 /**
       
   112 It is used in the production code.
       
   113 If the object data is not in a consistent state, the method will leave 
       
   114 with KErrGeneral error.
       
   115 @leave KErrGeneral, if the object data is not in a consistent state
       
   116 */
       
   117 void CPolicyBase::InvariantL() const
       
   118 	{
       
   119 	TUint32 mask = 0;
       
   120 	for(TInt i=(iPolicyCollection.Count()-1);i>-1;--i)
       
   121 		{
       
   122 		TPolicy& policy = const_cast <TPolicy&> (iPolicyCollection[i]);
       
   123 		if(policy.iType == EPTNone)
       
   124 			{
       
   125 			__LEAVE(KErrGeneral);
       
   126 			}
       
   127 		if(mask & policy.iType)	//This security policy is duplicated
       
   128 			{
       
   129 			__LEAVE(KErrGeneral);
       
   130 			}
       
   131 		TPtrC8 packet = policy.iData.Package();
       
   132 		if(policy.iData.Set(packet) != KErrNone)
       
   133 			{
       
   134 			__LEAVE(KErrGeneral);
       
   135 			}
       
   136 		mask |= policy.iType;
       
   137 		}
       
   138 	}
       
   139 
       
   140 /**
       
   141 This method implements pure virtual MPolicy::Get().
       
   142 It searches object's policy collection for a policy of type aPolicyType
       
   143 and initializes aPolicy parameter with the found policy.
       
   144 @param aPolicyType Type of the requested security policy: read/write/schema
       
   145 @param aPolicy Outout parameter, which will be initialized with the found security policy data.
       
   146 @return System-wide error code, including KErrNotFound if the requested policy was not found.
       
   147 */
       
   148 TInt CPolicyBase::Get(TPolicyType aPolicyType, TSecurityPolicy& aPolicy) const
       
   149 	{
       
   150 	DB_INVARIANT();
       
   151 	TInt err = KErrNotFound;
       
   152 	const TSecurityPolicy* securityPolicy = Policy(aPolicyType);
       
   153 	if(securityPolicy)
       
   154 		{
       
   155 		err = aPolicy.Set(securityPolicy->Package());
       
   156 		}
       
   157 	return err;
       
   158 	}
       
   159 
       
   160 #ifdef __DBINVARIANT__
       
   161 /**
       
   162 Asserts the internal state of CPolicyBase instance.
       
   163 It can be used for pre- or post- condition checks in CPolicyBase methods implementations.
       
   164 */
       
   165 void CPolicyBase::Invariant() const
       
   166 	{
       
   167 	TRAPD(err, InvariantL());
       
   168 	DB_INVARIANT_ASSERT(err == KErrNone);
       
   169 	}
       
   170 #endif//__DBINVARIANT__
       
   171 
       
   172 /**
       
   173 The method traverses the policies collection and searches for a policy of aPolicyType type.
       
   174 If such a policy exists, a const pointer to it will be returned, otherwise - NULL.
       
   175 @param aPolicyType Policy type - R/W/S
       
   176 @return A const pointer to the found policy or NULL if not found.
       
   177 */
       
   178 const TSecurityPolicy* CPolicyBase::Policy(TPolicyType aPolicyType) const
       
   179 	{
       
   180 	__ASSERT(aPolicyType != EPTNone);
       
   181 	const TSecurityPolicy* policy = NULL;
       
   182 	for(TInt i=(iPolicyCollection.Count()-1);i>-1;--i)
       
   183 		{
       
   184 		if(iPolicyCollection[i].iType == aPolicyType)
       
   185 			{
       
   186 			policy = &iPolicyCollection[i].iData;
       
   187 			break;
       
   188 			}
       
   189 		}
       
   190 	return policy;
       
   191 	}
       
   192 
       
   193 /**
       
   194 Asserts caller capabilities/SID/VID.
       
   195 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
       
   196 @param aPolicyType Policy type - R/W/S. 
       
   197 @return EPCNotFound - the policy cannot be found
       
   198         EPCPassed - policy check passed
       
   199         EPCNotPassed - policy check not passed
       
   200 */
       
   201 CPolicyBase::TPolicyCheckResult CPolicyBase::DoCheck(const RMessage2& aMessage, TPolicyType aPolicyType) const
       
   202 	{
       
   203 	const TSecurityPolicy* securityPolicy = Policy(aPolicyType);
       
   204 
       
   205 	if(!securityPolicy)
       
   206 		{
       
   207 		return EPCNotFound;
       
   208 		}
       
   209 
       
   210 	return securityPolicy->CheckPolicy(aMessage) ? EPCPassed : EPCNotPassed;
       
   211 	}
       
   212 
       
   213 ///////////////////////////////////////////////////////////////////////////////////////////
       
   214 //CDbPolicy class
       
   215 
       
   216 /**
       
   217 */
       
   218 CDbPolicy::~CDbPolicy()
       
   219 	{
       
   220 	}
       
   221 
       
   222 /**
       
   223 Asserts caller capabilities/SID/VID.
       
   224 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
       
   225 @param aPolicyType Policy type - R/W/S. 
       
   226 @return ETrue The caller capabilities/SID/VID satisfy the specified security policy.
       
   227         EFalse The check not passed.
       
   228 @panic EDBSCPolicyNotFound, if there is no such policy 
       
   229 */
       
   230 TBool CDbPolicy::Check(const RMessage2& aMessage, TPolicyType aPolicyType) const
       
   231 	{
       
   232 	__ASSERT(aPolicyType != EPTNone);
       
   233 	DB_INVARIANT();
       
   234 	TPolicyCheckResult res = DoCheck(aMessage, aPolicyType);
       
   235 	__ASSERT(res != EPCNotFound);
       
   236 	return  res == EPCPassed ? ETrue : EFalse;
       
   237 	}
       
   238 
       
   239 /**
       
   240 Standard phase-one factory method for CDbPolicy instance.
       
   241 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
       
   242        be used to control the access to the database, controlled by CDbPolicy instance.
       
   243 @return A pointer to just created CDbPolicy instance.
       
   244 @leave System-wide error codes, including KErrNoMemory.
       
   245 */
       
   246 CDbPolicy* CDbPolicy::NewLC(const CPolicyBase::RPolicyCollection& aPolicyCollection)
       
   247 	{
       
   248 	CDbPolicy* self = new (ELeave) CDbPolicy;
       
   249 	CleanupStack::PushL(self);
       
   250 	self->ConstructL(aPolicyCollection);
       
   251 	return self;
       
   252 	}
       
   253 
       
   254 #ifdef __DBDUMP__
       
   255 /**
       
   256 Dumps the content of a CDbPolicy instance to a text file.
       
   257 @param aFile A reference to RFile object, which has to be used for the output.
       
   258 */
       
   259 void CDbPolicy::Dump(RFile& aFile) const
       
   260 	{
       
   261 	DB_INVARIANT();
       
   262 
       
   263 	_LIT8(KClassName, "Class: CDbPolicy. this=%X");
       
   264 	_LIT8(KCrLf, "\r\n");
       
   265 	_LIT8(KObjType, "Object: Database");
       
   266 	_LIT8(KEnd, "==========================");
       
   267 	TBuf8<40> buf;
       
   268 
       
   269 	buf.Format(KClassName, this);
       
   270 	(void)aFile.Write(buf);
       
   271 	(void)aFile.Write(KCrLf);
       
   272 	(void)aFile.Write(KObjType);
       
   273 	(void)aFile.Write(KCrLf);
       
   274 	CPolicyBase::Dump(aFile);
       
   275 	(void)aFile.Write(KEnd);
       
   276 	(void)aFile.Write(KCrLf);
       
   277 	}
       
   278 #endif//__DBDUMP__
       
   279 
       
   280 /**
       
   281 It is used in the production code.
       
   282 If the object data is not in a consistent state, the method will leave 
       
   283 with KErrGeneral error.
       
   284 @leave KErrGeneral, if the object data is not in a consistent state
       
   285 */
       
   286 void CDbPolicy::InvariantL() const
       
   287 	{
       
   288 	for(TInt c=0;c<KPolicyTypesCount;++c)
       
   289 		{
       
   290 		TPolicyType t = static_cast <TPolicyType> (1 << c);
       
   291 		if(Policy(t) == NULL)
       
   292 			{
       
   293 			__LEAVE(KErrGeneral);
       
   294 			}
       
   295 		}
       
   296 	CPolicyBase::InvariantL();
       
   297 	}
       
   298 
       
   299 ///////////////////////////////////////////////////////////////////////////////////////////
       
   300 //CTblPolicy class
       
   301 
       
   302 /**
       
   303 */
       
   304 CTblPolicy::~CTblPolicy()
       
   305 	{
       
   306 	delete iTblName;
       
   307 	}
       
   308 
       
   309 /**
       
   310 Asserts caller capabilities/SID/VID.
       
   311 @param aMessage An object whith caller capabilities/SID/VID, which has to be checked.
       
   312 @param aPolicyType Policy type - R/W/S. 
       
   313 @return ETrue The caller capabilities/SID/VID satisfy the specified security policy.
       
   314         EFalse The check not passed.
       
   315 @panic EDBSCPolicyNotFound, if there is no such policy 
       
   316 */
       
   317 TBool CTblPolicy::Check(const RMessage2& aMessage, TPolicyType aPolicyType) const
       
   318 	{
       
   319 	__ASSERT(aPolicyType != EPTNone);
       
   320 	__ASSERT(iDbPolicy);
       
   321 	DB_INVARIANT();
       
   322 	TPolicyCheckResult res = EPCNotPassed;
       
   323 	//1. Check database security policy
       
   324 	if(iDbPolicy->Check(aMessage, aPolicyType))
       
   325 		{
       
   326 	//2. Check table security policy
       
   327 		res = DoCheck(aMessage, aPolicyType);
       
   328 		}
       
   329 	//If there is no table security policy of the requested type - no problem, the database
       
   330 	//security policy of that type has been checked already and the check passed.
       
   331 	return res == EPCNotPassed ? EFalse : ETrue;
       
   332 	}
       
   333 
       
   334 /**
       
   335 This method implements pure virtual MPolicy::Get().
       
   336 It searches object's policy collection for a policy of type aPolicyType
       
   337 and initializes aPolicy parameter with the found policy.
       
   338 @param aPolicyType Type of the requested security policy: read/write
       
   339 @param aPolicy Outout parameter, which will be initialized with the found security policy data.
       
   340 @return System-wide error codes, including KErrNotSupported, if the request is for a schema policy.
       
   341 */
       
   342 TInt CTblPolicy::Get(TPolicyType aPolicyType, TSecurityPolicy& aPolicy) const
       
   343 	{
       
   344 	if(aPolicyType == EPTSchema)
       
   345 		{
       
   346 		return KErrNotSupported;
       
   347 		}
       
   348 	DB_INVARIANT();
       
   349 	TInt err = CPolicyBase::Get(aPolicyType, aPolicy);
       
   350 	if(err == KErrNotFound)
       
   351 		{
       
   352 		err = iDbPolicy->Get(aPolicyType, aPolicy);
       
   353 		}
       
   354 	__ASSERT(err != KErrNotFound);
       
   355 	return err;
       
   356 	}
       
   357 
       
   358 /**
       
   359 Standard phase-one factory method for CTblPolicy instance.
       
   360 @param aTblName Name of the controlled by this instance database table.
       
   361 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
       
   362        be used to control the access to the table, controlled by CTblPolicy instance.
       
   363 @param aDbPolicy The related for the table database policy.
       
   364        CTblPolicy instance does not take the ownership on aDbPolicy pointer!        
       
   365 @return A pointer to just created CTblPolicy instance.
       
   366 @leave System-wide error codes, including KErrNoMemory.
       
   367 */
       
   368 CTblPolicy* CTblPolicy::NewLC(const TDesC& aTblName, 
       
   369 							  const CPolicyBase::RPolicyCollection& aPolicyCollection,
       
   370 							  const CDbPolicy* aDbPolicy)
       
   371 	{
       
   372 	CTblPolicy* self = new (ELeave) CTblPolicy(aDbPolicy);
       
   373 	CleanupStack::PushL(self);
       
   374 	self->ConstructL(aTblName, aPolicyCollection);
       
   375 	return self;
       
   376 	}
       
   377 
       
   378 #ifdef __DBDUMP__
       
   379 /**
       
   380 Dumps the content of a CTblPolicy instance to a text file.
       
   381 @param aFile A reference to RFile object, which has to be used for the output.
       
   382 */
       
   383 void CTblPolicy::Dump(RFile& aFile) const
       
   384 	{
       
   385 	DB_INVARIANT();
       
   386 
       
   387 	_LIT8(KClassName, "Class: CTblPolicy. this=%X");
       
   388 	_LIT8(KDbPolicyPtr, "Db policy ptr=%X");
       
   389 	_LIT8(KCrLf, "\r\n");
       
   390 	_LIT8(KName, "Table name: ");
       
   391 	_LIT8(KObjType, "Object: Table");
       
   392 	_LIT8(KEnd, "==========================");
       
   393 	TBuf8<100> buf;
       
   394 
       
   395 	buf.Format(KClassName, this);
       
   396 	(void)aFile.Write(buf);
       
   397 	(void)aFile.Write(KCrLf);
       
   398 	(void)aFile.Write(KObjType);
       
   399 	(void)aFile.Write(KCrLf);
       
   400 	buf.Format(KDbPolicyPtr, iDbPolicy);
       
   401 	(void)aFile.Write(buf);
       
   402 	(void)aFile.Write(KCrLf);
       
   403 	buf.Copy(KName);
       
   404 	buf.Append(*iTblName);
       
   405 	(void)aFile.Write(buf);
       
   406 	(void)aFile.Write(KCrLf);
       
   407 	CPolicyBase::Dump(aFile);
       
   408 	(void)aFile.Write(KEnd);
       
   409 	(void)aFile.Write(KCrLf);
       
   410 	}
       
   411 #endif//__DBDUMP__
       
   412 
       
   413 /**
       
   414 It is used in the production code.
       
   415 If the object data is not in a consistent state, the method will leave 
       
   416 with KErrGeneral error.
       
   417 @leave KErrGeneral, if the object data is not in a consistent state
       
   418 */
       
   419 void CTblPolicy::InvariantL() const
       
   420 	{
       
   421 	if(iDbPolicy == NULL)
       
   422 		{
       
   423 		__LEAVE(KErrGeneral);
       
   424 		}
       
   425 	if(iTblName == NULL || iTblName->Length() == 0)
       
   426 		{
       
   427 		__LEAVE(KErrGeneral);
       
   428 		}
       
   429 	if(Policy(EPTSchema) != NULL)
       
   430 		{
       
   431 		__LEAVE(KErrGeneral);
       
   432 		}
       
   433 	CPolicyBase::InvariantL();
       
   434 	}
       
   435 
       
   436 /**
       
   437 Standard phase-two construction method for CTblPolicy instance.
       
   438 @param aTblName Name of the controlled by this instance database table.
       
   439 @param aPolicyCollection A const reference to a collection of R/W/S policies, which has to
       
   440        be used to control the access to the table object, controlled by CTblPolicy
       
   441 	   instance.
       
   442 */
       
   443 void CTblPolicy::ConstructL(const TDesC& aTblName, const CPolicyBase::RPolicyCollection& aPolicyCollection)
       
   444 	{
       
   445 	iTblName = HBufC::NewL(aTblName.Length());
       
   446 	*iTblName = aTblName;
       
   447 	CPolicyBase::ConstructL(aPolicyCollection);
       
   448 	DB_INVARIANT();
       
   449 	}
       
   450 
       
   451 ///////////////////////////////////////////////////////////////////////////////////////////
       
   452 //CPolicyDomain class
       
   453 
       
   454 /**
       
   455 Standard phase-one factory method for CPolicyDomain instance.
       
   456 @param aUid UID of the controlled by this instance security policy domain.
       
   457 @param aPDLoader A reference to an implementation of MPolicyDomainLoader interface,
       
   458        which is used to load and add security policies to the controlled collection.
       
   459 @return A pointer to just created CPolicyDomain instance.
       
   460 @leave System-wide error codes, including KErrNoMemory.
       
   461 */
       
   462 CPolicyDomain* CPolicyDomain::NewLC(TUid aUid, MPolicyDomainLoader& aPDLoader)
       
   463 	{
       
   464 	CPolicyDomain* self = new (ELeave) CPolicyDomain(aUid);
       
   465 	CleanupStack::PushL(self);
       
   466 	self->InternalizeL(aPDLoader);
       
   467 	return self;
       
   468 	}
       
   469 
       
   470 /**
       
   471 */
       
   472 CPolicyDomain::~CPolicyDomain()
       
   473 	{
       
   474 	Destroy();
       
   475 	}
       
   476 
       
   477 /**
       
   478 The method returns the database policy interface.
       
   479 @return A const pointer to the database policy interface in CPolicyDomain.
       
   480 */
       
   481 const MPolicy* CPolicyDomain::DbPolicy() const
       
   482 	{
       
   483 	DB_INVARIANT();
       
   484 	return iDbPolicy;
       
   485 	}
       
   486 
       
   487 /**
       
   488 The method returns a table policy interface, identified by aTblName parameter.
       
   489 @param aTblName Name of the table, which policy interface has to be retrieved.
       
   490 @return A const pointer to the table policy interface, which is identified by aTblName parameter.
       
   491 */
       
   492 const MPolicy* CPolicyDomain::TblPolicy(const TDesC& aTblName) const
       
   493 	{
       
   494 	__ASSERT(aTblName.Length() > 0);
       
   495 	DB_INVARIANT();
       
   496 	const MPolicy* policy = NULL;
       
   497 	TInt cnt = iTPCollection.Count();
       
   498 	for(TInt i=0;i<cnt;++i)
       
   499 		{
       
   500 		CTblPolicy* tblPolicy = iTPCollection[i];
       
   501 		__ASSERT(tblPolicy);
       
   502 		if(aTblName.CompareF(tblPolicy->TableName()) == 0)
       
   503 			{
       
   504 			policy = tblPolicy;
       
   505 			break;
       
   506 			}
       
   507 		}
       
   508 	if(!policy)
       
   509 		{
       
   510 		policy = iDbPolicy;
       
   511 		}
       
   512 	__ASSERT(policy);
       
   513 	return policy;
       
   514 	}
       
   515 
       
   516 /**
       
   517 Externalizes the security policy collection using MPolicyDomainPersister interface as an
       
   518 persister.
       
   519 @param aPDPersister A reference to an MPolicyDomainPersister implementation, which will 
       
   520        persist the controlled collection of security policies.
       
   521 */
       
   522 void CPolicyDomain::ExternalizeL(MPolicyDomainPersister& aPDPersister) const
       
   523 	{
       
   524 	DB_INVARIANT();
       
   525 	TPolicyDomainReader reader(*this);
       
   526 	aPDPersister.RunL(reader);
       
   527 	}
       
   528 
       
   529 #ifdef __DBDUMP__
       
   530 /**
       
   531 Dumps the content of a CPolicyDomain instance to a text file.
       
   532 @param aFile A reference to RFile object, which has to be used for the output.
       
   533 */
       
   534 void CPolicyDomain::Dump(RFile& aFile) const
       
   535 	{
       
   536 	DB_INVARIANT();
       
   537 
       
   538 	_LIT8(KClassName, "Class: CPolicyDomain. this=%X");
       
   539 	_LIT8(KUidFmt, "UID=%X");
       
   540 	_LIT8(KCrLf, "\r\n");
       
   541 	_LIT8(KEnd, "==========================");
       
   542 	_LIT8(KBackupSIDFmt, "BackupSID=%X");
       
   543 	TBuf8<40> buf;
       
   544 
       
   545 	buf.Format(KClassName, this);
       
   546 	(void)aFile.Write(buf);
       
   547 	(void)aFile.Write(KCrLf);
       
   548 	buf.Format(KUidFmt, iUid.iUid);
       
   549 	(void)aFile.Write(buf);
       
   550 	(void)aFile.Write(KCrLf);
       
   551 	(void)aFile.Write(KEnd);
       
   552 	(void)aFile.Write(KCrLf);
       
   553 	iDbPolicy->Dump(aFile);
       
   554 	TInt cnt = iTPCollection.Count();
       
   555 	for(TInt i=0;i<cnt;++i)
       
   556 		{
       
   557 		__ASSERT(iTPCollection[i]);
       
   558 		iTPCollection[i]->Dump(aFile);
       
   559 		}
       
   560 	(void)aFile.Write(KEnd);
       
   561 	buf.Format(KBackupSIDFmt, iBackupSID.iUid);
       
   562 	(void)aFile.Write(buf);
       
   563 	(void)aFile.Write(KCrLf);
       
   564 	}
       
   565 #endif//__DBDUMP__
       
   566 
       
   567 /**
       
   568 It is used in the production code.
       
   569 If the object data is not in a consistent state, the method will leave 
       
   570 with KErrGeneral error.
       
   571 @leave KErrGeneral, if the object data is not in a consistent state
       
   572 */
       
   573 void CPolicyDomain::InvariantL() const
       
   574 	{
       
   575 	if(iUid == KNullUid)
       
   576 		{
       
   577 		__LEAVE(KErrGeneral);
       
   578 		}
       
   579 	if(iDbPolicy == NULL)
       
   580 		{
       
   581 		__LEAVE(KErrGeneral);
       
   582 		}
       
   583 	iDbPolicy->InvariantL();
       
   584 
       
   585 	TInt cnt = iTPCollection.Count();
       
   586 	TInt i;
       
   587 	for(i=0;i<cnt;++i)
       
   588 		{
       
   589 		if(iTPCollection[i] == NULL)
       
   590 			{
       
   591 			__LEAVE(KErrGeneral);
       
   592 			}
       
   593 		iTPCollection[i]->InvariantL();
       
   594 		}
       
   595 	//Check that each represented table has unique name
       
   596 	for(i=0;i<(cnt-1);++i)
       
   597 		{
       
   598 		for(TInt j=(i+1);j<cnt;++j)
       
   599 			{
       
   600 			if(iTPCollection[i]->TableName() == iTPCollection[j]->TableName())
       
   601 				{
       
   602 				__LEAVE(KErrGeneral);
       
   603 				}
       
   604 			}
       
   605 		}
       
   606 	}
       
   607 
       
   608 #ifdef __DBINVARIANT__
       
   609 /**
       
   610 Asserts the internal state of CPolicyDomain instance.
       
   611 It can be used for pre- or post- condition checks in CPolicyDomain methods implementations.
       
   612 */
       
   613 void CPolicyDomain::Invariant() const
       
   614 	{
       
   615 	TRAPD(err, InvariantL());
       
   616 	DB_INVARIANT_ASSERT(err == KErrNone);
       
   617 	}
       
   618 #endif//__DBINVARIANT__
       
   619 
       
   620 /**
       
   621 Creates the collection of security policies using MPolicyDomainLoader interface as a security
       
   622 policy loader.
       
   623 @param aPDLoader A reference to MPolicyDomainLoader implementation, which is used to load
       
   624        and add security policies to the controlled collection.
       
   625 @leave System-wide error code including KErrGeneral if the data is not consistent
       
   626 */
       
   627 void CPolicyDomain::InternalizeL(MPolicyDomainLoader& aPDLoader)
       
   628 	{
       
   629 	TPolicyDomainBuilder builder(*this);
       
   630 	aPDLoader.RunL(builder);
       
   631 #ifdef __DBINVARIANT__
       
   632 	Invariant();
       
   633 #else
       
   634 	InvariantL();
       
   635 #endif
       
   636 	}
       
   637 
       
   638 /**
       
   639 The method destroys the controlled by CPolicyDomain collection of security policies.
       
   640 */
       
   641 void CPolicyDomain::Destroy()
       
   642 	{
       
   643 	TInt cnt = iTPCollection.Count();
       
   644 	for(TInt i=0;i<cnt;++i)
       
   645 		{
       
   646 		__ASSERT(iTPCollection[i]);
       
   647 		delete iTPCollection[i];
       
   648 		}
       
   649 	iTPCollection.Close();
       
   650 	delete iDbPolicy;
       
   651 	iDbPolicy = NULL;
       
   652 	}
       
   653 
       
   654 } //end of - namespace DBSC
       
   655