webapprt/WebappRT.jsm
author Robert Strong <robert.bugzilla@gmail.com>
Wed, 05 Mar 2014 11:42:56 -0800
changeset 171974 3b71d879000c73a5988d7010db70b9688d5204ea
parent 171039 8693eace6c33818c0a31f5db65e846ecdf64bae5
child 173684 4ce831d82c1fef2be4dadebfee2e9b2f315bc8ec
permissions -rw-r--r--
Bug 900251 - Add support for add-if-not instruction added by bug 759469 to the mar generation scripts. r=nthomas

/* 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/. */

this.EXPORTED_SYMBOLS = ["WebappRT"];

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
  "resource://gre/modules/FileUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "appsService",
                                  "@mozilla.org/AppsService;1",
                                  "nsIAppsService");

this.WebappRT = {
  _config: null,

  get config() {
    if (this._config)
      return this._config;

    let webappFile = FileUtils.getFile("AppRegD", ["webapp.json"]);

    let inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
                      createInstance(Ci.nsIFileInputStream);
    inputStream.init(webappFile, -1, 0, Ci.nsIFileInputStream.CLOSE_ON_EOF);
    let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
    let config = json.decodeFromStream(inputStream, webappFile.fileSize);

    return this._config = config;
  },

  // This exists to support test mode, which installs webapps after startup.
  // Ideally we wouldn't have to have a setter, as tests can just delete
  // the getter and then set the property.  But the object to which they set it
  // will have a reference to its global object, so our reference to it
  // will leak that object (per bug 780674).  The setter enables us to clone
  // the new value so we don't actually retain a reference to it.
  set config(newVal) {
    this._config = JSON.parse(JSON.stringify(newVal));
  },

  get launchURI() {
    let manifest = this.localeManifest;
    return manifest.fullLaunchPath();
  },

  get localeManifest() {
    return new ManifestHelper(this.config.app.manifest,
                              this.config.app.origin);
  },

  get appID() {
    let manifestURL = WebappRT.config.app.manifestURL;
    if (!manifestURL) {
      return Ci.nsIScriptSecurityManager.NO_APP_ID;
    }

    return appsService.getAppLocalIdByManifestURL(manifestURL);
  },
};