michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["WebappRT"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/AppsUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", michael@0: "resource://gre/modules/FileUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "OS", michael@0: "resource://gre/modules/osfile.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Task", michael@0: "resource://gre/modules/Task.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp', michael@0: 'resource://gre/modules/NativeApp.jsm'); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "appsService", michael@0: "@mozilla.org/AppsService;1", michael@0: "nsIAppsService"); michael@0: michael@0: this.WebappRT = { michael@0: get launchURI() { michael@0: return this.localeManifest.fullLaunchPath(); michael@0: }, michael@0: michael@0: get localeManifest() { michael@0: return new ManifestHelper(this.config.app.manifest, michael@0: this.config.app.origin); michael@0: }, michael@0: michael@0: get appID() { michael@0: let manifestURL = WebappRT.config.app.manifestURL; michael@0: if (!manifestURL) { michael@0: return Ci.nsIScriptSecurityManager.NO_APP_ID; michael@0: } michael@0: michael@0: return appsService.getAppLocalIdByManifestURL(manifestURL); michael@0: }, michael@0: michael@0: loadConfig: function() { michael@0: if (this.config) { michael@0: return; michael@0: } michael@0: michael@0: let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, michael@0: "webapp.json"); michael@0: this.config = yield AppsUtils.loadJSONAsync(webappJson); michael@0: }, michael@0: michael@0: isUpdatePending: Task.async(function*() { michael@0: let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, michael@0: "update", "webapp.json"); michael@0: michael@0: if (!(yield OS.File.exists(webappJson))) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: }), michael@0: michael@0: applyUpdate: Task.async(function*() { michael@0: let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path, michael@0: "update", "webapp.json"); michael@0: let config = yield AppsUtils.loadJSONAsync(webappJson); michael@0: michael@0: let nativeApp = new NativeApp(config.app, config.app.manifest, michael@0: config.app.categories, michael@0: config.registryDir); michael@0: try { michael@0: yield nativeApp.applyUpdate(); michael@0: } catch (ex) { michael@0: return false; michael@0: } michael@0: michael@0: // The update has been applied successfully, the new config file michael@0: // is the config file that was in the update directory. michael@0: this.config = config; michael@0: michael@0: return true; michael@0: }), michael@0: michael@0: startUpdateService: function() { michael@0: let manifestURL = WebappRT.config.app.manifestURL; michael@0: // We used to install apps without storing their manifest URL. michael@0: // Now we can't update them. michael@0: if (!manifestURL) { michael@0: return; michael@0: } michael@0: michael@0: // Check for updates once a day. michael@0: let timerManager = Cc["@mozilla.org/updates/timer-manager;1"]. michael@0: getService(Ci.nsIUpdateTimerManager); michael@0: timerManager.registerTimer("updateTimer", () => { michael@0: let window = Services.wm.getMostRecentWindow("webapprt:webapp"); michael@0: window.navigator.mozApps.mgmt.getAll().onsuccess = function() { michael@0: let thisApp = null; michael@0: for (let app of this.result) { michael@0: if (app.manifestURL == manifestURL) { michael@0: thisApp = app; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // This shouldn't happen if the app is installed. michael@0: if (!thisApp) { michael@0: Cu.reportError("Couldn't find the app in the webapps registry"); michael@0: return; michael@0: } michael@0: michael@0: thisApp.ondownloadavailable = () => { michael@0: // Download available, download it! michael@0: thisApp.download(); michael@0: }; michael@0: michael@0: thisApp.ondownloadsuccess = () => { michael@0: // Update downloaded, apply it! michael@0: window.navigator.mozApps.mgmt.applyDownload(thisApp); michael@0: }; michael@0: michael@0: thisApp.ondownloadapplied = () => { michael@0: // Application updated, nothing to do. michael@0: }; michael@0: michael@0: thisApp.checkForUpdate(); michael@0: } michael@0: }, 24 * 60 * 60); michael@0: }, michael@0: };