1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is subprocess.jsm.
16
* The Initial Developer of this code is Patrick Brunschwig.
17
* Portions created by Patrick Brunschwig <patrick@enigmail.net>
18
* are Copyright (C) 2011 Patrick Brunschwig.
19
* All Rights Reserved.
22
* Jan Gerber <j@mailb.org>
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
35
* ***** END LICENSE BLOCK ***** */
38
* ChromeWorker Object subprocess.jsm on Windows to process stdin/stdout/stderr
39
* on separate threads.
43
// Being a ChromeWorker object, implicitly uses the following:
44
// Components.utils.import("resource://gre/modules/ctypes.jsm");
48
const BufferSize = 1024;
50
const BOOL = ctypes.bool;
51
const HANDLE = ctypes.size_t;
52
const DWORD = ctypes.uint32_t;
53
const LPDWORD = DWORD.ptr;
54
const PVOID = ctypes.voidptr_t;
58
typedef struct _OVERLAPPED {
60
ULONG_PTR InternalHigh;
69
} OVERLAPPED, *LPOVERLAPPED;
71
const OVERLAPPED = new ctypes.StructType("OVERLAPPED");
73
var ReadFileBuffer = ctypes.char.array(BufferSize);
74
var WriteFileBuffer = ctypes.uint8_t.array(BufferSize);
76
var kernel32dll = null;
79
function initLib(libName) {
80
if (ctypes.size_t.size == 8) {
81
var WinABI = ctypes.default_abi;
83
var WinABI = ctypes.winapi_abi;
86
kernel32dll = ctypes.open(libName);
89
BOOL WINAPI WriteFile(
91
__in LPCVOID lpBuffer,
92
__in DWORD nNumberOfBytesToWrite,
93
__out_opt LPDWORD lpNumberOfBytesWritten,
94
__inout_opt LPOVERLAPPED lpOverlapped
97
NOTE: lpBuffer is declared as array of unsigned int8 instead of char to avoid
98
implicit charset conversion
100
libFunc.WriteFile = kernel32dll.declare("WriteFile",
111
BOOL WINAPI ReadFile(
113
__out LPVOID ReadFileBuffer,
114
__in DWORD nNumberOfBytesToRead,
115
__out_opt LPDWORD lpNumberOfBytesRead,
116
__inout_opt LPOVERLAPPED lpOverlapped
119
libFunc.ReadFile = kernel32dll.declare("ReadFile",
130
BOOL WINAPI CloseHandle(
134
libFunc.CloseHandle = kernel32dll.declare("CloseHandle",
141
// Windows error codes
142
const ERROR_HANDLE_EOF = 38;
143
const ERROR_BROKEN_PIPE = 109;
145
function writePipe(pipe, data) {
146
var bytesWritten = DWORD(0);
148
var pData = new WriteFileBuffer();
150
var numChunks = Math.floor(data.length / BufferSize);
151
for (var chunk = 0; chunk <= numChunks; chunk ++) {
152
var numBytes = chunk < numChunks ? BufferSize : data.length - chunk * BufferSize;
153
for (var i=0; i < numBytes; i++) {
154
pData[i] = data.charCodeAt(chunk * BufferSize + i) % 256;
157
var r = libFunc.WriteFile(pipe, pData, numBytes, bytesWritten.address(), null);
158
if (bytesWritten.value != numBytes)
159
throw("error: wrote "+bytesWritten.value+" instead of "+numBytes+" bytes");
161
postMessage("wrote "+data.length+" bytes of data");
164
function readString(data, length, charset) {
166
for(var i = 0; i < length; i++) {
167
if(data[i] == 0 && charset != "null") // stop on NULL character for non-binary data
170
r += String.fromCharCode(data[i]);
177
function readPipe(pipe, charset, bufferedOutput) {
180
var bytesRead = DWORD(0);
181
var line = new ReadFileBuffer();
182
var r = libFunc.ReadFile(pipe, line, BufferSize, bytesRead.address(), null);
185
// stop if we get an error (such as EOF reached)
186
let lastErr = ctypes.winLastError;
188
case ERROR_HANDLE_EOF:
189
case ERROR_BROKEN_PIPE:
190
postMessage({msg: "info", data: "EOF reached"});
193
postMessage({msg: "error", data: "Windows error " + lastErr});
198
if (bytesRead.value > 0) {
199
var c = readString(line, bytesRead.value, charset);
201
postMessage({msg: "data", data: c, count: c.length});
211
postMessage({msg: "data", data: dataStr, count: dataStr.length});
213
libFunc.CloseHandle(pipe);
214
postMessage({msg: "done"});
219
onmessage = function (event) {
221
switch (event.data.msg) {
223
initLib(event.data.libc);
224
postMessage("InitOK");
229
// data: the data (string) to write
231
pipePtr = HANDLE.ptr(event.data.pipe);
232
writePipe(pipePtr.contents, event.data.data);
233
postMessage("WriteOK");
236
initLib(event.data.libc);
237
pipePtr = HANDLE.ptr(event.data.pipe);
238
readPipe(pipePtr.contents, event.data.charset, event.data.bufferedOutput);
241
pipePtr = HANDLE.ptr(event.data.pipe);
242
postMessage("closing stdin\n");
244
if (libFunc.CloseHandle(pipePtr.contents)) {
245
postMessage("ClosedOK");
248
postMessage("Could not close stdin handle");
255
throw("error: Unknown command"+event.data.msg+"\n");