webapprt/WebappRT.jsm

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

     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/AppsUtils.jsm");
    15 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
    16   "resource://gre/modules/FileUtils.jsm");
    18 XPCOMUtils.defineLazyModuleGetter(this, "OS",
    19   "resource://gre/modules/osfile.jsm");
    21 XPCOMUtils.defineLazyModuleGetter(this, "Task",
    22   "resource://gre/modules/Task.jsm");
    24 XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
    25   'resource://gre/modules/NativeApp.jsm');
    27 XPCOMUtils.defineLazyServiceGetter(this, "appsService",
    28                                   "@mozilla.org/AppsService;1",
    29                                   "nsIAppsService");
    31 this.WebappRT = {
    32   get launchURI() {
    33     return this.localeManifest.fullLaunchPath();
    34   },
    36   get localeManifest() {
    37     return new ManifestHelper(this.config.app.manifest,
    38                               this.config.app.origin);
    39   },
    41   get appID() {
    42     let manifestURL = WebappRT.config.app.manifestURL;
    43     if (!manifestURL) {
    44       return Ci.nsIScriptSecurityManager.NO_APP_ID;
    45     }
    47     return appsService.getAppLocalIdByManifestURL(manifestURL);
    48   },
    50   loadConfig: function() {
    51     if (this.config) {
    52       return;
    53     }
    55     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    56                                   "webapp.json");
    57     this.config = yield AppsUtils.loadJSONAsync(webappJson);
    58   },
    60   isUpdatePending: Task.async(function*() {
    61     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    62                                   "update", "webapp.json");
    64     if (!(yield OS.File.exists(webappJson))) {
    65       return false;
    66     }
    68     return true;
    69   }),
    71   applyUpdate: Task.async(function*() {
    72     let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    73                                   "update", "webapp.json");
    74     let config = yield AppsUtils.loadJSONAsync(webappJson);
    76     let nativeApp = new NativeApp(config.app, config.app.manifest,
    77                                   config.app.categories,
    78                                   config.registryDir);
    79     try {
    80       yield nativeApp.applyUpdate();
    81     } catch (ex) {
    82       return false;
    83     }
    85     // The update has been applied successfully, the new config file
    86     // is the config file that was in the update directory.
    87     this.config = config;
    89     return true;
    90   }),
    92   startUpdateService: function() {
    93     let manifestURL = WebappRT.config.app.manifestURL;
    94     // We used to install apps without storing their manifest URL.
    95     // Now we can't update them.
    96     if (!manifestURL) {
    97       return;
    98     }
   100     // Check for updates once a day.
   101     let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
   102                        getService(Ci.nsIUpdateTimerManager);
   103     timerManager.registerTimer("updateTimer", () => {
   104       let window = Services.wm.getMostRecentWindow("webapprt:webapp");
   105       window.navigator.mozApps.mgmt.getAll().onsuccess = function() {
   106         let thisApp = null;
   107         for (let app of this.result) {
   108           if (app.manifestURL == manifestURL) {
   109             thisApp = app;
   110             break;
   111           }
   112         }
   114         // This shouldn't happen if the app is installed.
   115         if (!thisApp) {
   116           Cu.reportError("Couldn't find the app in the webapps registry");
   117           return;
   118         }
   120         thisApp.ondownloadavailable = () => {
   121           // Download available, download it!
   122           thisApp.download();
   123         };
   125         thisApp.ondownloadsuccess = () => {
   126           // Update downloaded, apply it!
   127           window.navigator.mozApps.mgmt.applyDownload(thisApp);
   128         };
   130         thisApp.ondownloadapplied = () => {
   131           // Application updated, nothing to do.
   132         };
   134         thisApp.checkForUpdate();
   135       }
   136     }, 24 * 60 * 60);
   137   },
   138 };

mercurial