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;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-12-04 19:49:34 -08:00
|
|
|
Cu.import("resource://gre/modules/SystemMessagePermissionsChecker.jsm");
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gUUIDGenerator",
|
|
|
|
"@mozilla.org/uuid-generator;1",
|
|
|
|
"nsIUUIDGenerator");
|
|
|
|
|
2013-05-17 05:18:35 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "powerManagerService",
|
|
|
|
"@mozilla.org/power/powermanagerservice;1",
|
|
|
|
"nsIPowerManagerService");
|
|
|
|
|
2014-05-06 03:32:33 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "appsService",
|
|
|
|
"@mozilla.org/AppsService;1",
|
|
|
|
"nsIAppsService");
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
// Limit the number of pending messages for a given page.
|
|
|
|
let kMaxPendingMessages;
|
|
|
|
try {
|
2014-03-31 01:04:42 -07:00
|
|
|
kMaxPendingMessages =
|
|
|
|
Services.prefs.getIntPref("dom.messages.maxPendingMessages");
|
2012-07-02 17:16:55 -07:00
|
|
|
} catch(e) {
|
|
|
|
// getIntPref throws when the pref is not set.
|
|
|
|
kMaxPendingMessages = 5;
|
|
|
|
}
|
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
const kMessages =["SystemMessageManager:GetPendingMessages",
|
2012-10-25 11:45:14 -07:00
|
|
|
"SystemMessageManager:HasPendingMessages",
|
2012-09-18 10:34:56 -07:00
|
|
|
"SystemMessageManager:Register",
|
2012-11-06 18:32:09 -08:00
|
|
|
"SystemMessageManager:Unregister",
|
2012-10-08 19:54:50 -07:00
|
|
|
"SystemMessageManager:Message:Return:OK",
|
2012-10-08 20:15:47 -07:00
|
|
|
"SystemMessageManager:AskReadyToRegister",
|
2013-05-17 05:18:35 -07:00
|
|
|
"SystemMessageManager:HandleMessagesDone",
|
2012-10-01 13:29:59 -07:00
|
|
|
"child-process-shutdown"]
|
2012-09-18 10:34:56 -07:00
|
|
|
|
|
|
|
function debug(aMsg) {
|
2013-01-15 18:19:15 -08:00
|
|
|
// dump("-- SystemMessageInternal " + Date.now() + " : " + aMsg + "\n");
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
|
2013-07-03 04:20:06 -07:00
|
|
|
|
2013-07-16 08:09:58 -07:00
|
|
|
let defaultMessageConfigurator = {
|
2013-07-25 05:36:15 -07:00
|
|
|
get mustShowRunningApp() {
|
|
|
|
return false;
|
2013-07-16 08:09:58 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-03 04:20:06 -07:00
|
|
|
const MSG_SENT_SUCCESS = 0;
|
|
|
|
const MSG_SENT_FAILURE_PERM_DENIED = 1;
|
|
|
|
const MSG_SENT_FAILURE_APP_NOT_RUNNING = 2;
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
// Implementation of the component used by internal users.
|
|
|
|
|
|
|
|
function SystemMessageInternal() {
|
|
|
|
// The set of pages registered by installed apps. We keep the
|
|
|
|
// list of pending messages for each page here also.
|
|
|
|
this._pages = [];
|
2013-01-14 17:24:46 -08:00
|
|
|
|
|
|
|
// The set of listeners. This is a multi-dimensional object. The _listeners
|
2014-03-31 01:04:29 -07:00
|
|
|
// object itself is a map from manifest URL -> an array mapping proccesses to
|
2013-01-14 17:24:46 -08:00
|
|
|
// windows. We do this so that we can track both what processes we have to
|
|
|
|
// send system messages to as well as supporting the single-process case
|
|
|
|
// where we track windows instead.
|
2012-09-18 10:34:56 -07:00
|
|
|
this._listeners = {};
|
2012-10-01 22:03:59 -07:00
|
|
|
|
|
|
|
this._webappsRegistryReady = false;
|
|
|
|
this._bufferedSysMsgs = [];
|
|
|
|
|
2013-05-17 05:18:35 -07:00
|
|
|
this._cpuWakeLocks = {};
|
|
|
|
|
2013-07-16 08:09:58 -07:00
|
|
|
this._configurators = {};
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2012-10-16 21:30:43 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-registry-start", false);
|
2012-10-01 22:03:59 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-registry-ready", false);
|
2014-05-06 03:32:33 -07:00
|
|
|
Services.obs.addObserver(this, "webapps-clear-data", false);
|
2012-10-08 19:54:50 -07:00
|
|
|
kMessages.forEach(function(aMsg) {
|
2012-09-18 10:34:56 -07:00
|
|
|
ppmm.addMessageListener(aMsg, this);
|
2012-10-08 19:54:50 -07:00
|
|
|
}, this);
|
2012-10-08 20:15:47 -07:00
|
|
|
|
|
|
|
Services.obs.notifyObservers(this, "system-message-internal-ready", null);
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SystemMessageInternal.prototype = {
|
2013-05-17 05:18:35 -07:00
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_getMessageConfigurator: function(aType) {
|
2013-07-16 08:09:58 -07:00
|
|
|
debug("_getMessageConfigurator for type: " + aType);
|
|
|
|
if (this._configurators[aType] === undefined) {
|
2014-03-31 01:04:42 -07:00
|
|
|
let contractID =
|
|
|
|
"@mozilla.org/dom/system-messages/configurator/" + aType + ";1";
|
2013-07-16 08:09:58 -07:00
|
|
|
if (contractID in Cc) {
|
|
|
|
debug(contractID + " is registered, creating an instance");
|
|
|
|
this._configurators[aType] =
|
|
|
|
Cc[contractID].createInstance(Ci.nsISystemMessagesConfigurator);
|
|
|
|
} else {
|
|
|
|
debug(contractID + "is not registered, caching the answer");
|
|
|
|
this._configurators[aType] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._configurators[aType] || defaultMessageConfigurator;
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_cancelCpuWakeLock: function(aPageKey) {
|
2013-05-17 05:18:35 -07:00
|
|
|
let cpuWakeLock = this._cpuWakeLocks[aPageKey];
|
|
|
|
if (cpuWakeLock) {
|
|
|
|
debug("Releasing the CPU wake lock for page key = " + aPageKey);
|
|
|
|
cpuWakeLock.wakeLock.unlock();
|
|
|
|
cpuWakeLock.timer.cancel();
|
|
|
|
delete this._cpuWakeLocks[aPageKey];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_acquireCpuWakeLock: function(aPageKey) {
|
2013-05-17 05:18:35 -07:00
|
|
|
let cpuWakeLock = this._cpuWakeLocks[aPageKey];
|
|
|
|
if (!cpuWakeLock) {
|
|
|
|
// We have to ensure the CPU doesn't sleep during the process of the page
|
|
|
|
// handling the system messages, so that they can be handled on time.
|
|
|
|
debug("Acquiring a CPU wake lock for page key = " + aPageKey);
|
|
|
|
cpuWakeLock = this._cpuWakeLocks[aPageKey] = {
|
|
|
|
wakeLock: powerManagerService.newWakeLock("cpu"),
|
|
|
|
timer: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer),
|
|
|
|
lockCount: 1
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// We've already acquired the CPU wake lock for this page,
|
|
|
|
// so just add to the lock count and extend the timeout.
|
|
|
|
cpuWakeLock.lockCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a watchdog to avoid locking the CPU wake lock too long,
|
|
|
|
// because it'd exhaust the battery quickly which is very bad.
|
|
|
|
// This could probably happen if the app failed to launch or
|
|
|
|
// handle the system messages due to any unexpected reasons.
|
|
|
|
cpuWakeLock.timer.initWithCallback(function timerCb() {
|
|
|
|
debug("Releasing the CPU wake lock because the system messages " +
|
|
|
|
"were not handled by its registered page before time out.");
|
|
|
|
this._cancelCpuWakeLock(aPageKey);
|
|
|
|
}.bind(this), 30000, Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
},
|
|
|
|
|
|
|
|
_releaseCpuWakeLock: function _releaseCpuWakeLock(aPageKey, aHandledCount) {
|
|
|
|
let cpuWakeLock = this._cpuWakeLocks[aPageKey];
|
|
|
|
if (cpuWakeLock) {
|
|
|
|
cpuWakeLock.lockCount -= aHandledCount;
|
|
|
|
if (cpuWakeLock.lockCount <= 0) {
|
|
|
|
debug("Unlocking the CPU wake lock now that the system messages " +
|
|
|
|
"have been successfully handled by its registered page.");
|
|
|
|
this._cancelCpuWakeLock(aPageKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_findPage: function(aType, aPageURL, aManifestURL) {
|
2013-06-16 21:16:21 -07:00
|
|
|
let page = null;
|
|
|
|
this._pages.some(function(aPage) {
|
|
|
|
if (this._isPageMatched(aPage, aType, aPageURL, aManifestURL)) {
|
|
|
|
page = aPage;
|
|
|
|
}
|
|
|
|
return page !== null;
|
|
|
|
}, this);
|
|
|
|
return page;
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
sendMessage: function(aType, aMessage, aPageURI, aManifestURI, aExtra) {
|
2012-10-01 22:03:59 -07:00
|
|
|
// Buffer system messages until the webapps' registration is ready,
|
|
|
|
// so that we can know the correct pages registered to be sent.
|
|
|
|
if (!this._webappsRegistryReady) {
|
|
|
|
this._bufferedSysMsgs.push({ how: "send",
|
|
|
|
type: aType,
|
|
|
|
msg: aMessage,
|
|
|
|
pageURI: aPageURI,
|
2013-07-25 10:18:24 -07:00
|
|
|
manifestURI: aManifestURI,
|
|
|
|
extra: aExtra });
|
2012-10-01 22:03:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
// Give this message an ID so that we can identify the message and
|
|
|
|
// clean it up from the pending message queue when apps receive it.
|
|
|
|
let messageID = gUUIDGenerator.generateUUID().toString();
|
|
|
|
|
|
|
|
debug("Sending " + aType + " " + JSON.stringify(aMessage) +
|
2013-07-25 10:18:24 -07:00
|
|
|
" for " + aPageURI.spec + " @ " + aManifestURI.spec +
|
|
|
|
'; extra: ' + JSON.stringify(aExtra));
|
2012-12-04 19:49:34 -08:00
|
|
|
|
2013-07-03 04:20:06 -07:00
|
|
|
let result = this._sendMessageCommon(aType,
|
|
|
|
aMessage,
|
|
|
|
messageID,
|
|
|
|
aPageURI.spec,
|
2013-07-25 10:18:24 -07:00
|
|
|
aManifestURI.spec,
|
|
|
|
aExtra);
|
2013-07-03 04:20:06 -07:00
|
|
|
debug("Returned status of sending message: " + result);
|
|
|
|
|
2012-12-04 19:49:34 -08:00
|
|
|
// Don't need to open the pages and queue the system message
|
|
|
|
// which was not allowed to be sent.
|
2013-07-03 04:20:06 -07:00
|
|
|
if (result === MSG_SENT_FAILURE_PERM_DENIED) {
|
2012-12-04 19:49:34 -08:00
|
|
|
return;
|
2012-09-18 10:34:56 -07:00
|
|
|
}
|
|
|
|
|
2013-06-16 21:16:21 -07:00
|
|
|
let page = this._findPage(aType, aPageURI.spec, aManifestURI.spec);
|
|
|
|
if (page) {
|
2012-10-18 03:56:28 -07:00
|
|
|
// Queue this message in the corresponding pages.
|
2013-06-16 21:16:21 -07:00
|
|
|
this._queueMessage(page, aMessage, messageID);
|
2012-10-18 03:56:28 -07:00
|
|
|
|
2013-07-25 05:36:15 -07:00
|
|
|
this._openAppPage(page, aMessage, aExtra, result);
|
2013-06-16 21:16:21 -07:00
|
|
|
}
|
2012-08-30 12:27:41 -07:00
|
|
|
},
|
2012-07-02 17:16:55 -07:00
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
broadcastMessage: function(aType, aMessage, aExtra) {
|
2012-10-01 22:03:59 -07:00
|
|
|
// Buffer system messages until the webapps' registration is ready,
|
|
|
|
// so that we can know the correct pages registered to be broadcasted.
|
|
|
|
if (!this._webappsRegistryReady) {
|
|
|
|
this._bufferedSysMsgs.push({ how: "broadcast",
|
|
|
|
type: aType,
|
2013-07-25 10:18:24 -07:00
|
|
|
msg: aMessage,
|
|
|
|
extra: aExtra });
|
2012-10-01 22:03:59 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
// Give this message an ID so that we can identify the message and
|
|
|
|
// clean it up from the pending message queue when apps receive it.
|
|
|
|
let messageID = gUUIDGenerator.generateUUID().toString();
|
|
|
|
|
2013-07-25 10:18:24 -07:00
|
|
|
debug("Broadcasting " + aType + " " + JSON.stringify(aMessage) +
|
|
|
|
'; extra = ' + JSON.stringify(aExtra));
|
2012-09-11 03:07:03 -07:00
|
|
|
// Find pages that registered an handler for this type.
|
2012-08-30 12:27:41 -07:00
|
|
|
this._pages.forEach(function(aPage) {
|
|
|
|
if (aPage.type == aType) {
|
2013-07-03 04:20:06 -07:00
|
|
|
let result = this._sendMessageCommon(aType,
|
|
|
|
aMessage,
|
|
|
|
messageID,
|
2014-03-31 01:04:36 -07:00
|
|
|
aPage.pageURL,
|
2014-03-31 01:04:29 -07:00
|
|
|
aPage.manifestURL,
|
2013-07-25 10:18:24 -07:00
|
|
|
aExtra);
|
2013-07-03 04:20:06 -07:00
|
|
|
debug("Returned status of sending message: " + result);
|
|
|
|
|
|
|
|
|
2012-12-04 19:49:34 -08:00
|
|
|
// Don't need to open the pages and queue the system message
|
|
|
|
// which was not allowed to be sent.
|
2013-07-03 04:20:06 -07:00
|
|
|
if (result === MSG_SENT_FAILURE_PERM_DENIED) {
|
2012-12-04 19:49:34 -08:00
|
|
|
return;
|
2012-09-18 10:34:56 -07:00
|
|
|
}
|
2012-12-04 19:49:34 -08:00
|
|
|
|
2012-10-18 03:56:28 -07:00
|
|
|
// Queue this message in the corresponding pages.
|
|
|
|
this._queueMessage(aPage, aMessage, messageID);
|
|
|
|
|
2013-07-25 05:36:15 -07:00
|
|
|
this._openAppPage(aPage, aMessage, aExtra, result);
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
2012-10-08 19:54:50 -07:00
|
|
|
}, this);
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
registerPage: function(aType, aPageURI, aManifestURI) {
|
2012-07-02 17:16:55 -07:00
|
|
|
if (!aPageURI || !aManifestURI) {
|
|
|
|
throw Cr.NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2013-06-16 21:16:21 -07:00
|
|
|
let pageURL = aPageURI.spec;
|
|
|
|
let manifestURL = aManifestURI.spec;
|
|
|
|
|
|
|
|
// Don't register duplicates for this tuple.
|
|
|
|
let page = this._findPage(aType, pageURL, manifestURL);
|
|
|
|
if (page) {
|
|
|
|
debug("Ignoring duplicate registration of " +
|
|
|
|
[aType, pageURL, manifestURL]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-09 21:33:16 -07:00
|
|
|
this._pages.push({ type: aType,
|
2014-03-31 01:04:36 -07:00
|
|
|
pageURL: pageURL,
|
2014-03-31 01:04:29 -07:00
|
|
|
manifestURL: manifestURL,
|
2012-10-08 19:54:50 -07:00
|
|
|
pendingMessages: [] });
|
2012-07-02 17:16:55 -07:00
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_findTargetIndex: function(aTargets, aTarget) {
|
2013-01-15 18:19:15 -08:00
|
|
|
if (!aTargets || !aTarget) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (let index = 0; index < aTargets.length; ++index) {
|
|
|
|
let target = aTargets[index];
|
|
|
|
if (target.target === aTarget) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_isEmptyObject: function(aObj) {
|
2013-05-31 07:34:33 -07:00
|
|
|
for (let name in aObj) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:42 -07:00
|
|
|
_removeTargetFromListener: function(aTarget,
|
|
|
|
aManifestURL,
|
|
|
|
aRemoveListener,
|
|
|
|
aPageURL) {
|
2014-03-31 01:04:29 -07:00
|
|
|
let targets = this._listeners[aManifestURL];
|
2013-01-15 18:19:15 -08:00
|
|
|
if (!targets) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = this._findTargetIndex(targets, aTarget);
|
|
|
|
if (index === -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRemoveListener) {
|
2014-03-31 01:04:29 -07:00
|
|
|
debug("remove the listener for " + aManifestURL);
|
|
|
|
delete this._listeners[aManifestURL];
|
2013-01-15 18:19:15 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-31 07:34:33 -07:00
|
|
|
let target = targets[index];
|
2014-03-31 01:04:36 -07:00
|
|
|
if (aPageURL && target.winCounts[aPageURL] !== undefined &&
|
|
|
|
--target.winCounts[aPageURL] === 0) {
|
|
|
|
delete target.winCounts[aPageURL];
|
2013-05-31 07:34:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isEmptyObject(target.winCounts)) {
|
2013-01-15 18:19:15 -08:00
|
|
|
if (targets.length === 1) {
|
2014-03-31 01:04:29 -07:00
|
|
|
// If it's the only one, get rid of the entry of manifest URL entirely.
|
|
|
|
debug("remove the listener for " + aManifestURL);
|
|
|
|
delete this._listeners[aManifestURL];
|
2013-01-15 18:19:15 -08:00
|
|
|
} else {
|
|
|
|
// If more than one left, remove this one and leave the rest.
|
|
|
|
targets.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
receiveMessage: function(aMessage) {
|
2012-07-02 17:16:55 -07:00
|
|
|
let msg = aMessage.json;
|
2012-12-27 04:29:32 -08:00
|
|
|
|
|
|
|
// To prevent the hacked child process from sending commands to parent
|
|
|
|
// to manage system messages, we need to check its manifest URL.
|
|
|
|
if (["SystemMessageManager:Register",
|
2014-03-25 21:56:35 -07:00
|
|
|
// TODO: fix bug 988142 to re-enable.
|
|
|
|
// "SystemMessageManager:Unregister",
|
2012-12-27 04:29:32 -08:00
|
|
|
"SystemMessageManager:GetPendingMessages",
|
|
|
|
"SystemMessageManager:HasPendingMessages",
|
2013-05-17 05:18:35 -07:00
|
|
|
"SystemMessageManager:Message:Return:OK",
|
|
|
|
"SystemMessageManager:HandleMessagesDone"].indexOf(aMessage.name) != -1) {
|
2014-03-31 01:04:29 -07:00
|
|
|
if (!aMessage.target.assertContainApp(msg.manifestURL)) {
|
2012-12-27 04:29:32 -08:00
|
|
|
debug("Got message from a child process containing illegal manifest URL.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 10:34:56 -07:00
|
|
|
switch(aMessage.name) {
|
2012-10-08 20:15:47 -07:00
|
|
|
case "SystemMessageManager:AskReadyToRegister":
|
|
|
|
return true;
|
|
|
|
break;
|
2012-09-18 10:34:56 -07:00
|
|
|
case "SystemMessageManager:Register":
|
2012-10-08 19:54:50 -07:00
|
|
|
{
|
2014-03-31 01:04:36 -07:00
|
|
|
debug("Got Register from " + msg.pageURL + " @ " + msg.manifestURL);
|
|
|
|
let pageURL = msg.pageURL;
|
2013-01-15 18:19:15 -08:00
|
|
|
let targets, index;
|
2014-03-31 01:04:29 -07:00
|
|
|
if (!(targets = this._listeners[msg.manifestURL])) {
|
2013-05-31 07:34:33 -07:00
|
|
|
let winCounts = {};
|
2014-03-31 01:04:36 -07:00
|
|
|
winCounts[pageURL] = 1;
|
2014-03-31 01:04:29 -07:00
|
|
|
this._listeners[msg.manifestURL] = [{ target: aMessage.target,
|
2014-03-31 01:04:42 -07:00
|
|
|
winCounts: winCounts }];
|
|
|
|
} else if ((index = this._findTargetIndex(targets,
|
|
|
|
aMessage.target)) === -1) {
|
2013-05-31 07:34:33 -07:00
|
|
|
let winCounts = {};
|
2014-03-31 01:04:36 -07:00
|
|
|
winCounts[pageURL] = 1;
|
2013-01-15 18:19:15 -08:00
|
|
|
targets.push({ target: aMessage.target,
|
2013-05-31 07:34:33 -07:00
|
|
|
winCounts: winCounts });
|
2013-01-14 17:24:46 -08:00
|
|
|
} else {
|
2013-05-31 07:34:33 -07:00
|
|
|
let winCounts = targets[index].winCounts;
|
2014-03-31 01:04:36 -07:00
|
|
|
if (winCounts[pageURL] === undefined) {
|
|
|
|
winCounts[pageURL] = 1;
|
2013-05-31 07:34:33 -07:00
|
|
|
} else {
|
2014-03-31 01:04:36 -07:00
|
|
|
winCounts[pageURL]++;
|
2013-05-31 07:34:33 -07:00
|
|
|
}
|
2012-09-18 10:34:56 -07:00
|
|
|
}
|
2013-01-14 17:24:46 -08:00
|
|
|
|
2014-03-31 01:04:42 -07:00
|
|
|
debug("listeners for " + msg.manifestURL +
|
|
|
|
" innerWinID " + msg.innerWindowID);
|
2012-09-18 10:34:56 -07:00
|
|
|
break;
|
2012-10-08 19:54:50 -07:00
|
|
|
}
|
2012-10-01 13:29:59 -07:00
|
|
|
case "child-process-shutdown":
|
2012-10-08 19:54:50 -07:00
|
|
|
{
|
2012-11-06 18:32:09 -08:00
|
|
|
debug("Got child-process-shutdown from " + aMessage.target);
|
2014-03-31 01:04:29 -07:00
|
|
|
for (let manifestURL in this._listeners) {
|
|
|
|
// See if any processes in this manifest URL have this target.
|
2014-04-10 17:05:59 -07:00
|
|
|
this._removeTargetFromListener(aMessage.target,
|
|
|
|
manifestURL,
|
|
|
|
true,
|
|
|
|
null);
|
2012-09-18 10:34:56 -07:00
|
|
|
}
|
|
|
|
break;
|
2012-10-08 19:54:50 -07:00
|
|
|
}
|
2012-11-06 18:32:09 -08:00
|
|
|
case "SystemMessageManager:Unregister":
|
|
|
|
{
|
2014-03-31 01:04:42 -07:00
|
|
|
debug("Got Unregister from " + aMessage.target +
|
|
|
|
" innerWinID " + msg.innerWindowID);
|
|
|
|
this._removeTargetFromListener(aMessage.target,
|
|
|
|
msg.manifestURL,
|
|
|
|
false,
|
|
|
|
msg.pageURL);
|
2012-11-06 18:32:09 -08:00
|
|
|
break;
|
|
|
|
}
|
2012-10-08 19:54:50 -07:00
|
|
|
case "SystemMessageManager:GetPendingMessages":
|
|
|
|
{
|
|
|
|
debug("received SystemMessageManager:GetPendingMessages " + msg.type +
|
2014-03-31 01:04:36 -07:00
|
|
|
" for " + msg.pageURL + " @ " + msg.manifestURL);
|
2012-10-08 19:54:50 -07:00
|
|
|
|
|
|
|
// This is a sync call used to return the pending messages for a page.
|
|
|
|
// Find the right page to get its corresponding pending messages.
|
2014-03-31 01:04:36 -07:00
|
|
|
let page = this._findPage(msg.type, msg.pageURL, msg.manifestURL);
|
2012-09-18 10:34:56 -07:00
|
|
|
if (!page) {
|
2012-10-25 11:45:14 -07:00
|
|
|
return;
|
2012-09-18 10:34:56 -07:00
|
|
|
}
|
|
|
|
|
2012-10-08 19:54:50 -07:00
|
|
|
// Return the |msg| of each pending message (drop the |msgID|).
|
|
|
|
let pendingMessages = [];
|
|
|
|
page.pendingMessages.forEach(function(aMessage) {
|
|
|
|
pendingMessages.push(aMessage.msg);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Clear the pending queue for this page. This is OK since we'll store
|
|
|
|
// pending messages in the content process (|SystemMessageManager|).
|
|
|
|
page.pendingMessages.length = 0;
|
2012-09-18 10:34:56 -07:00
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
// Send the array of pending messages.
|
2013-06-01 01:25:59 -07:00
|
|
|
aMessage.target
|
|
|
|
.sendAsyncMessage("SystemMessageManager:GetPendingMessages:Return",
|
|
|
|
{ type: msg.type,
|
2014-03-31 01:04:29 -07:00
|
|
|
manifestURL: msg.manifestURL,
|
2014-03-31 01:04:36 -07:00
|
|
|
pageURL: msg.pageURL,
|
2013-06-01 01:25:59 -07:00
|
|
|
msgQueue: pendingMessages });
|
2012-10-25 11:45:14 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "SystemMessageManager:HasPendingMessages":
|
|
|
|
{
|
|
|
|
debug("received SystemMessageManager:HasPendingMessages " + msg.type +
|
2014-03-31 01:04:36 -07:00
|
|
|
" for " + msg.pageURL + " @ " + msg.manifestURL);
|
2012-10-25 11:45:14 -07:00
|
|
|
|
|
|
|
// This is a sync call used to return if a page has pending messages.
|
|
|
|
// Find the right page to get its corresponding pending messages.
|
2014-03-31 01:04:36 -07:00
|
|
|
let page = this._findPage(msg.type, msg.pageURL, msg.manifestURL);
|
2012-10-25 11:45:14 -07:00
|
|
|
if (!page) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return page.pendingMessages.length != 0;
|
2012-09-18 10:34:56 -07:00
|
|
|
break;
|
2012-10-08 19:54:50 -07:00
|
|
|
}
|
|
|
|
case "SystemMessageManager:Message:Return:OK":
|
|
|
|
{
|
|
|
|
debug("received SystemMessageManager:Message:Return:OK " + msg.type +
|
2014-03-31 01:04:36 -07:00
|
|
|
" for " + msg.pageURL + " @ " + msg.manifestURL);
|
2012-10-08 19:54:50 -07:00
|
|
|
|
|
|
|
// We need to clean up the pending message since the app has already
|
|
|
|
// received it, thus avoiding the re-lanunched app handling it again.
|
2014-03-31 01:04:36 -07:00
|
|
|
let page = this._findPage(msg.type, msg.pageURL, msg.manifestURL);
|
2013-06-16 21:16:21 -07:00
|
|
|
if (page) {
|
|
|
|
let pendingMessages = page.pendingMessages;
|
2012-10-08 19:54:50 -07:00
|
|
|
for (let i = 0; i < pendingMessages.length; i++) {
|
|
|
|
if (pendingMessages[i].msgID === msg.msgID) {
|
|
|
|
pendingMessages.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-16 21:16:21 -07:00
|
|
|
}
|
2012-10-08 19:54:50 -07:00
|
|
|
break;
|
|
|
|
}
|
2013-05-17 05:18:35 -07:00
|
|
|
case "SystemMessageManager:HandleMessagesDone":
|
|
|
|
{
|
|
|
|
debug("received SystemMessageManager:HandleMessagesDone " + msg.type +
|
2014-03-31 01:04:42 -07:00
|
|
|
" with " + msg.handledCount + " for " + msg.pageURL +
|
|
|
|
" @ " + msg.manifestURL);
|
2013-05-17 05:18:35 -07:00
|
|
|
|
|
|
|
// A page has finished handling some of its system messages, so we try
|
|
|
|
// to release the CPU wake lock we acquired on behalf of that page.
|
|
|
|
this._releaseCpuWakeLock(this._createKeyForPage(msg), msg.handledCount);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2012-10-01 22:03:59 -07:00
|
|
|
switch (aTopic) {
|
|
|
|
case "xpcom-shutdown":
|
2012-10-08 19:54:50 -07:00
|
|
|
kMessages.forEach(function(aMsg) {
|
2012-10-01 22:03:59 -07:00
|
|
|
ppmm.removeMessageListener(aMsg, this);
|
2012-10-08 19:54:50 -07:00
|
|
|
}, this);
|
2012-10-01 22:03:59 -07:00
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
2012-10-16 21:30:43 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-registry-start");
|
2012-10-01 22:03:59 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-registry-ready");
|
2014-05-06 03:32:33 -07:00
|
|
|
Services.obs.removeObserver(this, "webapps-clear-data");
|
2012-10-01 22:03:59 -07:00
|
|
|
ppmm = null;
|
|
|
|
this._pages = null;
|
|
|
|
this._bufferedSysMsgs = null;
|
|
|
|
break;
|
2012-10-16 21:30:43 -07:00
|
|
|
case "webapps-registry-start":
|
|
|
|
this._webappsRegistryReady = false;
|
|
|
|
break;
|
2012-10-01 22:03:59 -07:00
|
|
|
case "webapps-registry-ready":
|
|
|
|
// After the webapps' registration has been done for sure,
|
|
|
|
// re-fire the buffered system messages if there is any.
|
|
|
|
this._webappsRegistryReady = true;
|
2012-10-08 19:54:50 -07:00
|
|
|
this._bufferedSysMsgs.forEach(function(aSysMsg) {
|
2012-10-01 22:03:59 -07:00
|
|
|
switch (aSysMsg.how) {
|
|
|
|
case "send":
|
|
|
|
this.sendMessage(
|
2014-03-31 01:04:29 -07:00
|
|
|
aSysMsg.type, aSysMsg.msg,
|
|
|
|
aSysMsg.pageURI, aSysMsg.manifestURI, aSysMsg.extra);
|
2012-10-01 22:03:59 -07:00
|
|
|
break;
|
|
|
|
case "broadcast":
|
2013-07-25 10:18:24 -07:00
|
|
|
this.broadcastMessage(aSysMsg.type, aSysMsg.msg, aSysMsg.extra);
|
2012-10-01 22:03:59 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-10-08 19:54:50 -07:00
|
|
|
}, this);
|
2012-10-16 21:30:43 -07:00
|
|
|
this._bufferedSysMsgs.length = 0;
|
2012-10-01 22:03:59 -07:00
|
|
|
break;
|
2014-05-06 03:32:33 -07:00
|
|
|
case "webapps-clear-data":
|
|
|
|
let params =
|
|
|
|
aSubject.QueryInterface(Ci.mozIApplicationClearPrivateDataParams);
|
|
|
|
if (!params) {
|
|
|
|
debug("Error updating registered pages for an uninstalled app.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update registered pages for apps.
|
|
|
|
if (params.browserOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let manifestURL = appsService.getManifestURLByLocalId(params.appId);
|
|
|
|
if (!manifestURL) {
|
|
|
|
debug("Error updating registered pages for an uninstalled app.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = this._pages.length - 1; i >= 0; i--) {
|
|
|
|
let page = this._pages[i];
|
|
|
|
if (page.manifestURL === manifestURL) {
|
|
|
|
this._pages.splice(i, 1);
|
|
|
|
debug("Remove " + page.pageURL + " @ " + page.manifestURL +
|
|
|
|
" from registered pages due to app uninstallation.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("Finish updating registered pages for an uninstalled app.");
|
|
|
|
break;
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_queueMessage: function(aPage, aMessage, aMessageID) {
|
2012-10-08 19:54:50 -07:00
|
|
|
// Queue the message for this page because we've never known if an app is
|
|
|
|
// opened or not. We'll clean it up when the app has already received it.
|
|
|
|
aPage.pendingMessages.push({ msg: aMessage, msgID: aMessageID });
|
|
|
|
if (aPage.pendingMessages.length > kMaxPendingMessages) {
|
|
|
|
aPage.pendingMessages.splice(0, 1);
|
2012-09-11 03:07:03 -07:00
|
|
|
}
|
2012-10-18 03:56:28 -07:00
|
|
|
},
|
2012-08-30 12:27:41 -07:00
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_openAppPage: function(aPage, aMessage, aExtra, aMsgSentStatus) {
|
2013-07-25 05:36:15 -07:00
|
|
|
// This means the app must be brought to the foreground.
|
|
|
|
let showApp = this._getMessageConfigurator(aPage.type).mustShowRunningApp;
|
|
|
|
|
|
|
|
// We should send the open-app message if the system message was
|
|
|
|
// not sent, or if it was sent but we should show the app anyway.
|
|
|
|
if ((aMsgSentStatus === MSG_SENT_SUCCESS) && !showApp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This flag means the app must *only* be brought to the foreground
|
|
|
|
// and we don't need to load the app to handle messages.
|
|
|
|
let onlyShowApp = (aMsgSentStatus === MSG_SENT_SUCCESS) && showApp;
|
|
|
|
|
2014-05-30 00:48:25 -07:00
|
|
|
debug("Asking to open pageURL: " + aPage.pageURL +
|
|
|
|
", manifestURL: " + aPage.manifestURL + ", type: " + aPage.type +
|
|
|
|
", target: " + JSON.stringify(aMessage.target) +
|
|
|
|
", showApp: " + showApp + ", onlyShowApp: " + onlyShowApp +
|
|
|
|
", extra: " + JSON.stringify(aExtra));
|
|
|
|
|
|
|
|
let glue = Cc["@mozilla.org/dom/messages/system-message-glue;1"]
|
|
|
|
.createInstance(Ci.nsISystemMessageGlue);
|
|
|
|
if (glue) {
|
|
|
|
glue.openApp(aPage.pageURL, aPage.manifestURL, aPage.type, aMessage.target,
|
|
|
|
showApp, onlyShowApp, aExtra);
|
|
|
|
} else {
|
|
|
|
debug("Error! The UI glue component is not implemented.");
|
|
|
|
}
|
2012-08-30 12:27:41 -07:00
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:36 -07:00
|
|
|
_isPageMatched: function(aPage, aType, aPageURL, aManifestURL) {
|
2012-10-08 19:54:50 -07:00
|
|
|
return (aPage.type === aType &&
|
2014-03-31 01:04:29 -07:00
|
|
|
aPage.manifestURL === aManifestURL &&
|
2014-03-31 01:04:36 -07:00
|
|
|
aPage.pageURL === aPageURL)
|
2012-10-08 19:54:50 -07:00
|
|
|
},
|
|
|
|
|
2012-10-18 03:56:28 -07:00
|
|
|
_createKeyForPage: function _createKeyForPage(aPage) {
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
hasher.init(hasher.SHA1);
|
|
|
|
|
2014-03-31 01:04:36 -07:00
|
|
|
// add manifest/page URL and action to the hash
|
|
|
|
["type", "manifestURL", "pageURL"].forEach(function(aProp) {
|
2012-10-18 03:56:28 -07:00
|
|
|
let data = converter.convertToByteArray(aPage[aProp], {});
|
|
|
|
hasher.update(data, data.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
return hasher.finish(true);
|
|
|
|
},
|
|
|
|
|
2014-03-31 01:04:11 -07:00
|
|
|
_sendMessageCommon: function(aType, aMessage, aMessageID,
|
2014-03-31 01:04:36 -07:00
|
|
|
aPageURL, aManifestURL, aExtra) {
|
2012-12-04 19:49:34 -08:00
|
|
|
// Don't send the system message not granted by the app's permissions.
|
|
|
|
if (!SystemMessagePermissionsChecker
|
|
|
|
.isSystemMessagePermittedToSend(aType,
|
2014-03-31 01:04:36 -07:00
|
|
|
aPageURL,
|
2014-03-31 01:04:29 -07:00
|
|
|
aManifestURL)) {
|
2013-07-03 04:20:06 -07:00
|
|
|
return MSG_SENT_FAILURE_PERM_DENIED;
|
2012-12-04 19:49:34 -08:00
|
|
|
}
|
|
|
|
|
2013-05-17 05:18:35 -07:00
|
|
|
let appPageIsRunning = false;
|
|
|
|
let pageKey = this._createKeyForPage({ type: aType,
|
2014-03-31 01:04:29 -07:00
|
|
|
manifestURL: aManifestURL,
|
2014-03-31 01:04:36 -07:00
|
|
|
pageURL: aPageURL });
|
2013-07-16 08:09:58 -07:00
|
|
|
|
2014-03-31 01:04:29 -07:00
|
|
|
let targets = this._listeners[aManifestURL];
|
2013-07-25 05:36:15 -07:00
|
|
|
if (targets) {
|
|
|
|
for (let index = 0; index < targets.length; ++index) {
|
|
|
|
let target = targets[index];
|
|
|
|
// We only need to send the system message to the targets (processes)
|
|
|
|
// which contain the window page that matches the manifest/page URL of
|
|
|
|
// the destination of system message.
|
2014-03-31 01:04:36 -07:00
|
|
|
if (target.winCounts[aPageURL] === undefined) {
|
2013-07-25 05:36:15 -07:00
|
|
|
continue;
|
2013-07-16 08:09:58 -07:00
|
|
|
}
|
2013-07-25 05:36:15 -07:00
|
|
|
|
|
|
|
appPageIsRunning = true;
|
|
|
|
// We need to acquire a CPU wake lock for that page and expect that
|
|
|
|
// we'll receive a "SystemMessageManager:HandleMessagesDone" message
|
|
|
|
// when the page finishes handling the system message. At that point,
|
|
|
|
// we'll release the lock we acquired.
|
|
|
|
this._acquireCpuWakeLock(pageKey);
|
|
|
|
|
|
|
|
// Multiple windows can share the same target (process), the content
|
|
|
|
// window needs to check if the manifest/page URL is matched. Only
|
|
|
|
// *one* window should handle the system message.
|
|
|
|
let manager = target.target;
|
|
|
|
manager.sendAsyncMessage("SystemMessageManager:Message",
|
|
|
|
{ type: aType,
|
|
|
|
msg: aMessage,
|
2014-03-31 01:04:29 -07:00
|
|
|
manifestURL: aManifestURL,
|
2014-03-31 01:04:36 -07:00
|
|
|
pageURL: aPageURL,
|
2013-07-25 05:36:15 -07:00
|
|
|
msgID: aMessageID });
|
2012-12-04 19:49:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 05:18:35 -07:00
|
|
|
if (!appPageIsRunning) {
|
|
|
|
// The app page isn't running and relies on the 'open-app' chrome event to
|
|
|
|
// wake it up. We still need to acquire a CPU wake lock for that page and
|
|
|
|
// expect that we will receive a "SystemMessageManager:HandleMessagesDone"
|
|
|
|
// message when the page finishes handling the system message with other
|
|
|
|
// pending messages. At that point, we'll release the lock we acquired.
|
|
|
|
this._acquireCpuWakeLock(pageKey);
|
2013-07-03 04:20:06 -07:00
|
|
|
return MSG_SENT_FAILURE_APP_NOT_RUNNING;
|
|
|
|
} else {
|
|
|
|
return MSG_SENT_SUCCESS;
|
2013-05-17 05:18:35 -07:00
|
|
|
}
|
|
|
|
|
2012-12-04 19:49:34 -08:00
|
|
|
},
|
|
|
|
|
2012-07-02 17:16:55 -07:00
|
|
|
classID: Components.ID("{70589ca5-91ac-4b9e-b839-d6a88167d714}"),
|
|
|
|
|
2014-03-31 01:04:42 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsISystemMessagesInternal,
|
|
|
|
Ci.nsIObserver])
|
2012-07-02 17:16:55 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SystemMessageInternal]);
|