Bumping manifests a=b2g-bump
[gecko.git] / webapprt / WebappManager.jsm
blobdff03e3f9f453af1ba6ab1670cc1b0605ccee73f
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://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);
26     data.mm = aSubject;
28     let chromeWin;
29     switch (aTopic) {
30       case "webapps-ask-install":
31         chromeWin = Services.wm.getOuterWindowWithId(data.oid);
32         if (chromeWin)
33           this.doInstall(data, chromeWin);
34         break;
35       case "webapps-ask-uninstall":
36         chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
37         if (chromeWin) {
38           this.doUninstall(data, chromeWin);
39         }
40         break;
41       case "webapps-launch":
42         WebappOSUtils.launch(data);
43         break;
44       case "webapps-uninstall":
45         WebappOSUtils.uninstall(data).then(null, Cu.reportError);
46         break;
47     }
48   },
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);
55   },
57   doInstall: function(data, window) {
58     let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
59     let 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(
65       window,
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"),
74       null,
75       null,
76       {});
78     // Perform the install if the user allows it
79     if (choice == 0) {
80       let nativeApp = new NativeApp(data.app, jsonManifest,
81                                     data.app.categories,
82                                     WebappRT.config.registryDir);
83       let localDir;
84       try {
85         localDir = nativeApp.createProfile();
86       } catch (ex) {
87         DOMApplicationRegistry.denyInstall(data);
88         return;
89       }
91       DOMApplicationRegistry.confirmInstall(data, localDir,
92         Task.async(function*(aApp, aManifest, aZipPath) {
93           yield nativeApp.install(aApp, aManifest, aZipPath);
94         })
95       );
96     } else {
97       DOMApplicationRegistry.denyInstall(data);
98     }
99   },
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(
109       aWindow,
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"),
118       null,
119       null,
120       {});
122     // Perform the uninstall if the user allows it
123     if (choice == 0) {
124       DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
125         WebappOSUtils.uninstall(aApp).then(null, Cu.reportError);
126       });
127     } else {
128       DOMApplicationRegistry.denyUninstall(aData);
129     }
130   },
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);