Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/updater.js

Issue 11157034: Update info module and make updater use the new downloader module (Closed)
Patch Set: Created July 23, 2013, 10:03 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/prefs.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 let {Prefs} = require("prefs"); 18 let {Prefs} = require("prefs");
19 let {Downloader, Downloadable, MILLIS_IN_HOUR} = require("downloader");
20
19 let updateUrl = (_appInfo.developmentBuild ? Prefs.update_url_devbuild : Prefs.u pdate_url_release); 21 let updateUrl = (_appInfo.developmentBuild ? Prefs.update_url_devbuild : Prefs.u pdate_url_release);
20 updateUrl = updateUrl.replace(/%NAME%/g, encodeURIComponent(_appInfo.name)) 22 updateUrl = updateUrl.replace(/%NAME%/g, encodeURIComponent(_appInfo.name));
21 .replace(/%ID%/g, encodeURIComponent(_appInfo.id))
22 .replace(/%VERSION%/g, encodeURIComponent(_appInfo.version) )
23 .replace(/%APP%/g, encodeURIComponent(_appInfo.platform));
24 23
25 const HOURS_TO_MILLIS = 60 * 60 * 1000; 24 let callback = null;
26 const MIN_CHECK_INTERVAL = 18 * HOURS_TO_MILLIS;
27 const MAX_CHECK_INTERVAL = 30 * HOURS_TO_MILLIS;
28 25
26 const INITIAL_DELAY = 0.1 * MILLIS_IN_HOUR;
27 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
28 const EXPIRATION_INTERVAL = 24 * MILLIS_IN_HOUR;
29 const TYPE_AUTOMATIC = 0; 29 const TYPE_AUTOMATIC = 0;
30 const TYPE_MANUAL = 1; 30 const TYPE_MANUAL = 1;
31 31
32 let checkForUpdates = exports.checkForUpdates = function checkForUpdates(forceCh eck, callback) 32 let downloader = new Downloader(getDownloadables, INITIAL_DELAY, CHECK_INTERVAL) ;
33 downloader.onExpirationChange = onExpirationChange;
34 downloader.onDownloadSuccess = onDownloadSuccess;
35 downloader.onDownloadError = onDownloadError;
36
37 function getDownloadable(forceCheck)
33 { 38 {
34 let now = Date.now(); 39 let url = updateUrl.replace(/%TYPE%/g, forceCheck ? TYPE_MANUAL : TYPE_AUTOMAT IC);
35 if (!forceCheck && now < Prefs.next_update_check) 40 let downloadable = new Downloadable(url);
41 downloadable.lastError = Prefs.update_last_error;
42 downloadable.lastCheck = Prefs.update_last_check;
43 downloadable.softExpiration = Prefs.update_soft_expiration;
44 downloadable.hardExpiration = Prefs.update_hard_expiration;
45 return downloadable;
46 }
47
48 function getDownloadables()
49 {
50 yield getDownloadable(false);
51 }
52
53 function onExpirationChange(downloadable)
54 {
55 Prefs.update_last_check = downloadable.lastCheck;
56 Prefs.update_soft_expiration = downloadable.softExpiration;
57 Prefs.update_hard_expiration = downloadable.hardExpiration;
58 }
59
60 function onDownloadSuccess(downloadable, responseText, errorCallback, redirectCa llback)
61 {
62 Prefs.update_last_error = 0;
63 [Prefs.update_soft_expiration, Prefs.update_hard_expiration] = downloader.proc essExpirationInterval(EXPIRATION_INTERVAL);
64
65 try
36 { 66 {
37 if (Prefs.next_update_check - now > MAX_CHECK_INTERVAL) 67 let data = JSON.parse(responseText);
38 Prefs.next_update_check = now + MAX_CHECK_INTERVAL; 68 let updateInfo = null;
69 if (_appInfo.name in data)
70 updateInfo = data[_appInfo.name];
71 else if (_appInfo.name + "/" + _appInfo.application in data)
72 updateInfo = data[_appInfo.name + "/" + _appInfo.application];
39 73
40 window.setTimeout(checkForUpdates, Prefs.next_update_check - now); 74 if (updateInfo && "version" in updateInfo && "url" in updateInfo &&
41 return; 75 Services.vc.compare(updateInfo.version, _appInfo.version) > 0)
76 {
77 if (updateInfo.url.indexOf("https://") != 0)
78 throw new Error("Invalid update URL, HTTPS is mandatory for updates");
79 _triggerEvent("updateAvailable", updateInfo.url);
80 }
81 if (callback)
82 callback(null);
83 }
84 catch (e)
85 {
86 Cu.reportError(e);
87 errorCallback(e);
42 } 88 }
43 89
44 Prefs.next_update_check = now + MIN_CHECK_INTERVAL + 90 callback = null;
45 Math.random() * (MAX_CHECK_INTERVAL - MIN_CHECK_INTERVAL);
46 if (!forceCheck)
47 window.setTimeout(checkForUpdates, Prefs.next_update_check - now);
48
49 let url = updateUrl.replace(/%TYPE%/g, forceCheck ? TYPE_MANUAL : TYPE_AUTOMAT IC);
50 let request = new XMLHttpRequest();
51 request.open("GET", url);
52 request.addEventListener("load", function()
53 {
54 try
55 {
56 let data = JSON.parse(request.responseText);
57 let updateInfo = null;
58 if (_appInfo.name in data)
59 updateInfo = data[_appInfo.name];
60 else if (_appInfo.name + "/" + _appInfo.platform in data)
61 updateInfo = data[_appInfo.name + "/" + _appInfo.platform];
62
63 if (updateInfo && "version" in updateInfo && "url" in updateInfo &&
64 Services.vc.compare(updateInfo.version, _appInfo.version) > 0)
65 {
66 if (updateInfo.url.indexOf("https://") != 0)
67 throw new Error("Invalid update URL, HTTPS is mandatory for updates");
68 _triggerEvent("updateAvailable", updateInfo.url);
69 }
70 if (callback)
71 callback(null);
72 }
73 catch (e)
74 {
75 Cu.reportError(e);
76 if (callback)
77 callback(e);
78 }
79 }, false);
80
81 request.addEventListener("error", function()
82 {
83 let e = new Error("Update check failed (channel status " + request.channel.s tatus + ")");
84 Cu.reportError(e);
85 if (callback)
86 callback(e);
87 }, false);
88
89 request.send(null);
90 } 91 }
91 92
92 window.setTimeout(checkForUpdates, 0.1 * HOURS_TO_MILLIS); 93 function onDownloadError(downloadable, downloadURL, error, channelStatus, respon seStatus, redirectCallback)
94 {
95 Prefs.update_last_error = Date.now();
96 if (callback)
97 callback(error);
98 callback = null;
99 }
100
101 let checkForUpdates = exports.checkForUpdates = function checkForUpdates(_callba ck)
102 {
103 callback = _callback;
104 downloader.download(getDownloadable(true));
105 }
OLDNEW
« no previous file with comments | « lib/prefs.js ('k') | libadblockplus.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld