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

WIP: mutation observer #2727

Closed
wants to merge 6 commits into from
Closed
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
77 changes: 77 additions & 0 deletions browser/extensions/api/brave_shields_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_util.h"
#include "brave/components/brave_shields/browser/ad_block_base_service.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"

using brave_shields::BraveShieldsWebContentsObserver;
using brave_shields::ControlType;
Expand All @@ -35,6 +39,39 @@ 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());

adblock::HostnameResources resources = g_brave_browser_process->ad_block_service()->hostnameCosmeticResources(params->hostname);

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

result_list->GetList().push_back(base::Value(resources.stylesheet));

base::Value exceptions(base::Value::Type::LIST);
for(auto i = resources.exceptions.begin(); i != resources.exceptions.end(); i++) {
exceptions.GetList().push_back(base::Value(*i));
}
result_list->GetList().push_back(std::move(exceptions));

result_list->GetList().push_back(base::Value(resources.injected_script));

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 @@ -92,6 +129,46 @@ BraveShieldsGetBraveShieldsEnabledFunction::Run() {
return RespondNow(OneArgument(std::move(result)));
}

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

const GURL url(params->url);
// we don't allow setting defaults from the extension
if (url.is_empty() || !url.is_valid()) {
return RespondNow(Error(kInvalidUrlError, params->url));
}

auto control_type = ControlTypeFromString(params->control_type);
if (control_type == ControlType::INVALID) {
return RespondNow(Error(kInvalidControlTypeError, params->control_type));
}

Profile* profile = Profile::FromBrowserContext(browser_context());
::brave_shields::SetCosmeticFilteredElements(profile, control_type, url);

return RespondNow(NoArguments());
}

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

const GURL url(params->url);
// we don't allow getting defaults from the extension
if (url.is_empty() || !url.is_valid()) {
return RespondNow(Error(kInvalidUrlError, params->url));
}

Profile* profile = Profile::FromBrowserContext(browser_context());
auto type = ::brave_shields::GetCosmeticFilteredElements(profile, url);
auto result = std::make_unique<base::Value>(ControlTypeToString(type));

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
40 changes: 40 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 UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.hostnameCosmeticResources", UNKNOWN)

protected:
~BraveShieldsHostnameCosmeticResourcesFunction() override {}

ResponseAction Run() override;
};

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

protected:
~BraveShieldsClassIdStylesheetFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsAllowScriptsOnceFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.allowScriptsOnce", UNKNOWN)
Expand Down Expand Up @@ -43,6 +63,26 @@ class BraveShieldsGetBraveShieldsEnabledFunction
ResponseAction Run() override;
};

class BraveShieldsSetCosmeticFilteredElementsFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.setCosmeticFilteredElements", UNKNOWN)

protected:
~BraveShieldsSetCosmeticFilteredElementsFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsGetCosmeticFilteredElementsFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.getCosmeticFilteredElements", UNKNOWN)

protected:
~BraveShieldsGetCosmeticFilteredElementsFunction() override {}

ResponseAction Run() override;
};

class BraveShieldsSetAdControlTypeFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveShields.setAdControlType", UNKNOWN)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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 "base/path_service.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/thread_test_helper.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/extensions/brave_extension_functional_test.h"
#include "brave/common/brave_paths.h"
#include "brave/common/pref_names.h"
#include "brave/common/url_constants.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"

class BraveExtensionBrowserTest : public InProcessBrowserTest {
public:
using InProcessBrowserTest::InProcessBrowserTest;

void SetUp() override {
InitEmbeddedTestServer();
InProcessBrowserTest::SetUp();
}

void InitEmbeddedTestServer() {
brave::RegisterPathProvider();
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
ASSERT_TRUE(embedded_test_server()->Start());
}

private:
DISALLOW_COPY_AND_ASSIGN(BraveExtensionBrowserTest);
};

IN_PROC_BROWSER_TEST_F(BraveExtensionBrowserTest, MutationObserverTriggeredWhenDOMChanged) {
ASSERT_TRUE(true);
GURL url = embedded_test_server()->GetURL("reddit.com", "/cosmetic-filter/mutation_observer.html");
ui_test_utils::NavigateToURL(browser(), url);
}
104 changes: 104 additions & 0 deletions common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,68 @@
}
]
},
{
"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": "stylesheet",
"type": "string"
},
{
"name": "genericExceptions",
"type": "array",
"items": {"type": "string"}
},
{
"name": "injectedScript",
"type": "string"
}
]
}
]
},
{
"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 @@ -70,6 +132,48 @@
}
]
},
{
"name": "setCosmeticFilteredElements",
"type": "function",
"description": "Set cosmetic filtering type for a url",
"parameters": [
{
"name": "controlType",
"type": "string"
},
{
"name": "url",
"type": "string"
},
{
"type": "function",
"name": "callback",
"optional": true,
"parameters": []
}
]
},
{
"name": "getCosmeticFilteredElements",
"type": "function",
"description": "Get cosmetic filtering type for a url",
"parameters": [
{
"name": "url",
"type": "string"
},
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "controlType",
"type": "string"
}
]
}
]
},
{
"name": "setAdControlType",
"type": "function",
Expand Down
27 changes: 27 additions & 0 deletions components/brave_extension/adbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<!-- saved from url=(0040)http://raymondhill.net/ublock/adbox.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Cosmetic filtering: Test your blocker</title>
<style>
ul li {
font-size: larger;
background-color: #efe;
}
ul li.ADBox {
background-color: #fee;
}
</style>
</head>
<body>
<h1>Cosmetic filtering: Test your blocker</h1>
<p>Force a refresh of the page to test your blocker.</p>
<h2>Testing EasyList's ...</h2>
<h3><code>##.ADBox</code></h3>
<ul>
<li><code>&lt;li&gt;</code>: Should not be hidden
</li><li class="ADBox"><code>&lt;li class="ADBox"&gt;</code>: Should be hidden.
</li><li class="ADBox" style="display: block !important;"><code>&lt;li class="ADBox" style="display: block !important;"&gt;</code>: Should be hidden.
</li></ul>


</body></html>
36 changes: 36 additions & 0 deletions components/brave_extension/brave_extension_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* 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 <memory>

#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/memory/weak_ptr.h"
#include "brave/browser/ui/views/brave_actions/brave_actions_container.h"
#include "brave/browser/ui/views/location_bar/brave_location_bar_view.h"
#include "brave/common/brave_paths.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_impl.h" // NOLINT
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h" // NOLINT
#include "brave/components/brave_rewards/common/pref_names.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"message": "connection upgraded",
"description": "Message for the main blocked resources text when there is at least one ad/track/script blocked and one connection upgrade (singular)"
},
"firstPartyTrackersBlocked": {
"message": "This site's ads blocked",
"description": "Message for the cosmetic filtering row label"
},
"thirdPartyTrackersBlocked": {
"message": "Cross-site trackers blocked",
"description": "Message for the scripts blocked row label"
Expand Down
Loading