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/. */
7 this.EXPORTED_SYMBOLS = ["WebappManager"];
9 let Cc = Components.classes;
10 let Ci = Components.interfaces;
11 let Cu = Components.utils;
13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
14 Cu.import("resource://gre/modules/Services.jsm");
15 Cu.import("resource://gre/modules/Webapps.jsm");
16 Cu.import("resource://gre/modules/AppsUtils.jsm");
17 Cu.import("resource://gre/modules/NativeApp.jsm");
18 Cu.import("resource://gre/modules/WebappOSUtils.jsm");
19 Cu.import("resource://gre/modules/Task.jsm");
20 Cu.import("resource://webapprt/modules/WebappRT.jsm");
23 this.WebappManager = {
24 observe: function(aSubject, aTopic, aData) {
25 let data = JSON.parse(aData);
30 case "webapps-ask-install":
31 chromeWin = Services.wm.getOuterWindowWithId(data.oid);
33 this.doInstall(data, chromeWin);
35 case "webapps-ask-uninstall":
36 chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
38 this.doUninstall(data, chromeWin);
41 case "webapps-launch":
42 WebappOSUtils.launch(data);
44 case "webapps-uninstall":
45 WebappOSUtils.uninstall(data).then(null, Cu.reportError);
50 update: function(aApp, aManifest, aZipPath) {
51 let nativeApp = new NativeApp(aApp, aManifest,
52 WebappRT.config.app.categories,
53 WebappRT.config.registryDir);
54 nativeApp.prepareUpdate(aApp, aManifest, aZipPath);
57 doInstall: function(data, window) {
58 let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
60 new ManifestHelper(jsonManifest, data.app.origin, data.app.manifestURL);
61 let name = manifest.name;
62 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
64 let choice = Services.prompt.confirmEx(
66 bundle.formatStringFromName("webapps.install.title", [name], 1),
67 bundle.formatStringFromName("webapps.install.description", [name], 1),
68 // Set both buttons to strings with the cancel button being default
69 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
70 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
71 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
72 bundle.GetStringFromName("webapps.install.install"),
73 bundle.GetStringFromName("webapps.install.dontinstall"),
78 // Perform the install if the user allows it
80 let nativeApp = new NativeApp(data.app, jsonManifest,
82 WebappRT.config.registryDir);
85 localDir = nativeApp.createProfile();
87 DOMApplicationRegistry.denyInstall(data);
91 DOMApplicationRegistry.confirmInstall(data, localDir,
92 Task.async(function*(aApp, aManifest, aZipPath) {
93 yield nativeApp.install(aApp, aManifest, aZipPath);
97 DOMApplicationRegistry.denyInstall(data);
101 doUninstall: function(aData, aWindow) {
102 let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
103 let manifest = new ManifestHelper(jsonManifest, aData.app.origin,
104 aData.app.manifestURL);
105 let name = manifest.name;
106 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
108 let choice = Services.prompt.confirmEx(
110 bundle.formatStringFromName("webapps.uninstall.title", [name], 1),
111 bundle.formatStringFromName("webapps.uninstall.description", [name], 1),
112 // Set both buttons to strings with the cancel button being default
113 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
114 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
115 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
116 bundle.GetStringFromName("webapps.uninstall.uninstall"),
117 bundle.GetStringFromName("webapps.uninstall.dontuninstall"),
122 // Perform the uninstall if the user allows it
124 DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
125 WebappOSUtils.uninstall(aApp).then(null, Cu.reportError);
128 DOMApplicationRegistry.denyUninstall(aData);
132 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
133 Ci.nsISupportsWeakReference])
136 Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
137 Services.obs.addObserver(WebappManager, "webapps-ask-uninstall", false);
138 Services.obs.addObserver(WebappManager, "webapps-launch", false);
139 Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
140 Services.obs.addObserver(WebappManager, "webapps-update", false);
142 DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);