sysstatemgmt/systemstatemgr/ssm/src/ssmstatepolicyresolver.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 #include <f32file.h>
       
    17 #include <ssm/ssmstate.h>
       
    18 #include "ssmstatepolicyframe.h"
       
    19 #include "ssmstatepolicyresolver.h"
       
    20 #include "ssmdebug.h"
       
    21 #include "ssmserverpanic.h"
       
    22 
       
    23 _LIT(KRomDriveLetter, "Z:");
       
    24 
       
    25 /**
       
    26 */
       
    27 CSsmStatePolicyResolver::CSsmStatePolicyResolver()
       
    28 	{
       
    29 	}
       
    30 
       
    31 /**
       
    32 */
       
    33 CSsmStatePolicyResolver::~CSsmStatePolicyResolver()
       
    34 	{
       
    35 	delete iPolicy;
       
    36 	iLibrary.Close(); //Always ok to call Close()
       
    37 	}
       
    38 
       
    39 /**
       
    40 */
       
    41 void CSsmStatePolicyResolver::ReleasePolicyResolver()
       
    42  	{
       
    43  	delete this;
       
    44  	}
       
    45 
       
    46 /**
       
    47 */
       
    48 CSsmStatePolicyResolver* CSsmStatePolicyResolver::NewL()
       
    49 	{
       
    50 	return new (ELeave) CSsmStatePolicyResolver();
       
    51 	}
       
    52 
       
    53 /**
       
    54 */
       
    55 CSsmStatePolicyResolver* CSsmStatePolicyResolver::NewLC()
       
    56 	{
       
    57 	CSsmStatePolicyResolver* self = CSsmStatePolicyResolver::NewL();
       
    58 	CleanupStack::PushL(self);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 /**
       
    63 */
       
    64 void CSsmStatePolicyResolver::GetStatePolicyL(TSsmState aState)
       
    65 	{
       
    66 	//const TInt KMaxStatePolicyFileName =  KRomDriveLetter().Length() + KSsmStatePolicyFilenamePrefix().Length() +	4 /*MainState in hex*/ + KSsmStatePolicyFilenamePostfix().Length();
       
    67 	TBuf</*KMaxStatePolicyFileName*/32> libraryFilename;
       
    68 	GetFileNameForState(aState, libraryFilename);
       
    69 
       
    70 	if( NeedsLoading(libraryFilename) )
       
    71 		{
       
    72 		RLibrary nextLibrary;
       
    73 		LoadLibraryLC(nextLibrary, libraryFilename);
       
    74 		CSsmStatePolicyFrame* nextPolicy = CreatePolicyLC(nextLibrary);
       
    75 		nextPolicy->SetStatePolicyId(aState.MainState());
       
    76 
       
    77 		DEBUGPRINT2(_L("Switching to System State Policy DLL %S"), &libraryFilename);
       
    78 
       
    79 		delete iPolicy;
       
    80 		iPolicy = NULL;
       
    81 		iPolicy = nextPolicy;
       
    82 		CleanupStack::Pop(nextPolicy);
       
    83 
       
    84 		iLibrary.Close();
       
    85 		iLibrary = nextLibrary;
       
    86 		CleanupStack::Pop(&nextLibrary);
       
    87 		}
       
    88 	} //lint !e1746 Suppress parameter 'aState' could be made const reference
       
    89 
       
    90 /**
       
    91 */
       
    92 CSsmStatePolicyFrame* CSsmStatePolicyResolver::Policy()
       
    93   	{
       
    94   	return iPolicy;
       
    95   	}
       
    96 
       
    97 /**
       
    98 @return ETrue if the requested library is not the same as the one we have already loaded
       
    99 */
       
   100 TBool CSsmStatePolicyResolver::NeedsLoading(const TParsePtrC& aLibraryFilename) const
       
   101 	{
       
   102 	if (KNullHandle == iLibrary.Handle())
       
   103 		{
       
   104 		return ETrue;
       
   105 		}
       
   106 
       
   107 	TParsePtrC currentLibrary(iLibrary.FileName());
       
   108 	if (aLibraryFilename.NameAndExt() != currentLibrary.NameAndExt())
       
   109 		{
       
   110 		return ETrue;
       
   111 		}
       
   112 
       
   113 	return EFalse;
       
   114 	}
       
   115 
       
   116 /**
       
   117 */
       
   118 void CSsmStatePolicyResolver::GetFileNameForState(const TSsmState& aState, TDes& aLibraryFilename) const
       
   119 	{
       
   120 	aLibraryFilename.Zero();
       
   121 	aLibraryFilename.Append(KRomDriveLetter);
       
   122 	aLibraryFilename.Append(KSsmStatePolicyFilenamePrefix);
       
   123 	aLibraryFilename.AppendNumFixedWidth(aState.MainState(), EHex, 4);
       
   124 	aLibraryFilename.Append(KSsmStatePolicyFilenamePostfix);
       
   125 	}
       
   126 
       
   127 /**
       
   128 @leave KErrNotSupported If UID2 of the DLL is not KStatePolicyDllTypeUidValue
       
   129 @leave KErrNotFound If the requested policy DLL file is missing.
       
   130 @leave KErrCorrupt Or any other system wide error code that the fileserver can raise.
       
   131 */
       
   132 void CSsmStatePolicyResolver::LoadLibraryLC(RLibrary& aLibrary, const TDesC& aLibraryFilename) const
       
   133 	{
       
   134 	CleanupClosePushL(aLibrary);
       
   135 	const TInt fileErr = aLibrary.Load(aLibraryFilename);
       
   136 	if (fileErr != KErrNone)
       
   137 		{
       
   138 		DEBUGPRINT3(_L("Failed to load library file %S, file error-code: %d"), &aLibraryFilename, fileErr);
       
   139 		User::Leave(fileErr);
       
   140 		}
       
   141 	if (aLibrary.Type()[1] != KSsmStatePolicyDllTypeUid)
       
   142 		{
       
   143 		DEBUGPRINT4(_L("Wrong type (uid2) in state policy library dll %S. Expected %x found %x"),
       
   144 					&aLibraryFilename, KSsmStatePolicyDllTypeUid, aLibrary.Type()[1]);
       
   145 		User::Leave(KErrNotSupported);
       
   146 		}
       
   147 	}
       
   148 
       
   149 /**
       
   150 */
       
   151 CSsmStatePolicyFrame* CSsmStatePolicyResolver::CreatePolicyLC(const RLibrary& aLibrary) const
       
   152 	{
       
   153 	__ASSERT_DEBUG(KNullHandle != aLibrary.Handle(), PanicNow(KPanicSysStateMgr, ESsmStateResolverError1));
       
   154 
       
   155 	CSsmStatePolicyFrame* frame = NULL;
       
   156 	TRAPD(err, frame = CSsmStatePolicyFrame::NewL(aLibrary.Lookup(1)));
       
   157 	CleanupStack::PushL(frame);
       
   158 #ifdef _DEBUG
       
   159 	if(KErrNone != err)
       
   160 		{
       
   161 		TFileName name = aLibrary.FileName();
       
   162 		DEBUGPRINT3(_L("Error %d when calling first function in State Policy DLL %S."), err, &name);
       
   163 		}
       
   164 #endif
       
   165 	SSMLOGLEAVEIFERROR(err);
       
   166 	return frame;
       
   167 	}