Bug 882543 - Actually run offline MSG offline. r=roc
[gecko.git] / webapprt / WebappRT.jsm
blob3a920908eeee764f9bb55a4148c8e924dd9327b4
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 this.EXPORTED_SYMBOLS = ["WebappRT"];
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
12 Cu.import("resource://gre/modules/Services.jsm");
14 XPCOMUtils.defineLazyGetter(this, "FileUtils", function() {
15   Cu.import("resource://gre/modules/FileUtils.jsm");
16   return FileUtils;
17 });
19 XPCOMUtils.defineLazyGetter(this, "DOMApplicationRegistry", function() {
20   Cu.import("resource://gre/modules/Webapps.jsm");
21   return DOMApplicationRegistry;
22 });
24 this.WebappRT = {
25   _config: null,
27   get config() {
28     if (this._config)
29       return this._config;
31     let config;
32     let webappFile = FileUtils.getFile("AppRegD", ["webapp.json"]);
34     let inputStream = Cc["@mozilla.org/network/file-input-stream;1"].
35                       createInstance(Ci.nsIFileInputStream);
36     inputStream.init(webappFile, -1, 0, 0);
37     let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
38     config = json.decodeFromStream(inputStream, webappFile.fileSize);
40     return this._config = config;
41   },
43   // This exists to support test mode, which installs webapps after startup.
44   // Ideally we wouldn't have to have a setter, as tests can just delete
45   // the getter and then set the property.  But the object to which they set it
46   // will have a reference to its global object, so our reference to it
47   // will leak that object (per bug 780674).  The setter enables us to clone
48   // the new value so we don't actually retain a reference to it.
49   set config(newVal) {
50     this._config = JSON.parse(JSON.stringify(newVal));
51   },
53   get launchURI() {
54     let url = Services.io.newURI(this.config.app.origin, null, null);
55     if (this.config.app.manifest.launch_path) {
56       url = Services.io.newURI(this.config.app.manifest.launch_path, null, url);
57     }
58     return url;
59   }