appfw/apparchitecture/apstart/apstart.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     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 "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 <apacmdln.h>
       
    17 #include "apstart.h"
       
    18 #include "apststd.h"
       
    19 
       
    20 const TInt KAppArcAppListInitialCompletionTimeout = 10000000; //10s
       
    21 
       
    22 CApaAppStart::CApaAppStart()
       
    23 	{
       
    24 	}
       
    25 
       
    26 CApaAppStart::~CApaAppStart()
       
    27 	{
       
    28 	iApaLsSession.Close();
       
    29 	}
       
    30 
       
    31 /**
       
    32 Used to create an instance of @c CApaAppStart class
       
    33 
       
    34 @return An instance of @c CApaAppStart
       
    35 */
       
    36 EXPORT_C CApaAppStart* CApaAppStart::NewL()
       
    37 	{
       
    38  	CApaAppStart* self = new(ELeave) CApaAppStart();
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 /**
       
    43 Wait for apparc server to complete initial population of its app list. If list 
       
    44 population doesn't complete within @c KAppArcAppListInitialCompletionTimeout, 
       
    45 this function will leave with KErrTimedOut.
       
    46 
       
    47 @panic If Apsexe.exe isn't started.
       
    48 @leave KErrTimedOut if apparc doesn't complete the initial list population within KAppArcAppListInitialCompletionTimeout.
       
    49 @leave With system-wide error-codes for generic errors.
       
    50 */
       
    51 void CApaAppStart::WaitForApparcToInitialiseL()
       
    52 	{
       
    53 	// Make sure we have a usable session...
       
    54 	if (iApaLsSession.Handle()==KNullHandle)
       
    55 		{
       
    56 		const TInt err = iApaLsSession.Connect(); 
       
    57 		if(err != KErrNone)
       
    58 			{
       
    59 			Panic(EApsexeNotRunning);
       
    60 			}
       
    61 		}
       
    62 	
       
    63 	//...and a timer
       
    64 	RTimer timer;
       
    65 	User::LeaveIfError(timer.CreateLocal());
       
    66 	CleanupClosePushL(timer); 
       
    67 	
       
    68 	// Request apparc to notify us when the initial list population is complete
       
    69 	TRequestStatus apparcStatus;
       
    70 	iApaLsSession.RegisterListPopulationCompleteObserver(apparcStatus);
       
    71 
       
    72 	// Request a timeout.
       
    73 	TRequestStatus timerStatus;
       
    74 	timer.After(timerStatus, TTimeIntervalMicroSeconds32(KAppArcAppListInitialCompletionTimeout));
       
    75 	
       
    76 	// As soon as either request completes, cancel the other
       
    77 	User::WaitForRequest(timerStatus, apparcStatus);
       
    78 	if (timerStatus == KRequestPending)
       
    79 		{
       
    80 		timer.Cancel();
       
    81 		User::WaitForRequest(timerStatus);
       
    82 		}
       
    83 	else
       
    84 		{
       
    85 		// Timeout
       
    86 		User::LeaveIfError(iApaLsSession.CancelListPopulationCompleteObserver());
       
    87 		User::WaitForRequest(apparcStatus);
       
    88 		User::Leave(KErrTimedOut);
       
    89 		}
       
    90 		
       
    91 	CleanupStack::PopAndDestroy(&timer);
       
    92 	}
       
    93 
       
    94 /**
       
    95 Wait for apparc server to complete initial population of its app list. (Asynchronous). 
       
    96 
       
    97 @param aStatus 
       
    98 */
       
    99 void CApaAppStart::InitApparcServer(TRequestStatus& aStatus)
       
   100 	{
       
   101 	// Make sure we have a usable session...
       
   102 	if (iApaLsSession.Handle()==KNullHandle)
       
   103 		{
       
   104 		const TInt err = iApaLsSession.Connect(); 
       
   105 		if(err != KErrNone)
       
   106 			{
       
   107 			TRequestStatus* status = &aStatus;
       
   108 			User::RequestComplete(status, KErrCouldNotConnect);
       
   109 			return;
       
   110 			}
       
   111 		}
       
   112 	iApaLsSession.RegisterListPopulationCompleteObserver(aStatus);
       
   113 	}
       
   114 
       
   115 /**
       
   116 Cancel population of app list.
       
   117 */
       
   118 void CApaAppStart::InitApparcServerCancel()
       
   119 	{
       
   120 	if (iApaLsSession.Handle()!=KNullHandle)
       
   121 		{
       
   122 		// cancel population of app list
       
   123 		iApaLsSession.CancelListPopulationCompleteObserver();
       
   124 		}
       
   125 	}
       
   126 
       
   127 /** 
       
   128 Set up the CApaCommandLine object which will be used to start the app.
       
   129 */
       
   130 void CApaAppStart::SetupCommandLineL(CApaCommandLine& aCmdLine,
       
   131 					const TDesC& aFileName,
       
   132 					const TDesC8& aArgs,
       
   133 					TBool aViewLess,
       
   134 					TBool aStartInBackground)
       
   135 	{
       
   136 	
       
   137 	aCmdLine.SetExecutableNameL(aFileName);
       
   138 	aCmdLine.SetTailEndL(aArgs) ;
       
   139 	
       
   140 	// Define how the app will be launched 		
       
   141 	if (!aStartInBackground && !aViewLess)
       
   142 		{
       
   143 		aCmdLine.SetCommandL(EApaCommandRun);
       
   144 		}
       
   145 	else if (aStartInBackground && !aViewLess)
       
   146 		{
       
   147 		aCmdLine.SetCommandL(EApaCommandBackground);
       
   148 		}
       
   149 	else if (!aStartInBackground && aViewLess)
       
   150 		{
       
   151 		aCmdLine.SetCommandL(EApaCommandRunWithoutViews);
       
   152 		}
       
   153 	else 
       
   154 		{
       
   155 		aCmdLine.SetCommandL(EApaCommandBackgroundAndWithoutViews);
       
   156 		}
       
   157 	}
       
   158 
       
   159 /**
       
   160 Start an application as defined by the specified command line information. Feedback about 
       
   161 when the application is actually up and running is given via @c aRequestStatusForRendezvous.
       
   162 
       
   163 Rule-based launching and non-native applications are supported only after 
       
   164 @c WaitForApparcToInitialiseL (in case of sysstart) or InitApparcServer (in case of ssma) has been called. 
       
   165 @see struct INIT_APPARC.
       
   166 @see struct STRUCT SSM_WAIT_FOR_APPARC_INIT
       
   167 
       
   168 @param aFileName The full filename of the application.
       
   169 @param aArgs The arguments passed to the application.
       
   170 @param aViewLess Indicates whether the application has a view.
       
   171 @param aStartInBackground Indicates whether the application should start in background.
       
   172 @param aThreadId The id of the main thread for the application.
       
   173 @param aRequestStatusForRendezvous To be used for deferred return of the application startup status.
       
   174 
       
   175 @leave With a system-wide error-code e.g. if @c aFileName doesn't exists or if memory couldn't be allcoated.
       
   176 */
       
   177 void CApaAppStart::StartAppL(const TDesC& aFileName, 
       
   178 					const TDesC& aArgs, 
       
   179 					TBool aViewLess, 
       
   180 					TBool aStartInBackground, 
       
   181 					TThreadId& aThreadId, 
       
   182 					TRequestStatus& aRequestStatusForRendezvous)
       
   183 	{
       
   184 	DoStartAppL(aFileName, aArgs, aViewLess, aStartInBackground, aThreadId, &aRequestStatusForRendezvous);
       
   185 	}
       
   186 
       
   187 
       
   188 /**
       
   189 Start an application as defined by the specified command line information. No feedback is provided 
       
   190 about when the application is actually up and running.
       
   191 
       
   192 Rule-based launching and non-native applications are supported only after 
       
   193 @c WaitForApparcToInitialiseL (in case of sysstart) or InitApparcServer (in case of ssma) has been called. 
       
   194 @see struct INIT_APPARC.
       
   195 @see struct STRUCT SSM_WAIT_FOR_APPARC_INIT
       
   196 
       
   197 @param aFileName The full filename of the application.
       
   198 @param aArgs The arguments passed to the application.
       
   199 @param aViewLess Indicates whether the application has a view.
       
   200 @param aStartInBackground Indicates whether the application should start in background.
       
   201 @param aThreadId The id of the main thread for the application.
       
   202 
       
   203 @leave With a system-wide error-code e.g. if @c aFileName doesn't exists or if memory couldn't be allcoated.
       
   204 */
       
   205 void CApaAppStart::StartAppL(const TDesC& aFileName, 
       
   206 					const TDesC& aArgs, 
       
   207 					TBool aViewLess, 
       
   208 					TBool aStartInBackground, 
       
   209 					TThreadId& aThreadId)
       
   210 	{
       
   211 	DoStartAppL(aFileName, aArgs, aViewLess, aStartInBackground, aThreadId, NULL);
       
   212 	}
       
   213 
       
   214 
       
   215 void CApaAppStart::DoStartAppL(const TDesC &aFileName, 
       
   216 					const TDesC& aArgs, 
       
   217 					TBool aViewLess, 
       
   218 					TBool aStartInBackground, 
       
   219 					TThreadId& aThreadId, 
       
   220 					TRequestStatus* aRequestStatusForRendezvous)
       
   221 	{
       
   222 	// Create an 8-bit variant of aArgs
       
   223 	RBuf writableArgs;
       
   224 	writableArgs.CreateL(aArgs);
       
   225 	CleanupClosePushL(writableArgs);
       
   226 	TPtr8 args8 = writableArgs.Collapse();
       
   227 	
       
   228 	CApaCommandLine* const cmdLine = CApaCommandLine::NewLC();
       
   229 	SetupCommandLineL(*cmdLine, aFileName, args8, aViewLess, aStartInBackground) ;
       
   230 
       
   231 	User::LeaveIfError(iApaLsSession.StartApp(*cmdLine, aThreadId, aRequestStatusForRendezvous));
       
   232 	
       
   233 	CleanupStack::PopAndDestroy(cmdLine);
       
   234 	CleanupStack::PopAndDestroy(&writableArgs);
       
   235 	}