Skip to content

Commit

Permalink
support merging regional and custom cosmetic selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Feb 20, 2020
1 parent 9c05217 commit e0cc15d
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 11 deletions.
42 changes: 40 additions & 2 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<base::Value> selectors = g_brave_browser_process->
ad_block_service()->HiddenClassIdSelectors(params->classes,
params->ids,
params->exceptions);
return RespondNow(OneArgument(std::make_unique<base::Value>(stylesheet)));

base::Optional<base::Value> 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<base::Value> 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<base::Value>(
std::move(*selectors))));
}


Expand Down
3 changes: 2 additions & 1 deletion common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
"parameters": [
{
"name": "selectorsJson",
"type": "string"
"type": "array",
"items": {"type": "string"}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
9 changes: 5 additions & 4 deletions components/brave_shields/browser/ad_block_base_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ base::Optional<base::Value> AdBlockBaseService::HostnameCosmeticResources(
this->ad_block_client_->hostnameCosmeticResources(hostname));
}

std::string AdBlockBaseService::HiddenClassIdSelectors(
base::Optional<base::Value> AdBlockBaseService::HiddenClassIdSelectors(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& 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) {
Expand Down
2 changes: 1 addition & 1 deletion components/brave_shields/browser/ad_block_base_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class AdBlockBaseService : public BaseBraveShieldsService {

base::Optional<base::Value> HostnameCosmeticResources(
const std::string& hostname);
std::string HiddenClassIdSelectors(
base::Optional<base::Value> HiddenClassIdSelectors(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& exceptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,37 @@ AdBlockRegionalServiceManager::HostnameCosmeticResources(
return first_value;
}

base::Optional<base::Value>
AdBlockRegionalServiceManager::HiddenClassIdSelectors(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& exceptions) {
if (this->regional_services_.size() == 0) {
return base::Optional<base::Value>();
}
auto it = this->regional_services_.begin();
base::Optional<base::Value> first_value =
it->second->HiddenClassIdSelectors(classes, ids, exceptions);

for ( ; it != this->regional_services_.end(); it++) {
base::Optional<base::Value> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
Expand Down Expand Up @@ -56,6 +57,10 @@ class AdBlockRegionalServiceManager {

base::Optional<base::Value> HostnameCosmeticResources(
const std::string& hostname);
base::Optional<base::Value> HiddenClassIdSelectors(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& exceptions);

private:
friend class ::AdBlockServiceTest;
Expand Down
2 changes: 1 addition & 1 deletion components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e0cc15d

Please sign in to comment.