Merge mozilla-b2g34 to 2.1s. a=merge
[gecko.git] / webapprt / WebappManager.jsm
blob190070d9674cca65dbae4549339a8c7b10e09c36
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/. */
5 "use strict";
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);
24     data.mm = aSubject;
26     let chromeWin;
27     switch (aTopic) {
28       case "webapps-ask-install":
29         chromeWin = Services.wm.getOuterWindowWithId(data.oid);
30         if (chromeWin)
31           this.doInstall(data, chromeWin);
32         break;
33       case "webapps-ask-uninstall":
34         chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
35         if (chromeWin) {
36           this.doUninstall(data, chromeWin);
37         }
38         break;
39       case "webapps-launch":
40         WebappOSUtils.launch(data);
41         break;
42       case "webapps-uninstall":
43         WebappOSUtils.uninstall(data);
44         break;
45     }
46   },
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);
53   },
55   doInstall: function(data, window) {
56     let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
57     let 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(
63       window,
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"),
72       null,
73       null,
74       {});
76     // Perform the install if the user allows it
77     if (choice == 0) {
78       let nativeApp = new NativeApp(data.app, jsonManifest,
79                                     data.app.categories,
80                                     WebappRT.config.registryDir);
81       let localDir;
82       try {
83         localDir = nativeApp.createProfile();
84       } catch (ex) {
85         DOMApplicationRegistry.denyInstall(data);
86         return;
87       }
89       DOMApplicationRegistry.confirmInstall(data, localDir,
90         Task.async(function*(aApp, aManifest, aZipPath) {
91           yield nativeApp.install(aApp, aManifest, aZipPath);
92         })
93       );
94     } else {
95       DOMApplicationRegistry.denyInstall(data);
96     }
97   },
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(
107       aWindow,
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"),
116       null,
117       null,
118       {});
120     // Perform the uninstall if the user allows it
121     if (choice == 0) {
122       DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
123         WebappOSUtils.uninstall(aApp);
124       });
125     } else {
126       DOMApplicationRegistry.denyUninstall(aData);
127     }
128   },
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);