This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
webapp-manifests.js
318 lines (269 loc) · 9.83 KB
/
webapp-manifests.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var utils = require('./utils');
var config;
const { Cc, Ci, Cr, Cu } = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
const INSTALL_TIME = 132333986000;
// Match this to value in applications-data.js
function debug(msg) {
// dump('-*- webapp-manifest.js: ' + msg + '\n');
}
let io = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);
let webappsTargetDir = Cc['@mozilla.org/file/local;1']
.createInstance(Ci.nsILocalFile);
let manifests = {};
let id = 1;
function copyRec(source, target) {
let results = [];
let files = source.directoryEntries;
if (!target.exists())
target.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0755', 8));
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsILocalFile);
if (file.isDirectory()) {
let subFolder = target.clone();
subFolder.append(file.leafName);
copyRec(file, subFolder);
} else {
file.copyTo(target, file.leafName);
}
}
}
// Returns the nsIPrincipal compliant integer
// from the "type" property in manifests.
function getAppStatus(status) {
let appStatus = 1; // By default, apps are installed
switch (status) {
case 'certified':
appStatus = 3;
break;
case 'privileged':
appStatus = 2;
break;
case 'web':
default:
appStatus = 1;
break;
}
return appStatus;
}
function checkOrigin(origin) {
try {
return (io.newURI(origin, null, null).prePath === origin);
} catch (e) {
return false;
}
}
function fillCommsAppManifest(webapp, webappTargetDir) {
let manifestContent = utils.getFileContent(webapp.manifestFile);
var manifestObject = JSON.parse(manifestContent);
let redirects = manifestObject.redirects;
let indexedRedirects = {};
redirects.forEach(function(aRedirect) {
indexedRedirects[aRedirect.from] = aRedirect.to;
});
let mappingParameters = {
'facebook': 'redirectURI',
'live': 'redirectURI',
'gmail': 'redirectURI',
'facebook_dialogs': 'redirectMsg',
'facebook_logout': 'redirectLogout'
};
let content = JSON.parse(utils.getFileContent(utils.getFile(config.GAIA_DIR,
'build', 'communications_services.json')));
let custom = utils.getDistributionFileContent('communications_services',
content);
let commsServices = JSON.parse(custom);
let newRedirects = [];
redirects.forEach(function(aRedirect) {
let from = aRedirect.from;
let service = commsServices[from.split('_')[0] || from] || commsServices;
newRedirects.push({
from: service[mappingParameters[from]],
to: indexedRedirects[from]
});
});
manifestObject.redirects = newRedirects;
debug(webappTargetDir.path);
let file = utils.getFile(webappTargetDir.path, 'manifest.webapp');
utils.writeContent(file, JSON.stringify(manifestObject));
}
function fillAppManifest(webapp) {
// Compute webapp folder name in profile
let webappTargetDirName = webapp.domain;
// Copy webapp's manifest to the profile
let webappTargetDir = webappsTargetDir.clone();
webappTargetDir.append(webappTargetDirName);
webapp.manifestFile.copyTo(webappTargetDir, 'manifest.webapp');
if (webapp.url.indexOf('communications.gaiamobile.org') !== -1) {
fillCommsAppManifest(webapp, webappTargetDir);
}
else if (webapp.url.indexOf('keyboard.gaiamobile.org') !== -1) {
let kbdConfig = require('keyboard-config');
let kbdManifest = utils.getJSON(webapp.manifestFile);
kbdManifest = kbdConfig.addEntryPointsToManifest(config, kbdManifest);
utils.writeContent(utils.getFile(webappTargetDir.path, 'manifest.webapp'),
JSON.stringify(kbdManifest));
}
// Add webapp's entry to the webapps global manifest.
// appStatus == 3 means this is a certified app.
// appStatus == 2 means this is a privileged app.
// appStatus == 1 means this is an installed (unprivileged) app
var localId = id++;
// localId start from 1 in release build. For DESKTOP=1 build the system
// app can run inside Firefox desktop inside a regular tab and so the
// permissions set based on a principal are not working.
// To make it works the system app will be assigned an id of 0, which
// is the equivalent of the const NO_APP_ID.
if (config.DESKTOP &&
webappTargetDirName == ('system.' + config.GAIA_DOMAIN)) {
localId = 0;
}
let url = webapp.url;
manifests[webappTargetDirName] = {
origin: url,
installOrigin: url,
receipt: null,
installTime: INSTALL_TIME,
manifestURL: url + '/manifest.webapp',
appStatus: getAppStatus(webapp.manifest.type),
localId: localId
};
}
let errors = [];
function fillExternalAppManifest(webapp) {
// Compute webapp folder name in profile
let webappTargetDirName = webapp.sourceDirectoryName;
// Copy webapp's manifest to the profile
let webappTargetDir = webappsTargetDir.clone();
webappTargetDir.append(webappTargetDirName);
let removable;
let manifestURL = webapp.metaData.manifestURL;
if (!manifestURL) {
errors.push('External webapp `' + webapp.domain + '` does not have the ' +
'mandatory manifestURL property.');
return;
}
let manifestURI;
try {
manifestURI = io.newURI(manifestURL, null, null);
} catch (e) {
let msg = 'Error ' + e.name + ' while parsing manifestURL for webapp ' +
webapp.domain + ': ' + manifestURL;
if (e.name === 'NS_ERROR_MALFORMED_URI') {
msg += '\n Is it an absolute URL?';
}
errors.push(msg);
return;
}
if (manifestURI.scheme === 'app') {
dump('Warning: external webapp `' + webapp.domain + '` has a manifestURL ' +
'with an app:// scheme, which makes it non-updatable.\n');
}
let origin = webapp.metaData.origin;
if (origin) {
if (!checkOrigin(origin)) {
errors.push('External webapp `' + webapp.domain + '` has an invalid ' +
'origin: ' + origin);
return;
}
} else {
origin = manifestURI.prePath;
}
let installOrigin = webapp.metaData.installOrigin || origin;
if (!checkOrigin(installOrigin)) {
errors.push('External webapp `' + webapp.domain + '` has an invalid ' +
'installOrigin: ' + installOrigin);
return;
}
// In case of packaged app, just copy `application.zip` and `update.webapp`
let appPackage = webapp.buildDirectoryFile.clone();
appPackage.append('application.zip');
if (appPackage.exists()) {
let updateManifest = webapp.buildDirectoryFile.clone();
updateManifest.append('update.webapp');
if (!updateManifest.exists()) {
errors.push('External packaged webapp `' + webapp.domain + ' is ' +
'missing an `update.webapp` file. This JSON file ' +
'contains a `package_path` attribute specifying where ' +
'to download the application zip package from the origin ' +
'specified in `metadata.json` file.');
return;
}
appPackage.copyTo(webappTargetDir, 'application.zip');
updateManifest.copyTo(webappTargetDir, 'update.webapp');
removable = ('removable' in webapp.metaData) ? !!webapp.metaData.removable :
true;
} else {
webapp.manifestFile.copyTo(webappTargetDir, 'manifest.webapp');
removable = ('removable' in webapp.metaData) ? !!webapp.metaData.removable :
true;
// This is an hosted app. Check if there is an offline cache.
let srcCacheFolder = webapp.buildDirectoryFile.clone();
srcCacheFolder.append('cache');
if (srcCacheFolder.exists()) {
let cacheManifest = srcCacheFolder.clone();
cacheManifest.append('manifest.appcache');
if (!cacheManifest.exists()) {
errors.push('External webapp `' + webapp.domain + '` has a cache ' +
'directory without `manifest.appcache` file.');
return;
}
// Copy recursively the whole cache folder to webapp folder
let targetCacheFolder = webappTargetDir.clone();
targetCacheFolder.append('cache');
copyRec(srcCacheFolder, targetCacheFolder);
}
}
let etag = webapp.metaData.etag || null;
let packageEtag = webapp.metaData.packageEtag || null;
// Add webapp's entry to the webapps global manifest
manifests[webappTargetDirName] = {
origin: origin,
installOrigin: installOrigin,
receipt: null,
installTime: 132333986000,
manifestURL: manifestURL,
removable: removable,
localId: id++,
etag: etag,
packageEtag: packageEtag,
appStatus: getAppStatus(webapp.metaData.type || 'web')
};
}
function execute(options) {
config = options;
webappsTargetDir.initWithPath(config.PROFILE_DIR);
// Create profile folder if doesn't exists
if (!webappsTargetDir.exists())
webappsTargetDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0755', 8));
// Create webapps folder if doesn't exists
webappsTargetDir.append('webapps');
if (!webappsTargetDir.exists())
webappsTargetDir.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0755', 8));
utils.getGaia(config).webapps.forEach(function(webapp) {
// If BUILD_APP_NAME isn't `*`, we only accept one webapp
if (config.BUILD_APP_NAME != '*' &&
webapp.sourceDirectoryName != config.BUILD_APP_NAME) {
return;
}
if (webapp.metaData) {
fillExternalAppManifest(webapp);
} else {
fillAppManifest(webapp);
}
});
if (errors.length) {
var introMessage = 'We got ' + errors.length + ' manifest error' +
((errors.length > 1) ? 's' : '') + ' while building:';
errors.unshift(introMessage);
var message = errors.join('\n * ') + '\n';
throw new Error(message);
}
// Write webapps global manifest
let manifestFile = webappsTargetDir.clone();
manifestFile.append('webapps.json');
// stringify json with 2 spaces indentation
utils.writeContent(manifestFile, JSON.stringify(manifests, null, 2) + '\n');
}
exports.execute = execute;