sysstatemgmt/systemstarter/amastartsrc/amastartasync.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "amastartasync.h"
       
    22 
       
    23 #include "ssmdebug.h"
       
    24 
       
    25 CAmaStartAsync* CAmaStartAsync::NewL( RMessage2 aMessage )
       
    26 	{
       
    27 	DEBUGPRINT1A( ">CAmaStartAsync::NewL" );
       
    28 	
       
    29 	CAmaStartAsync* self = NewLC( aMessage );
       
    30 	CleanupStack::Pop();
       
    31 	
       
    32 	DEBUGPRINT1A( "CAmaStartAsync::NewL>" );
       
    33 	return self;
       
    34 	}
       
    35 	
       
    36 	
       
    37 	
       
    38 CAmaStartAsync* CAmaStartAsync::NewLC( RMessage2 aMessage )
       
    39 	{
       
    40 	DEBUGPRINT1A( ">CAmaStartAsync::NewLC" );
       
    41 	
       
    42 	CAmaStartAsync* self = new(ELeave) CAmaStartAsync( aMessage );
       
    43 	CleanupStack::PushL( self );
       
    44 	self->ConstructL();
       
    45 	
       
    46 	DEBUGPRINT1A( ">CAmaStartAsync::NewLC" );
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 
       
    51 
       
    52 CAmaStartAsync::~CAmaStartAsync()
       
    53 	{
       
    54 	DEBUGPRINT1A( ">CAmaStartAsync::~CAmaStartAsync" );
       
    55 	
       
    56 	Cancel();
       
    57 
       
    58 	delete iStartSafe;
       
    59 	if (iDscStore.IsOpened())
       
    60 		{
       
    61 		iDscStore.Close();
       
    62 		}
       
    63 	iSysMon.Close();
       
    64 	
       
    65 	DEBUGPRINT1A( "CAmaStartAsync::~CAmaStartAsync>" );
       
    66 	}
       
    67 
       
    68 
       
    69 
       
    70 /**
       
    71  * Initiate AMA processing.
       
    72  */	
       
    73 void CAmaStartAsync::StartL( const TUid& aDscId )
       
    74 	{
       
    75 	DEBUGPRINT1A( ">CAmaStartAsync::StartL" );
       
    76 	
       
    77 	if( iRunning )
       
    78 		{
       
    79 		User::Leave( KErrInUse );
       
    80 		}
       
    81 
       
    82 	DEBUGPRINT1( _L("***** Set running") );
       
    83 	
       
    84 	iRunning = ETrue;
       
    85 	iDscStore.EnumOpenLC(aDscId);
       
    86 	CleanupStack::Pop();
       
    87 		
       
    88 	SetActive();
       
    89 	TRequestStatus* trs = &iStatus;
       
    90 	User::RequestComplete( trs, KErrNone );
       
    91 	
       
    92 	DEBUGPRINT1A( "CAmaStartAsync::StartL>" );
       
    93 	}
       
    94 
       
    95 
       
    96 
       
    97 /**
       
    98  * Succsessive calls to StartDscItemL, but only one per RunL in order for
       
    99  * the scheduler/ server to remain responsive.
       
   100  */
       
   101 void CAmaStartAsync::RunL()
       
   102 	{
       
   103 	DEBUGPRINT1A( ">CAmaStartAsync::RunL %d" );
       
   104 	CDscItem* item = iDscStore.EnumReadNextL();
       
   105 	if (item)
       
   106 		{
       
   107 		//if loading of one AMA from the DSC fails we should move to next AMA - so we trap here 
       
   108 		TRAP_IGNORE(StartDscItemL(*item));
       
   109 		
       
   110 		delete item;
       
   111 		
       
   112 		SetActive();
       
   113 		TRequestStatus* status = &iStatus;
       
   114 		User::RequestComplete(status, KErrNone);
       
   115 		
       
   116 		}
       
   117 	else // No more items in store, so finish here
       
   118 		{
       
   119 		iDscStore.EnumClose();
       
   120 		iDscStore.Close();
       
   121 		iMessage.Complete(KErrNone);
       
   122 		}
       
   123 
       
   124 	DEBUGPRINT1A( "CAmaStartAsync::RunL>" );
       
   125 	}
       
   126 	
       
   127 	
       
   128 
       
   129 TInt CAmaStartAsync::RunError( TInt aError )
       
   130 	{
       
   131 	DEBUGPRINT1A( ">CAmaStartAsync::RunError" );
       
   132 	
       
   133 	iDscStore.EnumClose();
       
   134 	iDscStore.Close();
       
   135 	iMessage.Complete(aError);
       
   136 	
       
   137 	DEBUGPRINT1A( "CAmaStartAsync::RunError>" );
       
   138 	return KErrNone;
       
   139 	}
       
   140 
       
   141 
       
   142 
       
   143 /**
       
   144  * Cancel the current command.
       
   145  */
       
   146 void CAmaStartAsync::DoCancel()
       
   147 	{
       
   148 	DEBUGPRINT1A( ">CAmaStartAsync::DoCancel" );
       
   149 
       
   150 	if( iRunning )
       
   151 		{
       
   152 		iDscStore.EnumClose();
       
   153 		iMessage.Complete(KErrCancel);
       
   154 		iRunning = EFalse;
       
   155 		}
       
   156 	
       
   157 	DEBUGPRINT1A( "CAmaStartAsync::DoCancel>" );
       
   158 	}
       
   159 	
       
   160 
       
   161 
       
   162 void CAmaStartAsync::ConstructL()
       
   163 	{
       
   164 	DEBUGPRINT1A( ">CAmaStartAsync::ConstructL" );
       
   165 	
       
   166 	iStartSafe = CStartSafe::NewL();
       
   167 	iDscStore.OpenL();
       
   168 	CActiveScheduler::Add( this );
       
   169 	
       
   170 	DEBUGPRINT1A( "CAmaStartAsync::ConstructL>" );
       
   171 	}
       
   172 	
       
   173 	
       
   174 	
       
   175 CAmaStartAsync::CAmaStartAsync( RMessage2 aMessage )
       
   176 : CActive( EPriorityStandard ), iMessage( aMessage )
       
   177 	{
       
   178 	DEBUGPRINT1A( ">CAmaStartAsync::CAmaStartAsync> (Empty constructor)" );
       
   179 	}
       
   180 	
       
   181 	
       
   182 
       
   183 void CAmaStartAsync::StartDscItemL(const CDscItem& aDscItem)
       
   184 	{
       
   185 	DEBUGPRINT1A( ">CAmaStartAsync::StartDscItemL" );
       
   186 	
       
   187 	//A process to be created inside iStartSafe->StartL();
       
   188 	RProcess process;
       
   189 
       
   190 	//Number of retries made for starting the process.
       
   191 	TInt tried=0;
       
   192 	
       
   193 	// start the process
       
   194 	const CStartupProperties& properties = aDscItem.StartupProperties();
       
   195 	iStartSafe->StartL(properties, process, tried);
       
   196 	
       
   197 	DEBUGPRINT2(_L("%d times has been retried to start the process successfully"), tried);
       
   198 
       
   199 	TInt error=KErrNone;
       
   200 	//monitor the process if indicated
       
   201 	if(aDscItem.Monitored())
       
   202 		{
       
   203 		// first time monitoring, so connect with the SysMon server
       
   204 		if (iSysMon.Handle() == KNullHandle)
       
   205 			{
       
   206 			TRAP(error, iSysMon.OpenL());
       
   207 			}
       
   208 		// monitor the process	
       
   209 		if (KErrNone==error) 
       
   210 			{
       
   211 			TRAP(error, iSysMon.MonitorL(properties, process));
       
   212 			}
       
   213 		//Connect to SysMon fail or register to monitor the process fail
       
   214 		//Kill the started process because Start and Monitor should be one atomic function.
       
   215 		if (KErrNone !=error)
       
   216 			{
       
   217 			process.Kill(error);
       
   218 			process.Close();
       
   219 			}
       
   220 		User::LeaveIfError(error);	
       
   221 		}
       
   222 		
       
   223 	DEBUGPRINT1A( "CAmaStartAsync::StartDscItemL>" );
       
   224 	}