NsIProcess: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
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.  


===Sample Usage===
===JS API===


<code>
<code>
<pre>
<pre>
// first argument is the executable
Components.utils.import("resource://gre/Process.jsm");
var arguments = ["argument1", "argument2"];


// create nsIProcess
// Note: this only create the process object: the new process isn't actually
var process = Components.classes["@mozilla.org/process/util;1"]
// launched until .run() is called
                        .createInstance(Components.interfaces.nsIProcess);
var p = new Process("c:\\myapp.exe");


// start the executable
// Set up stdin of the process
process.open(arguments, arguments.length)
p.setstdin(null);  // the default value: inherit stdin from the current process


// alternatively it could be opened with interprocess communication
// open the file and use that as stdin of the new process
process.open(arguments, arguments.length, stdin, stdout, stderr)
p.setstdin(new Process.InputFile("c:\\myinput.txt"));
// stdin - callback
// stdout - callback
// stderr - callback


// check if the executable is still running
// set up a writable pipe to send data to the new process
process.isRunning()
// 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);
};


// stop the process
process.close()
</pre>
</pre>
</code>
</code>

Revision as of 17:39, 3 April 2009

nsIProcess API Proposal

First draft API Proposal

Although code will be written in standard XPCOM, the first draft proposal only describes how the API will look from javascript.

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