diff --git a/src/js/background.js b/src/js/background.js index 0109be2d64..226afcf128 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -68,8 +68,8 @@ function Badger(from_qunit) { self.setPrivacyOverrides(); // kick off async initialization steps - let ylistPromise = self.initializeYellowlist().catch(console.error), - dntHashesPromise = self.initializeDnt().catch(console.error), + let ylistPromise = self.initYellowlist().catch(console.error), + dntHashesPromise = self.initDntPolicyHashes().catch(console.error), tabDataPromise = self.tabData.initialize().catch(console.error); // async load known CNAME domain aliases (but don't wait on them) @@ -123,23 +123,8 @@ function Badger(from_qunit) { console.log("Set DEBUG=1 to view console messages."); self.INITIALIZED = true; - // get the latest yellowlist from eff.org - self.updateYellowlist(err => { - if (err) { - console.error(err); - } - }); - // set up periodic fetching of the yellowlist from eff.org - setInterval(self.updateYellowlist.bind(self), utils.oneDay()); - - // get the latest DNT policy hashes from eff.org - self.updateDntPolicyHashes(err => { - if (err) { - console.error(err); - } - }); - // set up periodic fetching of hashes from eff.org - setInterval(self.updateDntPolicyHashes.bind(self), utils.oneDay() * 4); + self.initYellowlistUpdates(); + self.initDntPolicyUpdates(); } /** @@ -458,7 +443,7 @@ Badger.prototype = { * * @returns {Promise} */ - initializeYellowlist: function () { + initYellowlist: function () { let self = this; return new Promise(function (resolve, reject) { @@ -484,6 +469,31 @@ Badger.prototype = { }); }, + /** + * Checks if it's time to fetch the latest yellowlist from eff.org. + * If it isn't yet time, schedules the next update for when it is. + */ + initYellowlistUpdates: function () { + let self = this, + next_update_time = self.getPrivateSettings().getItem('nextYellowlistUpdateTime'), + time_now = Date.now(); + + if (time_now < next_update_time) { + let msec_remaining = next_update_time - time_now; + log("Not yet time to update yellowlist; next update in %s mins", + Math.round(msec_remaining / 1000 / 60)); + // schedule an update for when the extension remains running that long + setTimeout(self.updateYellowlist.bind(self), msec_remaining); + return; + } + + self.updateYellowlist(err => { + if (err) { + console.error(err); + } + }); + }, + /** * Updates to the latest yellowlist from eff.org. * @param {Function} [callback] optional callback @@ -541,6 +551,11 @@ Badger.prototype = { self.storage.updateYellowlist(domains); log("Updated yellowlist from remote"); + // refresh next update time so that we don't update too often when the extension gets restarted + self.getPrivateSettings().setItem('nextYellowlistUpdateTime', utils.oneDayFromNow()); + // schedule the next update for when the extension keeps running without restarts + setTimeout(self.updateYellowlist.bind(self), utils.oneDay()); + return callback(null); }); }, @@ -550,7 +565,7 @@ Badger.prototype = { * * @returns {Promise} */ - initializeDnt: function () { + initDntPolicyHashes: function () { let self = this; return new Promise(function (resolve, reject) { @@ -586,6 +601,31 @@ Badger.prototype = { }); }, + /** + * Checks if it's time to get the latest EFF DNT policy hashes from eff.org. + * If it isn't yet time, schedules the next update for when it is. + */ + initDntPolicyUpdates: function () { + let self = this, + next_update_time = self.getPrivateSettings().getItem('nextDntHashesUpdateTime'), + time_now = Date.now(); + + if (time_now < next_update_time) { + let msec_remaining = next_update_time - time_now; + log("Not yet time to update DNT hashes; next update in %s mins", + Math.round(msec_remaining / 1000 / 60)); + // schedule an update for when the extension remains running that long + setTimeout(self.updateDntPolicyHashes.bind(self), msec_remaining); + return; + } + + self.updateDntPolicyHashes(err => { + if (err) { + console.error(err); + } + }); + }, + /** * Fetch acceptable DNT policy hashes from the EFF server * @param {Function} [cb] optional callback @@ -621,6 +661,12 @@ Badger.prototype = { self.storage.updateDntHashes(hashes); log("Updated hashes from remote"); + + // refresh next update time so that we don't update too often when the extension gets restarted + self.getPrivateSettings().setItem('nextDntHashesUpdateTime', utils.nDaysFromNow(4)); + // schedule the next update for when the extension keeps running without restarts + setTimeout(self.updateDntPolicyHashes.bind(self), utils.oneDay() * 4); + return cb(null); }); }, @@ -762,6 +808,8 @@ Badger.prototype = { let privateDefaultSettings = { blockThreshold: constants.TRACKING_THRESHOLD, firstRunTimerFinished: true, + nextDntHashesUpdateTime: 0, + nextYellowlistUpdateTime: 0, showLearningPrompt: false, shownBreakageNotes: [], };