sysstatemgmt/systemstatemgr/cmd/src/cmdcreateswp.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 <s32strm.h>
       
    17 #include <barsread2.h>
       
    18 #include <f32file.h>
       
    19 #include <e32property.h>
       
    20 #include <ssm/ssmstatemanager.h>
       
    21 
       
    22 #include "cmdcreateswp.h"
       
    23 #include "ssmdebug.h"
       
    24 #include "ssmpanic.h"
       
    25 #include "ssmcommandparameters.h"
       
    26 
       
    27 /**
       
    28 Used to create an instance of CCmdCreateSwp class from a read stream.
       
    29 CSsmCommandList::InternalizeL() uses this method to construct a command from stream.
       
    30 
       
    31 @param aReadStream Read stream containing data through which object can be created
       
    32 @return	A pointer to an object of type CCmdCreateSwp.
       
    33 */
       
    34 CCmdCreateSwp* CCmdCreateSwp::NewL(RReadStream& aReadStream)
       
    35 	{
       
    36 	CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp();
       
    37 	CleanupStack::PushL(self);
       
    38 	self->ConstructL(aReadStream);
       
    39 	CleanupStack::Pop(self);
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
    44 /**
       
    45 Used to create an instance of CCmdCreateSwp class from given parameters.
       
    46 This method is used by SsmCommandFactory to create a command.
       
    47  
       
    48 @param aSeverity The severity of the command
       
    49 @param aSwpInfo	The system wide property info
       
    50 @param aFilename The policy file to associate it to
       
    51 @param aPriority The priority of the command in the list
       
    52 @return	A pointer to an object of type CCmdCreateSwp.
       
    53 */
       
    54 CCmdCreateSwp* CCmdCreateSwp::NewL(TCmdErrorSeverity aSeverity, const TSsmSwp& aSwpInfo, const TDesC& aFilename, const TUint16 aPriority)
       
    55 	{
       
    56 	CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp(aSeverity, aSwpInfo, aPriority);
       
    57 	CleanupStack::PushL(self);
       
    58 	self->ConstructL(aFilename);
       
    59 	CleanupStack::Pop(self);
       
    60 	return self;
       
    61 	}
       
    62 #endif
       
    63 
       
    64 /**
       
    65 Used to create an instance of CCmdCreateSwp class from given parameters.
       
    66 This method is used by SsmCommandFactory to create a command.
       
    67  
       
    68 @param aSeverity The severity of the command
       
    69 @param aSwpInfo	The system wide property info
       
    70 @param aFilename The policy file to associate it to
       
    71 @return	A pointer to an object of type CCmdCreateSwp.
       
    72 */
       
    73 CCmdCreateSwp* CCmdCreateSwp::NewL(TCmdErrorSeverity aSeverity, const TSsmSwp& aSwpInfo, const TDesC& aFilename)
       
    74 	{
       
    75 	CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp(aSeverity, aSwpInfo);
       
    76 	CleanupStack::PushL(self);
       
    77 	self->ConstructL(aFilename);
       
    78 	CleanupStack::Pop(self);
       
    79 	return self;
       
    80 	}
       
    81 
       
    82 /**
       
    83 Used to create an instance of CCmdCreateSwp class from resource.
       
    84 
       
    85 @param aCommandParameters Object data from a resource file
       
    86 @return	A pointer to an object of type CCmdCreateSwp.
       
    87 */
       
    88 CCmdCreateSwp* CCmdCreateSwp::NewL(TSsmCommandParameters& aCommandParameters)
       
    89 	{
       
    90 	CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp();
       
    91 	CleanupStack::PushL(self);
       
    92 	self->ConstructL(aCommandParameters);
       
    93 	CleanupStack::Pop(self);
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 /**
       
    98 Used to create an instance of CCmdCreateSwp class from CCmdCreateSwp object
       
    99 Must be used only by CLE
       
   100 @param aCmdCreateSwp CCmdCreateSwp reference 
       
   101 @return A pointer to an object of type CCmdCreateSwp.
       
   102 */
       
   103 CCmdCreateSwp* CCmdCreateSwp::NewLC(const CCmdCreateSwp& aCmdCreateSwp)
       
   104     {
       
   105 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   106     CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp(aCmdCreateSwp.Severity(),aCmdCreateSwp.SsmSwpInfo(),
       
   107                                                      aCmdCreateSwp.Priority());
       
   108 #else
       
   109     CCmdCreateSwp* self = new (ELeave) CCmdCreateSwp(aCmdCreateSwp.Severity(),aCmdCreateSwp.SsmSwpInfo());
       
   110 #endif
       
   111     CleanupStack::PushL(self);
       
   112     self->ConstructL(aCmdCreateSwp);
       
   113     return self;
       
   114     }
       
   115 
       
   116 void CCmdCreateSwp::ConstructL(const CCmdCreateSwp& aCmdCreateSwp)
       
   117     {
       
   118     iConditionalResourceId = aCmdCreateSwp.ConditionalInformation();
       
   119     iFileName.CreateL(aCmdCreateSwp.FileName());
       
   120 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   121 	if (iConditionalResourceId != 0)
       
   122         {
       
   123         SetCommandResourceFileNameL(aCmdCreateSwp.GetCommandResourceFileName());
       
   124         }
       
   125 #endif    
       
   126     }
       
   127 
       
   128 /**
       
   129 Destructor
       
   130 */	
       
   131 CCmdCreateSwp::~CCmdCreateSwp()
       
   132 	{
       
   133 	Cancel();
       
   134 	iSsm.Close();
       
   135 	iFileName.Close();
       
   136 	}
       
   137 
       
   138 /**
       
   139 Initialises the BIC's active object and initiates the command
       
   140  
       
   141 @param aStatus The TRequestStatus of the active object calling this BIC
       
   142 */	
       
   143 void CCmdCreateSwp::Execute(TRequestStatus& aStatus)
       
   144 	{
       
   145 	aStatus = KRequestPending;
       
   146 	iExecuteRequest = &aStatus;
       
   147 
       
   148 	CompleteRequest(iStatus, iSsm.Connect());
       
   149 	SetActive();
       
   150 	}
       
   151 
       
   152 /**
       
   153 Releases resources associated with this BIC
       
   154 */	
       
   155 void CCmdCreateSwp::Release()
       
   156 	{
       
   157 	delete this;
       
   158 	}
       
   159 
       
   160 /**
       
   161 Initiates a Cancel on the object. 
       
   162 */	
       
   163 void CCmdCreateSwp::ExecuteCancel()
       
   164 	{
       
   165 	//cancel the outstanding request
       
   166 	Cancel();
       
   167 	//and complete the request with KErrCancel
       
   168 	CompleteExecuteRequest(KErrCancel);
       
   169 	}
       
   170 
       
   171 /**
       
   172 Returns the type of the BIC
       
   173   
       
   174 @return	The type of the BIC
       
   175 */	
       
   176 TSsmCommandType CCmdCreateSwp::Type() const
       
   177 	{
       
   178 	return (ESsmCmdCreateSwp);
       
   179 	}
       
   180 
       
   181 /**
       
   182 Returns the Version of the BIC
       
   183 
       
   184 @return	The maximum supported version of the BIC
       
   185 */	
       
   186 TInt CCmdCreateSwp::MaxSupportedVersion()
       
   187 	{
       
   188 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   189 	return (static_cast<TInt>(ECmdCreateSwpVersionWithPriority));
       
   190 #else
       
   191 	return (static_cast<TInt>(ECmdCreateSwpInitialVersion));
       
   192 #endif
       
   193 	}
       
   194 
       
   195 /**
       
   196 Configures the BIC using data contained in a ReadStream
       
   197 
       
   198 @param aReadStream 	A read stream containing BIC data
       
   199 */
       
   200 void CCmdCreateSwp::InternalizeL(RReadStream& aReadStream)
       
   201 	{
       
   202 	iSeverity = static_cast<TCmdErrorSeverity>(aReadStream.ReadInt16L());
       
   203 	const TUint key = aReadStream.ReadUint32L();
       
   204 	const TInt value = aReadStream.ReadInt32L();	
       
   205 	TSsmSwp swpInfo(key, value);
       
   206 	iSwpInfo = swpInfo;
       
   207 	HBufC* filename = HBufC::NewL(aReadStream, KMaxFileName);
       
   208 	CleanupStack::PushL(filename);
       
   209 	ConstructL(*filename);
       
   210 	CleanupStack::PopAndDestroy(filename);
       
   211 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   212 	iPriority = aReadStream.ReadUint16L();
       
   213 #endif	
       
   214 	}
       
   215 
       
   216 /**
       
   217 Externalises the configuration of the BIC
       
   218   
       
   219 @param aWriteStream A write stream to write BIC data to
       
   220 */
       
   221 void CCmdCreateSwp::ExternalizeL(RWriteStream& aWriteStream) const
       
   222 	{
       
   223 	aWriteStream.WriteInt16L(iSeverity);
       
   224 	aWriteStream.WriteUint32L(iSwpInfo.Key());	
       
   225 	aWriteStream.WriteInt32L(iSwpInfo.Value());
       
   226 	aWriteStream << iFileName;
       
   227 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   228 	aWriteStream.WriteUint16L(iPriority);
       
   229 #endif
       
   230 	}
       
   231 
       
   232 /**
       
   233 Completes the tasks of the BIC
       
   234 */
       
   235 void CCmdCreateSwp::RunL()
       
   236 	{
       
   237 	SSMLOGLEAVEIFERROR(iStatus.Int());
       
   238 
       
   239 	TUid category = RProcess().SecureId();
       
   240 	_LIT_SECURITY_POLICY_PASS(KReadPolicy);
       
   241 	_LIT_SECURITY_POLICY_S0(KWritePolicy, RProcess().SecureId());
       
   242 	TInt err = RProperty::Define(category, iSwpInfo.Key(), RProperty::EInt, KReadPolicy, KWritePolicy);
       
   243 	if (KErrNone != err && KErrAlreadyExists != err)
       
   244 		{
       
   245 		SSMLOGLEAVE(err);
       
   246 		}
       
   247 	SSMLOGLEAVEIFERROR(RProperty::Set(category, iSwpInfo.Key(), iSwpInfo.Value()));
       
   248 	SSMLOGLEAVEIFERROR(iSsm.RegisterSwpMapping(iSwpInfo.Key(), iFileName));
       
   249 	CompleteExecuteRequest(KErrNone);	//control reaching here implies there are no errors
       
   250 	}
       
   251 
       
   252 /**
       
   253 Called to handle any cleanup if RunL leaves
       
   254 
       
   255 @param aError The error to finish with
       
   256 @return	KErrNone
       
   257 */
       
   258 TInt CCmdCreateSwp::RunError(TInt aError)
       
   259 	{
       
   260 	CompleteExecuteRequest(aError);
       
   261 	return KErrNone;
       
   262 	}
       
   263 
       
   264 /**
       
   265 Called during cancellation of the active BIC
       
   266 */
       
   267 void CCmdCreateSwp::DoCancel()
       
   268 	{
       
   269 	}
       
   270 
       
   271 /**
       
   272 Default constructor.
       
   273 */
       
   274 CCmdCreateSwp::CCmdCreateSwp()
       
   275 	: iSwpInfo(0, 0)
       
   276 	{
       
   277 	}
       
   278 
       
   279 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   280 /**
       
   281 Overloaded constructor
       
   282 
       
   283 @param aSeverity The severity of the command
       
   284 @param aSwpInfo The system wide property info
       
   285 @param aPriority The priority of the command in the list
       
   286 */
       
   287 CCmdCreateSwp::CCmdCreateSwp(TCmdErrorSeverity aSeverity, const TSsmSwp& aSwpInfo, const TUint16 aPriority)
       
   288 	: CSsmCommandBase(aSeverity, aPriority), iSwpInfo(aSwpInfo)
       
   289 	{
       
   290 	}
       
   291 #endif
       
   292 
       
   293 /**
       
   294 Overloaded constructor
       
   295 
       
   296 @param aSeverity The severity of the command
       
   297 @param aSwpInfo The system wide property info
       
   298 */
       
   299 CCmdCreateSwp::CCmdCreateSwp(TCmdErrorSeverity aSeverity, const TSsmSwp& aSwpInfo)
       
   300 	: CSsmCommandBase(aSeverity), iSwpInfo(aSwpInfo)
       
   301 	{
       
   302 	}
       
   303 
       
   304 /**
       
   305 Constructs the object through read stream.
       
   306 
       
   307 @param aFileName Name of the dll implementing the MSsmSwpPolicy interface.
       
   308 */
       
   309 void CCmdCreateSwp::ConstructL(const TDesC& aFileName)
       
   310 	{
       
   311 	iFileName.CreateL(aFileName.Length());
       
   312 	iFileName.Append(aFileName);
       
   313 
       
   314 	ValidateL();
       
   315 	}
       
   316 
       
   317 /**
       
   318 Constructs the object through read stream and validates the data.
       
   319 
       
   320 @param aReadStream A read stream containing BIC data
       
   321 */
       
   322 void CCmdCreateSwp::ConstructL(RReadStream& aReadStream)
       
   323 	{
       
   324 	InternalizeL(aReadStream);
       
   325 	ValidateL();
       
   326 	}
       
   327 
       
   328 /**
       
   329 Validate the commands data
       
   330 @leave KErrArgument If file name is not valid
       
   331 */
       
   332 void CCmdCreateSwp::ValidateL()
       
   333 	{
       
   334 	RFs fs;
       
   335 	SSMLOGLEAVEIFERROR(fs.Connect());
       
   336 	TBool isValidFileName = fs.IsValidName(iFileName);
       
   337 	fs.Close();
       
   338 	if (!isValidFileName)
       
   339 		{
       
   340 		SSMLOGLEAVE(KErrArgument);
       
   341 		}
       
   342 	}
       
   343 
       
   344 /**
       
   345 Constructs an object from resource file and validates the data..
       
   346 
       
   347 @param aCommandParameters Object data from a resource file
       
   348 */
       
   349 void CCmdCreateSwp::ConstructL(TSsmCommandParameters& aCommandParameters)
       
   350 	{
       
   351 	RResourceReader& reader = aCommandParameters.MainReader();
       
   352 
       
   353 	const TSsmCommandType type = static_cast<TSsmCommandType>(reader.ReadInt16L());
       
   354 	SSMLOGLEAVEIFFALSE(type == Type(), KErrNotSupported);
       
   355 	const TInt version = reader.ReadInt16L();
       
   356 	SSMLOGLEAVEIFFALSE(__COMPARE_VERSION(version, CCmdCreateSwp::MaxSupportedVersion()), KErrNotSupported);
       
   357 	iSeverity = static_cast<TCmdErrorSeverity>(reader.ReadInt16L());
       
   358 	const TUint key = {reader.ReadUint32L()};
       
   359 	const TInt32 value = reader.ReadInt32L();
       
   360 	const TSsmSwp swpInfo(key, value);
       
   361 	iSwpInfo = swpInfo;
       
   362 	const TPtrC fileName = reader.ReadTPtrCL();
       
   363 	ConstructL(fileName);
       
   364 #ifdef SYMBIAN_SSM_FLEXIBLE_MERGE
       
   365 	iPriority = (version > ECmdCreateSwpInitialVersion) ? reader.ReadUint16L() : KDefaultCommandPriority;	
       
   366 #endif
       
   367 	}
       
   368