Skip to content

Commit

Permalink
Uplift of #20557 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds authored and bsclifton committed Oct 21, 2023
1 parent 5725190 commit bc0fd8f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
19 changes: 19 additions & 0 deletions components/brave_shields/browser/ad_block_component_installer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void RegisterAdBlockDefaultComponent(
void RegisterAdBlockDefaultResourceComponent(
component_updater::ComponentUpdateService* cus,
OnComponentReadyCallback callback);
void CheckAdBlockComponentsUpdate();

void RegisterAdBlockFilterListCatalogComponent(
component_updater::ComponentUpdateService* cus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,46 @@
#include <string>
#include <utility>

#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<int> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>

#include "base/functional/callback.h"
#include "base/timer/timer.h"
#include "brave/components/brave_shields/browser/ad_block_resource_provider.h"

namespace component_updater {
Expand Down Expand Up @@ -38,6 +39,7 @@ class AdBlockDefaultResourceProvider : public AdBlockResourceProvider {
void OnComponentReady(const base::FilePath&);

base::FilePath component_path_;
base::RepeatingTimer update_check_timer_;

base::WeakPtrFactory<AdBlockDefaultResourceProvider> weak_factory_{this};
};
Expand Down
14 changes: 14 additions & 0 deletions components/brave_shields/browser/ad_block_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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";
Expand Down Expand Up @@ -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(
const std::string& component_id,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_shields/browser/ad_block_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ class AdBlockService {
base::WeakPtrFactory<AdBlockService> weak_factory_{this};
};

void CheckAdBlockExceptionComponentsUpdate();

// Registers the local_state preferences used by Adblock
void RegisterPrefsForAdBlockService(PrefRegistrySimple* registry);

Expand Down

0 comments on commit bc0fd8f

Please sign in to comment.