2012-08-28 06:43:57 +04: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 Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
// This module exposes a subset of the functionalities of the parent DOM
|
|
|
|
// Registry to content processes, to be used from the AppsService component.
|
2012-08-28 06:43:57 +04:00
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry", "WrappedManifestCache"];
|
2012-08-28 06:43:57 +04:00
|
|
|
|
2014-06-03 19:35:20 +04:00
|
|
|
Cu.import("resource://gre/modules/AppsUtils.jsm");
|
2014-06-03 22:32:10 +04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-08-28 06:43:57 +04:00
|
|
|
|
|
|
|
function debug(s) {
|
|
|
|
//dump("-*- AppsServiceChild.jsm: " + s + "\n");
|
|
|
|
}
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
const APPS_IPC_MSG_NAMES = [
|
|
|
|
"Webapps:AddApp",
|
|
|
|
"Webapps:RemoveApp",
|
|
|
|
"Webapps:UpdateApp",
|
|
|
|
"Webapps:CheckForUpdate:Return:KO",
|
|
|
|
"Webapps:FireEvent",
|
|
|
|
"Webapps:UpdateState"
|
|
|
|
];
|
|
|
|
|
|
|
|
// A simple cache for the wrapped manifests.
|
|
|
|
this.WrappedManifestCache = {
|
|
|
|
_cache: { },
|
|
|
|
|
|
|
|
// Gets an entry from the cache, and populates the cache if needed.
|
|
|
|
get: function mcache_get(aManifestURL, aManifest, aWindow, aInnerWindowID) {
|
|
|
|
if (!aManifest) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(aManifestURL in this._cache)) {
|
|
|
|
this._cache[aManifestURL] = { };
|
|
|
|
}
|
|
|
|
|
|
|
|
let winObjs = this._cache[aManifestURL];
|
|
|
|
if (!(aInnerWindowID in winObjs)) {
|
|
|
|
winObjs[aInnerWindowID] = Cu.cloneInto(aManifest, aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
return winObjs[aInnerWindowID];
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalidates an entry in the cache.
|
|
|
|
evict: function mcache_evict(aManifestURL, aInnerWindowID) {
|
|
|
|
debug("Evicting manifest " + aManifestURL + " window ID " +
|
|
|
|
aInnerWindowID);
|
|
|
|
if (aManifestURL in this._cache) {
|
|
|
|
let winObjs = this._cache[aManifestURL];
|
|
|
|
if (aInnerWindowID in winObjs) {
|
|
|
|
delete winObjs[aInnerWindowID];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(winObjs).length == 0) {
|
|
|
|
delete this._cache[aManifestURL];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
|
|
|
// Clear the cache on memory pressure.
|
|
|
|
this._cache = { };
|
|
|
|
Cu.forceGC();
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
Services.obs.addObserver(this, "memory-pressure", false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.WrappedManifestCache.init();
|
|
|
|
|
|
|
|
|
|
|
|
// DOMApplicationRegistry keeps a cache containing a list of apps in the device.
|
|
|
|
// This information is updated with the data received from the main process and
|
|
|
|
// it is queried by the DOM objects to set their state.
|
|
|
|
// This module handle all the messages broadcasted from the parent process,
|
|
|
|
// including DOM events, which are dispatched to the corresponding DOM objects.
|
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.DOMApplicationRegistry = {
|
2014-05-24 23:28:13 +04:00
|
|
|
// DOMApps will hold a list of arrays of weak references to
|
|
|
|
// mozIDOMApplication objects indexed by manifest URL.
|
|
|
|
DOMApps: {},
|
|
|
|
|
|
|
|
ready: false,
|
|
|
|
webapps: null,
|
|
|
|
|
2012-08-28 06:43:57 +04:00
|
|
|
init: function init() {
|
|
|
|
this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsISyncMessageSender);
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
APPS_IPC_MSG_NAMES.forEach((function(aMsgName) {
|
2012-08-28 06:43:57 +04:00
|
|
|
this.cpmm.addMessageListener(aMsgName, this);
|
|
|
|
}).bind(this));
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
this.cpmm.sendAsyncMessage("Webapps:RegisterForMessages", {
|
|
|
|
messages: APPS_IPC_MSG_NAMES
|
|
|
|
});
|
2014-07-28 22:31:33 +04:00
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
// We need to prime the cache with the list of apps.
|
|
|
|
let list = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0];
|
2016-03-03 20:58:47 +03:00
|
|
|
this.webapps = list ? list.webapps : { };
|
2013-01-31 10:50:28 +04:00
|
|
|
// We need a fast mapping from localId -> app, so we add an index.
|
2014-05-24 23:28:13 +04:00
|
|
|
// We also add the manifest to the app object.
|
2013-01-31 10:50:28 +04:00
|
|
|
this.localIdIndex = { };
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
let app = this.webapps[id];
|
|
|
|
this.localIdIndex[app.localId] = app;
|
2014-05-24 23:28:13 +04:00
|
|
|
app.manifest = list.manifests[id];
|
2013-01-31 10:50:28 +04:00
|
|
|
}
|
2015-05-19 09:31:25 +03:00
|
|
|
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
2012-08-29 18:31:10 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2014-05-24 23:28:13 +04:00
|
|
|
// cpmm.addMessageListener causes the DOMApplicationRegistry object to
|
|
|
|
// live forever if we don't clean up properly.
|
2012-08-29 18:31:10 +04:00
|
|
|
this.webapps = null;
|
2014-05-24 23:28:13 +04:00
|
|
|
this.DOMApps = null;
|
|
|
|
|
|
|
|
APPS_IPC_MSG_NAMES.forEach((aMsgName) => {
|
2012-08-29 18:31:10 +04:00
|
|
|
this.cpmm.removeMessageListener(aMsgName, this);
|
2014-05-24 23:28:13 +04:00
|
|
|
});
|
2012-08-28 06:43:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function receiveMessage(aMessage) {
|
|
|
|
debug("Received " + aMessage.name + " message.");
|
2014-05-24 23:28:13 +04:00
|
|
|
let msg = aMessage.data;
|
2012-08-28 06:43:57 +04:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case "Webapps:AddApp":
|
|
|
|
this.webapps[msg.id] = msg.app;
|
2013-01-31 10:50:28 +04:00
|
|
|
this.localIdIndex[msg.app.localId] = msg.app;
|
2014-05-24 23:28:13 +04:00
|
|
|
if (msg.manifest) {
|
|
|
|
this.webapps[msg.id].manifest = msg.manifest;
|
|
|
|
}
|
2012-08-28 06:43:57 +04:00
|
|
|
break;
|
|
|
|
case "Webapps:RemoveApp":
|
2014-05-24 23:28:13 +04:00
|
|
|
delete this.DOMApps[this.webapps[msg.id].manifestURL];
|
2013-01-31 10:50:28 +04:00
|
|
|
delete this.localIdIndex[this.webapps[msg.id].localId];
|
2012-08-28 06:43:57 +04:00
|
|
|
delete this.webapps[msg.id];
|
|
|
|
break;
|
2014-05-24 23:28:13 +04:00
|
|
|
case "Webapps:UpdateApp":
|
|
|
|
let app = this.webapps[msg.oldId];
|
|
|
|
if (!app) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.app) {
|
|
|
|
for (let prop in msg.app) {
|
|
|
|
app[prop] = msg.app[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.webapps[msg.newId] = app;
|
|
|
|
this.localIdIndex[app.localId] = app;
|
|
|
|
delete this.webapps[msg.oldId];
|
|
|
|
|
|
|
|
let apps = this.DOMApps[msg.app.manifestURL];
|
|
|
|
if (!apps) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < apps.length; i++) {
|
|
|
|
let domApp = apps[i].get();
|
2014-07-15 01:27:57 +04:00
|
|
|
if (!domApp || domApp._window === null) {
|
|
|
|
apps.splice(i, 1);
|
2014-05-24 23:28:13 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
domApp._proxy = new Proxy(domApp, {
|
|
|
|
get: function(target, prop) {
|
|
|
|
if (!DOMApplicationRegistry.webapps[msg.newId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return DOMApplicationRegistry.webapps[msg.newId][prop];
|
|
|
|
},
|
|
|
|
set: function(target, prop, val) {
|
|
|
|
if (!DOMApplicationRegistry.webapps[msg.newId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DOMApplicationRegistry.webapps[msg.newId][prop] = val;
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Webapps:FireEvent":
|
|
|
|
this._fireEvent(aMessage);
|
|
|
|
break;
|
|
|
|
case "Webapps:UpdateState":
|
|
|
|
this._updateState(msg);
|
|
|
|
break;
|
|
|
|
case "Webapps:CheckForUpdate:Return:KO":
|
|
|
|
let DOMApps = this.DOMApps[msg.manifestURL];
|
|
|
|
if (!DOMApps || !msg.requestID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DOMApps.forEach((DOMApp) => {
|
|
|
|
let domApp = DOMApp.get();
|
|
|
|
if (domApp && msg.requestID) {
|
|
|
|
domApp._fireRequestResult(aMessage, true /* aIsError */);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* mozIDOMApplication management
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Every time a DOM app is created, we save a weak reference to it that will
|
|
|
|
// be used to dispatch events and fire request results.
|
|
|
|
addDOMApp: function(aApp, aManifestURL, aId) {
|
|
|
|
let weakRef = Cu.getWeakReference(aApp);
|
|
|
|
|
|
|
|
if (!this.DOMApps[aManifestURL]) {
|
|
|
|
this.DOMApps[aManifestURL] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let apps = this.DOMApps[aManifestURL];
|
|
|
|
|
|
|
|
// Get rid of dead weak references.
|
|
|
|
for (let i = 0; i < apps.length; i++) {
|
2014-07-15 01:27:57 +04:00
|
|
|
let app = apps[i].get();
|
|
|
|
if (!app || app._window === null) {
|
|
|
|
apps.splice(i, 1);
|
2014-05-24 23:28:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
apps.push(weakRef);
|
|
|
|
|
|
|
|
// Each DOM app contains a proxy object used to build their state. We
|
|
|
|
// return the handler for this proxy object with traps to get and set
|
|
|
|
// app properties kept in the DOMApplicationRegistry app cache.
|
|
|
|
return {
|
|
|
|
get: function(target, prop) {
|
|
|
|
if (!DOMApplicationRegistry.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-19 20:30:54 +04:00
|
|
|
|
|
|
|
if (prop in DOMApplicationRegistry.webapps[aId]) {
|
|
|
|
return DOMApplicationRegistry.webapps[aId][prop];
|
|
|
|
}
|
|
|
|
return null;
|
2014-05-24 23:28:13 +04:00
|
|
|
},
|
|
|
|
set: function(target, prop, val) {
|
|
|
|
if (!DOMApplicationRegistry.webapps[aId]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DOMApplicationRegistry.webapps[aId][prop] = val;
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireEvent: function(aMessage) {
|
|
|
|
let msg = aMessage.data;
|
|
|
|
debug("_fireEvent " + JSON.stringify(msg));
|
|
|
|
if (!this.DOMApps || !msg.manifestURL || !msg.eventType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let DOMApps = this.DOMApps[msg.manifestURL];
|
|
|
|
if (!DOMApps) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parent might ask childs to trigger more than one event in one
|
|
|
|
// shot, so in order to avoid needless IPC we allow an array for the
|
|
|
|
// 'eventType' IPC message field.
|
|
|
|
if (!Array.isArray(msg.eventType)) {
|
|
|
|
msg.eventType = [msg.eventType];
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMApps.forEach((DOMApp) => {
|
|
|
|
let domApp = DOMApp.get();
|
|
|
|
if (!domApp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msg.eventType.forEach((aEventType) => {
|
|
|
|
if ('on' + aEventType in domApp) {
|
|
|
|
domApp._fireEvent(aEventType);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (msg.requestID) {
|
|
|
|
aMessage.data.result = msg.manifestURL;
|
|
|
|
domApp._fireRequestResult(aMessage);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateState: function(aMessage) {
|
|
|
|
if (!this.DOMApps || !aMessage.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let app = this.webapps[aMessage.id];
|
|
|
|
if (!app) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMessage.app) {
|
|
|
|
for (let prop in aMessage.app) {
|
|
|
|
app[prop] = aMessage.app[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("error" in aMessage) {
|
|
|
|
app.downloadError = aMessage.error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMessage.manifest) {
|
|
|
|
app.manifest = aMessage.manifest;
|
|
|
|
// Evict the wrapped manifest cache for all the affected DOM objects.
|
|
|
|
let DOMApps = this.DOMApps[app.manifestURL];
|
|
|
|
if (!DOMApps) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DOMApps.forEach((DOMApp) => {
|
|
|
|
let domApp = DOMApp.get();
|
|
|
|
if (!domApp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WrappedManifestCache.evict(app.manifestURL, domApp.innerWindowID);
|
|
|
|
});
|
2012-08-28 06:43:57 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-07-07 22:01:30 +04:00
|
|
|
getAll: function(aCallback) {
|
|
|
|
debug("getAll()\n");
|
|
|
|
if (!aCallback || typeof aCallback !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = [];
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
res.push(this.webapps[id]);
|
|
|
|
}
|
|
|
|
aCallback(res);
|
|
|
|
},
|
|
|
|
|
2015-01-11 02:00:27 +03:00
|
|
|
getAdditionalLanguages: function(aManifestURL) {
|
|
|
|
for (let id in this.webapps) {
|
|
|
|
if (this.webapps[id].manifestURL == aManifestURL) {
|
|
|
|
return this.webapps[id].additionalLanguages || {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
2014-05-24 23:28:13 +04:00
|
|
|
/**
|
|
|
|
* nsIAppsService API
|
|
|
|
*/
|
2012-08-28 06:43:57 +04:00
|
|
|
getAppByManifestURL: function getAppByManifestURL(aManifestURL) {
|
|
|
|
debug("getAppByManifestURL " + aManifestURL);
|
|
|
|
return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) {
|
|
|
|
debug("getAppLocalIdByManifestURL " + aManifestURL);
|
|
|
|
return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL);
|
|
|
|
},
|
|
|
|
|
2014-09-17 00:14:02 +04:00
|
|
|
getManifestCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getManifestCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getManifestCSPByLocalId(this.webapps, aLocalId);
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultCSPByLocalId: function(aLocalId) {
|
|
|
|
debug("getDefaultCSPByLocalId:" + aLocalId);
|
|
|
|
return AppsUtils.getDefaultCSPByLocalId(this.webapps, aLocalId);
|
2012-10-19 14:43:17 +04:00
|
|
|
},
|
|
|
|
|
2013-01-25 13:57:14 +04:00
|
|
|
getAppLocalIdByStoreId: function(aStoreId) {
|
|
|
|
debug("getAppLocalIdByStoreId:" + aStoreId);
|
|
|
|
return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId);
|
|
|
|
},
|
|
|
|
|
2012-08-28 06:43:57 +04:00
|
|
|
getAppByLocalId: function getAppByLocalId(aLocalId) {
|
2014-05-24 23:28:13 +04:00
|
|
|
debug("getAppByLocalId " + aLocalId + " - ready: " + this.ready);
|
2013-01-31 10:50:28 +04:00
|
|
|
let app = this.localIdIndex[aLocalId];
|
|
|
|
if (!app) {
|
|
|
|
debug("Ouch, No app!");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:30:15 +04:00
|
|
|
return new mozIApplication(app);
|
2012-08-28 06:43:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
|
|
|
|
debug("getManifestURLByLocalId " + aLocalId);
|
|
|
|
return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
|
2012-08-31 18:34:28 +04:00
|
|
|
},
|
|
|
|
|
2012-12-22 17:56:21 +04:00
|
|
|
getCoreAppsBasePath: function getCoreAppsBasePath() {
|
|
|
|
debug("getCoreAppsBasePath() not yet supported on child!");
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getWebAppsBasePath: function getWebAppsBasePath() {
|
|
|
|
debug("getWebAppsBasePath() not yet supported on child!");
|
|
|
|
return null;
|
2013-02-01 01:35:13 +04:00
|
|
|
},
|
|
|
|
|
2016-02-18 07:57:41 +03:00
|
|
|
areAnyAppsInstalled: function() {
|
|
|
|
return AppsUtils.areAnyAppsInstalled(this.webapps);
|
|
|
|
},
|
|
|
|
|
2013-02-01 01:35:13 +04:00
|
|
|
getAppInfo: function getAppInfo(aAppId) {
|
2013-03-15 18:18:58 +04:00
|
|
|
return AppsUtils.getAppInfo(this.webapps, aAppId);
|
|
|
|
}
|
2012-08-28 06:43:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMApplicationRegistry.init();
|