dom/apps/src/AppsServiceChild.jsm

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 const Cu = Components.utils;
     8 const Cc = Components.classes;
     9 const Ci = Components.interfaces;
    11 // This module exposes a subset of the functionnalities of the parent DOM
    12 // Registry to content processes, to be be used from the AppsService component.
    14 this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"];
    16 Cu.import("resource://gre/modules/AppsUtils.jsm");
    17 Cu.import("resource://gre/modules/Services.jsm");
    19 function debug(s) {
    20   //dump("-*- AppsServiceChild.jsm: " + s + "\n");
    21 }
    23 this.DOMApplicationRegistry = {
    24   init: function init() {
    25     debug("init");
    26     this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
    27                   .getService(Ci.nsISyncMessageSender);
    29     ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) {
    30       this.cpmm.addMessageListener(aMsgName, this);
    31     }).bind(this));
    33     // We need to prime the cache with the list of apps.
    34     // XXX shoud we do this async and block callers if it's not yet there?
    35     this.webapps = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0];
    37     // We need a fast mapping from localId -> app, so we add an index.
    38     this.localIdIndex = { };
    39     for (let id in this.webapps) {
    40       let app = this.webapps[id];
    41       this.localIdIndex[app.localId] = app;
    42     }
    44     Services.obs.addObserver(this, "xpcom-shutdown", false);
    45   },
    47   observe: function(aSubject, aTopic, aData) {
    48     // cpmm.addMessageListener causes the DOMApplicationRegistry object to live
    49     // forever if we don't clean up properly.
    50     this.webapps = null;
    51     ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) {
    52       this.cpmm.removeMessageListener(aMsgName, this);
    53     }).bind(this));
    54   },
    56   receiveMessage: function receiveMessage(aMessage) {
    57     debug("Received " + aMessage.name + " message.");
    58     let msg = aMessage.json;
    59     switch (aMessage.name) {
    60       case "Webapps:AddApp":
    61         this.webapps[msg.id] = msg.app;
    62         this.localIdIndex[msg.app.localId] = msg.app;
    63         break;
    64       case "Webapps:RemoveApp":
    65         delete this.localIdIndex[this.webapps[msg.id].localId];
    66         delete this.webapps[msg.id];
    67         break;
    68     }
    69   },
    71   getAppByManifestURL: function getAppByManifestURL(aManifestURL) {
    72     debug("getAppByManifestURL " + aManifestURL);
    73     return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL);
    74   },
    76   getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) {
    77     debug("getAppLocalIdByManifestURL " + aManifestURL);
    78     return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL);
    79   },
    81   getCSPByLocalId: function(aLocalId) {
    82     debug("getCSPByLocalId:" + aLocalId);
    83     return AppsUtils.getCSPByLocalId(this.webapps, aLocalId);
    84   },
    86   getAppLocalIdByStoreId: function(aStoreId) {
    87     debug("getAppLocalIdByStoreId:" + aStoreId);
    88     return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId);
    89   },
    91   getAppByLocalId: function getAppByLocalId(aLocalId) {
    92     debug("getAppByLocalId " + aLocalId);
    93     let app = this.localIdIndex[aLocalId];
    94     if (!app) {
    95       debug("Ouch, No app!");
    96       return null;
    97     }
    99     return new mozIApplication(app);
   100   },
   102   getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
   103     debug("getManifestURLByLocalId " + aLocalId);
   104     return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
   105   },
   107   getCoreAppsBasePath: function getCoreAppsBasePath() {
   108     debug("getCoreAppsBasePath() not yet supported on child!");
   109     return null;
   110   },
   112   getWebAppsBasePath: function getWebAppsBasePath() {
   113     debug("getWebAppsBasePath() not yet supported on child!");
   114     return null;
   115   },
   117   getAppInfo: function getAppInfo(aAppId) {
   118     return AppsUtils.getAppInfo(this.webapps, aAppId);
   119   }
   120 }
   122 DOMApplicationRegistry.init();

mercurial