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

connect feature flag to cosmetic filtering mechanism

  • Loading branch information
antonok-edm committed Nov 22, 2019
commit 6a9fa30c3487bb830713aad3fb5505990dc6ff4a
@@ -70,6 +70,7 @@ source_set("extensions") {
"//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",
@@ -25,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"

@@ -153,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_));
@@ -72,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)
@@ -153,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",
@@ -27,7 +27,8 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {
chrome.braveShields.getHTTPSEverywhereEnabledAsync(tabData.url),
chrome.braveShields.getNoScriptControlTypeAsync(tabData.url),
chrome.braveShields.getFingerprintingControlTypeAsync(tabData.url),
chrome.braveShields.getCookieControlTypeAsync(tabData.url)
chrome.braveShields.getCookieControlTypeAsync(tabData.url),
chrome.braveShields.getCosmeticFilteringEnabledAsync()
]).then((details) => {
return {
url: url.href,
@@ -40,7 +41,8 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {
httpUpgradableResources: details[2] ? 'block' : 'allow',
javascript: details[3],
fingerprinting: details[4],
cookies: details[5]
cookies: details[5],
cosmeticBlocking: details[6]
}
}).catch(() => {
return {
@@ -347,7 +347,20 @@ export default function shieldsPanelReducer (
break
}
case shieldsPanelTypes.CONTENT_SCRIPTS_LOADED: {
applyAdblockCosmeticFilters(action.tabId, getHostname(action.url))
const tabData = state.tabs[action.tabId]
if (!tabData) {
console.error('Active tab not found')
break
}
const cosmeticBlockingEnabled = tabData.cosmeticBlocking
chrome.braveShields.getBraveShieldsEnabledAsync(action.url)
.then((braveShieldsEnabled: boolean) => {
const doCosmeticBlocking = braveShieldsEnabled && cosmeticBlockingEnabled
if (doCosmeticBlocking) {
applyAdblockCosmeticFilters(action.tabId, getHostname(action.url))
}
})
break
}
}

@@ -7,7 +7,7 @@ import { BlockTypes, BlockOptions, BlockFPOptions, BlockJSOptions, BlockCookiesO

export interface ShieldDetails {
id: number
cosmeticBlocking: BlockOptions
cosmeticBlocking: boolean
ads: BlockOptions
trackers: BlockOptions
httpUpgradableResources: BlockOptions
@@ -6,6 +6,7 @@ import { BlockOptions, BlockTypes, BlockFPOptions, BlockCookiesOptions } from '.
import { NoScriptInfo } from '../other/noScriptInfo'

export interface Tab {
cosmeticBlocking: boolean
ads: BlockOptions
adsBlocked: number
controlsOpen: boolean
@@ -19,6 +19,7 @@
#include "brave/vendor/adblock_rust_ffi/src/wrapper.hpp"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
@@ -785,7 +786,19 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectRulesAreRespected) {
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}

// Test simple cosmetic filtering
class CosmeticFilteringEnabledTest : public AdBlockServiceTest {
public:
CosmeticFilteringEnabledTest() {
feature_list_.InitAndEnableFeature(
features::kBraveAdblockCosmeticFiltering);
}

private:
base::test::ScopedFeatureList feature_list_;
};

// Ensure no cosmetic filtering occurs when the feature flag has not been
// enabled
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) {
UpdateAdBlockInstanceWithRules(
"b.com###ad-banner\n"
@@ -797,6 +810,43 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) {
"/cosmetic_filtering.html");
ui_test_utils::NavigateToURL(browser(), tab_url);

content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();

bool as_expected = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"checkSelector('#ad-banner', 'display', 'block')",
&as_expected));
EXPECT_TRUE(as_expected);

as_expected = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"checkSelector('.ad-banner', 'display', 'block')",
&as_expected));
EXPECT_TRUE(as_expected);

as_expected = false;
ASSERT_TRUE(ExecuteScriptAndExtractBool(
contents,
"checkSelector('.ad', 'display', 'block')",
&as_expected));
EXPECT_TRUE(as_expected);
}

// Test simple cosmetic filtering
IN_PROC_BROWSER_TEST_F(CosmeticFilteringEnabledTest, CosmeticFilteringSimple) {
UpdateAdBlockInstanceWithRules(
"b.com###ad-banner\n"
"##.ad");

WaitForBraveExtensionShieldsDataReady();

GURL tab_url = embedded_test_server()->GetURL("b.com",
"/cosmetic_filtering.html");
ui_test_utils::NavigateToURL(browser(), tab_url);

content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();

@@ -823,7 +873,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) {
}

// Test cosmetic filtering on elements added dynamically
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) {
IN_PROC_BROWSER_TEST_F(CosmeticFilteringEnabledTest, CosmeticFilteringDynamic) {
UpdateAdBlockInstanceWithRules("##.blockme");

WaitForBraveExtensionShieldsDataReady();
@@ -852,7 +902,8 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) {
}

// Test custom style rules
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringCustomStyle) {
IN_PROC_BROWSER_TEST_F(CosmeticFilteringEnabledTest,
CosmeticFilteringCustomStyle) {
UpdateAdBlockInstanceWithRules("b.com##.ad:style(padding-bottom: 10px)");

WaitForBraveExtensionShieldsDataReady();
@@ -873,7 +924,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringCustomStyle) {
}

// Test rules overridden by hostname-specific exception rules
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringUnhide) {
IN_PROC_BROWSER_TEST_F(CosmeticFilteringEnabledTest, CosmeticFilteringUnhide) {
UpdateAdBlockInstanceWithRules(
"##.ad\n"
"b.com#@#.ad\n"
@@ -203,6 +203,7 @@ declare namespace chrome.braveShields {
const allowScriptsOnce: any
const setBraveShieldsEnabledAsync: any
const getBraveShieldsEnabledAsync: any
const getCosmeticFilteringEnabledAsync: any
const setAdControlTypeAsync: any
const getAdControlTypeAsync: any
const setCookieControlTypeAsync: any
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.