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");
13 Cu.import("resource://gre/modules/Promise.jsm");
14 Cu.import("resource://gre/modules/AppsUtils.jsm");
16 XPCOMUtils.defineLazyModuleGetter(this, "OS",
17 "resource://gre/modules/osfile.jsm");
19 XPCOMUtils.defineLazyModuleGetter(this, "Task",
20 "resource://gre/modules/Task.jsm");
22 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
23 'resource://gre/modules/NativeApp.jsm');
25 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
26 "@mozilla.org/AppsService;1",
33 if (!this._configPromise) {
34 this._configPromise = Task.spawn(function*() {
35 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
38 WebappRT.config = yield AppsUtils.loadJSONAsync(webappJson);
42 return this._configPromise;
46 return this.localeManifest.fullLaunchPath();
49 get localeManifest() {
50 return new ManifestHelper(this.config.app.manifest,
51 this.config.app.origin,
52 this.config.app.manifestURL);
56 let manifestURL = this.config.app.manifestURL;
58 return Ci.nsIScriptSecurityManager.NO_APP_ID;
61 return appsService.getAppLocalIdByManifestURL(manifestURL);
64 isUpdatePending: Task.async(function*() {
65 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
66 "update", "webapp.json");
68 if (!(yield OS.File.exists(webappJson))) {
75 applyUpdate: Task.async(function*() {
76 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
77 "update", "webapp.json");
78 let config = yield AppsUtils.loadJSONAsync(webappJson);
80 let nativeApp = new NativeApp(config.app, config.app.manifest,
81 config.app.categories,
84 yield nativeApp.applyUpdate(config.app);
89 // The update has been applied successfully, the new config file
90 // is the config file that was in the update directory.
92 this._configPromise = Promise.resolve();
97 startUpdateService: function() {
98 let manifestURL = this.config.app.manifestURL;
99 // We used to install apps without storing their manifest URL.
100 // Now we can't update them.
105 // Check for updates once a day.
106 let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
107 getService(Ci.nsIUpdateTimerManager);
108 timerManager.registerTimer("updateTimer", () => {
109 let window = Services.wm.getMostRecentWindow("webapprt:webapp");
110 window.navigator.mozApps.mgmt.getAll().onsuccess = function() {
112 for (let app of this.result) {
113 if (app.manifestURL == manifestURL) {
119 // This shouldn't happen if the app is installed.
121 Cu.reportError("Couldn't find the app in the webapps registry");
125 thisApp.ondownloadavailable = () => {
126 // Download available, download it!
130 thisApp.ondownloadsuccess = () => {
131 // Update downloaded, apply it!
132 window.navigator.mozApps.mgmt.applyDownload(thisApp);
135 thisApp.ondownloadapplied = () => {
136 // Application updated, nothing to do.
139 thisApp.checkForUpdate();
141 }, Services.prefs.getIntPref("webapprt.app_update_interval"));