Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement cosmetic filtering #3303

Merged
merged 9 commits into from Dec 14, 2019
Next

expose cosmetic filtering javascript API

return a single script for cosmetic scriptlet injection
  • Loading branch information
antonok-edm committed Aug 8, 2019
commit 17150464de8f18b4941afa02030ac08c062c7336
2 DEPS
@@ -1,7 +1,7 @@
use_relative_paths = True

deps = {
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@89127a30655eaf54cf73794309846084ea8b91b9",
"vendor/adblock_rust_ffi": "https://github.com/brave/adblock-rust-ffi.git@d757c647699ff7e6dacc5d7bbd51649b509609a5",
"vendor/autoplay-whitelist": "https://github.com/brave/autoplay-whitelist.git@ea527a4d36051daedb34421e129c98eda06cb5d3",
"vendor/extension-whitelist": "https://github.com/brave/extension-whitelist.git@7843f62e26a23c51336330e220e9d7992680aae9",
"vendor/hashset-cpp": "https://github.com/brave/hashset-cpp.git@6eab0271d014ff09bd9f38abe1e0c117e13e9aa9",
@@ -68,6 +68,7 @@ source_set("extensions") {
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_extension:generated_resources",
"//brave/components/brave_extension:static_resources",
"//brave/components/brave_shields/browser",
"//chrome/browser/extensions",
"//components/gcm_driver:gcm_driver",
"//components/gcm_driver:gcm_buildflags",
@@ -10,10 +10,12 @@
#include <utility>

#include "base/strings/string_number_conversions.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/extensions/api/brave_action_api.h"
#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_service.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"
@@ -41,6 +43,40 @@ const char kInvalidControlTypeError[] = "Invalid ControlType.";

} // namespace


ExtensionFunction::ResponseAction
BraveShieldsHostnameCosmeticResourcesFunction::Run() {
std::unique_ptr<brave_shields::HostnameCosmeticResources::Params> params(
brave_shields::HostnameCosmeticResources::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

base::Optional<base::Value> resources = g_brave_browser_process->
ad_block_service()->HostnameCosmeticResources(params->hostname);

if (!resources || !resources->is_dict()) {
return RespondNow(Error(
"Hostname-specific cosmetic resources could not be returned"));
}
auto result_list = std::make_unique<base::ListValue>();

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

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

ExtensionFunction::ResponseAction BraveShieldsClassIdStylesheetFunction::Run() {
std::unique_ptr<brave_shields::ClassIdStylesheet::Params> params(
brave_shields::ClassIdStylesheet::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

std::string stylesheet = g_brave_browser_process->
ad_block_service()->ClassIdStylesheet(params->classes,
params->ids,
params->exceptions);
return RespondNow(OneArgument(std::make_unique<base::Value>(stylesheet)));
}


ExtensionFunction::ResponseAction BraveShieldsAllowScriptsOnceFunction::Run() {
std::unique_ptr<brave_shields::AllowScriptsOnce::Params> params(
brave_shields::AllowScriptsOnce::Params::Create(*args_));
@@ -11,6 +11,26 @@
namespace extensions {
namespace api {

class BraveShieldsHostnameCosmeticResourcesFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.hostnameCosmeticResources", UNKNOWN)

protected:
~BraveShieldsHostnameCosmeticResourcesFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsClassIdStylesheetFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.classIdStylesheet", UNKNOWN)

protected:
~BraveShieldsClassIdStylesheetFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsAllowScriptsOnceFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.allowScriptsOnce", UNKNOWN)
@@ -73,6 +73,65 @@
"description": "Notifies the browser about the fact of showing the panel",
"parameters": []
},
{
"name": "hostnameCosmeticResources",
"type": "function",
"description": "Get a cosmetic adblocking stylesheet, generic style exceptions, and script injections specific for the given hostname and domain",
"parameters": [
{
"name": "hostname",
"type": "string"
},
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "hostnameSpecificResources",
"type": "object",
"properties": {
"hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "Hostname-specific CSS selectors that should be hidden from the page"},
"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"}
}
}
]
}
]
},
{
"name": "classIdStylesheet",
"type": "function",
"description": "Get a stylesheet of generic rules that may apply to the given set of classes and ids without any of the given excepted selectors",
"parameters": [
{
"name": "classes",
"type": "array",
"items": {"type": "string"}
},
{
"name": "ids",
"type": "array",
"items": {"type": "string"}
},
{
"name": "exceptions",
"type": "array",
"items": {"type": "string"}
},
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "stylesheet",
"type": "string"
}
]
}
]
},
{
"name": "getBraveShieldsEnabled",
"type": "function",
@@ -55,6 +55,7 @@ source_set("browser") {
}

deps = [
"//base",
"//brave/components/brave_component_updater/browser",
"//brave/components/content_settings/core/browser",
"//brave/content:common",
@@ -12,6 +12,7 @@

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/json/json_reader.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
@@ -195,6 +196,19 @@ bool AdBlockBaseService::TagExists(const std::string& tag) {
return std::find(tags_.begin(), tags_.end(), tag) != tags_.end();
}

base::Optional<base::Value> AdBlockBaseService::HostnameCosmeticResources(
const std::string& hostname) {
return base::JSONReader::Read(
this->ad_block_client_->hostnameCosmeticResources(hostname));
}

std::string AdBlockBaseService::ClassIdStylesheet(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& exceptions) {
return this->ad_block_client_->classIdStylesheet(classes, ids, exceptions);
}

void AdBlockBaseService::GetDATFileData(const base::FilePath& dat_file_path) {
base::PostTaskAndReplyWithResult(
FROM_HERE, {base::ThreadPool(), base::MayBlock()},
@@ -16,6 +16,7 @@
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "brave/components/brave_shields/browser/base_brave_shields_service.h"
#include "brave/components/brave_component_updater/browser/dat_file_util.h"
#include "content/public/common/resource_type.h"
@@ -46,6 +47,13 @@ class AdBlockBaseService : public BaseBraveShieldsService {
void EnableTag(const std::string& tag, bool enabled);
bool TagExists(const std::string& tag);

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

protected:
friend class ::AdBlockServiceTest;
bool Init() override;
@@ -216,6 +216,15 @@ declare namespace chrome.braveShields {
const onShieldsPanelShown: any
const reportBrokenSite: any

interface HostnameSpecificResources {
hide_selectors: string[]
style_selectors: any
exceptions: string[]
injected_script: string
}
const hostnameCosmeticResources: (hostname: string, callback: (resources: HostnameSpecificResources) => void) => void
const classIdStylesheet: (classes: string[], ids: string[], exceptions: string[], callback: (stylesheet: string) => void) => void

type BraveShieldsViewPreferences = {
showAdvancedView: boolean
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.