sbProcess.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2009 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27 */
28 
29 #ifndef __SB_PROCESS_H__
30 #define __SB_PROCESS_H__
31 
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 //
35 // Songbird process services defs.
36 //
37 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 
45 //------------------------------------------------------------------------------
46 //
47 // Songbird process imported services.
48 //
49 //------------------------------------------------------------------------------
50 
51 // Songbird imports.
52 #include <sbMemoryUtils.h>
53 
54 // Mozilla imports.
55 #include <nsCOMPtr.h>
56 #include <nsIThread.h>
57 #include <nsStringGlue.h>
58 #include <nsTArray.h>
59 #include <prmon.h>
60 #include <prproces.h>
61 
62 
63 //------------------------------------------------------------------------------
64 //
65 // Songbird process services classes.
66 //
67 //------------------------------------------------------------------------------
68 
78 class sbProcess : public nsISupports
79 {
80  //----------------------------------------------------------------------------
81  //
82  // Public interface.
83  //
84  //----------------------------------------------------------------------------
85 
86 public :
87 
88  //
89  // Interface implementations.
90  //
91 
93 
94 
95  //
96  // Public services.
97  //
98 
104  static nsresult New(sbProcess** aProcess);
105 
109  nsresult Run();
110 
114  nsresult Kill();
115 
119  virtual ~sbProcess();
120 
125  nsresult GetArgList(nsTArray<nsString>& aArgList);
126  nsresult SetArgList(const nsTArray<nsString>& aArgList);
127 
131  nsresult GetStdinString(nsAString& aStdinString);
132  nsresult SetStdinString(const nsAString& aStdinString);
133 
137  nsresult GetStdoutString(nsAString& aStdoutString);
138 
142  nsresult GetPipeStdoutString(PRBool* aPipeStdoutString);
143  nsresult SetPipeStdoutString(PRBool aPipeStdoutString);
144 
149  nsresult GetDoneMonitor(PRMonitor** aDoneMonitor);
150  nsresult SetDoneMonitor(PRMonitor* aDoneMonitor);
151 
160  nsresult GetIsDone(PRBool* aIsDone);
161 
166  nsresult GetDoneResult(nsresult* aDoneResult);
167 
171  nsresult GetExitCode(PRInt32* aExitCode);
172 
173 
174  //----------------------------------------------------------------------------
175  //
176  // Private interface.
177  //
178  //----------------------------------------------------------------------------
179 
180 private:
181 
182  //
183  // Private fields.
184  //
185  // mProcessLock Lock used to serialize access to fields.
186  // mArgList List of process command line arguments.
187  // mPipeStdinString If true, pipe string to process stdin.
188  // mStdinString String to pipe to process stdin.
189  // mPipeStdoutString If true, pipe process stdout to string.
190  // mStdoutString String containing content of process stdout.
191  // mDoneResult Final result of process when it's done.
192  // mExitCode Process exit code.
193  // mBaseProcess Base process object.
194  // mStdinReadFD File descriptor for reading the process stdin.
195  // mStdoutReadFD File descriptor for reading the process stdout.
196  // mStdoutWriteFD File descriptor for writing the process stdout.
197  // mWaitForDoneThread Thread used to wait for the process to be done.
198  // mHasBeenKilled If true, process has been killed.
199  //
200  // mDoneMonitor Monitor to notify when process is done.
201  // mIsDone True if process is done.
202  //
203 
204  PRLock* mProcessLock;
205  nsTArray<nsString> mArgList;
206  PRBool mPipeStdinString;
207  nsString mStdinString;
208  PRBool mPipeStdoutString;
209  nsString mStdoutString;
210  nsresult mDoneResult;
211  PRInt32 mExitCode;
212  PRProcess* mBaseProcess;
213  PRFileDesc* mStdinReadFD;
214  PRFileDesc* mStdoutReadFD;
215  PRFileDesc* mStdoutWriteFD;
216  nsCOMPtr<nsIThread> mWaitForDoneThread;
217  PRBool mHasBeenKilled;
218 
219  PRMonitor* mDoneMonitor;
220  PRBool mIsDone;
221 
222 
223  //
224  // Private process services.
225  //
226 
230  sbProcess();
231 
237  void WaitForDone();
238 
242  nsresult HandleDone();
243 };
244 
245 
246 //
247 // Auto-disposal class wrappers.
248 //
249 // sbAutoPRProcessAttr Wrapper to auto-destroy process attributes.
250 // sbAutoPRFileDesc Wrapper to auto-close an NSPR file descriptor.
251 // sbAutoKillProcess Wrapper to auto-kill a Songbird process.
252 //
253 
254 SB_AUTO_NULL_CLASS(sbAutoPRProcessAttr,
255  PRProcessAttr*,
256  PR_DestroyProcessAttr(mValue));
257 SB_AUTO_NULL_CLASS(sbAutoPRFileDesc,
258  PRFileDesc*,
259  PR_Close(mValue));
260 SB_AUTO_NULL_CLASS(sbAutoKillProcess,
261  sbProcess*,
262  mValue->Kill());
263 
264 
265 //------------------------------------------------------------------------------
266 //
267 // Songbird process services.
268 //
269 //------------------------------------------------------------------------------
270 
288 nsresult SB_RunProcess(sbProcess** aProcess,
289  nsTArray<nsString>& aArgList,
290  const nsAString* aStdin = nsnull,
291  PRBool aPipeStdoutString = PR_FALSE,
292  PRMonitor* aDoneMonitor = nsnull);
293 
294 
295 #endif // __SB_PROCESS_H__
296 
nsresult GetStdoutString(nsAString &aStdoutString)
Definition: sbProcess.cpp:381
SB_AUTO_NULL_CLASS(sbAutoPRProcessAttr, PRProcessAttr *, PR_DestroyProcessAttr(mValue))
nsresult GetDoneMonitor(PRMonitor **aDoneMonitor)
Definition: sbProcess.cpp:435
virtual ~sbProcess()
Definition: sbProcess.cpp:269
nsresult GetExitCode(PRInt32 *aExitCode)
Definition: sbProcess.cpp:513
nsresult GetDoneResult(nsresult *aDoneResult)
Definition: sbProcess.cpp:493
nsresult Kill()
Definition: sbProcess.cpp:242
nsresult GetIsDone(PRBool *aIsDone)
Definition: sbProcess.cpp:473
nsresult SetDoneMonitor(PRMonitor *aDoneMonitor)
Definition: sbProcess.cpp:450
nsresult SetPipeStdoutString(PRBool aPipeStdoutString)
Definition: sbProcess.cpp:417
nsresult GetArgList(nsTArray< nsString > &aArgList)
Definition: sbProcess.cpp:307
nsresult GetPipeStdoutString(PRBool *aPipeStdoutString)
Definition: sbProcess.cpp:402
nsresult SB_RunProcess(sbProcess **aProcess, nsTArray< nsString > &aArgList, const nsAString *aStdin=nsnull, PRBool aPipeStdoutString=PR_FALSE, PRMonitor *aDoneMonitor=nsnull)
Definition: sbProcess.cpp:677
nsresult SetArgList(const nsTArray< nsString > &aArgList)
Definition: sbProcess.cpp:324
static NS_DECL_ISUPPORTS nsresult New(sbProcess **aProcess)
Definition: sbProcess.cpp:87
nsresult Run()
Definition: sbProcess.cpp:112
nsresult GetStdinString(nsAString &aStdinString)
Definition: sbProcess.cpp:346
nsresult SetStdinString(const nsAString &aStdinString)
Definition: sbProcess.cpp:362