diff --git a/browser/brave_browser_process_impl.cc b/browser/brave_browser_process_impl.cc index a15bf9c044a9..8b184a08a127 100644 --- a/browser/brave_browser_process_impl.cc +++ b/browser/brave_browser_process_impl.cc @@ -27,7 +27,6 @@ #include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h" #include "brave/components/brave_component_updater/browser/local_data_files_service.h" #include "brave/components/brave_referrals/browser/brave_referrals_service.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h" #include "brave/components/brave_shields/browser/brave_farbling_service.h" diff --git a/browser/brave_shields/ad_block_service_browsertest.cc b/browser/brave_shields/ad_block_service_browsertest.cc index 2576bb2db87e..aa40c4efa434 100644 --- a/browser/brave_shields/ad_block_service_browsertest.cc +++ b/browser/brave_shields/ad_block_service_browsertest.cc @@ -19,9 +19,9 @@ #include "base/test/thread_test_helper.h" #include "brave/browser/brave_browser_process.h" #include "brave/browser/net/brave_ad_block_tp_network_delegate_helper.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_provider.h" #include "brave/components/brave_shields/browser/ad_block_engine.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager_observer.h" @@ -92,10 +92,7 @@ using brave_shields::features::kCosmeticFilteringJsPerformance; AdBlockServiceTest::AdBlockServiceTest() : ws_server_(net::SpawnedTestServer::TYPE_WS, net::GetWebSocketTestDataDirectory()), - https_server_(net::EmbeddedTestServer::Type::TYPE_HTTPS) { - brave_shields::SetDefaultAdBlockComponentIdAndBase64PublicKeyForTest( - kDefaultAdBlockComponentTestId, kDefaultAdBlockComponentTest64PublicKey); -} + https_server_(net::EmbeddedTestServer::Type::TYPE_HTTPS) {} AdBlockServiceTest::~AdBlockServiceTest() = default; void AdBlockServiceTest::SetUpCommandLine(base::CommandLine* command_line) { @@ -150,26 +147,10 @@ HostContentSettingsMap* AdBlockServiceTest::content_settings() { void AdBlockServiceTest::UpdateAdBlockInstanceWithRules( const std::string& rules, - const std::string& resources) { - auto source_provider = - std::make_unique(rules, resources); - - brave_shields::AdBlockService* ad_block_service = - g_brave_browser_process->ad_block_service(); - ad_block_service->UseSourceProvidersForTest(source_provider.get(), - source_provider.get()); - - source_providers_.push_back(std::move(source_provider)); - - WaitForAdBlockServiceThreads(); -} - -void AdBlockServiceTest::UpdateAdBlockInstanceWithDAT( - const base::FilePath& dat_location, - const std::string& resources) { - base::ScopedAllowBlockingForTesting allow_blocking; + const std::string& resources, + uint8_t permission_mask) { auto source_provider = std::make_unique( - dat_location, resources); + rules, resources, true, permission_mask); brave_shields::AdBlockService* ad_block_service = g_brave_browser_process->ad_block_service(); @@ -184,8 +165,8 @@ void AdBlockServiceTest::UpdateAdBlockInstanceWithDAT( void AdBlockServiceTest::UpdateCustomAdBlockInstanceWithRules( const std::string& rules, const std::string& resources) { - auto source_provider = - std::make_unique(rules, resources); + auto source_provider = std::make_unique( + rules, resources, false); brave_shields::AdBlockService* ad_block_service = g_brave_browser_process->ad_block_service(); @@ -226,18 +207,31 @@ void AdBlockServiceTest::GetTestDataDir(base::FilePath* test_data_dir) { base::PathService::Get(brave::DIR_TEST_DATA, test_data_dir); } -bool AdBlockServiceTest::InstallDefaultAdBlockExtension( - const std::string& extension_dir) { +bool AdBlockServiceTest::InstallDefaultAdBlockComponent( + const std::string& component_dir) { base::FilePath test_data_dir; GetTestDataDir(&test_data_dir); - const extensions::Extension* ad_block_extension = LoadExtensionAsComponent( - test_data_dir.AppendASCII("adblock-data").AppendASCII(extension_dir)); - if (!ad_block_extension) + g_brave_browser_process->ad_block_service() + ->component_service_manager() + ->SetFilterListCatalog({brave_shields::FilterListCatalogEntry( + "default", "", "Brave Ad Block Updater", {}, "", + "Default lists for Brave Browser", true, true, true, 0, + kDefaultAdBlockComponentTestId, + kDefaultAdBlockComponentTest64PublicKey, "", "")}); + const extensions::Extension* ad_block_component = LoadExtensionAsComponent( + test_data_dir.AppendASCII("adblock-data").AppendASCII(component_dir)); + if (!ad_block_component) { return false; + } + + auto& component_providers = g_brave_browser_process->ad_block_service() + ->component_service_manager() + ->regional_filters_providers(); + EXPECT_EQ(component_providers.size(), 1UL); + auto& default_provider = component_providers.at("default"); + EXPECT_TRUE(default_provider); + default_provider->OnComponentReady(ad_block_component->path()); - g_brave_browser_process->ad_block_service() - ->default_filters_provider() - ->OnComponentReady(ad_block_extension->path()); WaitForAdBlockServiceThreads(); return true; @@ -268,12 +262,11 @@ class EngineTestObserver : public brave_shields::AdBlockEngine::TestObserver { raw_ptr engine_ = nullptr; }; -bool AdBlockServiceTest::InstallRegionalAdBlockExtension( +bool AdBlockServiceTest::InstallRegionalAdBlockComponent( const std::string& uuid, bool enable_list) { - // The regional adblock engines depend on the default engine for resource - // loads. - EXPECT_TRUE(InstallDefaultAdBlockExtension()); + // Ensure the default engine is initialized first + EXPECT_TRUE(InstallDefaultAdBlockComponent()); auto* default_engine = g_brave_browser_process->ad_block_service()->default_engine_.get(); EngineTestObserver default_engine_observer(default_engine); @@ -285,36 +278,38 @@ bool AdBlockServiceTest::InstallRegionalAdBlockExtension( filter_list_catalog.push_back(brave_shields::FilterListCatalogEntry( uuid, "https://easylist-downloads.adblockplus.org/liste_fr.txt", "EasyList Liste FR", {"fr"}, "https://forums.lanik.us/viewforum.php?f=91", + "Removes advertisements from French websites", false, false, false, 0, kRegionalAdBlockComponentTestId, kRegionalAdBlockComponentTest64PublicKey, - "Removes advertisements from French websites", "", "")); + "", "")); g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->SetFilterListCatalog(filter_list_catalog); if (enable_list) { - const extensions::Extension* ad_block_extension = + const extensions::Extension* ad_block_component = LoadExtensionAsComponent(test_data_dir.AppendASCII("adblock-data") .AppendASCII("adblock-regional")); - if (!ad_block_extension) + if (!ad_block_component) { return false; + } g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->EnableFilterList(uuid, true); const auto& regional_filters_providers = g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->regional_filters_providers(); - EXPECT_EQ(regional_filters_providers.size(), 1ULL); + EXPECT_EQ(regional_filters_providers.size(), 2ULL); auto* regional_engine = g_brave_browser_process->ad_block_service() ->additional_filters_engine_.get(); EngineTestObserver regional_engine_observer(regional_engine); auto regional_filters_provider = regional_filters_providers.find(uuid); regional_filters_provider->second->OnComponentReady( - ad_block_extension->path()); + ad_block_component->path()); regional_engine_observer.Wait(); } @@ -351,7 +346,7 @@ void AdBlockServiceTest::DisableAggressiveMode() { // Load a page with an ad image, and make sure it is blocked. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -369,7 +364,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByDefaultBlocker) { // blocked by custom filters. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByCustomBlocker) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateCustomAdBlockInstanceWithRules("*ad_banner.png"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -388,7 +383,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // Load a page with an ad image, and make sure it is blocked by custom // filters. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByCustomBlocker) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules(""); @@ -409,7 +404,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByCustomBlocker) { // Load a page with an ad image, with a corresponding exception installed in // the custom filters, and make sure it is not blocked. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DefaultBlockCustomException) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("*ad_banner.png"); @@ -429,7 +424,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DefaultBlockCustomException) { // Load a page with an image blocked by custom filters, with a corresponding // exception installed in the default filters, and make sure it is not blocked. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CustomBlockDefaultException) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("@@ad_banner.png"); UpdateCustomAdBlockInstanceWithRules("*ad_banner.png"); @@ -449,7 +444,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CustomBlockDefaultException) { // blocked. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByDefaultBlocker) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -463,15 +458,15 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); } -// Load a page with an ad image, and make sure it is blocked by the -// regional blocker. +// Load a page with an ad image, and make sure it is blocked by a filter from a +// regional list component. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByRegionalBlocker) { g_browser_process->SetApplicationLocale("fr"); ASSERT_STREQ(g_browser_process->GetApplicationLocale().c_str(), "fr"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); - ASSERT_TRUE(InstallRegionalAdBlockExtension(kAdBlockEasyListFranceUUID)); + ASSERT_TRUE(InstallRegionalAdBlockComponent(kAdBlockEasyListFranceUUID)); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); @@ -485,7 +480,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdsGetBlockedByRegionalBlocker) { } // Load a page with an image which is not an ad, and make sure it is -// NOT blocked by the regional blocker. +// NOT blocked by a filter from a regional list. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NotAdsDoNotGetBlockedByRegionalBlocker) { g_browser_process->SetApplicationLocale("fr"); @@ -493,7 +488,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); - ASSERT_TRUE(InstallRegionalAdBlockExtension(kAdBlockEasyListFranceUUID)); + ASSERT_TRUE(InstallRegionalAdBlockComponent(kAdBlockEasyListFranceUUID)); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url)); @@ -509,7 +504,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // Load a page with several of the same adblocked xhr requests, it should only // count 1. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoSameAdsGetCountedAsOne) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -531,7 +526,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoSameAdsGetCountedAsOne) { // Load a page with different adblocked xhr requests, it should count each. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoDiffAdsGetCountedAsTwo) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -553,7 +548,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TwoDiffAdsGetCountedAsTwo) { // New tab continues to count blocking the same resource IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NewTabContinuesToBlock) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -579,7 +574,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, NewTabContinuesToBlock) { // XHRs and ads in a cross-site iframe are blocked as well. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrame) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL("a.com", "/iframe_blocking.html"); @@ -610,7 +605,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrame) { // Checks nothing is blocked if shields are off. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrameShieldsOff) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL("a.com", "/iframe_blocking.html"); @@ -645,7 +640,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubFrameShieldsOff) { // Requests made by a service worker should be blocked as well. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ServiceWorkerRequest) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("adbanner.js"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -662,7 +657,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ServiceWorkerRequest) { } IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, WebSocketBlocking) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("*$websocket"); ASSERT_TRUE(ws_server_.Start()); @@ -679,10 +674,10 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, WebSocketBlocking) { ws_url.spec().c_str()))); } -// Load a page with an ad image which is matched on the regional blocker, -// but make sure it is saved by the default ad_block_client's exception. -// This test is the same as AdsGetBlockedByRegionalBlocker except for at -// the start it adds an exception rule to the non regional adblocker. +// Load a page with an ad image which is matched by a filter in the additional +// engine, but make sure it is saved by an exception in the default engine. +// This test is the same as AdsGetBlockedByRegionalBlocker except for at the +// start it adds an exception rule to the default engine. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ExceptionAdsAreAllowedAcrossClients) { g_browser_process->SetApplicationLocale("fr"); @@ -690,7 +685,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); - ASSERT_TRUE(InstallRegionalAdBlockExtension(kAdBlockEasyListFranceUUID)); + ASSERT_TRUE(InstallRegionalAdBlockComponent(kAdBlockEasyListFranceUUID)); UpdateAdBlockInstanceWithRules("*ad_fr*\n@@*ad_fr.png*"); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -705,7 +700,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // Make sure the third-party flag is passed into the ad-block library properly IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdBlockThirdPartyWorksByETLDP1) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||a.com$third-party"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -725,7 +720,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdBlockThirdPartyWorksByETLDP1) { // Make sure the third-party flag is passed into the ad-block library properly IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, AdBlockThirdPartyWorksForThirdPartyHost) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||a.com$third-party"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL tab_url = embedded_test_server()->GetURL("b.com", kAdBlockTestPage); @@ -791,7 +786,7 @@ class TestAdBlockSubscriptionServiceManagerObserver // The download in this test fails intermittently with a network error code, // although it doesn't seem to occur in real usage. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeToCustomSubscription) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL subscription_url = embedded_test_server()->GetURL("lists.com", "/list.txt"); @@ -901,7 +896,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeToCustomSubscription) { // Make sure the state of a list that cannot be fetched is as expected IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeTo404List) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL subscription_url = embedded_test_server()->GetURL("lists.com", "/this/list/does/not/exist"); @@ -946,7 +941,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeTo404List) { // Make sure that a list cannot be subscribed to twice IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeToListUrlTwice) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL subscription_url = embedded_test_server()->GetURL("lists.com", "/this/list/does/not/exist"); @@ -989,7 +984,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SubscribeToListUrlTwice) { // issue the correct number of DNS resolutions IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, MAYBE_CnameCloakedRequestsGetBlocked) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||cname-cloak-endpoint.tracking.com^"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL tab_url = embedded_test_server()->GetURL("a.com", kAdBlockTestPage); @@ -1072,7 +1067,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // to its CNAME-uncloaked equivalent. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, MAYBE_CnameCloakedRequestsCanBeExcepted) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "||cname-cloak-endpoint.tracking.com^\n" "@@a.com*/logo.png?unblock^"); @@ -1179,7 +1174,7 @@ class CnameUncloakingFlagDisabledTest : public AdBlockServiceTest { // Make sure that CNAME uncloaking does not occur when the CNAME uncloaking // flag is disabled. IN_PROC_BROWSER_TEST_F(CnameUncloakingFlagDisabledTest, NoDnsQueriesIssued) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||cname-cloak-endpoint.tracking.com^"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL tab_url = embedded_test_server()->GetURL("a.com", kAdBlockTestPage); @@ -1258,7 +1253,7 @@ IN_PROC_BROWSER_TEST_F(CnameUncloakingFlagDisabledTest, NoDnsQueriesIssued) { // Load an image from a specific subdomain, and make sure it is blocked. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, BlockNYP) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||sp1.nypost.com$third-party"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL tab_url = embedded_test_server()->GetURL("b.com", kAdBlockTestPage); @@ -1276,7 +1271,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, BlockNYP) { // Frame root URL is used for context rather than the tab URL IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, FrameSourceURL) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("adbanner.js$domain=a.com"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL("a.com", "/iframe_blocking.html"); @@ -1301,7 +1296,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, FrameSourceURL) { // Tags for social buttons work IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBlockTagTest) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( base::StringPrintf("||example.com^$tag=%s", brave_shields::kFacebookEmbeds) @@ -1325,7 +1320,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBlockTagTest) { // Lack of tags for social buttons work IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBlockDiffTagTest) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("||example.com^$tag=sup"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL tab_url = embedded_test_server()->GetURL("b.com", kAdBlockTestPage); @@ -1346,7 +1341,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, SocialButttonAdBlockDiffTagTest) { // Tags are preserved after resetting IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ResetPreservesTags) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); g_brave_browser_process->ad_block_service()->EnableTag( brave_shields::kFacebookEmbeds, true); WaitForAdBlockServiceThreads(); @@ -1404,7 +1399,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, TagPrefsControlTags) { // Load a page with a blocked image, and make sure it is collapsed. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CollapseBlockedImage) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -1427,7 +1422,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CollapseBlockedImage) { // Load a page with a blocked iframe, and make sure it is collapsed. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CollapseBlockedIframe) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -1459,7 +1454,7 @@ class CollapseBlockedElementsFlagDisabledTest : public AdBlockServiceTest { // Load a page with a blocked image, and make sure it is not collapsed. IN_PROC_BROWSER_TEST_F(CollapseBlockedElementsFlagDisabledTest, DontCollapseBlockedImage) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -1483,7 +1478,7 @@ IN_PROC_BROWSER_TEST_F(CollapseBlockedElementsFlagDisabledTest, // Load a page with a blocked iframe, and make sure it is not collapsed. IN_PROC_BROWSER_TEST_F(CollapseBlockedElementsFlagDisabledTest, DontCollapseBlockedIframe) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); GURL url = embedded_test_server()->GetURL(kAdBlockTestPage); @@ -1516,7 +1511,7 @@ class Default1pBlockingFlagDisabledTest : public AdBlockServiceTest { // match the same filter in the default engine. Ensure the third-party one is // blocked while the first-party one is allowed. IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Default1pBlocking) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("^ad_banner.png"); @@ -1541,7 +1536,7 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Default1pBlocking) { // match the same filter in the default engine. They should both be blocked on // special URLs like this one. IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, SpecialUrlException) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("^ad_banner.png"); @@ -1568,7 +1563,7 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, SpecialUrlException) { // ensure that both are blocked. IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Aggressive1pBlocking) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("^ad_banner.png"); @@ -1592,7 +1587,7 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, // match the same filter in the custom filters engine. Ensure that both are // blocked. IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Custom1pBlocking) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateCustomAdBlockInstanceWithRules("^ad_banner.png"); @@ -1616,7 +1611,7 @@ IN_PROC_BROWSER_TEST_F(Default1pBlockingFlagDisabledTest, Custom1pBlocking) { // Load a page with a script which uses a redirect data URL. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectRulesAreRespected) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("js_mock_me.js$redirect=noopjs", R"( [ @@ -1651,18 +1646,8 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectRulesAreRespected) { // A redirection should only be applied if there's also a matching blocking // rule. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectWithoutBlockIsNoop) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); - // The DAT for this test contains the following rules: - // .js?block=true - // js_mock_me.js$redirect-rule=noopjs - // At the time of this test's writing, `redirect-rule` parsing is currently - // not supported by the engine, but it should work correctly when the CRX - // packager eventually begins shipping DATs that include them. - base::FilePath test_data_dir; - GetTestDataDir(&test_data_dir); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); - base::FilePath dat_location = test_data_dir.AppendASCII("adblock-data") - .AppendASCII("redirect-rule.dat"); std::string resources = R"([{ "name": "noop.js", "aliases": ["noopjs"], @@ -1671,7 +1656,10 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectWithoutBlockIsNoop) { }, "content": "KGZ1bmN0aW9uKCkgewogICAgJ3VzZSBzdHJpY3QnOwp9KSgpOwo=" }])"; - UpdateAdBlockInstanceWithDAT(dat_location, resources); + UpdateAdBlockInstanceWithRules( + ".js?block=true\n" + "js_mock_me.js$redirect-rule=noopjs", + resources); WaitForAdBlockServiceThreads(); @@ -1727,7 +1715,7 @@ std::unique_ptr NoParamHandler( // `$removeparam` should be respected for subresource requests IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RemoveparamSubresource) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("*$subdocument,removeparam=evil"); @@ -1760,7 +1748,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RemoveparamSubresource) { // `$removeparam` should be respected for top-level navigations IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RemoveparamTopLevelNavigation) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); UpdateAdBlockInstanceWithRules("*$document,removeparam=evil"); @@ -1783,7 +1771,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RemoveparamTopLevelNavigation) { // `$removeparam` should not be activated in default blocking mode IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DefaultNoRemoveparam) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -1810,7 +1798,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DefaultNoRemoveparam) { // Verify that scripts violating a Content Security Policy from a `$csp` rule // are not loaded. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CspRule) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "||example.com^$csp=script-src 'nonce-abcdef' 'unsafe-eval' 'self'"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -1839,7 +1827,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CspRule) { // The policy resulting from two of the same kind of directive will be the // union of both. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CspRuleMerging) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "||example.com^$csp=script-src 'nonce-abcdef' 'unsafe-eval' 'self'"); UpdateCustomAdBlockInstanceWithRules( @@ -1866,10 +1854,32 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CspRuleMerging) { EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); } +// Lists with `hidden` set to `true` should not be shown in `GetRegionalLists`. +IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, HiddenListsNotPresented) { + std::vector filter_list_catalog; + filter_list_catalog.push_back(brave_shields::FilterListCatalogEntry( + "uuid1", "https://example.com", "Hidden list", {}, + "https://support.example.com", "first list", true, false, false, 0, + "testid1", "pubkey1", "", "")); + filter_list_catalog.push_back(brave_shields::FilterListCatalogEntry( + "uuid2", "https://example.com", "Normal list", {}, + "https://support.example.com", "second list", false, false, false, 0, + "testid2", "pubkey2", "", "")); + g_brave_browser_process->ad_block_service() + ->component_service_manager() + ->SetFilterListCatalog(filter_list_catalog); + + auto regional_lists = g_brave_browser_process->ad_block_service() + ->component_service_manager() + ->GetRegionalLists(); + + ASSERT_EQ(regional_lists.size(), 1UL); +} + // Verify that scripts violating a Content Security Policy from a `$csp` rule // are not loaded. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CspRuleShieldsDown) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "||example.com^$csp=script-src 'nonce-abcdef' 'unsafe-eval' 'self'"); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL); @@ -1906,7 +1916,7 @@ class CosmeticFilteringFlagDisabledTest : public AdBlockServiceTest { // Ensure no cosmetic filtering occurs when the feature flag is disabled IN_PROC_BROWSER_TEST_F(CosmeticFilteringFlagDisabledTest, CosmeticFilteringSimple) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "b.com###ad-banner\n" "##.ad"); @@ -1951,7 +1961,7 @@ class CosmeticFilteringPlaylistFlagEnabledTest : public AdBlockServiceTest { // background web contents. IN_PROC_BROWSER_TEST_F(CosmeticFilteringPlaylistFlagEnabledTest, AllowCosmeticFiltering) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); const GURL url = embedded_test_server()->GetURL("b.com", "/cosmetic_filtering.html"); @@ -1975,7 +1985,7 @@ IN_PROC_BROWSER_TEST_F(CosmeticFilteringPlaylistFlagEnabledTest, // Ensure no cosmetic filtering occurs when the shields setting is disabled IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDisabled) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); brave_shields::SetCosmeticFilteringControlType( content_settings(), brave_shields::ControlType::ALLOW, GURL()); UpdateAdBlockInstanceWithRules( @@ -2000,7 +2010,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDisabled) { // Test simple cosmetic filtering IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "b.com###ad-banner\n" "##.ad"); @@ -2031,7 +2041,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) { // Test that cosmetic filtering is applied independently in a third-party child // frame IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringFrames) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("frame.com##.ad\n"); GURL tab_url = @@ -2068,7 +2078,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringFrames) { // blocking mode IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHasPseudoclassStandard) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); UpdateAdBlockInstanceWithRules("b.com##.container:has(#promotion)\n"); @@ -2087,7 +2097,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // aggressive blocking mode IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHasPseudoclassAggressive) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("b.com##.container:has(#promotion)\n"); GURL tab_url = @@ -2117,7 +2127,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // Test cosmetic filtering ignores content determined to be 1st party IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringProtect1p) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); DisableAggressiveMode(); UpdateAdBlockInstanceWithRules( "appspot.com##.fpsponsored\n" @@ -2156,7 +2166,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringProtect1p) { // Test cosmetic filtering bypasses 1st party checks in Aggressive mode IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHide1pContent) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("b.com##.fpsponsored\n"); GURL tab_url = @@ -2174,7 +2184,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHide1pContent) { // Test cosmetic filtering on elements added dynamically IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("##.blockme\n##.hide-innerhtml"); GURL tab_url = @@ -2206,7 +2216,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) { // Test cosmetic filtering on elements added dynamically, using a rule from the // custom filters IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamicCustom) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); ASSERT_TRUE(g_brave_browser_process->ad_block_service() ->custom_filters_provider() ->UpdateCustomFilters("##.blockme")); @@ -2234,7 +2244,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamicCustom) { // `generichide` exception rule, both for elements added dynamically and // elements present at page load IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringGenerichide) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "##.blockme\n" "##img[src=\"https://example.com/logo.png\"]\n" @@ -2259,7 +2269,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringGenerichide) { // Test custom style rules IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringCustomStyle) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("b.com##.ad:style(padding-bottom: 10px)"); GURL tab_url = @@ -2277,7 +2287,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringCustomStyle) { // Test rules overridden by hostname-specific exception rules IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringUnhide) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "##.ad\n" "b.com#@#.ad\n" @@ -2304,7 +2314,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringUnhide) { // Test scriptlet injection that modifies window attributes IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringWindowScriptlet) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); std::string scriptlet = "(function() {" " const send = window.getComputedStyle;" @@ -2336,6 +2346,64 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringWindowScriptlet) { EXPECT_EQ(base::Value(true), result.value); } +// Test that permissioned scriptlets can only be injected from appropriately +// permissioned lists +IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ScriptletInjectionPermissions) { + ASSERT_TRUE(InstallDefaultAdBlockComponent()); + std::string scriptlet = + "(function() {" + " window.success = true;" + "})();"; + std::string scriptlet_base64; + base::Base64Encode(scriptlet, &scriptlet_base64); + std::string resources = + "[{" + "\"name\": \"set-success.js\"," + "\"aliases\": []," + "\"kind\": {\"mime\": \"application/javascript\"}," + "\"permission\": 3," // i.e. 0b00000011 + "\"content\": \"" + + scriptlet_base64 + "\"}]"; + std::string rules = "b.com##+js(set-success)"; + + GURL tab_url = + embedded_test_server()->GetURL("b.com", "/cosmetic_filtering.html"); + content::WebContents* contents; + + // Add the list with default (i.e. no) permissions + UpdateAdBlockInstanceWithRules(rules, resources); + + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url)); + contents = browser()->tab_strip_model()->GetActiveWebContents(); + + { + auto result = EvalJs(contents, R"(window.success === undefined)"); + EXPECT_EQ(base::Value(true), result.value); + } + + // Add a list with different but still insufficient permissions + UpdateAdBlockInstanceWithRules(rules, resources, 5); + + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url)); + contents = browser()->tab_strip_model()->GetActiveWebContents(); + + { + auto result = EvalJs(contents, R"(window.success === undefined)"); + EXPECT_EQ(base::Value(true), result.value); + } + + // Finally add a list with sufficient permissions + UpdateAdBlockInstanceWithRules(rules, resources, 7); + + ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url)); + contents = browser()->tab_strip_model()->GetActiveWebContents(); + + { + auto result = EvalJs(contents, R"(window.success)"); + EXPECT_EQ(base::Value(true), result.value); + } +} + class ScriptletDebugLogsFlagEnabledTest : public AdBlockServiceTest { public: ScriptletDebugLogsFlagEnabledTest() { @@ -2349,7 +2417,7 @@ class ScriptletDebugLogsFlagEnabledTest : public AdBlockServiceTest { // Test that scriptlet injection has access to `canDebug` inside of // `scriptletGlobals`, and that it is set to `true`. IN_PROC_BROWSER_TEST_F(ScriptletDebugLogsFlagEnabledTest, CanDebugSetToTrue) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); std::string scriptlet = "(function() {" " if (scriptletGlobals.get('canDebug')) {" @@ -2424,7 +2492,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CheckForDeAmpPref) { // Test scriptlet injection that modifies window attributes IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringIframeScriptlet) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); std::string scriptlet = "(function() {" " window.JSON.parse = function() { return {} }" @@ -2454,7 +2522,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringIframeScriptlet) { // marker on its `display` style. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DISABLED_CosmeticFilteringOverridesImportant) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("###inline-block-important"); GURL tab_url = @@ -2476,7 +2544,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, // exception policy. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CustomCosmeticFilteringOverridesImportant) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateCustomAdBlockInstanceWithRules("###inline-block-important"); GURL tab_url = @@ -2521,11 +2589,11 @@ class CookieListPrefObserver { // List UUID to be enabled, until manually enabled and then disabled again. IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ListEnabled) { ASSERT_TRUE( - InstallRegionalAdBlockExtension(brave_shields::kCookieListUuid, false)); + InstallRegionalAdBlockComponent(brave_shields::kCookieListUuid, false)); { const auto lists = g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->GetRegionalLists(); // Although never explicitly enabled, it should be presented as enabled by // default at first. @@ -2537,14 +2605,14 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, ListEnabled) { { CookieListPrefObserver pref_observer(g_browser_process->local_state()); g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->EnableFilterList(brave_shields::kCookieListUuid, false); pref_observer.Wait(); } { const auto lists = g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->GetRegionalLists(); // It should be actually disabled now. ASSERT_EQ(1UL, lists.size()); @@ -2597,7 +2665,7 @@ class AdBlockServiceTestJsPerformance : public AdBlockServiceTest { IN_PROC_BROWSER_TEST_F(AdBlockServiceTestJsPerformance, CosmeticFilteringDynamic) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules( "##.div-class-100\n##.div-class-500\n##.div-class-1000"); @@ -2641,7 +2709,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTestJsPerformance, IN_PROC_BROWSER_TEST_F(AdBlockServiceTestJsPerformance, CosmeticFilteringSubframeDynamic) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); UpdateAdBlockInstanceWithRules("##.div-class-500"); GURL tab_url = diff --git a/browser/brave_shields/ad_block_service_browsertest.h b/browser/brave_shields/ad_block_service_browsertest.h index 36e68f7cbbd9..5dc5182f7610 100644 --- a/browser/brave_shields/ad_block_service_browsertest.h +++ b/browser/brave_shields/ad_block_service_browsertest.h @@ -40,7 +40,8 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest { HostContentSettingsMap* content_settings(); void UpdateAdBlockInstanceWithRules(const std::string& rules, - const std::string& resources = "[]"); + const std::string& resources = "[]", + uint8_t permission_mask = 0); void UpdateAdBlockInstanceWithDAT(const base::FilePath& dat_location, const std::string& resources = "[]"); void UpdateCustomAdBlockInstanceWithRules( @@ -51,9 +52,9 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest { void GetTestDataDir(base::FilePath* test_data_dir); void SetDefaultComponentIdAndBase64PublicKeyForTest(); void SetRegionalComponentIdAndBase64PublicKeyForTest(); - bool InstallDefaultAdBlockExtension( + bool InstallDefaultAdBlockComponent( const std::string& extension_dir = "adblock-default"); - bool InstallRegionalAdBlockExtension(const std::string& uuid, + bool InstallRegionalAdBlockComponent(const std::string& uuid, bool enable_list = true); void SetSubscriptionIntervals(); void WaitForAdBlockServiceThreads(); diff --git a/browser/brave_shields/domain_block_page_browsertest.cc b/browser/brave_shields/domain_block_page_browsertest.cc index 7720e299d14a..017f9b52ba5b 100644 --- a/browser/brave_shields/domain_block_page_browsertest.cc +++ b/browser/brave_shields/domain_block_page_browsertest.cc @@ -105,7 +105,7 @@ class DomainBlockDisabledTest : public DomainBlockTestBase { }; IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitial) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -121,7 +121,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitial) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndProceed) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -145,7 +145,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndProceed) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndReload) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -166,7 +166,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndReload) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedAndReload) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -199,7 +199,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedAndReload) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedDoesNotAffectNewTabs) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -233,7 +233,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedDoesNotAffectNewTabs) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, DontWarnAgainAndProceed) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -274,7 +274,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, DontWarnAgainAndProceed) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndGoBack) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url_a = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url_a); @@ -300,7 +300,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ShowInterstitialAndGoBack) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoFetch) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); ASSERT_EQ(0, request_count_); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -319,7 +319,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoFetch) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoThirdPartyInterstitial) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); ASSERT_TRUE(g_brave_browser_process->ad_block_service() ->custom_filters_provider() ->UpdateCustomFilters("||b.com^$third-party")); @@ -354,7 +354,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoThirdPartyInterstitial) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoInterstitialUnlessAggressive) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -387,7 +387,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, NoInterstitialUnlessAggressive) { } IN_PROC_BROWSER_TEST_F(DomainBlockDisabledTest, NoInterstitial) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url); @@ -409,7 +409,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockDisabledTest, NoInterstitial) { } IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedDoesNotAffectOtherDomains) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url_a = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url_a); @@ -450,7 +450,7 @@ IN_PROC_BROWSER_TEST_F(DomainBlockTest, ProceedDoesNotAffectOtherDomains) { IN_PROC_BROWSER_TEST_F(DomainBlockTest, DontWarnAgainDoesNotAffectOtherDomains) { - ASSERT_TRUE(InstallDefaultAdBlockExtension()); + ASSERT_TRUE(InstallDefaultAdBlockComponent()); GURL url_a = embedded_test_server()->GetURL("a.com", "/simple.html"); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, url_a); diff --git a/browser/debounce/debounce_browsertest.cc b/browser/debounce/debounce_browsertest.cc index 919594d8bfc3..ae3219fdda7f 100644 --- a/browser/debounce/debounce_browsertest.cc +++ b/browser/debounce/debounce_browsertest.cc @@ -5,7 +5,6 @@ #include "base/base64url.h" #include "base/memory/raw_ptr.h" -#include "base/path_service.h" #include "base/scoped_observation.h" #include "base/strings/stringprintf.h" #include "base/test/scoped_feature_list.h" @@ -13,11 +12,9 @@ #include "brave/browser/brave_browser_process.h" #include "brave/browser/brave_content_browser_client.h" #include "brave/browser/extensions/brave_base_local_data_files_browsertest.h" -#include "brave/components/brave_shields/browser/ad_block_component_filters_provider.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" #include "brave/components/brave_shields/browser/test_filters_provider.h" -#include "brave/components/constants/brave_paths.h" #include "brave/components/debounce/browser/debounce_component_installer.h" #include "brave/components/debounce/common/features.h" #include "brave/components/debounce/common/pref_names.h" @@ -25,15 +22,12 @@ #include "chrome/browser/interstitials/security_interstitial_page_test_utils.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/common/content_client.h" #include "content/public/test/browser_test.h" #include "content/public/test/browser_test_utils.h" #include "content/public/test/test_utils.h" #include "net/base/url_util.h" -#include "net/dns/mock_host_resolver.h" -#include "net/test/embedded_test_server/default_handlers.h" #include "third_party/blink/public/common/loader/url_loader_throttle.h" namespace { @@ -181,19 +175,7 @@ class DebounceBrowserTest : public BaseLocalDataFilesBrowserTest { EXPECT_EQ(web_contents()->GetLastCommittedURL(), landing_url); } - bool InstallAdBlockForDebounce() { - base::FilePath test_data_dir; - GetTestDataDir(&test_data_dir); - const extensions::Extension* ad_block_extension = - InstallExtension(test_data_dir.AppendASCII("adblock-data") - .AppendASCII("adblock-default"), - 1); - if (!ad_block_extension) { - return false; - } - g_brave_browser_process->ad_block_service() - ->default_filters_provider() - ->OnComponentReady(ad_block_extension->path()); + bool InitAdBlockForDebounce() { auto source_provider = std::make_unique( "||blocked.com^", "[]"); g_brave_browser_process->ad_block_service()->UseSourceProvidersForTest( @@ -439,7 +421,7 @@ IN_PROC_BROWSER_TEST_F(DebounceBrowserTest, DebounceBeforeDomainBlock) { // Install adblock, turn on aggressive blocking for this URL, then attempt to // navigate to it. This should be interrupted by the domain block // interstitial. - ASSERT_TRUE(InstallAdBlockForDebounce()); + ASSERT_TRUE(InitAdBlockForDebounce()); SetCosmeticFilteringControlType(content_settings(), ControlType::BLOCK, original_url); NavigateTo(original_url); diff --git a/browser/perf/brave_perf_features_processor.cc b/browser/perf/brave_perf_features_processor.cc index 03d5d94adce7..79c67cac9434 100644 --- a/browser/perf/brave_perf_features_processor.cc +++ b/browser/perf/brave_perf_features_processor.cc @@ -13,7 +13,7 @@ #include "brave/components/brave_ads/core/public/prefs/pref_names.h" #include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_rewards/browser/rewards_service.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" #include "chrome/browser/profiles/profile.h" @@ -33,10 +33,10 @@ void EnableAdblockCookieList(base::WeakPtr profile) { } // Obtrusive cookie notices list in cosmetic filters. - auto* regional_service_manager = - g_brave_browser_process->ad_block_service()->regional_service_manager(); - if (!regional_service_manager || - !regional_service_manager->IsFilterListAvailable( + auto* component_service_manager = + g_brave_browser_process->ad_block_service()->component_service_manager(); + if (!component_service_manager || + !component_service_manager->IsFilterListAvailable( brave_shields::kCookieListUuid)) { base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask( FROM_HERE, base::BindOnce(&EnableAdblockCookieList, profile), @@ -44,8 +44,8 @@ void EnableAdblockCookieList(base::WeakPtr profile) { return; } - regional_service_manager->EnableFilterList(brave_shields::kCookieListUuid, - true); + component_service_manager->EnableFilterList(brave_shields::kCookieListUuid, + true); } } // namespace diff --git a/browser/ui/views/brave_shields/cookie_list_opt_in_browsertest.cc b/browser/ui/views/brave_shields/cookie_list_opt_in_browsertest.cc index beb6d10829e8..9cb66968b13c 100644 --- a/browser/ui/views/brave_shields/cookie_list_opt_in_browsertest.cc +++ b/browser/ui/views/brave_shields/cookie_list_opt_in_browsertest.cc @@ -11,7 +11,7 @@ #include "brave/browser/brave_browser_process.h" #include "brave/browser/perf/brave_perf_switches.h" #include "brave/browser/ui/views/brave_shields/cookie_list_opt_in_bubble_host.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/filter_list_catalog_entry.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" @@ -38,13 +38,13 @@ namespace brave_shields { namespace { -auto* GetRegionalServiceManager() { +auto* GetComponentServiceManager() { return g_brave_browser_process->ad_block_service() - ->regional_service_manager(); + ->component_service_manager(); } bool IsCookieListFilterEnabled() { - return GetRegionalServiceManager()->IsFilterListEnabled(kCookieListUuid); + return GetComponentServiceManager()->IsFilterListEnabled(kCookieListUuid); } class CookieListFilterEnabledObserver { @@ -127,10 +127,10 @@ class CookieListOptInBrowserTest : public InProcessBrowserTest { kCookieListUuid, "https://secure.fanboy.co.nz/fanboy-cookiemonster_ubo.txt", "Easylist-Cookie List - Filter Obtrusive Cookie Notices", {}, - "https://forums.lanik.us/", kRegionalAdBlockComponentTestId, - kRegionalAdBlockComponentTest64PublicKey, - "Removes obtrusive cookie law notices", "", "")}; - GetRegionalServiceManager()->SetFilterListCatalog(filter_list_catalog); + "https://forums.lanik.us/", "Removes obtrusive cookie law notices", + false, false, false, 0, kRegionalAdBlockComponentTestId, + kRegionalAdBlockComponentTest64PublicKey, "", "")}; + GetComponentServiceManager()->SetFilterListCatalog(filter_list_catalog); } base::test::ScopedFeatureList scoped_feature_list_; @@ -248,7 +248,7 @@ class CookieListOptInPreEnabledBrowserTest : public CookieListOptInBrowserTest { void SetUpLocalState() override { CookieListOptInBrowserTest::SetUpLocalState(); - GetRegionalServiceManager()->EnableFilterList(kCookieListUuid, true); + GetComponentServiceManager()->EnableFilterList(kCookieListUuid, true); // Since `AdBlockRegionalServiceManager::EnableFilterList` modifies local // state asynchronously in a posted task, waiting for the update to complete diff --git a/browser/ui/views/brave_shields/cookie_list_opt_in_bubble_host.cc b/browser/ui/views/brave_shields/cookie_list_opt_in_bubble_host.cc index d11fd753deb5..8d11107eb090 100644 --- a/browser/ui/views/brave_shields/cookie_list_opt_in_bubble_host.cc +++ b/browser/ui/views/brave_shields/cookie_list_opt_in_bubble_host.cc @@ -11,7 +11,7 @@ #include "base/metrics/histogram_functions.h" #include "brave/browser/brave_browser_process.h" #include "brave/browser/ui/webui/brave_shields/cookie_list_opt_in_ui.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/components/brave_shields/common/features.h" @@ -61,10 +61,10 @@ bool ShouldEventuallyShowBubble() { base::UmaHistogramExactLinear(kCookieListPromptHistogram, 0, 4); - auto* regional_service_manager = - g_brave_browser_process->ad_block_service()->regional_service_manager(); - DCHECK(regional_service_manager); - if (regional_service_manager->IsFilterListEnabled(kCookieListUuid)) { + auto* component_service_manager = + g_brave_browser_process->ad_block_service()->component_service_manager(); + DCHECK(component_service_manager); + if (component_service_manager->IsFilterListEnabled(kCookieListUuid)) { return false; } @@ -146,11 +146,11 @@ void CookieListOptInBubbleHost::ShowBubble() { } // Do not show the bubble if the filter list is not yet available, likely - // because the regional filter list has not yet been donwloaded. - auto* regional_service_manager = - g_brave_browser_process->ad_block_service()->regional_service_manager(); - DCHECK(regional_service_manager); - if (!regional_service_manager->IsFilterListAvailable(kCookieListUuid)) { + // because the filter list component has not yet been donwloaded. + auto* component_service_manager = + g_brave_browser_process->ad_block_service()->component_service_manager(); + DCHECK(component_service_manager); + if (!component_service_manager->IsFilterListAvailable(kCookieListUuid)) { return; } diff --git a/browser/ui/webui/brave_adblock_ui.cc b/browser/ui/webui/brave_adblock_ui.cc index 43e1f3788e8a..9926f719b3a2 100644 --- a/browser/ui/webui/brave_adblock_ui.cc +++ b/browser/ui/webui/brave_adblock_ui.cc @@ -13,8 +13,8 @@ #include "brave/browser/brave_browser_process.h" #include "brave/browser/ui/webui/brave_webui_source.h" #include "brave/components/brave_adblock/resources/grit/brave_adblock_generated_map.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_provider.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/ad_block_service_helper.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h" @@ -144,7 +144,7 @@ void AdblockDOMHandler::HandleEnableFilterList(const base::Value::List& args) { std::string uuid = args[0].GetString(); bool enabled = args[1].GetBool(); g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->EnableFilterList(uuid, enabled); } @@ -162,7 +162,7 @@ void AdblockDOMHandler::HandleGetRegionalLists(const base::Value::List& args) { DCHECK_EQ(args.size(), 0U); AllowJavascript(); auto regional_lists = g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->GetRegionalLists(); CallJavascriptFunction("brave_adblock.onGetRegionalLists", regional_lists); } diff --git a/browser/ui/webui/brave_shields/cookie_list_opt_in_page_handler.cc b/browser/ui/webui/brave_shields/cookie_list_opt_in_page_handler.cc index f1a95d870aab..feb3cf5e7f35 100644 --- a/browser/ui/webui/brave_shields/cookie_list_opt_in_page_handler.cc +++ b/browser/ui/webui/brave_shields/cookie_list_opt_in_page_handler.cc @@ -9,7 +9,7 @@ #include "base/metrics/histogram_functions.h" #include "brave/browser/brave_browser_process.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" @@ -43,7 +43,7 @@ void CookieListOptInPageHandler::CloseUI() { void CookieListOptInPageHandler::EnableFilter() { g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->EnableFilterList(brave_shields::kCookieListUuid, true); } diff --git a/browser/ui/webui/settings/brave_adblock_handler.cc b/browser/ui/webui/settings/brave_adblock_handler.cc index 339991706f50..51878f5bfd97 100644 --- a/browser/ui/webui/settings/brave_adblock_handler.cc +++ b/browser/ui/webui/settings/brave_adblock_handler.cc @@ -14,8 +14,8 @@ #include "base/values.h" #include "brave/browser/brave_browser_process.h" #include "brave/components/brave_adblock/resources/grit/brave_adblock_generated_map.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_provider.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" @@ -103,7 +103,7 @@ void BraveAdBlockHandler::OnServiceUpdateEvent() { void BraveAdBlockHandler::GetRegionalLists(const base::Value::List& args) { AllowJavascript(); auto regional_lists = g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->GetRegionalLists(); ResolveJavascriptCallback(args[0], regional_lists); @@ -119,7 +119,7 @@ void BraveAdBlockHandler::EnableFilterList(const base::Value::List& args) { bool enabled = args[1].GetBool(); g_brave_browser_process->ad_block_service() - ->regional_service_manager() + ->component_service_manager() ->EnableFilterList(uuid, enabled); } diff --git a/components/brave_shields/adblock/rs/BUILD.gn b/components/brave_shields/adblock/rs/BUILD.gn index 5e6a290cede7..bdf5b381707f 100644 --- a/components/brave_shields/adblock/rs/BUILD.gn +++ b/components/brave_shields/adblock/rs/BUILD.gn @@ -13,6 +13,7 @@ rust_static_library("rust_lib") { sources = [ "src/convert.rs", "src/engine.rs", + "src/filter_set.rs", "src/lib.rs", "src/result.rs", ] diff --git a/components/brave_shields/adblock/rs/src/engine.rs b/components/brave_shields/adblock/rs/src/engine.rs index feffb5e187d3..a74da5e1fb5d 100644 --- a/components/brave_shields/adblock/rs/src/engine.rs +++ b/components/brave_shields/adblock/rs/src/engine.rs @@ -6,17 +6,17 @@ use std::collections::HashSet; use std::str::Utf8Error; -use adblock::Engine as InnerEngine; -use adblock::lists::FilterSet; +use adblock::lists::FilterSet as InnerFilterSet; use adblock::resources::{MimeType, Resource, ResourceType}; use adblock::url_parser::ResolvesDomain; +use adblock::Engine as InnerEngine; use cxx::{let_cxx_string, CxxString, CxxVector}; use crate::ffi::{ resolve_domain_position, BlockerResult, BoxEngineResult, ContentBlockingRulesResult, - FilterListMetadata, RegexDebugInfo, RegexManagerDiscardPolicy, UnitResult, - VecStringResult, + FilterListMetadata, RegexDebugInfo, RegexManagerDiscardPolicy, UnitResult, VecStringResult, }; +use crate::filter_set::FilterSet; use crate::result::InternalError; #[cfg(feature = "ios")] @@ -38,7 +38,7 @@ pub fn new_engine() -> Box { pub fn engine_with_rules(rules: &CxxVector) -> BoxEngineResult { || -> Result, InternalError> { - let mut filter_set = FilterSet::new(false); + let mut filter_set = InnerFilterSet::new(false); filter_set.add_filter_list(std::str::from_utf8(rules.as_slice())?, Default::default()); let engine = InnerEngine::from_filter_set(filter_set, true); Ok(Box::new(Engine { engine })) @@ -46,6 +46,15 @@ pub fn engine_with_rules(rules: &CxxVector) -> BoxEngineResult { .into() } +/// Creates a new engine with rules from a given filter set. +pub fn engine_from_filter_set(filter_set: Box) -> BoxEngineResult { + || -> Result, InternalError> { + let engine = InnerEngine::from_filter_set(filter_set.0, true); + Ok(Box::new(Engine { engine })) + }() + .into() +} + struct DomainResolver; impl ResolvesDomain for DomainResolver { @@ -75,7 +84,7 @@ pub fn convert_rules_to_content_blocking(rules: &CxxString) -> ContentBlockingRu /// https://github.com/WebKit/WebKit/blob/4a2df13be2253f64d8da58b794d74347a3742652/Source/WebCore/contentextensions/ContentExtensionParser.cpp#L299 const MAX_CB_LIST_SIZE: usize = 150000; - let mut filter_set = FilterSet::new(true); + let mut filter_set = InnerFilterSet::new(true); filter_set.add_filter_list( rules.to_str()?, ParseOptions { rule_types: RuleTypes::NetworkOnly, ..Default::default() }, diff --git a/components/brave_shields/adblock/rs/src/filter_set.rs b/components/brave_shields/adblock/rs/src/filter_set.rs new file mode 100644 index 000000000000..0310396ae710 --- /dev/null +++ b/components/brave_shields/adblock/rs/src/filter_set.rs @@ -0,0 +1,48 @@ +/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */ + +use adblock::{lists::ParseOptions, resources::PermissionMask, FilterSet as InnerFilterSet}; +use cxx::CxxVector; + +use crate::ffi::{FilterListMetadata, FilterListMetadataResult}; +use crate::result::InternalError; + +pub struct FilterSet(pub(crate) InnerFilterSet); + +impl Default for Box { + fn default() -> Self { + new_filter_set() + } +} + +pub fn new_filter_set() -> Box { + Box::new(FilterSet(InnerFilterSet::new(false))) +} + +impl FilterSet { + pub fn add_filter_list(&mut self, rules: &CxxVector) -> FilterListMetadataResult { + self.add_filter_list_with_permissions(rules, 0) + } + + pub fn add_filter_list_with_permissions( + &mut self, + rules: &CxxVector, + permission_mask: u8, + ) -> FilterListMetadataResult { + || -> Result { + Ok(self + .0 + .add_filter_list( + std::str::from_utf8(rules.as_slice())?, + ParseOptions { + permissions: PermissionMask::from_bits(permission_mask), + ..Default::default() + }, + ) + .into()) + }() + .into() + } +} diff --git a/components/brave_shields/adblock/rs/src/lib.rs b/components/brave_shields/adblock/rs/src/lib.rs index f1d6d3609422..77cac6778a72 100644 --- a/components/brave_shields/adblock/rs/src/lib.rs +++ b/components/brave_shields/adblock/rs/src/lib.rs @@ -8,19 +8,33 @@ mod convert; mod engine; +mod filter_set; mod result; use engine::*; +use filter_set::*; #[allow(unsafe_op_in_unsafe_fn)] #[cxx::bridge(namespace = adblock)] mod ffi { + extern "Rust" { + type FilterSet; + fn new_filter_set() -> Box; + fn add_filter_list(&mut self, rules: &CxxVector) -> FilterListMetadataResult; + fn add_filter_list_with_permissions( + &mut self, + rules: &CxxVector, + permission_mask: u8, + ) -> FilterListMetadataResult; + } extern "Rust" { type Engine; /// Creates a new engine with no rules. fn new_engine() -> Box; /// Creates a new engine with rules from a given filter list. fn engine_with_rules(rules: &CxxVector) -> BoxEngineResult; + /// Creates a new engine with rules from a given filter set. + fn engine_from_filter_set(filter_set: Box) -> BoxEngineResult; /// Configures the adblock domain resolver to the call /// `resolve_domain_position` implemented in adblock_domain_resolver.cc. @@ -187,6 +201,12 @@ mod ffi { error_message: String, } + struct FilterListMetadataResult { + value: FilterListMetadata, + result_kind: ResultKind, + error_message: String, + } + // Created custom Option struct because automatic conversion of Option // is not yet supported in cxx. #[derive(Default)] diff --git a/components/brave_shields/adblock/rs/src/result.rs b/components/brave_shields/adblock/rs/src/result.rs index a1e4addbd9b4..4e96179087fc 100644 --- a/components/brave_shields/adblock/rs/src/result.rs +++ b/components/brave_shields/adblock/rs/src/result.rs @@ -83,6 +83,7 @@ where impl_result_from_trait!(VecStringResult, Vec); impl_result_from_trait!(BoxEngineResult, Box); +impl_result_from_trait!(FilterListMetadataResult, FilterListMetadata); #[cfg(feature = "ios")] impl_result_from_trait!(ContentBlockingRulesResult, ContentBlockingRules); diff --git a/components/brave_shields/browser/BUILD.gn b/components/brave_shields/browser/BUILD.gn index 5942f4279e23..ac40d3fd61f5 100644 --- a/components/brave_shields/browser/BUILD.gn +++ b/components/brave_shields/browser/BUILD.gn @@ -8,6 +8,8 @@ if (!is_ios) { sources = [ "ad_block_component_filters_provider.cc", "ad_block_component_filters_provider.h", + "ad_block_component_service_manager.cc", + "ad_block_component_service_manager.h", "ad_block_custom_filters_provider.cc", "ad_block_custom_filters_provider.h", "ad_block_default_resource_provider.cc", @@ -24,8 +26,6 @@ if (!is_ios) { "ad_block_localhost_filters_provider.h", "ad_block_pref_service.cc", "ad_block_pref_service.h", - "ad_block_regional_service_manager.cc", - "ad_block_regional_service_manager.h", "ad_block_resource_provider.cc", "ad_block_resource_provider.h", "ad_block_service.cc", diff --git a/components/brave_shields/browser/ad_block_component_filters_provider.cc b/components/brave_shields/browser/ad_block_component_filters_provider.cc index 9bd489ddf2ee..7aa7b5f33a07 100644 --- a/components/brave_shields/browser/ad_block_component_filters_provider.cc +++ b/components/brave_shields/browser/ad_block_component_filters_provider.cc @@ -21,14 +21,29 @@ constexpr char kListFile[] = "list.txt"; namespace brave_shields { +namespace { + +// static +void AddDATBufferToFilterSet(base::OnceCallback cb, + rust::Box* filter_set, + uint8_t permission_mask, + DATFileDataBuffer buffer) { + (*filter_set)->add_filter_list_with_permissions(buffer, permission_mask); + std::move(cb).Run(); +} + +} // namespace + AdBlockComponentFiltersProvider::AdBlockComponentFiltersProvider( component_updater::ComponentUpdateService* cus, std::string component_id, std::string base64_public_key, std::string title, + uint8_t permission_mask, bool is_default_engine) : AdBlockFiltersProvider(is_default_engine), component_id_(component_id), + permission_mask_(permission_mask), component_updater_service_(cus) { // Can be nullptr in unit tests if (cus) { @@ -51,6 +66,7 @@ AdBlockComponentFiltersProvider::AdBlockComponentFiltersProvider( catalog_entry.component_id, catalog_entry.base64_public_key, catalog_entry.title, + catalog_entry.permission_mask, is_default_engine) {} AdBlockComponentFiltersProvider::~AdBlockComponentFiltersProvider() {} @@ -69,13 +85,13 @@ void AdBlockComponentFiltersProvider::OnComponentReady( NotifyObservers(); } -void AdBlockComponentFiltersProvider::LoadDATBuffer( - base::OnceCallback - cb) { +void AdBlockComponentFiltersProvider::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback cb) { if (component_path_.empty()) { - // If the path is not ready yet, run the callback with an empty list. An + // If the path is not ready yet, run the callback with no changes. An // update will be pushed later to notify about the newly available list. - std::move(cb).Run(false, DATFileDataBuffer()); + std::move(cb).Run(); return; } @@ -84,7 +100,8 @@ void AdBlockComponentFiltersProvider::LoadDATBuffer( base::ThreadPool::PostTaskAndReplyWithResult( FROM_HERE, {base::MayBlock()}, base::BindOnce(&brave_component_updater::ReadDATFileData, list_file_path), - base::BindOnce(std::move(cb), false)); + base::BindOnce(&AddDATBufferToFilterSet, std::move(cb), filter_set, + permission_mask_)); } } // namespace brave_shields diff --git a/components/brave_shields/browser/ad_block_component_filters_provider.h b/components/brave_shields/browser/ad_block_component_filters_provider.h index 0b369351aa40..2e09006c1f68 100644 --- a/components/brave_shields/browser/ad_block_component_filters_provider.h +++ b/components/brave_shields/browser/ad_block_component_filters_provider.h @@ -38,6 +38,7 @@ class AdBlockComponentFiltersProvider : public AdBlockFiltersProvider { std::string component_id, std::string base64_public_key, std::string title, + uint8_t permission_mask, bool is_default_engine = true); // Helper to build a particular adblock component from a catalog entry AdBlockComponentFiltersProvider( @@ -50,9 +51,8 @@ class AdBlockComponentFiltersProvider : public AdBlockFiltersProvider { AdBlockComponentFiltersProvider& operator=( const AdBlockComponentFiltersProvider&) = delete; - void LoadDATBuffer( - base::OnceCallback) override; + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; // Remove the component. This will force it to be redownloaded next time it // is registered. @@ -68,6 +68,7 @@ class AdBlockComponentFiltersProvider : public AdBlockFiltersProvider { base::FilePath component_path_; std::string component_id_; + uint8_t permission_mask_; const raw_ptr component_updater_service_; diff --git a/components/brave_shields/browser/ad_block_regional_service_manager.cc b/components/brave_shields/browser/ad_block_component_service_manager.cc similarity index 68% rename from components/brave_shields/browser/ad_block_regional_service_manager.cc rename to components/brave_shields/browser/ad_block_component_service_manager.cc index 48d4c7894159..e873461c6f89 100644 --- a/components/brave_shields/browser/ad_block_regional_service_manager.cc +++ b/components/brave_shields/browser/ad_block_component_service_manager.cc @@ -1,9 +1,9 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2023 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/. */ + * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include #include @@ -31,19 +31,16 @@ namespace { typedef struct ListDefaultOverrideConstants { const raw_ref feature; - const char* local_override_pref; const char* list_uuid; } ListDefaultOverrideConstants; const ListDefaultOverrideConstants kCookieListConstants{ .feature = raw_ref(kBraveAdblockCookieListDefault), - .local_override_pref = prefs::kAdBlockCookieListSettingTouched, .list_uuid = kCookieListUuid}; const ListDefaultOverrideConstants kMobileNotificationsListConstants{ .feature = raw_ref( kBraveAdblockMobileNotificationsListDefault), - .local_override_pref = prefs::kAdBlockMobileNotificationsListSettingTouched, .list_uuid = kMobileNotificationsListUuid}; const ListDefaultOverrideConstants kOverrideConstants[2] = { @@ -51,7 +48,7 @@ const ListDefaultOverrideConstants kOverrideConstants[2] = { } // namespace -AdBlockRegionalServiceManager::AdBlockRegionalServiceManager( +AdBlockComponentServiceManager::AdBlockComponentServiceManager( PrefService* local_state, std::string locale, component_updater::ComponentUpdateService* cus, @@ -61,12 +58,12 @@ AdBlockRegionalServiceManager::AdBlockRegionalServiceManager( component_update_service_(cus), catalog_provider_(catalog_provider) { catalog_provider_->LoadFilterListCatalog( - base::BindOnce(&AdBlockRegionalServiceManager::OnFilterListCatalogLoaded, + base::BindOnce(&AdBlockComponentServiceManager::OnFilterListCatalogLoaded, weak_factory_.GetWeakPtr())); catalog_provider_->AddObserver(this); } -AdBlockRegionalServiceManager::~AdBlockRegionalServiceManager() { +AdBlockComponentServiceManager::~AdBlockComponentServiceManager() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); catalog_provider_->RemoveObserver(this); } @@ -75,7 +72,7 @@ AdBlockRegionalServiceManager::~AdBlockRegionalServiceManager() { // might have been enabled. If so, make sure the user hasn't explicitly // modified any of these locale-specific list settings, to determine if all // should be enabled. -bool AdBlockRegionalServiceManager::NeedsLocaleListsMigration( +bool AdBlockComponentServiceManager::NeedsLocaleListsMigration( std::vector> locale_lists) { // This can only apply to locales with more than one available list @@ -96,10 +93,11 @@ bool AdBlockRegionalServiceManager::NeedsLocaleListsMigration( return true; } -void AdBlockRegionalServiceManager::StartRegionalServices() { +void AdBlockComponentServiceManager::StartRegionalServices() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (!local_state_) + if (!local_state_) { return; + } if (filter_list_catalog_.size() == 0) { return; @@ -125,77 +123,48 @@ void AdBlockRegionalServiceManager::StartRegionalServices() { local_state_->SetBoolean(prefs::kAdBlockCheckedAllDefaultRegions, true); } - // Start all regional services associated with enabled filter lists - const auto& regional_filters_dict = - local_state_->GetDict(prefs::kAdBlockRegionalFilters); - - auto regional_filters_dict_with_overrides = regional_filters_dict.Clone(); - for (const auto& constants : kOverrideConstants) { - const bool list_touched = - local_state_->GetBoolean(constants.local_override_pref); - - if (base::FeatureList::IsEnabled(*constants.feature) && !list_touched) { - base::Value::Dict list_entry; - list_entry.Set("enabled", true); - regional_filters_dict_with_overrides.Set(constants.list_uuid, - std::move(list_entry)); - } - } - - for (const auto kv : regional_filters_dict_with_overrides) { - const std::string uuid = kv.first; - bool enabled = false; - const auto* regional_filter_dict = - regional_filters_dict_with_overrides.FindDict(uuid); - if (regional_filter_dict) { - enabled = regional_filter_dict->FindBool("enabled").value_or(false); - } - if (enabled) { - auto catalog_entry = brave_shields::FindAdBlockFilterListByUUID( - filter_list_catalog_, uuid); - auto existing_provider = regional_filters_providers_.find(uuid); - // Iterating through locally enabled lists - don't disable any providers - // or update existing providers with a potentially new catalog entry. + // Start component services associated with enabled filter lists + for (const auto& catalog_entry : filter_list_catalog_) { + if (IsFilterListEnabled(catalog_entry.uuid)) { + auto existing_provider = + regional_filters_providers_.find(catalog_entry.uuid); + // Only check for new lists that are part of the catalog - don't touch any + // existing providers to account for modified or removed catalog entries. // They'll be handled after a browser restart. - if (catalog_entry != filter_list_catalog_.end() && - existing_provider == regional_filters_providers_.end()) { + if (existing_provider == regional_filters_providers_.end()) { auto regional_filters_provider = std::make_unique( - component_update_service_, *catalog_entry, false); + component_update_service_, catalog_entry, + catalog_entry.first_party_protections); regional_filters_providers_.insert( - {uuid, std::move(regional_filters_provider)}); + {catalog_entry.uuid, std::move(regional_filters_provider)}); } } } } -void AdBlockRegionalServiceManager::UpdateFilterListPrefs( +void AdBlockComponentServiceManager::UpdateFilterListPrefs( const std::string& uuid, bool enabled) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (!local_state_) + if (!local_state_) { return; + } ScopedDictPrefUpdate update(local_state_, prefs::kAdBlockRegionalFilters); base::Value::Dict regional_filter_dict; regional_filter_dict.Set("enabled", enabled); update->Set(uuid, std::move(regional_filter_dict)); - for (const auto& constants : kOverrideConstants) { - if (uuid == constants.list_uuid) { - local_state_->SetBoolean(constants.local_override_pref, true); - } - } - RecordP3ACookieListEnabled(); } -void AdBlockRegionalServiceManager::RecordP3ACookieListEnabled() { +void AdBlockComponentServiceManager::RecordP3ACookieListEnabled() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); UMA_HISTOGRAM_BOOLEAN(kCookieListEnabledHistogram, IsFilterListEnabled(kCookieListUuid)); } -bool AdBlockRegionalServiceManager::IsFilterListAvailable( +bool AdBlockComponentServiceManager::IsFilterListAvailable( const std::string& uuid) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(!uuid.empty()); @@ -204,32 +173,49 @@ bool AdBlockRegionalServiceManager::IsFilterListAvailable( return catalog_entry != filter_list_catalog_.end(); } -bool AdBlockRegionalServiceManager::IsFilterListEnabled( +bool AdBlockComponentServiceManager::IsFilterListEnabled( const std::string& uuid) const { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(!uuid.empty()); DCHECK(local_state_); + // Retrieve user's setting for the list from preferences + const auto& regional_filters_dict = + local_state_->GetDict(prefs::kAdBlockRegionalFilters); + const auto* user_pref_dict = regional_filters_dict.FindDict(uuid); + const bool list_touched = user_pref_dict; + + // Apply feature overrides from Griffin without overriding user preference for (const auto& constants : kOverrideConstants) { if (uuid == constants.list_uuid && - base::FeatureList::IsEnabled(*constants.feature) && - !local_state_->GetBoolean(constants.local_override_pref)) { + base::FeatureList::IsEnabled(*constants.feature) && !list_touched) { return true; } } - const auto& regional_filters_dict = - local_state_->GetDict(prefs::kAdBlockRegionalFilters); + // Apply overrides for lists with a `default_enabled` designation in the + // catalog + auto catalog_entry = + brave_shields::FindAdBlockFilterListByUUID(filter_list_catalog_, uuid); + if (catalog_entry != filter_list_catalog_.end() && + catalog_entry->default_enabled) { + // prefer any user setting for the list, unless it's hidden + if (!list_touched || catalog_entry->hidden) { + return true; + } + } - if (const auto* regional_filter_dict = regional_filters_dict.FindDict(uuid)) { - return regional_filter_dict->FindBool("enabled").value_or(false); + // Use the user's preference if there was no valid override + if (user_pref_dict) { + return user_pref_dict->FindBool("enabled").value_or(false); } + // Otherwise the list is disabled return false; } -void AdBlockRegionalServiceManager::EnableFilterList(const std::string& uuid, - bool enabled) { +void AdBlockComponentServiceManager::EnableFilterList(const std::string& uuid, + bool enabled) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(!uuid.empty()); auto catalog_entry = @@ -246,7 +232,8 @@ void AdBlockRegionalServiceManager::EnableFilterList(const std::string& uuid, } auto regional_filters_provider = std::make_unique( - component_update_service_, *catalog_entry, false); + component_update_service_, *catalog_entry, + catalog_entry->first_party_protections); regional_filters_providers_.insert( {uuid, std::move(regional_filters_provider)}); } else { @@ -262,7 +249,7 @@ void AdBlockRegionalServiceManager::EnableFilterList(const std::string& uuid, UpdateFilterListPrefs(uuid, enabled); } -void AdBlockRegionalServiceManager::SetFilterListCatalog( +void AdBlockComponentServiceManager::SetFilterListCatalog( std::vector catalog) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); filter_list_catalog_ = std::move(catalog); @@ -271,17 +258,20 @@ void AdBlockRegionalServiceManager::SetFilterListCatalog( } const std::vector& -AdBlockRegionalServiceManager::GetFilterListCatalog() { +AdBlockComponentServiceManager::GetFilterListCatalog() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); return filter_list_catalog_; } -base::Value::List AdBlockRegionalServiceManager::GetRegionalLists() { +base::Value::List AdBlockComponentServiceManager::GetRegionalLists() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK(local_state_); base::Value::List list; for (const auto& region_list : filter_list_catalog_) { + if (region_list.hidden) { + continue; + } // Most settings come directly from the regional catalog from // https://github.com/brave/adblock-resources base::Value::Dict dict; @@ -301,7 +291,7 @@ base::Value::List AdBlockRegionalServiceManager::GetRegionalLists() { return list; } -void AdBlockRegionalServiceManager::OnFilterListCatalogLoaded( +void AdBlockComponentServiceManager::OnFilterListCatalogLoaded( const std::string& catalog_json) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); SetFilterListCatalog(FilterListCatalogFromJSON(catalog_json)); diff --git a/components/brave_shields/browser/ad_block_regional_service_manager.h b/components/brave_shields/browser/ad_block_component_service_manager.h similarity index 74% rename from components/brave_shields/browser/ad_block_regional_service_manager.h rename to components/brave_shields/browser/ad_block_component_service_manager.h index c0d9df1ad25d..61121d945c26 100644 --- a/components/brave_shields/browser/ad_block_regional_service_manager.h +++ b/components/brave_shields/browser/ad_block_component_service_manager.h @@ -1,10 +1,10 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2023 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/. */ + * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_REGIONAL_SERVICE_MANAGER_H_ -#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_REGIONAL_SERVICE_MANAGER_H_ +#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_COMPONENT_SERVICE_MANAGER_H_ +#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_COMPONENT_SERVICE_MANAGER_H_ #include #include @@ -25,20 +25,21 @@ namespace brave_shields { class FilterListCatalogEntry; -// The AdBlock regional service manager, in charge of initializing and -// managing regional AdBlock clients. -class AdBlockRegionalServiceManager +// The adblock component service manager, in charge of initializing and +// managing adblock lists served via CRX components. +class AdBlockComponentServiceManager : public AdBlockFilterListCatalogProvider::Observer { public: - explicit AdBlockRegionalServiceManager( + explicit AdBlockComponentServiceManager( PrefService* local_state, std::string locale, component_updater::ComponentUpdateService* cus, AdBlockFilterListCatalogProvider* catalog_provider); - AdBlockRegionalServiceManager(const AdBlockRegionalServiceManager&) = delete; - AdBlockRegionalServiceManager& operator=( - const AdBlockRegionalServiceManager&) = delete; - ~AdBlockRegionalServiceManager() override; + AdBlockComponentServiceManager(const AdBlockComponentServiceManager&) = + delete; + AdBlockComponentServiceManager& operator=( + const AdBlockComponentServiceManager&) = delete; + ~AdBlockComponentServiceManager() override; base::Value::List GetRegionalLists(); @@ -85,9 +86,9 @@ class AdBlockRegionalServiceManager SEQUENCE_CHECKER(sequence_checker_); - base::WeakPtrFactory weak_factory_{this}; + base::WeakPtrFactory weak_factory_{this}; }; } // namespace brave_shields -#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_REGIONAL_SERVICE_MANAGER_H_ +#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_COMPONENT_SERVICE_MANAGER_H_ diff --git a/components/brave_shields/browser/ad_block_component_service_unittest.cc b/components/brave_shields/browser/ad_block_component_service_unittest.cc new file mode 100644 index 000000000000..091dd973e9fc --- /dev/null +++ b/components/brave_shields/browser/ad_block_component_service_unittest.cc @@ -0,0 +1,66 @@ +/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_shields/browser/filter_list_catalog_entry.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(AdBlockComponentServiceTest, UserModelLanguages) { + std::vector catalog; + catalog.push_back(brave_shields::FilterListCatalogEntry( + "uuid", "https://brave.com", "Testing Filter List #1", {"fr"}, + "https://support.brave.com", "Filter list for testing purposes", false, + false, false, 0, "componentid", "base64publickey", "", "")); + catalog.push_back(brave_shields::FilterListCatalogEntry( + "uuid", "https://brave.com", "Testing Filter List #2", {"en"}, + "https://support.brave.com", "Filter list for testing purposes", false, + false, false, 0, "componentid", "base64publickey", "", "")); + catalog.push_back(brave_shields::FilterListCatalogEntry( + "uuid", "https://brave.com", "Testing Filter List #2", {"fr"}, + "https://support.brave.com", "Filter list for testing purposes", false, + false, false, 0, "componentid", "base64publickey", "", "")); + + std::vector languages({"fr", "fR", "fr-FR", "fr-ca"}); + std::for_each( + languages.begin(), languages.end(), [&](const std::string& language) { + EXPECT_EQ( + brave_shields::FindAdBlockFilterListsByLocale(catalog, language) + .size(), + 2UL); + }); + + EXPECT_EQ(brave_shields::FindAdBlockFilterListsByLocale(catalog, "en").size(), + 1UL); + + EXPECT_EQ(brave_shields::FindAdBlockFilterListsByLocale(catalog, "is").size(), + 0UL); +} + +TEST(AdBlockComponentServiceTest, MissingFieldDefaultValues) { + auto catalog = brave_shields::FilterListCatalogFromJSON(R"([{ + "uuid": "uuid", + "title": "Test list", + "desc": "Just used for testing", + "langs": [], + "component_id": "componentid", + "base64_public_key": "base64publickey", + "list_text_component": { + "component_id": "componentid", + "base64_public_key": "base64publickey" + }, + "sources": [ + { + "url": "https://example.com", + "format": "Standard", + "support_url": "https://support.example.com" + } + ] + }])"); + + ASSERT_EQ(catalog.size(), 1UL); + ASSERT_EQ(catalog[0].hidden, false); + ASSERT_EQ(catalog[0].default_enabled, false); + ASSERT_EQ(catalog[0].first_party_protections, false); + ASSERT_EQ(catalog[0].permission_mask, 0); +} diff --git a/components/brave_shields/browser/ad_block_custom_filters_provider.cc b/components/brave_shields/browser/ad_block_custom_filters_provider.cc index 51e35dafa0fa..9bcdc69cd4e5 100644 --- a/components/brave_shields/browser/ad_block_custom_filters_provider.cc +++ b/components/brave_shields/browser/ad_block_custom_filters_provider.cc @@ -15,6 +15,14 @@ namespace brave_shields { +namespace { + +// Custom filters get all permissions granted, i.e. all bits of the mask set, +// i.e. the maximum possible uint8_t. +const uint8_t kCustomFiltersPermissionLevel = UINT8_MAX; + +} // namespace + AdBlockCustomFiltersProvider::AdBlockCustomFiltersProvider( PrefService* local_state) : AdBlockFiltersProvider(false), local_state_(local_state) {} @@ -59,18 +67,20 @@ bool AdBlockCustomFiltersProvider::UpdateCustomFilters( return true; } -void AdBlockCustomFiltersProvider::LoadDATBuffer( - base::OnceCallback - cb) { +void AdBlockCustomFiltersProvider::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback cb) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); auto custom_filters = GetCustomFilters(); auto buffer = std::vector(custom_filters.begin(), custom_filters.end()); + (*filter_set) + ->add_filter_list_with_permissions(buffer, kCustomFiltersPermissionLevel); // PostTask so this has an async return to match other loaders base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( - FROM_HERE, base::BindOnce(std::move(cb), false, std::move(buffer))); + FROM_HERE, base::BindOnce(std::move(cb))); } // The custom filters provider can provide its filters immediately after being diff --git a/components/brave_shields/browser/ad_block_custom_filters_provider.h b/components/brave_shields/browser/ad_block_custom_filters_provider.h index fd229627182e..f1c6fd6c0d2b 100644 --- a/components/brave_shields/browser/ad_block_custom_filters_provider.h +++ b/components/brave_shields/browser/ad_block_custom_filters_provider.h @@ -35,9 +35,8 @@ class AdBlockCustomFiltersProvider : public AdBlockFiltersProvider { std::string GetCustomFilters(); bool UpdateCustomFilters(const std::string& custom_filters); - void LoadDATBuffer( - base::OnceCallback) override; + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; // AdBlockFiltersProvider void AddObserver(AdBlockFiltersProvider::Observer* observer); diff --git a/components/brave_shields/browser/ad_block_engine.cc b/components/brave_shields/browser/ad_block_engine.cc index 5aa3f42b6551..0085948063e2 100644 --- a/components/brave_shields/browser/ad_block_engine.cc +++ b/components/brave_shields/browser/ad_block_engine.cc @@ -250,6 +250,11 @@ void AdBlockEngine::Load(bool deserialize, } } +void AdBlockEngine::Load(rust::Box filter_set, + const std::string& resources_json) { + OnFilterSetLoaded(std::move(filter_set), resources_json); +} + void AdBlockEngine::UpdateAdBlockClient( rust::Box ad_block_client, const std::string& resources_json) { @@ -273,6 +278,17 @@ void AdBlockEngine::AddKnownTagsToAdBlockInstance() { }); } +void AdBlockEngine::OnFilterSetLoaded(rust::Box filter_set, + const std::string& resources_json) { + auto result = adblock::engine_from_filter_set(std::move(filter_set)); + if (result.result_kind != adblock::ResultKind::Success) { + VLOG(0) << "AdBlockEngine::OnFilterSetLoaded failed: " + << result.error_message.c_str(); + return; + } + UpdateAdBlockClient(std::move(result.value), resources_json); +} + void AdBlockEngine::OnListSourceLoaded(const DATFileDataBuffer& filters, const std::string& resources_json) { auto result = adblock::engine_with_rules(filters); diff --git a/components/brave_shields/browser/ad_block_engine.h b/components/brave_shields/browser/ad_block_engine.h index 0e615dbc0a16..c9c5bf0c32f1 100644 --- a/components/brave_shields/browser/ad_block_engine.h +++ b/components/brave_shields/browser/ad_block_engine.h @@ -71,6 +71,8 @@ class AdBlockEngine : public base::SupportsWeakPtr { void Load(bool deserialize, const DATFileDataBuffer& dat_buf, const std::string& resources_json); + void Load(rust::Box filter_set, + const std::string& resources_json); class TestObserver : public base::CheckedObserver { public: @@ -84,9 +86,11 @@ class AdBlockEngine : public base::SupportsWeakPtr { void AddKnownTagsToAdBlockInstance(); void UpdateAdBlockClient(rust::Box ad_block_client, const std::string& resources_json); + + void OnFilterSetLoaded(rust::Box filter_set, + const std::string& resources_json); void OnListSourceLoaded(const DATFileDataBuffer& filters, const std::string& resources_json); - void OnDATLoaded(const DATFileDataBuffer& dat_buf, const std::string& resources_json); diff --git a/components/brave_shields/browser/ad_block_filter_list_catalog_provider.cc b/components/brave_shields/browser/ad_block_filter_list_catalog_provider.cc index 177cba29fd35..e93847b8a4f6 100644 --- a/components/brave_shields/browser/ad_block_filter_list_catalog_provider.cc +++ b/components/brave_shields/browser/ad_block_filter_list_catalog_provider.cc @@ -11,7 +11,7 @@ #include "brave/components/brave_shields/browser/ad_block_component_installer.h" #include "brave/components/brave_shields/browser/ad_block_filter_list_catalog_provider.h" -constexpr char kRegionalCatalogFile[] = "regional_catalog.json"; +constexpr char kListCatalogFile[] = "list_catalog.json"; namespace brave_shields { @@ -53,7 +53,7 @@ void AdBlockFilterListCatalogProvider::OnComponentReady( base::ThreadPool::PostTaskAndReplyWithResult( FROM_HERE, {base::MayBlock()}, base::BindOnce(&brave_component_updater::GetDATFileAsString, - component_path_.AppendASCII(kRegionalCatalogFile)), + component_path_.AppendASCII(kListCatalogFile)), base::BindOnce( &AdBlockFilterListCatalogProvider::OnFilterListCatalogLoaded, weak_factory_.GetWeakPtr())); @@ -70,7 +70,7 @@ void AdBlockFilterListCatalogProvider::LoadFilterListCatalog( base::ThreadPool::PostTaskAndReplyWithResult( FROM_HERE, {base::MayBlock()}, base::BindOnce(&brave_component_updater::GetDATFileAsString, - component_path_.AppendASCII(kRegionalCatalogFile)), + component_path_.AppendASCII(kListCatalogFile)), std::move(cb)); } diff --git a/components/brave_shields/browser/ad_block_filters_provider.cc b/components/brave_shields/browser/ad_block_filters_provider.cc index 4c20c9d840eb..5a50402f975e 100644 --- a/components/brave_shields/browser/ad_block_filters_provider.cc +++ b/components/brave_shields/browser/ad_block_filters_provider.cc @@ -42,12 +42,6 @@ void AdBlockFiltersProvider::NotifyObservers() { } } -void AdBlockFiltersProvider::LoadDAT( - base::OnceCallback - cb) { - LoadDATBuffer(std::move(cb)); -} - base::WeakPtr AdBlockFiltersProvider::AsWeakPtr() { return weak_factory_.GetWeakPtr(); } diff --git a/components/brave_shields/browser/ad_block_filters_provider.h b/components/brave_shields/browser/ad_block_filters_provider.h index 479c4a55776d..94f0cd42df02 100644 --- a/components/brave_shields/browser/ad_block_filters_provider.h +++ b/components/brave_shields/browser/ad_block_filters_provider.h @@ -13,6 +13,7 @@ #include "base/observer_list.h" #include "base/observer_list_types.h" #include "brave/components/brave_component_updater/browser/dat_file_util.h" +#include "brave/components/brave_shields/adblock/rs/src/lib.rs.h" using brave_component_updater::DATFileDataBuffer; @@ -37,8 +38,8 @@ class AdBlockFiltersProvider { void AddObserver(Observer* observer); void RemoveObserver(Observer* observer); - void LoadDAT(base::OnceCallback); + virtual void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) = 0; base::WeakPtr AsWeakPtr(); @@ -47,9 +48,6 @@ class AdBlockFiltersProvider { protected: bool engine_is_default_; - virtual void LoadDATBuffer( - base::OnceCallback) = 0; void NotifyObservers(); private: diff --git a/components/brave_shields/browser/ad_block_filters_provider_manager.cc b/components/brave_shields/browser/ad_block_filters_provider_manager.cc index ce0379431bcd..4b454d1fea32 100644 --- a/components/brave_shields/browser/ad_block_filters_provider_manager.cc +++ b/components/brave_shields/browser/ad_block_filters_provider_manager.cc @@ -9,7 +9,7 @@ #include #include -#include "base/barrier_callback.h" +#include "base/barrier_closure.h" #include "base/location.h" #include "base/logging.h" #include "base/no_destructor.h" @@ -18,22 +18,6 @@ namespace brave_shields { -namespace { - -static void OnDATLoaded( - base::OnceCallback collect_and_merge, - bool deserialize, - const DATFileDataBuffer& dat_buf) { - // This manager should never be used for a provider that returns a serialized - // DAT. The ability should be removed from the FiltersProvider API when - // possible. - CHECK(!deserialize); - - std::move(collect_and_merge).Run(dat_buf); -} - -} // namespace - // static AdBlockFiltersProviderManager* AdBlockFiltersProviderManager::GetInstance() { static base::NoDestructor instance; @@ -67,7 +51,7 @@ void AdBlockFiltersProviderManager::RemoveProvider( } std::string AdBlockFiltersProviderManager::GetNameForDebugging() { - return "AdBlockCustomFiltersProvider"; + return "AdBlockFiltersProviderManager"; } void AdBlockFiltersProviderManager::OnChanged() { @@ -75,47 +59,28 @@ void AdBlockFiltersProviderManager::OnChanged() { } // Use LoadDATBufferForEngine instead, for Filter Provider Manager. -void AdBlockFiltersProviderManager::LoadDATBuffer( - base::OnceCallback - cb) { +void AdBlockFiltersProviderManager::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback) { NOTREACHED(); } -void AdBlockFiltersProviderManager::LoadDATBufferForEngine( +void AdBlockFiltersProviderManager::LoadFilterSetForEngine( bool is_for_default_engine, - base::OnceCallback - cb) { + rust::Box* filter_set, + base::OnceCallback cb) { auto& filters_providers = is_for_default_engine ? default_engine_filters_providers_ : additional_engine_filters_providers_; - const auto collect_and_merge = base::BarrierCallback( - filters_providers.size(), - base::BindOnce(&AdBlockFiltersProviderManager::FinishCombinating, - weak_factory_.GetWeakPtr(), std::move(cb))); + const auto collect_and_merge = + base::BarrierClosure(filters_providers.size(), std::move(cb)); for (auto* const provider : filters_providers) { task_tracker_.PostTask( base::SequencedTaskRunner::GetCurrentDefault().get(), FROM_HERE, - base::BindOnce( - &AdBlockFiltersProvider::LoadDAT, provider->AsWeakPtr(), - base::BindOnce(OnDATLoaded, std::move(collect_and_merge)))); - } -} - -void AdBlockFiltersProviderManager::FinishCombinating( - base::OnceCallback cb, - const std::vector& results) { - DATFileDataBuffer combined_list; - for (const auto& dat_buf : results) { - combined_list.push_back('\n'); - combined_list.insert(combined_list.end(), dat_buf.begin(), dat_buf.end()); - } - if (combined_list.size() == 0) { - // Small workaround for code in - // AdBlockService::SourceProviderObserver::OnResourcesLoaded that encodes a - // state using an entirely empty DAT. - combined_list.push_back('\n'); + base::BindOnce(&AdBlockFiltersProvider::LoadFilterSet, + provider->AsWeakPtr(), filter_set, + std::move(collect_and_merge))); } - std::move(cb).Run(false, combined_list); } } // namespace brave_shields diff --git a/components/brave_shields/browser/ad_block_filters_provider_manager.h b/components/brave_shields/browser/ad_block_filters_provider_manager.h index a74cbffba576..64da33887982 100644 --- a/components/brave_shields/browser/ad_block_filters_provider_manager.h +++ b/components/brave_shields/browser/ad_block_filters_provider_manager.h @@ -13,6 +13,7 @@ #include "base/functional/callback.h" #include "base/task/cancelable_task_tracker.h" #include "brave/components/brave_component_updater/browser/dat_file_util.h" +#include "brave/components/brave_shields/adblock/rs/src/lib.rs.h" #include "brave/components/brave_shields/browser/ad_block_filters_provider.h" using brave_component_updater::DATFileDataBuffer; @@ -41,14 +42,12 @@ class AdBlockFiltersProviderManager : public AdBlockFiltersProvider, static AdBlockFiltersProviderManager* GetInstance(); - void LoadDATBuffer( - base::OnceCallback) override; + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; - void LoadDATBufferForEngine( - bool is_for_default_engine, - base::OnceCallback cb); + void LoadFilterSetForEngine(bool is_for_default_engine, + rust::Box* filter_set, + base::OnceCallback cb); // AdBlockFiltersProvider::Observer void OnChanged() override; @@ -63,10 +62,6 @@ class AdBlockFiltersProviderManager : public AdBlockFiltersProvider, private: friend base::NoDestructor; - void FinishCombinating( - base::OnceCallback cb, - const std::vector& results); - base::flat_set default_engine_filters_providers_; base::flat_set additional_engine_filters_providers_; diff --git a/components/brave_shields/browser/ad_block_localhost_filters_provider.cc b/components/brave_shields/browser/ad_block_localhost_filters_provider.cc index 37b34f130d28..77d833130826 100644 --- a/components/brave_shields/browser/ad_block_localhost_filters_provider.cc +++ b/components/brave_shields/browser/ad_block_localhost_filters_provider.cc @@ -14,6 +14,20 @@ namespace brave_shields { +namespace { + +const char kLocalhostBadfilters[] = R"( +||0.0.0.0^$third-party,domain=~[::]|~[::ffff:0:0],badfilter +||[::]^$third-party,domain=~0.0.0.0|~[::ffff:0:0],badfilter +||[::ffff:0:0]^$third-party,domain=~0.0.0.0|~[::],badfilter +||localhost^$third-party,domain=~127.0.0.1|~[::1]|~[::ffff:7f00:1],badfilter +||127.0.0.1^$third-party,domain=~localhost|~[::1]|~[::ffff:7f00:1],badfilter +||[::1]^$third-party,domain=~localhost|~127.0.0.1|~[::ffff:7f00:1],badfilter +||[::ffff:7f00:1]^$third-party,domain=~localhost|~127.0.0.1|~[::1],badfilter +)"; + +} // namespace + AdBlockLocalhostFiltersProvider::AdBlockLocalhostFiltersProvider() : AdBlockFiltersProvider(true) {} @@ -23,28 +37,18 @@ std::string AdBlockLocalhostFiltersProvider::GetNameForDebugging() { return "AdBlockLocalhostFiltersProvider"; } -void AdBlockLocalhostFiltersProvider::LoadDATBuffer( - base::OnceCallback - cb) { +void AdBlockLocalhostFiltersProvider::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback cb) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - const std::string& badfilters_for_localhost = - R"( - ||0.0.0.0^$third-party,domain=~[::]|~[::ffff:0:0],badfilter - ||[::]^$third-party,domain=~0.0.0.0|~[::ffff:0:0],badfilter - ||[::ffff:0:0]^$third-party,domain=~0.0.0.0|~[::],badfilter - ||localhost^$third-party,domain=~127.0.0.1|~[::1]|~[::ffff:7f00:1],badfilter - ||127.0.0.1^$third-party,domain=~localhost|~[::1]|~[::ffff:7f00:1],badfilter - ||[::1]^$third-party,domain=~localhost|~127.0.0.1|~[::ffff:7f00:1],badfilter - ||[::ffff:7f00:1]^$third-party,domain=~localhost|~127.0.0.1|~[::1],badfilter - )"; - - auto buffer = std::vector(badfilters_for_localhost.begin(), - badfilters_for_localhost.end()); + auto buffer = std::vector(std::begin(kLocalhostBadfilters), + std::end(kLocalhostBadfilters)); + (*filter_set)->add_filter_list(buffer); // PostTask so this has an async return to match other loaders base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( - FROM_HERE, base::BindOnce(std::move(cb), false, std::move(buffer))); + FROM_HERE, base::BindOnce(std::move(cb))); } void AdBlockLocalhostFiltersProvider::AddObserver( diff --git a/components/brave_shields/browser/ad_block_localhost_filters_provider.h b/components/brave_shields/browser/ad_block_localhost_filters_provider.h index 77aaf97bc180..8864b18259a3 100644 --- a/components/brave_shields/browser/ad_block_localhost_filters_provider.h +++ b/components/brave_shields/browser/ad_block_localhost_filters_provider.h @@ -30,9 +30,8 @@ class AdBlockLocalhostFiltersProvider : public AdBlockFiltersProvider { std::string GetLocalhostFilters(); bool UpdateLocalhostFilters(const std::string& localhost_filters); - void LoadDATBuffer( - base::OnceCallback) override; + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; // AdBlockFiltersProvider void AddObserver(AdBlockFiltersProvider::Observer* observer); diff --git a/components/brave_shields/browser/ad_block_regional_service_unittest.cc b/components/brave_shields/browser/ad_block_regional_service_unittest.cc deleted file mode 100644 index e927392d061a..000000000000 --- a/components/brave_shields/browser/ad_block_regional_service_unittest.cc +++ /dev/null @@ -1,38 +0,0 @@ -/* 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 "brave/components/brave_shields/browser/filter_list_catalog_entry.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(AdBlockRegionalServiceTest, UserModelLanguages) { - std::vector catalog; - catalog.push_back(brave_shields::FilterListCatalogEntry( - "uuid", "https://brave.com", "Testing Filter List #1", {"fr"}, - "https://support.brave.com", "componentid", "base64publickey", - "Filter list for testing purposes", "", "")); - catalog.push_back(brave_shields::FilterListCatalogEntry( - "uuid", "https://brave.com", "Testing Filter List #2", {"en"}, - "https://support.brave.com", "componentid", "base64publickey", - "Filter list for testing purposes", "", "")); - catalog.push_back(brave_shields::FilterListCatalogEntry( - "uuid", "https://brave.com", "Testing Filter List #2", {"fr"}, - "https://support.brave.com", "componentid", "base64publickey", - "Filter list for testing purposes", "", "")); - - std::vector languages({ "fr", "fR", "fr-FR", "fr-ca" }); - std::for_each( - languages.begin(), languages.end(), [&](const std::string& language) { - EXPECT_EQ( - brave_shields::FindAdBlockFilterListsByLocale(catalog, language) - .size(), - 2UL); - }); - - EXPECT_EQ(brave_shields::FindAdBlockFilterListsByLocale(catalog, "en").size(), - 1UL); - - EXPECT_EQ(brave_shields::FindAdBlockFilterListsByLocale(catalog, "is").size(), - 0UL); -} diff --git a/components/brave_shields/browser/ad_block_service.cc b/components/brave_shields/browser/ad_block_service.cc index d81bc9640569..b82ff6b36286 100644 --- a/components/brave_shields/browser/ad_block_service.cc +++ b/components/brave_shields/browser/ad_block_service.cc @@ -16,13 +16,13 @@ #include "base/threading/thread_restrictions.h" #include "brave/components/brave_shields/adblock/rs/src/lib.rs.h" #include "brave/components/brave_shields/browser/ad_block_component_filters_provider.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_provider.h" #include "brave/components/brave_shields/browser/ad_block_default_resource_provider.h" #include "brave/components/brave_shields/browser/ad_block_engine.h" #include "brave/components/brave_shields/browser/ad_block_filter_list_catalog_provider.h" #include "brave/components/brave_shields/browser/ad_block_filters_provider_manager.h" #include "brave/components/brave_shields/browser/ad_block_localhost_filters_provider.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service_helper.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h" #include "brave/components/brave_shields/common/features.h" @@ -33,37 +33,6 @@ #include "third_party/abseil-cpp/absl/types/optional.h" #include "url/origin.h" -namespace { - -const char kAdBlockDefaultComponentName[] = "Brave Ad Block Updater"; -const char kAdBlockDefaultComponentId[] = "iodkpdagapdfkphljnddpjlldadblomo"; -const char kAdBlockDefaultComponentBase64PublicKey[] = - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsD/B/MGdz0gh7WkcFARn" - "ZTBX9KAw2fuGeogijoI+fET38IK0L+P/trCT2NshqhRNmrDpLzV2+Dmes6PvkA+O" - "dQkUV6VbChJG+baTfr3Oo5PdE0WxmP9Xh8XD7p85DQrk0jJilKuElxpK7Yq0JhcT" - "Sc3XNHeTwBVqCnHwWZZ+XysYQfjuDQ0MgQpS/s7U04OZ63NIPe/iCQm32stvS/pE" - "ya7KdBZXgRBQ59U6M1n1Ikkp3vfECShbBld6VrrmNrl59yKWlEPepJ9oqUc2Wf2M" - "q+SDNXROG554RnU4BnDJaNETTkDTZ0Pn+rmLmp1qY5Si0yGsfHkrv3FS3vdxVozO" - "PQIDAQAB"; - -const char kAdBlockExceptionComponentName[] = - "Brave Ad Block First Party Filters"; -const char kAdBlockExceptionComponentId[] = "adcocjohghhfpidemphmcmlmhnfgikei"; -const char kAdBlockExceptionComponentBase64PublicKey[] = - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtvmLp4MOseThuH/vFSc7" - "kjr+CDCzR/ieGI8TJZyFQhzA1SKWRl4y0wB+HGkmoq0KPOzKNZq6hxK7jdm/r/nx" - "xOjqutPoUEL+ysxePErMTse2XeWu3psGSTEjPFdQTPEwH8MF2SwXXneOraD0V/GS" - "iCCvlx8yKIXNX7V9ujMo+QoD6hPGslKUZQJAg+OaZ7pAfq5cOuWXNN6jv12UL0eM" - "t6Dhl31yEu4kZWeTkiccHqdlB/KvPiqXTrV+qd3Tjvsk6kmUlexu3/zlOwVDz5H/" - "kPuOGvW7kYaW22NWQ9TH6fjffgVcSgHDbZETDiP8fHd76kyi1SZ5YJ09XHTE+i9i" - "kQIDAQAB"; - -std::string g_ad_block_default_component_id_(kAdBlockDefaultComponentId); -std::string g_ad_block_default_component_base64_public_key_( - kAdBlockDefaultComponentBase64PublicKey); - -} // namespace - namespace brave_shields { AdBlockService::SourceProviderObserver::SourceProviderObserver( @@ -78,17 +47,7 @@ AdBlockService::SourceProviderObserver::SourceProviderObserver( task_runner_(task_runner), is_filter_provider_manager_(is_filter_provider_manager) { filters_provider_->AddObserver(this); - if (is_filter_provider_manager_) { - static_cast(filters_provider_.get()) - ->LoadDATBufferForEngine( - adblock_engine_->IsDefaultEngine(), - base::BindOnce(&AdBlockService::SourceProviderObserver::OnDATLoaded, - weak_factory_.GetWeakPtr())); - } else { - filters_provider_->LoadDAT( - base::BindOnce(&AdBlockService::SourceProviderObserver::OnDATLoaded, - weak_factory_.GetWeakPtr())); - } + OnChanged(); } AdBlockService::SourceProviderObserver::~SourceProviderObserver() { @@ -97,24 +56,24 @@ AdBlockService::SourceProviderObserver::~SourceProviderObserver() { } void AdBlockService::SourceProviderObserver::OnChanged() { + auto filter_set = std::make_unique>( + adblock::new_filter_set()); + rust::Box* filter_set_ptr = filter_set.get(); + auto on_loaded_cb = + base::BindOnce(&AdBlockService::SourceProviderObserver::OnFilterSetLoaded, + weak_factory_.GetWeakPtr(), std::move(filter_set)); if (is_filter_provider_manager_) { static_cast(filters_provider_.get()) - ->LoadDATBufferForEngine( - adblock_engine_->IsDefaultEngine(), - base::BindOnce(&AdBlockService::SourceProviderObserver::OnDATLoaded, - weak_factory_.GetWeakPtr())); + ->LoadFilterSetForEngine(adblock_engine_->IsDefaultEngine(), + filter_set_ptr, std::move(on_loaded_cb)); } else { - filters_provider_->LoadDAT( - base::BindOnce(&AdBlockService::SourceProviderObserver::OnDATLoaded, - weak_factory_.GetWeakPtr())); + filters_provider_->LoadFilterSet(filter_set_ptr, std::move(on_loaded_cb)); } } -void AdBlockService::SourceProviderObserver::OnDATLoaded( - bool deserialize, - const DATFileDataBuffer& dat_buf) { - deserialize_ = deserialize; - dat_buf_ = std::move(dat_buf); +void AdBlockService::SourceProviderObserver::OnFilterSetLoaded( + std::unique_ptr> filter_set) { + filter_set_ = std::move(filter_set); // multiple AddObserver calls are ignored resource_provider_->AddObserver(this); resource_provider_->LoadResources(base::BindOnce( @@ -123,21 +82,21 @@ void AdBlockService::SourceProviderObserver::OnDATLoaded( void AdBlockService::SourceProviderObserver::OnResourcesLoaded( const std::string& resources_json) { - if (dat_buf_.empty()) { + if (!filter_set_) { task_runner_->PostTask( FROM_HERE, base::BindOnce(&AdBlockEngine::UseResources, adblock_engine_->AsWeakPtr(), resources_json)); } else { auto engine_load_callback = base::BindOnce( - [](base::WeakPtr engine, bool deserialize, - DATFileDataBuffer dat_buf, const std::string& resources_json) { + [](base::WeakPtr engine, + std::unique_ptr> filter_set, + const std::string& resources_json) { if (engine) { - engine->Load(deserialize, std::move(dat_buf), resources_json); + engine->Load(std::move(*filter_set.get()), resources_json); } }, - adblock_engine_->AsWeakPtr(), deserialize_, std::move(dat_buf_), - resources_json); + adblock_engine_->AsWeakPtr(), std::move(filter_set_), resources_json); task_runner_->PostTask(FROM_HERE, std::move(engine_load_callback)); } } @@ -264,9 +223,9 @@ base::Value::Dict AdBlockService::HiddenClassIdSelectors( return result; } -AdBlockRegionalServiceManager* AdBlockService::regional_service_manager() { +AdBlockComponentServiceManager* AdBlockService::component_service_manager() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - return regional_service_manager_.get(); + return component_service_manager_.get(); } AdBlockCustomFiltersProvider* AdBlockService::custom_filters_provider() { @@ -321,16 +280,7 @@ AdBlockService::AdBlockService( std::make_unique( component_update_service_); - default_filters_provider_ = std::make_unique( - component_update_service_, g_ad_block_default_component_id_, - g_ad_block_default_component_base64_public_key_, - kAdBlockDefaultComponentName); - default_exception_filters_provider_ = - std::make_unique( - component_update_service_, kAdBlockExceptionComponentId, - kAdBlockExceptionComponentBase64PublicKey, - kAdBlockExceptionComponentName); - regional_service_manager_ = std::make_unique( + component_service_manager_ = std::make_unique( local_state_, locale_, component_update_service_, filter_list_catalog_provider_.get()); subscription_service_manager_ = @@ -469,12 +419,4 @@ void AdBlockService::TagExistsForTest(const std::string& tag, std::move(cb)); } -// static -void SetDefaultAdBlockComponentIdAndBase64PublicKeyForTest( - const std::string& component_id, - const std::string& component_base64_public_key) { - g_ad_block_default_component_id_ = component_id; - g_ad_block_default_component_base64_public_key_ = component_base64_public_key; -} - } // namespace brave_shields diff --git a/components/brave_shields/browser/ad_block_service.h b/components/brave_shields/browser/ad_block_service.h index 4e2f79029e69..569becbd3041 100644 --- a/components/brave_shields/browser/ad_block_service.h +++ b/components/brave_shields/browser/ad_block_service.h @@ -44,7 +44,7 @@ namespace brave_shields { class AdBlockEngine; class AdBlockComponentFiltersProvider; class AdBlockDefaultResourceProvider; -class AdBlockRegionalServiceManager; +class AdBlockComponentServiceManager; class AdBlockCustomFiltersProvider; class AdBlockLocalhostFiltersProvider; class AdBlockFilterListCatalogProvider; @@ -67,7 +67,7 @@ class AdBlockService { ~SourceProviderObserver() override; private: - void OnDATLoaded(bool deserialize, const DATFileDataBuffer& dat_buf); + void OnFilterSetLoaded(std::unique_ptr>); // AdBlockFiltersProvider::Observer void OnChanged() override; @@ -75,8 +75,7 @@ class AdBlockService { // AdBlockResourceProvider::Observer void OnResourcesLoaded(const std::string& resources_json) override; - bool deserialize_; - DATFileDataBuffer dat_buf_; + std::unique_ptr> filter_set_; raw_ptr adblock_engine_; raw_ptr filters_provider_; // not owned raw_ptr resource_provider_; // not owned @@ -116,7 +115,7 @@ class AdBlockService { const std::vector& ids, const std::vector& exceptions); - AdBlockRegionalServiceManager* regional_service_manager(); + AdBlockComponentServiceManager* component_service_manager(); AdBlockSubscriptionServiceManager* subscription_service_manager(); AdBlockCustomFiltersProvider* custom_filters_provider(); @@ -182,7 +181,7 @@ class AdBlockService { filter_list_catalog_provider_ GUARDED_BY_CONTEXT(sequence_checker_); std::unique_ptr subscription_service_manager_ GUARDED_BY_CONTEXT(sequence_checker_); - std::unique_ptr regional_service_manager_ + std::unique_ptr component_service_manager_ GUARDED_BY_CONTEXT(sequence_checker_); std::unique_ptr default_engine_; @@ -202,11 +201,6 @@ class AdBlockService { // Registers the local_state preferences used by Adblock void RegisterPrefsForAdBlockService(PrefRegistrySimple* registry); -// static -void SetDefaultAdBlockComponentIdAndBase64PublicKeyForTest( - const std::string& component_id, - const std::string& component_base64_public_key); - } // namespace brave_shields #endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_SERVICE_H_ diff --git a/components/brave_shields/browser/ad_block_subscription_filters_provider.cc b/components/brave_shields/browser/ad_block_subscription_filters_provider.cc index 67764923ec01..2fd3b71e683f 100644 --- a/components/brave_shields/browser/ad_block_subscription_filters_provider.cc +++ b/components/brave_shields/browser/ad_block_subscription_filters_provider.cc @@ -8,6 +8,7 @@ #include #include +#include "base/logging.h" #include "base/task/thread_pool.h" #include "brave/components/brave_shields/adblock/rs/src/lib.rs.h" #include "brave/components/brave_shields/browser/ad_block_filters_provider.h" @@ -27,14 +28,14 @@ AdBlockSubscriptionFiltersProvider::AdBlockSubscriptionFiltersProvider( AdBlockSubscriptionFiltersProvider::~AdBlockSubscriptionFiltersProvider() = default; -void AdBlockSubscriptionFiltersProvider::LoadDATBuffer( - base::OnceCallback - cb) { +void AdBlockSubscriptionFiltersProvider::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback cb) { base::ThreadPool::PostTaskAndReplyWithResult( FROM_HERE, {base::MayBlock()}, base::BindOnce(&brave_component_updater::ReadDATFileData, list_file_), base::BindOnce(&AdBlockSubscriptionFiltersProvider::OnDATFileDataReady, - weak_factory_.GetWeakPtr(), std::move(cb))); + weak_factory_.GetWeakPtr(), std::move(cb), filter_set)); } std::string AdBlockSubscriptionFiltersProvider::GetNameForDebugging() { @@ -42,12 +43,17 @@ std::string AdBlockSubscriptionFiltersProvider::GetNameForDebugging() { } void AdBlockSubscriptionFiltersProvider::OnDATFileDataReady( - base::OnceCallback - cb, + base::OnceCallback cb, + rust::Box* filter_set, const DATFileDataBuffer& dat_buf) { - auto metadata = adblock::read_list_metadata(dat_buf); - on_metadata_retrieved_.Run(metadata); - std::move(cb).Run(false, dat_buf); + auto result = (*filter_set)->add_filter_list(dat_buf); + if (result.result_kind == adblock::ResultKind::Success) { + on_metadata_retrieved_.Run(result.value); + } else { + VLOG(0) << "Subscription list parsing failed: " + << result.error_message.c_str(); + } + std::move(cb).Run(); } void AdBlockSubscriptionFiltersProvider::OnListAvailable() { diff --git a/components/brave_shields/browser/ad_block_subscription_filters_provider.h b/components/brave_shields/browser/ad_block_subscription_filters_provider.h index 5a5f4d0dffc1..5e03aa1d186b 100644 --- a/components/brave_shields/browser/ad_block_subscription_filters_provider.h +++ b/components/brave_shields/browser/ad_block_subscription_filters_provider.h @@ -36,14 +36,12 @@ class AdBlockSubscriptionFiltersProvider : public AdBlockFiltersProvider { const AdBlockSubscriptionFiltersProvider&) = delete; ~AdBlockSubscriptionFiltersProvider() override; - void LoadDATBuffer( - base::OnceCallback) override; - - void OnDATFileDataReady( - base::OnceCallback cb, - const DATFileDataBuffer& dat_buf); + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; + + void OnDATFileDataReady(base::OnceCallback cb, + rust::Box* filter_set, + const DATFileDataBuffer& dat_buf); void OnListAvailable(); diff --git a/components/brave_shields/browser/cookie_list_opt_in_service.cc b/components/brave_shields/browser/cookie_list_opt_in_service.cc index 17468880b726..59fbc7749762 100644 --- a/components/brave_shields/browser/cookie_list_opt_in_service.cc +++ b/components/brave_shields/browser/cookie_list_opt_in_service.cc @@ -9,7 +9,7 @@ #include "base/feature_list.h" #include "base/metrics/histogram_functions.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/components/brave_shields/common/cookie_list_opt_in.mojom-forward.h" diff --git a/components/brave_shields/browser/filter_list_catalog_entry.cc b/components/brave_shields/browser/filter_list_catalog_entry.cc index 65e675a8793b..86b69515c951 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.cc +++ b/components/brave_shields/browser/filter_list_catalog_entry.cc @@ -13,6 +13,7 @@ #include "base/json/json_reader.h" #include "base/json/json_value_converter.h" #include "base/logging.h" +#include "base/numerics/safe_conversions.h" #include "base/ranges/algorithm.h" #include "base/strings/string_util.h" #include "base/values.h" @@ -70,6 +71,20 @@ bool GetStringVector(const base::Value* value, } } +bool GetUint8(const base::Value* value, uint8_t* field) { + DCHECK(field); + if (value == nullptr || !value->is_int()) { + return false; + } else { + int i = value->GetInt(); + if (!base::IsValueInRangeForNumericType(i)) { + return false; + } + *field = base::checked_cast(i); + return true; + } +} + } // namespace namespace brave_shields { @@ -82,9 +97,13 @@ FilterListCatalogEntry::FilterListCatalogEntry( const std::string& title, const std::vector& langs, const std::string& support_url, + const std::string& desc, + bool hidden, + bool default_enabled, + bool first_party_protections, + uint8_t permission_mask, const std::string& component_id, const std::string& base64_public_key, - const std::string& desc, const std::string& ios_component_id, const std::string& ios_base64_public_key) : uuid(uuid), @@ -92,9 +111,13 @@ FilterListCatalogEntry::FilterListCatalogEntry( title(title), langs(langs), support_url(support_url), + desc(desc), + hidden(hidden), + default_enabled(default_enabled), + first_party_protections(first_party_protections), + permission_mask(permission_mask), component_id(component_id), base64_public_key(base64_public_key), - desc(desc), ios_component_id(ios_component_id), ios_base64_public_key(ios_base64_public_key) {} @@ -112,13 +135,21 @@ void FilterListCatalogEntry::RegisterJSONConverter( "langs", &FilterListCatalogEntry::langs, &GetStringVector); converter->RegisterStringField("support_url", &FilterListCatalogEntry::support_url); + converter->RegisterStringField("desc", &FilterListCatalogEntry::desc); + converter->RegisterBoolField("hidden", &FilterListCatalogEntry::hidden); + converter->RegisterBoolField("default_enabled", + &FilterListCatalogEntry::default_enabled); + converter->RegisterBoolField( + "first_party_protections", + &FilterListCatalogEntry::first_party_protections); + converter->RegisterCustomValueField( + "permission_mask", &FilterListCatalogEntry::permission_mask, &GetUint8); converter->RegisterCustomValueField("list_text_component", &FilterListCatalogEntry::component_id, &GetComponentId); converter->RegisterCustomValueField( "list_text_component", &FilterListCatalogEntry::base64_public_key, &GetBase64PublicKey); - converter->RegisterStringField("desc", &FilterListCatalogEntry::desc); converter->RegisterStringField("component_id", &FilterListCatalogEntry::ios_component_id); converter->RegisterStringField( @@ -128,9 +159,7 @@ void FilterListCatalogEntry::RegisterJSONConverter( std::vector::const_iterator FindAdBlockFilterListByUUID( const std::vector& region_lists, const std::string& uuid) { - std::string uuid_uppercase = base::ToUpperASCII(uuid); - return base::ranges::find(region_lists, uuid_uppercase, - &FilterListCatalogEntry::uuid); + return base::ranges::find(region_lists, uuid, &FilterListCatalogEntry::uuid); } // Given a locale like `en-US`, find regional lists corresponding to the diff --git a/components/brave_shields/browser/filter_list_catalog_entry.h b/components/brave_shields/browser/filter_list_catalog_entry.h index 807449a00d3d..8f7bc9e2e206 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.h +++ b/components/brave_shields/browser/filter_list_catalog_entry.h @@ -19,7 +19,9 @@ namespace brave_shields { // Represents an entry of the catalog of filter lists that Brave makes // available. Corresponds to the entries in -// https://github.com/brave/adblock-resources/blob/master/filter_lists/regional.json +// https://github.com/brave/adblock-resources/blob/master/filter_lists/regional.json. +// See https://github.com/brave/adblock-resources#filter-list-description-format +// for details. class FilterListCatalogEntry { public: FilterListCatalogEntry(); @@ -28,9 +30,13 @@ class FilterListCatalogEntry { const std::string& title, const std::vector& langs, const std::string& support_url, + const std::string& desc, + bool hidden, + bool default_enabled, + bool first_party_protections, + uint8_t permission_mask, const std::string& component_id, const std::string& base64_public_key, - const std::string& desc, const std::string& ios_component_id, const std::string& ios_base64_public_key); explicit FilterListCatalogEntry(const FilterListCatalogEntry& other); @@ -41,9 +47,16 @@ class FilterListCatalogEntry { std::string title; std::vector langs; std::string support_url; + std::string desc; + + // Optional fields with default values + bool hidden = false; + bool default_enabled = false; + bool first_party_protections = false; + uint8_t permission_mask = 0; + std::string component_id; std::string base64_public_key; - std::string desc; std::string ios_component_id; std::string ios_base64_public_key; diff --git a/components/brave_shields/browser/filter_list_service.cc b/components/brave_shields/browser/filter_list_service.cc index a097504b83eb..9cc44715624c 100644 --- a/components/brave_shields/browser/filter_list_service.cc +++ b/components/brave_shields/browser/filter_list_service.cc @@ -10,8 +10,8 @@ #include #include "base/feature_list.h" #include "base/strings/utf_string_conversions.h" +#include "brave/components/brave_shields/browser/ad_block_component_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_custom_filters_provider.h" -#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" #include "brave/components/brave_shields/browser/ad_block_service.h" #include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h" #include "brave/components/brave_shields/common/features.h" @@ -44,19 +44,19 @@ void FilterListService::IsFilterListEnabled( const std::string& filterListUuid, IsFilterListEnabledCallback callback) { std::move(callback).Run( - ad_block_service_->regional_service_manager()->IsFilterListEnabled( + ad_block_service_->component_service_manager()->IsFilterListEnabled( filterListUuid)); } void FilterListService::EnableFilter(const std::string& filterListUuid, bool shouldEnableFilter) { - ad_block_service_->regional_service_manager()->EnableFilterList( + ad_block_service_->component_service_manager()->EnableFilterList( filterListUuid, shouldEnableFilter); } void FilterListService::GetFilterLists(GetFilterListsCallback callback) { std::move(callback).Run( - ad_block_service_->regional_service_manager()->GetRegionalLists()); + ad_block_service_->component_service_manager()->GetRegionalLists()); } void FilterListService::GetSubscriptions(GetSubscriptionsCallback callback) { diff --git a/components/brave_shields/browser/test_filters_provider.cc b/components/brave_shields/browser/test_filters_provider.cc index 06983850a958..a72c9474c32e 100644 --- a/components/brave_shields/browser/test_filters_provider.cc +++ b/components/brave_shields/browser/test_filters_provider.cc @@ -15,16 +15,14 @@ namespace brave_shields { TestFiltersProvider::TestFiltersProvider(const std::string& rules, const std::string& resources) : AdBlockFiltersProvider(true), rules_(rules), resources_(resources) {} - -TestFiltersProvider::TestFiltersProvider(const base::FilePath& dat_location, - const std::string& resources) - : AdBlockFiltersProvider(true), resources_(resources) { - CHECK(!dat_location.empty()); - - dat_buffer_ = brave_component_updater::ReadDATFileData(dat_location); - - CHECK(!dat_buffer_.empty()); -} +TestFiltersProvider::TestFiltersProvider(const std::string& rules, + const std::string& resources, + bool engine_is_default, + uint8_t permission_mask) + : AdBlockFiltersProvider(engine_is_default), + rules_(rules), + resources_(resources), + permission_mask_(permission_mask) {} TestFiltersProvider::~TestFiltersProvider() = default; @@ -32,15 +30,12 @@ std::string TestFiltersProvider::GetNameForDebugging() { return "TestFiltersProvider"; } -void TestFiltersProvider::LoadDATBuffer( - base::OnceCallback - cb) { - if (dat_buffer_.empty()) { - auto buffer = std::vector(rules_.begin(), rules_.end()); - std::move(cb).Run(false, buffer); - } else { - std::move(cb).Run(true, dat_buffer_); - } +void TestFiltersProvider::LoadFilterSet( + rust::Box* filter_set, + base::OnceCallback cb) { + auto buffer = std::vector(rules_.begin(), rules_.end()); + (*filter_set)->add_filter_list_with_permissions(buffer, permission_mask_); + std::move(cb).Run(); } void TestFiltersProvider::LoadResources( diff --git a/components/brave_shields/browser/test_filters_provider.h b/components/brave_shields/browser/test_filters_provider.h index 3fe19edf00bb..04ac4778262b 100644 --- a/components/brave_shields/browser/test_filters_provider.h +++ b/components/brave_shields/browser/test_filters_provider.h @@ -9,7 +9,6 @@ #include #include -#include "base/files/file_path.h" #include "base/functional/callback.h" #include "brave/components/brave_component_updater/browser/dat_file_util.h" #include "brave/components/brave_shields/browser/ad_block_filters_provider.h" @@ -23,13 +22,14 @@ class TestFiltersProvider : public AdBlockFiltersProvider, public AdBlockResourceProvider { public: TestFiltersProvider(const std::string& rules, const std::string& resources); - TestFiltersProvider(const base::FilePath& dat_location, - const std::string& resources); + TestFiltersProvider(const std::string& rules, + const std::string& resources, + bool engine_is_default, + uint8_t permission_mask = 0); ~TestFiltersProvider() override; - void LoadDATBuffer( - base::OnceCallback cb) override; + void LoadFilterSet(rust::Box* filter_set, + base::OnceCallback) override; void LoadResources( base::OnceCallback cb) override; @@ -37,9 +37,9 @@ class TestFiltersProvider : public AdBlockFiltersProvider, std::string GetNameForDebugging() override; private: - DATFileDataBuffer dat_buffer_; std::string rules_; std::string resources_; + uint8_t permission_mask_; }; } // namespace brave_shields diff --git a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.h b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.h index 74de4ddd48bb..71d74ab721a7 100644 --- a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.h +++ b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.h @@ -17,9 +17,13 @@ OBJC_EXPORT @property(readonly) NSString* title; @property(readonly) NSArray* languages; @property(readonly) NSString* supportURL; +@property(readonly) NSString* desc; +@property(readonly) bool hidden; +@property(readonly) bool defaultEnabled; +@property(readonly) bool firstPartyProtections; +@property(readonly) uint8_t permissionMask; @property(readonly) NSString* componentId; @property(readonly) NSString* base64PublicKey; -@property(readonly) NSString* desc; @property(readonly) NSString* iosComponentId; @property(readonly) NSString* iosBase64PublicKey; - (instancetype)init NS_UNAVAILABLE; diff --git a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.mm b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.mm index 37dff599608c..ddba168099e4 100644 --- a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.mm +++ b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry.mm @@ -15,9 +15,13 @@ @interface AdblockFilterListCatalogEntry () @property(nonatomic, copy) NSString* title; @property(nonatomic, copy) NSArray* languages; @property(nonatomic, copy) NSString* supportURL; +@property(nonatomic, copy) NSString* desc; +@property(nonatomic) bool hidden; +@property(nonatomic) bool defaultEnabled; +@property(nonatomic) bool firstPartyProtections; +@property(nonatomic) uint8_t permissionMask; @property(nonatomic, copy) NSString* componentId; @property(nonatomic, copy) NSString* base64PublicKey; -@property(nonatomic, copy) NSString* desc; @property(nonatomic, copy) NSString* iosComponentId; @property(nonatomic, copy) NSString* iosBase64PublicKey; @end @@ -32,9 +36,13 @@ - (instancetype)initWithFilterListCatalogEntry: self.title = base::SysUTF8ToNSString(entry.title); self.languages = brave::vector_to_ns(entry.langs); self.supportURL = base::SysUTF8ToNSString(entry.support_url); + self.desc = base::SysUTF8ToNSString(entry.desc); + self.hidden = entry.hidden; + self.defaultEnabled = entry.default_enabled; + self.firstPartyProtections = entry.first_party_protections; + self.permissionMask = entry.permission_mask; self.componentId = base::SysUTF8ToNSString(entry.component_id); self.base64PublicKey = base::SysUTF8ToNSString(entry.base64_public_key); - self.desc = base::SysUTF8ToNSString(entry.desc); self.iosComponentId = base::SysUTF8ToNSString(entry.ios_component_id); self.iosBase64PublicKey = base::SysUTF8ToNSString(entry.ios_base64_public_key); @@ -48,9 +56,10 @@ - (instancetype)initWithFilterListCatalogEntry: base::SysNSStringToUTF8(self.title), brave::ns_to_vector(self.languages), base::SysNSStringToUTF8(self.supportURL), + base::SysNSStringToUTF8(self.desc), self.hidden, self.defaultEnabled, + self.firstPartyProtections, self.permissionMask, base::SysNSStringToUTF8(self.componentId), base::SysNSStringToUTF8(self.base64PublicKey), - base::SysNSStringToUTF8(self.desc), base::SysNSStringToUTF8(self.iosComponentId), base::SysNSStringToUTF8(self.iosBase64PublicKey)); } diff --git a/test/BUILD.gn b/test/BUILD.gn index 83d90cae8060..cf1a63713ce1 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -129,7 +129,7 @@ test("brave_unit_tests") { "//brave/components/brave_private_cdn/private_cdn_helper_unittest.cc", "//brave/components/brave_search/browser/brave_search_default_host_unittest.cc", "//brave/components/brave_search/browser/brave_search_fallback_host_unittest.cc", - "//brave/components/brave_shields/browser/ad_block_regional_service_unittest.cc", + "//brave/components/brave_shields/browser/ad_block_component_service_unittest.cc", "//brave/components/brave_shields/browser/adblock_stub_response_unittest.cc", "//brave/components/brave_shields/browser/brave_farbling_service_unittest.cc", "//brave/components/brave_shields/browser/cookie_list_opt_in_service_unittest.cc", diff --git a/test/data/adblock-data/redirect-rule.dat b/test/data/adblock-data/redirect-rule.dat deleted file mode 100644 index 6caa28b8f22f..000000000000 Binary files a/test/data/adblock-data/redirect-rule.dat and /dev/null differ