210
edits
m (→Sample Usage) |
Jamesboston (talk | contribs) |
||
| Line 5: | Line 5: | ||
Although code will be written in standard XPCOM, the first draft proposal only describes how the API will look from javascript. | Although code will be written in standard XPCOM, the first draft proposal only describes how the API will look from javascript. | ||
=== | ===JS API=== | ||
<code> | <code> | ||
<pre> | <pre> | ||
// | Components.utils.import("resource://gre/Process.jsm"); | ||
// create | // Note: this only create the process object: the new process isn't actually | ||
var | // launched until .run() is called | ||
var p = new Process("c:\\myapp.exe"); | |||
// | // Set up stdin of the process | ||
p.setstdin(null); // the default value: inherit stdin from the current process | |||
// | // open the file and use that as stdin of the new process | ||
p.setstdin(new Process.InputFile("c:\\myinput.txt")); | |||
// | // set up a writable pipe to send data to the new process | ||
process. | // later, you can write to the pipe using stdin.write(data) | ||
// the writing is asynchronous... | |||
// if buffers are full, the write() call may throw an exception | |||
let stdin = new Process.WritePipe(); | |||
p.setstdin(stdin); | |||
// Set up stdout/stderr of the process | |||
p.setstdout(null); // the default value: inherit stdin from the current process | |||
// open the file and use that as stdout of the new process | |||
p.setstdout(new Process.OutputFile("c:\\myoutput.txt")); | |||
// capture output and processes it via a callback | |||
let stdout = new Process.ReadPipe(); | |||
p.setstdout(stdout); | |||
stdout.addEventListener("dataReceived", function(d) { | |||
alert("Got data from child process: " + d); | |||
}); | |||
// Add a callback to get the result code of running the process | |||
p.addCallback("processCompleted", function(r) { | |||
alert("Child process finished with result: " + r); | |||
}); | |||
// Launch the process | |||
p.run(["argument1", "argument2"]); | |||
</pre> | |||
</code> | |||
=== Underlying XPCOM interfaces === | |||
<code> | |||
<pre> | |||
interface nsIProcess2 : public nsISupports | |||
{ | |||
void init(in nsIFile program); | |||
void inheritStdin(); | |||
void pipeStdin(in nsIPOSIXOutputStream); | |||
void inheritStdout(); | |||
void fileStdout(in nsIFile); | |||
void pipeStdout(in nsIPOSIXInputStream); | |||
void inheritStderr(); | |||
void fileStderr(in nsIFile); | |||
void pipeStderr(in nsIPOSIXInputStream); | |||
readonly attribute unsigned long result; | |||
readonly attribute unsigned long pid; | |||
attribute nsIObserver listener; | |||
void run([array, size_is(argv)] in wstring argv, in unsigned long argc); | |||
}; | |||
interface nsIPOSIXOutputStream : public nsIOutputStream | |||
{ | |||
/* the native POSIX file descriptor for this stream */ | |||
readonly attribute unsigned long fd; | |||
}; | |||
interface nsIPOSIXInputStream : public nsIInputStream | |||
{ | |||
/* the native POSIX file descriptor for this stream */ | |||
readonly attribute unsigned long fd; | |||
}; | |||
interface nsIPipeManager : public nsISupports | |||
{ | |||
nsIPOSIXInputStream createIncomingPipe(); | |||
nsIPOSIXOutputStream createOutgoingPipe(); | |||
nsIPOSIXInputStream readFile(in nsILocalFile file); | |||
nsIPOSIXOutputStream writeFile(in nsILocalFile file); | |||
}; | |||
</pre> | </pre> | ||
</code> | </code> | ||
edits