packagemanager-service.js
7.92 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
/*
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright 2017 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 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 Promise */
define('screens/player/firmware/packagemanager/impl/packagemanager-service', [
'screens/player/firmware/packagemanager/packagemanager',
'screens/player/firmware/packagemanager/spi/sync',
'screens/player/firmware/preferences/preferences',
'screens/player/firmware/filesystem/filesystem',
'screens/player/shared/serviceadmin',
'eventemitter',
'underscore'
], function(PackageManager, SyncSpi, Preferences, FilesystemService, ServiceAdmin, EventEmitter, _) {
'use strict';
/**
* template of the file name that contains the 'firmware' timestamp.
* @type {string}
*/
var TIMESTAMP_FILENAME_TEMPLATE = 'pge-package-update-{uid}.json';
/**
* name of the file that contains the 'firmware' timestamp.
* @type {string}
* @deprecated
*/
var TIMESTAMP_FILENAME = 'pge-package-update.json';
/**
* Reads the timestamp when the package was last modified.
* @memberof PackageManagerService
*
* @param {string} path Path to the package.
* @param {string?} uid Package unique id (used to find the pge-package-update file).
* @returns {Promise} A promise that resolves with the timestamp or 0 if not defined.
*/
function getLastModified(path, uid) {
var filesystemService = ServiceAdmin.getService(FilesystemService.serviceName);
return filesystemService.getUserDataPath()
.then(function(userDataPath) {
if (uid) {
return filesystemService.joinPath([userDataPath, path, 'www', TIMESTAMP_FILENAME_TEMPLATE.replace('{uid}', uid)]);
}
// backward compatibility: check for old file name
return filesystemService.joinPath([userDataPath, path, 'www', TIMESTAMP_FILENAME]);
}).then(function(timestampFilePath) {
return filesystemService.readFileAsText(timestampFilePath);
}).then(function(txt) {
var data = JSON.parse(txt);
console.log('getLastModified: timestamp contents for ' + path + ' is ', data);
return data.lastModified;
}).catch(function(e) {
if (uid) {
// backward compatibility: give an extra try without the uid
return getLastModified(path);
}
console.log('getLastModified: error reading timestamp file.', e);
return Promise.reject(e);
});
}
/**
* Update the specified package.
* @memberof PackageManagerService
*
* @param {String} url The url to the update package
* @param {String} path The path to extract the package to
* @param {String} mode Un-packing mode
* @param {String} title Friendly package title
* @return {Promise} A Promise that an updated package was downloaded
*
* @private
*/
function createUpdatePromise(url, path, mode, title) {
var self = this;
return new Promise(function(resolve, reject) {
var syncSpi = ServiceAdmin.getService(SyncSpi.serviceName);
var lastProgress = '';
var lastStatus = -1;
var lastProgressLogged = 0;
var lastProgressEmitted = 0;
var onProgress = function(data) {
var hash = data.status + '-' + data.progress;
if (lastProgress === hash) {
return;
}
var now = (new Date()).getTime();
if (now - lastProgressLogged > 500 || lastStatus !== data.status) {
console.log(data.statusText + ' - ' + data.progress + '% - ' + title);
lastProgressLogged = now;
}
if (now - lastProgressEmitted > 100 || lastStatus !== data.status) {
lastProgressEmitted = now;
self.emit(PackageManager.EVENTS.SYNC_PROGRESS, {
title: title,
url: url,
status: data.status,
progress: data.progress
});
}
lastStatus = data.status;
};
syncSpi.sync(url, path, mode, onProgress)
.then(function() {
resolve({
title: title,
url: url
});
})
.catch(function(e) {
reject({
title: title,
url: url,
error: e
});
});
self.emit(PackageManager.EVENTS.SYNC_STARTED, {
title: title,
url: url
});
});
}
/**
* Package Manager implementation.
*
* @class PackageManagerImpl
*/
var PackageManagerService = function() {
};
PackageManagerService.prototype = _.assign({}, EventEmitter.prototype, PackageManager,
/** @lends PackageManagerService.prototype */ {
/**
* @memberof PackageManagerService
* @inheritdoc
*/
activate: function() {
return new Promise(function(resolve) {
ServiceAdmin.onServiceHighestRankedStart(SyncSpi.serviceName, function() {
resolve();
});
}).then(function() {
return new Promise(function(resolve) {
ServiceAdmin.onServiceHighestRankedStart(Preferences.serviceName, function() {
resolve();
});
});
});
},
/**
* @memberof PackageManagerService
* @inheritdoc
*/
updatePackage: function(urlPath, path, expiration, mode, title, uid) {
var self = this;
var preferences = ServiceAdmin.getService(Preferences.serviceName).getPreferences();
var packageUrl = preferences.server + urlPath;
if (expiration) {
return getLastModified.call(self, path, uid)
.catch(function(e) {
console.log('Cannot read timestamp. Forcing update assuming the package does not exist yet', e);
return Promise.resolve(0);
})
.then(function(ts) {
if (ts < expiration) {
console.log('Updating expired package (' + ts + ' < ' + expiration + ')');
return createUpdatePromise.call(self, packageUrl, path, mode, title);
}
console.log('Package is not expired. Skipping update (' + ts + ' >= ' + expiration + ')');
return Promise.resolve({
title: title,
url: packageUrl
});
});
}
return createUpdatePromise.call(self, packageUrl, path, mode, title);
}
});
return PackageManagerService;
});
/* istanbul ignore next */
require([
'screens/player/firmware/packagemanager/impl/packagemanager-service',
'screens/player/shared/serviceadmin'
], function(PackageManagerService, ServiceAdmin) {
'use strict';
ServiceAdmin.register(new PackageManagerService());
});