core/src/command_wrappers.h
changeset 0 7f656887cf89
child 78 b3ffff030d5c
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // command_wrappers.h
       
     2 // 
       
     3 // Copyright (c) 2006 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 
       
    14 #ifndef __COMMAND_WRAPPERS_H__
       
    15 #define __COMMAND_WRAPPERS_H__
       
    16 
       
    17 #include <e32base.h>
       
    18 #include <fshell/iocli.h>
       
    19 #include <fshell/ioutils.h>
       
    20 #include "command_constructors.h"
       
    21 
       
    22 using namespace IoUtils;
       
    23 
       
    24 
       
    25 class MCommandObserver;
       
    26 
       
    27 
       
    28 class MCommand
       
    29 	{
       
    30 public:
       
    31 	virtual RIoReadHandle& CmndStdin() = 0;
       
    32 	virtual RIoWriteHandle& CmndStdout() = 0;
       
    33 	virtual RIoWriteHandle& CmndStderr() = 0;
       
    34 	virtual TInt CmndRun(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver, RIoSession& aIoSession) = 0;
       
    35 	virtual void CmndForeground() = 0;
       
    36 	virtual void CmndBackground() = 0;
       
    37 	virtual void CmndKill() = 0;
       
    38 	virtual TInt CmndSuspend() = 0;
       
    39 	virtual TInt CmndResume() = 0;
       
    40 	virtual void CmndRelease() = 0;
       
    41 	virtual TExitType CmndExitType() const = 0;
       
    42 	virtual TExitCategoryName CmndExitCategory() const = 0;
       
    43 	virtual const TDesC& CmndName() const = 0;
       
    44 	virtual TInt CmndReattachStdin(RIoEndPoint& aStdinEndPoint) = 0;
       
    45 	virtual TInt CmndReattachStdout(RIoEndPoint& aStdoutEndPoint) = 0;
       
    46 	virtual TInt CmndReattachStderr(RIoEndPoint& aStderrEndPoint) = 0;
       
    47 	virtual TBool CmndIsDisownable() const = 0;
       
    48 	virtual void CmndDisown() = 0;
       
    49 	};
       
    50 
       
    51 
       
    52 class MCommandObserver
       
    53 	{
       
    54 public:
       
    55 	virtual void HandleCommandComplete(MCommand& aCommand, TInt aError) = 0;
       
    56 	};
       
    57 
       
    58 
       
    59 class CCommandWrapperBase : public CBase, public MCommand
       
    60 	{
       
    61 protected:
       
    62 	CCommandWrapperBase();
       
    63 	~CCommandWrapperBase();
       
    64 	void BaseConstructL(const TDesC& aName);
       
    65 protected: // From MCommand.
       
    66 	virtual RIoReadHandle& CmndStdin();
       
    67 	virtual RIoWriteHandle& CmndStdout();
       
    68 	virtual RIoWriteHandle& CmndStderr();
       
    69 	virtual const TDesC& CmndName() const;
       
    70 	virtual TInt CmndReattachStdin(RIoEndPoint& aStdinEndPoint);
       
    71 	virtual TInt CmndReattachStdout(RIoEndPoint& aStdoutEndPoint);
       
    72 	virtual TInt CmndReattachStderr(RIoEndPoint& aStderrEndPoint);
       
    73 	virtual TBool CmndIsDisownable() const;
       
    74 	virtual void CmndDisown();
       
    75 private: // From MCommand.
       
    76 	virtual void CmndRelease();
       
    77 private:
       
    78 	HBufC* iName;	///< This is used by concrete classes as they see fit.
       
    79 	RIoReadHandle iStdin;
       
    80 	RIoWriteHandle iStdout;
       
    81 	RIoWriteHandle iStderr;
       
    82 	};
       
    83 
       
    84 
       
    85 class CThreadCommand : public CCommandWrapperBase
       
    86 	{
       
    87 public:
       
    88 	enum TFlags
       
    89 		{
       
    90 		EUpdateEnvironment = 0x00000001,
       
    91 		ESharedHeap = 0x00000002, // Any command that accesses gShell must have this set
       
    92 		};
       
    93 public:
       
    94 	static CThreadCommand* NewL(const TDesC& aName, TCommandConstructor aCommandConstructor, TUint aFlags);
       
    95 	~CThreadCommand();
       
    96 private:
       
    97 	CThreadCommand(TCommandConstructor aCommandConstructor, TUint aFlags);
       
    98 	void ConstructL(const TDesC& aName);
       
    99 private: // From MCommand.
       
   100 	virtual TInt CmndRun(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver, RIoSession& aIoSession);
       
   101 	virtual void CmndForeground();
       
   102 	virtual void CmndBackground();
       
   103 	virtual void CmndKill();
       
   104 	virtual TInt CmndSuspend();
       
   105 	virtual TInt CmndResume();
       
   106 	virtual TExitType CmndExitType() const;
       
   107 	virtual TExitCategoryName CmndExitCategory() const;
       
   108 public:
       
   109 	class TArgs
       
   110 		{
       
   111 	public:
       
   112 		TArgs(TUint aFlags, CEnvironment& aEnv, TCommandConstructor aCommandConstructor, const TDesC& aCommandLine, TRequestStatus& aParentStatus);
       
   113 	public:
       
   114 		TUint iFlags;
       
   115 		CEnvironment& iEnv;
       
   116 		TCommandConstructor iCommandConstructor;
       
   117 		const TPtrC iCommandLine;
       
   118 		TRequestStatus* iParentStatus;
       
   119 		TThreadId iParentThreadId;
       
   120 		};
       
   121 	class CThreadWatcher : public CActive
       
   122 		{
       
   123 	public:
       
   124 		static CThreadWatcher* NewL();
       
   125 		~CThreadWatcher();
       
   126 		TInt Logon(CThreadCommand& aCommand, RThread& aThread, MCommandObserver& aObserver);
       
   127 		void SetActive();
       
   128 	private:
       
   129 		CThreadWatcher();
       
   130 	private: // From CActive.
       
   131 		virtual void RunL();
       
   132 		virtual void DoCancel();
       
   133 	private:
       
   134 		CThreadCommand* iCommand;
       
   135 		RThread* iThread;
       
   136 		MCommandObserver* iObserver;
       
   137 		};
       
   138 private:
       
   139 	TUint iFlags;
       
   140 	TCommandConstructor iCommandConstructor;
       
   141 	MCommandObserver* iObserver;
       
   142 	TArgs* iArgs;
       
   143 	RThread iThread;
       
   144 	CThreadWatcher* iWatcher;
       
   145 	};
       
   146 
       
   147 
       
   148 class CProcessCommand : public CCommandWrapperBase
       
   149 	{
       
   150 public:
       
   151 	static CProcessCommand* NewL(const TDesC& aExeName);
       
   152 	static CProcessCommand* NewL(const TDesC& aExeName, RProcess& aProcess);
       
   153 	~CProcessCommand();
       
   154 protected:
       
   155 	CProcessCommand();
       
   156 	void ConstructL(const TDesC& aExeName, RProcess* aProcess = NULL);
       
   157 	virtual void CreateProcessL(const TDesC& aCommandLine, CEnvironment& aEnv);
       
   158 private:
       
   159 	void CmndRunL(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver);
       
   160 protected: // From MCommand.
       
   161 	virtual TInt CmndRun(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver, RIoSession& aIoSession);
       
   162 	virtual void CmndForeground();
       
   163 	virtual void CmndBackground();
       
   164 	virtual void CmndKill();
       
   165 	virtual TInt CmndSuspend();
       
   166 	virtual TInt CmndResume();
       
   167 	virtual TExitType CmndExitType() const;
       
   168 	virtual TExitCategoryName CmndExitCategory() const;
       
   169 	virtual TBool CmndIsDisownable() const;
       
   170 	virtual void CmndDisown();
       
   171 public:
       
   172 	class CProcessWatcher : public CActive
       
   173 		{
       
   174 	public:
       
   175 		static CProcessWatcher* NewL();
       
   176 		~CProcessWatcher();
       
   177 		TInt Logon(CProcessCommand& aCommand, RProcess& aProcess, MCommandObserver& aObserver);
       
   178 	private:
       
   179 		CProcessWatcher();
       
   180 	private: // From CActive.
       
   181 		virtual void RunL();
       
   182 		virtual void DoCancel();
       
   183 	private:
       
   184 		CProcessCommand* iCommand;
       
   185 		RProcess* iProcess;
       
   186 		MCommandObserver* iObserver;
       
   187 		};
       
   188 protected:
       
   189 	MCommandObserver* iObserver;
       
   190 	RProcess iProcess;
       
   191 	CProcessWatcher* iWatcher;
       
   192 	};
       
   193 
       
   194 
       
   195 class CPipsCommand : public CProcessCommand
       
   196 	{
       
   197 public:
       
   198 	static CPipsCommand* NewL(const TDesC& aExeName);
       
   199 	CPipsCommand();
       
   200 	~CPipsCommand();
       
   201 private: // From MCommand.
       
   202 	virtual TInt CmndRun(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver, RIoSession& aIoSession);
       
   203 	virtual void CmndKill();
       
   204 private: // From CProcessCommand.
       
   205 	virtual void CreateProcessL(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv);
       
   206 private:
       
   207 	TBool iUsingPipsRun;
       
   208 	TInt iPipsRunChildProcessId;
       
   209 	};
       
   210 
       
   211 
       
   212 class CAliasCommand : public CCommandWrapperBase, public MCommandObserver
       
   213 	{
       
   214 public:
       
   215 	static CAliasCommand* NewL(MCommand& aAliasedCommand, const TDesC* aAdditionalArguments, const TDesC* aReplacementArguments);
       
   216 	~CAliasCommand();
       
   217 private:
       
   218 	CAliasCommand(MCommand& aAliasedCommand);
       
   219 	void ConstructL(const TDesC* aAdditionalArguments, const TDesC* aReplacementArguments);
       
   220 private: // From MCommand.
       
   221 	RIoReadHandle& CmndStdin();
       
   222 	RIoWriteHandle& CmndStdout();
       
   223 	RIoWriteHandle& CmndStderr();
       
   224 	TInt CmndRun(const TDesC& aCommandLine, IoUtils::CEnvironment& aEnv, MCommandObserver& aObserver, RIoSession& aIoSession);
       
   225 	void CmndForeground();
       
   226 	void CmndBackground();
       
   227 	void CmndKill();
       
   228 	TInt CmndSuspend();
       
   229 	TInt CmndResume();
       
   230 	TExitType CmndExitType() const;
       
   231 	TExitCategoryName CmndExitCategory() const;
       
   232 private: // From MCommandObserver.
       
   233 	virtual void HandleCommandComplete(MCommand& aCommand, TInt aError);
       
   234 private:
       
   235 	MCommand& iAliasedCommand;
       
   236 	HBufC* iAdditionalArguments;
       
   237 	HBufC* iReplacementArguments;
       
   238 	MCommandObserver* iCommandObserver;
       
   239 	};
       
   240 
       
   241 
       
   242 #endif // __COMMAND_WRAPPERS_H__