Skip to content

Commit

Permalink
Update static assets only if enough time elapsed
Browse files Browse the repository at this point in the history
Instead of unconditionally on every extension startup.
  • Loading branch information
ghostwords committed Feb 28, 2023
1 parent ef3fdbf commit 6f8230a
Showing 1 changed file with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -458,7 +443,7 @@ Badger.prototype = {
*
* @returns {Promise}
*/
initializeYellowlist: function () {
initYellowlist: function () {
let self = this;

return new Promise(function (resolve, reject) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
});
},
Expand All @@ -550,7 +565,7 @@ Badger.prototype = {
*
* @returns {Promise}
*/
initializeDnt: function () {
initDntPolicyHashes: function () {
let self = this;

return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
},
Expand Down Expand Up @@ -762,6 +808,8 @@ Badger.prototype = {
let privateDefaultSettings = {
blockThreshold: constants.TRACKING_THRESHOLD,
firstRunTimerFinished: true,
nextDntHashesUpdateTime: 0,
nextYellowlistUpdateTime: 0,
showLearningPrompt: false,
shownBreakageNotes: [],
};
Expand Down

0 comments on commit 6f8230a

Please sign in to comment.