Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
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(subject, topic, data) {
23 data = JSON.parse(data);
24 data.mm = subject;
26 switch (topic) {
27 case "webapps-ask-install":
28 let chromeWin = Services.wm.getOuterWindowWithId(data.oid);
29 if (chromeWin)
30 this.doInstall(data, chromeWin);
31 break;
32 case "webapps-launch":
33 WebappOSUtils.launch(data);
34 break;
35 case "webapps-uninstall":
36 WebappOSUtils.uninstall(data);
37 break;
38 }
39 },
41 update: function(aApp, aManifest, aZipPath) {
42 let nativeApp = new NativeApp(aApp, aManifest,
43 WebappRT.config.app.categories,
44 WebappRT.config.registryDir);
45 nativeApp.prepareUpdate(aManifest, aZipPath);
46 },
48 doInstall: function(data, window) {
49 let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
50 let manifest = new ManifestHelper(jsonManifest, data.app.origin);
51 let name = manifest.name;
52 let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
54 let choice = Services.prompt.confirmEx(
55 window,
56 bundle.formatStringFromName("webapps.install.title", [name], 1),
57 bundle.formatStringFromName("webapps.install.description", [name], 1),
58 // Set both buttons to strings with the cancel button being default
59 Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
60 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
61 Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
62 bundle.GetStringFromName("webapps.install.install"),
63 bundle.GetStringFromName("webapps.install.dontinstall"),
64 null,
65 null,
66 {});
68 // Perform the install if the user allows it
69 if (choice == 0) {
70 let nativeApp = new NativeApp(data.app, jsonManifest,
71 WebappRT.config.app.categories,
72 WebappRT.config.registryDir);
73 let localDir;
74 try {
75 localDir = nativeApp.createProfile();
76 } catch (ex) {
77 DOMApplicationRegistry.denyInstall(aData);
78 return;
79 }
81 DOMApplicationRegistry.confirmInstall(data, localDir,
82 function (aManifest, aZipPath) {
83 nativeApp.install(aManifest, aZipPath);
84 }
85 );
86 } else {
87 DOMApplicationRegistry.denyInstall(data);
88 }
89 },
91 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
92 Ci.nsISupportsWeakReference])
93 };
95 Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
96 Services.obs.addObserver(WebappManager, "webapps-launch", false);
97 Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
98 Services.obs.addObserver(WebappManager, "webapps-update", false);
100 DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);