From ff223a5176d62aaefd83fc7a602d0a14d8c7fea6 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Tue, 7 Sep 2021 14:18:46 -0700 Subject: [PATCH 1/2] remove cosmetic filtering extension version --- browser/about_flags.cc | 11 - browser/extensions/api/brave_shields_api.cc | 110 --- browser/extensions/api/brave_shields_api.h | 43 -- common/extensions/api/brave_shields.json | 88 --- .../extension/brave_extension/BUILD.gn | 4 - .../actions/shieldsPanelActions.ts | 29 - .../background/api/cosmeticFilterAPI.ts | 77 --- .../background/api/shieldsAPI.ts | 14 +- .../background/events/cosmeticFilterEvents.ts | 33 - .../reducers/shieldsPanelReducer.ts | 60 -- .../constants/shieldsPanelTypes.ts | 3 - .../brave_extension/content_cosmetic.ts | 627 ------------------ .../extension/brave_extension/manifest.json | 11 - .../state/shieldsPanelState.ts | 7 - .../types/actions/shieldsPanelActions.ts | 40 +- .../types/adblock/adblockTypes.ts | 4 - .../types/constants/shieldsPanelTypes.ts | 3 - .../types/state/shieldsPannelState.ts | 3 - .../brave_shields/browser/ad_block_service.cc | 23 +- components/brave_shields/common/features.cc | 2 - components/brave_shields/common/features.h | 1 - components/definitions/chromel.d.ts | 11 - .../background/api/shieldsAPI_test.ts | 2 - .../reducers/shieldsPanelReducer_test.ts | 1 - .../controls/adsTrackersControl_test.tsx | 1 - .../state/shieldsPanelState_test.ts | 2 - components/test/testData.ts | 3 - renderer/brave_content_renderer_client.cc | 5 +- 28 files changed, 14 insertions(+), 1204 deletions(-) delete mode 100644 components/brave_extension/extension/brave_extension/content_cosmetic.ts diff --git a/browser/about_flags.cc b/browser/about_flags.cc index 489d71a41beb..bcdbcd2c612e 100644 --- a/browser/about_flags.cc +++ b/browser/about_flags.cc @@ -51,7 +51,6 @@ using brave_shields::features::kBraveAdblockCnameUncloaking; using brave_shields::features::kBraveAdblockCollapseBlockedElements; using brave_shields::features::kBraveAdblockCosmeticFiltering; -using brave_shields::features::kBraveAdblockCosmeticFilteringNative; using brave_shields::features::kBraveAdblockCspRules; using brave_shields::features::kBraveAdblockDefault1pBlocking; using brave_shields::features::kBraveDarkModeBlock; @@ -81,11 +80,6 @@ constexpr char kBraveAdblockCosmeticFilteringName[] = constexpr char kBraveAdblockCosmeticFilteringDescription[] = "Enable support for cosmetic filtering"; -constexpr char kBraveAdblockCosmeticFilteringNativeName[] = - "Use native implementation for cosmetic filtering"; -constexpr char kBraveAdblockCosmeticFilteringNativeDescription[] = - "Uses native implementation for cosmetic filtering instead of extension"; - constexpr char kBraveAdblockCspRulesName[] = "Enable support for CSP rules"; constexpr char kBraveAdblockCspRulesDescription[] = "Applies additional CSP rules to pages for which a $csp rule has been " @@ -323,11 +317,6 @@ constexpr char kUseDevUpdaterUrlDescription[] = flag_descriptions::kBraveAdblockCosmeticFilteringName, \ flag_descriptions::kBraveAdblockCosmeticFilteringDescription, kOsAll, \ FEATURE_VALUE_TYPE(kBraveAdblockCosmeticFiltering)}, \ - {"brave-adblock-cosmetic-filtering-native", \ - flag_descriptions::kBraveAdblockCosmeticFilteringNativeName, \ - flag_descriptions::kBraveAdblockCosmeticFilteringNativeDescription, \ - kOsMac | kOsWin | kOsLinux, \ - FEATURE_VALUE_TYPE(kBraveAdblockCosmeticFilteringNative)}, \ {"brave-adblock-csp-rules", \ flag_descriptions::kBraveAdblockCspRulesName, \ flag_descriptions::kBraveAdblockCspRulesDescription, kOsAll, \ diff --git a/browser/extensions/api/brave_shields_api.cc b/browser/extensions/api/brave_shields_api.cc index 1fb86f136685..83f26de6c530 100644 --- a/browser/extensions/api/brave_shields_api.cc +++ b/browser/extensions/api/brave_shields_api.cc @@ -47,90 +47,6 @@ const char kInvalidControlTypeError[] = "Invalid ControlType."; } // namespace -ExtensionFunction::ResponseAction -BraveShieldsUrlCosmeticResourcesFunction::Run() { - std::unique_ptr params( - brave_shields::UrlCosmeticResources::Params::Create(*args_)); - EXTENSION_FUNCTION_VALIDATE(params.get()); - g_brave_browser_process->ad_block_service() - ->GetTaskRunner() - ->PostTaskAndReplyWithResult( - FROM_HERE, - base::BindOnce(&BraveShieldsUrlCosmeticResourcesFunction:: - GetUrlCosmeticResourcesOnTaskRunner, - this, params->url), - base::BindOnce(&BraveShieldsUrlCosmeticResourcesFunction:: - GetUrlCosmeticResourcesOnUI, - this)); - return RespondLater(); -} - -std::unique_ptr -BraveShieldsUrlCosmeticResourcesFunction::GetUrlCosmeticResourcesOnTaskRunner( - const std::string& url) { - absl::optional resources = - g_brave_browser_process->ad_block_service()->UrlCosmeticResources(url); - - if (!resources || !resources->is_dict()) { - return std::unique_ptr(); - } - - auto result_list = std::make_unique(); - result_list->Append(std::move(*resources)); - return result_list; -} - -void BraveShieldsUrlCosmeticResourcesFunction::GetUrlCosmeticResourcesOnUI( - std::unique_ptr resources) { - if (!resources) { - Respond(Error("Url-specific cosmetic resources could not be returned")); - return; - } - Respond(ArgumentList(std::move(resources))); -} - -ExtensionFunction::ResponseAction -BraveShieldsHiddenClassIdSelectorsFunction::Run() { - std::unique_ptr params( - brave_shields::HiddenClassIdSelectors::Params::Create(*args_)); - EXTENSION_FUNCTION_VALIDATE(params.get()); - g_brave_browser_process->ad_block_service() - ->GetTaskRunner() - ->PostTaskAndReplyWithResult( - FROM_HERE, - base::BindOnce(&BraveShieldsHiddenClassIdSelectorsFunction:: - GetHiddenClassIdSelectorsOnTaskRunner, - this, params->classes, params->ids, - params->exceptions), - base::BindOnce(&BraveShieldsHiddenClassIdSelectorsFunction:: - GetHiddenClassIdSelectorsOnUI, - this)); - return RespondLater(); -} - -std::unique_ptr BraveShieldsHiddenClassIdSelectorsFunction:: - GetHiddenClassIdSelectorsOnTaskRunner( - const std::vector& classes, - const std::vector& ids, - const std::vector& exceptions) { - absl::optional hide_selectors = - g_brave_browser_process->ad_block_service()->HiddenClassIdSelectors( - classes, ids, exceptions); - - if (!hide_selectors || !hide_selectors->is_list()) - return std::make_unique(); - - auto result_list = - std::make_unique(hide_selectors->GetList()); - - return result_list; -} - -void BraveShieldsHiddenClassIdSelectorsFunction::GetHiddenClassIdSelectorsOnUI( - std::unique_ptr selectors) { - Respond(ArgumentList(std::move(selectors))); -} - ExtensionFunction::ResponseAction BraveShieldsAddSiteCosmeticFilterFunction::Run() { std::unique_ptr params( @@ -238,32 +154,6 @@ BraveShieldsGetBraveShieldsEnabledFunction::Run() { return RespondNow(OneArgument(base::Value(enabled))); } -ExtensionFunction::ResponseAction -BraveShieldsShouldDoCosmeticFilteringFunction::Run() { -#if !defined(OS_ANDROID) && !defined(CHROME_OS) - if (base::FeatureList::IsEnabled( - ::brave_shields::features::kBraveAdblockCosmeticFilteringNative)) - return RespondNow(OneArgument(base::Value(false))); -#endif - std::unique_ptr - params( - brave_shields::ShouldDoCosmeticFiltering::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()); - const bool enabled = ::brave_shields::ShouldDoCosmeticFiltering( - HostContentSettingsMapFactory::GetForProfile(profile), - url); - - return RespondNow(OneArgument(base::Value(enabled))); -} - ExtensionFunction::ResponseAction BraveShieldsSetCosmeticFilteringControlTypeFunction::Run() { std::unique_ptr diff --git a/browser/extensions/api/brave_shields_api.h b/browser/extensions/api/brave_shields_api.h index e833527194c5..5c58c99c6cd6 100644 --- a/browser/extensions/api/brave_shields_api.h +++ b/browser/extensions/api/brave_shields_api.h @@ -15,39 +15,6 @@ namespace extensions { namespace api { -class BraveShieldsUrlCosmeticResourcesFunction : public ExtensionFunction { - public: - DECLARE_EXTENSION_FUNCTION("braveShields.urlCosmeticResources", UNKNOWN) - - protected: - ~BraveShieldsUrlCosmeticResourcesFunction() override {} - - ResponseAction Run() override; - - private: - std::unique_ptr GetUrlCosmeticResourcesOnTaskRunner( - const std::string& url); - void GetUrlCosmeticResourcesOnUI(std::unique_ptr resources); -}; - -class BraveShieldsHiddenClassIdSelectorsFunction : public ExtensionFunction { - public: - DECLARE_EXTENSION_FUNCTION("braveShields.hiddenClassIdSelectors", UNKNOWN) - - protected: - ~BraveShieldsHiddenClassIdSelectorsFunction() override {} - - ResponseAction Run() override; - - private: - std::unique_ptr GetHiddenClassIdSelectorsOnTaskRunner( - const std::vector& classes, - const std::vector& ids, - const std::vector& exceptions); - void GetHiddenClassIdSelectorsOnUI( - std::unique_ptr selectors); -}; - class BraveShieldsAddSiteCosmeticFilterFunction : public ExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("braveShields.addSiteCosmeticFilter", UNKNOWN) @@ -109,16 +76,6 @@ class BraveShieldsGetBraveShieldsEnabledFunction : public ExtensionFunction { ResponseAction Run() override; }; -class BraveShieldsShouldDoCosmeticFilteringFunction : public ExtensionFunction { - public: - DECLARE_EXTENSION_FUNCTION("braveShields.shouldDoCosmeticFiltering", UNKNOWN) - - protected: - ~BraveShieldsShouldDoCosmeticFilteringFunction() override {} - - ResponseAction Run() override; -}; - class BraveShieldsSetCosmeticFilteringControlTypeFunction : public ExtensionFunction { public: diff --git a/common/extensions/api/brave_shields.json b/common/extensions/api/brave_shields.json index 0d3af73864a0..589891428c85 100644 --- a/common/extensions/api/brave_shields.json +++ b/common/extensions/api/brave_shields.json @@ -73,73 +73,6 @@ "description": "Notifies the browser about the fact of showing the panel", "parameters": [] }, - { - "name": "urlCosmeticResources", - "type": "function", - "description": "Get a cosmetic adblocking stylesheet, generic style exceptions, script injections, and whether or not a generichide rule was present, all specific for the given URL", - "parameters": [ - { - "name": "url", - "type": "string" - }, - { - "type": "function", - "name": "callback", - "parameters": [ - { - "name": "urlSpecificResources", - "type": "object", - "properties": { - "hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "URL-specific CSS selectors that should be hidden from the page if they are determined to not include 1st party content"}, - "style_selectors": {"type": "object", "additionalProperties": {"type": "array", "items": {"type": "string"}}, "description": "URL-specific CSS selectors that should be restyled, with their associated CSS style rules"}, - "exceptions": {"type": "array", "items": {"type": "string"}, "description": "URL-specific overrides for generic cosmetic blocking selectors"}, - "injected_script": {"type": "string", "description": "A script to inject as the page is loading"}, - "force_hide_selectors": {"type": "array", "items": {"type": "string"}, "description": "URL-specific CSS selectors that should be hidden from the page"}, - "generichide": {"type": "boolean", "description": "Indicates whether or not the URL matched a generichide exception rule"} - } - } - ] - } - ] - }, - { - "name": "hiddenClassIdSelectors", - "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": "selectors", - "type": "array", - "items": {"type": "string"} - }, - { - "name": "forceHideSelectors", - "type": "array", - "items": {"type": "string"} - } - ] - } - ] - }, { "name": "addSiteCosmeticFilter", "type": "function", @@ -184,27 +117,6 @@ } ] }, - { - "name": "shouldDoCosmeticFiltering", - "type": "function", - "description": "Determine whether or not to do cosmetic filtering on the given url", - "parameters": [ - { - "name": "url", - "type": "string" - }, - { - "type": "function", - "name": "callback", - "parameters": [ - { - "name": "doCosmeticFiltering", - "type": "boolean" - } - ] - } - ] - }, { "name": "setCosmeticFilteringControlType", "type": "function", diff --git a/components/brave_extension/extension/brave_extension/BUILD.gn b/components/brave_extension/extension/brave_extension/BUILD.gn index 1d7d65e75730..4a5b2cf7cbde 100644 --- a/components/brave_extension/extension/brave_extension/BUILD.gn +++ b/components/brave_extension/extension/brave_extension/BUILD.gn @@ -17,10 +17,6 @@ transpile_web_ui("brave_extension") { "content_dapps", rebase_path("content_dapps.ts"), ], - [ - "content_cosmetic", - rebase_path("content_cosmetic.ts"), - ], [ "content_element_picker", rebase_path("content_element_picker.ts"), diff --git a/components/brave_extension/extension/brave_extension/actions/shieldsPanelActions.ts b/components/brave_extension/extension/brave_extension/actions/shieldsPanelActions.ts index 4abebb1c715d..b566c3247c9d 100644 --- a/components/brave_extension/extension/brave_extension/actions/shieldsPanelActions.ts +++ b/components/brave_extension/extension/brave_extension/actions/shieldsPanelActions.ts @@ -137,32 +137,3 @@ 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: actions.CosmeticFilterRuleExceptions = (tabId: number, frameId: number, exceptions: string[], scriptlet: string, generichide: boolean) => { - return { - type: types.COSMETIC_FILTER_RULE_EXCEPTIONS, - tabId, - frameId, - exceptions, - scriptlet, - generichide - } -} - -export const contentScriptsLoaded: actions.ContentScriptsLoaded = (tabId: number, frameId: number, url: string) => { - return { - type: types.CONTENT_SCRIPTS_LOADED, - tabId, - frameId, - url - } -} diff --git a/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts b/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts index 86002628b79f..6fae53c0d265 100644 --- a/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts +++ b/components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts @@ -2,83 +2,6 @@ * 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/. */ -import shieldsPanelActions from '../actions/shieldsPanelActions' - -const informTabOfCosmeticRulesToConsider = (tabId: number, selectors: string[]) => { - if (selectors.length !== 0) { - const message = { - type: 'cosmeticFilterConsiderNewSelectors', - selectors - } - const options = { - frameId: 0 - } - chrome.tabs.sendMessage(tabId, message, options) - } -} - -// Fires when content-script calls hiddenClassIdSelectors -export const injectClassIdStylesheet = (tabId: number, classes: string[], ids: string[], exceptions: string[], hide1pContent: boolean) => { - chrome.braveShields.hiddenClassIdSelectors(classes, ids, exceptions, (selectors, forceHideSelectors) => { - if (hide1pContent) { - forceHideSelectors.push(...selectors) - } else { - informTabOfCosmeticRulesToConsider(tabId, selectors) - } - - if (forceHideSelectors.length > 0) { - const forceHideStylesheet = forceHideSelectors.join(',') + '{display:none!important;}\n' - - chrome.tabs.insertCSS(tabId, { - code: forceHideStylesheet, - cssOrigin: 'user', - runAt: 'document_start' - }, () => { - if (chrome.runtime.lastError) { - console.error('tabs.insertCSS failed: ' + chrome.runtime.lastError.message) - } - }) - } - }) -} - -// Fires on content-script loaded -export const applyAdblockCosmeticFilters = (tabId: number, frameId: number, url: string, hide1pContent: boolean) => { - chrome.braveShields.urlCosmeticResources(url, async (resources) => { - if (chrome.runtime.lastError) { - console.warn('Unable to get cosmetic filter data for the current host', chrome.runtime.lastError) - return - } - - if (frameId === 0) { - if (hide1pContent) { - resources.force_hide_selectors.push(...resources.hide_selectors) - } else { - informTabOfCosmeticRulesToConsider(tabId, resources.hide_selectors) - } - - let styledStylesheet = '' - if (resources.force_hide_selectors.length > 0) { - styledStylesheet += resources.force_hide_selectors.join(',') + '{display:none!important;}\n' - } - for (const selector in resources.style_selectors) { - styledStylesheet += selector + '{' + resources.style_selectors[selector].join(';') + ';}\n' - } - chrome.tabs.insertCSS(tabId, { - code: styledStylesheet, - cssOrigin: 'user', - runAt: 'document_start' - }, () => { - if (chrome.runtime.lastError) { - console.error('tabs.insertCSS failed: ' + chrome.runtime.lastError.message) - } - }) - } - - shieldsPanelActions.cosmeticFilterRuleExceptions(tabId, frameId, resources.exceptions, resources.injected_script || '', resources.generichide) - }) -} - export const addSiteCosmeticFilter = (host: string, cssSelector: string) => { chrome.braveShields.addSiteCosmeticFilter(host, cssSelector) } diff --git a/components/brave_extension/extension/brave_extension/background/api/shieldsAPI.ts b/components/brave_extension/extension/brave_extension/background/api/shieldsAPI.ts index 9a8bb2214772..497fd82890af 100644 --- a/components/brave_extension/extension/brave_extension/background/api/shieldsAPI.ts +++ b/components/brave_extension/extension/brave_extension/background/api/shieldsAPI.ts @@ -24,7 +24,6 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => { return Promise.all([ chrome.braveShields.getBraveShieldsEnabledAsync(tabData.url), chrome.braveShields.getAdControlTypeAsync(tabData.url), - chrome.braveShields.shouldDoCosmeticFilteringAsync(tabData.url), chrome.braveShields.isFirstPartyCosmeticFilteringEnabledAsync(tabData.url), chrome.braveShields.getHTTPSEverywhereEnabledAsync(tabData.url), chrome.braveShields.getNoScriptControlTypeAsync(tabData.url), @@ -39,12 +38,11 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => { braveShields: details[0] ? 'allow' : 'block', ads: details[1], trackers: details[1], - cosmeticFiltering: details[2], - firstPartyCosmeticFiltering: details[3], - httpUpgradableResources: details[4] ? 'block' : 'allow', - javascript: details[5], - fingerprinting: details[6], - cookies: details[7] + firstPartyCosmeticFiltering: details[2], + httpUpgradableResources: details[3] ? 'block' : 'allow', + javascript: details[4], + fingerprinting: details[5], + cookies: details[6] } }).catch(() => { return { @@ -55,8 +53,6 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => { braveShields: 'block', ads: 0, trackers: 0, - cosmeticFiltering: 0, - cosmeticFilteringFeatureFlag: 0, httpUpgradableResources: 0, javascript: 0, fingerprinting: 0 diff --git a/components/brave_extension/extension/brave_extension/background/events/cosmeticFilterEvents.ts b/components/brave_extension/extension/brave_extension/background/events/cosmeticFilterEvents.ts index b6600b2bc027..ebe0a41fdd4d 100644 --- a/components/brave_extension/extension/brave_extension/background/events/cosmeticFilterEvents.ts +++ b/components/brave_extension/extension/brave_extension/background/events/cosmeticFilterEvents.ts @@ -1,6 +1,5 @@ import { getLocale } from '../api/localeAPI' import { addSiteCosmeticFilter, openFilterManagementPage } from '../api/cosmeticFilterAPI' -import shieldsPanelActions from '../actions/shieldsPanelActions' export let rule = { host: '', @@ -58,38 +57,6 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { rule.host = msg.baseURI break } - case 'hiddenClassIdSelectors': { - const tab = sender.tab - if (tab === undefined) { - break - } - const tabId = tab.id - if (tabId === undefined) { - break - } - shieldsPanelActions.generateClassIdStylesheet(tabId, msg.classes, msg.ids) - break - } - case 'contentScriptsLoaded': { - const tab = sender.tab - if (tab === undefined) { - break - } - const tabId = tab.id - if (tabId === undefined) { - break - } - const url = msg.location.href - if (url === undefined) { - break - } - const frameId = sender.frameId - if (frameId === undefined) { - break - } - shieldsPanelActions.contentScriptsLoaded(tabId, frameId, url) - break - } case 'cosmeticFilterCreate': { const { host, selector } = msg applyCosmeticFilter(host, selector) diff --git a/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts b/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts index c927a6312fe8..cae398f603f8 100644 --- a/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts +++ b/components/brave_extension/extension/brave_extension/background/reducers/shieldsPanelReducer.ts @@ -36,10 +36,6 @@ import { setAllowScriptOriginsOnce } from '../api/shieldsAPI' import { reloadTab } from '../api/tabsAPI' -import { - injectClassIdStylesheet, - applyAdblockCosmeticFilters -} from '../api/cosmeticFilterAPI' // Helpers import { getAllowedScriptsOrigins } from '../../helpers/noScriptUtils' @@ -380,62 +376,6 @@ export default function shieldsPanelReducer ( }) break } - case shieldsPanelTypes.GENERATE_CLASS_ID_STYLESHEET: { - const tabData = state.tabs[action.tabId] - if (!tabData) { - console.error('Active tab not found') - break - } - const exceptions = tabData.cosmeticFilters.ruleExceptions - const hide1pContent = tabData.firstPartyCosmeticFiltering - - // setTimeout is used to prevent injectClassIdStylesheet from calling - // another Redux function immediately - setTimeout(() => injectClassIdStylesheet(action.tabId, action.classes, action.ids, exceptions, hide1pContent), 0) - break - } - case shieldsPanelTypes.COSMETIC_FILTER_RULE_EXCEPTIONS: { - const tabData = state.tabs[action.tabId] - if (!tabData) { - console.error('Active tab not found') - break - } - let message: { type: string, scriptlet: string, hideOptions?: { hide1pContent: boolean, generichide: boolean } } = { - type: 'cosmeticFilteringBackgroundReady', - scriptlet: action.scriptlet, - hideOptions: undefined - } - if (action.frameId === 0) { - // Non-scriptlet cosmetic filters are only applied on the top-level frame - state = shieldsPanelState.saveCosmeticFilterRuleExceptions(state, action.tabId, action.exceptions) - message.hideOptions = { - hide1pContent: tabData.firstPartyCosmeticFiltering, - generichide: action.generichide - } - } - chrome.tabs.sendMessage(action.tabId, message, { - frameId: action.frameId - }) - break - } - case shieldsPanelTypes.CONTENT_SCRIPTS_LOADED: { - const tabData = state.tabs[action.tabId] - if (!tabData) { - console.error('Active tab not found') - break - } - Promise.all([chrome.braveShields.shouldDoCosmeticFilteringAsync(action.url), chrome.braveShields.isFirstPartyCosmeticFilteringEnabledAsync(action.url)]) - .then(([doCosmeticBlocking, hide1pContent]: [boolean, boolean]) => { - if (doCosmeticBlocking) { - applyAdblockCosmeticFilters(action.tabId, action.frameId, action.url, hide1pContent) - } - return null - }) - .catch(() => { - console.error('Could not apply cosmetic blocking') - }) - break - } } if (!areObjectsEqual(state.persistentData, initialPersistentData)) { diff --git a/components/brave_extension/extension/brave_extension/constants/shieldsPanelTypes.ts b/components/brave_extension/extension/brave_extension/constants/shieldsPanelTypes.ts index faea8d578819..0b1ee008ef0d 100644 --- a/components/brave_extension/extension/brave_extension/constants/shieldsPanelTypes.ts +++ b/components/brave_extension/extension/brave_extension/constants/shieldsPanelTypes.ts @@ -20,6 +20,3 @@ export const SET_FINAL_SCRIPTS_BLOCKED_ONCE_STATE = 'SET_FINAL_SCRIPTS_BLOCKED_O export const SET_ADVANCED_VIEW_FIRST_ACCESS = 'SET_ADVANCED_VIEW_FIRST_ACCESS' export const TOGGLE_ADVANCED_VIEW = 'TOGGLE_ADVANCED_VIEW' export const SHIELDS_READY = 'SHIELDS_READY' -export const GENERATE_CLASS_ID_STYLESHEET = 'GENERATE_CLASS_ID_STYLESHEET' -export const COSMETIC_FILTER_RULE_EXCEPTIONS = 'COSMETIC_FILTER_RULE_EXCEPTIONS' -export const CONTENT_SCRIPTS_LOADED = 'CONTENT_SCRIPTS_LOADED' diff --git a/components/brave_extension/extension/brave_extension/content_cosmetic.ts b/components/brave_extension/extension/brave_extension/content_cosmetic.ts deleted file mode 100644 index e696b6b58864..000000000000 --- a/components/brave_extension/extension/brave_extension/content_cosmetic.ts +++ /dev/null @@ -1,627 +0,0 @@ -// Copyright (c) 2020 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/. - -// Notify the background script as soon as the content script has loaded. -// chrome.tabs.insertCSS may sometimes fail to inject CSS in a newly navigated -// page when using the chrome.webNavigation API. -// See: https://bugs.chromium.org/p/chromium/issues/detail?id=331654#c15 -// The RenderView should always be ready when the content script begins, so -// this message is used to trigger CSS insertion instead. -chrome.runtime.sendMessage({ - type: 'contentScriptsLoaded', - location: window.location -}) - -const { parseDomain, ParseResultType } = require('parse-domain') - -// Start looking for things to unhide before at most this long after -// the backend script is up and connected (eg backgroundReady = true), -// or sooner if the thread is idle. -const maxTimeMSBeforeStart = 2500 - -// The cutoff for text ads. If something has only text in it, it needs to have -// this many, or more, characters. Similarly, require it to have a non-trivial -// number of words in it, to look like an actual text ad. -const minAdTextChars = 30 -const minAdTextWords = 5 - -const queriedIds = new Set() -const queriedClasses = new Set() - -// Each of these get setup once the mutation observer starts running. -let notYetQueriedClasses: string[] -let notYetQueriedIds: string[] -let cosmeticObserver: MutationObserver | undefined = undefined - -const allSelectorsToRules = new Map() -const cosmeticStyleSheet = new CSSStyleSheet() - -const injectScriptlet = (text: string) => { - let script - try { - script = document.createElement('script') - const textnode: Text = document.createTextNode(text) - script.appendChild(textnode); - (document.head || document.documentElement).appendChild(script) - } catch (ex) { - /* Unused catch */ - } - if (script) { - if (script.parentNode) { - script.parentNode.removeChild(script) - } - script.textContent = '' - } -} - -/** - * Provides a new function which can only be scheduled once at a time. - * - * @param onIdle function to run when the thread is less busy - * @param timeout max time to wait. at or after this time the function will be run regardless of thread noise - */ -const idleize = (onIdle: Function, timeout: number) => { - let idleId: number | undefined = undefined - return function WillRunOnIdle () { - if (idleId !== undefined) { - return - } - idleId = window.requestIdleCallback(() => { - idleId = undefined - onIdle() - }, { timeout }) - } -} - -const isRelativeUrl = (url: string): boolean => { - return ( - !url.startsWith('//') && - !url.startsWith('http://') && - !url.startsWith('https://') - ) -} - -const isElement = (node: Node): boolean => { - return (node.nodeType === 1) -} - -const asElement = (node: Node): Element | null => { - return isElement(node) ? node as Element : null -} - -const isHTMLElement = (node: Node): boolean => { - return ('innerText' in node) -} - -// @ts-ignore unused function -const asHTMLElement = (node: Node): HTMLElement | null => { - return isHTMLElement(node) ? node as HTMLElement : null -} - -const fetchNewClassIdRules = () => { - if ((!notYetQueriedClasses || notYetQueriedClasses.length === 0) && - (!notYetQueriedIds || notYetQueriedIds.length === 0)) { - return - } - chrome.runtime.sendMessage({ - type: 'hiddenClassIdSelectors', - classes: notYetQueriedClasses || [], - ids: notYetQueriedIds || [] - }) - notYetQueriedClasses = [] - notYetQueriedIds = [] -} - -const handleMutations: MutationCallback = (mutations: MutationRecord[]) => { - for (const aMutation of mutations) { - if (aMutation.type === 'attributes') { - // Since we're filtering for attribute modifications, we can be certain - // that the targets are always HTMLElements, and never TextNode. - const changedElm = aMutation.target as Element - switch (aMutation.attributeName) { - case 'class': - for (const aClassName of changedElm.classList.values()) { - if (queriedClasses.has(aClassName) === false) { - notYetQueriedClasses.push(aClassName) - queriedClasses.add(aClassName) - } - } - break - - case 'id': - const mutatedId = changedElm.id - if (queriedIds.has(mutatedId) === false) { - notYetQueriedIds.push(mutatedId) - queriedIds.add(mutatedId) - } - break - } - } else if (aMutation.addedNodes.length > 0) { - for (const node of aMutation.addedNodes) { - const element = asElement(node) - if (!element) { - continue - } - const id = element.id - if (id && !queriedIds.has(id)) { - notYetQueriedIds.push(id) - queriedIds.add(id) - } - const classList = element.classList - if (classList) { - for (const className of classList.values()) { - if (className && !queriedClasses.has(className)) { - notYetQueriedClasses.push(className) - queriedClasses.add(className) - } - } - } - } - } - } - - fetchNewClassIdRules() -} - -const _parseDomainCache = Object.create(null) -const getParsedDomain = (aDomain: string) => { - const cacheResult = _parseDomainCache[aDomain] - if (cacheResult !== undefined) { - return cacheResult - } - - const newResult = parseDomain(aDomain) - _parseDomainCache[aDomain] = newResult - return newResult -} - -const _parsedCurrentDomain = getParsedDomain(window.location.host) -const isFirstPartyUrl = (url: string): boolean => { - if (isRelativeUrl(url)) { - return true - } - - const parsedTargetDomain = getParsedDomain(url) - - if (parsedTargetDomain.type !== _parsedCurrentDomain.type) { - return false - } - - if (parsedTargetDomain.type === ParseResultType.Listed) { - const isSameEtldP1 = (_parsedCurrentDomain.icann.topLevelDomains === parsedTargetDomain.icann.topLevelDomains && - _parsedCurrentDomain.icann.domain === parsedTargetDomain.icann.domain) - return isSameEtldP1 - } - - const looksLikePrivateOrigin = - [ParseResultType.NotListed, ParseResultType.Ip, ParseResultType.Reserved].includes(parsedTargetDomain.type) - if (looksLikePrivateOrigin) { - return _parsedCurrentDomain.hostname === parsedTargetDomain.hostname - } - - return false -} - -const stripChildTagsFromText = (elm: HTMLElement, tagName: string, text: string): string => { - const childElms = Array.from(elm.getElementsByTagName(tagName)) as HTMLElement[] - let localText = text - for (const anElm of childElms) { - localText = localText.replaceAll(anElm.innerText, '') - } - return localText -} - -/** - * Used to just call innerText on the root of the subtree, but in some cases - * this will surprisingly include the text content of script nodes - * (possibly of nodes that haven't been executed yet?). - * - * So instead * we call innerText on the root, and remove the contents of any - * script or style nodes. - * - * @see https://github.com/brave/brave-browser/issues/9955 - */ -const showsSignificantText = (elm: Element): boolean => { - if (isHTMLElement(elm) === false) { - return false - } - - const htmlElm = elm as HTMLElement - const tagsTextToIgnore = ['script', 'style'] - - let currentText = htmlElm.innerText - for (const aTagName of tagsTextToIgnore) { - currentText = stripChildTagsFromText(htmlElm, aTagName, currentText) - } - - const trimmedText = currentText.trim() - if (trimmedText.length < minAdTextChars) { - return false - } - - let wordCount = 0 - for (const aWord of trimmedText.split(' ')) { - if (aWord.trim().length === 0) { - continue - } - wordCount += 1 - } - - return wordCount >= minAdTextWords -} - -interface IsFirstPartyQueryResult { - foundFirstPartyResource: boolean, - foundThirdPartyResource: boolean, - foundKnownThirdPartyAd: boolean -} - -/** - * Determine whether a subtree should be considered as "first party" content. - * - * Uses the following process in making this determination. - * - If the subtree contains any first party resources, the subtree is 1p. - * - If the subtree contains no remote resources, the subtree is first party. - * - Otherwise, its 3rd party. - * - * Note that any instances of "url(" or escape characters in style attributes - * are automatically treated as third-party URLs. These patterns and special - * cases were generated from looking at patterns in ads with resources in the - * style attribute. - * - * Similarly, an empty srcdoc attribute is also considered 3p, since many - * third party ads clear this attribute in practice. - * - * Finally, special case some ids we know are used only for third party ads. - */ -const isSubTreeFirstParty = (elm: Element, possibleQueryResult?: IsFirstPartyQueryResult): boolean => { - let queryResult: IsFirstPartyQueryResult - let isTopLevel: boolean - - if (possibleQueryResult) { - queryResult = possibleQueryResult - isTopLevel = false - } else { - queryResult = { - foundFirstPartyResource: false, - foundThirdPartyResource: false, - foundKnownThirdPartyAd: false - } - isTopLevel = true - } - - if (elm.getAttribute) { - if (elm.hasAttribute('id')) { - const elmId = elm.getAttribute('id') as string - if (elmId.startsWith('google_ads_iframe_') || - elmId.startsWith('div-gpt-ad') || - elmId.startsWith('adfox_')) { - queryResult.foundKnownThirdPartyAd = true - return false - } - } - - if (elm.hasAttribute('src')) { - const elmSrc = elm.getAttribute('src') as string - const elmSrcIsFirstParty = isFirstPartyUrl(elmSrc) - if (elmSrcIsFirstParty === true) { - queryResult.foundFirstPartyResource = true - return true - } - queryResult.foundThirdPartyResource = true - } - - if (elm.hasAttribute('style')) { - const elmStyle = elm.getAttribute('style') as string - if (elmStyle.includes('url(') || - elmStyle.includes('//')) { - queryResult.foundThirdPartyResource = true - } - } - - if (elm.hasAttribute('srcdoc')) { - const elmSrcDoc = elm.getAttribute('srcdoc') as string - if (elmSrcDoc.trim() === '') { - queryResult.foundThirdPartyResource = true - } - } - } - - if (elm.firstChild) { - isSubTreeFirstParty(elm.firstChild as Element, queryResult) - if (queryResult.foundKnownThirdPartyAd === true) { - return false - } - if (queryResult.foundFirstPartyResource === true) { - return true - } - } - - if (elm.nextSibling) { - isSubTreeFirstParty(elm.nextSibling as Element, queryResult) - if (queryResult.foundKnownThirdPartyAd === true) { - return false - } - if (queryResult.foundFirstPartyResource === true) { - return true - } - } - - if (isTopLevel === false) { - return (queryResult.foundThirdPartyResource === false) - } - - if (queryResult.foundThirdPartyResource) { - return false - } - - return true -} - -const unhideSelectors = (selectors: Set) => { - if (selectors.size === 0) { - return - } - // Find selectors we have a rule index for - const rulesToRemove = Array.from(selectors) - .map(selector => allSelectorsToRules.get(selector)) - .filter(i => i !== undefined) - .sort() - .reverse() - // Delete the rules - let lastIdx: number = allSelectorsToRules.size - 1 - for (const ruleIdx of rulesToRemove) { - // Safe to asset ruleIdx is a number because we've already filtered out - // any `undefined` instances with the filter call above. - cosmeticStyleSheet.deleteRule(ruleIdx as number) - } - // Re-sync the indexes - // TODO: Sync is hard, just re-build by iterating through the StyleSheet rules. - const ruleLookup = Array.from(allSelectorsToRules.entries()) - let countAtLastHighest = rulesToRemove.length - for (let i = lastIdx; i > 0; i--) { - const [selector, oldIdx] = ruleLookup[i] - // Is this one we removed? - if (rulesToRemove.includes(i)) { - allSelectorsToRules.delete(selector) - countAtLastHighest-- - if (countAtLastHighest === 0) { - break - } - continue - } - if (oldIdx !== i) { - // Probably out of sync - console.error('Cosmetic Filters: old index did not match lookup index', { selector, oldIdx, i }) - } - allSelectorsToRules.set(selector, oldIdx - countAtLastHighest) - } -} - -const alreadyUnhiddenSelectors = new Set() -const alreadyKnownFirstPartySubtrees = new WeakSet() -// All new selectors go in `firstRunQueue` -const firstRunQueue = new Set() -// Third party matches go in the second and third queues. -const secondRunQueue = new Set() -// Once a selector gets in to this queue, it's only evaluated for 1p content one -// more time. -const finalRunQueue = new Set() -const allQueues = [firstRunQueue, secondRunQueue, finalRunQueue] -const numQueues = allQueues.length -const pumpIntervalMinMs = 40 -const pumpIntervalMaxMs = 1000 -const maxWorkSize = 60 -let queueIsSleeping = false - -/** - * Go through each of the 3 queues, only take 50 items from each one - * 1. Take 50 selects from the first queue with any items - * 2. Determine partyness of matched element: - * - If any are 3rd party, keep 'hide' rule and check again later in next queue. - * - If any are 1st party, remove 'hide' rule and never check selector again. - * 3. If we're looking at the 3rd queue, don't requeue any selectors. - */ -const pumpCosmeticFilterQueues = () => { - if (queueIsSleeping === true) { - return - } - - let didPumpAnything = false - // For each "pump", walk through each queue until we find selectors - // to evaluate. This means that nothing in queue N+1 will be evaluated - // until queue N is completely empty. - for (let queueIndex = 0; queueIndex < numQueues; queueIndex += 1) { - const currentQueue = allQueues[queueIndex] - const nextQueue = allQueues[queueIndex + 1] - if (currentQueue.size === 0) { - continue - } - - const currentWorkLoad = Array.from(currentQueue.values()).slice(0, maxWorkSize) - const comboSelector = currentWorkLoad.join(',') - const matchingElms = document.querySelectorAll(comboSelector) - // Will hold selectors identified by _this_ queue pumping, that were - // newly identified to be matching 1p content. Will be sent to - // the background script to do the un-hiding. - const newlyIdentifiedFirstPartySelectors = new Set() - - for (const aMatchingElm of matchingElms) { - // Don't recheck elements / subtrees we already know are first party. - // Once we know something is third party, we never need to evaluate it - // again. - if (alreadyKnownFirstPartySubtrees.has(aMatchingElm)) { - continue - } - - const elmSubtreeIsFirstParty = isSubTreeFirstParty(aMatchingElm) - // If we find that a subtree is third party, then no need to change - // anything, leave the selector as "hiding" and move on. - // This element will likely be checked again on the next 'pump' - // as long as another element from the selector does not match 1st party. - if (elmSubtreeIsFirstParty === false) { - continue - } - - // If the subtree doesn't have a significant amount of text (e.g., it - // just says "Advertisement"), then no need to change anything; it should - // stay hidden. - if (showsSignificantText(aMatchingElm) === false) { - continue - } - - // Otherwise, we know that the given subtree was evaluated to be - // first party, so we need to figure out which selector from the combo - // selector did the matching. - for (const selector of currentWorkLoad) { - if (aMatchingElm.matches(selector) === false) { - continue - } - - // Similarly, if we already know a selector matches 1p content, - // there is no need to notify the background script again, so - // we don't need to consider further. - if (alreadyUnhiddenSelectors.has(selector) === true) { - continue - } - - newlyIdentifiedFirstPartySelectors.add(selector) - alreadyUnhiddenSelectors.add(selector) - } - alreadyKnownFirstPartySubtrees.add(aMatchingElm) - } - - unhideSelectors(newlyIdentifiedFirstPartySelectors) - - for (const aUsedSelector of currentWorkLoad) { - currentQueue.delete(aUsedSelector) - // Don't requeue selectors we know identify first party content. - const selectorMatchedFirstParty = newlyIdentifiedFirstPartySelectors.has(aUsedSelector) - if (nextQueue && selectorMatchedFirstParty === false) { - nextQueue.add(aUsedSelector) - } - } - - didPumpAnything = true - // If we did something, process the next queue, save it for next time. - break - } - - if (didPumpAnything) { - queueIsSleeping = true - window.setTimeout(() => { - // Set this to false now even though there's a gap in time between now and - // idle since all other calls to `pumpCosmeticFilterQueuesOnIdle` that occur during this time - // will be ignored (and nothing else should be calling `pumpCosmeticFilterQueues` straight). - queueIsSleeping = false - // tslint:disable-next-line:no-use-before-declare - pumpCosmeticFilterQueuesOnIdle() - }, pumpIntervalMinMs) - } -} - -const pumpCosmeticFilterQueuesOnIdle = idleize( - pumpCosmeticFilterQueues, - pumpIntervalMaxMs -) - -const startObserving = () => { - // First queue up any classes and ids that exist before the mutation observer - // starts running. - const elmWithClassOrId = document.querySelectorAll('[class],[id]') - for (const elm of elmWithClassOrId) { - for (const aClassName of elm.classList.values()) { - queriedClasses.add(aClassName) - } - const elmId = elm.getAttribute('id') - if (elmId) { - queriedIds.add(elmId) - } - } - - notYetQueriedClasses = Array.from(queriedClasses) - notYetQueriedIds = Array.from(queriedIds) - fetchNewClassIdRules() - - // Second, set up a mutation observer to handle any new ids or classes - // that are added to the document. - cosmeticObserver = new MutationObserver(handleMutations) - let observerConfig = { - subtree: true, - childList: true, - attributeFilter: ['id', 'class'] - } - cosmeticObserver.observe(document.documentElement, observerConfig) -} - -let _hasDelayOcurred: boolean = false -let _startCheckingId: number | undefined = undefined -const scheduleQueuePump = (hide1pContent: boolean, generichide: boolean) => { - // Three states possible here. First, the delay has already occurred. If so, - // pass through to pumpCosmeticFilterQueues immediately. - if (_hasDelayOcurred === true) { - pumpCosmeticFilterQueuesOnIdle() - return - } - // Second possibility is that we're already waiting for the delay to pass / - // occur. In this case, do nothing. - if (_startCheckingId !== undefined) { - return - } - // Third / final possibility, this is this the first time this has been - // called, in which case set up a timmer and quit - _startCheckingId = window.requestIdleCallback(function ({ didTimeout }) { - _hasDelayOcurred = true - if (!generichide) { - startObserving() - } - if (!hide1pContent) { - pumpCosmeticFilterQueuesOnIdle() - } - }, { timeout: maxTimeMSBeforeStart }) -} - -const vettedSearchEngines = ['duckduckgo', 'qwant', 'bing', 'startpage', 'google', 'yandex', 'ecosia', 'brave'] - -chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { - const action = typeof msg === 'string' ? msg : msg.type - switch (action) { - case 'cosmeticFilteringBackgroundReady': { - if (msg.hideOptions !== undefined) { - scheduleQueuePump(msg.hideOptions.hide1pContent, msg.hideOptions.generichide) - } - injectScriptlet(msg.scriptlet) - break - } - case 'cosmeticFilterConsiderNewSelectors': { - const { selectors } = msg - let nextIndex = cosmeticStyleSheet.rules.length - for (const selector of selectors) { - if (_parsedCurrentDomain.type === ParseResultType.Listed && vettedSearchEngines.includes(_parsedCurrentDomain.icann.domain)) { - continue - } - if (allSelectorsToRules.has(selector)) { - continue - } - // insertRule always adds to index 0, - // so we always add to end of list manually. - cosmeticStyleSheet.insertRule( - `${selector}{display:none !important;}`, - nextIndex - ) - allSelectorsToRules.set(selector, nextIndex) - nextIndex++ - firstRunQueue.add(selector) - } - // @ts-ignore - if (!document.adoptedStyleSheets.includes(cosmeticStyleSheet)) { - // @ts-ignore - document.adoptedStyleSheets = [cosmeticStyleSheet] - } - scheduleQueuePump(false, false) - break - } - } -}) diff --git a/components/brave_extension/extension/brave_extension/manifest.json b/components/brave_extension/extension/brave_extension/manifest.json index 0008d5a5d6b6..74fec14050bc 100644 --- a/components/brave_extension/extension/brave_extension/manifest.json +++ b/components/brave_extension/extension/brave_extension/manifest.json @@ -33,17 +33,6 @@ ] }, "content_scripts": [ - { - "matches": [ - "http://*/*", - "https://*/*" - ], - "js": [ - "out/content_cosmetic.bundle.js" - ], - "run_at": "document_start", - "all_frames": true - }, { "matches": [ "http://*/*", diff --git a/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts b/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts index 09df19301eb6..321289e970e4 100644 --- a/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts +++ b/components/brave_extension/extension/brave_extension/state/shieldsPanelState.ts @@ -60,7 +60,6 @@ export const updateTabShieldsData: shieldState.UpdateTabShieldsData = (state, ta braveShields: 'allow', ads: 'allow', trackers: 'allow', - cosmeticFiltering: 'allow', httpUpgradableResources: 'allow', javascript: 'allow', fingerprinting: 'allow', @@ -117,12 +116,6 @@ export const updateResourceBlocked: shieldState.UpdateResourceBlocked = (state, return { ...state, tabs } } -export const saveCosmeticFilterRuleExceptions: shieldState.SaveCosmeticFilterRuleExceptions = (state, tabId, exceptions) => { - const tabs: shieldState.Tabs = { ...state.tabs } - tabs[tabId] = { ...tabs[tabId], ...{ cosmeticFilters: { ...tabs[tabId].cosmeticFilters, ruleExceptions: exceptions } } } - return { ...state, tabs } -} - export const resetBlockingStats: shieldState.ResetBlockingStats = (state, tabId) => { const tabs: shieldState.Tabs = { ...state.tabs } tabs[tabId] = { ...tabs[tabId], ...{ adsBlocked: 0, trackersBlocked: 0, httpsRedirected: 0, javascriptBlocked: 0, fingerprintingBlocked: 0 } } diff --git a/components/brave_extension/extension/brave_extension/types/actions/shieldsPanelActions.ts b/components/brave_extension/extension/brave_extension/types/actions/shieldsPanelActions.ts index 39c6a8c4c87b..95153159c32e 100644 --- a/components/brave_extension/extension/brave_extension/types/actions/shieldsPanelActions.ts +++ b/components/brave_extension/extension/brave_extension/types/actions/shieldsPanelActions.ts @@ -173,41 +173,6 @@ export interface ShieldsReady { (): ShieldsReadyReturn } -interface GenerateClassIdStylesheetReturn { - type: types.GENERATE_CLASS_ID_STYLESHEET, - tabId: number, - classes: string[], - ids: string[] -} - -export interface GenerateClassIdStylesheet { - (tabId: number, classes: string[], ids: string[]): GenerateClassIdStylesheetReturn -} - -interface CosmeticFilterRuleExceptionsReturn { - type: types.COSMETIC_FILTER_RULE_EXCEPTIONS, - tabId: number, - frameId: number, - exceptions: string[], - scriptlet: string, - generichide: boolean -} - -export interface CosmeticFilterRuleExceptions { - (tabId: number, frameId: number, exceptions: string[], scriptlet: string, generichide: boolean): CosmeticFilterRuleExceptionsReturn -} - -interface ContentScriptsLoadedReturn { - type: types.CONTENT_SCRIPTS_LOADED, - tabId: number, - frameId: number, - url: string, -} - -export interface ContentScriptsLoaded { - (tabId: number, frameId: number, url: string): ContentScriptsLoadedReturn -} - export type shieldPanelActions = ShieldsPanelDataUpdatedReturn | ShieldsToggledReturn | @@ -225,7 +190,4 @@ export type shieldPanelActions = SetAllScriptsBlockedCurrentStateReturn | SetFinalScriptsBlockedStateReturn | SetAdvancedViewFirstAccessReturn | - ShieldsReadyReturn | - GenerateClassIdStylesheetReturn | - CosmeticFilterRuleExceptionsReturn | - ContentScriptsLoadedReturn + ShieldsReadyReturn diff --git a/components/brave_extension/extension/brave_extension/types/adblock/adblockTypes.ts b/components/brave_extension/extension/brave_extension/types/adblock/adblockTypes.ts index 06522f9bf65f..0b5906194927 100644 --- a/components/brave_extension/extension/brave_extension/types/adblock/adblockTypes.ts +++ b/components/brave_extension/extension/brave_extension/types/adblock/adblockTypes.ts @@ -6,7 +6,3 @@ export type BlockTypes = 'shieldsAds' | 'trackers' | 'httpUpgradableResources' | export type BlockOptions = 'allow' | 'block' export type BlockFPOptions = 'allow' | 'block' | 'block_third_party' export type BlockCookiesOptions = 'allow' | 'block' | 'block_third_party' - -export interface CosmeticFilteringState { - ruleExceptions: Array -} diff --git a/components/brave_extension/extension/brave_extension/types/constants/shieldsPanelTypes.ts b/components/brave_extension/extension/brave_extension/types/constants/shieldsPanelTypes.ts index 05c2568a3cbb..646d2f97c11d 100644 --- a/components/brave_extension/extension/brave_extension/types/constants/shieldsPanelTypes.ts +++ b/components/brave_extension/extension/brave_extension/types/constants/shieldsPanelTypes.ts @@ -22,6 +22,3 @@ export type SET_FINAL_SCRIPTS_BLOCKED_ONCE_STATE = typeof types.SET_FINAL_SCRIPT export type SET_ADVANCED_VIEW_FIRST_ACCESS = typeof types.SET_ADVANCED_VIEW_FIRST_ACCESS export type TOGGLE_ADVANCED_VIEW = typeof types.TOGGLE_ADVANCED_VIEW export type SHIELDS_READY = typeof types.SHIELDS_READY -export type GENERATE_CLASS_ID_STYLESHEET = typeof types.GENERATE_CLASS_ID_STYLESHEET -export type COSMETIC_FILTER_RULE_EXCEPTIONS = typeof types.COSMETIC_FILTER_RULE_EXCEPTIONS -export type CONTENT_SCRIPTS_LOADED = typeof types.CONTENT_SCRIPTS_LOADED diff --git a/components/brave_extension/extension/brave_extension/types/state/shieldsPannelState.ts b/components/brave_extension/extension/brave_extension/types/state/shieldsPannelState.ts index 7e7bc553b98f..73a9b8ad78c8 100644 --- a/components/brave_extension/extension/brave_extension/types/state/shieldsPannelState.ts +++ b/components/brave_extension/extension/brave_extension/types/state/shieldsPannelState.ts @@ -3,12 +3,10 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ import { BlockOptions, BlockTypes, BlockFPOptions, BlockCookiesOptions } from '../other/blockTypes' -import { CosmeticFilteringState } from '../adblock/adblockTypes' import { NoScriptInfo } from '../other/noScriptInfo' import { SettingsData } from '../other/settingsTypes' export interface Tab { - cosmeticFiltering: boolean firstPartyCosmeticFiltering: boolean ads: BlockOptions adsBlocked: number @@ -32,7 +30,6 @@ export interface Tab { trackersBlockedResources: Array httpsRedirectedResources: Array fingerprintingBlockedResources: Array - cosmeticFilters: CosmeticFilteringState } export interface Tabs { diff --git a/components/brave_shields/browser/ad_block_service.cc b/components/brave_shields/browser/ad_block_service.cc index 12accbc77ba2..a503a7c3d4bd 100644 --- a/components/brave_shields/browser/ad_block_service.cc +++ b/components/brave_shields/browser/ad_block_service.cc @@ -204,26 +204,15 @@ absl::optional AdBlockService::HiddenClassIdSelectors( custom_selectors = std::move(subscription_selectors); } -#if !defined(OS_ANDROID) && !defined(CHROME_OS) - if (!base::FeatureList::IsEnabled( - brave_shields::features::kBraveAdblockCosmeticFilteringNative)) { - auto result_list = std::make_unique(); - if (hide_selectors && hide_selectors->is_list()) { - result_list->Append(std::move(*hide_selectors)); - } - if (custom_selectors && custom_selectors->is_list()) { - result_list->Append(std::move(*custom_selectors)); - } - - return std::move(*result_list); - } -#endif - if (!hide_selectors || !hide_selectors->is_list()) hide_selectors = base::ListValue(); - if (custom_selectors && custom_selectors->is_list()) - hide_selectors->Append(std::move(*custom_selectors)); + if (custom_selectors && custom_selectors->is_list()) { + for (auto i = custom_selectors->GetList().begin(); + i < custom_selectors->GetList().end(); i++) { + hide_selectors->Append(std::move(*i)); + } + } return hide_selectors; } diff --git a/components/brave_shields/common/features.cc b/components/brave_shields/common/features.cc index 1063749056d1..8a25386c8d3d 100644 --- a/components/brave_shields/common/features.cc +++ b/components/brave_shields/common/features.cc @@ -28,8 +28,6 @@ const base::Feature kBraveAdblockCollapseBlockedElements{ const base::Feature kBraveAdblockCosmeticFiltering{ "BraveAdblockCosmeticFiltering", base::FEATURE_ENABLED_BY_DEFAULT}; -const base::Feature kBraveAdblockCosmeticFilteringNative{ - "BraveAdblockCosmeticFilteringNative", base::FEATURE_DISABLED_BY_DEFAULT}; const base::Feature kBraveAdblockCspRules{ "BraveAdblockCspRules", base::FEATURE_ENABLED_BY_DEFAULT}; // When enabled, Brave will block domains listed in the user's selected adblock diff --git a/components/brave_shields/common/features.h b/components/brave_shields/common/features.h index 796a7e2bf95e..6f0995c4d448 100644 --- a/components/brave_shields/common/features.h +++ b/components/brave_shields/common/features.h @@ -16,7 +16,6 @@ extern const base::Feature kBraveAdblockDefault1pBlocking; extern const base::Feature kBraveAdblockCnameUncloaking; extern const base::Feature kBraveAdblockCollapseBlockedElements; extern const base::Feature kBraveAdblockCosmeticFiltering; -extern const base::Feature kBraveAdblockCosmeticFilteringNative; extern const base::Feature kBraveAdblockCspRules; extern const base::Feature kBraveDomainBlock; extern const base::Feature kBraveExtensionNetworkBlocking; diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 3f2c73e5e992..fa50a5090cee 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -316,7 +316,6 @@ declare namespace chrome.braveShields { const allowScriptsOnce: any const setBraveShieldsEnabledAsync: any const getBraveShieldsEnabledAsync: any - const shouldDoCosmeticFilteringAsync: any const setCosmeticFilteringControlTypeAsync: any const isFirstPartyCosmeticFilteringEnabledAsync: any const setAdControlTypeAsync: any @@ -332,16 +331,6 @@ declare namespace chrome.braveShields { const onShieldsPanelShown: any const reportBrokenSite: any - interface UrlSpecificResources { - hide_selectors: string[] - style_selectors: any - exceptions: string[] - injected_script: string - force_hide_selectors: string[] - generichide: boolean - } - const urlCosmeticResources: (url: string, callback: (resources: UrlSpecificResources) => void) => void - const hiddenClassIdSelectors: (classes: string[], ids: string[], exceptions: string[], callback: (selectors: string[], forceHideSelectors: string[]) => void) => void const addSiteCosmeticFilter: (origin: string, cssSelector: string) => void const openFilterManagementPage: () => void diff --git a/components/test/brave_extension/background/api/shieldsAPI_test.ts b/components/test/brave_extension/background/api/shieldsAPI_test.ts index 9bf7096b6b40..0258bc457dbe 100644 --- a/components/test/brave_extension/background/api/shieldsAPI_test.ts +++ b/components/test/brave_extension/background/api/shieldsAPI_test.ts @@ -40,7 +40,6 @@ describe('Shields API', () => { braveShields: 'block', ads: 'block', trackers: 'block', - cosmeticFiltering: true, firstPartyCosmeticFiltering: false, httpUpgradableResources: 'block', javascript: 'block', @@ -98,7 +97,6 @@ describe('Shields API', () => { const details: ShieldDetails = { ads: 'block', trackers: 'block', - cosmeticFiltering: true, firstPartyCosmeticFiltering: false, httpUpgradableResources: 'block', origin: 'https://www.brave.com', diff --git a/components/test/brave_extension/background/reducers/shieldsPanelReducer_test.ts b/components/test/brave_extension/background/reducers/shieldsPanelReducer_test.ts index 396532cc8bb8..cd170beed98c 100644 --- a/components/test/brave_extension/background/reducers/shieldsPanelReducer_test.ts +++ b/components/test/brave_extension/background/reducers/shieldsPanelReducer_test.ts @@ -39,7 +39,6 @@ const details: ShieldDetails = { javascript: 'block', trackers: 'block', ads: 'block', - cosmeticFiltering: 'block_third_party', fingerprinting: 'block', cookies: 'block' } diff --git a/components/test/brave_extension/components/advancedView/controls/adsTrackersControl_test.tsx b/components/test/brave_extension/components/advancedView/controls/adsTrackersControl_test.tsx index 0aee5f09f487..f461e690216c 100644 --- a/components/test/brave_extension/components/advancedView/controls/adsTrackersControl_test.tsx +++ b/components/test/brave_extension/components/advancedView/controls/adsTrackersControl_test.tsx @@ -14,7 +14,6 @@ const fakeProps: Props = { hostname: 'brave.com', favicon: '', ads: 'allow', - cosmeticFiltering: 'block_third_party', adsBlocked: 0, adsBlockedResources: [], trackers: 'allow', diff --git a/components/test/brave_extension/state/shieldsPanelState_test.ts b/components/test/brave_extension/state/shieldsPanelState_test.ts index 0c81d64046a3..f4ad225d2c2e 100644 --- a/components/test/brave_extension/state/shieldsPanelState_test.ts +++ b/components/test/brave_extension/state/shieldsPanelState_test.ts @@ -170,7 +170,6 @@ describe('shieldsPanelState test', () => { expect(shieldsPanelState.updateTabShieldsData(state, this.tabId, { ads: 'allow', trackers: 'allow', - cosmeticFiltering: 'allow', httpUpgradableResources: 'allow', javascript: 'allow', fingerprinting: 'allow', @@ -183,7 +182,6 @@ describe('shieldsPanelState test', () => { ...state.tabs[this.tabId], ads: 'allow', trackers: 'allow', - cosmeticFiltering: 'allow', httpUpgradableResources: 'allow', javascript: 'allow', fingerprinting: 'allow', diff --git a/components/test/testData.ts b/components/test/testData.ts index 854b0bd89f36..f5f6c3b8aec7 100644 --- a/components/test/testData.ts +++ b/components/test/testData.ts @@ -219,9 +219,6 @@ export const getMockChrome = () => { getBraveShieldsEnabledAsync: function (url: string) { return Promise.resolve(false) }, - shouldDoCosmeticFilteringAsync: function (url: string) { - return Promise.resolve(true) - }, getAdControlTypeAsync: function (url: string) { return Promise.resolve('block') }, diff --git a/renderer/brave_content_renderer_client.cc b/renderer/brave_content_renderer_client.cc index a4dda21a5309..26a79a00e216 100644 --- a/renderer/brave_content_renderer_client.cc +++ b/renderer/brave_content_renderer_client.cc @@ -56,12 +56,11 @@ void BraveContentRendererClient::RenderFrameCreated( content::RenderFrame* render_frame) { ChromeContentRendererClient::RenderFrameCreated(render_frame); -#if !defined(OS_ANDROID) && !defined(CHROME_OS) if (base::FeatureList::IsEnabled( - brave_shields::features::kBraveAdblockCosmeticFilteringNative)) -#endif + brave_shields::features::kBraveAdblockCosmeticFiltering)) { new cosmetic_filters::CosmeticFiltersJsRenderFrameObserver( render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL); + } #if BUILDFLAG(BRAVE_WALLET_ENABLED) if (base::FeatureList::IsEnabled( From 7ad048e09a15600d0e6bd0eaf881ad8e3230ab35 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Wed, 8 Sep 2021 10:54:20 -0700 Subject: [PATCH 2/2] remove unused includes --- browser/extensions/api/brave_shields_api.cc | 2 -- browser/extensions/api/brave_shields_api.h | 1 - 2 files changed, 3 deletions(-) diff --git a/browser/extensions/api/brave_shields_api.cc b/browser/extensions/api/brave_shields_api.cc index 83f26de6c530..2cb8cb484645 100644 --- a/browser/extensions/api/brave_shields_api.cc +++ b/browser/extensions/api/brave_shields_api.cc @@ -7,7 +7,6 @@ #include -#include "base/feature_list.h" #include "base/strings/string_number_conversions.h" #include "brave/browser/brave_browser_process.h" #include "brave/browser/brave_shields/brave_shields_web_contents_observer.h" @@ -20,7 +19,6 @@ #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/common/brave_shield_constants.h" -#include "brave/components/brave_shields/common/features.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/extensions/api/tabs/tabs_constants.h" diff --git a/browser/extensions/api/brave_shields_api.h b/browser/extensions/api/brave_shields_api.h index 5c58c99c6cd6..365dee00ab72 100644 --- a/browser/extensions/api/brave_shields_api.h +++ b/browser/extensions/api/brave_shields_api.h @@ -8,7 +8,6 @@ #include #include -#include #include "extensions/browser/extension_function.h"