bootloader.js
11.6 KB
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
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals ContentSync, ES6Promise, cordova */
ES6Promise.polyfill();
/**
* Bootloader script that is used to get or create the local copy of the 'www' directory of the firmware.
* It automatically changes the window.location to the location where the firmware is loaded.
*
* @module bootloader
*/
(function(window) {
'use strict';
/**
* Name of the content sync package.
* @type {string}
*/
var CONTENT_SYNC_PACKAGE_NAME = 'firmware';
/**
* Name of the app id to use for content sync
* @type {string}
*/
var CONTENT_SYNC_APP_ID = CONTENT_SYNC_PACKAGE_NAME + '/www';
/**
* the parent directory for the start page, relative to the 'sync' directory
* @type {string}
*/
var ROOT_DIRECTORY_PATH = '/libs/screens/player/content';
/**
* name of the file that contains the 'firmware' timestamp.
* @type {string}
*/
var TIMESTAMP_FILENAME = 'pge-package-update.json';
/**
* Name of the default start page
* @type {string}
*/
var DEFAULT_START_PAGE = 'firmware.html';
/*
* Name of session storage key that may contain the url set by the handleOpenURL method
*/
var URLSCHEME_URL_STORAGE_KEY = 'com.adobe.cq.screens.player.urlscheme.url';
// todo: change rendition based on desired resolution
// var DEFAULT_START_PAGE = 'firmware.1920x1080.html';
/**
* Cordova FileSystem Utilities
*
* todo: use the one in shared/fs.js
*/
var fs = {
/**
* Resolves the local file system url.
* @param {String} url URL to the resource
* @returns {Promise} A promise that resolves to the file or directory entry.
*/
resolveLocalFileSystemURL: function(url) {
return new Promise(function(resolve, reject) {
console.log('resolveLocalFileSystemURL for ', url);
window.resolveLocalFileSystemURL(url,
function(dir) {
console.log('resolveLocalFileSystemURL.success', dir.name);
resolve(dir);
}, function(error) {
console.log('resolveLocalFileSystemURL.error', error.code);
reject(error);
});
});
},
/**
* Retrieves the file relative to the given directory.
* @param {DirectoryEntry} directory The Directory entry
* @param {String} path path to the file
* @returns {Promise} A promise that resolves to the file entry
*/
getFileEntry: function(directory, path) {
return new Promise(function(resolve, reject) {
console.log('getFileEntry ', directory, path);
directory.getFile(path, {create: false},
function(file) {
console.log('getFileEntry.success', file);
resolve(file);
}, function(error) {
console.log('getFileEntry.error', error);
reject(error);
});
});
},
/**
* Retrieves the file object that represents the current state of the file that the FileEntry represents.
* @param {FileEntry} fileEntry The file entry
* @returns {Promise} A promise that resolves to the file
*/
getFile: function(fileEntry) {
return new Promise(function(resolve, reject) {
return fileEntry.file(resolve, reject);
});
},
/**
* Reads the given file as test.
* @param {File} file The file
* @returns {Promise} A promise that resolves to the text of the file
*/
readFileAsText: function(file) {
return new Promise(function(resolve, reject) {
console.log('fileEntry.file.success ' + file);
var reader = new FileReader();
reader.onerror = reader.onabort = reject;
reader.onload = function() {
console.log('readFileEntryAsText.success ' + this.result);
resolve(this.result);
};
reader.readAsText(file);
});
},
/**
* Deletes a directory and all of its contents. In the event of an error (such as trying to delete a directory
* containing a file that can't be removed), some of the contents of the directory may be deleted.
*
* @param {DirectoryEntry} dirEntry The directory entry to remove
*
* @returns {Promise} A promise to remove the directory recursively
*/
removeRecursively: function(dirEntry) {
return new Promise(function(resolve, reject) {
dirEntry.removeRecursively(function() {
console.log('removeRecursively.success', dirEntry);
resolve();
}, function(e) {
console.log('removeRecursively.failed', e);
reject(e);
});
});
}
};
/**
* Returns a promise that resolves to the URL of the current start page.
*
* @returns {Promise} A promise to resolves the URL of the current start page.
*/
function currentStartPage() {
var syncRootURL;
/**
* Invokes the content-sync plugin in order to retrieve the location of the current copy. It copies
* the original 'www' files if required.
*
* @returns {Promise} A promise that resolves to the local root URL.
*/
function syncContent() {
return new Promise(function(resolve, reject) {
var sync = ContentSync.sync({
type: 'local',
id: CONTENT_SYNC_APP_ID,
// copyCordovaAssets: true,
copyRootApp: true
});
sync.on('complete', function(data) {
console.log('sync.complete', data);
syncRootURL = 'file://' + data.localPath;
resolve(syncRootURL);
});
sync.on('error', function(error) {
console.log('sync.error', error);
syncRootURL = '';
reject(error);
});
});
}
/**
* Retrieves the firmware root directory entry.
* @param {String} path the local content sync root path
* @returns {Promise} A promise that resolves to the directory entry.
*/
function getRootDirectory(path) {
return fs.resolveLocalFileSystemURL(path + ROOT_DIRECTORY_PATH);
}
/**
* Retrieves the 'firmware.html' file entry.
* @param {DirectoryEntry} rootDir The root directory entry
* @returns {Promise} A promise that resolves to the file entry
*/
function getFirmwareHTML(rootDir) {
return fs.getFileEntry(rootDir, DEFAULT_START_PAGE);
}
/**
* Retrieves the native path of the file entry
* @param {FileEntry} fileEntry The file entry
* @returns {String} The Url of the file entry
*/
function getNativePath(fileEntry) {
console.log('getNativePath.success', fileEntry.toURL());
return fileEntry.toURL();
}
/**
* Returns the last modified timestamp stored in the json file addressed by URL
* @param {String} url the url to the json file
* @returns {Promise} A promise that resolves to the timestamp.
*/
function getLastModified(url) {
return fs.resolveLocalFileSystemURL(url)
.then(fs.getFile)
.then(fs.readFileAsText)
.then(function(txt) {
var data = JSON.parse(txt);
console.log('timestamp contents for ' + url + ' is ', data);
return data.lastModified;
}).catch(function(e) {
console.log('error reading timestamp file.', e);
return Promise.reject(e);
});
}
/**
* Returns a promise that appends the given suffix to the input value.
* @param {String} suffix Suffix to add.
* @returns {Promise} that resolves by adding the suffix to the input value
*/
function append(suffix) {
return function(source) {
return source + suffix;
};
}
// get timestamps
var getTS1 = getLastModified(cordova.file.applicationDirectory + '/www/' + TIMESTAMP_FILENAME);
var getTS2 = syncContent().then(append('/' + TIMESTAMP_FILENAME)).then(getLastModified);
return Promise.all([getTS1, getTS2])
.then(function(values) {
var useBundled = values[0] > values[1];
console.log('bundled timestamp ' + values[0] + (useBundled ? ' > ' : ' < ') + ' synced timestamp ' + values[1]);
if (useBundled) {
// force re-sync. currently we need to delete the existing synced root and start again
return fs.resolveLocalFileSystemURL(syncRootURL)
.then(fs.removeRecursively)
.then(syncContent);
}
return syncRootURL;
})
.then(getRootDirectory)
.then(getFirmwareHTML)
.then(getNativePath);
}
// --------------------------------------------------------------
if (window.cordova) {
// START: handleOpenURL plugin handling
// if handleOpenURL call happens in the bootloader phase, we just set the url in the sessionStorage
// for further treatment
window.sessionStorage.removeItem(URLSCHEME_URL_STORAGE_KEY);
window.handleOpenURL = function(url) {
window.sessionStorage.setItem(URLSCHEME_URL_STORAGE_KEY, url);
};
// END: handleOpenURL plugin handling
document.addEventListener('deviceready', function() {
var currentLocation = window.location.href;
currentStartPage()
.then(function(newLocation) {
console.log(currentLocation);
console.log(newLocation);
if (currentLocation !== newLocation) {
window.location.href = newLocation;
}
})
.catch(function(e) {
console.log('sync failed or start page not found. using default.', e);
window.location.href = './' + ROOT_DIRECTORY_PATH + '/' + DEFAULT_START_PAGE;
}
);
});
} else {
window.document.writeln('<p style="color:red">Bootloader only useful in cordova environment...</p>');
console.error('No cordova detected. Bootloader only useful in cordova environment.');
}
}(window));