Backed out changeset cb123997572c (bug 937006) for Desktop B2G mochitest orange.
[gecko.git] / webapprt / WebappsHandler.jsm
blobf1308e212d7d17f44a738f749903334a57670077
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 = ["WebappsHandler"];
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/WebappsInstaller.jsm");
18 Cu.import("resource://gre/modules/WebappOSUtils.jsm");
20 this.WebappsHandler = {
21   observe: function(subject, topic, data) {
22     data = JSON.parse(data);
23     data.mm = subject;
25     switch (topic) {
26       case "webapps-ask-install":
27         let chromeWin = Services.wm.getOuterWindowWithId(data.oid);
28         if (chromeWin)
29           this.doInstall(data, chromeWin);
30         break;
31       case "webapps-launch":
32         WebappOSUtils.launch(data);
33         break;
34       case "webapps-uninstall":
35         WebappOSUtils.uninstall(data);
36         break;
37     }
38   },
40   doInstall: function(data, window) {
41     let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
42     let manifest = new ManifestHelper(jsonManifest, data.app.origin);
43     let name = manifest.name;
44     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
46     let choice = Services.prompt.confirmEx(
47       window,
48       bundle.formatStringFromName("webapps.install.title", [name], 1),
49       bundle.formatStringFromName("webapps.install.description", [name], 1),
50       // Set both buttons to strings with the cancel button being default
51       Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
52         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
53         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
54       bundle.GetStringFromName("webapps.install.install"),
55       bundle.GetStringFromName("webapps.install.dontinstall"),
56       null,
57       null,
58       {});
60     // Perform the install if the user allows it
61     if (choice == 0) {
62       let shell = WebappsInstaller.init(data);
64       if (shell) {
65         let localDir = null;
66         if (shell.appProfile) {
67           localDir = shell.appProfile.localDir;
68         }
70         DOMApplicationRegistry.confirmInstall(data, localDir,
71           function (aManifest) {
72             WebappsInstaller.install(data, aManifest);
73           }
74         );
75       } else {
76         DOMApplicationRegistry.denyInstall(data);
77       }
78     } else {
79       DOMApplicationRegistry.denyInstall(data);
80     }
81   },
83   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
84                                          Ci.nsISupportsWeakReference])
87 Services.obs.addObserver(WebappsHandler, "webapps-ask-install", false);
88 Services.obs.addObserver(WebappsHandler, "webapps-launch", false);
89 Services.obs.addObserver(WebappsHandler, "webapps-uninstall", false);