forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebappManager.jsm
142 lines (124 loc) · 5.3 KB
/
WebappManager.jsm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.EXPORTED_SYMBOLS = ["WebappManager"];
let Cc = Components.classes;
let Ci = Components.interfaces;
let Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/AppsUtils.jsm");
Cu.import("resource://gre/modules/NativeApp.jsm");
Cu.import("resource://gre/modules/WebappOSUtils.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://webapprt/modules/WebappRT.jsm");
this.WebappManager = {
observe: function(aSubject, aTopic, aData) {
let data = JSON.parse(aData);
data.mm = aSubject;
let chromeWin;
switch (aTopic) {
case "webapps-ask-install":
chromeWin = Services.wm.getOuterWindowWithId(data.oid);
if (chromeWin)
this.doInstall(data, chromeWin);
break;
case "webapps-ask-uninstall":
chromeWin = Services.wm.getOuterWindowWithId(data.windowId);
if (chromeWin) {
this.doUninstall(data, chromeWin);
}
break;
case "webapps-launch":
WebappOSUtils.launch(data);
break;
case "webapps-uninstall":
WebappOSUtils.uninstall(data).then(null, Cu.reportError);
break;
}
},
update: function(aApp, aManifest, aZipPath) {
let nativeApp = new NativeApp(aApp, aManifest,
WebappRT.config.app.categories,
WebappRT.config.registryDir);
nativeApp.prepareUpdate(aApp, aManifest, aZipPath);
},
doInstall: function(data, window) {
let jsonManifest = data.isPackage ? data.app.updateManifest : data.app.manifest;
let manifest =
new ManifestHelper(jsonManifest, data.app.origin, data.app.manifestURL);
let name = manifest.name;
let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
let choice = Services.prompt.confirmEx(
window,
bundle.formatStringFromName("webapps.install.title", [name], 1),
bundle.formatStringFromName("webapps.install.description", [name], 1),
// Set both buttons to strings with the cancel button being default
Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
bundle.GetStringFromName("webapps.install.install"),
bundle.GetStringFromName("webapps.install.dontinstall"),
null,
null,
{});
// Perform the install if the user allows it
if (choice == 0) {
let nativeApp = new NativeApp(data.app, jsonManifest,
data.app.categories,
WebappRT.config.registryDir);
let localDir;
try {
localDir = nativeApp.createProfile();
} catch (ex) {
DOMApplicationRegistry.denyInstall(data);
return;
}
DOMApplicationRegistry.confirmInstall(data, localDir,
Task.async(function*(aApp, aManifest, aZipPath) {
yield nativeApp.install(aApp, aManifest, aZipPath);
})
);
} else {
DOMApplicationRegistry.denyInstall(data);
}
},
doUninstall: function(aData, aWindow) {
let jsonManifest = aData.isPackage ? aData.app.updateManifest : aData.app.manifest;
let manifest = new ManifestHelper(jsonManifest, aData.app.origin,
aData.app.manifestURL);
let name = manifest.name;
let bundle = Services.strings.createBundle("chrome://webapprt/locale/webapp.properties");
let choice = Services.prompt.confirmEx(
aWindow,
bundle.formatStringFromName("webapps.uninstall.title", [name], 1),
bundle.formatStringFromName("webapps.uninstall.description", [name], 1),
// Set both buttons to strings with the cancel button being default
Ci.nsIPromptService.BUTTON_POS_1_DEFAULT |
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_0 |
Ci.nsIPromptService.BUTTON_TITLE_IS_STRING * Ci.nsIPromptService.BUTTON_POS_1,
bundle.GetStringFromName("webapps.uninstall.uninstall"),
bundle.GetStringFromName("webapps.uninstall.dontuninstall"),
null,
null,
{});
// Perform the uninstall if the user allows it
if (choice == 0) {
DOMApplicationRegistry.confirmUninstall(aData).then((aApp) => {
WebappOSUtils.uninstall(aApp).then(null, Cu.reportError);
});
} else {
DOMApplicationRegistry.denyUninstall(aData);
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Ci.nsISupportsWeakReference])
};
Services.obs.addObserver(WebappManager, "webapps-ask-install", false);
Services.obs.addObserver(WebappManager, "webapps-ask-uninstall", false);
Services.obs.addObserver(WebappManager, "webapps-launch", false);
Services.obs.addObserver(WebappManager, "webapps-uninstall", false);
Services.obs.addObserver(WebappManager, "webapps-update", false);
DOMApplicationRegistry.registerUpdateHandler(WebappManager.update);