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
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,7 +68,9 @@ 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",
"//chrome/common",
"//components/gcm_driver:gcm_driver",
"//components/gcm_driver:gcm_buildflags",
"//components/prefs",
@@ -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"
@@ -23,6 +25,7 @@
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h"
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@bridiver

bridiver Dec 10, 2019

Collaborator

you need a dep for //chrome/common in brave/browser/extensions/BUILD.gn

This comment has been minimized.

Copy link
@bbondy

bbondy Dec 12, 2019

Member

I verified it's there now. Resolving this.

#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_util.h"

@@ -41,6 +44,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_));
@@ -117,6 +154,14 @@ BraveShieldsGetBraveShieldsEnabledFunction::Run() {
return RespondNow(OneArgument(std::move(result)));
}

ExtensionFunction::ResponseAction
BraveShieldsGetCosmeticFilteringEnabledFunction::Run() {
auto result = std::make_unique<base::Value>(
base::FeatureList::IsEnabled(features::kBraveAdblockCosmeticFiltering));

return RespondNow(OneArgument(std::move(result)));
}

ExtensionFunction::ResponseAction BraveShieldsSetAdControlTypeFunction::Run() {
std::unique_ptr<brave_shields::SetAdControlType::Params> params(
brave_shields::SetAdControlType::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)
@@ -52,6 +72,18 @@ class BraveShieldsGetBraveShieldsEnabledFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveShieldsGetCosmeticFilteringEnabledFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.getCosmeticFilteringEnabled",
UNKNOWN)

protected:
~BraveShieldsGetCosmeticFilteringEnabledFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsSetAdControlTypeFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.setAdControlType", UNKNOWN)
@@ -3,6 +3,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#define BRAVE_FEATURE_ENTRIES \
{"brave-adblock-cosmetic-filtering", \
flag_descriptions::kBraveAdblockCosmeticFilteringName, \
flag_descriptions::kBraveAdblockCosmeticFilteringDescription, kOsAll, \
FEATURE_VALUE_TYPE(features::kBraveAdblockCosmeticFiltering)},

#define SetFeatureEntryEnabled SetFeatureEntryEnabled_ChromiumImpl
#include "../../../../chrome/browser/about_flags.cc" // NOLINT
#include "../../../../components/flags_ui/flags_state.cc" // NOLINT
@@ -0,0 +1,12 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "../../../../chrome/browser/flag_descriptions.cc"

namespace flag_descriptions {
const char kBraveAdblockCosmeticFilteringName[] = "Enable cosmetic filtering";
const char kBraveAdblockCosmeticFilteringDescription[] =
"Enable support for cosmetic filtering";
}
@@ -0,0 +1,16 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_FLAG_DESCRIPTIONS_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_FLAG_DESCRIPTIONS_H_

#include "../../../../chrome/browser/flag_descriptions.h"

namespace flag_descriptions {
extern const char kBraveAdblockCosmeticFilteringName[];
extern const char kBraveAdblockCosmeticFilteringDescription[];
}

#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_FLAG_DESCRIPTIONS_H_
@@ -0,0 +1,12 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "../../../../chrome/common/chrome_features.cc"

namespace features {
const base::Feature kBraveAdblockCosmeticFiltering{
"BraveAdblockCosmeticFiltering",
base::FEATURE_DISABLED_BY_DEFAULT};
}
@@ -0,0 +1,16 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_FEATURES_H_
#define BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_FEATURES_H_

#include "../../../../chrome/common/chrome_features.h"

namespace features {
COMPONENT_EXPORT(CHROME_FEATURES)
extern const base::Feature kBraveAdblockCosmeticFiltering;
}

#endif // BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_FEATURES_H_
@@ -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",
@@ -94,6 +153,23 @@
}
]
},
{
"name": "getCosmeticFilteringEnabled",
"type": "function",
"description": "Get whether or not the cosmetic filtering feature flag is enabled",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "enabled",
"type": "boolean"
}
]
}
]
},
{
"name": "setAdControlType",
"type": "function",
@@ -9,6 +9,7 @@ transpile_web_ui("brave_extension") {
["brave_extension_background", rebase_path("background.ts")],
["content", rebase_path("content.ts")],
["content_dapps", rebase_path("content_dapps.ts")],
["content_cosmetic", rebase_path("content_cosmetic.ts")],
["webstore", rebase_path("webstore.ts")],
]

@@ -137,3 +137,28 @@ export const shieldsReady: actions.ShieldsReady = () => {
type: types.SHIELDS_READY
}
}

export const generateClassIdStylesheet = (tabId: number, classes: string[], ids: string[]) => {
return {
type: types.GENERATE_CLASS_ID_STYLESHEET,
tabId,
classes,
ids
}
}

export const cosmeticFilterRuleExceptions = (tabId: number, exceptions: string[]) => {
return {
type: types.COSMETIC_FILTER_RULE_EXCEPTIONS,
tabId,
exceptions
}
}

export const contentScriptsLoaded: actions.ContentScriptsLoaded = (tabId: number, url: string) => {
return {
type: types.CONTENT_SCRIPTS_LOADED,
tabId,
url
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.