OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 let {Prefs} = require("prefs"); |
| 19 let updateUrl = (_appInfo.developmentBuild ? Prefs.update_url_devbuild : Prefs.u
pdate_url_release); |
| 20 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 |
| 25 const HOURS_TO_MILLIS = 60 * 60 * 1000; |
| 26 const MIN_CHECK_INTERVAL = 18 * HOURS_TO_MILLIS; |
| 27 const MAX_CHECK_INTERVAL = 30 * HOURS_TO_MILLIS; |
| 28 |
| 29 const TYPE_AUTOMATIC = 0; |
| 30 const TYPE_MANUAL = 1; |
| 31 |
| 32 let checkForUpdates = exports.checkForUpdates = function checkForUpdates(forceCh
eck, callback) |
| 33 { |
| 34 let now = Date.now(); |
| 35 if (!forceCheck && now < Prefs.next_update_check) |
| 36 { |
| 37 if (Prefs.next_update_check - now > MAX_CHECK_INTERVAL) |
| 38 Prefs.next_update_check = now + MAX_CHECK_INTERVAL; |
| 39 |
| 40 window.setTimeout(checkForUpdates, Prefs.next_update_check - now); |
| 41 return; |
| 42 } |
| 43 |
| 44 Prefs.next_update_check = now + MIN_CHECK_INTERVAL + |
| 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 |
| 92 window.setTimeout(checkForUpdates, 0.1 * HOURS_TO_MILLIS); |
OLD | NEW |