forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebappManager.jsm
215 lines (177 loc) · 7.13 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 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"];
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/AppsUtils.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
Cu.import("resource://gre/modules/Webapps.jsm");
Cu.import("resource://gre/modules/osfile.jsm");
function dump(a) {
Services.console.logStringMessage("* * WebappManager.jsm: " + a);
}
function sendMessageToJava(aMessage) {
return Services.androidBridge.handleGeckoMessage(JSON.stringify(aMessage));
}
this.WebappManager = {
__proto__: DOMRequestIpcHelper.prototype,
downloadApk: function(aMsg) {
let manifestUrl = aMsg.app.manifestURL;
dump("downloadApk for " + manifestUrl);
// Get the endpoint URL and convert it to an nsIURI/nsIURL object.
const GENERATOR_URL_PREF = "browser.webapps.apkFactoryUrl";
const GENERATOR_URL_BASE = Services.prefs.getCharPref(GENERATOR_URL_PREF);
let generatorUrl = NetUtil.newURI(GENERATOR_URL_BASE).QueryInterface(Ci.nsIURL);
// Populate the query part of the URL with the manifest URL parameter.
let params = {
manifestUrl: manifestUrl,
};
generatorUrl.query =
[p + "=" + encodeURIComponent(params[p]) for (p in params)].join("&");
dump("downloading APK from " + generatorUrl.spec);
let file = Cc["@mozilla.org/download-manager;1"].
getService(Ci.nsIDownloadManager).
defaultDownloadsDirectory.
clone();
file.append(manifestUrl.replace(/[^a-zA-Z0-9]/gi, "") + ".apk");
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
dump("downloading APK to " + file.path);
let worker = new ChromeWorker("resource://gre/modules/WebappManagerWorker.js");
worker.onmessage = function(event) {
let { type, message } = event.data;
worker.terminate();
if (type == "success") {
sendMessageToJava({
type: "WebApps:InstallApk",
filePath: file.path,
data: JSON.stringify(aMsg),
});
} else { // type == "failure"
// TODO: handle error better.
dump("error downloading APK: " + message);
}
}
// Trigger the download.
worker.postMessage({ url: generatorUrl.spec, path: file.path });
},
askInstall: function(aData) {
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(aData.profilePath);
// We don't yet support pre-installing an appcache because it isn't clear
// how to do it without degrading the user experience (since users expect
// apps to be available after the system tells them they've been installed,
// which has already happened) and because nsCacheService shuts down
// when we trigger the native install dialog and doesn't re-init itself
// afterward (TODO: file bug about this behavior).
if ("appcache_path" in aData.app.manifest) {
dump("deleting appcache_path from manifest: " + aData.app.manifest.appcache_path);
delete aData.app.manifest.appcache_path;
}
DOMApplicationRegistry.confirmInstall(aData, file, (function(aManifest) {
let localeManifest = new ManifestHelper(aManifest, aData.app.origin);
// aData.app.origin may now point to the app: url that hosts this app.
sendMessageToJava({
type: "WebApps:PostInstall",
packageName: aData.app.packageName,
origin: aData.app.origin,
});
this.writeDefaultPrefs(file, localeManifest);
}).bind(this));
},
launch: function({ manifestURL, origin }) {
dump("launchWebapp: " + manifestURL);
sendMessageToJava({
type: "WebApps:Open",
manifestURL: manifestURL,
origin: origin
});
},
uninstall: function(aData) {
dump("uninstall: " + aData.manifestURL);
// TODO: uninstall the APK.
},
autoInstall: function(aData) {
let mm = {
sendAsyncMessage: function (aMessageName, aData) {
// TODO hook this back to Java to report errors.
dump("sendAsyncMessage " + aMessageName + ": " + JSON.stringify(aData));
}
};
let origin = Services.io.newURI(aData.manifestUrl, null, null).prePath;
let message = aData.request || {
app: {
origin: origin
}
};
if (aData.updateManifest) {
if (aData.zipFilePath) {
aData.updateManifest.package_path = aData.zipFilePath;
}
message.app.updateManifest = aData.updateManifest;
}
// The manifest url may be subtly different between the
// time the APK was built and the APK being installed.
// Thus, we should take the APK as the source of truth.
message.app.manifestURL = aData.manifestUrl;
message.app.manifest = aData.manifest;
message.app.packageName = aData.packageName;
message.profilePath = aData.profilePath;
message.autoInstall = true;
message.mm = mm;
switch (aData.type) { // can be hosted or packaged.
case "hosted":
DOMApplicationRegistry.doInstall(message, mm);
break;
case "packaged":
message.isPackage = true;
DOMApplicationRegistry.doInstallPackage(message, mm);
break;
}
},
autoUninstall: function(aData) {
let mm = {
sendAsyncMessage: function (aMessageName, aData) {
// TODO hook this back to Java to report errors.
dump("autoUninstall sendAsyncMessage " + aMessageName + ": " + JSON.stringify(aData));
}
};
let installedPackages = {};
DOMApplicationRegistry.doGetAll(installedPackages, mm);
for (let app in installedPackages.apps) {
if (aData.packages.indexOf(installedPackages.apps[app].packageName) > -1) {
let appToRemove = installedPackages.apps[app];
dump("should remove: " + appToRemove.name);
DOMApplicationRegistry.uninstall(appToRemove.manifestURL, function() {
dump(appToRemove.name + " uninstalled");
}, function() {
dump(appToRemove.name + " did not uninstall");
});
}
}
},
writeDefaultPrefs: function(aProfile, aManifest) {
// build any app specific default prefs
let prefs = [];
if (aManifest.orientation) {
prefs.push({name:"app.orientation.default", value: aManifest.orientation.join(",") });
}
// write them into the app profile
let defaultPrefsFile = aProfile.clone();
defaultPrefsFile.append(this.DEFAULT_PREFS_FILENAME);
this._writeData(defaultPrefsFile, prefs);
},
_writeData: function(aFile, aPrefs) {
if (aPrefs.length > 0) {
let array = new TextEncoder().encode(JSON.stringify(aPrefs));
OS.File.writeAtomic(aFile.path, array, { tmpPath: aFile.path + ".tmp" }).then(null, function onError(reason) {
dump("Error writing default prefs: " + reason);
});
}
},
DEFAULT_PREFS_FILENAME: "default-prefs.js",
};