smartinstaller/adm/inc/ADMStateMachine.h
branchADM
changeset 48 364021cecc90
equal deleted inserted replaced
47:3f419852be07 48:364021cecc90
       
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *     State machine
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #ifndef ADMSTATEMACHINE_H_
       
    22 #define ADMSTATEMACHINE_H_
       
    23 
       
    24 #include <e32base.h>
       
    25 #include <e32debug.h>
       
    26 
       
    27 #include "ADMAppUi.h"
       
    28 #include "ADMInstallManager.h"
       
    29 #include "ADMPackageInfo.h"
       
    30 #include "ADMSisParser.h"
       
    31 #include "ADMXmlParser.h"
       
    32 
       
    33 // Forward declarations
       
    34 class TState;
       
    35 class CStateMachine;
       
    36 
       
    37 enum TStatePanic
       
    38 	{
       
    39 	EPanicStateIndexOutOfBounds = 1000,
       
    40 	EPanicStateImplementationMissing,
       
    41 	EPanicStateInvalidBootstrapVersion,
       
    42 	EPanicStateInvalidMachineUid,
       
    43 	EPanicStateFetchStatus,
       
    44 	EPanicStateFetchPackageNull,
       
    45 	EPanicStateUnexpectedPackage,
       
    46 	EPanicStateUnexpectedRootPackage,
       
    47 	EPanicStateWrapperNotInstalled
       
    48 	};
       
    49 
       
    50 const TInt KUnknownState = -40000;
       
    51 
       
    52 // Timeout for waiting for download deletion. Default 2 s.
       
    53 const TInt KDlDeletingTimeout = 2*1000*1000;
       
    54 
       
    55 class CStateFactory : public CBase
       
    56 	{
       
    57 public:
       
    58 	enum TAppState
       
    59 		{
       
    60 		EStateSetup = 0,                   // 0
       
    61 		EStateSetupParsing,
       
    62 		EStateBuildDepTree,
       
    63 		EStateDownloadChangesFile,
       
    64 		EStateParseChangesFile,
       
    65 		EStateDownloadDepFile,             // 5
       
    66 		EStateParseDepFile,
       
    67 		EStateBuildFetchList,
       
    68 		EStateVerifyAvailableDiskSpace,
       
    69 		EStateConfirmDownload,
       
    70 		EStateConfirmRoamingDownload,      // 10
       
    71 		EStateStartDependencyDownload,
       
    72 		EStateDownloadDependency,
       
    73 		EStateInstallDependency,
       
    74 		EStateInstallAppSis,
       
    75 		EStateUninstallDependency,         // 15
       
    76 		EStatePrepareExitWithError,
       
    77 		EStateLaunchApp,
       
    78 		EStateExit,
       
    79 
       
    80 		EStateLast
       
    81 		};
       
    82 
       
    83 	TState& GetState(const TAppState aState) const;
       
    84 	TInt StateIndex(const TState* aState) const;
       
    85 
       
    86 	inline CStateMachine& StateMachine() const { return iStateMachine; }
       
    87 
       
    88 	static CStateFactory* NewL(CStateMachine& aStateMachine);
       
    89 	~CStateFactory();
       
    90 
       
    91 private:
       
    92 	CStateFactory(CStateMachine& aStateMachine);
       
    93 	void ConstructL();
       
    94 
       
    95 private:
       
    96 	TFixedArray<TState*, EStateLast> iStates;
       
    97 	CStateMachine&   iStateMachine;
       
    98 	};
       
    99 
       
   100 // Base class for a state
       
   101 class TState
       
   102 	{
       
   103 public:
       
   104 	//friend class CStateFactory;
       
   105 	//friend class CStateMachine;
       
   106 
       
   107 	TState(const CStateFactory& aFactory);
       
   108 
       
   109 	// Change of states
       
   110 	virtual void Enter() const;
       
   111 	virtual void Exit() const;
       
   112 
       
   113 	// Events from UI
       
   114 	virtual void UserAccepted();
       
   115 	virtual void UserRejected();
       
   116 	virtual void UserCancelled();
       
   117 
       
   118 	// Events from DownloadManager
       
   119 	virtual void DownloadInProgress(const TInt aAlreadyDownloaded, const TInt aDownloadSize, const TReal32 aAvgDlSpeed);
       
   120 	virtual void DownloadCompleted(const TInt aBytesDownloaded);
       
   121 	virtual void DownloadFailed();
       
   122 	virtual void DownloadCancelled();
       
   123 
       
   124 	// Events from InstallManager
       
   125 	virtual void InstallInProgress();
       
   126 	virtual void InstallCompleted(TInt aStatus);
       
   127 	virtual void InstallationCancelled();
       
   128 
       
   129 protected:
       
   130 	void DownloadFailed(const CStateFactory::TAppState aState);
       
   131 	void PanicInState(TStatePanic aPanic) const;
       
   132 
       
   133 protected:
       
   134 	const CStateFactory& iFactory;
       
   135 	CStateMachine& iStateMachine;
       
   136 	};
       
   137 
       
   138 // ----------------------------------------------------------------------------
       
   139 class TStateSetup : public TState
       
   140 	{
       
   141 public:
       
   142 	TStateSetup(const CStateFactory& aFactory);
       
   143 
       
   144 	void Enter() const;
       
   145 	};
       
   146 // ----------------------------------------------------------------------------
       
   147 class TStateSetupParsing : public TState
       
   148 	{
       
   149 public:
       
   150 	TStateSetupParsing(const CStateFactory& aFactory);
       
   151 
       
   152 	void Enter() const;
       
   153 	};
       
   154 // ----------------------------------------------------------------------------
       
   155 class TStateDownloadDepFile : public TState
       
   156 	{
       
   157 public:
       
   158 	TStateDownloadDepFile(const CStateFactory& aFactory);
       
   159 
       
   160 	void Enter() const;
       
   161 
       
   162 	void DownloadCompleted(const TInt aBytesDownloaded);
       
   163 	void DownloadFailed();
       
   164 	};
       
   165 // ----------------------------------------------------------------------------
       
   166 class TStateParseDepFile : public TState
       
   167 	{
       
   168 public:
       
   169 	TStateParseDepFile(const CStateFactory& aFactory);
       
   170 
       
   171 	void Enter() const;
       
   172 	};
       
   173 // ----------------------------------------------------------------------------
       
   174 class TStateBuildDepTree : public TState
       
   175 	{
       
   176 public:
       
   177 	TStateBuildDepTree(const CStateFactory& aFactory);
       
   178 
       
   179 	void Enter() const;
       
   180 	};
       
   181 // ----------------------------------------------------------------------------
       
   182 class TStateDownloadChangesFile : public TState
       
   183 	{
       
   184 public:
       
   185 	TStateDownloadChangesFile(const CStateFactory& aFactory);
       
   186 
       
   187 	void Enter() const;
       
   188 
       
   189 	void DownloadCompleted(const TInt aBytesDownloaded);
       
   190 	void DownloadFailed();
       
   191 	};
       
   192 // ----------------------------------------------------------------------------
       
   193 class TStateParseChangesFile : public TState
       
   194 	{
       
   195 public:
       
   196 	TStateParseChangesFile(const CStateFactory& aFactory);
       
   197 
       
   198 	void Enter() const;
       
   199 	};
       
   200 // ----------------------------------------------------------------------------
       
   201 class TStateBuildFetchList : public TState
       
   202 	{
       
   203 public:
       
   204 	TStateBuildFetchList(const CStateFactory& aFactory);
       
   205 
       
   206 	void Enter() const;
       
   207 	};
       
   208 // ----------------------------------------------------------------------------
       
   209 class TStateVerifyAvailableDiskSpace : public TState
       
   210 	{
       
   211 public:
       
   212 	TStateVerifyAvailableDiskSpace(const CStateFactory& aFactory);
       
   213 
       
   214 	void Enter() const;
       
   215 	};
       
   216 // ----------------------------------------------------------------------------
       
   217 class TStateConfirmDownload : public TState
       
   218 	{
       
   219 public:
       
   220 	TStateConfirmDownload(const CStateFactory& aFactory);
       
   221 
       
   222 	void Enter() const;
       
   223 	void UserAccepted();
       
   224 	};
       
   225 // ----------------------------------------------------------------------------
       
   226 class TStateConfirmRoamingDownload : public TState
       
   227 	{
       
   228 public:
       
   229 	TStateConfirmRoamingDownload(const CStateFactory& aFactory);
       
   230 
       
   231 	void Enter() const;
       
   232 	void UserAccepted();
       
   233 	};
       
   234 // ----------------------------------------------------------------------------
       
   235 class TStateStartDependencyDownload : public TState
       
   236 	{
       
   237 public:
       
   238 	TStateStartDependencyDownload(const CStateFactory& aFactory);
       
   239 
       
   240 	void Enter() const;
       
   241 	};
       
   242 // ----------------------------------------------------------------------------
       
   243 class TStateDownloadDependency : public TState
       
   244 	{
       
   245 public:
       
   246 	TStateDownloadDependency(const CStateFactory& aFactory);
       
   247 
       
   248 	void Enter() const;
       
   249 
       
   250 	void DownloadCompleted(const TInt aBytesDownloaded);
       
   251 	void DownloadInProgress(const TInt aAlreadyDownloaded, const TInt aDownloadSize, const TReal32 aAvgDlSpeed);
       
   252 	void DownloadFailed();
       
   253 
       
   254 private:
       
   255 	TBool iRetryDownload; // Set to true, if we're retrying download
       
   256 	};
       
   257 // ----------------------------------------------------------------------------
       
   258 class TStateInstallDependency : public TState
       
   259 	{
       
   260 public:
       
   261 	TStateInstallDependency(const CStateFactory& aFactory);
       
   262 
       
   263 	void Enter() const;
       
   264 	void Exit() const;
       
   265 	void InstallCompleted(TInt aStatus);
       
   266 	};
       
   267 // ----------------------------------------------------------------------------
       
   268 class TStateInstallAppSis : public TState
       
   269 	{
       
   270 public:
       
   271 	TStateInstallAppSis(const CStateFactory& aFactory);
       
   272 
       
   273 	void Enter() const;
       
   274 	void InstallCompleted(TInt aStatus);
       
   275 	};
       
   276 // ----------------------------------------------------------------------------
       
   277 class TStateUninstallDependency : public TState
       
   278 	{
       
   279 public:
       
   280 	TStateUninstallDependency(const CStateFactory& aFactory);
       
   281 
       
   282 	void Enter() const;
       
   283 	void InstallCompleted(TInt aStatus);
       
   284 	};
       
   285 // ----------------------------------------------------------------------------
       
   286 class TStatePrepareExitWithError : public TState
       
   287 	{
       
   288 public:
       
   289 	TStatePrepareExitWithError(const CStateFactory& aFactory);
       
   290 
       
   291 	void Enter() const;
       
   292 
       
   293 private:
       
   294 	TInt CreateInstallationResumeInfo() const;
       
   295 	void BackupFilesL() const;
       
   296 	};
       
   297 // ----------------------------------------------------------------------------
       
   298 class TStateLaunchApp : public TState
       
   299 	{
       
   300 public:
       
   301 	TStateLaunchApp(const CStateFactory& aFactory);
       
   302 
       
   303 	void Enter() const;
       
   304 	void UserAccepted();
       
   305 	void UserRejected();
       
   306 };
       
   307 // ----------------------------------------------------------------------------
       
   308 class TStateExit : public TState
       
   309 	{
       
   310 public:
       
   311 	TStateExit(const CStateFactory& aFactory);
       
   312 
       
   313 	void Enter() const;
       
   314 
       
   315 private:
       
   316 	void RemoveResumeFiles() const;
       
   317 
       
   318 #ifdef FEATURE_REPORTING
       
   319 	void Report() const;
       
   320 
       
   321 public:
       
   322 	void DownloadInProgress(const TInt /* aAlreadyDownloaded */, const TInt /* aDownloadSize */, const TReal32 /* aAvgDlSpeed */);
       
   323 	void DownloadCompleted(const TInt /* aBytesDownloaded */);
       
   324 	void DownloadFailed();
       
   325 	void DownloadCancelled();
       
   326 #endif
       
   327 	};
       
   328 
       
   329 // ----------------------------------------------------------------------------
       
   330 class CStateMachine : public CActive,
       
   331 	public CInstallManager::MInstallClient,
       
   332 	public CDownloadHandler::MDownloadClient
       
   333 	{
       
   334 public:
       
   335 	friend class TState;
       
   336 	friend class TStateBuildDepTree;
       
   337 	friend class TStateBuildFetchList;
       
   338 	friend class TStateConfirmDownload;
       
   339 	friend class TStateConfirmRoaming;
       
   340 	friend class TStateDownloadChangesFile;
       
   341 	friend class TStateDownloadDepFile;
       
   342 	friend class TStateDownloadDependency;
       
   343 	friend class TStateExit;
       
   344 	friend class TStateInstallAppSis;
       
   345 	friend class TStateInstallDependency;
       
   346 	friend class TStateLaunchApp;
       
   347 	friend class TStateParseChangesFile;
       
   348 	friend class TStateParseDepFile;
       
   349 	friend class TStateParseSisFile;
       
   350 	friend class TStatePrepareExitWithError;
       
   351 	friend class TStateSetup;
       
   352 	friend class TStateSetupParsing;
       
   353 	friend class TStateStartDependencyDownload;
       
   354 	friend class TStateUninstallDependency;
       
   355 	friend class TStateVerifyAvailableDiskSpace;
       
   356 	friend class TStateWaitForUserConfirmation;
       
   357 
       
   358 	static CStateMachine* NewL(CADMAppUi& aAppUi, const TDesC& aDownloadPath);
       
   359 	~CStateMachine();
       
   360 
       
   361 	void Start();
       
   362 	void Stop();
       
   363 	void SetStartState(const CStateFactory::TAppState aState);
       
   364 	void SetState(const CStateFactory::TAppState aState);
       
   365 	TInt StateIndex() const;
       
   366 
       
   367 	// Interfacing for ADM
       
   368 	// Called when user has made a selection in a query dialog
       
   369 	void SetIAP(TUint32 aIAP);
       
   370 	void HandleUserResponse(TInt aResponse);
       
   371 	CStateFactory::TAppState FailedState() const { return iFailedState; }
       
   372 	inline TInt32 TotalDownloadSize() const { return iDepTree->GetTotalDownloadSize(); }
       
   373 	inline HBufC* LaunchCaption() const
       
   374 		{
       
   375 			CPackageInfo* root = iDepTree->GetRootNode();
       
   376 			if (root)
       
   377 				return root->GetPackageName();
       
   378 			else
       
   379 				return NULL;
       
   380 		}
       
   381 
       
   382 #ifdef USE_LOGFILE
       
   383 	RFileLogger& Log() const { return iAppUi.iLog; }
       
   384 #endif
       
   385 
       
   386 	// Interface to ADM
       
   387 	inline TBool ResumeRequired() const { return iAppUi.iIsResumeRequired; }
       
   388 	inline TInt MachineUid() const { return iAppUi.iMachineUid; }
       
   389 	inline TInt BootstrapVersion() const { return iAppUi.iBootstrapVersion; }
       
   390 	inline TBool RegisteredRoaming() const { return iAppUi.RegisteredRoaming(); }
       
   391 	inline TBool UsingWLAN() const { return iAppUi.UsingWLAN(); }
       
   392 	inline TBool OviStoreRunning() const { return iAppUi.iOviStoreRunning; }
       
   393 	inline TBool ResumingInstallation() const { return iAppUi.iResumingInstallation; }
       
   394 	inline HBufC* DepFilename() const { return iAppUi.iDepFileName; }
       
   395 	inline HBufC* SisFilename() const { return iAppUi.iSisFileName; }
       
   396 	inline HBufC8* ConfigUrl() const { return iAppUi.iConfigUrl; }
       
   397 	inline TUid& WrapperPackageUid() const { return (TUid&)iAppUi.iWrapperPackageUid; }
       
   398 	inline RFs& FsSession() const { return iAppUi.EikonEnv()->FsSession(); }
       
   399 	inline void SetSilentInstallationOk(const TBool aOk) { iAppUi.iSilentInstallationOk = aOk; }
       
   400 	inline void SetNonSilentInstallation(const TBool aVal) { iAppUi.iNonSilentInstallation = aVal; }
       
   401 	inline void SetAppLaunch(const TBool aSuccess) { iAppUi.iAppLaunch = aSuccess; }
       
   402 	inline TBool AppLaunch() const { return iAppUi.iAppLaunch; }
       
   403 	inline TInt ExitReason() const { return iAppUi.iExitReason; }
       
   404 	void UpdateProgressBar(const TInt aStartValue, const TInt aEndValue);
       
   405 	void StopProgressBar() const;
       
   406 	void SetFailureReason(TExitReason aReason);
       
   407 	void ShowDownloadQueryL(const TInt aResourceId, const TInt aInfoResourceId = -1);
       
   408 	void ShowWaitNoteL(const TInt aResourceId);
       
   409 	void ShowInstallingWaitNoteL(const TInt aCurrent, const TInt aLast);
       
   410 	void ShowProgressBarL(const TInt aStartValue, const TInt aEndValue, const TInt aCurrent = -1, const TInt aLast = -1);
       
   411 	void StopWaitNote();
       
   412 
       
   413 protected:
       
   414 	void RunL();
       
   415 	void DoCancel();
       
   416 	TInt RunError(const TInt aError);
       
   417 
       
   418 	void SetState(TState& aState);
       
   419 
       
   420 	//From MInstallClient
       
   421 	void HandleInstallCompletedL(TInt aStatus);
       
   422 
       
   423 	//From MDownloadClient
       
   424 	/**
       
   425 	 * Called by Download Manager when IAP has changed.
       
   426 	 * @param aIAP IAP ID
       
   427 	 */
       
   428 	void HandleIapChanged(const TUint32 aIAP);
       
   429 	/**
       
   430 	 * Called by Download Manager when downloading of a packet is completed.
       
   431 	 * @param aDlFileName Name of the downloaded file.
       
   432 	 * @param aResponseType Download type.
       
   433 	 * @param aDownloadSize Total number of bytes downloaded.
       
   434 	 */
       
   435 	void HandleHttpFetchCompleted( const TDesC& aDlFilename, const TInt aDownloadType, const TInt32 aBytesDownloaded, const TReal32 aAvgDlSpeed, const TInt64 aDlTime );
       
   436 
       
   437 	/**
       
   438 	 * Called when download manager has retrieved a packet from the network.
       
   439 	 * Required for updating the progress bar.
       
   440 	 */
       
   441 	void HandleHttpFetchInProgress(const TInt32 aAlreadyDownloaded, const TInt32 aDownloadSize, const TReal32 aAvgDlSpeed);
       
   442 
       
   443 	/**
       
   444 	 * Called when download manager encounters a problem while downloading
       
   445 	 */
       
   446 	void HandleHttpFetchFailure( const TDesC& aDlFilename, const TInt aGlobalErrorId, const TInt aErrorId, const TInt aFetchType );
       
   447 
       
   448 	/**
       
   449 	 * Called when download manager is deleting the download.
       
   450 	 */
       
   451 	void HandleDownloadDeleting();
       
   452 
       
   453 	// State machine required functions
       
   454 	//void HandleFailure();
       
   455 	//TBool VerifyAvailableDiskSpace();
       
   456 	//void SilentInstallDownloadedPackageL();
       
   457 
       
   458 private:
       
   459 	CStateMachine(CADMAppUi& aAppUi);
       
   460 	void ConstructL(const TDesC& aDownloadPath);
       
   461 
       
   462 private:
       
   463 	CADMAppUi&			iAppUi;
       
   464 	CStateFactory*		iStateFactory;     //< owns
       
   465 	CInstallManager*	iInstallManager;   //< owns
       
   466 	CDownloadHandler*	iDownloadHandler;  //< owns
       
   467 
       
   468 	CXmlParser*			iXmlParser; //< owns
       
   469 	CSisParser*			iSisParser; //< owns
       
   470 	CDepTree*			iDepTree;   //< owns
       
   471 	CPackageInfo*		iCurrentPackage;
       
   472 
       
   473 	/**
       
   474 	 * Path to the downloaded file. Updated after each finishing each
       
   475 	 * download (completed or failed).
       
   476 	 */
       
   477 	HBufC*              iDownloadedFile;   //< owns
       
   478 
       
   479 	/**
       
   480 	 * Current state.
       
   481 	 */
       
   482 	TState*             iState;
       
   483 
       
   484 	/**
       
   485 	 * The state that experienced failure that causes ADM to exit.
       
   486 	 */
       
   487 	CStateFactory::TAppState    iFailedState;
       
   488 
       
   489 	/**
       
   490 	 * Number of dependency tree nodes processed so far.
       
   491 	 */
       
   492 	TInt   iNodesProcessed;
       
   493 
       
   494 	/**
       
   495 	 * Progress bar current value.
       
   496 	 */
       
   497 	TInt32 iProgressCurrentValue;
       
   498 
       
   499 	/**
       
   500 	 * Progress bar maximum value. Stays constant after being set.
       
   501 	 */
       
   502 	TInt32 iProgressMaxValue;
       
   503 
       
   504 	/**
       
   505 	 * Current package # being downloaded/installed.
       
   506 	 */
       
   507 	TInt iCounterCurrent;
       
   508 
       
   509 	/**
       
   510 	 * Total number of packages being downloaded/installed.
       
   511 	 */
       
   512 	TInt iCounterMax;
       
   513 
       
   514 	/**
       
   515 	 * Total size of the packages to be downloaded in bytes.
       
   516 	 * Used in progress bar update.
       
   517 	 */
       
   518 	TInt32 iDownloadSize;
       
   519 
       
   520 	/**
       
   521 	 * Downloaded bytes so far.
       
   522 	 * Used in progress bar update.
       
   523 	 */
       
   524 	TInt32 iCumulativeDepDownloaded;
       
   525 
       
   526 	/**
       
   527 	 * Total number of bytes downloaded during session. Includes all
       
   528 	 * dependency XML file downloads and .sis file downloads.
       
   529 	 */
       
   530 	TInt32 iCumulativeDownloaded;
       
   531 
       
   532 	/**
       
   533 	 * Time in microseconds used in downloading, used for calculating average download speed.
       
   534 	 */
       
   535 	TInt64 iDownloadTime;
       
   536 
       
   537 	/**
       
   538 	 * Drive in which wrapper is installed.
       
   539 	 */
       
   540 	TChar iWrapperInstalledDrive;
       
   541 
       
   542 	/**
       
   543 	 * Restart Attempt for Download Failure
       
   544 	 */
       
   545 	TInt iRestartAttempt;
       
   546 
       
   547 #ifdef FEATURE_LAUNCH_INSTALLED_APP
       
   548 	/**
       
   549 	 * App Launch name
       
   550 	 */
       
   551 	TFileName iLaunchName;
       
   552 	TFileName iLaunchIconFilename;
       
   553 	TApaAppCaption iLaunchCaption;
       
   554 #endif
       
   555 
       
   556 	/**
       
   557 	 * Semaphore for signalling download deleting.
       
   558 	 */
       
   559 	RSemaphore iDlDeletingSemaphore;
       
   560 	};
       
   561 
       
   562 #endif /* ADMSTATEMACHINE_H_ */