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: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["WebappManager"]; michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let 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/Webapps.jsm"); michael@0: Cu.import("resource://gre/modules/AppsUtils.jsm"); michael@0: Cu.import("resource://gre/modules/NativeApp.jsm"); michael@0: Cu.import("resource://gre/modules/WebappOSUtils.jsm"); michael@0: Cu.import("resource://webapprt/modules/WebappRT.jsm"); michael@0: michael@0: this.WebappManager = { michael@0: observe: function(subject, topic, data) { michael@0: data = JSON.parse(data); michael@0: data.mm = subject; michael@0: michael@0: switch (topic) { michael@0: case "webapps-ask-install": michael@0: let chromeWin = Services.wm.getOuterWindowWithId(data.oid); michael@0: if (chromeWin) michael@0: this.doInstall(data, chromeWin); michael@0: break; michael@0: case "webapps-launch": michael@0: WebappOSUtils.launch(data); michael@0: break; michael@0: case "webapps-uninstall": michael@0: WebappOSUtils.uninstall(data); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: update: function(aApp, aManifest, aZipPath) { michael@0: let nativeApp = new NativeApp(aApp, aManifest, michael@0: WebappRT.config.app.categories, michael@0: WebappRT.config.registryDir); michael@0: nativeApp.prepareUpdate(aManifest, aZipPath); michael@0: }, michael@0: michael@0: doInstall: function(data, window) { michael@0: let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest; michael@0: let manifest = new ManifestHelper(jsonManifest, data.app.origin); michael@0: let name = manifest.name; michael@0: let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties"); michael@0: michael@0: let choice = Services.prompt.confirmEx( michael@0: window, michael@0: bundle.formatStringFromName("webapps.install.title", [name], 1), michael@0: bundle.formatStringFromName("webapps.install.description", [name], 1), michael@0: // Set both buttons to strings with the cancel button being default michael@0: Ci.nsIPromptService.BUTTON_POS_1_DEFAULT | michael@0: Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 | michael@0: Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1, michael@0: bundle.GetStringFromName("webapps.install.install"), michael@0: bundle.GetStringFromName("webapps.install.dontinstall"), michael@0: null, michael@0: null, michael@0: {}); michael@0: michael@0: // Perform the install if the user allows it michael@0: if (choice == 0) { michael@0: let nativeApp = new NativeApp(data.app, jsonManifest, michael@0: WebappRT.config.app.categories, michael@0: WebappRT.config.registryDir); michael@0: let localDir; michael@0: try { michael@0: localDir = nativeApp.createProfile(); michael@0: } catch (ex) { michael@0: DOMApplicationRegistry.denyInstall(aData); michael@0: return; michael@0: } michael@0: michael@0: DOMApplicationRegistry.confirmInstall(data, localDir, michael@0: function (aManifest, aZipPath) { michael@0: nativeApp.install(aManifest, aZipPath); michael@0: } michael@0: ); michael@0: } else { michael@0: DOMApplicationRegistry.denyInstall(data); michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference]) michael@0: }; michael@0: michael@0: Services.obs.addObserver(WebappManager, "webapps-ask-install", false); michael@0: Services.obs.addObserver(WebappManager, "webapps-launch", false); michael@0: Services.obs.addObserver(WebappManager, "webapps-uninstall", false); michael@0: Services.obs.addObserver(WebappManager, "webapps-update", false); michael@0: michael@0: DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);