Skip to content

Commit

Permalink
WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Sep 17, 2019
1 parent 2987938 commit 1de6056
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 114 deletions.
5 changes: 0 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 @@ -84,11 +84,6 @@ int OnBeforeURLRequest_AdBlockTPPreWork(
return net::OK;
}

if (GetPolyfillForAdBlock(ctx->allow_brave_shields, ctx->allow_ads,
ctx->tab_origin, ctx->request_url, &ctx->new_url_spec)) {
return net::OK;
}

// Most blocked resources have been moved to our ad block lists.
// This is only for special cases like the PDFjs ping which can
// occur before the ad block lists are fully loaded.
Expand Down
3 changes: 0 additions & 3 deletions browser/net/brave_ad_block_tp_network_delegate_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ int OnBeforeURLRequest_AdBlockTPPreWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);

bool GetPolyfillForAdBlock(bool allow_brave_shields, bool allow_ads,
const GURL& tab_origin, const GURL& gurl, std::string* new_url_spec);

} // namespace brave

#endif // BRAVE_BROWSER_NET_BRAVE_AD_BLOCK_TP_NETWORK_DELEGATE_H_
88 changes: 0 additions & 88 deletions browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"

using brave::GetPolyfillForAdBlock;

namespace {

class BraveAdBlockTPNetworkDelegateHelperTest: public testing::Test {
Expand Down Expand Up @@ -74,32 +72,6 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, EmptyRequestURL) {
EXPECT_EQ(ret, net::OK);
}


TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, RedirectsToStubs) {
std::vector<GURL> urls({
GURL(kGoogleTagManagerPattern),
GURL(kGoogleTagServicesPattern)
});
std::for_each(urls.begin(), urls.end(),
[this](GURL url){
net::TestDelegate test_delegate;
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(url, net::IDLE, &test_delegate,
TRAFFIC_ANNOTATION_FOR_TESTS);
std::shared_ptr<brave::BraveRequestInfo>
brave_request_info(new brave::BraveRequestInfo());
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = OnBeforeURLRequest_AdBlockTPPreWork(callback,
brave_request_info);
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
EXPECT_EQ(ret, net::OK);
EXPECT_TRUE(GURL(brave_request_info->new_url_spec).SchemeIs("data"));
});
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
std::vector<GURL> urls({
GURL("https://pdfjs.robwu.nl/ping"),
Expand All @@ -123,64 +95,4 @@ TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, Blocking) {
});
}

TEST_F(BraveAdBlockTPNetworkDelegateHelperTest, GetPolyfill) {
GURL tab_origin("https://test.com");
GURL google_analytics_url(kGoogleAnalyticsPattern);
GURL tag_manager_url(kGoogleTagManagerPattern);
GURL tag_services_url(kGoogleTagServicesPattern);
GURL normal_url("https://a.com");
std::string out_url_spec;
// Shields up, block ads, google analytics should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin,
google_analytics_url, &out_url_spec));
// Shields up, block ads, tag manager should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_manager_url,
&out_url_spec));
// Shields up, block ads, tag services should get polyfill
ASSERT_TRUE(GetPolyfillForAdBlock(true, false, tab_origin, tag_services_url,
&out_url_spec));
// Shields up, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, false, tab_origin, normal_url,
&out_url_spec));

// Shields up, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin,
google_analytics_url, &out_url_spec));
// Shields up, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_manager_url,
&out_url_spec));
// Shields up, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, tag_services_url,
&out_url_spec));
// Shields up, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(true, true, tab_origin, normal_url,
&out_url_spec));

// Shields down, allow ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin,
google_analytics_url, &out_url_spec));
// Shields down, allow ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_manager_url,
&out_url_spec));
// Shields down, allow ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, tag_services_url,
&out_url_spec));
// Shields down, allow ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, true, tab_origin, normal_url,
&out_url_spec));

// Shields down, block ads, google analytics should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin,
google_analytics_url, &out_url_spec));
// Shields down, block ads, tag manager should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, tag_manager_url,
&out_url_spec));
// Shields down, block ads, tag services should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin,
tag_services_url, &out_url_spec));
// Shields down, block ads, normal URL should NOT get polyfill
ASSERT_FALSE(GetPolyfillForAdBlock(false, false, tab_origin, normal_url,
&out_url_spec));
}

} // namespace
6 changes: 0 additions & 6 deletions common/network_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ const char kCRLSetPrefix3[] =
const char kCRLSetPrefix4[] =
"*://storage.googleapis.com/update-delta/hfnkpimlhhgieaddgfemjhofmfblmnib"
"/*crxd";
const char kGoogleAnalyticsPattern[] =
"https://www.google-analytics.com/analytics.js";
const char kChromeCastPrefix[] =
"*://*.gvt1.com/edgedl/chromewebstore/*pkedcjkdefgpdelpbcmbmeomcjbeemfm*";
const char kGoogleTagManagerPattern[] =
"https://www.googletagmanager.com/gtm.js";
const char kGoogleTagServicesPattern[] =
"https://www.googletagservices.com/tag/js/gpt.js";
const char kForbesPattern[] = "https://www.forbes.com/*";
const char kForbesExtraCookies[] =
"forbes_ab=true; welcomeAd=true; adblock_session=Off; "
Expand Down
3 changes: 0 additions & 3 deletions common/network_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ extern const char kEmptyDataURI[];
extern const char kEmptyImageDataURI[];
extern const char kJSDataURLPrefix[];
extern const char kGeoLocationsPattern[];
extern const char kGoogleAnalyticsPattern[];
extern const char kGoogleTagManagerPattern[];
extern const char kGoogleTagServicesPattern[];
extern const char kForbesPattern[];
extern const char kForbesExtraCookies[];
extern const char kSafeBrowsingPrefix[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ void AdBlockRegionalServiceManager::EnableTag(const std::string& tag,
}
}

void AdBlockRegionalServiceManager::AddResources(
const std::string& resources) {
base::AutoLock lock(regional_services_lock_);
for (const auto& regional_service : regional_services_) {
regional_service.second->AddResources(resources);
}
}

void AdBlockRegionalServiceManager::EnableFilterList(const std::string& uuid,
bool enabled) {
DCHECK(!uuid.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AdBlockRegionalServiceManager {
bool* cancel_request_explicitly,
std::string* redirect);
void EnableTag(const std::string& tag, bool enabled);
void AddResources(const std::string& resources);
void EnableFilterList(const std::string& uuid, bool enabled);

private:
Expand Down
16 changes: 7 additions & 9 deletions components/brave_shields/browser/ad_block_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ std::string AdBlockService::g_ad_block_component_base64_public_key_(

AdBlockService::AdBlockService(
brave_component_updater::BraveComponent::Delegate* delegate)
: AdBlockBaseService(delegate) {}
: AdBlockBaseService(delegate), weak_factory_(this) {
}

AdBlockService::~AdBlockService() {}

Expand All @@ -72,29 +73,26 @@ void AdBlockService::OnComponentReady(const std::string& component_id,
base::FilePath dat_file_path = install_dir.AppendASCII(DAT_FILE);
GetDATFileData(dat_file_path);

install_dir_ = install_dir;
base::FilePath dat_file_path =
base::FilePath resources_file_path =
install_dir.AppendASCII(kAdBlockResourcesFilename);
LOG(ERROR) << "===== Component ready, path: " << resources_file_path;
base::PostTaskAndReplyWithResult(
GetTaskRunner().get(), FROM_HERE,
base::BindOnce(&brave_component_updater::GetDATFileAsString,
dat_file_path),
resources_file_path),
base::BindOnce(&AdBlockService::OnResourcesFileDataReady,
weak_factory_.GetWeakPtr()));
}

void AdBlockService::OnResourcesFileDataReady(std::string resources) {
AddResources(resources);

g_brave_browser_process->ad_block_service()->EnableTag(resources);
LOG(ERROR) << "===== COmponent adding resources: " << resources;
g_brave_browser_process->ad_block_service()->AddResources(resources);
g_brave_browser_process->ad_block_regional_service_manager()->AddResources(
resources);
g_brave_browser_process->ad_block_custom_filters_service()->AddResources(
resources);
}

}

// static
void AdBlockService::SetComponentIdAndBase64PublicKeyForTest(
const std::string& component_id,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AdBlockService : public AdBlockBaseService {
void OnComponentReady(const std::string& component_id,
const base::FilePath& install_dir,
const std::string& manifest) override;
void OnResourcesFileDataReady(std::string resources);

private:
friend class ::AdBlockServiceTest;
Expand All @@ -58,6 +59,7 @@ class AdBlockService : public AdBlockBaseService {
const std::string& component_id,
const std::string& component_base64_public_key);

base::WeakPtrFactory<AdBlockService> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(AdBlockService);
};

Expand Down

0 comments on commit 1de6056

Please sign in to comment.