applaunchservices/aftermarketappstarter/amastartsrc/amastartasync.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 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include "amastartasync.h"
       
    22 #include "ssmdebug.h"
       
    23 
       
    24 CAmaStartAsync* CAmaStartAsync::NewL()
       
    25 	{
       
    26 	CAmaStartAsync* self = NewLC();
       
    27 	CleanupStack::Pop();	
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CAmaStartAsync* CAmaStartAsync::NewLC()
       
    32 	{
       
    33 	CAmaStartAsync* self = new(ELeave) CAmaStartAsync();
       
    34 	CleanupStack::PushL( self );
       
    35 	self->ConstructL();
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 void CAmaStartAsync::ConstructL()
       
    40 	{
       
    41 	iSsmStartSafe = CSsmStartSafe::NewL();
       
    42 	CActiveScheduler::Add(this);
       
    43 	}
       
    44 
       
    45 CAmaStartAsync::CAmaStartAsync()
       
    46 : CActive( EPriorityStandard )
       
    47 	{
       
    48 	}
       
    49 
       
    50 CAmaStartAsync::~CAmaStartAsync()
       
    51 	{
       
    52 	Cancel();
       
    53 	delete iCurrentDscItem;
       
    54 	delete iSsmStartSafe;
       
    55 	iDscStore.Close();
       
    56 	iSysMon.Close();
       
    57 	}
       
    58 
       
    59 /**
       
    60  * Initiate AMA processing.
       
    61  */	
       
    62 void CAmaStartAsync::StartL(const TUid& aDscId, TRequestStatus& aStatus)
       
    63 	{
       
    64 	if (iRunning)
       
    65 		{
       
    66 		DEBUGPRINT1( _L("CAmaStartAsync is already running..."));
       
    67 		User::Leave(KErrInUse);
       
    68 		}
       
    69 
       
    70 	iDscStore.OpenL();
       
    71 	TBool result = iDscStore.DscExistsL(aDscId);
       
    72 	if (!result)
       
    73 		{
       
    74 		TRequestStatus* status = &aStatus;
       
    75 		User::RequestComplete(status, KErrArgument);
       
    76 		DEBUGPRINT1A( "RAmaStartSession::StartDsc> (DSC does not exist)") ;
       
    77 		return;
       
    78 		}
       
    79 
       
    80 	iAmaStatus = &aStatus;
       
    81 	iRunning = ETrue;
       
    82 	iDscStore.EnumOpenLC(aDscId);
       
    83 	CleanupStack::Pop();
       
    84 
       
    85 	DoStartL();
       
    86 	}
       
    87 
       
    88 void CAmaStartAsync::DoStartL()
       
    89 	{
       
    90 	iCurrentDscItem = iDscStore.EnumReadNextL();
       
    91 	if (iCurrentDscItem)
       
    92 		{
       
    93 		//if loading of one AMA from the DSC fails we should move to next AMA 
       
    94 		StartDscItem(*iCurrentDscItem);
       
    95 		}
       
    96 	else // No more items in store, so finish here
       
    97 		{
       
    98 		iDscStore.EnumClose();
       
    99 		iDscStore.Close();
       
   100 		User::RequestComplete(iAmaStatus, KErrNone);
       
   101 		iAmaStatus = NULL;
       
   102 		delete iCurrentDscItem;
       
   103 		iCurrentDscItem = NULL;
       
   104 		iRunning = EFalse;
       
   105 		}
       
   106 	}
       
   107 
       
   108 TInt CAmaStartAsync::RunError( TInt aError )
       
   109 	{
       
   110 	iDscStore.EnumClose();
       
   111 	iDscStore.Close();
       
   112 	User::RequestComplete(iAmaStatus, aError);
       
   113 	iRunning = EFalse;
       
   114 	return KErrNone;
       
   115 	}
       
   116 
       
   117 /**
       
   118  * Cancel the current command.
       
   119  */
       
   120 void CAmaStartAsync::DoCancel()
       
   121 	{
       
   122 	if (iRunning)
       
   123 		{
       
   124 		iSsmStartSafe->StartCancel(iSSCancelIndex);
       
   125 		iDscStore.EnumClose();
       
   126 		User::RequestComplete(iAmaStatus, KErrCancel);
       
   127 		iRunning = EFalse;
       
   128 		}	
       
   129 	}
       
   130 
       
   131 void CAmaStartAsync::StartDscItem(const CDscItem& aDscItem)
       
   132 	{
       
   133 	// start the process
       
   134 	const CSsmStartupProperties& properties = aDscItem.SsmStartupProperties();
       
   135 	iSsmStartSafe->Start(properties, iCurrentProcess, iStatus, iSSCancelIndex);
       
   136 	SetActive();
       
   137 	}
       
   138 
       
   139 void CAmaStartAsync::MonitorProcessL(const CStartupProperties& aProperties, const RProcess& aProcess)
       
   140 	{
       
   141 	// first time monitoring, so connect with the SysMon server
       
   142 	if (KNullHandle == iSysMon.Handle())
       
   143 		{
       
   144 		iSysMon.OpenL();
       
   145 		}
       
   146 
       
   147 	// monitor the process	
       
   148 	iSysMon.MonitorL(aProperties, aProcess);
       
   149 	}
       
   150 
       
   151 /**
       
   152  * Succsessive calls to StartDscItem, but only one per RunL in order for
       
   153  * the scheduler/ server to remain responsive.
       
   154  */
       
   155 void CAmaStartAsync::RunL()
       
   156 	{
       
   157 	//monitor the process if indicated
       
   158 	if (iCurrentDscItem->Monitored() && KErrNone == iStatus.Int())
       
   159 		{
       
   160 		const CStartupProperties& properties = iCurrentDscItem->StartupProperties();
       
   161 		//if the start method is Fire and Forget don't take any action, 
       
   162 		//monitor will be started by fire and forget itself if required.
       
   163 		if(properties.StartMethod() != EFireAndForget)
       
   164 			{
       
   165 			TRAPD(error, MonitorProcessL(properties, iCurrentProcess));
       
   166 
       
   167 			//Kill the started process in case of an error as Start and Monitor should be one atomic function.
       
   168 			if (KErrNone != error)
       
   169 				{
       
   170 				iCurrentProcess.Kill(error);
       
   171 				}
       
   172 			}
       
   173 		}
       
   174 
       
   175 	iCurrentProcess.Close();
       
   176 	delete iCurrentDscItem;
       
   177 	iCurrentDscItem = NULL;
       
   178 	DoStartL();
       
   179 	}