Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # subprocess |
michael@0 | 2 | |
michael@0 | 3 | |
michael@0 | 4 | Created by [Jan Gerber](j@mailb.org), with contributions from [Patrick Brunschwig](patrick@enigmail.net), subprocess.jsm is used as the underlying library, supporting the Addon-SDK's implementation of Node's `child-process` API. |
michael@0 | 5 | |
michael@0 | 6 | `subprocess.jsm` is originally from [http://hg.mozilla.org/ipccode/](http://hg.mozilla.org/ipccode/). |
michael@0 | 7 | |
michael@0 | 8 | How to Use subprocess.jsm in your Add-on |
michael@0 | 9 | ---------------------------------------- |
michael@0 | 10 | |
michael@0 | 11 | 1. copy subprocess.jsm and subprocess_worker_*.js into the modules/ directory |
michael@0 | 12 | of your add-on. |
michael@0 | 13 | |
michael@0 | 14 | 2. add this line to chrome.manifest: |
michael@0 | 15 | resource EXTENSION modules/ |
michael@0 | 16 | |
michael@0 | 17 | 3. import it where needed: |
michael@0 | 18 | Components.utils.import("resource://EXTENSION/subprocess.jsm"); |
michael@0 | 19 | |
michael@0 | 20 | This object allows to start a process, and read/write data to/from it |
michael@0 | 21 | using stdin/stdout/stderr streams. |
michael@0 | 22 | |
michael@0 | 23 | Usage example: |
michael@0 | 24 | |
michael@0 | 25 | var p = subprocess.call({ |
michael@0 | 26 | command: '/bin/foo', |
michael@0 | 27 | arguments: ['-v', 'foo'], |
michael@0 | 28 | environment: [ "XYZ=abc", "MYVAR=def" ], |
michael@0 | 29 | charset: 'UTF-8', |
michael@0 | 30 | workdir: '/home/foo', |
michael@0 | 31 | //stdin: "some value to write to stdin\nfoobar", |
michael@0 | 32 | stdin: function(stdin) { |
michael@0 | 33 | stdin.write("some value to write to stdin\nfoobar"); |
michael@0 | 34 | stdin.close(); |
michael@0 | 35 | }, |
michael@0 | 36 | stdout: function(data) { |
michael@0 | 37 | dump("got data on stdout:" + data + "\n"); |
michael@0 | 38 | }, |
michael@0 | 39 | stderr: function(data) { |
michael@0 | 40 | dump("got data on stderr:" + data + "\n"); |
michael@0 | 41 | }, |
michael@0 | 42 | done: function(result) { |
michael@0 | 43 | dump("process terminated with " + result.exitCode + "\n"); |
michael@0 | 44 | }, |
michael@0 | 45 | mergeStderr: false |
michael@0 | 46 | }); |
michael@0 | 47 | |
michael@0 | 48 | p.wait(); // wait for the subprocess to terminate, |
michael@0 | 49 | // this will block the main thread, |
michael@0 | 50 | // only do if you can wait that long |
michael@0 | 51 | |
michael@0 | 52 | |
michael@0 | 53 | Description of subprocess.call(...) Parameters |
michael@0 | 54 | ---------------------------------------------- |
michael@0 | 55 | Apart from <command>, all arguments are optional. |
michael@0 | 56 | |
michael@0 | 57 | command: either a |nsIFile| object pointing to an executable file or a |
michael@0 | 58 | String containing the platform-dependent path to an executable |
michael@0 | 59 | file. |
michael@0 | 60 | |
michael@0 | 61 | arguments: optional string array containing the arguments to the command. |
michael@0 | 62 | |
michael@0 | 63 | environment: optional string array containing environment variables to pass |
michael@0 | 64 | to the command. The array elements must have the form |
michael@0 | 65 | "VAR=data". Please note that if environment is defined, it |
michael@0 | 66 | replaces any existing environment variables for the subprocess. |
michael@0 | 67 | |
michael@0 | 68 | charset: Output is decoded with given charset and a string is returned. |
michael@0 | 69 | If charset is undefined, "UTF-8" is used as default. |
michael@0 | 70 | To get binary data, set this to null and the returned string |
michael@0 | 71 | is not decoded in any way. |
michael@0 | 72 | |
michael@0 | 73 | workdir: Optional; either a |nsIFile| object or string containing the |
michael@0 | 74 | platform-dependent path to a directory to become the current |
michael@0 | 75 | working directory of the subprocess. |
michael@0 | 76 | |
michael@0 | 77 | stdin: Optional input data for the process to be passed on standard |
michael@0 | 78 | input. stdin can either be a string or a function. |
michael@0 | 79 | A |string| gets written to stdin and stdin gets closed; |
michael@0 | 80 | A |function| gets passed an object with write and close function. |
michael@0 | 81 | Please note that the write() function will return almost immediately; |
michael@0 | 82 | data is always written asynchronously on a separate thread. |
michael@0 | 83 | |
michael@0 | 84 | stdout: An optional function that can receive output data from the |
michael@0 | 85 | process. The stdout-function is called asynchronously; it can be |
michael@0 | 86 | called mutliple times during the execution of a process. |
michael@0 | 87 | At a minimum at each occurance of \n or \r. |
michael@0 | 88 | Please note that null-characters might need to be escaped |
michael@0 | 89 | with something like 'data.replace(/\0/g, "\\0");'. |
michael@0 | 90 | |
michael@0 | 91 | stderr: An optional function that can receive stderr data from the |
michael@0 | 92 | process. The stderr-function is called asynchronously; it can be |
michael@0 | 93 | called mutliple times during the execution of a process. Please |
michael@0 | 94 | note that null-characters might need to be escaped with |
michael@0 | 95 | something like 'data.replace(/\0/g, "\\0");'. |
michael@0 | 96 | (on windows it only gets called once right now) |
michael@0 | 97 | |
michael@0 | 98 | done: Optional function that is called when the process has terminated. |
michael@0 | 99 | The exit code from the process available via result.exitCode. If |
michael@0 | 100 | stdout is not defined, then the output from stdout is available |
michael@0 | 101 | via result.stdout. stderr data is in result.stderr |
michael@0 | 102 | |
michael@0 | 103 | mergeStderr: Optional boolean value. If true, stderr is merged with stdout; |
michael@0 | 104 | no data will be provided to stderr. |
michael@0 | 105 | |
michael@0 | 106 | |
michael@0 | 107 | Description of object returned by subprocess.call(...) |
michael@0 | 108 | ------------------------------------------------------ |
michael@0 | 109 | The object returned by subprocess.call offers a few methods that can be |
michael@0 | 110 | executed: |
michael@0 | 111 | |
michael@0 | 112 | wait(): waits for the subprocess to terminate. It is not required to use |
michael@0 | 113 | wait; done will be called in any case when the subprocess terminated. |
michael@0 | 114 | |
michael@0 | 115 | kill(): kill the subprocess. Any open pipes will be closed and |
michael@0 | 116 | done will be called. |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | Other methods exported by subprocess |
michael@0 | 120 | ------------------------------------ |
michael@0 | 121 | The following functions help debugging and provide logging facilities. |
michael@0 | 122 | |
michael@0 | 123 | registerDebugHandler(functionRef): register a handler that is called to get |
michael@0 | 124 | debugging information |
michael@0 | 125 | registerLogHandler(functionRef): register a handler that is called to get error |
michael@0 | 126 | messages |
michael@0 | 127 | |
michael@0 | 128 | example: |
michael@0 | 129 | subprocess.registerLogHandler( function(s) { dump(s); } ); |