Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | "use strict"; |
michael@0 | 5 | |
michael@0 | 6 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 7 | |
michael@0 | 8 | |
michael@0 | 9 | let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"] |
michael@0 | 10 | .getService(Ci.mozIJSSubScriptLoader); |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * Start a new RIL worker. |
michael@0 | 14 | * |
michael@0 | 15 | * @param custom_ns |
michael@0 | 16 | * Namespace with symbols to be injected into the new worker |
michael@0 | 17 | * namespace. |
michael@0 | 18 | * |
michael@0 | 19 | * @return an object that represents the worker's namespace. |
michael@0 | 20 | * |
michael@0 | 21 | * @note that this does not start an actual worker thread. The worker |
michael@0 | 22 | * is executed on the main thread, within a separate namespace object. |
michael@0 | 23 | */ |
michael@0 | 24 | function newWorker(custom_ns) { |
michael@0 | 25 | let worker_ns = { |
michael@0 | 26 | importScripts: function() { |
michael@0 | 27 | Array.slice(arguments).forEach(function(script) { |
michael@0 | 28 | if (!script.startsWith("resource:")) { |
michael@0 | 29 | script = "resource://gre/modules/" + script; |
michael@0 | 30 | } |
michael@0 | 31 | subscriptLoader.loadSubScript(script, this); |
michael@0 | 32 | }, this); |
michael@0 | 33 | }, |
michael@0 | 34 | |
michael@0 | 35 | postRILMessage: function(message) { |
michael@0 | 36 | }, |
michael@0 | 37 | |
michael@0 | 38 | postMessage: function(message) { |
michael@0 | 39 | }, |
michael@0 | 40 | |
michael@0 | 41 | // Define these variables inside the worker scope so ES5 strict mode |
michael@0 | 42 | // doesn't flip out. |
michael@0 | 43 | onmessage: undefined, |
michael@0 | 44 | onerror: undefined, |
michael@0 | 45 | |
michael@0 | 46 | DEBUG: true |
michael@0 | 47 | }; |
michael@0 | 48 | // The 'self' variable in a worker points to the worker's own namespace. |
michael@0 | 49 | worker_ns.self = worker_ns; |
michael@0 | 50 | |
michael@0 | 51 | // Copy the custom definitions over. |
michael@0 | 52 | for (let key in custom_ns) { |
michael@0 | 53 | worker_ns[key] = custom_ns[key]; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | // fake require() for toolkit/components/workerloader/require.js |
michael@0 | 57 | let require = (function() { |
michael@0 | 58 | return function require(script) { |
michael@0 | 59 | worker_ns.module = {}; |
michael@0 | 60 | worker_ns.importScripts(script); |
michael@0 | 61 | return worker_ns; |
michael@0 | 62 | } |
michael@0 | 63 | })(); |
michael@0 | 64 | |
michael@0 | 65 | Object.freeze(require); |
michael@0 | 66 | Object.defineProperty(worker_ns, "require", { |
michael@0 | 67 | value: require, |
michael@0 | 68 | enumerable: true, |
michael@0 | 69 | configurable: false |
michael@0 | 70 | }); |
michael@0 | 71 | |
michael@0 | 72 | // Load the RIL worker itself. |
michael@0 | 73 | worker_ns.importScripts("ril_worker.js"); |
michael@0 | 74 | |
michael@0 | 75 | // Register at least one client. |
michael@0 | 76 | worker_ns.ContextPool.registerClient({ clientId: 0 }); |
michael@0 | 77 | |
michael@0 | 78 | return worker_ns; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Create a parcel suitable for postRILMessage(). |
michael@0 | 83 | * |
michael@0 | 84 | * @param fakeParcelSize |
michael@0 | 85 | * Value to be written to parcel size field for testing |
michael@0 | 86 | * incorrect/incomplete parcel reading. Replaced with correct |
michael@0 | 87 | * one determined length of data if negative. |
michael@0 | 88 | * @param response |
michael@0 | 89 | * Response code of the incoming parcel. |
michael@0 | 90 | * @param request |
michael@0 | 91 | * Request code of the incoming parcel. |
michael@0 | 92 | * @param data |
michael@0 | 93 | * Extra data to be appended. |
michael@0 | 94 | * |
michael@0 | 95 | * @return an Uint8Array carrying all parcel data. |
michael@0 | 96 | */ |
michael@0 | 97 | function newIncomingParcel(fakeParcelSize, response, request, data) { |
michael@0 | 98 | const UINT32_SIZE = 4; |
michael@0 | 99 | const PARCEL_SIZE_SIZE = 4; |
michael@0 | 100 | |
michael@0 | 101 | let realParcelSize = data.length + 2 * UINT32_SIZE; |
michael@0 | 102 | let buffer = new ArrayBuffer(realParcelSize + PARCEL_SIZE_SIZE); |
michael@0 | 103 | let bytes = new Uint8Array(buffer); |
michael@0 | 104 | |
michael@0 | 105 | let writeIndex = 0; |
michael@0 | 106 | function writeUint8(value) { |
michael@0 | 107 | bytes[writeIndex] = value; |
michael@0 | 108 | ++writeIndex; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function writeInt32(value) { |
michael@0 | 112 | writeUint8(value & 0xff); |
michael@0 | 113 | writeUint8((value >> 8) & 0xff); |
michael@0 | 114 | writeUint8((value >> 16) & 0xff); |
michael@0 | 115 | writeUint8((value >> 24) & 0xff); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | function writeParcelSize(value) { |
michael@0 | 119 | writeUint8((value >> 24) & 0xff); |
michael@0 | 120 | writeUint8((value >> 16) & 0xff); |
michael@0 | 121 | writeUint8((value >> 8) & 0xff); |
michael@0 | 122 | writeUint8(value & 0xff); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | if (fakeParcelSize < 0) { |
michael@0 | 126 | fakeParcelSize = realParcelSize; |
michael@0 | 127 | } |
michael@0 | 128 | writeParcelSize(fakeParcelSize); |
michael@0 | 129 | |
michael@0 | 130 | writeInt32(response); |
michael@0 | 131 | writeInt32(request); |
michael@0 | 132 | |
michael@0 | 133 | // write parcel data |
michael@0 | 134 | for (let ii = 0; ii < data.length; ++ii) { |
michael@0 | 135 | writeUint8(data[ii]); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | return bytes; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * |
michael@0 | 143 | */ |
michael@0 | 144 | let ril_ns; |
michael@0 | 145 | function newRadioInterface() { |
michael@0 | 146 | if (!ril_ns) { |
michael@0 | 147 | ril_ns = {}; |
michael@0 | 148 | subscriptLoader.loadSubScript("resource://gre/components/RadioInterfaceLayer.js", ril_ns); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | return { |
michael@0 | 152 | __proto__: ril_ns.RadioInterface.prototype, |
michael@0 | 153 | }; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | /** |
michael@0 | 157 | * Test whether specified function throws exception with expected |
michael@0 | 158 | * result. |
michael@0 | 159 | * |
michael@0 | 160 | * @param func |
michael@0 | 161 | * Function to be tested. |
michael@0 | 162 | * @param message |
michael@0 | 163 | * Message of expected exception. <code>null</code> for no throws. |
michael@0 | 164 | * @param stack |
michael@0 | 165 | * Optional stack object to be printed. <code>null</code> for |
michael@0 | 166 | * Components#stack#caller. |
michael@0 | 167 | */ |
michael@0 | 168 | function do_check_throws(func, message, stack) |
michael@0 | 169 | { |
michael@0 | 170 | if (!stack) |
michael@0 | 171 | stack = Components.stack.caller; |
michael@0 | 172 | |
michael@0 | 173 | try { |
michael@0 | 174 | func(); |
michael@0 | 175 | } catch (exc) { |
michael@0 | 176 | if (exc.message === message) { |
michael@0 | 177 | return; |
michael@0 | 178 | } |
michael@0 | 179 | do_throw("expecting exception '" + message |
michael@0 | 180 | + "', caught '" + exc.message + "'", stack); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | if (message) { |
michael@0 | 184 | do_throw("expecting exception '" + message + "', none thrown", stack); |
michael@0 | 185 | } |
michael@0 | 186 | } |
michael@0 | 187 |