Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count TP blocks separately on newtab page #703

Merged
merged 1 commit into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ void OnBeforeURLRequestAdBlockTPOnTaskRunner(std::shared_ptr<BraveRequestInfo> c
if (!g_brave_browser_process->tracking_protection_service()->
ShouldStartRequest(ctx->request_url, ctx->resource_type, ctx->tab_origin.host())) {
ctx->new_url_spec = GetBlankDataURLForResourceType(ctx->resource_type).spec();
ctx->blocked_by = kTrackerBlocked;
} else if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
ctx->request_url, ctx->resource_type, ctx->tab_origin.host()) ||
!g_brave_browser_process->ad_block_regional_service()
->ShouldStartRequest(ctx->request_url, ctx->resource_type,
ctx->tab_origin.host())) {
ctx->new_url_spec = GetBlankDataURLForResourceType(ctx->resource_type).spec();
ctx->blocked_by = kAdBlocked;
}
}

Expand All @@ -115,11 +117,15 @@ void OnBeforeURLRequestDispatchOnIOThread(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!ctx->new_url_spec.empty() &&
ctx->new_url_spec != ctx->request_url.spec()) {
// TODO: If we ever want to differentiate ads from tracking library
// counts, then use brave_shields::kTrackers below.
brave_shields::DispatchBlockedEventFromIO(ctx->request_url,
ctx->render_process_id, ctx->render_frame_id, ctx->frame_tree_node_id,
brave_shields::kAds);
if (ctx->blocked_by == kAdBlocked) {
brave_shields::DispatchBlockedEventFromIO(ctx->request_url,
ctx->render_process_id, ctx->render_frame_id, ctx->frame_tree_node_id,
brave_shields::kAds);
} else if (ctx->blocked_by == kTrackerBlocked) {
brave_shields::DispatchBlockedEventFromIO(ctx->request_url,
ctx->render_process_id, ctx->render_frame_id, ctx->frame_tree_node_id,
brave_shields::kTrackers);
}
}

next_callback.Run();
Expand Down
8 changes: 8 additions & 0 deletions browser/net/url_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ enum BraveNetworkDelegateEventType {
kUnknownEventType
};

enum BlockedBy {
kNotBlocked ,
kAdBlocked,
kTrackerBlocked,
kOtherBlocked
};

struct BraveRequestInfo {
BraveRequestInfo();
~BraveRequestInfo();
Expand All @@ -57,6 +64,7 @@ struct BraveRequestInfo {
GURL* allowed_unsafe_redirect_url = nullptr;
BraveNetworkDelegateEventType event_type = kUnknownEventType;
const base::ListValue* referral_headers_list = nullptr;
BlockedBy blocked_by = kNotBlocked;
// Default to invalid type for resource_type, so delegate helpers
// can properly detect that the info couldn't be obtained.
content::ResourceType resource_type = content::RESOURCE_TYPE_LAST_TYPE;
Expand Down
13 changes: 13 additions & 0 deletions components/brave_shields/browser/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "base/test/thread_test_helper.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/brave_paths.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_utils.h"

using extensions::ExtensionBrowserTest;
Expand Down Expand Up @@ -150,6 +152,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) {
kDefaultAdBlockComponentTestId,
kDefaultAdBlockComponentTestBase64PublicKey);
ASSERT_TRUE(InstallDefaultAdBlockExtension());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL(kAdBlockTestPage);
ui_test_utils::NavigateToURL(browser(), url);
Expand All @@ -163,6 +166,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) {
"addElement('ad_banner.png')",
&img_loaded));
EXPECT_FALSE(img_loaded);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}

// Load a page with an image which is not an ad, and make sure it is NOT blocked.
Expand All @@ -171,6 +175,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByDefaultBlocker
kDefaultAdBlockComponentTestId,
kDefaultAdBlockComponentTestBase64PublicKey);
ASSERT_TRUE(InstallDefaultAdBlockExtension());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL(kAdBlockTestPage);
ui_test_utils::NavigateToURL(browser(), url);
Expand All @@ -184,6 +189,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByDefaultBlocker
"addElement('logo.png')",
&img_loaded));
EXPECT_TRUE(img_loaded);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
}

// Load a page with an ad image, and make sure it is blocked by the
Expand All @@ -193,6 +199,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByRegionalBlocker) {
ASSERT_EQ(g_browser_process->GetApplicationLocale(), "fr");

ASSERT_TRUE(StartAdBlockRegionalService());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

SetRegionalComponentIdAndBase64PublicKeyForTest(
kRegionalAdBlockComponentTestId,
Expand All @@ -211,6 +218,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByRegionalBlocker) {
"addElement('ad_fr.png')",
&img_loaded));
EXPECT_FALSE(img_loaded);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}

// Load a page with an image which is not an ad, and make sure it is
Expand All @@ -220,6 +228,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByRegionalBlocke
ASSERT_EQ(g_browser_process->GetApplicationLocale(), "fr");

ASSERT_TRUE(StartAdBlockRegionalService());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

SetRegionalComponentIdAndBase64PublicKeyForTest(
kRegionalAdBlockComponentTestId,
Expand All @@ -238,6 +247,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByRegionalBlocke
"addElement('logo.png')",
&img_loaded));
EXPECT_TRUE(img_loaded);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);
}

// Upgrade from v3 to v4 format data file and make sure v4-specific ad
Expand All @@ -257,6 +267,8 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedAfterDataFileVersionUpgr
SetDATFileVersionForTest("4");
ASSERT_TRUE(InstallDefaultAdBlockExtension("adblock-v4", 0));

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();
Expand All @@ -269,4 +281,5 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedAfterDataFileVersionUpgr
"addElement('v4_specific_banner.png')",
&img_loaded));
EXPECT_FALSE(img_loaded);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include "base/test/thread_test_helper.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/brave_paths.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"

Expand Down Expand Up @@ -111,6 +113,8 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest {
IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, TrackerReferencedFromTrustedDomainNotBlocked) {
ASSERT_TRUE(InstallTrackingProtectionExtension());

EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL("365media.com", kTrackingPage);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
Expand All @@ -125,12 +129,15 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, TrackerReferencedFromTrust
base::StringPrintf(kTrackingScript, test_url.spec().c_str()),
&img_loaded));
EXPECT_TRUE(img_loaded);

EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 0ULL);
}

// Load a page that references a tracker from an untrusted domain, and
// make sure it is blocked.
IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, TrackerReferencedFromUntrustedDomainGetsBlocked) {
ASSERT_TRUE(InstallTrackingProtectionExtension());
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL("google.com", kTrackingPage);
ui_test_utils::NavigateToURL(browser(), url);
Expand All @@ -146,4 +153,6 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, TrackerReferencedFromUntru
base::StringPrintf(kTrackingScript, test_url.spec().c_str()),
&img_loaded));
EXPECT_FALSE(img_loaded);

EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), 1ULL);
}