Skip to content

Commit

Permalink
add platforms field to catalog entries
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Jan 26, 2024
1 parent 60622cf commit 61424d0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 13 deletions.
8 changes: 4 additions & 4 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1908,11 +1908,11 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, HiddenListsNotPresented) {
std::vector<brave_shields::FilterListCatalogEntry> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> languages({"fr", "fR", "fr-FR", "fr-ca"});
std::for_each(
Expand Down Expand Up @@ -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);
}
27 changes: 27 additions & 0 deletions components/brave_shields/browser/filter_list_catalog_entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -102,6 +116,7 @@ FilterListCatalogEntry::FilterListCatalogEntry(
bool default_enabled,
bool first_party_protections,
uint8_t permission_mask,
const std::vector<std::string>& platforms,
const std::string& component_id,
const std::string& base64_public_key)
: uuid(uuid),
Expand All @@ -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) {}

Expand Down Expand Up @@ -146,6 +162,17 @@ void FilterListCatalogEntry::RegisterJSONConverter(
converter->RegisterCustomValueField(
"list_text_component", &FilterListCatalogEntry::base64_public_key,
&GetBase64PublicKey);
converter->RegisterCustomValueField<std::vector<std::string>>(
"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<FilterListCatalogEntry>::const_iterator FindAdBlockFilterListByUUID(
Expand Down
4 changes: 4 additions & 0 deletions components/brave_shields/browser/filter_list_catalog_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FilterListCatalogEntry {
bool default_enabled,
bool first_party_protections,
uint8_t permission_mask,
const std::vector<std::string>& platforms,
const std::string& component_id,
const std::string& base64_public_key);
explicit FilterListCatalogEntry(const FilterListCatalogEntry& other);
Expand All @@ -52,12 +53,15 @@ class FilterListCatalogEntry {
bool default_enabled = false;
bool first_party_protections = false;
uint8_t permission_mask = 0;
std::vector<std::string> platforms = {};

std::string component_id;
std::string base64_public_key;

static void RegisterJSONConverter(
base::JSONValueConverter<FilterListCatalogEntry>*);

bool SupportsCurrentPlatform() const;
};

std::vector<FilterListCatalogEntry>::const_iterator FindAdBlockFilterListByUUID(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ OBJC_EXPORT
@property(readonly) bool defaultEnabled;
@property(readonly) bool firstPartyProtections;
@property(readonly) uint8_t permissionMask;
@property(readonly) NSArray<NSString*>* platforms;
@property(readonly) NSString* componentId;
@property(readonly) NSString* base64PublicKey;
- (instancetype)init NS_UNAVAILABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface AdblockFilterListCatalogEntry ()
@property(nonatomic) bool defaultEnabled;
@property(nonatomic) bool firstPartyProtections;
@property(nonatomic) uint8_t permissionMask;
@property(nonatomic, copy) NSArray<NSString*>* platforms;
@property(nonatomic, copy) NSString* componentId;
@property(nonatomic, copy) NSString* base64PublicKey;
@end
Expand All @@ -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<std::string>(entry.platforms);
self.componentId = base::SysUTF8ToNSString(entry.component_id);
self.base64PublicKey = base::SysUTF8ToNSString(entry.base64_public_key);
}
Expand All @@ -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<std::string>(self.platforms),
base::SysNSStringToUTF8(self.componentId),
base::SysNSStringToUTF8(self.base64PublicKey));
}
Expand Down

0 comments on commit 61424d0

Please sign in to comment.