messagingfw/msgtest/targetautomation/TechviewStart/AppStarter.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2001-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 "AppStarter.h"
       
    17 #include "Starter.h"
       
    18 #include "ThreadWatch.h"
       
    19 #include "Start.hrh"
       
    20 
       
    21 #if defined(__WINS__)
       
    22 _LIT(KExtension, ".dll");
       
    23 #else 
       
    24 _LIT(KExtension, ".exe");
       
    25 #endif
       
    26 
       
    27 _LIT(KGlobalSemaphore, "SPLASH");
       
    28 
       
    29 const TInt KRetryWait = 10*1000;
       
    30 const TInt KMaxRetries = (10*1000*100)/KRetryWait;
       
    31 
       
    32 void CAppStarter::NewLD(TInt aAppType, TUint32 aAppUid, TDesC& aExeFileName, TDesC& aDllFileName, TDesC* aCmdLineArgs, TUint32 aStackSize,
       
    33 						TUint32 aMinHeapSize, TUint32 aMaxHeapSize, TBool aMonitoring, TBool aViewless, TBool aSemaphore,
       
    34 						TSglQue<CThreadWatcher>* aQue, RApaLsSession& aSession, CStarter* aStarter)
       
    35 	{
       
    36 	CAppStarter* self = new(ELeave)CAppStarter(aSession);
       
    37 	self->iAppType = aAppType;
       
    38 	self->iAppUid = aAppUid;
       
    39 	self->iExeFileName = aExeFileName;
       
    40 	self->iDllFileName = aDllFileName;
       
    41 	self->iCmdLineArgs = aCmdLineArgs;
       
    42 	self->iStackSize = aStackSize;
       
    43 	self->iMinHeapSize = aMinHeapSize;
       
    44 	self->iMaxHeapSize = aMaxHeapSize;	
       
    45 	self->iMonitoring = aMonitoring;
       
    46 	self->iViewless = aViewless;
       
    47 	self->iSemaphore = aSemaphore;
       
    48 	self->iQue = aQue;
       
    49 	self->iStarter = aStarter;
       
    50 
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53 	CleanupStack::Pop(self);
       
    54 	CActiveScheduler::Add(self);
       
    55 	self->RunL();
       
    56 	}
       
    57 
       
    58 void CAppStarter::RunL()
       
    59 	{
       
    60 	if(iRetries++ > KMaxRetries)
       
    61 		{
       
    62 		iState = EAppFailed;
       
    63 		}
       
    64 
       
    65 	switch(iState)
       
    66 		{
       
    67 		case EStartApp:
       
    68 			StartL();
       
    69 			break;
       
    70 		case EAppStarted:
       
    71 			Done(ETrue);
       
    72 			break;
       
    73 		default:
       
    74 		case EAppFailed:
       
    75 			Done(EFalse);
       
    76 			break;
       
    77 		}
       
    78 	}
       
    79 
       
    80 CAppStarter::CAppStarter(RApaLsSession& aSession) : CTimer(EPriorityStandard), iState(EStartApp), iSession(aSession), iRetries(0)
       
    81 	{
       
    82 
       
    83 	}
       
    84 
       
    85 CAppStarter::~CAppStarter()
       
    86 	{
       
    87 
       
    88 	}
       
    89 
       
    90 TInt CAppStarter::RunError(TInt /*aError*/)
       
    91 	{
       
    92 	return KErrNone;
       
    93 	}
       
    94 
       
    95 void CAppStarter::StartL()
       
    96 	{
       
    97 	TFileName newname = iExeFileName;
       
    98 
       
    99 	TThreadId threadId = 0;
       
   100 
       
   101 	switch(iAppType)
       
   102 		{
       
   103 		case EApplicationType:
       
   104 			{
       
   105 			TApaAppInfo info;
       
   106 			if(iSession.GetAppInfo(info,TUid::Uid(iAppUid)) != KErrNone)
       
   107 				{
       
   108 				After(KRetryWait);
       
   109 				return;
       
   110 				}
       
   111 			
       
   112 			if(iViewless)
       
   113 				{
       
   114 				TRAPD(err, TryStartViewlessAppL(info, threadId));
       
   115 				if (err != KErrNone)
       
   116 					{
       
   117 					After(KRetryWait);
       
   118 					return; //ignore error
       
   119 					}
       
   120 				}
       
   121 			else if (info.iUid!=KNullUid)
       
   122 				{
       
   123 				if(iSession.StartDocument(newname, TUid::Uid(iAppUid), threadId) != KErrNone)
       
   124 					{
       
   125 					After(KRetryWait);
       
   126 					return;
       
   127 					}
       
   128 				}
       
   129 			else
       
   130 				{
       
   131 				iState = EAppFailed;
       
   132 				Next();
       
   133 				}
       
   134 			}
       
   135 			break;
       
   136 		case EExecutableType:
       
   137 			{
       
   138 			newname.Append(KExtension);
       
   139 			if(iSession.StartDocument(newname, TUid::Uid(0), threadId) != KErrNone)
       
   140 				{
       
   141 				After(KRetryWait);
       
   142 				return;
       
   143 				}
       
   144 			}
       
   145 			break;
       
   146 		case ECmdLnArgExecutableType:
       
   147 			{
       
   148 			ASSERT( !iMonitoring); // Not imlpemented yet
       
   149 #if defined(__WINS__) || defined(__WINSCW__)
       
   150 			TName libName = iDllFileName;
       
   151 			libName.Append(KExtension);
       
   152 			RLibrary lib;
       
   153 			TInt error = lib.Load(libName);
       
   154 			if (error!=KErrNone)
       
   155 				{
       
   156 				After(KRetryWait);
       
   157 				return;
       
   158 				}
       
   159 			TThreadFunction serverFunc=reinterpret_cast<TThreadFunction>(lib.Lookup(1));
       
   160 			RThread server;
       
   161 			error=server.Create(libName,serverFunc, iStackSize, iCmdLineArgs, &lib,NULL,
       
   162 								iMinHeapSize,iMaxHeapSize,EOwnerProcess);
       
   163 			lib.Close();	// if successful, server thread has handle to library now
       
   164 #else
       
   165 			RProcess server;
       
   166 			TInt error = server.Create(newname, *iCmdLineArgs);
       
   167 #endif
       
   168 			if( error != KErrNone)
       
   169 				{
       
   170 				After(KRetryWait);
       
   171 				return;
       
   172 				}
       
   173 			server.Resume();
       
   174 			server.Close();
       
   175 			}
       
   176 			break;
       
   177 		default:
       
   178 			iState = EAppFailed;
       
   179 			Next();
       
   180 		}
       
   181 	
       
   182 	if (iMonitoring)
       
   183 		{
       
   184 		CThreadWatcher *threadWatcher=NULL;
       
   185 		TRAPD(err, threadWatcher = CThreadWatcher::NewL(iAppType, threadId, newname, iStarter, iAppUid, iViewless));
       
   186 		if (err == KErrNone) //ignore errors
       
   187 			iQue->AddLast(*threadWatcher);
       
   188 		}
       
   189 	iState = EAppStarted;
       
   190 	Next();
       
   191 	}
       
   192 
       
   193 void CAppStarter::Done(TBool aSuccess)
       
   194 	{
       
   195 	if(iSemaphore)
       
   196 		{
       
   197 		iStarter->Done(aSuccess);
       
   198 		}
       
   199 	delete this;
       
   200 	}
       
   201 
       
   202 void CAppStarter::Next()
       
   203 	{
       
   204 	TRequestStatus* status = &iStatus;
       
   205 	User::RequestComplete(status, KErrNone);
       
   206 	SetActive();
       
   207 	}
       
   208 	
       
   209 void CAppStarter::TryStartViewlessAppL(TApaAppInfo aInfo, TThreadId aThreadId)
       
   210 	{
       
   211 	CApaCommandLine* cmdLine=CApaCommandLine::NewLC();
       
   212 	cmdLine->SetLibraryNameL(aInfo.iFullName);
       
   213 	cmdLine->SetCommandL(EApaCommandRunWithoutViews);
       
   214 	User::LeaveIfError(iSession.StartApp(*cmdLine, aThreadId));
       
   215 	CleanupStack::PopAndDestroy();	// cmdLine
       
   216 	}