localisation/apparchitecture/tef/TSidChecker/TestSidChecker.cpp
branchSymbian2
changeset 1 8758140453c0
child 6 c108117318cb
equal deleted inserted replaced
0:e8c1ea2c6496 1:8758140453c0
       
     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 the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TestSidChecker.h"
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <ImplementationProxy.h>
       
    20 #include <e32property.h>
       
    21 #include <e32test.h>
       
    22 #include <e32debug.h>
       
    23 #include <f32file.h>
       
    24 
       
    25 
       
    26 TBool E32Dll()
       
    27 	{
       
    28 	return (ETrue);
       
    29 	}
       
    30 
       
    31 const TImplementationProxy ImplementationTable[] =
       
    32 	{
       
    33 	IMPLEMENTATION_PROXY_ENTRY(0x10281FDE, CTestSidChecker::NewL)
       
    34 	};
       
    35 
       
    36 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
    37 	{
       
    38 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
    39 	return ImplementationTable;
       
    40 	}
       
    41 
       
    42 //////////////////////////////
       
    43 // private class declarations
       
    44 //////////////////////////////
       
    45 
       
    46 NONSHARABLE_CLASS(CApFileTestPropertyMonitor) : public CActive
       
    47 	{
       
    48 public:
       
    49 	static CApFileTestPropertyMonitor* NewL(TCallBack aCallBack);
       
    50 	~CApFileTestPropertyMonitor();
       
    51 	void Start();
       
    52 
       
    53 private:
       
    54 	CApFileTestPropertyMonitor(TCallBack aCallBack);
       
    55 	void RunL();
       
    56 	void DoCancel();
       
    57 
       
    58 private:
       
    59 	TCallBack iCallBack;
       
    60 	RProperty iProperty;
       
    61 	};
       
    62 
       
    63 NONSHARABLE_CLASS(CApFileTestOneShotTimer) : public CActive
       
    64 	{
       
    65 public:
       
    66 	static CApFileTestOneShotTimer* NewL(TCallBack aCallBack);
       
    67 	~CApFileTestOneShotTimer();
       
    68 	void Start(TTimeIntervalMicroSeconds32 aDelay);
       
    69 
       
    70 private:
       
    71 	CApFileTestOneShotTimer(TCallBack aCallBack);
       
    72 	void RunL();
       
    73 	void DoCancel();
       
    74 
       
    75 private:
       
    76 	TCallBack iCallBack;
       
    77 	RTimer iTimer;
       
    78 	};
       
    79 
       
    80 //////////////////////////////
       
    81 // CApFileTestPropertyMonitor
       
    82 //////////////////////////////
       
    83 
       
    84 CApFileTestPropertyMonitor* CApFileTestPropertyMonitor::NewL(TCallBack aCallBack)
       
    85 	{	
       
    86 	CApFileTestPropertyMonitor* self = new(ELeave) CApFileTestPropertyMonitor(aCallBack);
       
    87 	TInt err = self->iProperty.Attach(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey);
       
    88 	if(err != KErrNone)
       
    89 		{
       
    90 		delete self;
       
    91 		User::Leave(err);
       
    92 		}
       
    93 	return self;
       
    94 	}
       
    95 
       
    96 CApFileTestPropertyMonitor::CApFileTestPropertyMonitor(TCallBack aCallBack) :
       
    97 		CActive(EPriorityHigh),
       
    98 		iCallBack(aCallBack)
       
    99 	{
       
   100 	CActiveScheduler::Add(this);
       
   101 	}
       
   102 
       
   103 CApFileTestPropertyMonitor::~CApFileTestPropertyMonitor()
       
   104 	{
       
   105 	Cancel();
       
   106 	iProperty.Close();
       
   107 	}
       
   108 
       
   109 void CApFileTestPropertyMonitor::Start()
       
   110 	{
       
   111 	iProperty.Subscribe(iStatus);
       
   112 	SetActive();
       
   113 	}
       
   114 
       
   115 void CApFileTestPropertyMonitor::RunL()
       
   116 	{
       
   117 	if(iStatus.Int() != KErrNone)
       
   118 		{
       
   119 		RDebug::Print(_L("TestSidChecker: Property subcribe failed, error code: %i"), iStatus.Int());
       
   120 		}
       
   121 	iCallBack.CallBack();
       
   122 	}
       
   123 
       
   124 void CApFileTestPropertyMonitor::DoCancel()
       
   125 	{
       
   126 	iProperty.Cancel();
       
   127 	}
       
   128 
       
   129 //////////////////////////////
       
   130 // CApFileTestOneShotTimer
       
   131 //////////////////////////////
       
   132 
       
   133 CApFileTestOneShotTimer* CApFileTestOneShotTimer::NewL(TCallBack aCallBack)
       
   134 	{
       
   135 	CApFileTestOneShotTimer* self = new(ELeave) CApFileTestOneShotTimer(aCallBack);
       
   136 	TInt err = self->iTimer.CreateLocal();
       
   137 	if(err != KErrNone)
       
   138 		{
       
   139 		delete self;
       
   140 		User::Leave(err);
       
   141 		}
       
   142 	return self;
       
   143 	}
       
   144 
       
   145 CApFileTestOneShotTimer::CApFileTestOneShotTimer(TCallBack aCallBack) :
       
   146 		CActive(EPriorityHigh),
       
   147 		iCallBack(aCallBack)
       
   148 	{
       
   149 	CActiveScheduler::Add(this);
       
   150 	}
       
   151 
       
   152 CApFileTestOneShotTimer::~CApFileTestOneShotTimer()
       
   153 	{
       
   154 	Cancel();
       
   155 	iTimer.Close();
       
   156 	}
       
   157 
       
   158 void CApFileTestOneShotTimer::Start(TTimeIntervalMicroSeconds32 aDelay)
       
   159 	{
       
   160 	iTimer.After(iStatus,aDelay);
       
   161 	SetActive();
       
   162 	}
       
   163 
       
   164 void CApFileTestOneShotTimer::RunL()
       
   165 	{
       
   166 	if(iStatus.Int() != KErrNone)
       
   167 		{
       
   168 		RDebug::Print(_L("TestSidChecker: OneShotTimer Failed, error code: %i"), iStatus.Int());
       
   169 		}
       
   170 	iCallBack.CallBack();
       
   171 	}
       
   172 	
       
   173 void CApFileTestOneShotTimer::DoCancel()
       
   174 	{
       
   175 	iTimer.Cancel();
       
   176 	}
       
   177 
       
   178 //////////////////////////////
       
   179 // CTestSidChecker
       
   180 //////////////////////////////
       
   181 
       
   182 CTestSidChecker* CTestSidChecker::NewL()
       
   183 	{
       
   184 	CTestSidChecker* self = new(ELeave) CTestSidChecker();
       
   185 	return self;
       
   186 	}
       
   187 
       
   188 CTestSidChecker::CTestSidChecker()
       
   189 	{
       
   190 	}
       
   191 	
       
   192 CTestSidChecker::~CTestSidChecker()
       
   193 	{
       
   194 	}
       
   195 
       
   196 TBool CTestSidChecker::AppRegisteredAt(const TUid& aSid, TDriveUnit aDrive)
       
   197 	{
       
   198 	TBool ret = EFalse;
       
   199 	if(aSid == KApFileTestBadApp)
       
   200 		{
       
   201 		ret = EFalse;
       
   202 		}
       
   203 	else if(aSid == KApFileTestGoodApp)
       
   204 		{
       
   205 		#ifdef __WINS__
       
   206 		TDriveUnit drive(EDriveX);
       
   207 		#else
       
   208 		TDriveUnit drive(EDriveX);
       
   209 		RFs	fs;
       
   210 		User::LeaveIfError(fs.Connect());
       
   211 		CleanupClosePushL(fs);
       
   212 		//The removable media is expected at D: on NAND ROM and at E: on normal ROMs.
       
   213 		//The following code works on techview but not guaranteed to work on all platforms. 
       
   214 		TDriveInfo driveInfo;
       
   215 		TInt error = fs.Drive(driveInfo, EDriveD);
       
   216 		if(error == KErrNone && ((driveInfo.iDriveAtt & KDriveAttRemovable) != 0))
       
   217 			{
       
   218 		 	// Use drive D
       
   219 		 	drive = EDriveD;
       
   220 		 	}
       
   221 		 else
       
   222 		 	{
       
   223 			error = fs.Drive(driveInfo, EDriveE);
       
   224 			if(error == KErrNone && ((driveInfo.iDriveAtt & KDriveAttRemovable) != 0))
       
   225 				{
       
   226 			 	// Use drive E
       
   227 			 	drive = EDriveE;
       
   228 			 	}
       
   229 			}
       
   230 		fs.Close();
       
   231 		CleanupStack::PopAndDestroy(&fs);
       
   232 		#endif
       
   233 		ret = (aDrive == drive);
       
   234 		}
       
   235 	else if(aSid == KApFileTestCallBackApp)
       
   236 		{
       
   237 		ret = ETrue;
       
   238 		CallBackAppChecked();
       
   239 		}
       
   240 	else if(aSid == KApFileTestForcedRegistration)
       
   241 		{
       
   242 		ret = EFalse;
       
   243 		}
       
   244 	else if(aSid == KApTriggerRescan)
       
   245 		{
       
   246 		ret = ETrue;
       
   247 		if (!iRescanTriggered)
       
   248 			{
       
   249 			iRescanTriggered = ETrue;
       
   250 			iRescanCallBack.CallBack();
       
   251 			}
       
   252 		}
       
   253 	else if(aSid == KApFileTestAppWithMultipleRegistrationFiles)
       
   254 		{
       
   255 		ret = ETrue;
       
   256 		AppWithMultipleRegistrationFilesChecked();
       
   257 		}
       
   258 	else
       
   259 		{
       
   260 		Error(_L("TestSidChecker: Unknown sid %x"), aSid.iUid);
       
   261 		}
       
   262 	return ret;
       
   263 	}
       
   264 	
       
   265 void CTestSidChecker::SetRescanCallBackL(const TCallBack &aCallBack)
       
   266 	{
       
   267 	iRescanCallBack = aCallBack;
       
   268 	if(aCallBack.iFunction)
       
   269 		{
       
   270 		iMonitor = CApFileTestPropertyMonitor::NewL(TCallBack(&PropertyEventCallBack,this));
       
   271 		iTimer = CApFileTestOneShotTimer::NewL(TCallBack(&TimerEventCallBack,this));
       
   272 		}
       
   273 	}
       
   274 
       
   275 void CTestSidChecker::AppWithMultipleRegistrationFilesChecked()
       
   276 	{
       
   277 		TInt value;
       
   278 		TInt err = RProperty::Get(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, value);
       
   279 		if(err == KErrNone)
       
   280 			{
       
   281 			if(value == ECheckedOnce)
       
   282 				{
       
   283 				TInt err = RProperty::Set(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, ECheckedTwice);
       
   284 				if(err != KErrNone)
       
   285 					{
       
   286 					Error(_L("TestSidChecker: Property set failed, error code: %i"), err);
       
   287 					}
       
   288 				}
       
   289 			else if(value == ECheckedTwice)
       
   290 				{
       
   291 				TInt err = RProperty::Set(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, ECheckedThrice);
       
   292 				if(err != KErrNone)
       
   293 					{
       
   294 					Error(_L("TestSidChecker: Property set failed, error code: %i"), err);
       
   295 					}
       
   296 				}
       
   297 			else
       
   298 				{
       
   299 				TInt err = RProperty::Set(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, ECheckedOnce);
       
   300 				if(err != KErrNone)
       
   301 					{
       
   302 					Error(_L("TestSidChecker: Property set failed, error code: %i"), err);
       
   303 					}
       
   304 				}
       
   305 			}
       
   306 	}
       
   307 	
       
   308 void CTestSidChecker::CallBackAppChecked()
       
   309 	{
       
   310 	if(iState == ENotStarted)
       
   311 		{
       
   312 		TInt value;
       
   313 		TInt err = RProperty::Get(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, value);
       
   314 		if(err == KErrNone)
       
   315 			{
       
   316 			if(value == EPluginLoad)
       
   317 				{
       
   318 				iMonitor->Start();
       
   319 				iState = EMonitorStarted;
       
   320 				}
       
   321 			else
       
   322 				{
       
   323 				Error(_L("TestSidChecker: Bad property value %u"), value);
       
   324 				}
       
   325 			}
       
   326 		else
       
   327 			{
       
   328 			// don't fail here, it's probably just a stale reg file.
       
   329 			}
       
   330 		}
       
   331 	else if(iState == EMonitorStarted)
       
   332 		{
       
   333 		// do nothing, it's a random scan happening before we start one manually
       
   334 		}
       
   335 	else if(iState == EScanStarted)
       
   336 		{
       
   337 		iState = EAppChecked;
       
   338 		TInt err = RProperty::Set(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, EScanOccurred);
       
   339 		if(err != KErrNone)
       
   340 			{
       
   341 			Error(_L("TestSidChecker: Property set failed, error code: %i"), err);
       
   342 			}
       
   343 		}
       
   344 	else
       
   345 		{
       
   346 		Error(_L("TestSidChecker: Unexpected state %u"), iState);
       
   347 		}
       
   348 	}
       
   349 
       
   350 TInt CTestSidChecker::PropertyEventCallBack(TAny* aSelf)
       
   351 	{	
       
   352 	return STATIC_CAST(CTestSidChecker*,aSelf)->PropertyEvent();
       
   353 	}
       
   354 
       
   355 TInt CTestSidChecker::PropertyEvent()
       
   356 	{
       
   357 	if(iState == EMonitorStarted)
       
   358 		{
       
   359 		TInt value;
       
   360 		TInt err = RProperty::Get(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, value);
       
   361 		if(err == KErrNone)
       
   362 			{
       
   363 			if(value == ETriggerScan)
       
   364 				{
       
   365 				User::After(1000000); // wait a second for the testexecute to subscribe to the property
       
   366 				iTimer->Start(7 * 1000000); // wait 7 seconds before assuming the callback has failed
       
   367 				iState = EScanStarted;
       
   368 				iRescanCallBack.CallBack();
       
   369 				}
       
   370 			else
       
   371 				{
       
   372 				Error(_L("TestSidChecker: Bad property value %u"), value);
       
   373 				}
       
   374 			}
       
   375 		else
       
   376 			{
       
   377 			Error(_L("TestSidChecker: PropertyGetFailed, error code: %i"), err);
       
   378 			}
       
   379 		}
       
   380 	else
       
   381 		{
       
   382 		Error(_L("TestSidChecker: Unexpected State %u"), iState);
       
   383 		}
       
   384 
       
   385 	return KErrNone;
       
   386 	}
       
   387 
       
   388 TInt CTestSidChecker::TimerEventCallBack(TAny* aSelf)
       
   389 	{
       
   390 	return STATIC_CAST(CTestSidChecker*,aSelf)->TimerEvent();
       
   391 	}
       
   392 
       
   393 TInt CTestSidChecker::TimerEvent()
       
   394 	{
       
   395 	if(iState == EScanStarted)
       
   396 		{
       
   397 		TInt err = RProperty::Set(KApFileTestPubSubCategory, KApFileTestPubSubCallBackKey, ETimedOut);
       
   398 		if(err != KErrNone)
       
   399 			{
       
   400 			Error(_L("TestSidChecker: Property set failed, error code: %i"), err);
       
   401 			}
       
   402 		}
       
   403 	else if(iState != EAppChecked)
       
   404 		{
       
   405 		Error(_L("TestSidChecker: Unexpected state %u"), iState);
       
   406 		}
       
   407 
       
   408 	return KErrNone;
       
   409 	}
       
   410 	
       
   411 void CTestSidChecker::Error(TRefByValue<const TDesC> aFmt, TInt aValue)
       
   412 	{
       
   413 	// Something rather unexpected happened. Give up, keep quiet.
       
   414 	RDebug::Print(aFmt,aValue);
       
   415 	iTimer->Cancel();
       
   416 	iMonitor->Cancel();
       
   417 	iState = ECallBackTestError;
       
   418 	}