webapprt/WebappRT.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webapprt/WebappRT.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = ["WebappRT"];
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +Cu.import("resource://gre/modules/Services.jsm");
    1.16 +Cu.import("resource://gre/modules/AppsUtils.jsm");
    1.17 +
    1.18 +XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
    1.19 +  "resource://gre/modules/FileUtils.jsm");
    1.20 +
    1.21 +XPCOMUtils.defineLazyModuleGetter(this, "OS",
    1.22 +  "resource://gre/modules/osfile.jsm");
    1.23 +
    1.24 +XPCOMUtils.defineLazyModuleGetter(this, "Task",
    1.25 +  "resource://gre/modules/Task.jsm");
    1.26 +
    1.27 +XPCOMUtils.defineLazyModuleGetter(this, 'NativeApp',
    1.28 +  'resource://gre/modules/NativeApp.jsm');
    1.29 +
    1.30 +XPCOMUtils.defineLazyServiceGetter(this, "appsService",
    1.31 +                                  "@mozilla.org/AppsService;1",
    1.32 +                                  "nsIAppsService");
    1.33 +
    1.34 +this.WebappRT = {
    1.35 +  get launchURI() {
    1.36 +    return this.localeManifest.fullLaunchPath();
    1.37 +  },
    1.38 +
    1.39 +  get localeManifest() {
    1.40 +    return new ManifestHelper(this.config.app.manifest,
    1.41 +                              this.config.app.origin);
    1.42 +  },
    1.43 +
    1.44 +  get appID() {
    1.45 +    let manifestURL = WebappRT.config.app.manifestURL;
    1.46 +    if (!manifestURL) {
    1.47 +      return Ci.nsIScriptSecurityManager.NO_APP_ID;
    1.48 +    }
    1.49 +
    1.50 +    return appsService.getAppLocalIdByManifestURL(manifestURL);
    1.51 +  },
    1.52 +
    1.53 +  loadConfig: function() {
    1.54 +    if (this.config) {
    1.55 +      return;
    1.56 +    }
    1.57 +
    1.58 +    let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    1.59 +                                  "webapp.json");
    1.60 +    this.config = yield AppsUtils.loadJSONAsync(webappJson);
    1.61 +  },
    1.62 +
    1.63 +  isUpdatePending: Task.async(function*() {
    1.64 +    let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    1.65 +                                  "update", "webapp.json");
    1.66 +
    1.67 +    if (!(yield OS.File.exists(webappJson))) {
    1.68 +      return false;
    1.69 +    }
    1.70 +
    1.71 +    return true;
    1.72 +  }),
    1.73 +
    1.74 +  applyUpdate: Task.async(function*() {
    1.75 +    let webappJson = OS.Path.join(Services.dirsvc.get("AppRegD", Ci.nsIFile).path,
    1.76 +                                  "update", "webapp.json");
    1.77 +    let config = yield AppsUtils.loadJSONAsync(webappJson);
    1.78 +
    1.79 +    let nativeApp = new NativeApp(config.app, config.app.manifest,
    1.80 +                                  config.app.categories,
    1.81 +                                  config.registryDir);
    1.82 +    try {
    1.83 +      yield nativeApp.applyUpdate();
    1.84 +    } catch (ex) {
    1.85 +      return false;
    1.86 +    }
    1.87 +
    1.88 +    // The update has been applied successfully, the new config file
    1.89 +    // is the config file that was in the update directory.
    1.90 +    this.config = config;
    1.91 +
    1.92 +    return true;
    1.93 +  }),
    1.94 +
    1.95 +  startUpdateService: function() {
    1.96 +    let manifestURL = WebappRT.config.app.manifestURL;
    1.97 +    // We used to install apps without storing their manifest URL.
    1.98 +    // Now we can't update them.
    1.99 +    if (!manifestURL) {
   1.100 +      return;
   1.101 +    }
   1.102 +
   1.103 +    // Check for updates once a day.
   1.104 +    let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
   1.105 +                       getService(Ci.nsIUpdateTimerManager);
   1.106 +    timerManager.registerTimer("updateTimer", () => {
   1.107 +      let window = Services.wm.getMostRecentWindow("webapprt:webapp");
   1.108 +      window.navigator.mozApps.mgmt.getAll().onsuccess = function() {
   1.109 +        let thisApp = null;
   1.110 +        for (let app of this.result) {
   1.111 +          if (app.manifestURL == manifestURL) {
   1.112 +            thisApp = app;
   1.113 +            break;
   1.114 +          }
   1.115 +        }
   1.116 +
   1.117 +        // This shouldn't happen if the app is installed.
   1.118 +        if (!thisApp) {
   1.119 +          Cu.reportError("Couldn't find the app in the webapps registry");
   1.120 +          return;
   1.121 +        }
   1.122 +
   1.123 +        thisApp.ondownloadavailable = () => {
   1.124 +          // Download available, download it!
   1.125 +          thisApp.download();
   1.126 +        };
   1.127 +
   1.128 +        thisApp.ondownloadsuccess = () => {
   1.129 +          // Update downloaded, apply it!
   1.130 +          window.navigator.mozApps.mgmt.applyDownload(thisApp);
   1.131 +        };
   1.132 +
   1.133 +        thisApp.ondownloadapplied = () => {
   1.134 +          // Application updated, nothing to do.
   1.135 +        };
   1.136 +
   1.137 +        thisApp.checkForUpdate();
   1.138 +      }
   1.139 +    }, 24 * 60 * 60);
   1.140 +  },
   1.141 +};

mercurial