sysstatemgmt/systemstarter/amastartsrc/amastart.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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 <e32base.h>
       
    17 #include <dscitem.h>
       
    18 #include "startsafe.h"
       
    19 
       
    20 #include "amastart.h"
       
    21 
       
    22 /**
       
    23  * Used to create an instance of CAmaStart class
       
    24  * @return An instance of CAmaStart
       
    25  */
       
    26 EXPORT_C CAmaStart* CAmaStart::NewL()
       
    27 	{
       
    28 	RDebug::Print(_L(">CAmaStart::NewL"));
       
    29 	
       
    30 	CAmaStart* ama = new(ELeave) CAmaStart();
       
    31 	CleanupStack::PushL(ama);
       
    32 	ama->ConstructL();
       
    33 	CleanupStack::Pop(ama);
       
    34 	
       
    35 	RDebug::Print(_L("CAmaStart::NewL>"));
       
    36 	return ama;
       
    37 	}
       
    38 
       
    39 
       
    40 
       
    41 /**
       
    42  * Used to launch AMAs (after market application) from a DSC. 
       
    43  * Returns when processing of the DSC is complete. 
       
    44  * (Synchronous)
       
    45  * @param aDscId Id of the DSC containing AMAs to be started.
       
    46  */
       
    47 EXPORT_C void CAmaStart::StartL(const TUid aDscId)
       
    48 	{
       
    49 	RDebug::Print(_L(">CAmaStart::StartL"));
       
    50 	
       
    51 	iDscStore.EnumOpenLC(aDscId) ; // leaves if opening fails
       
    52 
       
    53 	CDscItem* item = iDscStore.EnumReadNextL();
       
    54 	while (item)
       
    55 		{
       
    56 		//if loading of one AMA from the DSC fails we should move to next AMA - so we trap here 
       
    57 		TRAP_IGNORE(StartDscItemL(*item));
       
    58 		
       
    59 		delete item;
       
    60 		item = iDscStore.EnumReadNextL();
       
    61 		}
       
    62 
       
    63 	//EnumClose()
       
    64 	//Pop and destroy cleanup rollback operation left on cleanstack by EnumOpenLC()
       
    65 	CleanupStack::PopAndDestroy();
       
    66 	
       
    67 	RDebug::Print(_L("CAmaStart::StartL>"));
       
    68 	}
       
    69 
       
    70 /**
       
    71  * Destructor for CAmaStart class
       
    72  */
       
    73 EXPORT_C CAmaStart::~CAmaStart()
       
    74 	{
       
    75 	RDebug::Print(_L( ">CAmaStart::~CAmaStart"));
       
    76 	delete iStartSafe;
       
    77 	if (&(this)->iDscStore)
       
    78 		{
       
    79 		iDscStore.Close();
       
    80 		}
       
    81 	if (&(this)->iSysMon)
       
    82 		{
       
    83 		iSysMon.Close();
       
    84 		}
       
    85 	RDebug::Print(_L( "CAmaStart::~CAmaStart>"));
       
    86 	}
       
    87 
       
    88 
       
    89 
       
    90 void CAmaStart::StartDscItemL(const CDscItem& aDscItem)
       
    91 	{
       
    92 	RDebug::Print(_L( ">CAmaStart::StartDscItemL"));
       
    93 	//A process to be created inside iStartSafe->StartL();
       
    94 	RProcess process;
       
    95 	CleanupClosePushL(process);
       
    96 
       
    97 	//Number of retries made for starting the process.
       
    98 	TInt tried=0;
       
    99 	
       
   100 	// start the process
       
   101 	const CStartupProperties& properties = aDscItem.StartupProperties();
       
   102 	iStartSafe->StartL(properties, process, tried);
       
   103 		
       
   104 #ifdef _DEBUG
       
   105 	_LIT(KNumRetried, "%d times has been retried to start the process successfully");
       
   106 	RDebug::Print(KNumRetried, tried);
       
   107 #endif
       
   108 
       
   109 	TInt error=KErrNone;
       
   110 	//monitor the process if indicated
       
   111 	if(aDscItem.Monitored())
       
   112 		{
       
   113 		// first time monitoring, so connect with the SysMon server
       
   114 		if (iSysMon.Handle() == KNullHandle)
       
   115 			{
       
   116 			TRAP(error, iSysMon.OpenL());
       
   117 			}
       
   118 		// monitor the process	
       
   119 		if (KErrNone==error) 
       
   120 			{
       
   121 			TRAP(error, iSysMon.MonitorL(properties, process));
       
   122 			}
       
   123 		//Connect to SysMon fail or register to monitor the process fail
       
   124 		//Kill the started process because Start and Monitor should be one atomic function.
       
   125 		if (KErrNone !=error)
       
   126 			{
       
   127 			process.Kill(error);
       
   128 			process.Close();
       
   129 			}
       
   130 		User::LeaveIfError(error);	
       
   131 		}
       
   132 	CleanupStack::PopAndDestroy(&process);
       
   133 	RDebug::Print(_L( "CAmaStart::StartDscItemL>"));
       
   134 	}
       
   135 
       
   136 
       
   137 	
       
   138 CAmaStart::CAmaStart()
       
   139 	{
       
   140 	RDebug::Print(_L(">CAmaStart::CAmaStart> (Empty constructor)"));
       
   141 	}
       
   142 
       
   143 
       
   144 
       
   145 void CAmaStart::ConstructL()
       
   146 	{
       
   147 	RDebug::Print(_L(">CAmaStart::ConstructL"));
       
   148 	iStartSafe = CStartSafe::NewL();
       
   149 	iDscStore.OpenL();
       
   150 	RDebug::Print(_L("CAmaStart::ConstructL>"));
       
   151 	}