From e0cc15d537b0ea4f6de59809324c312e6ca0e6e3 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Thu, 20 Feb 2020 17:46:29 -0500 Subject: [PATCH] support merging regional and custom cosmetic selectors --- browser/extensions/api/brave_shields_api.cc | 42 ++++++++++++++++++- common/extensions/api/brave_shields.json | 3 +- .../background/api/cosmeticFilterAPI.ts | 3 +- .../browser/ad_block_base_service.cc | 9 ++-- .../browser/ad_block_base_service.h | 2 +- .../ad_block_regional_service_manager.cc | 31 ++++++++++++++ .../ad_block_regional_service_manager.h | 5 +++ components/definitions/chromel.d.ts | 2 +- 8 files changed, 86 insertions(+), 11 deletions(-) diff --git a/browser/extensions/api/brave_shields_api.cc b/browser/extensions/api/brave_shields_api.cc index aa70fe11ec42..ed48c698c235 100644 --- a/browser/extensions/api/brave_shields_api.cc +++ b/browser/extensions/api/brave_shields_api.cc @@ -92,11 +92,49 @@ BraveShieldsHiddenClassIdSelectorsFunction::Run() { brave_shields::HiddenClassIdSelectors::Params::Create(*args_)); EXTENSION_FUNCTION_VALIDATE(params.get()); - std::string stylesheet = g_brave_browser_process-> + base::Optional selectors = g_brave_browser_process-> ad_block_service()->HiddenClassIdSelectors(params->classes, params->ids, params->exceptions); - return RespondNow(OneArgument(std::make_unique(stylesheet))); + + base::Optional regional_selectors = g_brave_browser_process-> + ad_block_regional_service_manager()-> + HiddenClassIdSelectors(params->classes, + params->ids, + params->exceptions); + + if (selectors && selectors->is_list()) { + if (regional_selectors && regional_selectors->is_list()) { + for (auto i = regional_selectors->GetList().begin(); + i < regional_selectors->GetList().end(); + i++) { + selectors->Append(std::move(*i)); + } + } + } else { + selectors = std::move(regional_selectors); + } + + base::Optional custom_selectors = g_brave_browser_process-> + ad_block_custom_filters_service()-> + HiddenClassIdSelectors(params->classes, + params->ids, + params->exceptions); + + if (selectors && custom_selectors->is_list()) { + if (custom_selectors && custom_selectors->is_list()) { + for (auto i = custom_selectors->GetList().begin(); + i < custom_selectors->GetList().end(); + i++) { + selectors->Append(std::move(*i)); + } + } + } else { + selectors = std::move(custom_selectors); + } + + return RespondNow(OneArgument(std::make_unique( + std::move(*selectors)))); } diff --git a/common/extensions/api/brave_shields.json b/common/extensions/api/brave_shields.json index 832f924945f1..f3e859b65ffa 100644 --- a/common/extensions/api/brave_shields.json +++ b/common/extensions/api/brave_shields.json @@ -126,7 +126,8 @@ "parameters": [ { "name": "selectorsJson", - "type": "string" + "type": "array", + "items": {"type": "string"} } ] } diff --git a/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts b/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts index 580ea2c6efc7..bec58c3d30fe 100644 --- a/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts +++ b/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts @@ -19,8 +19,7 @@ const informTabOfCosmeticRulesToConsider = (tabId: number, selectors: string[]) // Fires when content-script calls hiddenClassIdSelectors export const injectClassIdStylesheet = (tabId: number, classes: string[], ids: string[], exceptions: string[]) => { - chrome.braveShields.hiddenClassIdSelectors(classes, ids, exceptions, (jsonSelectors) => { - const selectors = JSON.parse(jsonSelectors) + chrome.braveShields.hiddenClassIdSelectors(classes, ids, exceptions, (selectors) => { informTabOfCosmeticRulesToConsider(tabId, selectors) }) } diff --git a/components/brave_shields/browser/ad_block_base_service.cc b/components/brave_shields/browser/ad_block_base_service.cc index 6d2b10843181..89d2fb9ee71f 100644 --- a/components/brave_shields/browser/ad_block_base_service.cc +++ b/components/brave_shields/browser/ad_block_base_service.cc @@ -202,13 +202,14 @@ base::Optional AdBlockBaseService::HostnameCosmeticResources( this->ad_block_client_->hostnameCosmeticResources(hostname)); } -std::string AdBlockBaseService::HiddenClassIdSelectors( +base::Optional AdBlockBaseService::HiddenClassIdSelectors( const std::vector& classes, const std::vector& ids, const std::vector& exceptions) { - return this->ad_block_client_->hiddenClassIdSelectors(classes, - ids, - exceptions); + return base::JSONReader::Read( + this->ad_block_client_->hiddenClassIdSelectors(classes, + ids, + exceptions)); } void AdBlockBaseService::GetDATFileData(const base::FilePath& dat_file_path) { diff --git a/components/brave_shields/browser/ad_block_base_service.h b/components/brave_shields/browser/ad_block_base_service.h index 887f9a41df90..074844ac00cf 100644 --- a/components/brave_shields/browser/ad_block_base_service.h +++ b/components/brave_shields/browser/ad_block_base_service.h @@ -49,7 +49,7 @@ class AdBlockBaseService : public BaseBraveShieldsService { base::Optional HostnameCosmeticResources( const std::string& hostname); - std::string HiddenClassIdSelectors( + base::Optional HiddenClassIdSelectors( const std::vector& classes, const std::vector& ids, const std::vector& exceptions); diff --git a/components/brave_shields/browser/ad_block_regional_service_manager.cc b/components/brave_shields/browser/ad_block_regional_service_manager.cc index 05537e44e47f..562220faf0d1 100644 --- a/components/brave_shields/browser/ad_block_regional_service_manager.cc +++ b/components/brave_shields/browser/ad_block_regional_service_manager.cc @@ -217,6 +217,37 @@ AdBlockRegionalServiceManager::HostnameCosmeticResources( return first_value; } +base::Optional +AdBlockRegionalServiceManager::HiddenClassIdSelectors( + const std::vector& classes, + const std::vector& ids, + const std::vector& exceptions) { + if (this->regional_services_.size() == 0) { + return base::Optional(); + } + auto it = this->regional_services_.begin(); + base::Optional first_value = + it->second->HiddenClassIdSelectors(classes, ids, exceptions); + + for ( ; it != this->regional_services_.end(); it++) { + base::Optional next_value = + it->second->HiddenClassIdSelectors(classes, ids, exceptions); + if (first_value && first_value->is_list()) { + if (next_value && next_value->is_list()) { + for (auto i = next_value->GetList().begin(); + i < next_value->GetList().end(); + i++) { + first_value->Append(std::move(*i)); + } + } + } else { + first_value = std::move(next_value); + } + } + + return first_value; +} + // static bool AdBlockRegionalServiceManager::IsSupportedLocale( const std::string& locale) { diff --git a/components/brave_shields/browser/ad_block_regional_service_manager.h b/components/brave_shields/browser/ad_block_regional_service_manager.h index 0fd22650bf5c..d4b188017cde 100644 --- a/components/brave_shields/browser/ad_block_regional_service_manager.h +++ b/components/brave_shields/browser/ad_block_regional_service_manager.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "base/macros.h" #include "base/memory/scoped_refptr.h" @@ -56,6 +57,10 @@ class AdBlockRegionalServiceManager { base::Optional HostnameCosmeticResources( const std::string& hostname); + base::Optional HiddenClassIdSelectors( + const std::vector& classes, + const std::vector& ids, + const std::vector& exceptions); private: friend class ::AdBlockServiceTest; diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 4185f4cf3429..88122ad6b3eb 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -220,7 +220,7 @@ declare namespace chrome.braveShields { injected_script: string } const hostnameCosmeticResources: (hostname: string, callback: (resources: HostnameSpecificResources) => void) => void - const hiddenClassIdSelectors: (classes: string[], ids: string[], exceptions: string[], callback: (selectorsJson: string) => void) => void + const hiddenClassIdSelectors: (classes: string[], ids: string[], exceptions: string[], callback: (selectors: string[]) => void) => void type BraveShieldsViewPreferences = { showAdvancedView: boolean