serviceproviders/sapi_applicationmanager/appmanagerservice/src/launcher.cpp
changeset 19 989d2f495d90
child 67 5146369cfdc9
equal deleted inserted replaced
14:a36b1e19a461 19:989d2f495d90
       
     1 /*
       
     2 * Copyright (c) 2007-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This Class provides the functionality to launch the application
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <apgcli.h>
       
    22 #include <apacmdln.h>
       
    23 #include <e32std.h>
       
    24 #include <eikappui.h>
       
    25 #include <eikenv.h>
       
    26 #include <e32base.h>
       
    27 #include <f32file.h>
       
    28 //#include <documenthandler.h>
       
    29 
       
    30 #include "launcher.h"
       
    31 #include "launcherobserver.h"
       
    32 #include "appmanagerservice.h"
       
    33 
       
    34 _LIT(KBackground ,"Background");
       
    35 _LIT(KChained,"Chained");
       
    36 _LIT(KScheme,"s60uid://0x");
       
    37 const TInt KDocMaxDigitsInHexString = 8; // 32 bits.
       
    38 
       
    39 const TInt KLen = 2; // "//" for count these
       
    40 
       
    41 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CLauncher::NewLC
       
    45 // Returns the instance of CLauncher class.
       
    46 // -----------------------------------------------------------------------------
       
    47 CLauncher* CLauncher::NewL( RApaLsSession& aSession )
       
    48 	{
       
    49 	CLauncher* self = new ( ELeave )CLauncher( aSession );
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CLauncher::LaunchApplication
       
    56 // This function Launch the Given Application
       
    57 // -----------------------------------------------------------------------------
       
    58 
       
    59  TThreadId  CLauncher::LaunchApplicationL( const TDesC&    aAppId,
       
    60                                             const TDesC8&   aCmdLine,
       
    61                                             const TOptions& aOptions
       
    62                                            )
       
    63 	{
       
    64 
       
    65 	TUid appUid;
       
    66     TApaAppInfo appInfo;
       
    67 
       
    68     //parse the application string
       
    69     ParseAppIdL( aAppId, appUid );
       
    70 
       
    71     
       
    72     if(!( (0==aOptions.iMode.CompareF( KChained ) || 0 == aOptions.iMode.CompareF( KStandalone )  ) && (0==aOptions.iPostion.CompareF( KBackground ) || 0 == aOptions.iPostion.CompareF( KForeground ) ) ) )
       
    73         {
       
    74         User::Leave( KErrArgument );
       
    75         }
       
    76     
       
    77     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
    78 
       
    79     //Setting Executable name
       
    80 
       
    81     if( KErrNone == iApaLsSession.GetAppInfo( appInfo, appUid) )
       
    82         {
       
    83         cmdLine->SetExecutableNameL( appInfo.iFullName );
       
    84         }
       
    85     else
       
    86         {
       
    87         //Invalid UID
       
    88         User::Leave( KErrArgument );
       
    89         }
       
    90 
       
    91 
       
    92     //Setting command Line Argument
       
    93 
       
    94     if ( aCmdLine.Length() )
       
    95         {
       
    96         cmdLine->SetTailEndL( aCmdLine );
       
    97         }
       
    98 
       
    99 
       
   100     //Setting Document Name
       
   101 
       
   102     if ( aOptions.iDocument.Length() )
       
   103         {
       
   104         CAppManagerService::ValidateFilePathL(aOptions.iDocument);
       
   105         
       
   106         cmdLine->SetDocumentNameL( aOptions.iDocument );
       
   107         }
       
   108 
       
   109     //Set the Launchine Mode
       
   110     if( 0 == aOptions.iMode.CompareF( KChained ) )
       
   111        {
       
   112 
       
   113        //Chianed mode doesn;t make any sense with background 
       
   114        if( 0 != aOptions.iPostion.CompareF( KBackground ) )
       
   115 	        cmdLine->SetCommandL( EApaCommandRun );
       
   116        else
       
   117             User::Leave( KErrNotSupported );
       
   118 
       
   119        SetChainedModeL( cmdLine, appUid );
       
   120 
       
   121        }
       
   122 
       
   123     else
       
   124         {
       
   125          //Setting the Launching postion means either background or foreground
       
   126         if( 0 == aOptions.iPostion.CompareF( KBackground ) )
       
   127             {
       
   128             cmdLine->SetCommandL( EApaCommandBackground );
       
   129             }
       
   130         else
       
   131             {
       
   132             cmdLine->SetCommandL( EApaCommandRun );
       
   133             }
       
   134 
       
   135         }
       
   136 
       
   137     // Launch the requested Application
       
   138     TThreadId threadNotUsed;
       
   139     TRequestStatus requestStatusForRendezvous;
       
   140     User::LeaveIfError( iApaLsSession.StartApp( *cmdLine,
       
   141 								               threadNotUsed,
       
   142 								               &requestStatusForRendezvous ));
       
   143 
       
   144 
       
   145 
       
   146 	User::WaitForRequest( requestStatusForRendezvous );
       
   147 	User::LeaveIfError( requestStatusForRendezvous.Int());
       
   148 	CleanupStack::PopAndDestroy( cmdLine  );
       
   149 
       
   150 
       
   151     return threadNotUsed;
       
   152     }
       
   153 
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CLauncher::LaunchDocumentL
       
   157 // This function Launch the Given Document
       
   158 // -----------------------------------------------------------------------------
       
   159 
       
   160  TThreadId CLauncher::LaunchDocumentL(TDocument& aCriteria,
       
   161        	                               const TDesC8& aMimeType,
       
   162        	                               const TOptions& aOptions ,
       
   163        	                               TDesC& aFileName
       
   164        	                               )
       
   165     {
       
   166 
       
   167     TUid appUid;
       
   168     TApaAppInfo appInfo;
       
   169     TDataType dataType( aMimeType );
       
   170     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   171     TFileName temp;
       
   172 
       
   173      //Extracting Application Uid
       
   174      if( 0 != aCriteria.iHandle.SubSessionHandle())
       
   175          {
       
   176          iApaLsSession.AppForDocument( aCriteria.iHandle,appUid,dataType);
       
   177          cmdLine->SetCommandL( EApaCommandOpen );
       
   178          cmdLine->SetFileByHandleL( aCriteria.iHandle );
       
   179          }
       
   180     else if (0 != aCriteria.iPath.CompareF(KNullDesC) )
       
   181          {
       
   182          CAppManagerService::ValidateFilePathL(aCriteria.iPath);
       
   183          
       
   184          iApaLsSession.AppForDocument( aCriteria.iPath,appUid,dataType);
       
   185          cmdLine->SetCommandL( EApaCommandOpen );
       
   186          cmdLine->SetDocumentNameL(aCriteria.iPath );	
       
   187          }
       
   188     else if ( 0 != aMimeType.CompareF( KNullDesC8 ) )
       
   189          {
       
   190          iApaLsSession.AppForDataType(dataType,appUid);
       
   191          cmdLine->SetCommandL( EApaCommandCreate );
       
   192 
       
   193             //This is the logic for handling the buffer right now we are not supported as 
       
   194             //underlying S60 classes showing memory leak and pecular behaviour with different 
       
   195             //buffer
       
   196             
       
   197          	/*if( 0 != aOptions.iBuffer.CompareF( KNullDesC8 ) )
       
   198              {
       
   199                 __UHEAP_MARK;
       
   200                 CDocumentHandler *temp = CDocumentHandler::NewLC();
       
   201                 TRAPD (err, temp->SaveL(aOptions.iBuffer,dataType,KEntryAttNormal) );
       
   202                 temp->GetPath(aFileName);
       
   203                 __UHEAP_MARKEND;
       
   204                 CleanupStack::PopAndDestroy( );
       
   205              }*/
       
   206         
       
   207          //left to the launch application for setting
       
   208          cmdLine->SetDocumentNameL( aFileName );
       
   209 
       
   210 
       
   211          }
       
   212     else
       
   213         {
       
   214         User::Leave(KErrArgument);
       
   215         }
       
   216 
       
   217 
       
   218     //extraction application path
       
   219     if( KErrNone == iApaLsSession.GetAppInfo( appInfo, appUid) )
       
   220         {
       
   221         cmdLine->SetExecutableNameL( appInfo.iFullName );
       
   222         }
       
   223     else
       
   224         {
       
   225         User::Leave( KErrNotFound );
       
   226         }
       
   227 
       
   228 
       
   229 
       
   230 
       
   231     //Set the Launchine Mode
       
   232     if( 0 == aOptions.iMode.CompareF( KChained ) )
       
   233        {
       
   234         SetChainedModeL( cmdLine  ,appUid );
       
   235        }
       
   236     else if ( 0 != aOptions.iMode.CompareF( KStandalone ) ) //The only other mode is "Standalone"
       
   237         {   
       
   238             User::Leave(KErrArgument);
       
   239         }
       
   240 
       
   241 
       
   242     // Launch the requested Application
       
   243     TThreadId threadNotUsed;
       
   244     TRequestStatus requestStatusForRendezvous;
       
   245     User::LeaveIfError( iApaLsSession.StartApp( *cmdLine,
       
   246 								               threadNotUsed,
       
   247 								               &requestStatusForRendezvous ));
       
   248 
       
   249 
       
   250 
       
   251 	User::WaitForRequest( requestStatusForRendezvous );
       
   252 	User::LeaveIfError( requestStatusForRendezvous.Int());
       
   253 	CleanupStack::PopAndDestroy( cmdLine  );
       
   254     
       
   255     
       
   256     
       
   257     return threadNotUsed;
       
   258 
       
   259 
       
   260     }
       
   261 
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CLauncher::LaunchDocumentL
       
   265 // This function will parse the application ID string and return the UID
       
   266 // -----------------------------------------------------------------------------
       
   267 
       
   268 void CLauncher::ParseAppIdL( const TDesC & aSource ,TUid & aUid )
       
   269 {
       
   270 
       
   271 	// Icoming string : s60uid://0x101f4d90 , output : 0x101f4d90
       
   272 	
       
   273 	if( (aSource.Length() > KScheme.iTypeLength )  &&(0 == aSource.Mid(0,KScheme.iTypeLength).CompareF(KScheme) ) )
       
   274 		{
       
   275 		TChar a('/');
       
   276 		TInt offset = aSource.Locate(a);
       
   277 		
       
   278 		TPtrC uiddata;
       
   279 
       
   280 		// To chk for pattern ://
       
   281 		if((0 != aSource.Mid(offset+1,1).Compare(_L("/"))) && (0 != aSource.Mid(offset-1,1).Compare(_L(":"))) )
       
   282 	    	User::LeaveIfError(KErrArgument);
       
   283 
       
   284 	    TPtrC a1(aSource.Mid(offset+1,1));
       
   285 	    TPtrC a2(aSource.Mid(offset-1,1));
       
   286 
       
   287 		TInt sourcelen = aSource.Length();
       
   288 		TInt off = offset + KLen;
       
   289 		TInt len = sourcelen -(off) ;
       
   290 
       
   291 		uiddata.Set(aSource.Mid(off,len));
       
   292 		TInt32 uidinint ;
       
   293 	    User::LeaveIfError( ConvertHexStringToInt32(uiddata,uidinint) );
       
   294 
       
   295 	    aUid.iUid = uidinint;
       
   296 		}
       
   297 	else
       
   298 		{
       
   299 		TUid empt(TUid::Null());
       
   300 		aUid.iUid = empt.iUid;
       
   301 		}
       
   302 
       
   303 }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CLauncher::ConvertHexStringToInt32
       
   307 // This function will convert the Hax string into TInt32
       
   308 // -----------------------------------------------------------------------------
       
   309 
       
   310 
       
   311 
       
   312 TInt CLauncher::ConvertHexStringToInt32(const TDesC& aHexString,TInt32& aInt32 )
       
   313     {
       
   314     aInt32 = 0;
       
   315 
       
   316     TInt pos = aHexString.LocateF( 'x' );
       
   317     if ( pos == KErrNotFound )
       
   318         {
       
   319         pos = 0;
       
   320         }
       
   321     else
       
   322         {
       
   323         pos++;
       
   324         }
       
   325 
       
   326     if( ( aHexString.Length() - pos ) > KDocMaxDigitsInHexString )
       
   327         {
       
   328         return KErrArgument; // Error: value too big.
       
   329         }
       
   330     TPtrC hexStringPtr( aHexString.Mid( pos ) );
       
   331 
       
   332     TInt64 value64;
       
   333     if ( TLex( hexStringPtr ).Val( value64, EHex ) != KErrNone )
       
   334         {
       
   335         return KErrArgument; // Error: conversion failed.
       
   336         }
       
   337     aInt32 = value64;
       
   338 
       
   339     return KErrNone;
       
   340     }
       
   341 
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CLauncher::SetChainedModeL
       
   345 // This function will set the application to launch in chained mode
       
   346 // -----------------------------------------------------------------------------
       
   347 
       
   348 
       
   349 void CLauncher::SetChainedModeL( CApaCommandLine* aCmdLine  , const TUid& aAppUid )
       
   350     {
       
   351 
       
   352        CEikonEnv* eikEnv = CEikonEnv::Static();
       
   353 
       
   354        if( !eikEnv )
       
   355            {
       
   356 	        //Console Based Application can not launch another application in
       
   357 	        //chained mode
       
   358 	        User::Leave( KErrNotSupported );
       
   359            }
       
   360        else
       
   361            {
       
   362             RWindowGroup& wg = eikEnv->RootWin();
       
   363             CEikAppUi* appUi = eikEnv->EikAppUi();
       
   364 
       
   365 
       
   366 	        // Window chain the client with the current application
       
   367 	        const TInt parentWindowGroupID = wg.Identifier();
       
   368 	        if ( parentWindowGroupID )
       
   369 		        {
       
   370 		        aCmdLine->SetParentWindowGroupID( parentWindowGroupID );
       
   371 		        wg.AllowProcessToCreateChildWindowGroups( aAppUid );
       
   372 		        }
       
   373 		    
       
   374 		        if ( appUi )
       
   375 		        {
       
   376      		     appUi->DeactivateActiveViewL();
       
   377                 }
       
   378            }
       
   379     }
       
   380 
       
   381 
       
   382 
       
   383 
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CLauncher::CLauncher
       
   387 // Constructor
       
   388 // -----------------------------------------------------------------------------
       
   389 CLauncher::CLauncher( RApaLsSession& aSession ): iApaLsSession( aSession )
       
   390 
       
   391 	{
       
   392 
       
   393 	}
       
   394 
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CLauncher::~CLauncher
       
   398 // Destructor
       
   399 // -----------------------------------------------------------------------------
       
   400 CLauncher::~CLauncher( )
       
   401 
       
   402 	{
       
   403 
       
   404 	}
       
   405 
       
   406 
       
   407