2013-07-30 14:03:06 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-08-16 09:50:37 +00:00
|
|
|
// TODO Bug 907060 Per off-line discussion, after the MessagePort is done
|
|
|
|
// at Bug 643325, we will start to refactorize the common logic of both
|
|
|
|
// Inter-App Communication and Shared Worker. For now, we hope to design an
|
|
|
|
// MozInterAppMessagePort to meet the timeline, which still follows exactly
|
|
|
|
// the same interface and semantic as the MessagePort is. In the future,
|
|
|
|
// we can then align it back to MessagePort with backward compatibility.
|
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-08-16 09:50:37 +00:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
2013-07-30 14:03:06 +00:00
|
|
|
|
2013-09-12 13:01:00 +00:00
|
|
|
const DEBUG = false;
|
2013-07-30 14:03:06 +00:00
|
|
|
function debug(aMsg) {
|
2013-09-12 13:01:00 +00:00
|
|
|
dump("-- InterAppMessagePort: " + Date.now() + ": " + aMsg + "\n");
|
2013-07-30 14:03:06 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 09:50:37 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsIMessageSender");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
|
|
|
|
2014-08-11 17:37:09 +00:00
|
|
|
const kMessages = ["InterAppMessagePort:OnMessage",
|
|
|
|
"InterAppMessagePort:Shutdown"];
|
2013-08-16 09:50:37 +00:00
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
function InterAppMessagePort() {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("InterAppMessagePort()");
|
2013-07-30 14:03:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
InterAppMessagePort.prototype = {
|
2013-08-16 09:50:37 +00:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
classDescription: "MozInterAppMessagePort",
|
|
|
|
|
|
|
|
classID: Components.ID("{c66e0f8c-e3cb-11e2-9e85-43ef6244b884}"),
|
|
|
|
|
|
|
|
contractID: "@mozilla.org/dom/inter-app-message-port;1",
|
|
|
|
|
2013-08-16 09:50:37 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
|
2013-11-20 05:33:10 +00:00
|
|
|
Ci.nsISupportsWeakReference,
|
|
|
|
Ci.nsIObserver]),
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
// Ci.nsIDOMGlobalPropertyInitializer implementation.
|
|
|
|
init: function(aWindow) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Calling init().");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
this.initDOMRequestHelper(aWindow, kMessages);
|
|
|
|
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
|
2014-01-06 20:25:28 +00:00
|
|
|
this._pageURL = principal.URI.specIgnoringRef;
|
|
|
|
|
|
|
|
// Remove query string.
|
|
|
|
this._pageURL = this._pageURL.split("?")[0];
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
this._started = false;
|
|
|
|
this._closed = false;
|
|
|
|
this._messageQueue = [];
|
|
|
|
},
|
2013-07-30 14:03:06 +00:00
|
|
|
|
2013-08-16 09:49:15 +00:00
|
|
|
// WebIDL implementation for constructor.
|
2013-09-12 14:14:48 +00:00
|
|
|
__init: function(aMessagePortID) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) {
|
2013-09-12 14:14:48 +00:00
|
|
|
debug("Calling __init(): aMessagePortID: " + aMessagePortID);
|
2013-09-12 13:01:00 +00:00
|
|
|
}
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
this._messagePortID = aMessagePortID;
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("InterAppMessagePort:Register",
|
|
|
|
{ messagePortID: this._messagePortID,
|
|
|
|
manifestURL: this._manifestURL,
|
|
|
|
pageURL: this._pageURL });
|
|
|
|
},
|
|
|
|
|
|
|
|
// DOMRequestIpcHelper implementation.
|
|
|
|
uninit: function() {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Calling uninit().");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
// When the message port is uninitialized, we need to disentangle the
|
|
|
|
// coupling ports, as if the close() method had been called.
|
|
|
|
if (this._closed) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("close() has been called. Don't need to close again.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.close();
|
2013-08-16 09:49:15 +00:00
|
|
|
},
|
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
postMessage: function(aMessage) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Calling postMessage().");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
if (this._closed) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("close() has been called. Cannot post message.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("InterAppMessagePort:PostMessage",
|
|
|
|
{ messagePortID: this._messagePortID,
|
|
|
|
manifestURL: this._manifestURL,
|
|
|
|
message: aMessage });
|
2013-07-30 14:03:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
start: function() {
|
2013-08-16 09:50:37 +00:00
|
|
|
// Begin dispatching messages received on the port.
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Calling start().");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
if (this._closed) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("close() has been called. Cannot call start().");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._started) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("start() has been called. Don't need to start again.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a port's port message queue is enabled, the event loop must use it
|
|
|
|
// as one of its task sources.
|
|
|
|
this._started = true;
|
|
|
|
while (this._messageQueue.length) {
|
|
|
|
let message = this._messageQueue.shift();
|
|
|
|
this._dispatchMessage(message);
|
|
|
|
}
|
2013-07-30 14:03:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
2013-08-16 09:50:37 +00:00
|
|
|
// Disconnecting the port, so that it is no longer active.
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Calling close().");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
if (this._closed) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("close() has been called. Don't need to close again.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._closed = true;
|
|
|
|
this._messageQueue.length = 0;
|
|
|
|
|
|
|
|
// When this method called on a local port that is entangled with another
|
|
|
|
// port, must cause the user agent to disentangle the coupling ports.
|
|
|
|
cpmm.sendAsyncMessage("InterAppMessagePort:Unregister",
|
|
|
|
{ messagePortID: this._messagePortID,
|
|
|
|
manifestURL: this._manifestURL });
|
2014-08-11 17:37:09 +00:00
|
|
|
|
|
|
|
this.removeMessageListeners(kMessages);
|
2013-07-30 14:03:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get onmessage() {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Getting onmessage handler.");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onmessage");
|
|
|
|
},
|
|
|
|
|
|
|
|
set onmessage(aHandler) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Setting onmessage handler.");
|
2013-08-16 09:50:37 +00:00
|
|
|
|
2013-07-30 14:03:06 +00:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onmessage", aHandler);
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
// The first time a MessagePort object's onmessage IDL attribute is set,
|
|
|
|
// the port's message queue must be enabled, as if the start() method had
|
|
|
|
// been called.
|
|
|
|
if (this._started) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("start() has been called. Don't need to start again.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.start();
|
|
|
|
},
|
|
|
|
|
|
|
|
_dispatchMessage: function _dispatchMessage(aMessage) {
|
2014-02-01 19:06:59 +00:00
|
|
|
let wrappedMessage = Cu.cloneInto(aMessage, this._window);
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("_dispatchMessage: wrappedMessage: " +
|
|
|
|
JSON.stringify(wrappedMessage));
|
|
|
|
}
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
let event = new this._window
|
|
|
|
.MozInterAppMessageEvent("message",
|
|
|
|
{ data: wrappedMessage });
|
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("receiveMessage: name: " + aMessage.name);
|
2013-08-16 09:50:37 +00:00
|
|
|
|
|
|
|
let message = aMessage.json;
|
|
|
|
if (message.manifestURL != this._manifestURL ||
|
|
|
|
message.pageURL != this._pageURL ||
|
|
|
|
message.messagePortID != this._messagePortID) {
|
2014-08-11 17:37:09 +00:00
|
|
|
if (DEBUG) debug("The message doesn't belong to this page. Returning. " +
|
|
|
|
uneval(message));
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case "InterAppMessagePort:OnMessage":
|
|
|
|
if (this._closed) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("close() has been called. Drop the message.");
|
2013-08-16 09:50:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._started) {
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Not yet called start(). Queue up the message.");
|
2013-08-16 09:50:37 +00:00
|
|
|
this._messageQueue.push(message.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._dispatchMessage(message.message);
|
|
|
|
break;
|
|
|
|
|
2014-08-11 17:37:09 +00:00
|
|
|
case "InterAppMessagePort:Shutdown":
|
|
|
|
this.close();
|
|
|
|
break;
|
2013-08-16 09:50:37 +00:00
|
|
|
default:
|
2013-09-12 13:01:00 +00:00
|
|
|
if (DEBUG) debug("Error! Shouldn't fall into this case.");
|
2013-08-16 09:50:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-30 14:03:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-27 10:50:05 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([InterAppMessagePort]);
|
2013-07-30 14:03:06 +00:00
|
|
|
|