toolkit/components/social/MessagePortWorker.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Note: this is never instantiated in chrome - the source is sent across
michael@0 6 // to the worker and it is evaluated there and created in response to a
michael@0 7 // port-create message we send.
michael@0 8 function WorkerPort(portid) {
michael@0 9 AbstractPort.call(this, portid);
michael@0 10 }
michael@0 11
michael@0 12 WorkerPort.prototype = {
michael@0 13 __proto__: AbstractPort.prototype,
michael@0 14 _portType: "worker",
michael@0 15
michael@0 16 _dopost: function fw_WorkerPort_dopost(data) {
michael@0 17 // postMessage is injected into the sandbox.
michael@0 18 _postMessage(data, "*");
michael@0 19 },
michael@0 20
michael@0 21 _onerror: function fw_WorkerPort_onerror(err) {
michael@0 22 // We throw an object that "derives" from the exception, but with
michael@0 23 // a more detailed message.
michael@0 24 throw {message: "Port " + this + " handler failed: " + err.message, __proto__: err};
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 function importScripts() {
michael@0 29 for (var i=0; i < arguments.length; i++) {
michael@0 30 // load the url *synchronously*
michael@0 31 var scriptURL = arguments[i];
michael@0 32 var xhr = new XMLHttpRequest();
michael@0 33 xhr.open('GET', scriptURL, false);
michael@0 34 xhr.onreadystatechange = function(aEvt) {
michael@0 35 if (xhr.readyState == 4) {
michael@0 36 if (xhr.status == 200 || xhr.status == 0) {
michael@0 37 _evalInSandbox(xhr.responseText, scriptURL);
michael@0 38 }
michael@0 39 else {
michael@0 40 throw new Error("Unable to importScripts ["+scriptURL+"], status " + xhr.status)
michael@0 41 }
michael@0 42 }
michael@0 43 };
michael@0 44 xhr.send(null);
michael@0 45 }
michael@0 46 }
michael@0 47
michael@0 48 // This function is magically injected into the sandbox and used there.
michael@0 49 // Thus, it is only ever dealing with "worker" ports.
michael@0 50 function __initWorkerMessageHandler() {
michael@0 51
michael@0 52 let ports = {}; // all "worker" ports currently alive, keyed by ID.
michael@0 53
michael@0 54 function messageHandler(event) {
michael@0 55 // We will ignore all messages destined for otherType.
michael@0 56 let data = event.data;
michael@0 57 let portid = data.portId;
michael@0 58 let port;
michael@0 59 if (!data.portFromType || data.portFromType === "worker") {
michael@0 60 // this is a message posted by ourself so ignore it.
michael@0 61 return;
michael@0 62 }
michael@0 63 switch (data.portTopic) {
michael@0 64 case "port-create":
michael@0 65 // a new port was created on the "client" side - create a new worker
michael@0 66 // port and store it in the map
michael@0 67 port = new WorkerPort(portid);
michael@0 68 ports[portid] = port;
michael@0 69 // and call the "onconnect" handler.
michael@0 70 try {
michael@0 71 onconnect({ports: [port]});
michael@0 72 } catch(e) {
michael@0 73 // we have a bad worker and cannot continue, we need to signal
michael@0 74 // an error
michael@0 75 port._postControlMessage("port-connection-error", JSON.stringify(e.toString()));
michael@0 76 throw e;
michael@0 77 }
michael@0 78 break;
michael@0 79
michael@0 80 case "port-close":
michael@0 81 // the client side of the port was closed, so close this side too.
michael@0 82 port = ports[portid];
michael@0 83 if (!port) {
michael@0 84 // port already closed (which will happen when we call port.close()
michael@0 85 // below - the client side will send us this message but we've
michael@0 86 // already closed it.)
michael@0 87 return;
michael@0 88 }
michael@0 89 delete ports[portid];
michael@0 90 port.close();
michael@0 91 break;
michael@0 92
michael@0 93 case "port-message":
michael@0 94 // the client posted a message to this worker port.
michael@0 95 port = ports[portid];
michael@0 96 if (!port) {
michael@0 97 // port must be closed - this shouldn't happen!
michael@0 98 return;
michael@0 99 }
michael@0 100 port._onmessage(data.data);
michael@0 101 break;
michael@0 102
michael@0 103 default:
michael@0 104 break;
michael@0 105 }
michael@0 106 }
michael@0 107 // addEventListener is injected into the sandbox.
michael@0 108 _addEventListener('message', messageHandler);
michael@0 109 }
michael@0 110 __initWorkerMessageHandler();

mercurial