| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 "use strict"; | |
| 19 | |
| 20 /* globals XMLHttpRequest */ | |
|
Sebastian Noack
2017/02/20 13:14:51
I would rather put this in the .eslintrc.json. Gen
kzar
2017/02/21 06:13:58
Done.
| |
| 21 | |
| 18 /** | 22 /** |
| 19 * @fileOverview Downloads a set of URLs in regular time intervals. | 23 * @fileOverview Downloads a set of URLs in regular time intervals. |
| 20 */ | 24 */ |
| 21 | 25 |
| 22 let {Utils} = require("utils"); | 26 let {Utils} = require("utils"); |
| 23 | 27 |
| 24 let MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; | 28 let MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; |
| 25 let MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | 29 let MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; |
| 26 let MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | 30 let MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; |
| 27 let MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | 31 let MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; |
| 28 | 32 |
| 33 let Downloader = | |
| 29 /** | 34 /** |
| 30 * Creates a new downloader instance. | 35 * Creates a new downloader instance. |
| 31 * @param {Function} dataSource Function that will yield downloadable objects o n each check | 36 * @param {Function} dataSource Function that will yield downloadable objects |
| 32 * @param {Integer} initialDelay Number of milliseconds to wait before the firs t check | 37 * on each check |
| 38 * @param {Integer} initialDelay Number of milliseconds to wait before the | |
| 39 * first check | |
| 33 * @param {Integer} checkInterval Interval between the checks | 40 * @param {Integer} checkInterval Interval between the checks |
| 34 * @constructor | 41 * @constructor |
| 35 */ | 42 */ |
| 36 let Downloader = exports.Downloader = function Downloader(dataSource, initialDel ay, checkInterval) | 43 exports.Downloader = function(dataSource, initialDelay, checkInterval) |
| 37 { | 44 { |
| 38 this.dataSource = dataSource; | 45 this.dataSource = dataSource; |
| 39 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | 46 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
| 40 this._timer.initWithCallback(function() | 47 this._timer.initWithCallback(() => |
| 41 { | 48 { |
| 42 this._timer.delay = checkInterval; | 49 this._timer.delay = checkInterval; |
| 43 this._doCheck(); | 50 this._doCheck(); |
| 44 }.bind(this), initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); | 51 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); |
| 45 this._downloading = Object.create(null); | 52 this._downloading = Object.create(null); |
| 46 } | 53 }; |
| 47 Downloader.prototype = | 54 Downloader.prototype = |
| 48 { | 55 { |
| 49 /** | 56 /** |
| 50 * Timer triggering the downloads. | 57 * Timer triggering the downloads. |
| 51 * @type nsITimer | 58 * @type {nsITimer} |
| 52 */ | 59 */ |
| 53 _timer: null, | 60 _timer: null, |
| 54 | 61 |
| 55 /** | 62 /** |
| 56 * Map containing the URLs of objects currently being downloaded as its keys. | 63 * Map containing the URLs of objects currently being downloaded as its keys. |
| 57 */ | 64 */ |
| 58 _downloading: null, | 65 _downloading: null, |
| 59 | 66 |
| 60 /** | 67 /** |
| 61 * Function that will yield downloadable objects on each check. | 68 * Function that will yield downloadable objects on each check. |
| 62 * @type Function | 69 * @type {Function} |
| 63 */ | 70 */ |
| 64 dataSource: null, | 71 dataSource: null, |
| 65 | 72 |
| 66 /** | 73 /** |
| 67 * Maximal time interval that the checks can be left out until the soft | 74 * Maximal time interval that the checks can be left out until the soft |
| 68 * expiration interval increases. | 75 * expiration interval increases. |
| 69 * @type Integer | 76 * @type {Integer} |
|
Sebastian Noack
2017/02/20 13:14:51
There is no type or class called "Integer". This s
kzar
2017/02/21 06:13:58
Done.
| |
| 70 */ | 77 */ |
| 71 maxAbsenceInterval: 1 * MILLIS_IN_DAY, | 78 maxAbsenceInterval: 1 * MILLIS_IN_DAY, |
| 72 | 79 |
| 73 /** | 80 /** |
| 74 * Minimal time interval before retrying a download after an error. | 81 * Minimal time interval before retrying a download after an error. |
| 75 * @type Integer | 82 * @type {Integer} |
| 76 */ | 83 */ |
| 77 minRetryInterval: 1 * MILLIS_IN_DAY, | 84 minRetryInterval: 1 * MILLIS_IN_DAY, |
| 78 | 85 |
| 79 /** | 86 /** |
| 80 * Maximal allowed expiration interval, larger expiration intervals will be | 87 * Maximal allowed expiration interval, larger expiration intervals will be |
| 81 * corrected. | 88 * corrected. |
| 82 * @type Integer | 89 * @type {Integer} |
| 83 */ | 90 */ |
| 84 maxExpirationInterval: 14 * MILLIS_IN_DAY, | 91 maxExpirationInterval: 14 * MILLIS_IN_DAY, |
| 85 | 92 |
| 86 /** | 93 /** |
| 87 * Maximal number of redirects before the download is considered as failed. | 94 * Maximal number of redirects before the download is considered as failed. |
| 88 * @type Integer | 95 * @type {Integer} |
| 89 */ | 96 */ |
| 90 maxRedirects: 5, | 97 maxRedirects: 5, |
| 91 | 98 |
| 92 /** | 99 /** |
| 93 * Called whenever expiration intervals for an object need to be adapted. | 100 * Called whenever expiration intervals for an object need to be adapted. |
| 94 * @type Function | 101 * @type {Function} |
| 95 */ | 102 */ |
| 96 onExpirationChange: null, | 103 onExpirationChange: null, |
| 97 | 104 |
| 98 /** | 105 /** |
| 99 * Callback to be triggered whenever a download starts. | 106 * Callback to be triggered whenever a download starts. |
| 100 * @type Function | 107 * @type {Function} |
| 101 */ | 108 */ |
| 102 onDownloadStarted: null, | 109 onDownloadStarted: null, |
| 103 | 110 |
| 104 /** | 111 /** |
| 105 * Callback to be triggered whenever a download finishes successfully. The | 112 * Callback to be triggered whenever a download finishes successfully. The |
| 106 * callback can return an error code to indicate that the data is wrong. | 113 * callback can return an error code to indicate that the data is wrong. |
| 107 * @type Function | 114 * @type {Function} |
| 108 */ | 115 */ |
| 109 onDownloadSuccess: null, | 116 onDownloadSuccess: null, |
| 110 | 117 |
| 111 /** | 118 /** |
| 112 * Callback to be triggered whenever a download fails. | 119 * Callback to be triggered whenever a download fails. |
| 113 * @type Function | 120 * @type {Function} |
| 114 */ | 121 */ |
| 115 onDownloadError: null, | 122 onDownloadError: null, |
| 116 | 123 |
| 117 /** | 124 /** |
| 118 * Checks whether anything needs downloading. | 125 * Checks whether anything needs downloading. |
| 119 */ | 126 */ |
| 120 _doCheck: function() | 127 _doCheck() |
| 121 { | 128 { |
| 122 let now = Date.now(); | 129 let now = Date.now(); |
| 123 for (let downloadable of this.dataSource()) | 130 for (let downloadable of this.dataSource()) |
| 124 { | 131 { |
| 125 if (downloadable.lastCheck && now - downloadable.lastCheck > this.maxAbsen ceInterval) | 132 if (downloadable.lastCheck && |
| 133 now - downloadable.lastCheck > this.maxAbsenceInterval) | |
| 126 { | 134 { |
| 127 // No checks for a long time interval - user must have been offline, e.g . | 135 // No checks for a long time interval - user must have been |
| 128 // during a weekend. Increase soft expiration to prevent load peaks on t he | 136 // offline, e.g. during a weekend. Increase soft expiration |
| 129 // server. | 137 // to prevent load peaks on the server. |
| 130 downloadable.softExpiration += now - downloadable.lastCheck; | 138 downloadable.softExpiration += now - downloadable.lastCheck; |
| 131 } | 139 } |
| 132 downloadable.lastCheck = now; | 140 downloadable.lastCheck = now; |
| 133 | 141 |
| 134 // Sanity check: do expiration times make sense? Make sure people changing | 142 // Sanity check: do expiration times make sense? Make sure people changing |
| 135 // system clock don't get stuck with outdated subscriptions. | 143 // system clock don't get stuck with outdated subscriptions. |
| 136 if (downloadable.hardExpiration - now > this.maxExpirationInterval) | 144 if (downloadable.hardExpiration - now > this.maxExpirationInterval) |
| 137 downloadable.hardExpiration = now + this.maxExpirationInterval; | 145 downloadable.hardExpiration = now + this.maxExpirationInterval; |
| 138 if (downloadable.softExpiration - now > this.maxExpirationInterval) | 146 if (downloadable.softExpiration - now > this.maxExpirationInterval) |
| 139 downloadable.softExpiration = now + this.maxExpirationInterval; | 147 downloadable.softExpiration = now + this.maxExpirationInterval; |
| 140 | 148 |
| 141 // Notify the caller about changes to expiration parameters | 149 // Notify the caller about changes to expiration parameters |
| 142 if (this.onExpirationChange) | 150 if (this.onExpirationChange) |
| 143 this.onExpirationChange(downloadable); | 151 this.onExpirationChange(downloadable); |
| 144 | 152 |
| 145 // Does that object need downloading? | 153 // Does that object need downloading? |
| 146 if (downloadable.softExpiration > now && downloadable.hardExpiration > now ) | 154 if (downloadable.softExpiration > now && |
| 155 downloadable.hardExpiration > now) | |
| 147 continue; | 156 continue; |
| 148 | 157 |
| 149 // Do not retry downloads too often | 158 // Do not retry downloads too often |
| 150 if (downloadable.lastError && now - downloadable.lastError < this.minRetry Interval) | 159 if (downloadable.lastError && |
| 160 now - downloadable.lastError < this.minRetryInterval) | |
| 151 continue; | 161 continue; |
| 152 | 162 |
| 153 this._download(downloadable, 0); | 163 this._download(downloadable, 0); |
| 154 } | 164 } |
| 155 }, | 165 }, |
| 156 | 166 |
| 157 /** | 167 /** |
| 158 * Stops the periodic checks. | 168 * Stops the periodic checks. |
| 159 */ | 169 */ |
| 160 cancel: function() | 170 cancel() |
| 161 { | 171 { |
| 162 this._timer.cancel(); | 172 this._timer.cancel(); |
| 163 }, | 173 }, |
| 164 | 174 |
| 165 /** | 175 /** |
| 166 * Checks whether an address is currently being downloaded. | 176 * Checks whether an address is currently being downloaded. |
| 177 * @param {String} url | |
| 178 * @return {Boolean} | |
| 167 */ | 179 */ |
| 168 isDownloading: function(/**String*/ url) /**Boolean*/ | 180 isDownloading(url) |
| 169 { | 181 { |
| 170 return url in this._downloading; | 182 return url in this._downloading; |
| 171 }, | 183 }, |
| 172 | 184 |
| 173 /** | 185 /** |
| 174 * Starts downloading for an object. | 186 * Starts downloading for an object. |
| 187 * @param {Downloadable} downloadable | |
| 175 */ | 188 */ |
| 176 download: function(/**Downloadable*/ downloadable) | 189 download(downloadable) |
| 177 { | 190 { |
| 178 // Make sure to detach download from the current execution context | 191 // Make sure to detach download from the current execution context |
| 179 Utils.runAsync(this._download.bind(this, downloadable, 0)); | 192 Utils.runAsync(this._download.bind(this, downloadable, 0)); |
| 180 }, | 193 }, |
| 181 | 194 |
| 182 /** | 195 /** |
| 183 * Generates the real download URL for an object by appending various | 196 * Generates the real download URL for an object by appending various |
| 184 * parameters. | 197 * parameters. |
| 198 * @param {Downloadable} downloadable | |
| 199 * @return {String} | |
| 185 */ | 200 */ |
| 186 getDownloadUrl: function(/**Downloadable*/ downloadable) /** String*/ | 201 getDownloadUrl(downloadable) |
| 187 { | 202 { |
| 188 let {addonName, addonVersion, application, applicationVersion, platform, pla tformVersion} = require("info"); | 203 let {addonName, addonVersion, application, applicationVersion, |
| 204 platform, platformVersion} = require("info"); | |
| 189 let url = downloadable.redirectURL || downloadable.url; | 205 let url = downloadable.redirectURL || downloadable.url; |
| 190 if (url.indexOf("?") >= 0) | 206 if (url.indexOf("?") >= 0) |
| 191 url += "&"; | 207 url += "&"; |
| 192 else | 208 else |
| 193 url += "?"; | 209 url += "?"; |
| 194 // We limit the download count to 4+ to keep the request anonymized | 210 // We limit the download count to 4+ to keep the request anonymized |
| 195 let downloadCount = downloadable.downloadCount; | 211 let {downloadCount} = downloadable; |
| 196 if (downloadCount > 4) | 212 if (downloadCount > 4) |
| 197 downloadCount = "4+"; | 213 downloadCount = "4+"; |
| 198 url += "addonName=" + encodeURIComponent(addonName) + | 214 url += "addonName=" + encodeURIComponent(addonName) + |
| 199 "&addonVersion=" + encodeURIComponent(addonVersion) + | 215 "&addonVersion=" + encodeURIComponent(addonVersion) + |
| 200 "&application=" + encodeURIComponent(application) + | 216 "&application=" + encodeURIComponent(application) + |
| 201 "&applicationVersion=" + encodeURIComponent(applicationVersion) + | 217 "&applicationVersion=" + encodeURIComponent(applicationVersion) + |
| 202 "&platform=" + encodeURIComponent(platform) + | 218 "&platform=" + encodeURIComponent(platform) + |
| 203 "&platformVersion=" + encodeURIComponent(platformVersion) + | 219 "&platformVersion=" + encodeURIComponent(platformVersion) + |
| 204 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + | 220 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + |
| 205 "&downloadCount=" + encodeURIComponent(downloadCount); | 221 "&downloadCount=" + encodeURIComponent(downloadCount); |
| 206 return url; | 222 return url; |
| 207 }, | 223 }, |
| 208 | 224 |
| 209 _download: function(downloadable, redirects) | 225 _download(downloadable, redirects) |
| 210 { | 226 { |
| 211 if (this.isDownloading(downloadable.url)) | 227 if (this.isDownloading(downloadable.url)) |
| 212 return; | 228 return; |
| 213 | 229 |
| 214 let downloadUrl = this.getDownloadUrl(downloadable); | 230 let downloadUrl = this.getDownloadUrl(downloadable); |
| 215 let request = null; | 231 let request = null; |
| 216 | 232 |
| 217 let errorCallback = function errorCallback(error) | 233 let errorCallback = function errorCallback(error) |
| 218 { | 234 { |
| 219 let channelStatus = -1; | 235 let channelStatus = -1; |
| 220 try | 236 try |
| 221 { | 237 { |
| 222 channelStatus = request.channel.status; | 238 channelStatus = request.channel.status; |
| 223 } catch (e) {} | 239 } |
| 240 catch (e) {} | |
| 224 | 241 |
| 225 let responseStatus = request.status; | 242 let responseStatus = request.status; |
| 226 | 243 |
| 227 Cu.reportError("Adblock Plus: Downloading URL " + downloadable.url + " fai led (" + error + ")\n" + | 244 Cu.reportError("Adblock Plus: Downloading URL " + downloadable.url + |
| 245 " failed (" + error + ")\n" + | |
| 228 "Download address: " + downloadUrl + "\n" + | 246 "Download address: " + downloadUrl + "\n" + |
| 229 "Channel status: " + channelStatus + "\n" + | 247 "Channel status: " + channelStatus + "\n" + |
| 230 "Server response: " + responseStatus); | 248 "Server response: " + responseStatus); |
| 231 | 249 |
| 232 if (this.onDownloadError) | 250 if (this.onDownloadError) |
| 233 { | 251 { |
| 234 // Allow one extra redirect if the error handler gives us a redirect URL | 252 // Allow one extra redirect if the error handler gives us a redirect URL |
| 235 let redirectCallback = null; | 253 let redirectCallback = null; |
| 236 if (redirects <= this.maxRedirects) | 254 if (redirects <= this.maxRedirects) |
| 237 { | 255 { |
| 238 redirectCallback = function redirectCallback(url) | 256 redirectCallback = (url) => |
| 239 { | 257 { |
| 240 downloadable.redirectURL = url; | 258 downloadable.redirectURL = url; |
| 241 this._download(downloadable, redirects + 1); | 259 this._download(downloadable, redirects + 1); |
| 242 }.bind(this); | 260 }; |
| 243 } | 261 } |
| 244 | 262 |
| 245 this.onDownloadError(downloadable, downloadUrl, error, channelStatus, re sponseStatus, redirectCallback); | 263 this.onDownloadError(downloadable, downloadUrl, error, channelStatus, |
| 264 responseStatus, redirectCallback); | |
| 246 } | 265 } |
| 247 }.bind(this); | 266 }.bind(this); |
| 248 | 267 |
| 249 try | 268 try |
| 250 { | 269 { |
| 251 request = new XMLHttpRequest(); | 270 request = new XMLHttpRequest(); |
| 252 request.mozBackgroundRequest = true; | 271 request.mozBackgroundRequest = true; |
| 253 request.open("GET", downloadUrl); | 272 request.open("GET", downloadUrl); |
| 254 } | 273 } |
| 255 catch (e) | 274 catch (e) |
| 256 { | 275 { |
| 257 errorCallback("synchronize_invalid_url"); | 276 errorCallback("synchronize_invalid_url"); |
| 258 return; | 277 return; |
| 259 } | 278 } |
| 260 | 279 |
| 261 try { | 280 try |
| 281 { | |
| 262 request.overrideMimeType("text/plain"); | 282 request.overrideMimeType("text/plain"); |
| 263 request.channel.loadFlags = request.channel.loadFlags | | 283 request.channel.loadFlags = request.channel.loadFlags | |
| 264 request.channel.INHIBIT_CACHING | | 284 request.channel.INHIBIT_CACHING | |
| 265 request.channel.VALIDATE_ALWAYS; | 285 request.channel.VALIDATE_ALWAYS; |
| 266 | 286 |
| 267 // Override redirect limit from preferences, user might have set it to 1 | 287 // Override redirect limit from preferences, user might have set it to 1 |
| 268 if (request.channel instanceof Ci.nsIHttpChannel) | 288 if (request.channel instanceof Ci.nsIHttpChannel) |
| 269 request.channel.redirectionLimit = this.maxRedirects; | 289 request.channel.redirectionLimit = this.maxRedirects; |
| 270 } | 290 } |
| 271 catch (e) | 291 catch (e) |
| 272 { | 292 { |
| 273 Cu.reportError(e) | 293 Cu.reportError(e); |
| 274 } | 294 } |
| 275 | 295 |
| 276 request.addEventListener("error", function(event) | 296 request.addEventListener("error", event => |
| 277 { | 297 { |
| 278 if (onShutdown.done) | 298 if (onShutdown.done) |
| 279 return; | 299 return; |
| 280 | 300 |
| 281 delete this._downloading[downloadable.url]; | 301 delete this._downloading[downloadable.url]; |
| 282 errorCallback("synchronize_connection_error"); | 302 errorCallback("synchronize_connection_error"); |
| 283 }.bind(this), false); | 303 }, false); |
| 284 | 304 |
| 285 request.addEventListener("load", function(event) | 305 request.addEventListener("load", event => |
| 286 { | 306 { |
| 287 if (onShutdown.done) | 307 if (onShutdown.done) |
| 288 return; | 308 return; |
| 289 | 309 |
| 290 delete this._downloading[downloadable.url]; | 310 delete this._downloading[downloadable.url]; |
| 291 | 311 |
| 292 // Status will be 0 for non-HTTP requests | 312 // Status will be 0 for non-HTTP requests |
| 293 if (request.status && request.status != 200) | 313 if (request.status && request.status != 200) |
| 294 { | 314 { |
| 295 errorCallback("synchronize_connection_error"); | 315 errorCallback("synchronize_connection_error"); |
| 296 return; | 316 return; |
| 297 } | 317 } |
| 298 | 318 |
| 299 downloadable.downloadCount++; | 319 downloadable.downloadCount++; |
| 300 | 320 |
| 301 this.onDownloadSuccess(downloadable, request.responseText, errorCallback, function redirectCallback(url) | 321 this.onDownloadSuccess( |
| 302 { | 322 downloadable, request.responseText, errorCallback, |
| 303 if (redirects >= this.maxRedirects) | 323 url => |
| 304 errorCallback("synchronize_connection_error"); | |
| 305 else | |
| 306 { | 324 { |
| 307 downloadable.redirectURL = url; | 325 if (redirects >= this.maxRedirects) |
| 308 this._download(downloadable, redirects + 1); | 326 errorCallback("synchronize_connection_error"); |
| 327 else | |
| 328 { | |
| 329 downloadable.redirectURL = url; | |
| 330 this._download(downloadable, redirects + 1); | |
| 331 } | |
| 309 } | 332 } |
| 310 }.bind(this)); | 333 ); |
| 311 }.bind(this), false); | 334 }); |
| 312 | 335 |
| 313 request.send(null); | 336 request.send(null); |
| 314 | 337 |
| 315 this._downloading[downloadable.url] = true; | 338 this._downloading[downloadable.url] = true; |
| 316 if (this.onDownloadStarted) | 339 if (this.onDownloadStarted) |
| 317 this.onDownloadStarted(downloadable); | 340 this.onDownloadStarted(downloadable); |
| 318 }, | 341 }, |
| 319 | 342 |
| 320 /** | 343 /** |
| 321 * Produces a soft and a hard expiration interval for a given supplied | 344 * Produces a soft and a hard expiration interval for a given supplied |
| 322 * expiration interval. | 345 * expiration interval. |
| 346 * @param {Integer} interval | |
| 323 * @return {Array} soft and hard expiration interval | 347 * @return {Array} soft and hard expiration interval |
| 324 */ | 348 */ |
| 325 processExpirationInterval: function(/**Integer*/ interval) | 349 processExpirationInterval(interval) |
| 326 { | 350 { |
| 327 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); | 351 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); |
| 328 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); | 352 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); |
| 329 let hard = interval * 2; | 353 let hard = interval * 2; |
| 330 let now = Date.now(); | 354 let now = Date.now(); |
| 331 return [now + soft, now + hard]; | 355 return [now + soft, now + hard]; |
| 332 } | 356 } |
| 333 }; | 357 }; |
| 334 | 358 |
| 335 /** | 359 /** |
| 336 * An object that can be downloaded by the downloadable | 360 * An object that can be downloaded by the downloadable |
| 337 * @param {String} url URL that has to be requested for the object | 361 * @param {String} url URL that has to be requested for the object |
| 338 * @constructor | 362 * @constructor |
| 339 */ | 363 */ |
| 340 let Downloadable = exports.Downloadable = function Downloadable(url) | 364 let Downloadable = exports.Downloadable = function Downloadable(url) |
| 341 { | 365 { |
| 342 this.url = url; | 366 this.url = url; |
| 343 } | 367 }; |
| 344 Downloadable.prototype = | 368 Downloadable.prototype = |
| 345 { | 369 { |
| 346 /** | 370 /** |
| 347 * URL that has to be requested for the object. | 371 * URL that has to be requested for the object. |
| 348 * @type String | 372 * @type {String} |
| 349 */ | 373 */ |
| 350 url: null, | 374 url: null, |
| 351 | 375 |
| 352 /** | 376 /** |
| 353 * URL that the download was redirected to if any. | 377 * URL that the download was redirected to if any. |
| 354 * @type String | 378 * @type {String} |
| 355 */ | 379 */ |
| 356 redirectURL: null, | 380 redirectURL: null, |
| 357 | 381 |
| 358 /** | 382 /** |
| 359 * Time of last download error or 0 if the last download was successful. | 383 * Time of last download error or 0 if the last download was successful. |
| 360 * @type Integer | 384 * @type {Integer} |
| 361 */ | 385 */ |
| 362 lastError: 0, | 386 lastError: 0, |
| 363 | 387 |
| 364 /** | 388 /** |
| 365 * Time of last check whether the object needs downloading. | 389 * Time of last check whether the object needs downloading. |
| 366 * @type Integer | 390 * @type {Integer} |
| 367 */ | 391 */ |
| 368 lastCheck: 0, | 392 lastCheck: 0, |
| 369 | 393 |
| 370 /** | 394 /** |
| 371 * Object version corresponding to the last successful download. | 395 * Object version corresponding to the last successful download. |
| 372 * @type Integer | 396 * @type {Integer} |
| 373 */ | 397 */ |
| 374 lastVersion: 0, | 398 lastVersion: 0, |
| 375 | 399 |
| 376 /** | 400 /** |
| 377 * Soft expiration interval, will increase if no checks are performed for a | 401 * Soft expiration interval, will increase if no checks are performed for a |
| 378 * while. | 402 * while. |
| 379 * @type Integer | 403 * @type {Integer} |
| 380 */ | 404 */ |
| 381 softExpiration: 0, | 405 softExpiration: 0, |
| 382 | 406 |
| 383 /** | 407 /** |
| 384 * Hard expiration interval, this is fixed. | 408 * Hard expiration interval, this is fixed. |
| 385 * @type Integer | 409 * @type {Integer} |
| 386 */ | 410 */ |
| 387 hardExpiration: 0, | 411 hardExpiration: 0, |
| 388 | 412 |
| 389 /** | 413 /** |
| 390 * Number indicating how often the object was downloaded. | 414 * Number indicating how often the object was downloaded. |
| 391 * @type Integer | 415 * @type {Integer} |
| 392 */ | 416 */ |
| 393 downloadCount: 0, | 417 downloadCount: 0 |
| 394 }; | 418 }; |
| OLD | NEW |