webapprt/WebappManager.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webapprt/WebappManager.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     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 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["WebappManager"];
    1.11 +
    1.12 +let Cc = Components.classes;
    1.13 +let Ci = Components.interfaces;
    1.14 +let Cu = Components.utils;
    1.15 +
    1.16 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.17 +Cu.import("resource://gre/modules/Services.jsm");
    1.18 +Cu.import("resource://gre/modules/Webapps.jsm");
    1.19 +Cu.import("resource://gre/modules/AppsUtils.jsm");
    1.20 +Cu.import("resource://gre/modules/NativeApp.jsm");
    1.21 +Cu.import("resource://gre/modules/WebappOSUtils.jsm");
    1.22 +Cu.import("resource://webapprt/modules/WebappRT.jsm");
    1.23 +
    1.24 +this.WebappManager = {
    1.25 +  observe: function(subject, topic, data) {
    1.26 +    data = JSON.parse(data);
    1.27 +    data.mm = subject;
    1.28 +
    1.29 +    switch (topic) {
    1.30 +      case "webapps-ask-install":
    1.31 +        let chromeWin = Services.wm.getOuterWindowWithId(data.oid);
    1.32 +        if (chromeWin)
    1.33 +          this.doInstall(data, chromeWin);
    1.34 +        break;
    1.35 +      case "webapps-launch":
    1.36 +        WebappOSUtils.launch(data);
    1.37 +        break;
    1.38 +      case "webapps-uninstall":
    1.39 +        WebappOSUtils.uninstall(data);
    1.40 +        break;
    1.41 +    }
    1.42 +  },
    1.43 +
    1.44 +  update: function(aApp, aManifest, aZipPath) {
    1.45 +    let nativeApp = new NativeApp(aApp, aManifest,
    1.46 +                                  WebappRT.config.app.categories,
    1.47 +                                  WebappRT.config.registryDir);
    1.48 +    nativeApp.prepareUpdate(aManifest, aZipPath);
    1.49 +  },
    1.50 +
    1.51 +  doInstall: function(data, window) {
    1.52 +    let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
    1.53 +    let manifest = new ManifestHelper(jsonManifest, data.app.origin);
    1.54 +    let name = manifest.name;
    1.55 +    let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
    1.56 +
    1.57 +    let choice = Services.prompt.confirmEx(
    1.58 +      window,
    1.59 +      bundle.formatStringFromName("webapps.install.title", [name], 1),
    1.60 +      bundle.formatStringFromName("webapps.install.description", [name], 1),
    1.61 +      // Set both buttons to strings with the cancel button being default
    1.62 +      Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
    1.63 +        Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
    1.64 +        Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
    1.65 +      bundle.GetStringFromName("webapps.install.install"),
    1.66 +      bundle.GetStringFromName("webapps.install.dontinstall"),
    1.67 +      null,
    1.68 +      null,
    1.69 +      {});
    1.70 +
    1.71 +    // Perform the install if the user allows it
    1.72 +    if (choice == 0) {
    1.73 +      let nativeApp = new NativeApp(data.app, jsonManifest,
    1.74 +                                    WebappRT.config.app.categories,
    1.75 +                                    WebappRT.config.registryDir);
    1.76 +      let localDir;
    1.77 +      try {
    1.78 +        localDir = nativeApp.createProfile();
    1.79 +      } catch (ex) {
    1.80 +        DOMApplicationRegistry.denyInstall(aData);
    1.81 +        return;
    1.82 +      }
    1.83 +
    1.84 +      DOMApplicationRegistry.confirmInstall(data, localDir,
    1.85 +        function (aManifest, aZipPath) {
    1.86 +          nativeApp.install(aManifest, aZipPath);
    1.87 +        }
    1.88 +      );
    1.89 +    } else {
    1.90 +      DOMApplicationRegistry.denyInstall(data);
    1.91 +    }
    1.92 +  },
    1.93 +
    1.94 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
    1.95 +                                         Ci.nsISupportsWeakReference])
    1.96 +};
    1.97 +
    1.98 +Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
    1.99 +Services.obs.addObserver(WebappManager, "webapps-launch", false);
   1.100 +Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
   1.101 +Services.obs.addObserver(WebappManager, "webapps-update", false);
   1.102 +
   1.103 +DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);

mercurial