Skip to content

Commit

Permalink
Adjusted some adblock components update check interval (uplift to 1.6…
Browse files Browse the repository at this point in the history
…0.x) (#20591)

* Fix #32799: Use correct components for ad-blocking (#20137)

* Uplift of #20557 (squashed) to beta

---------

Co-authored-by: Jacob Sikorski <jacob.sikorski@gmail.com>
  • Loading branch information
brave-builds and cuba authored Oct 24, 2023
1 parent 8e4c4ca commit ba8511c
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 129 deletions.
2 changes: 1 addition & 1 deletion browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
57 changes: 37 additions & 20 deletions components/brave_shields/browser/ad_block_component_installer.cc
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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 @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -179,11 +181,10 @@ void RegisterAdBlockIosDefaultDatComponent(

auto installer = base::MakeRefCounted<component_updater::ComponentInstaller>(
std::make_unique<AdBlockComponentInstallerPolicy>(
kAdBlockIosDefaultDatComponentBase64PublicKey,
kAdBlockIosDefaultDatComponentId, kAdBlockIosDefaultDatComponentName,
callback));
kAdBlockDefaultComponentBase64PublicKey, kAdBlockDefaultComponentId,
kAdBlockDefaultComponentName, callback));
installer->Register(
cus, base::BindOnce(&OnRegistered, kAdBlockIosDefaultDatComponentId));
cus, base::BindOnce(&OnRegistered, kAdBlockDefaultComponentId));
}

void RegisterAdBlockDefaultResourceComponent(
Expand All @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions components/brave_shields/browser/ad_block_component_installer.h
Original file line number Diff line number Diff line change
@@ -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_
Expand All @@ -20,13 +20,14 @@ namespace brave_shields {
using OnComponentReadyCallback =
base::RepeatingCallback<void(const base::FilePath& install_path)>;

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,
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> languages({"fr", "fR", "fr-FR", "fr-ca"});
std::for_each(
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 @@ -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,
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
12 changes: 2 additions & 10 deletions components/brave_shields/browser/filter_list_catalog_entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,15 @@ 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),
langs(langs),
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;
Expand All @@ -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<FilterListCatalogEntry>::const_iterator FindAdBlockFilterListByUUID(
Expand Down
6 changes: 1 addition & 5 deletions components/brave_shields/browser/filter_list_catalog_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<FilterListCatalogEntry>*);
Expand Down
2 changes: 0 additions & 2 deletions ios/app/brave_core_main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ - (void)registerComponentsForUpdate:

RegisterSafetyTipsComponent(cus);
brave_wallet::RegisterWalletDataFilesComponent(cus);

[self.adblockService registerDefaultShieldsComponent];
}

+ (void)setLogHandler:(BraveCoreLogHandler)logHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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
Loading

0 comments on commit ba8511c

Please sign in to comment.