michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Cu = Components.utils; michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: // This module exposes a subset of the functionnalities of the parent DOM michael@0: // Registry to content processes, to be be used from the AppsService component. michael@0: michael@0: this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"]; michael@0: michael@0: Cu.import("resource://gre/modules/AppsUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function debug(s) { michael@0: //dump("-*- AppsServiceChild.jsm: " + s + "\n"); michael@0: } michael@0: michael@0: this.DOMApplicationRegistry = { michael@0: init: function init() { michael@0: debug("init"); michael@0: this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsISyncMessageSender); michael@0: michael@0: ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) { michael@0: this.cpmm.addMessageListener(aMsgName, this); michael@0: }).bind(this)); michael@0: michael@0: // We need to prime the cache with the list of apps. michael@0: // XXX shoud we do this async and block callers if it's not yet there? michael@0: this.webapps = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0]; michael@0: michael@0: // We need a fast mapping from localId -> app, so we add an index. michael@0: this.localIdIndex = { }; michael@0: for (let id in this.webapps) { michael@0: let app = this.webapps[id]; michael@0: this.localIdIndex[app.localId] = app; michael@0: } michael@0: michael@0: Services.obs.addObserver(this, "xpcom-shutdown", false); michael@0: }, michael@0: michael@0: observe: function(aSubject, aTopic, aData) { michael@0: // cpmm.addMessageListener causes the DOMApplicationRegistry object to live michael@0: // forever if we don't clean up properly. michael@0: this.webapps = null; michael@0: ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) { michael@0: this.cpmm.removeMessageListener(aMsgName, this); michael@0: }).bind(this)); michael@0: }, michael@0: michael@0: receiveMessage: function receiveMessage(aMessage) { michael@0: debug("Received " + aMessage.name + " message."); michael@0: let msg = aMessage.json; michael@0: switch (aMessage.name) { michael@0: case "Webapps:AddApp": michael@0: this.webapps[msg.id] = msg.app; michael@0: this.localIdIndex[msg.app.localId] = msg.app; michael@0: break; michael@0: case "Webapps:RemoveApp": michael@0: delete this.localIdIndex[this.webapps[msg.id].localId]; michael@0: delete this.webapps[msg.id]; michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: getAppByManifestURL: function getAppByManifestURL(aManifestURL) { michael@0: debug("getAppByManifestURL " + aManifestURL); michael@0: return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL); michael@0: }, michael@0: michael@0: getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) { michael@0: debug("getAppLocalIdByManifestURL " + aManifestURL); michael@0: return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL); michael@0: }, michael@0: michael@0: getCSPByLocalId: function(aLocalId) { michael@0: debug("getCSPByLocalId:" + aLocalId); michael@0: return AppsUtils.getCSPByLocalId(this.webapps, aLocalId); michael@0: }, michael@0: michael@0: getAppLocalIdByStoreId: function(aStoreId) { michael@0: debug("getAppLocalIdByStoreId:" + aStoreId); michael@0: return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId); michael@0: }, michael@0: michael@0: getAppByLocalId: function getAppByLocalId(aLocalId) { michael@0: debug("getAppByLocalId " + aLocalId); michael@0: let app = this.localIdIndex[aLocalId]; michael@0: if (!app) { michael@0: debug("Ouch, No app!"); michael@0: return null; michael@0: } michael@0: michael@0: return new mozIApplication(app); michael@0: }, michael@0: michael@0: getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) { michael@0: debug("getManifestURLByLocalId " + aLocalId); michael@0: return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId); michael@0: }, michael@0: michael@0: getCoreAppsBasePath: function getCoreAppsBasePath() { michael@0: debug("getCoreAppsBasePath() not yet supported on child!"); michael@0: return null; michael@0: }, michael@0: michael@0: getWebAppsBasePath: function getWebAppsBasePath() { michael@0: debug("getWebAppsBasePath() not yet supported on child!"); michael@0: return null; michael@0: }, michael@0: michael@0: getAppInfo: function getAppInfo(aAppId) { michael@0: return AppsUtils.getAppInfo(this.webapps, aAppId); michael@0: } michael@0: } michael@0: michael@0: DOMApplicationRegistry.init();