From 52de133f686207a685c6479c37ec3e32730abbe7 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Wed, 18 Oct 2023 20:38:25 +0000 Subject: [PATCH] Uplift of #20557 (squashed) to beta --- .../browser/ad_block_component_installer.cc | 19 ++++++++++ .../browser/ad_block_component_installer.h | 1 + .../ad_block_default_resource_provider.cc | 35 +++++++++++++++---- .../ad_block_default_resource_provider.h | 2 ++ .../brave_shields/browser/ad_block_service.cc | 14 ++++++++ .../brave_shields/browser/ad_block_service.h | 2 ++ 6 files changed, 67 insertions(+), 6 deletions(-) diff --git a/components/brave_shields/browser/ad_block_component_installer.cc b/components/brave_shields/browser/ad_block_component_installer.cc index b89ee0d6bf02..1ba326b98768 100644 --- a/components/brave_shields/browser/ad_block_component_installer.cc +++ b/components/brave_shields/browser/ad_block_component_installer.cc @@ -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" @@ -199,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 db3c43e2383a..0212a278d6f8 100644 --- a/components/brave_shields/browser/ad_block_component_installer.h +++ b/components/brave_shields/browser/ad_block_component_installer.h @@ -27,6 +27,7 @@ void RegisterAdBlockDefaultComponent( 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_service.cc b/components/brave_shields/browser/ad_block_service.cc index 59f2e2cee4c2..41935768fbc5 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"; @@ -469,6 +474,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 4e2f79029e69..5ae72acd2c5b 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);