webapprt/WebappRT.jsm

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = ["WebappRT"];
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 12 Cu.import("resource://gre/modules/Services.jsm");
michael@0 13 Cu.import("resource://gre/modules/AppsUtils.jsm");
michael@0 14
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
michael@0 16 "resource://gre/modules/FileUtils.jsm");
michael@0 17
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "OS",
michael@0 19 "resource://gre/modules/osfile.jsm");
michael@0 20
michael@0 21 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 22 "resource://gre/modules/Task.jsm");
michael@0 23
michael@0 24 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
michael@0 25 'resource://gre/modules/NativeApp.jsm');
michael@0 26
michael@0 27 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
michael@0 28 "@mozilla.org/AppsService;1",
michael@0 29 "nsIAppsService");
michael@0 30
michael@0 31 this.WebappRT = {
michael@0 32 get launchURI() {
michael@0 33 return this.localeManifest.fullLaunchPath();
michael@0 34 },
michael@0 35
michael@0 36 get localeManifest() {
michael@0 37 return new ManifestHelper(this.config.app.manifest,
michael@0 38 this.config.app.origin);
michael@0 39 },
michael@0 40
michael@0 41 get appID() {
michael@0 42 let manifestURL = WebappRT.config.app.manifestURL;
michael@0 43 if (!manifestURL) {
michael@0 44 return Ci.nsIScriptSecurityManager.NO_APP_ID;
michael@0 45 }
michael@0 46
michael@0 47 return appsService.getAppLocalIdByManifestURL(manifestURL);
michael@0 48 },
michael@0 49
michael@0 50 loadConfig: function() {
michael@0 51 if (this.config) {
michael@0 52 return;
michael@0 53 }
michael@0 54
michael@0 55 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
michael@0 56 "webapp.json");
michael@0 57 this.config = yield AppsUtils.loadJSONAsync(webappJson);
michael@0 58 },
michael@0 59
michael@0 60 isUpdatePending: Task.async(function*() {
michael@0 61 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
michael@0 62 "update", "webapp.json");
michael@0 63
michael@0 64 if (!(yield OS.File.exists(webappJson))) {
michael@0 65 return false;
michael@0 66 }
michael@0 67
michael@0 68 return true;
michael@0 69 }),
michael@0 70
michael@0 71 applyUpdate: Task.async(function*() {
michael@0 72 let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
michael@0 73 "update", "webapp.json");
michael@0 74 let config = yield AppsUtils.loadJSONAsync(webappJson);
michael@0 75
michael@0 76 let nativeApp = new NativeApp(config.app, config.app.manifest,
michael@0 77 config.app.categories,
michael@0 78 config.registryDir);
michael@0 79 try {
michael@0 80 yield nativeApp.applyUpdate();
michael@0 81 } catch (ex) {
michael@0 82 return false;
michael@0 83 }
michael@0 84
michael@0 85 // The update has been applied successfully, the new config file
michael@0 86 // is the config file that was in the update directory.
michael@0 87 this.config = config;
michael@0 88
michael@0 89 return true;
michael@0 90 }),
michael@0 91
michael@0 92 startUpdateService: function() {
michael@0 93 let manifestURL = WebappRT.config.app.manifestURL;
michael@0 94 // We used to install apps without storing their manifest URL.
michael@0 95 // Now we can't update them.
michael@0 96 if (!manifestURL) {
michael@0 97 return;
michael@0 98 }
michael@0 99
michael@0 100 // Check for updates once a day.
michael@0 101 let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
michael@0 102 getService(Ci.nsIUpdateTimerManager);
michael@0 103 timerManager.registerTimer("updateTimer", () => {
michael@0 104 let window = Services.wm.getMostRecentWindow("webapprt:webapp");
michael@0 105 window.navigator.mozApps.mgmt.getAll().onsuccess = function() {
michael@0 106 let thisApp = null;
michael@0 107 for (let app of this.result) {
michael@0 108 if (app.manifestURL == manifestURL) {
michael@0 109 thisApp = app;
michael@0 110 break;
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 // This shouldn't happen if the app is installed.
michael@0 115 if (!thisApp) {
michael@0 116 Cu.reportError("Couldn't find the app in the webapps registry");
michael@0 117 return;
michael@0 118 }
michael@0 119
michael@0 120 thisApp.ondownloadavailable = () => {
michael@0 121 // Download available, download it!
michael@0 122 thisApp.download();
michael@0 123 };
michael@0 124
michael@0 125 thisApp.ondownloadsuccess = () => {
michael@0 126 // Update downloaded, apply it!
michael@0 127 window.navigator.mozApps.mgmt.applyDownload(thisApp);
michael@0 128 };
michael@0 129
michael@0 130 thisApp.ondownloadapplied = () => {
michael@0 131 // Application updated, nothing to do.
michael@0 132 };
michael@0 133
michael@0 134 thisApp.checkForUpdate();
michael@0 135 }
michael@0 136 }, 24 * 60 * 60);
michael@0 137 },
michael@0 138 };

mercurial