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://webapprt/modules/WebappRT.jsm");
21 this.WebappManager = {
22 observe: function(aSubject, aTopic, aData) {
23 let data = JSON.parse(aData);
28 case "webapps-ask-install":
29 chromeWin = Services.wm.getOuterWindowWithId(data.oid);
31 this.doInstall(data, chromeWin);
33 case "webapps-ask-uninstall":
34 chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
36 this.doUninstall(data, chromeWin);
39 case "webapps-launch":
40 WebappOSUtils.launch(data);
42 case "webapps-uninstall":
43 WebappOSUtils.uninstall(data);
48 update: function(aApp, aManifest, aZipPath) {
49 let nativeApp = new NativeApp(aApp, aManifest,
50 WebappRT.config.app.categories,
51 WebappRT.config.registryDir);
52 nativeApp.prepareUpdate(aApp, aManifest, aZipPath);
55 doInstall: function(data, window) {
56 let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
58 new ManifestHelper(jsonManifest, data.app.origin, data.app.manifestURL);
59 let name = manifest.name;
60 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
62 let choice = Services.prompt.confirmEx(
64 bundle.formatStringFromName("webapps.install.title", [name], 1),
65 bundle.formatStringFromName("webapps.install.description", [name], 1),
66 // Set both buttons to strings with the cancel button being default
67 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
68 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
69 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
70 bundle.GetStringFromName("webapps.install.install"),
71 bundle.GetStringFromName("webapps.install.dontinstall"),
76 // Perform the install if the user allows it
78 let nativeApp = new NativeApp(data.app, jsonManifest,
80 WebappRT.config.registryDir);
83 localDir = nativeApp.createProfile();
85 DOMApplicationRegistry.denyInstall(data);
89 DOMApplicationRegistry.confirmInstall(data, localDir,
90 Task.async(function*(aApp, aManifest, aZipPath) {
91 yield nativeApp.install(aApp, aManifest, aZipPath);
95 DOMApplicationRegistry.denyInstall(data);
99 doUninstall: function(aData, aWindow) {
100 let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
101 let manifest = new ManifestHelper(jsonManifest, aData.app.origin,
102 aData.app.manifestURL);
103 let name = manifest.name;
104 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
106 let choice = Services.prompt.confirmEx(
108 bundle.formatStringFromName("webapps.uninstall.title", [name], 1),
109 bundle.formatStringFromName("webapps.uninstall.description", [name], 1),
110 // Set both buttons to strings with the cancel button being default
111 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
112 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
113 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
114 bundle.GetStringFromName("webapps.uninstall.uninstall"),
115 bundle.GetStringFromName("webapps.uninstall.dontuninstall"),
120 // Perform the uninstall if the user allows it
122 DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
123 WebappOSUtils.uninstall(aApp);
126 DOMApplicationRegistry.denyUninstall(aData);
130 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
131 Ci.nsISupportsWeakReference])
134 Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
135 Services.obs.addObserver(WebappManager, "webapps-ask-uninstall", false);
136 Services.obs.addObserver(WebappManager, "webapps-launch", false);
137 Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
138 Services.obs.addObserver(WebappManager, "webapps-update", false);
140 DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);