Skip to content

Commit

Permalink
support exception rules in the custom filters box of brave://adblock
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Jan 20, 2021
1 parent 33ebfab commit 22bbba5
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 64 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use_relative_paths = True

deps = {
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@8c5103fd864e23cb10e0a8e0ec1169bc6f6246bf",
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@31c37afa3e9b777257502fdacd0944c41ab12de4",
"vendor/extension-whitelist": "https://github.com/brave/extension-whitelist.git@b4d059c73042cacf3a5e9156d4b1698e7bc18678",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@6eab0271d014ff09bd9f38abe1e0c117e13e9aa9",
"vendor/requests": "https://github.com/kennethreitz/requests@e4d59bedfd3c7f4f254f4f5d036587bcd8152458",
Expand Down
7 changes: 5 additions & 2 deletions browser/net/brave_ad_block_tp_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ content::WebContents* GetWebContents(int render_process_id,
void ShouldBlockAdOnTaskRunner(std::shared_ptr<BraveRequestInfo> ctx,
base::Optional<std::string> canonical_name) {
bool did_match_exception = false;
bool did_match_important = false;
if (!ctx->initiator_url.is_valid()) {
return;
}
std::string source_host = ctx->initiator_url.host();
if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
ctx->request_url, ctx->resource_type, source_host,
&did_match_exception, &ctx->mock_data_url)) {
&did_match_exception, &did_match_important, &ctx->mock_data_url,
false, false)) {
ctx->blocked_by = kAdBlocked;
} else if (!did_match_exception && canonical_name.has_value() &&
ctx->request_url.host() != *canonical_name &&
Expand All @@ -76,7 +78,8 @@ void ShouldBlockAdOnTaskRunner(std::shared_ptr<BraveRequestInfo> ctx,

if (!g_brave_browser_process->ad_block_service()->ShouldStartRequest(
canonical_url, ctx->resource_type, source_host,
&did_match_exception, &ctx->mock_data_url)) {
&did_match_exception, &did_match_important, &ctx->mock_data_url,
false, false)) {
ctx->blocked_by = kAdBlocked;
}
}
Expand Down
38 changes: 17 additions & 21 deletions components/brave_shields/browser/ad_block_base_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "brave/common/pref_names.h"
#include "brave/components/brave_component_updater/browser/dat_file_util.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
Expand Down Expand Up @@ -118,7 +118,10 @@ bool AdBlockBaseService::ShouldStartRequest(
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url) {
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) {
DCHECK(GetTaskRunner()->RunsTasksInCurrentSequence());

// Determine third-party here so the library doesn't need to figure it out.
Expand All @@ -128,27 +131,20 @@ bool AdBlockBaseService::ShouldStartRequest(
url,
url::Origin::CreateFromNormalizedTuple("https", tab_host.c_str(), 80),
INCLUDE_PRIVATE_REGISTRIES);
bool saved_from_exception;
if (ad_block_client_->matches(url.spec(), url.host(), tab_host,
is_third_party,
ResourceTypeToString(resource_type),
&saved_from_exception, mock_data_url)) {
// We'd only possibly match an exception filter if we're returning true.
if (did_match_exception) {
*did_match_exception = false;
}
// LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: "
// << tab_host
// << ", resource type: " << resource_type
// << ", url.spec(): " << url.spec();
return false;
}
bool matched_exception_here;
bool matched = ad_block_client_->matches(
url.spec(), url.host(), tab_host, is_third_party,
ResourceTypeToString(resource_type), &matched_exception_here,
did_match_important, mock_data_url, previously_matched_rule,
previously_matched_exception);

if (did_match_exception) {
*did_match_exception = saved_from_exception;
}
*did_match_exception |= matched_exception_here;

return true;
// LOG(ERROR) << "AdBlockBaseService::ShouldStartRequest(), host: "
// << tab_host
// << ", resource type: " << resource_type
// << ", url.spec(): " << url.spec();
return !matched;
}

void AdBlockBaseService::EnableTag(const std::string& tag, bool enabled) {
Expand Down
5 changes: 4 additions & 1 deletion components/brave_shields/browser/ad_block_base_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class AdBlockBaseService : public BaseBraveShieldsService {
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url) override;
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) override;
void AddResources(const std::string& resources);
void EnableTag(const std::string& tag, bool enabled);
bool TagExists(const std::string& tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_service_helper.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "components/prefs/pref_service.h"

namespace brave_shields {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#include "base/files/file_path.h"
#include "brave/components/brave_shields/browser/ad_block_base_service.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"

class AdBlockServiceTest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "brave/components/brave_shields/browser/ad_block_regional_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_service_helper.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/browser_task_traits.h"
Expand Down Expand Up @@ -127,18 +127,26 @@ bool AdBlockRegionalServiceManager::ShouldStartRequest(
const GURL& url,
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* matching_exception_filter,
std::string* mock_data_url) {
bool* did_match_exception,
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) {
base::AutoLock lock(regional_services_lock_);

for (const auto& regional_service : regional_services_) {
if (!regional_service.second->ShouldStartRequest(
url, resource_type, tab_host, matching_exception_filter,
mock_data_url)) {
bool matched = !regional_service.second->ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, did_match_important,
mock_data_url, previously_matched_rule, previously_matched_exception);
if (did_match_important && *did_match_important) {
return false;
}
if (matching_exception_filter && *matching_exception_filter) {
return true;
}
previously_matched_rule |= matched;
previously_matched_exception |= *did_match_exception;
}

if (previously_matched_rule && !previously_matched_exception) {
return false;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "base/synchronization/lock.h"
#include "base/values.h"
#include "brave/components/brave_component_updater/browser/brave_component.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom-shared.h"
#include "url/gurl.h"

Expand Down Expand Up @@ -50,8 +50,11 @@ class AdBlockRegionalServiceManager {
bool ShouldStartRequest(const GURL& url,
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* matching_exception_filter,
std::string* mock_data_url);
bool* did_match_exception,
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception);
void EnableTag(const std::string& tag, bool enabled);
void AddResources(const std::string& resources);
void EnableFilterList(const std::string& uuid, bool enabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_shields/browser/ad_block_service_helper.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "testing/gtest/include/gtest/gtest.h"

TEST(AdBlockRegionalServiceTest, UserModelLanguages) {
Expand Down
43 changes: 26 additions & 17 deletions components/brave_shields/browser/ad_block_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
#include "brave/components/brave_shields/browser/ad_block_service_helper.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
Expand Down Expand Up @@ -77,30 +77,39 @@ bool AdBlockService::ShouldStartRequest(
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url) {

if (!AdBlockBaseService::ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, mock_data_url)) {
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) {
bool matched = !AdBlockBaseService::ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, did_match_important,
mock_data_url, previously_matched_rule, previously_matched_exception);
if (did_match_important && *did_match_important) {
return false;
}
if (did_match_exception && *did_match_exception) {
return true;
}
previously_matched_rule |= matched;
previously_matched_exception |= *did_match_exception;

if (!regional_service_manager()->ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, mock_data_url)) {
matched = !regional_service_manager()->ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, did_match_important,
mock_data_url, previously_matched_rule, previously_matched_exception);
if (did_match_important && *did_match_important) {
return false;
}
if (did_match_exception && *did_match_exception) {
return true;
}
previously_matched_rule |= matched;
previously_matched_exception |= *did_match_exception;

if (!custom_filters_service()->ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, mock_data_url)) {
matched = !custom_filters_service()->ShouldStartRequest(
url, resource_type, tab_host, did_match_exception, did_match_important,
mock_data_url, previously_matched_rule, previously_matched_exception);
if (did_match_important && *did_match_important) {
return false;
}
if (did_match_exception && *did_match_exception) {
return true;
previously_matched_rule |= matched;
previously_matched_exception |= *did_match_exception;

if (previously_matched_rule && !previously_matched_exception) {
return false;
}

return true;
Expand Down
5 changes: 4 additions & 1 deletion components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class AdBlockService : public AdBlockBaseService {
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url) override;
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) override;

AdBlockRegionalServiceManager* regional_service_manager();
AdBlockCustomFiltersService* custom_filters_service();
Expand Down
2 changes: 1 addition & 1 deletion components/brave_shields/browser/ad_block_service_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <vector>

#include "base/values.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.h"

namespace brave_shields {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ bool BaseBraveShieldsService::ShouldStartRequest(
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url) {
if (did_match_exception) {
*did_match_exception = false;
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception) {
if (previously_matched_rule && !previously_matched_exception) {
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class BaseBraveShieldsService : public BraveComponent {
blink::mojom::ResourceType resource_type,
const std::string& tab_host,
bool* did_match_exception,
std::string* mock_data_url);
bool* did_match_important,
std::string* mock_data_url,
bool previously_matched_rule,
bool previously_matched_exception);

protected:
virtual bool Init() = 0;
Expand Down

0 comments on commit 22bbba5

Please sign in to comment.