NsIProcess: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 9: Line 9:
Components.utils.import("resource://gre/Process.jsm");
Components.utils.import("resource://gre/Process.jsm");


// Note: this only create the process object: the new process isn't actually
function callback(evt) {
// launched until .run() is called
  // do stuff
var p = new Process("c:\\myapp.exe");
}


// Set up stdin of the process
var p = new Process(["appdir/myapp", "someargs],
p.setstdin(null); // the default value: inherit stdin from the current process
                    {cwd: "ProfD", charset: "utf8"},
                    callback);
p.start();


// open the file and use that as stdin of the new process
// or without callback
p.setstdin(new Process.InputFile("c:\\myinput.txt"));


// set up a writable pipe to send data to the new process
var p = new Process(["appdir/myapp", "someargs],  
// later, you can write to the pipe using stdin.write(data)
                    {cwd: "ProfD", charset: "utf8"});
// the writing is asynchronous...
p.start();
// 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
var ret = p.communicate("something");
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>
</pre>
</code>
</code>

Latest revision as of 17:29, 6 July 2009

nsIProcess2 API Proposal

Project page: Fixing nsIProcess

JS API

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

function callback(evt) {
   // do stuff
}

var p = new Process(["appdir/myapp", "someargs], 
                    {cwd: "ProfD", charset: "utf8"},
                    callback);
p.start();

// or without callback

var p = new Process(["appdir/myapp", "someargs], 
                    {cwd: "ProfD", charset: "utf8"});
p.start();

var ret = p.communicate("something");

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