NsIProcess

From MozillaWiki
Revision as of 17:41, 3 April 2009 by Jamesboston (talk | contribs)
Jump to navigation Jump to search

nsIProcess2 API Proposal

JS API

Components.utils.import("resource://gre/Process.jsm");

// Note: this only create the process object: the new process isn't actually
// 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
// 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"]);

Underlying XPCOM interfaces

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);
};

Reference

Project page: Fixing nsIProcess