dom/apps/src/AppsServiceChild.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/src/AppsServiceChild.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const Cu = Components.utils;
    1.11 +const Cc = Components.classes;
    1.12 +const Ci = Components.interfaces;
    1.13 +
    1.14 +// This module exposes a subset of the functionnalities of the parent DOM
    1.15 +// Registry to content processes, to be be used from the AppsService component.
    1.16 +
    1.17 +this.EXPORTED_SYMBOLS = ["DOMApplicationRegistry"];
    1.18 +
    1.19 +Cu.import("resource://gre/modules/AppsUtils.jsm");
    1.20 +Cu.import("resource://gre/modules/Services.jsm");
    1.21 +
    1.22 +function debug(s) {
    1.23 +  //dump("-*- AppsServiceChild.jsm: " + s + "\n");
    1.24 +}
    1.25 +
    1.26 +this.DOMApplicationRegistry = {
    1.27 +  init: function init() {
    1.28 +    debug("init");
    1.29 +    this.cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
    1.30 +                  .getService(Ci.nsISyncMessageSender);
    1.31 +
    1.32 +    ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) {
    1.33 +      this.cpmm.addMessageListener(aMsgName, this);
    1.34 +    }).bind(this));
    1.35 +
    1.36 +    // We need to prime the cache with the list of apps.
    1.37 +    // XXX shoud we do this async and block callers if it's not yet there?
    1.38 +    this.webapps = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0];
    1.39 +
    1.40 +    // We need a fast mapping from localId -> app, so we add an index.
    1.41 +    this.localIdIndex = { };
    1.42 +    for (let id in this.webapps) {
    1.43 +      let app = this.webapps[id];
    1.44 +      this.localIdIndex[app.localId] = app;
    1.45 +    }
    1.46 +
    1.47 +    Services.obs.addObserver(this, "xpcom-shutdown", false);
    1.48 +  },
    1.49 +
    1.50 +  observe: function(aSubject, aTopic, aData) {
    1.51 +    // cpmm.addMessageListener causes the DOMApplicationRegistry object to live
    1.52 +    // forever if we don't clean up properly.
    1.53 +    this.webapps = null;
    1.54 +    ["Webapps:AddApp", "Webapps:RemoveApp"].forEach((function(aMsgName) {
    1.55 +      this.cpmm.removeMessageListener(aMsgName, this);
    1.56 +    }).bind(this));
    1.57 +  },
    1.58 +
    1.59 +  receiveMessage: function receiveMessage(aMessage) {
    1.60 +    debug("Received " + aMessage.name + " message.");
    1.61 +    let msg = aMessage.json;
    1.62 +    switch (aMessage.name) {
    1.63 +      case "Webapps:AddApp":
    1.64 +        this.webapps[msg.id] = msg.app;
    1.65 +        this.localIdIndex[msg.app.localId] = msg.app;
    1.66 +        break;
    1.67 +      case "Webapps:RemoveApp":
    1.68 +        delete this.localIdIndex[this.webapps[msg.id].localId];
    1.69 +        delete this.webapps[msg.id];
    1.70 +        break;
    1.71 +    }
    1.72 +  },
    1.73 +
    1.74 +  getAppByManifestURL: function getAppByManifestURL(aManifestURL) {
    1.75 +    debug("getAppByManifestURL " + aManifestURL);
    1.76 +    return AppsUtils.getAppByManifestURL(this.webapps, aManifestURL);
    1.77 +  },
    1.78 +
    1.79 +  getAppLocalIdByManifestURL: function getAppLocalIdByManifestURL(aManifestURL) {
    1.80 +    debug("getAppLocalIdByManifestURL " + aManifestURL);
    1.81 +    return AppsUtils.getAppLocalIdByManifestURL(this.webapps, aManifestURL);
    1.82 +  },
    1.83 +
    1.84 +  getCSPByLocalId: function(aLocalId) {
    1.85 +    debug("getCSPByLocalId:" + aLocalId);
    1.86 +    return AppsUtils.getCSPByLocalId(this.webapps, aLocalId);
    1.87 +  },
    1.88 +
    1.89 +  getAppLocalIdByStoreId: function(aStoreId) {
    1.90 +    debug("getAppLocalIdByStoreId:" + aStoreId);
    1.91 +    return AppsUtils.getAppLocalIdByStoreId(this.webapps, aStoreId);
    1.92 +  },
    1.93 +
    1.94 +  getAppByLocalId: function getAppByLocalId(aLocalId) {
    1.95 +    debug("getAppByLocalId " + aLocalId);
    1.96 +    let app = this.localIdIndex[aLocalId];
    1.97 +    if (!app) {
    1.98 +      debug("Ouch, No app!");
    1.99 +      return null;
   1.100 +    }
   1.101 +
   1.102 +    return new mozIApplication(app);
   1.103 +  },
   1.104 +
   1.105 +  getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {
   1.106 +    debug("getManifestURLByLocalId " + aLocalId);
   1.107 +    return AppsUtils.getManifestURLByLocalId(this.webapps, aLocalId);
   1.108 +  },
   1.109 +
   1.110 +  getCoreAppsBasePath: function getCoreAppsBasePath() {
   1.111 +    debug("getCoreAppsBasePath() not yet supported on child!");
   1.112 +    return null;
   1.113 +  },
   1.114 +
   1.115 +  getWebAppsBasePath: function getWebAppsBasePath() {
   1.116 +    debug("getWebAppsBasePath() not yet supported on child!");
   1.117 +    return null;
   1.118 +  },
   1.119 +
   1.120 +  getAppInfo: function getAppInfo(aAppId) {
   1.121 +    return AppsUtils.getAppInfo(this.webapps, aAppId);
   1.122 +  }
   1.123 +}
   1.124 +
   1.125 +DOMApplicationRegistry.init();

mercurial