From 61424d0046c2caba27523586c72a2b3c0caa7457 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Thu, 25 Jan 2024 23:05:45 -0800 Subject: [PATCH] add platforms field to catalog entries --- .../ad_block_service_browsertest.cc | 8 +++--- .../cookie_list_opt_in_browsertest.cc | 2 +- .../ad_block_component_service_manager.cc | 12 +++++---- .../ad_block_component_service_unittest.cc | 7 ++--- .../browser/filter_list_catalog_entry.cc | 27 +++++++++++++++++++ .../browser/filter_list_catalog_entry.h | 4 +++ .../adblock_filter_list_catalog_entry.h | 1 + .../adblock_filter_list_catalog_entry.mm | 3 +++ 8 files changed, 51 insertions(+), 13 deletions(-) diff --git a/browser/brave_shields/ad_block_service_browsertest.cc b/browser/brave_shields/ad_block_service_browsertest.cc index 8a582ec8b5c1..c9876d66d4c6 100644 --- a/browser/brave_shields/ad_block_service_browsertest.cc +++ b/browser/brave_shields/ad_block_service_browsertest.cc @@ -224,7 +224,7 @@ bool AdBlockServiceTest::InstallDefaultAdBlockComponent( ->component_service_manager() ->SetFilterListCatalog({brave_shields::FilterListCatalogEntry( "default", "", "Brave Ad Block Updater", {}, "", - "Default lists for Brave Browser", true, true, true, 0, + "Default lists for Brave Browser", true, true, true, 0, {}, kDefaultAdBlockComponentTestId, kDefaultAdBlockComponentTest64PublicKey)}); const extensions::Extension* ad_block_component = LoadExtensionAsComponent( @@ -262,7 +262,7 @@ bool AdBlockServiceTest::InstallRegionalAdBlockComponent( 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, + "Removes advertisements from French websites", false, false, false, 0, {}, kRegionalAdBlockComponentTestId, kRegionalAdBlockComponentTest64PublicKey)); g_brave_browser_process->ad_block_service() @@ -1908,11 +1908,11 @@ 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, + "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, + "https://support.example.com", "second list", false, false, false, 0, {}, "testid2", "pubkey2")); g_brave_browser_process->ad_block_service() ->component_service_manager() 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 ce78e9e8bb2e..36cbd03e6426 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 @@ -128,7 +128,7 @@ class CookieListOptInBrowserTest : public InProcessBrowserTest { "https://secure.fanboy.co.nz/fanboy-cookiemonster_ubo.txt", "Easylist-Cookie List - Filter Obtrusive Cookie Notices", {}, "https://forums.lanik.us/", "Removes obtrusive cookie law notices", - false, false, false, 0, kRegionalAdBlockComponentTestId, + false, false, false, 0, {}, kRegionalAdBlockComponentTestId, kRegionalAdBlockComponentTest64PublicKey)}; GetComponentServiceManager()->SetFilterListCatalog(filter_list_catalog); } diff --git a/components/brave_shields/browser/ad_block_component_service_manager.cc b/components/brave_shields/browser/ad_block_component_service_manager.cc index a4ffdb435ee6..1a0487e91e6c 100644 --- a/components/brave_shields/browser/ad_block_component_service_manager.cc +++ b/components/brave_shields/browser/ad_block_component_service_manager.cc @@ -197,10 +197,12 @@ bool AdBlockComponentServiceManager::IsFilterListEnabled( // 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) { + if (catalog_entry != filter_list_catalog_.end()) { + if (!catalog_entry->SupportsCurrentPlatform()) { + return false; + } + // prefer any user setting for a default-enabled list, unless it's hidden + if (catalog_entry->default_enabled && (!list_touched || catalog_entry->hidden)) { return true; } } @@ -269,7 +271,7 @@ base::Value::List AdBlockComponentServiceManager::GetRegionalLists() { base::Value::List list; for (const auto& region_list : filter_list_catalog_) { - if (region_list.hidden) { + if (region_list.hidden || !region_list.SupportsCurrentPlatform()) { continue; } // Most settings come directly from the regional catalog from diff --git a/components/brave_shields/browser/ad_block_component_service_unittest.cc b/components/brave_shields/browser/ad_block_component_service_unittest.cc index ef22220a1911..52d53c8403a0 100644 --- a/components/brave_shields/browser/ad_block_component_service_unittest.cc +++ b/components/brave_shields/browser/ad_block_component_service_unittest.cc @@ -11,15 +11,15 @@ TEST(AdBlockComponentServiceTest, UserModelLanguages) { 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")); + 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")); + 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")); + false, false, 0, {}, "componentid", "base64publickey")); std::vector languages({"fr", "fR", "fr-FR", "fr-ca"}); std::for_each( @@ -63,4 +63,5 @@ TEST(AdBlockComponentServiceTest, MissingFieldDefaultValues) { ASSERT_EQ(catalog[0].default_enabled, false); ASSERT_EQ(catalog[0].first_party_protections, false); ASSERT_EQ(catalog[0].permission_mask, 0); + ASSERT_EQ(catalog[0].platforms.size(), 0UL); } diff --git a/components/brave_shields/browser/filter_list_catalog_entry.cc b/components/brave_shields/browser/filter_list_catalog_entry.cc index 2a367d631abc..f653aa6e41b5 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.cc +++ b/components/brave_shields/browser/filter_list_catalog_entry.cc @@ -85,6 +85,20 @@ bool GetUint8(const base::Value* value, uint8_t* field) { } } +#if BUILDFLAG(IS_LINUX) +constexpr char kCurrentPlatform[] = "LINUX"; +#elif BUILDFLAG(IS_WIN) +constexpr char kCurrentPlatform[] = "WINDOWS"; +#elif BUILDFLAG(IS_MAC) +constexpr char kCurrentPlatform[] = "MAC"; +#elif BUILDFLAG(IS_ANDROID) +constexpr char kCurrentPlatform[] = "ANDROID"; +#elif BUILDFLAG(IS_IOS) +constexpr char kCurrentPlatform[] = "IOS"; +#else +constexpr char kCurrentPlatform[] = "OTHER"; +#endif + } // namespace namespace brave_shields { @@ -102,6 +116,7 @@ FilterListCatalogEntry::FilterListCatalogEntry( bool default_enabled, bool first_party_protections, uint8_t permission_mask, + const std::vector& platforms, const std::string& component_id, const std::string& base64_public_key) : uuid(uuid), @@ -114,6 +129,7 @@ FilterListCatalogEntry::FilterListCatalogEntry( default_enabled(default_enabled), first_party_protections(first_party_protections), permission_mask(permission_mask), + platforms(platforms), component_id(component_id), base64_public_key(base64_public_key) {} @@ -146,6 +162,17 @@ void FilterListCatalogEntry::RegisterJSONConverter( converter->RegisterCustomValueField( "list_text_component", &FilterListCatalogEntry::base64_public_key, &GetBase64PublicKey); + converter->RegisterCustomValueField>( + "platforms", &FilterListCatalogEntry::platforms, &GetStringVector); +} + +bool FilterListCatalogEntry::SupportsCurrentPlatform() const { + if (platforms.empty()) { + return true; + } + + return std::find(platforms.begin(), platforms.end(), kCurrentPlatform) != + platforms.end(); } std::vector::const_iterator FindAdBlockFilterListByUUID( diff --git a/components/brave_shields/browser/filter_list_catalog_entry.h b/components/brave_shields/browser/filter_list_catalog_entry.h index 9b7b66ae1425..fc2a297ada08 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.h +++ b/components/brave_shields/browser/filter_list_catalog_entry.h @@ -35,6 +35,7 @@ class FilterListCatalogEntry { bool default_enabled, bool first_party_protections, uint8_t permission_mask, + const std::vector& platforms, const std::string& component_id, const std::string& base64_public_key); explicit FilterListCatalogEntry(const FilterListCatalogEntry& other); @@ -52,12 +53,15 @@ class FilterListCatalogEntry { bool default_enabled = false; bool first_party_protections = false; uint8_t permission_mask = 0; + std::vector platforms = {}; std::string component_id; std::string base64_public_key; static void RegisterJSONConverter( base::JSONValueConverter*); + + bool SupportsCurrentPlatform() const; }; std::vector::const_iterator FindAdBlockFilterListByUUID( 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 c1082015c503..0dac7feece35 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 @@ -22,6 +22,7 @@ OBJC_EXPORT @property(readonly) bool defaultEnabled; @property(readonly) bool firstPartyProtections; @property(readonly) uint8_t permissionMask; +@property(readonly) NSArray* platforms; @property(readonly) NSString* componentId; @property(readonly) NSString* base64PublicKey; - (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 72a54da43971..71712d9bbf68 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 @@ -20,6 +20,7 @@ @interface AdblockFilterListCatalogEntry () @property(nonatomic) bool defaultEnabled; @property(nonatomic) bool firstPartyProtections; @property(nonatomic) uint8_t permissionMask; +@property(nonatomic, copy) NSArray* platforms; @property(nonatomic, copy) NSString* componentId; @property(nonatomic, copy) NSString* base64PublicKey; @end @@ -39,6 +40,7 @@ - (instancetype)initWithFilterListCatalogEntry: self.defaultEnabled = entry.default_enabled; self.firstPartyProtections = entry.first_party_protections; self.permissionMask = entry.permission_mask; + self.platforms = brave::vector_to_ns(entry.platforms); self.componentId = base::SysUTF8ToNSString(entry.component_id); self.base64PublicKey = base::SysUTF8ToNSString(entry.base64_public_key); } @@ -53,6 +55,7 @@ - (instancetype)initWithFilterListCatalogEntry: base::SysNSStringToUTF8(self.supportURL), base::SysNSStringToUTF8(self.desc), self.hidden, self.defaultEnabled, self.firstPartyProtections, self.permissionMask, + brave::ns_to_vector(self.platforms), base::SysNSStringToUTF8(self.componentId), base::SysNSStringToUTF8(self.base64PublicKey)); }