Skip to content

Commit

Permalink
Merge pull request #7921 from brave/pr7910_fix-serviceworker-adblocki…
Browse files Browse the repository at this point in the history
…ng_1.21.x

Allow adblocking from service workers (uplift to 1.21.x)
  • Loading branch information
kjozwiak authored Feb 18, 2021
2 parents 59c15d8 + 368f212 commit b31c138
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
17 changes: 17 additions & 0 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,23 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrame) {
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 2ULL);
}

// Requests made by a service worker should be blocked as well.
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ServiceWorkerRequest) {
UpdateAdBlockInstanceWithRules("adbanner.js");
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL(kAdBlockTestPage);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();

ASSERT_EQ(true, EvalJs(contents,
"setExpectations(0, 0, 0, 1);"
"installBlockingServiceWorker()"));
// https://github.com/brave/brave-browser/issues/14087
// EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}

// Load a page with an ad image which is matched on the regional blocker,
// but make sure it is saved by the default ad_block_client's exception.
// This test is the same as AdsGetBlockedByRegionalBlocker except for at
Expand Down
15 changes: 4 additions & 11 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,9 @@ class AdblockCnameResolveHostClient : public network::mojom::ResolveHostClient {
void OnBeforeURLRequestAdBlockTP(const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// If the following info isn't available, then proper content settings can't
// be looked up, so do nothing.
if (ctx->tab_origin.is_empty() || !ctx->tab_origin.has_host() ||
ctx->request_url.is_empty()) {
return;
}
DCHECK_NE(ctx->request_identifier, 0UL);
DCHECK(!ctx->request_url.is_empty());
DCHECK(!ctx->initiator_url.is_empty());

scoped_refptr<base::SequencedTaskRunner> task_runner =
g_brave_browser_process->ad_block_service()->GetTaskRunner();
Expand All @@ -211,13 +207,10 @@ void OnBeforeURLRequestAdBlockTP(const ResponseCallback& next_callback,

int OnBeforeURLRequest_AdBlockTPPreWork(const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
if (ctx->request_url.is_empty()) {
return net::OK;
}

// If the following info isn't available, then proper content settings can't
// be looked up, so do nothing.
if (ctx->tab_origin.is_empty() || !ctx->allow_brave_shields ||
if (ctx->request_url.is_empty() || ctx->initiator_url.is_empty() ||
!ctx->initiator_url.has_host() || !ctx->allow_brave_shields ||
ctx->allow_ads ||
ctx->resource_type == BraveRequestInfo::kInvalidResourceType) {
return net::OK;
Expand Down
26 changes: 26 additions & 0 deletions test/data/blocking.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@
});
}

function installBlockingServiceWorker() {
return new Promise(resolve => {
navigator.serviceWorker.addEventListener('message', msg => {
if (msg.data.includes('LOADED')) {
numXHRLoaded++
resolve(onLoad())
} else if (msg.data.includes('FAILED')) {
numXHRBlocked++
resolve(onLoad())
}
});
navigator.serviceWorker.register('./serviceworker.js')
.then(registration => {
if (registration.active) {
registration.active.postMessage('fetch');
} else if (registration.installing) {
registration.installing.addEventListener('statechange', () => {
if (registration.active) {
registration.active.postMessage('fetch');
}
});
}
})
});
}

// Sets the expectation for the number of images and XHR loaded and blocked
function setExpectations(numImgLoaded, numImgBlocked, numXHRLoaded, numXHRBlocked) {
expectedImgLoaded = numImgLoaded
Expand Down
12 changes: 12 additions & 0 deletions test/data/serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
self.addEventListener('message', (event) => {
if (event.data == 'fetch') {
fetch('/adbanner.js')
.then(r => r.text())
.then(data => {
event.source.postMessage('LOADED');
})
.catch((e) => {
event.source.postMessage('FAILED');
});
}
});

0 comments on commit b31c138

Please sign in to comment.