Skip to content

Commit

Permalink
Merge pull request #2047.
Browse files Browse the repository at this point in the history
Fix sporadic DNT header test failures by having Badger's INITIALIZED
flag account for listener initialization and then checking INITIALIZED
before performing the test.
  • Loading branch information
ghostwords committed Sep 7, 2018
2 parents d308e64 + 79452a6 commit 1dfb0f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
35 changes: 16 additions & 19 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ function Badger() {
});

self.storage = new pbStorage.BadgerPen(function(thisStorage) {
if (self.INITIALIZED) { return; }
if (self.INITIALIZED) {
return;
}

self.heuristicBlocking = new HeuristicBlocking.HeuristicBlocker(thisStorage);
self.updateTabList();
self.initializeDefaultSettings();

try {
self.runMigrations();
} finally {
// TODO "await" to set INITIALIZED until both below async functions resolve?
// see TODO in qunit_config.js
self.loadFirstRunSeedData();
self.initializeYellowlist();
self.initializeDNT();
Expand All @@ -74,7 +80,15 @@ function Badger() {
}
});

// TODO: register all privacy badger listeners here in the storage callback
// start all the listeners
incognito.startListeners();
webrequest.startListeners();
HeuristicBlocking.startListeners();
FirefoxAndroid.startListeners();
startBackgroundListeners();

console.log("Privacy Badger is ready to rock!");
console.log("Set DEBUG=1 to view console messages.");

self.INITIALIZED = true;
});
Expand Down Expand Up @@ -856,21 +870,4 @@ function startBackgroundListeners() {
}
}

/**
* lets get this party started
*/
console.log('Loading badgers into the pen.');
var badger = window.badger = new Badger();

/**
* Start all the listeners
*/
incognito.startListeners();
webrequest.startListeners();
HeuristicBlocking.startListeners();
FirefoxAndroid.startListeners();
startBackgroundListeners();

// TODO move listeners and this message behind INITIALIZED
console.log('Privacy badger is ready to rock!');
console.log('Set DEBUG=1 to view console messages.');
12 changes: 2 additions & 10 deletions src/js/heuristicblocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,7 @@ function startListeners() {
* Adds heuristicBlockingAccounting as listened to onBeforeSendHeaders request
*/
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
if (badger) {
return badger.heuristicBlocking.heuristicBlockingAccounting(details);
} else {
return {};
}
return badger.heuristicBlocking.heuristicBlockingAccounting(details);
}, {urls: ["<all_urls>"]}, ["requestHeaders"]);

/**
Expand All @@ -534,11 +530,7 @@ function startListeners() {
}
}
if (hasSetCookie) {
if (badger) {
return badger.heuristicBlocking.heuristicBlockingAccounting(details);
} else {
return {};
}
return badger.heuristicBlocking.heuristicBlockingAccounting(details);
}
},
{urls: ["<all_urls>"]}, ["responseHeaders"]);
Expand Down
5 changes: 5 additions & 0 deletions tests/selenium/dnt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ def test_should_not_record_nontracking_domains(self):

def test_first_party_dnt_header(self):
TEST_URL = "https://httpbin.org/get"

self.load_url(self.bg_url)
# wait until DNT-injecting webRequest listeners have been registered
self.wait_for_script("return badger.INITIALIZED")

headers = retry_until(partial(self.get_first_party_headers, TEST_URL),
times=8)
self.assertTrue(headers is not None, "It seems we failed to get DNT headers")
Expand Down

0 comments on commit 1dfb0f8

Please sign in to comment.