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 | /* 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 file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // TODO Bug 907060 Per off-line discussion, after the MessagePort is done |
michael@0 | 6 | // at Bug 643325, we will start to refactorize the common logic of both |
michael@0 | 7 | // Inter-App Communication and Shared Worker. For now, we hope to design an |
michael@0 | 8 | // MozInterAppMessagePort to meet the timeline, which still follows exactly |
michael@0 | 9 | // the same interface and semantic as the MessagePort is. In the future, |
michael@0 | 10 | // we can then align it back to MessagePort with backward compatibility. |
michael@0 | 11 | |
michael@0 | 12 | "use strict"; |
michael@0 | 13 | |
michael@0 | 14 | const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
michael@0 | 15 | |
michael@0 | 16 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 17 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 18 | Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); |
michael@0 | 19 | |
michael@0 | 20 | const DEBUG = false; |
michael@0 | 21 | function debug(aMsg) { |
michael@0 | 22 | dump("-- InterAppMessagePort: " + Date.now() + ": " + aMsg + "\n"); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | XPCOMUtils.defineLazyServiceGetter(this, "cpmm", |
michael@0 | 26 | "@mozilla.org/childprocessmessagemanager;1", |
michael@0 | 27 | "nsIMessageSender"); |
michael@0 | 28 | |
michael@0 | 29 | XPCOMUtils.defineLazyServiceGetter(this, "appsService", |
michael@0 | 30 | "@mozilla.org/AppsService;1", |
michael@0 | 31 | "nsIAppsService"); |
michael@0 | 32 | |
michael@0 | 33 | const kMessages = ["InterAppMessagePort:OnMessage"]; |
michael@0 | 34 | |
michael@0 | 35 | function InterAppMessagePort() { |
michael@0 | 36 | if (DEBUG) debug("InterAppMessagePort()"); |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | InterAppMessagePort.prototype = { |
michael@0 | 40 | __proto__: DOMRequestIpcHelper.prototype, |
michael@0 | 41 | |
michael@0 | 42 | classDescription: "MozInterAppMessagePort", |
michael@0 | 43 | |
michael@0 | 44 | classID: Components.ID("{c66e0f8c-e3cb-11e2-9e85-43ef6244b884}"), |
michael@0 | 45 | |
michael@0 | 46 | contractID: "@mozilla.org/dom/inter-app-message-port;1", |
michael@0 | 47 | |
michael@0 | 48 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, |
michael@0 | 49 | Ci.nsISupportsWeakReference, |
michael@0 | 50 | Ci.nsIObserver]), |
michael@0 | 51 | |
michael@0 | 52 | // Ci.nsIDOMGlobalPropertyInitializer implementation. |
michael@0 | 53 | init: function(aWindow) { |
michael@0 | 54 | if (DEBUG) debug("Calling init()."); |
michael@0 | 55 | |
michael@0 | 56 | this.initDOMRequestHelper(aWindow, kMessages); |
michael@0 | 57 | |
michael@0 | 58 | let principal = aWindow.document.nodePrincipal; |
michael@0 | 59 | this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); |
michael@0 | 60 | this._pageURL = principal.URI.specIgnoringRef; |
michael@0 | 61 | |
michael@0 | 62 | // Remove query string. |
michael@0 | 63 | this._pageURL = this._pageURL.split("?")[0]; |
michael@0 | 64 | |
michael@0 | 65 | this._started = false; |
michael@0 | 66 | this._closed = false; |
michael@0 | 67 | this._messageQueue = []; |
michael@0 | 68 | }, |
michael@0 | 69 | |
michael@0 | 70 | // WebIDL implementation for constructor. |
michael@0 | 71 | __init: function(aMessagePortID) { |
michael@0 | 72 | if (DEBUG) { |
michael@0 | 73 | debug("Calling __init(): aMessagePortID: " + aMessagePortID); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | this._messagePortID = aMessagePortID; |
michael@0 | 77 | |
michael@0 | 78 | cpmm.sendAsyncMessage("InterAppMessagePort:Register", |
michael@0 | 79 | { messagePortID: this._messagePortID, |
michael@0 | 80 | manifestURL: this._manifestURL, |
michael@0 | 81 | pageURL: this._pageURL }); |
michael@0 | 82 | }, |
michael@0 | 83 | |
michael@0 | 84 | // DOMRequestIpcHelper implementation. |
michael@0 | 85 | uninit: function() { |
michael@0 | 86 | if (DEBUG) debug("Calling uninit()."); |
michael@0 | 87 | |
michael@0 | 88 | // When the message port is uninitialized, we need to disentangle the |
michael@0 | 89 | // coupling ports, as if the close() method had been called. |
michael@0 | 90 | if (this._closed) { |
michael@0 | 91 | if (DEBUG) debug("close() has been called. Don't need to close again."); |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | this.close(); |
michael@0 | 96 | }, |
michael@0 | 97 | |
michael@0 | 98 | postMessage: function(aMessage) { |
michael@0 | 99 | if (DEBUG) debug("Calling postMessage()."); |
michael@0 | 100 | |
michael@0 | 101 | if (this._closed) { |
michael@0 | 102 | if (DEBUG) debug("close() has been called. Cannot post message."); |
michael@0 | 103 | return; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | cpmm.sendAsyncMessage("InterAppMessagePort:PostMessage", |
michael@0 | 107 | { messagePortID: this._messagePortID, |
michael@0 | 108 | manifestURL: this._manifestURL, |
michael@0 | 109 | message: aMessage }); |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | start: function() { |
michael@0 | 113 | // Begin dispatching messages received on the port. |
michael@0 | 114 | if (DEBUG) debug("Calling start()."); |
michael@0 | 115 | |
michael@0 | 116 | if (this._closed) { |
michael@0 | 117 | if (DEBUG) debug("close() has been called. Cannot call start()."); |
michael@0 | 118 | return; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | if (this._started) { |
michael@0 | 122 | if (DEBUG) debug("start() has been called. Don't need to start again."); |
michael@0 | 123 | return; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | // When a port's port message queue is enabled, the event loop must use it |
michael@0 | 127 | // as one of its task sources. |
michael@0 | 128 | this._started = true; |
michael@0 | 129 | while (this._messageQueue.length) { |
michael@0 | 130 | let message = this._messageQueue.shift(); |
michael@0 | 131 | this._dispatchMessage(message); |
michael@0 | 132 | } |
michael@0 | 133 | }, |
michael@0 | 134 | |
michael@0 | 135 | close: function() { |
michael@0 | 136 | // Disconnecting the port, so that it is no longer active. |
michael@0 | 137 | if (DEBUG) debug("Calling close()."); |
michael@0 | 138 | |
michael@0 | 139 | if (this._closed) { |
michael@0 | 140 | if (DEBUG) debug("close() has been called. Don't need to close again."); |
michael@0 | 141 | return; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | this._closed = true; |
michael@0 | 145 | this._messageQueue.length = 0; |
michael@0 | 146 | |
michael@0 | 147 | // When this method called on a local port that is entangled with another |
michael@0 | 148 | // port, must cause the user agent to disentangle the coupling ports. |
michael@0 | 149 | cpmm.sendAsyncMessage("InterAppMessagePort:Unregister", |
michael@0 | 150 | { messagePortID: this._messagePortID, |
michael@0 | 151 | manifestURL: this._manifestURL }); |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | get onmessage() { |
michael@0 | 155 | if (DEBUG) debug("Getting onmessage handler."); |
michael@0 | 156 | |
michael@0 | 157 | return this.__DOM_IMPL__.getEventHandler("onmessage"); |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | set onmessage(aHandler) { |
michael@0 | 161 | if (DEBUG) debug("Setting onmessage handler."); |
michael@0 | 162 | |
michael@0 | 163 | this.__DOM_IMPL__.setEventHandler("onmessage", aHandler); |
michael@0 | 164 | |
michael@0 | 165 | // The first time a MessagePort object's onmessage IDL attribute is set, |
michael@0 | 166 | // the port's message queue must be enabled, as if the start() method had |
michael@0 | 167 | // been called. |
michael@0 | 168 | if (this._started) { |
michael@0 | 169 | if (DEBUG) debug("start() has been called. Don't need to start again."); |
michael@0 | 170 | return; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | this.start(); |
michael@0 | 174 | }, |
michael@0 | 175 | |
michael@0 | 176 | _dispatchMessage: function _dispatchMessage(aMessage) { |
michael@0 | 177 | let wrappedMessage = Cu.cloneInto(aMessage, this._window); |
michael@0 | 178 | if (DEBUG) { |
michael@0 | 179 | debug("_dispatchMessage: wrappedMessage: " + |
michael@0 | 180 | JSON.stringify(wrappedMessage)); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | let event = new this._window |
michael@0 | 184 | .MozInterAppMessageEvent("message", |
michael@0 | 185 | { data: wrappedMessage }); |
michael@0 | 186 | this.__DOM_IMPL__.dispatchEvent(event); |
michael@0 | 187 | }, |
michael@0 | 188 | |
michael@0 | 189 | receiveMessage: function(aMessage) { |
michael@0 | 190 | if (DEBUG) debug("receiveMessage: name: " + aMessage.name); |
michael@0 | 191 | |
michael@0 | 192 | let message = aMessage.json; |
michael@0 | 193 | if (message.manifestURL != this._manifestURL || |
michael@0 | 194 | message.pageURL != this._pageURL || |
michael@0 | 195 | message.messagePortID != this._messagePortID) { |
michael@0 | 196 | if (DEBUG) debug("The message doesn't belong to this page. Returning."); |
michael@0 | 197 | return; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | switch (aMessage.name) { |
michael@0 | 201 | case "InterAppMessagePort:OnMessage": |
michael@0 | 202 | if (this._closed) { |
michael@0 | 203 | if (DEBUG) debug("close() has been called. Drop the message."); |
michael@0 | 204 | return; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | if (!this._started) { |
michael@0 | 208 | if (DEBUG) debug("Not yet called start(). Queue up the message."); |
michael@0 | 209 | this._messageQueue.push(message.message); |
michael@0 | 210 | return; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | this._dispatchMessage(message.message); |
michael@0 | 214 | break; |
michael@0 | 215 | |
michael@0 | 216 | default: |
michael@0 | 217 | if (DEBUG) debug("Error! Shouldn't fall into this case."); |
michael@0 | 218 | break; |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | }; |
michael@0 | 222 | |
michael@0 | 223 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([InterAppMessagePort]); |
michael@0 | 224 |