From ba8511cceb4279985f2e42cd4be5798647211b64 Mon Sep 17 00:00:00 2001 From: brave-builds <45370463+brave-builds@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:06:10 -0400 Subject: [PATCH] Adjusted some adblock components update check interval (uplift to 1.60.x) (#20591) * Fix #32799: Use correct components for ad-blocking (#20137) * Uplift of #20557 (squashed) to beta --------- Co-authored-by: Jacob Sikorski --- .../ad_block_service_browsertest.cc | 2 +- .../cookie_list_opt_in_browsertest.cc | 2 +- .../browser/ad_block_component_installer.cc | 57 ++++++++---- .../browser/ad_block_component_installer.h | 11 ++- .../ad_block_default_resource_provider.cc | 35 +++++-- .../ad_block_default_resource_provider.h | 2 + .../ad_block_regional_service_unittest.cc | 6 +- .../brave_shields/browser/ad_block_service.cc | 14 +++ .../brave_shields/browser/ad_block_service.h | 2 + .../browser/filter_list_catalog_entry.cc | 12 +-- .../browser/filter_list_catalog_entry.h | 6 +- ios/app/brave_core_main.mm | 2 - ...dblock_filter_list_catalog_entry+private.h | 3 +- .../adblock_filter_list_catalog_entry.h | 2 - .../adblock_filter_list_catalog_entry.mm | 9 +- .../api/brave_shields/adblock_service.h | 29 +++--- .../api/brave_shields/adblock_service.mm | 92 ++++++++----------- 17 files changed, 157 insertions(+), 129 deletions(-) diff --git a/browser/brave_shields/ad_block_service_browsertest.cc b/browser/brave_shields/ad_block_service_browsertest.cc index afa1767b9f56..30cd52b3cc18 100644 --- a/browser/brave_shields/ad_block_service_browsertest.cc +++ b/browser/brave_shields/ad_block_service_browsertest.cc @@ -287,7 +287,7 @@ bool AdBlockServiceTest::InstallRegionalAdBlockExtension( uuid, "https://easylist-downloads.adblockplus.org/liste_fr.txt", "EasyList Liste FR", {"fr"}, "https://forums.lanik.us/viewforum.php?f=91", kRegionalAdBlockComponentTestId, kRegionalAdBlockComponentTest64PublicKey, - "Removes advertisements from French websites", "", "")); + "Removes advertisements from French websites")); g_brave_browser_process->ad_block_service() ->regional_service_manager() ->SetFilterListCatalog(filter_list_catalog); 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..78448eb1a5f4 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 @@ -129,7 +129,7 @@ class CookieListOptInBrowserTest : public InProcessBrowserTest { "Easylist-Cookie List - Filter Obtrusive Cookie Notices", {}, "https://forums.lanik.us/", kRegionalAdBlockComponentTestId, kRegionalAdBlockComponentTest64PublicKey, - "Removes obtrusive cookie law notices", "", "")}; + "Removes obtrusive cookie law notices")}; GetRegionalServiceManager()->SetFilterListCatalog(filter_list_catalog); } diff --git a/components/brave_shields/browser/ad_block_component_installer.cc b/components/brave_shields/browser/ad_block_component_installer.cc index 07a21132ac08..1ba326b98768 100644 --- a/components/brave_shields/browser/ad_block_component_installer.cc +++ b/components/brave_shields/browser/ad_block_component_installer.cc @@ -1,7 +1,7 @@ -/* Copyright (c) 2021 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/. */ +// 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/ad_block_component_installer.h" @@ -12,6 +12,9 @@ #include "base/base64.h" #include "base/functional/bind.h" #include "base/functional/callback.h" +#include "base/rand_util.h" +#include "base/task/sequenced_task_runner.h" +#include "base/time/time.h" #include "brave/components/brave_component_updater/browser/brave_on_demand_updater.h" #include "components/component_updater/component_installer.h" #include "components/component_updater/component_updater_service.h" @@ -48,17 +51,16 @@ const char kAdBlockFilterListCatalogComponentBase64PublicKey[] = "1H8y9SR970LqsUMozu3ioSHtFh/IVgq7Nqy4TljaKsTE+3AdtjiOyHpW9ZaOkA7j" "2QIDAQAB"; -const char kAdBlockIosDefaultDatComponentName[] = "Brave Ad Block Updater"; -const char kAdBlockIosDefaultDatComponentId[] = - "cffkpbalmllkdoenhmdmpbkajipdjfam"; -const char kAdBlockIosDefaultDatComponentBase64PublicKey[] = - "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs0qzJmHSgIiw7IGFCxij" - "1NnB5hJ5ZQ1LKW9htL4EBOaMJvmqaDs/wfq0nw/goBHWsqqkMBynRTu2Hxxirvdb" - "cugn1Goys5QKPgAvKwDHJp9jlnADWm5xQvPQ4GE1mK1/I3ka9cEOCzPW6GI+wGLi" - "VPx9VZrxHHsSBIJRaEB5Tyi5bj0CZ+kcfMnRTsXIBw3C6xJgCVKISQUkd8mawVvG" - "vqOhBOogCdb9qza5eJ1Cgx8RWKucFfaWWxKLOelCiBMT1Hm1znAoVBHG/blhJJOD" - "5HcH/heRrB4MvrE1J76WF3fvZ03aHVcnlLtQeiNNOZ7VbBDXdie8Nomf/QswbBGa" - "VwIDAQAB"; +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"; class AdBlockComponentInstallerPolicy : public component_updater::ComponentInstallerPolicy { @@ -170,7 +172,7 @@ void OnRegistered(const std::string& component_id) { } // namespace -void RegisterAdBlockIosDefaultDatComponent( +void RegisterAdBlockDefaultComponent( component_updater::ComponentUpdateService* cus, OnComponentReadyCallback callback) { // In test, |cus| could be nullptr. @@ -179,11 +181,10 @@ void RegisterAdBlockIosDefaultDatComponent( auto installer = base::MakeRefCounted( std::make_unique( - kAdBlockIosDefaultDatComponentBase64PublicKey, - kAdBlockIosDefaultDatComponentId, kAdBlockIosDefaultDatComponentName, - callback)); + kAdBlockDefaultComponentBase64PublicKey, kAdBlockDefaultComponentId, + kAdBlockDefaultComponentName, callback)); installer->Register( - cus, base::BindOnce(&OnRegistered, kAdBlockIosDefaultDatComponentId)); + cus, base::BindOnce(&OnRegistered, kAdBlockDefaultComponentId)); } void RegisterAdBlockDefaultResourceComponent( @@ -201,6 +202,22 @@ void RegisterAdBlockDefaultResourceComponent( cus, base::BindOnce(&OnRegistered, kAdBlockResourceComponentId)); } +void CheckAdBlockComponentsUpdate() { + auto runner = base::SequencedTaskRunner::GetCurrentDefault(); + + runner->PostDelayedTask(FROM_HERE, base::BindOnce([]() { + BraveOnDemandUpdater::GetInstance()->OnDemandUpdate( + kAdBlockResourceComponentId); + }), + base::Seconds(base::RandInt(0, 10))); + + runner->PostDelayedTask(FROM_HERE, base::BindOnce([]() { + BraveOnDemandUpdater::GetInstance()->OnDemandUpdate( + kAdBlockDefaultComponentId); + }), + base::Seconds(base::RandInt(0, 10))); +} + void RegisterAdBlockFilterListCatalogComponent( component_updater::ComponentUpdateService* cus, OnComponentReadyCallback callback) { diff --git a/components/brave_shields/browser/ad_block_component_installer.h b/components/brave_shields/browser/ad_block_component_installer.h index c08edf4c88fc..0212a278d6f8 100644 --- a/components/brave_shields/browser/ad_block_component_installer.h +++ b/components/brave_shields/browser/ad_block_component_installer.h @@ -1,7 +1,7 @@ -/* Copyright (c) 2021 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/. */ +// 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/. #ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_COMPONENT_INSTALLER_H_ #define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_AD_BLOCK_COMPONENT_INSTALLER_H_ @@ -20,13 +20,14 @@ namespace brave_shields { using OnComponentReadyCallback = base::RepeatingCallback; -void RegisterAdBlockIosDefaultDatComponent( +void RegisterAdBlockDefaultComponent( component_updater::ComponentUpdateService* cus, OnComponentReadyCallback callback); void RegisterAdBlockDefaultResourceComponent( component_updater::ComponentUpdateService* cus, OnComponentReadyCallback callback); +void CheckAdBlockComponentsUpdate(); void RegisterAdBlockFilterListCatalogComponent( component_updater::ComponentUpdateService* cus, diff --git a/components/brave_shields/browser/ad_block_default_resource_provider.cc b/components/brave_shields/browser/ad_block_default_resource_provider.cc index 6c1ade0a4b25..48030cdd9843 100644 --- a/components/brave_shields/browser/ad_block_default_resource_provider.cc +++ b/components/brave_shields/browser/ad_block_default_resource_provider.cc @@ -8,23 +8,46 @@ #include #include +#include "base/feature_list.h" #include "base/files/file_path.h" +#include "base/metrics/field_trial_params.h" #include "base/task/thread_pool.h" #include "brave/components/brave_shields/browser/ad_block_component_installer.h" +#include "brave/components/brave_shields/browser/ad_block_service.h" -const char kAdBlockResourcesFilename[] = "resources.json"; +namespace { +constexpr char kAdBlockResourcesFilename[] = "resources.json"; + +BASE_DECLARE_FEATURE(kAdBlockDefaultResourceUpdateInterval); +constexpr base::FeatureParam kComponentUpdateCheckIntervalMins{ + &kAdBlockDefaultResourceUpdateInterval, "update_interval_mins", 100}; +BASE_FEATURE(kAdBlockDefaultResourceUpdateInterval, + "AdBlockDefaultResourceUpdateInterval", + base::FEATURE_ENABLED_BY_DEFAULT); + +} // namespace namespace brave_shields { AdBlockDefaultResourceProvider::AdBlockDefaultResourceProvider( component_updater::ComponentUpdateService* cus) { // Can be nullptr in unit tests - if (cus) { - RegisterAdBlockDefaultResourceComponent( - cus, - base::BindRepeating(&AdBlockDefaultResourceProvider::OnComponentReady, - weak_factory_.GetWeakPtr())); + if (!cus) { + return; } + + RegisterAdBlockDefaultResourceComponent( + cus, + base::BindRepeating(&AdBlockDefaultResourceProvider::OnComponentReady, + weak_factory_.GetWeakPtr())); + update_check_timer_.Start( + FROM_HERE, base::Minutes(kComponentUpdateCheckIntervalMins.Get()), + base::BindRepeating([]() { + // Separated into two methods as exception component is not available in + // iOS. So can't check it from CheckAdBlockComponentsUpdate() together. + CheckAdBlockComponentsUpdate(); + CheckAdBlockExceptionComponentsUpdate(); + })); } AdBlockDefaultResourceProvider::~AdBlockDefaultResourceProvider() = default; diff --git a/components/brave_shields/browser/ad_block_default_resource_provider.h b/components/brave_shields/browser/ad_block_default_resource_provider.h index b6fb8ade7ad1..78023bfd7ff2 100644 --- a/components/brave_shields/browser/ad_block_default_resource_provider.h +++ b/components/brave_shields/browser/ad_block_default_resource_provider.h @@ -9,6 +9,7 @@ #include #include "base/functional/callback.h" +#include "base/timer/timer.h" #include "brave/components/brave_shields/browser/ad_block_resource_provider.h" namespace component_updater { @@ -38,6 +39,7 @@ class AdBlockDefaultResourceProvider : public AdBlockResourceProvider { void OnComponentReady(const base::FilePath&); base::FilePath component_path_; + base::RepeatingTimer update_check_timer_; base::WeakPtrFactory weak_factory_{this}; }; diff --git a/components/brave_shields/browser/ad_block_regional_service_unittest.cc b/components/brave_shields/browser/ad_block_regional_service_unittest.cc index 87309a4e4439..7d5a3ba582fb 100644 --- a/components/brave_shields/browser/ad_block_regional_service_unittest.cc +++ b/components/brave_shields/browser/ad_block_regional_service_unittest.cc @@ -11,15 +11,15 @@ TEST(AdBlockRegionalServiceTest, UserModelLanguages) { 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", "", "")); + "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", "", "")); + "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", "", "")); + "Filter list for testing purposes")); std::vector languages({"fr", "fR", "fr-FR", "fr-ca"}); std::for_each( diff --git a/components/brave_shields/browser/ad_block_service.cc b/components/brave_shields/browser/ad_block_service.cc index d1f770af4fa6..54075b03e0be 100644 --- a/components/brave_shields/browser/ad_block_service.cc +++ b/components/brave_shields/browser/ad_block_service.cc @@ -13,7 +13,10 @@ #include "base/files/file_path.h" #include "base/functional/bind.h" #include "base/logging.h" +#include "base/rand_util.h" #include "base/threading/thread_restrictions.h" +#include "base/time/time.h" +#include "brave/components/brave_component_updater/browser/brave_on_demand_updater.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_custom_filters_provider.h" @@ -33,6 +36,8 @@ #include "third_party/abseil-cpp/absl/types/optional.h" #include "url/origin.h" +using brave_component_updater::BraveOnDemandUpdater; + namespace { const char kAdBlockDefaultComponentName[] = "Brave Ad Block Updater"; @@ -473,6 +478,15 @@ void AdBlockService::TagExistsForTest(const std::string& tag, std::move(cb)); } +void CheckAdBlockExceptionComponentsUpdate() { + base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask( + FROM_HERE, base::BindOnce([]() { + BraveOnDemandUpdater::GetInstance()->OnDemandUpdate( + kAdBlockExceptionComponentId); + }), + base::Seconds(base::RandInt(0, 10))); +} + // static void SetDefaultAdBlockComponentIdAndBase64PublicKeyForTest( // IN-TEST const std::string& component_id, diff --git a/components/brave_shields/browser/ad_block_service.h b/components/brave_shields/browser/ad_block_service.h index aea5b976f595..b7845eba4c2c 100644 --- a/components/brave_shields/browser/ad_block_service.h +++ b/components/brave_shields/browser/ad_block_service.h @@ -199,6 +199,8 @@ class AdBlockService { base::WeakPtrFactory weak_factory_{this}; }; +void CheckAdBlockExceptionComponentsUpdate(); + // Registers the local_state preferences used by Adblock void RegisterPrefsForAdBlockService(PrefRegistrySimple* registry); diff --git a/components/brave_shields/browser/filter_list_catalog_entry.cc b/components/brave_shields/browser/filter_list_catalog_entry.cc index 65e675a8793b..4e2dd9c14edb 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.cc +++ b/components/brave_shields/browser/filter_list_catalog_entry.cc @@ -84,9 +84,7 @@ FilterListCatalogEntry::FilterListCatalogEntry( const std::string& support_url, 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) + const std::string& desc) : uuid(uuid), url(url), title(title), @@ -94,9 +92,7 @@ FilterListCatalogEntry::FilterListCatalogEntry( support_url(support_url), 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) {} + desc(desc) {} FilterListCatalogEntry::FilterListCatalogEntry( const FilterListCatalogEntry& other) = default; @@ -119,10 +115,6 @@ void FilterListCatalogEntry::RegisterJSONConverter( "list_text_component", &FilterListCatalogEntry::base64_public_key, &GetBase64PublicKey); converter->RegisterStringField("desc", &FilterListCatalogEntry::desc); - converter->RegisterStringField("component_id", - &FilterListCatalogEntry::ios_component_id); - converter->RegisterStringField( - "base64_public_key", &FilterListCatalogEntry::ios_base64_public_key); } 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 807449a00d3d..7549b4629f9d 100644 --- a/components/brave_shields/browser/filter_list_catalog_entry.h +++ b/components/brave_shields/browser/filter_list_catalog_entry.h @@ -30,9 +30,7 @@ class FilterListCatalogEntry { const std::string& support_url, 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); + const std::string& desc); explicit FilterListCatalogEntry(const FilterListCatalogEntry& other); ~FilterListCatalogEntry(); @@ -44,8 +42,6 @@ class FilterListCatalogEntry { std::string component_id; std::string base64_public_key; std::string desc; - std::string ios_component_id; - std::string ios_base64_public_key; static void RegisterJSONConverter( base::JSONValueConverter*); diff --git a/ios/app/brave_core_main.mm b/ios/app/brave_core_main.mm index 9c295475436b..838cad1cbf80 100644 --- a/ios/app/brave_core_main.mm +++ b/ios/app/brave_core_main.mm @@ -314,8 +314,6 @@ - (void)registerComponentsForUpdate: RegisterSafetyTipsComponent(cus); brave_wallet::RegisterWalletDataFilesComponent(cus); - - [self.adblockService registerDefaultShieldsComponent]; } + (void)setLogHandler:(BraveCoreLogHandler)logHandler { diff --git a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry+private.h b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry+private.h index 5a1ab78b0b3a..c9a774d624a7 100644 --- a/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry+private.h +++ b/ios/browser/api/brave_shields/adblock_filter_list_catalog_entry+private.h @@ -16,9 +16,10 @@ class FilterListCatalogEntry; NS_ASSUME_NONNULL_BEGIN @interface AdblockFilterListCatalogEntry (Private) +@property(readonly) NSString* base64PublicKey; + - (instancetype)initWithFilterListCatalogEntry: (brave_shields::FilterListCatalogEntry)entry; -- (brave_shields::FilterListCatalogEntry)entry; @end NS_ASSUME_NONNULL_END 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..f957057ae633 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 @@ -20,8 +20,6 @@ OBJC_EXPORT @property(readonly) NSString* componentId; @property(readonly) NSString* base64PublicKey; @property(readonly) NSString* desc; -@property(readonly) NSString* iosComponentId; -@property(readonly) NSString* iosBase64PublicKey; - (instancetype)init NS_UNAVAILABLE; @end 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..551ea1aabe0a 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 @@ -18,8 +18,6 @@ @interface AdblockFilterListCatalogEntry () @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 @implementation AdblockFilterListCatalogEntry @@ -35,9 +33,6 @@ - (instancetype)initWithFilterListCatalogEntry: 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); } return self; } @@ -50,9 +45,7 @@ - (instancetype)initWithFilterListCatalogEntry: base::SysNSStringToUTF8(self.supportURL), base::SysNSStringToUTF8(self.componentId), base::SysNSStringToUTF8(self.base64PublicKey), - base::SysNSStringToUTF8(self.desc), - base::SysNSStringToUTF8(self.iosComponentId), - base::SysNSStringToUTF8(self.iosBase64PublicKey)); + base::SysNSStringToUTF8(self.desc)); } @end diff --git a/ios/browser/api/brave_shields/adblock_service.h b/ios/browser/api/brave_shields/adblock_service.h index 95e7e2ee28fe..e9c13d8a8a34 100644 --- a/ios/browser/api/brave_shields/adblock_service.h +++ b/ios/browser/api/brave_shields/adblock_service.h @@ -15,27 +15,30 @@ NS_ASSUME_NONNULL_BEGIN OBJC_EXPORT @interface AdblockService : NSObject -/// The main shields file install path (KVO compiliant) -@property(readonly, nullable) NSString* shieldsInstallPath; - -/// Regional filter lists -@property(readonly, nullable) - NSArray* regionalFilterLists; - -/// Executed each time the main shields component is updated -@property(nonatomic, copy, nullable) void (^shieldsComponentReady) - (NSString* _Nullable installPath); +/// Registers the default filter list component and calls `componentReady` each +/// time the component is updated +- (void)registerDefaultComponent: + (void (^)(NSString* _Nullable installPath))componentReady; + +/// Registers the filter list catalog component and calls `componentReady` each +/// time the component is updated +- (void)registerFilterListCatalogComponent: + (void (^)(NSArray* filterLists)) + componentReady; + +/// Registers the resources component and calls `componentReady` +/// each time the component is updated +- (void)registerResourceComponent: + (void (^)(NSString* _Nullable installPath))componentReady; /// Registers a filter list with the component updater and calls /// `componentReady` each time the component is updated - (void)registerFilterListComponent:(AdblockFilterListCatalogEntry*)entry - useLegacyComponent:(bool)useLegacyComponent componentReady:(void (^)(NSString* _Nullable installPath)) componentReady; /// Unregisters a filter list with the component updater -- (void)unregisterFilterListComponent:(AdblockFilterListCatalogEntry*)entry - useLegacyComponent:(bool)useLegacyComponent; +- (void)unregisterFilterListComponent:(AdblockFilterListCatalogEntry*)entry; - (instancetype)init NS_UNAVAILABLE; diff --git a/ios/browser/api/brave_shields/adblock_service.mm b/ios/browser/api/brave_shields/adblock_service.mm index 9179da82e135..4b6695cf77b3 100644 --- a/ios/browser/api/brave_shields/adblock_service.mm +++ b/ios/browser/api/brave_shields/adblock_service.mm @@ -17,9 +17,6 @@ @interface AdblockService () { component_updater::ComponentUpdateService* _cus; // NOT OWNED } -@property(nonatomic, copy) NSString* shieldsInstallPath; -@property(nonatomic, copy) - NSArray* regionalFilterLists; @end @implementation AdblockService @@ -32,75 +29,66 @@ - (instancetype)initWithComponentUpdater: return self; } -- (void)registerDefaultShieldsComponent { - __weak auto weakSelf = self; - brave_shields::RegisterAdBlockIosDefaultDatComponent( +- (void)registerDefaultComponent: + (void (^)(NSString* _Nullable installPath))componentReady { + brave_shields::RegisterAdBlockDefaultComponent( _cus, base::BindRepeating(^(const base::FilePath& install_path) { - [weakSelf adblockComponentDidBecomeReady:install_path]; + const auto installPath = base::SysUTF8ToNSString(install_path.value()); + componentReady(installPath); })); } -- (void)adblockComponentDidBecomeReady:(base::FilePath)install_path { - // Update shields install path (w/ KVO) - [self willChangeValueForKey:@"shieldsInstallPath"]; - self.shieldsInstallPath = base::SysUTF8ToNSString(install_path.value()); - [self didChangeValueForKey:@"shieldsInstallPath"]; +- (void)registerResourceComponent: + (void (^)(NSString* _Nullable installPath))componentReady { + brave_shields::RegisterAdBlockDefaultResourceComponent( + _cus, base::BindRepeating(^(const base::FilePath& install_path) { + const auto installPath = base::SysUTF8ToNSString(install_path.value()); + componentReady(installPath); + })); +} - __weak auto weakSelf = self; - // Get filter lists from catalog - base::ThreadPool::PostTaskAndReplyWithResult( - FROM_HERE, {base::MayBlock()}, - base::BindOnce(&brave_component_updater::GetDATFileAsString, - install_path.AppendASCII("regional_catalog.json")), - base::BindOnce(^(const std::string& json) { - auto strongSelf = weakSelf; - if (!strongSelf) { - return; - } - auto catalog = brave_shields::FilterListCatalogFromJSON(json); +- (void)registerFilterListCatalogComponent: + (void (^)(NSArray* filterLists)) + componentReady { + brave_shields::RegisterAdBlockFilterListCatalogComponent( + _cus, base::BindRepeating(^(const base::FilePath& install_path) { + // Get filter lists from catalog + base::ThreadPool::PostTaskAndReplyWithResult( + FROM_HERE, {base::MayBlock()}, + base::BindOnce(&brave_component_updater::GetDATFileAsString, + install_path.AppendASCII("regional_catalog.json")), + base::BindOnce(^(const std::string& json) { + // Parse data + auto catalog = brave_shields::FilterListCatalogFromJSON(json); - NSMutableArray* lists = [[NSMutableArray alloc] init]; - for (const auto& entry : catalog) { - [lists - addObject:[[AdblockFilterListCatalogEntry alloc] - initWithFilterListCatalogEntry: - brave_shields::FilterListCatalogEntry(entry)]]; - } - strongSelf.regionalFilterLists = lists; + NSMutableArray* lists = [[NSMutableArray alloc] init]; + for (const auto& entry : catalog) { + [lists addObject:[[AdblockFilterListCatalogEntry alloc] + initWithFilterListCatalogEntry: + brave_shields::FilterListCatalogEntry( + entry)]]; + } - if (strongSelf.shieldsComponentReady) { - strongSelf.shieldsComponentReady(strongSelf.shieldsInstallPath); - } + componentReady(lists); + })); })); } - (void)registerFilterListComponent:(AdblockFilterListCatalogEntry*)entry - useLegacyComponent:(bool)useLegacyComponent componentReady:(void (^)(NSString* _Nullable installPath)) componentReady { - std::string base64PublicKey = base::SysNSStringToUTF8(entry.base64PublicKey); - std::string componentId = base::SysNSStringToUTF8(entry.componentId); - - if (useLegacyComponent) { - base64PublicKey = base::SysNSStringToUTF8(entry.iosBase64PublicKey); - componentId = base::SysNSStringToUTF8(entry.iosComponentId); - } - brave_shields::RegisterAdBlockFiltersComponent( - _cus, base64PublicKey, componentId, base::SysNSStringToUTF8(entry.title), + _cus, base::SysNSStringToUTF8(entry.base64PublicKey), + base::SysNSStringToUTF8(entry.componentId), + base::SysNSStringToUTF8(entry.title), base::BindRepeating(^(const base::FilePath& install_path) { const auto installPath = base::SysUTF8ToNSString(install_path.value()); componentReady(installPath); })); } -- (void)unregisterFilterListComponent:(AdblockFilterListCatalogEntry*)entry - useLegacyComponent:(bool)useLegacyComponent { - if (useLegacyComponent) { - _cus->UnregisterComponent(base::SysNSStringToUTF8(entry.iosComponentId)); - } else { - _cus->UnregisterComponent(base::SysNSStringToUTF8(entry.componentId)); - } +- (void)unregisterFilterListComponent:(AdblockFilterListCatalogEntry*)entry { + _cus->UnregisterComponent(base::SysNSStringToUTF8(entry.componentId)); } @end