messagingfw/msgtest/targetautomation/TechviewStart/Starter.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 1997-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 "Starter.h"
       
    17 #include "AppStarter.h"
       
    18 #include "ThreadWatch.h"
       
    19 #include <e32property.h>
       
    20 #include "MsvTestUtils.h"
       
    21 #include <Start.rsg>
       
    22 #include "Start.hrh"
       
    23 
       
    24 typedef TInt (*ShellEntryPoint)(TAny *);
       
    25 #if defined(__WINS__)
       
    26 LOCAL_D const TUint KHeapSize=0x10000;
       
    27 LOCAL_D const TUint KMaxHeapSize=0x400000;
       
    28 #endif
       
    29 
       
    30 
       
    31 _LIT(KStubRunResourceFileName, "Z:\\SYSTEM\\PROGRAMS\\Start.RSC");
       
    32 _LIT(KGlobalSemaphore, "SPLASH");
       
    33 _LIT(KStartChunkName,"Start");
       
    34 
       
    35 const TInt KRetryTime = 100000;
       
    36 
       
    37 /**
       
    38 @internalComponent
       
    39 */
       
    40 TBool definedPhonePwrValue = EFalse;
       
    41 
       
    42 
       
    43 /**
       
    44 * Contructor
       
    45 */
       
    46 CStarter::CStarter() :	CActive(EPriorityStandard), iNrSemaphore(0)
       
    47 	{}
       
    48 
       
    49 /**
       
    50 * Destructor.
       
    51 */
       
    52 CStarter::~CStarter()
       
    53 	{
       
    54 	if (iQueHeader)
       
    55 		{
       
    56 		while (!(iQueHeader->IsEmpty()))
       
    57 			{
       
    58 			CThreadWatcher* threadWatcher = iQueHeader->First();
       
    59 			iQueHeader->Remove(*threadWatcher);
       
    60 			delete threadWatcher;
       
    61 			}
       
    62 		delete iQueHeader;
       
    63 		}
       
    64 
       
    65 	iLsSess.Close();
       
    66 #if defined (__WINS__)
       
    67 	iAppUiSession.Close();
       
    68 #endif
       
    69 	delete iStartupList;
       
    70 	delete iExtrasList;
       
    71 	delete iSplashExe;
       
    72 	iCmdLineArgs.Close();
       
    73 	iResourceFile.Close();
       
    74 	iFsSession.Close();
       
    75 	}
       
    76 
       
    77 /**
       
    78 * Creates an instance of a CStarter object.
       
    79 * @return			a pointer to this object
       
    80 */
       
    81 CStarter* CStarter::NewL()
       
    82 	{
       
    83 	CStarter* self = new (ELeave) CStarter;
       
    84 	CleanupStack::PushL(self);
       
    85 	self->ConstructL();
       
    86 	CleanupStack::Pop();
       
    87 	return self;
       
    88 	}
       
    89 
       
    90 /**
       
    91 * Handles the actual initialization of this object.
       
    92 */
       
    93 void CStarter::ConstructL()
       
    94 	{
       
    95 	// Connect to the File Server
       
    96 	while (iFsSession.Connect() != KErrNone)
       
    97 		{
       
    98 		User::After(KRetryTime);
       
    99 		}
       
   100 
       
   101 	//Connected to File Server
       
   102 	iResourceFile.OpenL(iFsSession, KStubRunResourceFileName);
       
   103 
       
   104 	// read the first one
       
   105     HBufC8* res = iResourceFile.AllocReadLC(R_APP_START_PARAMS);
       
   106     TResourceReader theReader;
       
   107     theReader.SetBuffer(res);
       
   108 	//Main configuration resource read
       
   109 
       
   110 	// get data from it
       
   111 	iSplashExe = theReader.ReadHBufCL();
       
   112 
       
   113 	TUint32 applicationsListId = theReader.ReadInt32();
       
   114 	TUint32 exeListId = theReader.ReadInt32();
       
   115 
       
   116 	// free up memory
       
   117 	CleanupStack::PopAndDestroy();	// res
       
   118 
       
   119 	// prepare the data for the active object
       
   120 	iStartupList = iResourceFile.AllocReadL(applicationsListId);
       
   121     iStartupListReader.SetBuffer(iStartupList);
       
   122 	iStartupCount = iStartupListReader.ReadUint16();	//The size of the resource array
       
   123 
       
   124 	iExtrasList= iResourceFile.AllocReadL(exeListId);
       
   125     iExtrasListReader.SetBuffer(iExtrasList);
       
   126 	iExtrasCount = iExtrasListReader.ReadUint16();	//The size of the resource array
       
   127 
       
   128 	iQueHeader = new (ELeave) TSglQue<CThreadWatcher>(_FOFF(CThreadWatcher,iLink));
       
   129 
       
   130 	// Tell system agent the phone is on.
       
   131 	// This is only appropriate for 2-box solutions, and is needed for SMS sending and receiving.
       
   132 	// Also, at some point, this should probably be moved to a separate component that startup launches.
       
   133 	TInt testState;
       
   134 	if(KErrNone != RProperty::Get(KUidSystemCategory, KMsvTestUidPhonePwrValue, testState))
       
   135 		{
       
   136 		User::LeaveIfError(RProperty::Define(KUidSystemCategory, KMsvTestUidPhonePwrValue, RProperty::EInt));
       
   137 		definedPhonePwrValue = ETrue;
       
   138 		}
       
   139 	User::LeaveIfError(RProperty::Set(KUidSystemCategory, KMsvTestUidPhonePwrValue, EMsvTestPhoneOn));
       
   140 
       
   141 	if(definedPhonePwrValue)
       
   142 		{
       
   143 		User::LeaveIfError(RProperty::Delete(KUidSystemCategory, KMsvTestUidPhonePwrValue));
       
   144 		}
       
   145 
       
   146 	RChunk chunk;
       
   147 	TUint8 finish=0;
       
   148 	if(	chunk.CreateGlobal(KStartChunkName,1,1)== KErrNone)
       
   149 		{
       
   150 		if(	chunk.OpenGlobal(KStartChunkName,EFalse)== KErrNone)
       
   151 			{
       
   152 			TUint8* base=chunk.Base();
       
   153 			*base=finish;
       
   154 			chunk.Close();
       
   155 			}
       
   156 		}
       
   157 	}
       
   158 
       
   159 /**
       
   160 * Actually launch the splash screen exe
       
   161 */
       
   162 void CStarter::StartSplashScreenL()
       
   163 	{
       
   164 	if (iSplashExe == NULL || iSplashExe->Length() == 0)
       
   165 		{
       
   166 		return;
       
   167 		}
       
   168 
       
   169 	// launch the splash screen
       
   170 	TPtrC splashCmd;
       
   171 	splashCmd.Set(*iSplashExe);
       
   172 
       
   173 #if defined(__WINS__)
       
   174 	TParse fileName;
       
   175 	User::LeaveIfError(fileName.Set(_L(".DLL"),&splashCmd,NULL));
       
   176 	User::LeaveIfError(iLib.Load(fileName.FullName()));
       
   177 	ShellEntryPoint libFunc=(ShellEntryPoint)iLib.Lookup(1);
       
   178 	if (!libFunc)
       
   179 		{
       
   180 		User::Leave(KErrGeneral);
       
   181 		}
       
   182 	TBuf<256> name;
       
   183 	TInt num=0;
       
   184 	TInt ret=KErrNone;
       
   185 	do
       
   186 		{
       
   187 		name.Format(TRefByValue<const TDesC>(_L("Splash%d")),num++);
       
   188 		ret=iSplash.Create(name,libFunc,KDefaultStackSize,&splashCmd,&iLib,NULL,/*NULL,*/KHeapSize,KMaxHeapSize,EOwnerProcess);
       
   189 		} while(ret==KErrAlreadyExists);
       
   190 	User::LeaveIfError(ret);
       
   191 #else
       
   192 	TFindFile findShell(iFsSession);
       
   193 	User::LeaveIfError(findShell.FindByDir(splashCmd,_L("\\System\\Programs\\.EXE")));
       
   194 	//User::LeaveIfError(iSplash.Create(findShell.File(),splashCmd));
       
   195 	TInt err1 = 0;
       
   196 	TRAPD(err,(err1 = iSplash.Create(findShell.File(),splashCmd)));	//Change back to commented out line when E32 bug is fixed ###
       
   197 	if (err!=KErrNone && err1 != KErrNone)
       
   198 		{
       
   199 		iSplash.SetHandle(KNullHandle);
       
   200 		User::Leave(err);
       
   201 		}
       
   202 #endif
       
   203 	iSplash.Resume();
       
   204 
       
   205 	}
       
   206 
       
   207 /**
       
   208 * Method to be called to procced the startup
       
   209 */
       
   210 void CStarter::Start()
       
   211 	{
       
   212 	iStage = ESplashScreen;
       
   213 	RequestNotify();
       
   214 	}
       
   215 
       
   216 /**
       
   217 * Start then connect the Eikon server
       
   218 */
       
   219 void CStarter::Connect()
       
   220 	{
       
   221 	// Connect to the AppUi session, effective starting it.
       
   222 	while(iAppUiSession.Connect() != KErrNone)
       
   223 		{
       
   224 		User::After(KRetryTime);
       
   225 		}
       
   226 
       
   227 	// Connect to the AppLsServer
       
   228 	while(iLsSess.Connect() != KErrNone)
       
   229 		{
       
   230 		User::After(KRetryTime);
       
   231 		}
       
   232 
       
   233 	TInt count=0;
       
   234 	while(iLsSess.GetAllApps() != KErrNone) //collect data
       
   235 		{
       
   236 		}
       
   237   	while(iLsSess.AppCount(count) != KErrNone) //collect data
       
   238 		{
       
   239 		}
       
   240 	RequestNotify();
       
   241 	}
       
   242 
       
   243 /**
       
   244 * Proceed the next application to be launched
       
   245 */
       
   246 void CStarter::LaunchNextStartupAppL()
       
   247 	{
       
   248 	TUint32 applicationUid = iStartupListReader.ReadUint32();
       
   249 	TFullName exeFilePath = iStartupListReader.ReadTPtrC();
       
   250 	TFullName dllFilePath = iStartupListReader.ReadTPtrC();
       
   251 	iCmdLineArgs.Append( iStartupListReader.ReadTPtrC());
       
   252 	TDesC *commandLineArgs = &iCmdLineArgs[ iCmdLineArgs.Count()-1];
       
   253 
       
   254 	TUint32 stackSize = iStartupListReader.ReadUint32();
       
   255 	stackSize = stackSize ? stackSize : KDefaultStackSize;
       
   256 	TUint32 minHeapSize = iStartupListReader.ReadUint32();
       
   257 	minHeapSize = minHeapSize ? minHeapSize : KMinHeapSize;
       
   258 	TUint32 maxHeapSize = iStartupListReader.ReadUint32();
       
   259 
       
   260 	TBool monitored = (TUint16)iStartupListReader.ReadUint16();
       
   261 	TBool semaphore = (TUint16)iStartupListReader.ReadUint16();
       
   262 	TBool viewless = (TUint16)iStartupListReader.ReadUint16();
       
   263 	TInt bootType  = (TUint16)iStartupListReader.ReadUint16();
       
   264 			
       
   265 	if(semaphore)
       
   266 		{
       
   267 		iNrSemaphore++; //increase with one.
       
   268 		iWaiting++;
       
   269 		}
       
   270 
       
   271 	iStartupCount--; // next app
       
   272 	CAppStarter::NewLD(bootType, applicationUid, exeFilePath, dllFilePath, commandLineArgs,
       
   273 						stackSize, minHeapSize, maxHeapSize, monitored, viewless,
       
   274 						semaphore, iQueHeader, iLsSess, this);
       
   275 	}
       
   276 
       
   277 /**
       
   278 * Proceed the next extra app to be launched
       
   279 */
       
   280 void CStarter::LaunchNextExtrasL()
       
   281 	{
       
   282 	TUint32 applicationUid = iExtrasListReader.ReadUint32();
       
   283 	TFullName exeFilePath = iExtrasListReader.ReadTPtrC();
       
   284 	TFullName dllFilePath = iExtrasListReader.ReadTPtrC();
       
   285 	iCmdLineArgs.Append( iExtrasListReader.ReadTPtrC());
       
   286 	TDesC *commandLineArgs = &iCmdLineArgs[ iCmdLineArgs.Count()-1];
       
   287 
       
   288 	TUint32 stackSize = iExtrasListReader.ReadUint32();
       
   289 	stackSize = stackSize ? stackSize : KDefaultStackSize;
       
   290 	TUint32 minHeapSize = iExtrasListReader.ReadUint32();
       
   291 	minHeapSize = minHeapSize ? minHeapSize : KMinHeapSize;
       
   292 	TUint32 maxHeapSize = iExtrasListReader.ReadUint32();
       
   293 	
       
   294 	TBool monitored = (TUint16)iExtrasListReader.ReadUint16();
       
   295 	TBool viewless = (TUint16)iExtrasListReader.ReadUint16();
       
   296 	TInt bootType  = (TUint16)iExtrasListReader.ReadUint16();
       
   297 
       
   298 	iExtrasCount--; // next app
       
   299 	CAppStarter::NewLD(bootType, applicationUid, exeFilePath, dllFilePath, commandLineArgs,
       
   300 						stackSize, minHeapSize, maxHeapSize, monitored, viewless,
       
   301 						EFalse, iQueHeader, iLsSess, this);
       
   302 	}
       
   303 
       
   304 void CStarter::RestartMonitoredThreadL(TInt appType, TThreadId& aThreadId, TDesC& aExeFileName, TUint32 aAppUid, TBool aViewless)
       
   305   	{
       
   306   	if(appType == EExecutableType) 
       
   307   		{
       
   308   		User::LeaveIfError(iLsSess.StartDocument(aExeFileName, TUid::Uid(0), aThreadId));
       
   309   		} 
       
   310   	else if(aViewless)
       
   311   		{
       
   312   		TApaAppInfo info;
       
   313   		User::LeaveIfError(iLsSess.GetAppInfo(info,TUid::Uid(aAppUid)));
       
   314   		CApaCommandLine* cmdLine=CApaCommandLine::NewLC();	
       
   315   		cmdLine->SetLibraryNameL(info.iFullName);
       
   316   		cmdLine->SetCommandL(EApaCommandRunWithoutViews);
       
   317   		User::LeaveIfError(iLsSess.StartApp(*cmdLine, aThreadId));
       
   318   		CleanupStack::PopAndDestroy();	// cmdLine
       
   319 		}
       
   320   	else
       
   321   		{
       
   322   		TApaAppInfo info;
       
   323   		User::LeaveIfError(iLsSess.GetAppInfo(info,TUid::Uid(aAppUid)));
       
   324   		TFileName fName;
       
   325   		if (info.iUid!=KNullUid)
       
   326   			{
       
   327   			User::LeaveIfError(iLsSess.StartDocument(fName, TUid::Uid(aAppUid), aThreadId));
       
   328   			}
       
   329   		}
       
   330    	}
       
   331 
       
   332 /**
       
   333 * Method called when the requested step have been procced
       
   334 */
       
   335 void CStarter::RunL()
       
   336 	{
       
   337 	switch (iStage)
       
   338 		{
       
   339 		case ESplashScreen:
       
   340 			{
       
   341 			iStage =  EStarting;
       
   342 			// don't care about the error - for now
       
   343 			TRAPD(err, StartSplashScreenL());
       
   344 			iSplashStarted = (err == KErrNone);
       
   345 			RequestNotify();		
       
   346 			}
       
   347 			break;
       
   348 		case EStarting:
       
   349 			iStage = EConnected;
       
   350 			Connect();
       
   351 			break;
       
   352 		case EConnected:
       
   353 			{
       
   354 			iStage = EStart;
       
   355 			iSema.CreateGlobal(KGlobalSemaphore, EOwnerProcess); //For Shell
       
   356 			RequestNotify();
       
   357 			}
       
   358 			break;
       
   359 		case EStart:
       
   360 			{
       
   361 			iStage = EStart;
       
   362 			if (iStartupCount)
       
   363 				{
       
   364 				LaunchNextStartupAppL();
       
   365 				RequestNotify();
       
   366 				}
       
   367 			break;
       
   368 		case EWait:
       
   369  			iStage = EStartExtras;
       
   370 			if(iSema.OpenGlobal(KGlobalSemaphore, EOwnerProcess) == KErrNone)
       
   371 				{
       
   372 				while(iNrSemaphore > 0) //Defined in the resourcefile for apps that has to be started before the splash screen is removed
       
   373 					{
       
   374 					iSema.Wait();
       
   375 					iNrSemaphore--;
       
   376 					}
       
   377 				iSema.Close();
       
   378 				}
       
   379 
       
   380 			if(iSplashStarted)
       
   381 				{
       
   382 				//use chunk to tell splash screen to stop by setting to 1
       
   383 				RChunk chunk;
       
   384 				TUint8 finish=1;
       
   385 				if(	chunk.OpenGlobal(KStartChunkName,EFalse)== KErrNone)
       
   386 					{
       
   387 					TUint8* base=chunk.Base();
       
   388 					*base=finish;
       
   389 					chunk.Close();
       
   390 					}
       
   391 				}
       
   392 
       
   393 			RequestNotify();
       
   394 			}
       
   395 			break;
       
   396 		case EStartExtras:
       
   397 			if(iExtrasCount)
       
   398 				{
       
   399 				LaunchNextExtrasL();
       
   400 				RequestNotify();
       
   401 				}
       
   402 			break;
       
   403 		default: 
       
   404 			iStage = ESplashScreen;
       
   405 			break;
       
   406 		}
       
   407 	}
       
   408 
       
   409 TInt CStarter::RunError(TInt /*aError*/)
       
   410 	{
       
   411 	return KErrNone;
       
   412 	}
       
   413 
       
   414 void CStarter::Done(TBool aSuccess)
       
   415 	{
       
   416 	if(aSuccess)
       
   417 		{
       
   418 		iWaiting--;
       
   419 		}
       
   420 	else
       
   421 		{
       
   422 		iWaiting--;
       
   423 		iNrSemaphore--;
       
   424 		}
       
   425 	if(iWaiting <= 0)
       
   426 		{
       
   427 		iStage = EWait;
       
   428 		RequestNotify();
       
   429 		}
       
   430 	}
       
   431 /**
       
   432 * Cancel the execution
       
   433 */
       
   434 void CStarter::DoCancel()
       
   435 	{
       
   436 	TRequestStatus* status = &iStatus;
       
   437 	User::RequestComplete(status, KErrCancel);
       
   438 	}
       
   439 
       
   440 void CStarter::RequestNotify()
       
   441 	{
       
   442 	TRequestStatus* status = &iStatus;
       
   443 	User::RequestComplete(status, KErrNone);
       
   444 	SetActive();
       
   445 	}