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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
bbondy marked this conversation as resolved.
Show resolved Hide resolved
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_util.h"

Expand All @@ -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_));
Expand Down Expand Up @@ -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_));
Expand Down
32 changes: 32 additions & 0 deletions browser/extensions/api/brave_shields_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions chromium_src/chrome/browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions chromium_src/chrome/browser/flag_descriptions.cc
Original file line number Diff line number Diff line change
@@ -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";
}
16 changes: 16 additions & 0 deletions chromium_src/chrome/browser/flag_descriptions.h
Original file line number Diff line number Diff line change
@@ -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_
12 changes: 12 additions & 0 deletions chromium_src/chrome/common/chrome_features.cc
Original file line number Diff line number Diff line change
@@ -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};
}
16 changes: 16 additions & 0 deletions chromium_src/chrome/common/chrome_features.h
Original file line number Diff line number Diff line change
@@ -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_
76 changes: 76 additions & 0 deletions common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")],
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading