Skip to content

Commit

Permalink
Uplift of #4700 (squashed) to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Feb 27, 2020
1 parent f2251ad commit 1dbae02
Show file tree
Hide file tree
Showing 12 changed files with 410 additions and 14 deletions.
61 changes: 59 additions & 2 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
#include "brave/browser/webcompat_reporter/webcompat_reporter_dialog.h"
#include "brave/common/extensions/api/brave_shields.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_shields/browser/ad_block_base_service.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#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/components/brave_shields/browser/brave_shields_p3a.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
Expand Down Expand Up @@ -59,6 +63,29 @@ BraveShieldsHostnameCosmeticResourcesFunction::Run() {
return RespondNow(Error(
"Hostname-specific cosmetic resources could not be returned"));
}

base::Optional<base::Value> regional_resources = g_brave_browser_process->
ad_block_regional_service_manager()->
HostnameCosmeticResources(params->hostname);

if (regional_resources && regional_resources->is_dict()) {
::brave_shields::MergeResourcesInto(
&*resources,
&*regional_resources,
false);
}

base::Optional<base::Value> custom_resources = g_brave_browser_process->
ad_block_custom_filters_service()->
HostnameCosmeticResources(params->hostname);

if (custom_resources && custom_resources->is_dict()) {
::brave_shields::MergeResourcesInto(
&*resources,
&*custom_resources,
true);
}

auto result_list = std::make_unique<base::ListValue>();

result_list->GetList().push_back(std::move(*resources));
Expand All @@ -72,11 +99,41 @@ BraveShieldsHiddenClassIdSelectorsFunction::Run() {
brave_shields::HiddenClassIdSelectors::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

std::string stylesheet = g_brave_browser_process->
base::Optional<base::Value> hide_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 (hide_selectors && hide_selectors->is_list()) {
if (regional_selectors && regional_selectors->is_list()) {
for (auto i = regional_selectors->GetList().begin();
i < regional_selectors->GetList().end();
i++) {
hide_selectors->Append(std::move(*i));
}
}
} else {
hide_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);

auto result_list = std::make_unique<base::ListValue>();

result_list->GetList().push_back(std::move(*hide_selectors));
result_list->GetList().push_back(std::move(*custom_selectors));

return RespondNow(ArgumentList(std::move(result_list)));
}


Expand Down
15 changes: 11 additions & 4 deletions common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@
"name": "hostnameSpecificResources",
"type": "object",
"properties": {
"hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "Hostname-specific CSS selectors that should be hidden from the page"},
"hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "Hostname-specific CSS selectors that should be hidden from the page if they are determined to not include 1st party content"},
"style_selectors": {"type": "object", "additionalProperties": {"type": "array", "items": {"type": "string"}}, "description": "Hostname-specific CSS selectors that should be restyled, with their associated CSS style rules"},
"exceptions": {"type": "array", "items": {"type": "string"}, "description": "Hostname-specific overrides for generic cosmetic blocking selectors"},
"injected_script": {"type": "string", "description": "A script to inject as the page is loading"}
"injected_script": {"type": "string", "description": "A script to inject as the page is loading"},
"force_hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "Hostname-specific CSS selectors that should be hidden from the page"}
}
}
]
Expand Down Expand Up @@ -125,8 +126,14 @@
"name": "callback",
"parameters": [
{
"name": "selectorsJson",
"type": "string"
"name": "selectors",
"type": "array",
"items": {"type": "string"}
},
{
"name": "forceHideSelectors",
"type": "array",
"items": {"type": "string"}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ 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, forceHideSelectors) => {
informTabOfCosmeticRulesToConsider(tabId, selectors)

if (forceHideSelectors.length > 0) {
const forceHideStylesheet = forceHideSelectors.join(',') + '{display:none!important;}\n'

chrome.tabs.insertCSS(tabId, {
code: forceHideStylesheet,
cssOrigin: 'user',
runAt: 'document_start'
})
}
})
}

Expand All @@ -34,7 +43,11 @@ export const applyAdblockCosmeticFilters = (tabId: number, hostname: string) =>
}

informTabOfCosmeticRulesToConsider(tabId, resources.hide_selectors)

let styledStylesheet = ''
if (resources.force_hide_selectors.length > 0) {
styledStylesheet += resources.force_hide_selectors.join(',') + '{display:none!important;}\n'
}
for (const selector in resources.style_selectors) {
styledStylesheet += selector + '{' + resources.style_selectors[selector].join(';') + ';}\n'
}
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 @@ -191,6 +191,62 @@ void AdBlockRegionalServiceManager::EnableFilterList(const std::string& uuid,
base::Unretained(this), uuid, enabled));
}

base::Optional<base::Value>
AdBlockRegionalServiceManager::HostnameCosmeticResources(
const std::string& hostname) {
auto it = this->regional_services_.begin();
if (it == this->regional_services_.end()) {
return base::Optional<base::Value>();
}
base::Optional<base::Value> first_value =
it->second->HostnameCosmeticResources(hostname);

for ( ; it != this->regional_services_.end(); it++) {
base::Optional<base::Value> next_value =
it->second->HostnameCosmeticResources(hostname);
if (first_value) {
if (next_value) {
MergeResourcesInto(&*first_value, &*next_value, false);
}
} else {
first_value = std::move(next_value);
}
}

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) {
auto it = this->regional_services_.begin();
if (it == this->regional_services_.end()) {
return base::Optional<base::Value>();
}
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,10 +9,13 @@
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/optional.h"
#include "base/synchronization/lock.h"
#include "base/values.h"
#include "brave/components/brave_component_updater/browser/brave_component.h"
#include "content/public/common/resource_type.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -52,6 +55,13 @@ class AdBlockRegionalServiceManager {
void AddResources(const std::string& resources);
void EnableFilterList(const std::string& uuid, bool enabled);

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;
bool Init();
Expand Down
72 changes: 72 additions & 0 deletions components/brave_shields/browser/ad_block_service_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "brave/components/brave_shields/browser/ad_block_service_helper.h"

#include <algorithm>
#include <utility>

#include "base/strings/string_util.h"
#include "base/values.h"

using adblock::FilterList;

Expand Down Expand Up @@ -44,4 +46,74 @@ std::vector<FilterList>::const_iterator FindAdBlockFilterListByLocale(
});
}

// Merges the contents of the second HostnameCosmeticResources Value into the
// first one provided.
//
// If `force_hide` is true, the contents of `from`'s `hide_selectors` field
// will be moved into a possibly new field of `into` called
// `force_hide_selectors`.
void MergeResourcesInto(
base::Value* into,
base::Value* from,
bool force_hide) {
base::Value* resources_hide_selectors = nullptr;
if (force_hide) {
resources_hide_selectors = into->FindKey("force_hide_selectors");
if (!resources_hide_selectors || !resources_hide_selectors->is_list()) {
into->SetPath("force_hide_selectors", base::ListValue());
resources_hide_selectors = into->FindKey("force_hide_selectors");
}
} else {
resources_hide_selectors = into->FindKey("hide_selectors");
}
base::Value* from_resources_hide_selectors =
from->FindKey("hide_selectors");
if (resources_hide_selectors && from_resources_hide_selectors) {
for (auto i = from_resources_hide_selectors->GetList().begin();
i < from_resources_hide_selectors->GetList().end();
i++) {
resources_hide_selectors->Append(std::move(*i));
}
}

base::Value* resources_style_selectors = into->FindKey("style_selectors");
base::Value* from_resources_style_selectors =
from->FindKey("style_selectors");
if (resources_style_selectors && from_resources_style_selectors) {
for (auto i : from_resources_style_selectors->DictItems()) {
base::Value* resources_entry =
resources_style_selectors->FindKey(i.first);
if (resources_entry) {
for (auto j = i.second.GetList().begin();
j < i.second.GetList().end();
j++) {
resources_entry->Append(std::move(*j));
}
} else {
resources_style_selectors->SetPath(i.first, std::move(i.second));
}
}
}

base::Value* resources_exceptions = into->FindKey("exceptions");
base::Value* from_resources_exceptions = from->FindKey("exceptions");
if (resources_exceptions && from_resources_exceptions) {
for (auto i = from_resources_exceptions->GetList().begin();
i < from_resources_exceptions->GetList().end();
i++) {
resources_exceptions->Append(std::move(*i));
}
}

base::Value* resources_injected_script = into->FindKey("injected_script");
base::Value* from_resources_injected_script =
from->FindKey("injected_script");
if (resources_injected_script && from_resources_injected_script) {
*resources_injected_script = base::Value(
resources_injected_script->GetString()
+ '\n'
+ from_resources_injected_script->GetString());
}
}

} // namespace brave_shields
3 changes: 3 additions & 0 deletions components/brave_shields/browser/ad_block_service_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

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

namespace brave_shields {
Expand All @@ -20,6 +21,8 @@ std::vector<adblock::FilterList>::const_iterator FindAdBlockFilterListByLocale(
const std::vector<adblock::FilterList>& region_lists,
const std::string& locale);

void MergeResourcesInto(base::Value* into, base::Value* from, bool force_hide);

} // namespace brave_shields

#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_SERVICE_HELPER_H_
Loading

0 comments on commit 1dbae02

Please sign in to comment.