2012-07-02 17:16:55 -07: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2013-01-18 05:50:01 -08:00
|
|
|
const Cr = Components.results;
|
2012-07-02 17:16:55 -07:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-08-19 12:00:19 -07:00
|
|
|
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-10-08 20:15:47 -07:00
|
|
|
const kSystemMessageInternalReady = "system-message-internal-ready";
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsISyncMessageSender");
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-08-23 11:56:36 -07:00
|
|
|
function debug(aMsg) {
|
2013-01-18 05:50:01 -08:00
|
|
|
// dump("-- SystemMessageManager " + Date.now() + " : " + aMsg + "\n");
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of the DOM API for system messages
|
|
|
|
|
|
|
|
function SystemMessageManager() {
|
|
|
|
// Message handlers for this page.
|
|
|
|
// We can have only one handler per message type.
|
|
|
|
this._handlers = {};
|
|
|
|
|
|
|
|
// Pending messages for this page, keyed by message type.
|
|
|
|
this._pendings = {};
|
2012-10-08 20:15:47 -07:00
|
|
|
|
|
|
|
// Flag to specify if this process has already registered manifest.
|
|
|
|
this._registerManifestReady = false;
|
|
|
|
|
|
|
|
// Flag to determine this process is a parent or child process.
|
|
|
|
let appInfo = Cc["@mozilla.org/xre/app-info;1"];
|
|
|
|
this._isParentProcess =
|
|
|
|
!appInfo || appInfo.getService(Ci.nsIXULRuntime)
|
|
|
|
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
|
|
|
|
|
|
|
// An oberver to listen to whether the |SystemMessageInternal| is ready.
|
|
|
|
if (this._isParentProcess) {
|
|
|
|
Services.obs.addObserver(this, kSystemMessageInternalReady, false);
|
|
|
|
}
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SystemMessageManager.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
_dispatchMessage: function sysMessMgr_dispatchMessage(aType, aHandler, aMessage) {
|
|
|
|
// We get a json blob, but in some cases we want another kind of object
|
|
|
|
// to be dispatched.
|
|
|
|
// To do so, we check if we have a with a contract ID of
|
|
|
|
// "@mozilla.org/dom/system-messages/wrapper/TYPE;1" component implementing
|
|
|
|
// nsISystemMessageWrapper.
|
|
|
|
debug("Dispatching " + JSON.stringify(aMessage) + "\n");
|
|
|
|
let contractID = "@mozilla.org/dom/system-messages/wrapper/" + aType + ";1";
|
2012-08-23 11:56:36 -07:00
|
|
|
let wrapped = false;
|
2012-07-20 08:41:30 -07:00
|
|
|
|
|
|
|
if (contractID in Cc) {
|
|
|
|
debug(contractID + " is registered, creating an instance");
|
|
|
|
let wrapper = Cc[contractID].createInstance(Ci.nsISystemMessagesWrapper);
|
|
|
|
if (wrapper) {
|
2012-08-24 21:07:58 -07:00
|
|
|
aMessage = wrapper.wrapMessage(aMessage, this._window);
|
2012-08-23 11:56:36 -07:00
|
|
|
wrapped = true;
|
2012-07-20 08:41:30 -07:00
|
|
|
debug("wrapped = " + aMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
aHandler.handleMessage(wrapped ? aMessage
|
|
|
|
: ObjectWrapper.wrap(aMessage, this._window));
|
2012-10-31 00:52:01 -07:00
|
|
|
|
2013-02-14 12:41:30 -08:00
|
|
|
Services.obs.notifyObservers(/* aSubject */ null,
|
|
|
|
"SystemMessageManager:HandleMessageDone",
|
|
|
|
/* aData */ null);
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
mozSetMessageHandler: function sysMessMgr_setMessageHandler(aType, aHandler) {
|
2013-01-18 05:50:01 -08:00
|
|
|
debug("set message handler for [" + aType + "] " + aHandler);
|
|
|
|
|
|
|
|
if (this._isInBrowserElement) {
|
|
|
|
debug("the app loaded in the browser cannot set message handler");
|
2013-03-08 12:13:12 -08:00
|
|
|
// Don't throw there, but ignore the registration.
|
|
|
|
return;
|
2013-01-18 05:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
if (!aType) {
|
|
|
|
// Just bail out if we have no type.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let handlers = this._handlers;
|
|
|
|
if (!aHandler) {
|
|
|
|
// Setting the handler to null means we don't want to receive messages
|
|
|
|
// of this type anymore.
|
|
|
|
delete handlers[aType];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last registered handler wins.
|
|
|
|
handlers[aType] = aHandler;
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
// Ask for the list of currently pending messages.
|
|
|
|
cpmm.sendAsyncMessage("SystemMessageManager:GetPendingMessages",
|
|
|
|
{ type: aType,
|
|
|
|
uri: this._uri,
|
|
|
|
manifest: this._manifest });
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
mozHasPendingMessage: function sysMessMgr_hasPendingMessage(aType) {
|
2013-01-18 05:50:01 -08:00
|
|
|
debug("asking pending message for [" + aType + "]");
|
|
|
|
|
|
|
|
if (this._isInBrowserElement) {
|
|
|
|
debug("the app loaded in the browser cannot ask pending message");
|
2013-03-08 12:13:12 -08:00
|
|
|
// Don't throw there, but pretend to have no messages available.
|
|
|
|
return false;
|
2013-01-18 05:50:01 -08:00
|
|
|
}
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
// If we have a handler for this type, we can't have any pending message.
|
2012-10-25 11:45:14 -07:00
|
|
|
if (aType in this._handlers) {
|
2012-07-02 17:16:55 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
return cpmm.sendSyncMessage("SystemMessageManager:HasPendingMessages",
|
|
|
|
{ type: aType,
|
|
|
|
uri: this._uri,
|
|
|
|
manifest: this._manifest })[0];
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function sysMessMgr_uninit() {
|
|
|
|
this._handlers = null;
|
2012-10-08 20:15:47 -07:00
|
|
|
this._pendings = null;
|
|
|
|
|
|
|
|
if (this._isParentProcess) {
|
|
|
|
Services.obs.removeObserver(this, kSystemMessageInternalReady);
|
|
|
|
}
|
2012-11-06 18:32:09 -08:00
|
|
|
|
2013-01-18 05:50:01 -08:00
|
|
|
if (this._isInBrowserElement) {
|
|
|
|
debug("the app loaded in the browser doesn't need to unregister " +
|
|
|
|
"the manifest for listening to the system messages");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-06 18:32:09 -08:00
|
|
|
cpmm.sendAsyncMessage("SystemMessageManager:Unregister",
|
|
|
|
{ manifest: this._manifest,
|
|
|
|
innerWindowID: this.innerWindowID
|
|
|
|
});
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function sysMessMgr_receiveMessage(aMessage) {
|
2013-01-18 05:50:01 -08:00
|
|
|
debug("receiveMessage " + aMessage.name + " for [" + aMessage.data.type + "] " +
|
|
|
|
"with manifest = " + aMessage.data.manifest + " (" + this._manifest + ") " +
|
|
|
|
"and uri = " + aMessage.data.uri + " (" + this._uri + ")");
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
let msg = aMessage.data;
|
2012-10-13 01:05:42 -07:00
|
|
|
if (msg.manifest != this._manifest || msg.uri != this._uri) {
|
2012-07-02 17:16:55 -07:00
|
|
|
return;
|
2012-10-13 01:05:42 -07:00
|
|
|
}
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
if (aMessage.name == "SystemMessageManager:Message") {
|
|
|
|
// Send an acknowledgement to parent to clean up the pending message,
|
|
|
|
// so a re-launched app won't handle it again, which is redundant.
|
|
|
|
cpmm.sendAsyncMessage(
|
|
|
|
"SystemMessageManager:Message:Return:OK",
|
|
|
|
{ type: msg.type,
|
|
|
|
manifest: msg.manifest,
|
|
|
|
uri: msg.uri,
|
|
|
|
msgID: msg.msgID });
|
|
|
|
}
|
2012-10-08 19:54:50 -07:00
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
// Bail out if we have no handlers registered for this type.
|
|
|
|
if (!(msg.type in this._handlers)) {
|
|
|
|
debug("No handler for this type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
let messages = (aMessage.name == "SystemMessageManager:Message")
|
|
|
|
? [msg.msg]
|
|
|
|
: msg.msgQueue;
|
|
|
|
|
|
|
|
messages.forEach(function(aMsg) {
|
|
|
|
this._dispatchMessage(msg.type, this._handlers[msg.type], aMsg);
|
|
|
|
}, this);
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIDOMGlobalPropertyInitializer implementation.
|
|
|
|
init: function sysMessMgr_init(aWindow) {
|
|
|
|
debug("init");
|
2012-10-25 11:45:14 -07:00
|
|
|
this.initHelper(aWindow, ["SystemMessageManager:Message",
|
|
|
|
"SystemMessageManager:GetPendingMessages:Return"]);
|
2012-08-13 09:58:38 -07:00
|
|
|
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
2013-01-18 05:50:01 -08:00
|
|
|
this._isInBrowserElement = principal.isInBrowserElement;
|
2012-08-13 09:58:38 -07:00
|
|
|
this._uri = principal.URI.spec;
|
|
|
|
|
|
|
|
let appsService = Cc["@mozilla.org/AppsService;1"]
|
|
|
|
.getService(Ci.nsIAppsService);
|
|
|
|
this._manifest = appsService.getManifestURLByLocalId(principal.appId);
|
2012-10-08 20:15:47 -07:00
|
|
|
|
|
|
|
// Two cases are valid to register the manifest for the current process:
|
|
|
|
// 1. This is asked by a child process (parent process must be ready).
|
|
|
|
// 2. Parent process has already constructed the |SystemMessageInternal|.
|
|
|
|
// Otherwise, delay to do it when the |SystemMessageInternal| is ready.
|
|
|
|
let readyToRegister = true;
|
|
|
|
if (this._isParentProcess) {
|
|
|
|
let ready = cpmm.sendSyncMessage(
|
|
|
|
"SystemMessageManager:AskReadyToRegister", null);
|
|
|
|
if (ready.length == 0 || !ready[0]) {
|
|
|
|
readyToRegister = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readyToRegister) {
|
|
|
|
this._registerManifest();
|
|
|
|
}
|
|
|
|
|
2012-09-18 10:34:56 -07:00
|
|
|
debug("done");
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
2012-10-08 20:15:47 -07:00
|
|
|
observe: function sysMessMgr_observe(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic === kSystemMessageInternalReady) {
|
|
|
|
this._registerManifest();
|
|
|
|
}
|
2012-12-04 19:49:34 -08:00
|
|
|
// Call the DOMRequestIpcHelper.observe method.
|
2012-11-06 18:32:09 -08:00
|
|
|
this.__proto__.__proto__.observe.call(this, aSubject, aTopic, aData);
|
2012-10-08 20:15:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_registerManifest: function sysMessMgr_registerManifest() {
|
2013-01-18 05:50:01 -08:00
|
|
|
if (this._isInBrowserElement) {
|
|
|
|
debug("the app loaded in the browser doesn't need to register " +
|
|
|
|
"the manifest for listening to the system messages");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-08 20:15:47 -07:00
|
|
|
if (!this._registerManifestReady) {
|
|
|
|
cpmm.sendAsyncMessage("SystemMessageManager:Register",
|
2012-11-06 18:32:09 -08:00
|
|
|
{ manifest: this._manifest,
|
|
|
|
innerWindowID: this.innerWindowID
|
|
|
|
});
|
2012-10-08 20:15:47 -07:00
|
|
|
this._registerManifestReady = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
classID: Components.ID("{bc076ea0-609b-4d8f-83d7-5af7cbdc3bb2}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMNavigatorSystemMessages,
|
2012-10-08 20:15:47 -07:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
|
|
|
Ci.nsIObserver]),
|
2012-07-02 17:16:55 -07:00
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{bc076ea0-609b-4d8f-83d7-5af7cbdc3bb2}"),
|
|
|
|
contractID: "@mozilla.org/system-message-manager;1",
|
|
|
|
interfaces: [Ci.nsIDOMNavigatorSystemMessages],
|
|
|
|
flags: Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
classDescription: "System Messages"})
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SystemMessageManager]);
|