Bug 846687 - Set the transport as non-seekable if the server sends Accept-Ranges...
[gecko.git] / webapprt / WebappsHandler.jsm
bloba6fe4440de47d6d6e38399494434be91a4f9b6f6
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/Services.jsm");
14 Cu.import("resource://gre/modules/Webapps.jsm");
15 Cu.import("resource://gre/modules/WebappsInstaller.jsm");
16 Cu.import("resource://gre/modules/WebappOSUtils.jsm");
18 this.WebappsHandler = {
19   init: function() {
20     Services.obs.addObserver(this, "webapps-ask-install", false);
21     Services.obs.addObserver(this, "webapps-launch", false);
22     Services.obs.addObserver(this, "webapps-uninstall", false);
23   },
25   observe: function(subject, topic, data) {
26     data = JSON.parse(data);
27     data.mm = subject;
29     switch (topic) {
30       case "webapps-ask-install":
31         let chromeWin = this._getWindowByOuterId(data.oid);
32         if (chromeWin)
33           this.doInstall(data, chromeWin);
34         break;
35       case "webapps-launch":
36         WebappOSUtils.launch(data);
37         break;
38       case "webapps-uninstall":
39         WebappOSUtils.uninstall(data);
40         break;
41     }
42   },
44   _getWindowByOuterId: function(outerId) {
45     let someWindow = Services.wm.getMostRecentWindow(null);
46     if (!someWindow) {
47       return null;
48     }
50     let content = someWindow.QueryInterface(Ci.nsIInterfaceRequestor).
51                              getInterface(Ci.nsIDOMWindowUtils).
52                              getOuterWindowWithId(outerId);
53     return content;
54   },
56   doInstall: function(data, window) {
57     let {name} = data.app.manifest;
58     let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
60     let choice = Services.prompt.confirmEx(
61       window,
62       bundle.formatStringFromName("webapps.install.title", [name], 1),
63       bundle.formatStringFromName("webapps.install.description", [name], 1),
64       // Set both buttons to strings with the cancel button being default
65       Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
66         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
67         Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
68       bundle.GetStringFromName("webapps.install.install"),
69       bundle.GetStringFromName("webapps.install.dontinstall"),
70       null,
71       null,
72       {});
74     // Perform the install if the user allows it
75     if (choice == 0 && WebappsInstaller.install(data)) {
76       DOMApplicationRegistry.confirmInstall(data);
77     }
78     else {
79       DOMApplicationRegistry.denyInstall(data);
80     }
81   }