|         |      1 // Copyright (c) 2004-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 // cwatcherlauncher.cpp | 
|         |     15 // | 
|         |     16  | 
|         |     17 #include "cwatcherlauncher.h" | 
|         |     18  | 
|         |     19 #include <ecom/ecom.h> | 
|         |     20  | 
|         |     21 #include <watcher.h> | 
|         |     22 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS  | 
|         |     23 #include "cwatcher.h" | 
|         |     24 #endif | 
|         |     25  | 
|         |     26 const TInt KWatcherMaxStartCount = 5; | 
|         |     27  | 
|         |     28 CWatcherLauncher* CWatcherLauncher::NewL(const TDesC& aWatcherName, TUid aWatcherUid, RFs& aFs, CWatcherLog& aLog) | 
|         |     29 	{ | 
|         |     30 	CWatcherLauncher* self = new (ELeave) CWatcherLauncher(aWatcherUid, aFs, aLog); | 
|         |     31 	CleanupStack::PushL(self); | 
|         |     32 	self->ConstructL(aWatcherName); | 
|         |     33 	CleanupStack::Pop(self); | 
|         |     34 	return self; | 
|         |     35 	} | 
|         |     36 	 | 
|         |     37 CWatcherLauncher::CWatcherLauncher(TUid aWatcherUid, RFs& aFs, CWatcherLog& aLog) | 
|         |     38 : CActive(CActive::EPriorityStandard), iWatcherUid(aWatcherUid), iFs(aFs), iLog(aLog) | 
|         |     39 	{ | 
|         |     40 	CActiveScheduler::Add(this);	 | 
|         |     41 	} | 
|         |     42 	 | 
|         |     43 void CWatcherLauncher::ConstructL(const TDesC& aWatcherName) | 
|         |     44 	{ | 
|         |     45 	// Create timer for use in RunError(). Closed if RunL() doesn't leave | 
|         |     46 	User::LeaveIfError(iTimer.CreateLocal()); | 
|         |     47  | 
|         |     48 	// iWatcherName is used for logging only. Deleted if RunL() doesn't leave | 
|         |     49 	iWatcherName = aWatcherName.AllocL(); | 
|         |     50  | 
|         |     51 	// Defer launching of the watcher to RunL() | 
|         |     52 	TRequestStatus* status = &iStatus; | 
|         |     53 	User::RequestComplete(status, KErrNone); | 
|         |     54 	SetActive(); | 
|         |     55 	} | 
|         |     56  | 
|         |     57 CWatcherLauncher::~CWatcherLauncher() | 
|         |     58 	{ | 
|         |     59 	Cancel(); | 
|         |     60 	 | 
|         |     61 	delete iWatcher; | 
|         |     62 	delete iWatcherName; | 
|         |     63 	iTimer.Close(); | 
|         |     64 	 | 
|         |     65 	REComSession::DestroyedImplementation(iDtor_ID_Key); | 
|         |     66 	} | 
|         |     67 	 | 
|         |     68 // methods from CActive | 
|         |     69  | 
|         |     70 void CWatcherLauncher::RunL() | 
|         |     71 	{ | 
|         |     72 	iLog.Printf(_L("Watcher %S starting..."), iWatcherName); | 
|         |     73 	 | 
|         |     74 	// Create the new watcher object - 	get the instantiation of the active  | 
|         |     75 	// object form the ECOM server. | 
|         |     76 	TWatcherParams params(iFs, iLog); | 
|         |     77 	iWatcher = reinterpret_cast<CActive*> | 
|         |     78 						(REComSession::CreateImplementationL( | 
|         |     79 														iWatcherUid, | 
|         |     80 														iDtor_ID_Key, | 
|         |     81 														¶ms)); | 
|         |     82  | 
|         |     83 	// Members no longer required... | 
|         |     84 	iTimer.Close(); | 
|         |     85 	delete iWatcherName; | 
|         |     86 	iWatcherName = NULL; | 
|         |     87 	} | 
|         |     88 	 | 
|         |     89 void CWatcherLauncher::DoCancel() | 
|         |     90 	{ | 
|         |     91 	iTimer.Cancel(); | 
|         |     92 	} | 
|         |     93 	 | 
|         |     94 TInt CWatcherLauncher::RunError(TInt aError) | 
|         |     95 	{ | 
|         |     96 	iLog.Printf(_L("Watcher %S construction error: %d [startCount=%d]"), iWatcherName, aError, iStartCount); | 
|         |     97  | 
|         |     98 	// Restart the watcher if iStartCount < KWatcherMaxStartCount | 
|         |     99 	if( iStartCount < KWatcherMaxStartCount ) | 
|         |    100 		{ | 
|         |    101 		++iStartCount; | 
|         |    102 		iLog.Printf(_L("Watcher %S restarting in %d secs"), iWatcherName, KWatcherDelay/1000000); | 
|         |    103 		iTimer.After(iStatus, KWatcherDelay); | 
|         |    104 		SetActive(); | 
|         |    105 		} | 
|         |    106 	else | 
|         |    107 		{ | 
|         |    108 		iLog.Printf(_L("Watcher %S permanent fail"), iWatcherName); | 
|         |    109 		iTimer.Close(); | 
|         |    110 		} | 
|         |    111 	return KErrNone; | 
|         |    112 	} |