applaunchservices/aftermarketappstarter/amastartsrc/amastart.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2008-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 "ssmdebug.h"
       
    17 #include "amastart.h"
       
    18 #include <dscitem.h>
       
    19 #include <startsafe.h>
       
    20 
       
    21 /**
       
    22  * Used to create an instance of CAmaStart class
       
    23  * @return An instance of CAmaStart
       
    24  */
       
    25 EXPORT_C CAmaStart* CAmaStart::NewL()
       
    26 	{
       
    27 	CAmaStart* ama = new(ELeave) CAmaStart();
       
    28 	CleanupStack::PushL(ama);
       
    29 	ama->ConstructL();
       
    30 	CleanupStack::Pop(ama);
       
    31 	return ama;
       
    32 	}
       
    33 
       
    34 void CAmaStart::ConstructL()
       
    35 	{
       
    36 	iAmaStarter = CAmaStartAsync::NewL();
       
    37 	iStartSafe = CStartSafe::NewL();
       
    38 	}
       
    39 
       
    40 CAmaStart::CAmaStart()
       
    41 	{
       
    42 	}
       
    43 
       
    44 /**
       
    45  * Destructor for CAmaStart class
       
    46  */
       
    47 EXPORT_C CAmaStart::~CAmaStart()
       
    48 	{
       
    49 	delete iStartSafe;
       
    50 	iDscStore.Close();
       
    51 	iSysMon.Close();
       
    52 	delete iAmaStarter;
       
    53 	}
       
    54 
       
    55 /**
       
    56  * Used to launch AMAs (after market application) from a DSC. 
       
    57  * Returns when processing of the DSC is complete. 
       
    58  * (Synchronous)
       
    59  * @param aDscId Id of the DSC containing AMAs to be started.
       
    60  */
       
    61 EXPORT_C void CAmaStart::StartL(const TUid aDscId)
       
    62 	{
       
    63 	if (iClientStatus && *iClientStatus == KRequestPending)
       
    64 		{
       
    65 		User::Leave(KErrNotReady);
       
    66 		}
       
    67 
       
    68 	// Opens a connection with the DSC database.
       
    69 	iDscStore.OpenL();
       
    70 
       
    71 	//Prepares the database to read the given details of the given dsc id
       
    72 	iDscStore.EnumOpenLC(aDscId) ;
       
    73 
       
    74 	//Read the details of the current dscid (aDscId) 
       
    75 	CDscItem* item = iDscStore.EnumReadNextL();
       
    76 	while (item)
       
    77 		{
       
    78 		//if loading of one AMA from the DSC fails we should move to next AMA - so we trap here 
       
    79 		TRAP_IGNORE(StartDscItemL(*item));
       
    80 		delete item;
       
    81 		item = iDscStore.EnumReadNextL();
       
    82 		}
       
    83 
       
    84 	//EnumClose()
       
    85 	//Pop and destroy cleanup rollback operation left on cleanstack by EnumOpenLC()
       
    86 	CleanupStack::PopAndDestroy();
       
    87 	}
       
    88 
       
    89 /**
       
    90  * Used to begin launching AMAs (after market application) from a DSC.
       
    91  * Returns immediately. The supplied TRequestStatus object is used to signal completion. 
       
    92  * When the request is completed it will have one of the following values: 
       
    93  * KErrNone - if the request complete successfully
       
    94  * KErrCancel - if the request was cancelled using CAmaStart::CancelStart
       
    95  * KErrNotReady - if another asynchronous request is outstanding
       
    96  * KErrArgument - if a DSC with the supplied UID does not exist
       
    97  * or one of the other system-wide error codes.
       
    98  * (Asynchronous)
       
    99  * @param aDscId Id of the DSC containing AMAs to be started.
       
   100  * @param aRequestStatus Status object to signal completion.
       
   101  */
       
   102 EXPORT_C void CAmaStart::Start(const TUid aDscId, TRequestStatus& aRequestStatus)
       
   103 	{
       
   104 	aRequestStatus = KRequestPending;
       
   105 	TRequestStatus* trs = &aRequestStatus;
       
   106 	if (iClientStatus && *iClientStatus == KRequestPending)
       
   107 		{
       
   108 		User::RequestComplete(trs, KErrNotReady);
       
   109 		return;
       
   110 		}
       
   111 	
       
   112 	iClientStatus = &aRequestStatus;
       
   113 	TRAPD(err, iAmaStarter->StartL(aDscId, aRequestStatus));
       
   114 	if (err != KErrNone)
       
   115 		{
       
   116 		//Complete the request with the error code returned
       
   117 		User::RequestComplete(trs, err);
       
   118 		return;
       
   119 		}
       
   120 	}
       
   121 
       
   122 /**
       
   123  * Cancel's a pending asynchronous StarL request.
       
   124  * If there is no request pending, calling thisw method has no effect.
       
   125  */
       
   126 EXPORT_C void CAmaStart::CancelStart()
       
   127 	{
       
   128 	iAmaStarter->Cancel();
       
   129 	}
       
   130 
       
   131 void CAmaStart::StartDscItemL(const CDscItem& aDscItem)
       
   132 	{
       
   133 	//A process to be created inside iStartSafe->StartL();
       
   134 	RProcess process;
       
   135 	CleanupClosePushL(process);
       
   136 
       
   137 	//Number of retries made for starting the process.
       
   138 	TInt tried = 0;
       
   139 
       
   140 	// start the process
       
   141 	const CStartupProperties& properties = aDscItem.StartupProperties();
       
   142 	iStartSafe->StartL(properties, process, tried);
       
   143 	DEBUGPRINT2A("CAmaStart::StartDscItemL(): %d times has been retried to start the process successfully", tried);
       
   144 
       
   145 	//monitor the process if indicated
       
   146 	if (aDscItem.Monitored())
       
   147 		{
       
   148 		TRAPD(error, MonitorProcessL(properties, process));
       
   149 
       
   150 		//Kill the started process in case of an error as Start and Monitor should be one atomic function.
       
   151 		if (KErrNone != error)
       
   152 			{
       
   153 			process.Kill(error);
       
   154 			}
       
   155 		}
       
   156 	CleanupStack::PopAndDestroy(&process);
       
   157  	}
       
   158 
       
   159 void CAmaStart::MonitorProcessL(const CStartupProperties& aProperties, const RProcess& aProcess)
       
   160 	{
       
   161 	// first time monitoring, so connect with the SysMon server
       
   162 	if (KNullHandle == iSysMon.Handle())
       
   163 		{
       
   164 		iSysMon.OpenL();
       
   165 		}
       
   166 
       
   167 	// monitor the process	
       
   168 	iSysMon.MonitorL(aProperties, aProcess);
       
   169 	}