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

Side by Side Diff: lib/updater.js

Issue 29545700: Issue 5685 - Pass ESLint (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created Sept. 15, 2017, 7:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« lib/init.js ('K') | « lib/prefs.js ('k') | lib/utils.js » ('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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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
18 let {Prefs} = require("prefs"); 20 let {Prefs} = require("prefs");
19 let {Downloader, Downloadable, MILLIS_IN_HOUR} = require("downloader"); 21 let {Downloader, Downloadable, MILLIS_IN_HOUR} = require("downloader");
20 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); 22 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
21 23
22 let updateUrl = (_appInfo.developmentBuild ? Prefs.update_url_devbuild : Prefs.u pdate_url_release); 24 let updateUrl = (_appInfo.developmentBuild ?
25 Prefs.update_url_devbuild : Prefs.update_url_release);
23 updateUrl = updateUrl.replace(/%NAME%/g, encodeURIComponent(_appInfo.name)); 26 updateUrl = updateUrl.replace(/%NAME%/g, encodeURIComponent(_appInfo.name));
24 27
25 let callback = null; 28 let callback = null;
26 29
27 const INITIAL_DELAY = 0.1 * MILLIS_IN_HOUR; 30 const INITIAL_DELAY = 0.1 * MILLIS_IN_HOUR;
28 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 31 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
29 const EXPIRATION_INTERVAL = 24 * MILLIS_IN_HOUR; 32 const EXPIRATION_INTERVAL = 24 * MILLIS_IN_HOUR;
30 const TYPE_AUTOMATIC = 0; 33 const TYPE_AUTOMATIC = 0;
31 const TYPE_MANUAL = 1; 34 const TYPE_MANUAL = 1;
32 35
33 let downloader = new Downloader(getDownloadables, INITIAL_DELAY, CHECK_INTERVAL) ; 36 let downloader = new Downloader(
37 getDownloadables, INITIAL_DELAY, CHECK_INTERVAL);
34 downloader.onExpirationChange = onExpirationChange; 38 downloader.onExpirationChange = onExpirationChange;
35 downloader.onDownloadSuccess = onDownloadSuccess; 39 downloader.onDownloadSuccess = onDownloadSuccess;
36 downloader.onDownloadError = onDownloadError; 40 downloader.onDownloadError = onDownloadError;
37 41
38 function getDownloadable(forceCheck) 42 function getDownloadable(forceCheck)
39 { 43 {
40 if (!forceCheck && Prefs.disable_auto_updates) 44 if (!forceCheck && Prefs.disable_auto_updates)
41 { 45 {
42 return null; 46 return null;
43 } 47 }
44 let url = updateUrl.replace(/%TYPE%/g, forceCheck ? TYPE_MANUAL : TYPE_AUTOMAT IC); 48 let url = updateUrl.replace(/%TYPE%/g,
49 forceCheck ? TYPE_MANUAL : TYPE_AUTOMATIC);
45 let downloadable = new Downloadable(url); 50 let downloadable = new Downloadable(url);
46 downloadable.lastError = Prefs.update_last_error; 51 downloadable.lastError = Prefs.update_last_error;
47 downloadable.lastCheck = Prefs.update_last_check; 52 downloadable.lastCheck = Prefs.update_last_check;
48 downloadable.softExpiration = Prefs.update_soft_expiration; 53 downloadable.softExpiration = Prefs.update_soft_expiration;
49 downloadable.hardExpiration = Prefs.update_hard_expiration; 54 downloadable.hardExpiration = Prefs.update_hard_expiration;
50 return downloadable; 55 return downloadable;
51 } 56 }
52 57
53 function* getDownloadables() 58 function* getDownloadables()
54 { 59 {
55 yield getDownloadable(false); 60 yield getDownloadable(false);
56 } 61 }
57 62
58 function onExpirationChange(downloadable) 63 function onExpirationChange(downloadable)
59 { 64 {
60 Prefs.update_last_check = downloadable.lastCheck; 65 Prefs.update_last_check = downloadable.lastCheck;
61 Prefs.update_soft_expiration = downloadable.softExpiration; 66 Prefs.update_soft_expiration = downloadable.softExpiration;
62 Prefs.update_hard_expiration = downloadable.hardExpiration; 67 Prefs.update_hard_expiration = downloadable.hardExpiration;
63 } 68 }
64 69
65 function onDownloadSuccess(downloadable, responseText, errorCallback, redirectCa llback) 70 function onDownloadSuccess(downloadable, responseText, errorCallback,
71 redirectCallback)
66 { 72 {
67 Prefs.update_last_error = 0; 73 Prefs.update_last_error = 0;
68 [Prefs.update_soft_expiration, Prefs.update_hard_expiration] = downloader.proc essExpirationInterval(EXPIRATION_INTERVAL); 74 [Prefs.update_soft_expiration, Prefs.update_hard_expiration] =
75 downloader.processExpirationInterval(EXPIRATION_INTERVAL);
69 76
70 try 77 try
71 { 78 {
72 let data = JSON.parse(responseText); 79 let data = JSON.parse(responseText);
73 let updateInfo = null; 80 let updateInfo = null;
74 if (_appInfo.name in data) 81 if (_appInfo.name in data)
75 updateInfo = data[_appInfo.name]; 82 updateInfo = data[_appInfo.name];
76 else if (_appInfo.name + "/" + _appInfo.application in data) 83 else if (_appInfo.name + "/" + _appInfo.application in data)
77 updateInfo = data[_appInfo.name + "/" + _appInfo.application]; 84 updateInfo = data[_appInfo.name + "/" + _appInfo.application];
78 85
79 if (updateInfo && "version" in updateInfo && "url" in updateInfo && 86 if (updateInfo && "version" in updateInfo && "url" in updateInfo &&
80 Services.vc.compare(updateInfo.version, _appInfo.version) > 0) 87 Services.vc.compare(updateInfo.version, _appInfo.version) > 0)
81 { 88 {
82 if (updateInfo.url.indexOf("https://") != 0) 89 if (updateInfo.url.indexOf("https://") != 0)
83 throw new Error("Invalid update URL, HTTPS is mandatory for updates"); 90 throw new Error("Invalid update URL, HTTPS is mandatory for updates");
84 _triggerEvent("updateAvailable", updateInfo.url); 91 _triggerEvent("updateAvailable", updateInfo.url);
85 } 92 }
86 if (callback) 93 if (callback)
87 callback(null); 94 callback(null);
88 } 95 }
89 catch (e) 96 catch (e)
90 { 97 {
91 Cu.reportError(e); 98 Cu.reportError(e);
92 errorCallback(e); 99 errorCallback(e);
93 } 100 }
94 101
95 callback = null; 102 callback = null;
96 } 103 }
97 104
98 function onDownloadError(downloadable, downloadURL, error, channelStatus, respon seStatus, redirectCallback) 105 function onDownloadError(downloadable, downloadURL, error,
106 channelStatus, responseStatus, redirectCallback)
99 { 107 {
100 Prefs.update_last_error = Date.now(); 108 Prefs.update_last_error = Date.now();
101 if (callback) 109 if (callback)
102 callback(error); 110 callback(error);
103 callback = null; 111 callback = null;
104 } 112 }
105 113
106 let checkForUpdates = exports.checkForUpdates = function checkForUpdates(_callba ck) 114 let checkForUpdates = function(_callback)
107 { 115 {
108 callback = _callback; 116 callback = _callback;
109 downloader.download(getDownloadable(true)); 117 downloader.download(getDownloadable(true));
110 } 118 };
119
120 exports.checkForUpdates = checkForUpdates;
OLDNEW
« lib/init.js ('K') | « lib/prefs.js ('k') | lib/utils.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld