Skip to content

Commit

Permalink
Merge pull request #8124 from brave/adblock-custom-subscriptions
Browse files Browse the repository at this point in the history
Support custom filter list subscriptions in brave://adblock
  • Loading branch information
bridiver authored Sep 1, 2021
2 parents bf3b1cb + d189f95 commit db5da23
Show file tree
Hide file tree
Showing 63 changed files with 2,834 additions and 144 deletions.
16 changes: 11 additions & 5 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/bind.h"
#include "base/path_service.h"
#include "base/task/post_task.h"
#include "brave/browser/brave_shields/ad_block_subscription_download_manager_getter.h"
#include "brave/browser/brave_stats/brave_stats_updater.h"
#include "brave/browser/component_updater/brave_component_updater_configurator.h"
#include "brave/browser/component_updater/brave_component_updater_delegate.h"
Expand All @@ -27,6 +28,7 @@
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "brave/components/brave_sync/network_time_helper.h"
#include "brave/components/ntp_background_images/browser/features.h"
Expand Down Expand Up @@ -195,11 +197,15 @@ void BraveBrowserProcessImpl::StartBraveServices() {
}

brave_shields::AdBlockService* BraveBrowserProcessImpl::ad_block_service() {
if (ad_block_service_)
return ad_block_service_.get();

ad_block_service_ =
brave_shields::AdBlockServiceFactory(brave_component_updater_delegate());
if (!ad_block_service_) {
ad_block_service_ = std::make_unique<brave_shields::AdBlockService>(
brave_component_updater_delegate(),
std::make_unique<brave_shields::AdBlockSubscriptionServiceManager>(
brave_component_updater_delegate(),
AdBlockSubscriptionDownloadManagerGetter(),
profile_manager()->user_data_dir().Append(
profile_manager()->GetInitialProfileDir())));
}
return ad_block_service_.get();
}

Expand Down
298 changes: 272 additions & 26 deletions browser/brave_shields/ad_block_service_browsertest.cc

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions browser/brave_shields/ad_block_service_browsertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AdBlockServiceTest : public extensions::ExtensionBrowserTest {
int expected_change = 1);
bool InstallRegionalAdBlockExtension(const std::string& uuid);
bool StartAdBlockRegionalServices();
void SetSubscriptionIntervals();
void WaitForAdBlockServiceThreads();
void WaitForBraveExtensionShieldsDataReady();
void ShieldsDown(const GURL& url);
Expand Down
121 changes: 121 additions & 0 deletions browser/brave_shields/ad_block_subscription_download_manager_getter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* 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/. */

#include "brave/browser/brave_shields/ad_block_subscription_download_manager_getter.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "brave/browser/brave_browser_process.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/background_download_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "components/keyed_service/core/simple_dependency_manager.h"
#include "components/keyed_service/core/simple_keyed_service_factory.h"
#include "content/public/browser/browser_context.h"

using brave_shields::AdBlockSubscriptionDownloadManager;

namespace {

class AdBlockSubscriptionDownloadManagerFactory
: public SimpleKeyedServiceFactory {
public:
static AdBlockSubscriptionDownloadManagerFactory* GetInstance() {
return base::Singleton<AdBlockSubscriptionDownloadManagerFactory>::get();
}

static AdBlockSubscriptionDownloadManager* GetForKey(SimpleFactoryKey* key) {
return static_cast<AdBlockSubscriptionDownloadManager*>(
GetInstance()->GetServiceForKey(key, true));
}

private:
friend struct base::DefaultSingletonTraits<
AdBlockSubscriptionDownloadManagerFactory>;

AdBlockSubscriptionDownloadManagerFactory()
: SimpleKeyedServiceFactory("AdBlockSubscriptionDownloadManagerFactory",
SimpleDependencyManager::GetInstance()) {
DependsOn(BackgroundDownloadServiceFactory::GetInstance());
}

~AdBlockSubscriptionDownloadManagerFactory() override = default;

// SimpleKeyedServiceFactory overrides:
std::unique_ptr<KeyedService> BuildServiceInstanceFor(
SimpleFactoryKey* key) const override {
return std::make_unique<AdBlockSubscriptionDownloadManager>(
BackgroundDownloadServiceFactory::GetForKey(key),
base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}));
}

SimpleFactoryKey* GetKeyToUse(SimpleFactoryKey* key) const override {
return key;
}

DISALLOW_COPY_AND_ASSIGN(AdBlockSubscriptionDownloadManagerFactory);
};

class AdBlockSubscriptionDownloadManagerGetterImpl
: public ProfileManagerObserver {
public:
AdBlockSubscriptionDownloadManagerGetterImpl(
base::OnceCallback<void(AdBlockSubscriptionDownloadManager*)> callback)
: callback_(std::move(callback)) {
auto* profile_manager = g_browser_process->profile_manager();
auto* profile = profile_manager->GetProfileByPath(
profile_manager->user_data_dir().Append(
profile_manager->GetInitialProfileDir()));
if (profile) {
GetDownloadManager(profile);
} else {
g_browser_process->profile_manager()->AddObserver(this);
}
}

private:
void OnProfileAdded(Profile* profile) override {
auto* profile_manager = g_browser_process->profile_manager();
if (profile->GetPath() == profile_manager->user_data_dir().Append(
profile_manager->GetInitialProfileDir())) {
profile_manager->RemoveObserver(this);
GetDownloadManager(profile);
}
}

void GetDownloadManager(Profile* profile) {
auto* download_manager =
AdBlockSubscriptionDownloadManagerFactory::GetInstance()->GetForKey(
profile->GetProfileKey());
std::move(callback_).Run(download_manager);
delete this;
}

base::OnceCallback<void(AdBlockSubscriptionDownloadManager*)> callback_;
};

} // namespace

// static
AdBlockSubscriptionDownloadManager::DownloadManagerGetter
AdBlockSubscriptionDownloadManagerGetter() {
return base::BindOnce(
[](base::OnceCallback<void(AdBlockSubscriptionDownloadManager*)>
callback) {
// self deletes when the download manager is retrieved
new AdBlockSubscriptionDownloadManagerGetterImpl(std::move(callback));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* 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/. */

#ifndef BRAVE_BROWSER_BRAVE_SHIELDS_AD_BLOCK_SUBSCRIPTION_DOWNLOAD_MANAGER_GETTER_H_
#define BRAVE_BROWSER_BRAVE_SHIELDS_AD_BLOCK_SUBSCRIPTION_DOWNLOAD_MANAGER_GETTER_H_

#include "brave/components/brave_shields/browser/ad_block_subscription_download_manager.h"

using brave_shields::AdBlockSubscriptionDownloadManager;

AdBlockSubscriptionDownloadManager::DownloadManagerGetter
AdBlockSubscriptionDownloadManagerGetter();

#endif // BRAVE_BROWSER_BRAVE_SHIELDS_AD_BLOCK_SUBSCRIPTION_DOWNLOAD_MANAGER_GETTER_H_
2 changes: 2 additions & 0 deletions browser/brave_shields/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import("//extensions/buildflags/buildflags.gni")
brave_browser_brave_shields_sources = [
"//brave/browser/brave_shields/ad_block_pref_service_factory.cc",
"//brave/browser/brave_shields/ad_block_pref_service_factory.h",
"//brave/browser/brave_shields/ad_block_subscription_download_manager_getter.cc",
"//brave/browser/brave_shields/ad_block_subscription_download_manager_getter.h",
"//brave/browser/brave_shields/brave_shields_web_contents_observer.cc",
"//brave/browser/brave_shields/brave_shields_web_contents_observer.h",
"//brave/browser/brave_shields/cookie_pref_service_factory.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace brave {

BraveComponentUpdaterDelegate::BraveComponentUpdaterDelegate()
: task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
{base::MayBlock(), base::TaskPriority::USER_BLOCKING,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})) {}

BraveComponentUpdaterDelegate::~BraveComponentUpdaterDelegate() {}
Expand Down
21 changes: 18 additions & 3 deletions browser/net/brave_ad_block_tp_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#include <string>
#include <utility>

#include "base/path_service.h"
#include "base/threading/thread_task_runner_handle.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/net/url_context.h"
#include "brave/common/network_constants.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_subscription_download_manager.h"
#include "brave/components/brave_shields/browser/ad_block_subscription_service_manager.h"
#include "brave/test/base/testing_brave_browser_process.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/test/browser_task_environment.h"
#include "net/base/net_errors.h"
#include "net/dns/mock_host_resolver.h"
Expand Down Expand Up @@ -60,21 +64,32 @@ class TestingBraveComponentUpdaterDelegate : public BraveComponent::Delegate {

const std::string locale() const override { return "en"; }
PrefService* local_state() override {
NOTREACHED();
return nullptr;
}
};

} // namespace

void FakeAdBlockSubscriptionDownloadManagerGetter(
base::OnceCallback<
void(brave_shields::AdBlockSubscriptionDownloadManager*)>) {
// no-op, subscription services are not currently used in unit tests
}

class BraveAdBlockTPNetworkDelegateHelperTest : public testing::Test {
protected:
void SetUp() override {
brave_component_updater_delegate_ =
std::make_unique<TestingBraveComponentUpdaterDelegate>();

auto adblock_service = brave_shields::AdBlockServiceFactory(
brave_component_updater_delegate_.get());
base::FilePath user_data_dir;
DCHECK(base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
auto adblock_service = std::make_unique<brave_shields::AdBlockService>(
brave_component_updater_delegate_.get(),
std::make_unique<brave_shields::AdBlockSubscriptionServiceManager>(
brave_component_updater_delegate_.get(),
base::BindOnce(&FakeAdBlockSubscriptionDownloadManagerGetter),
user_data_dir));

TestingBraveBrowserProcess::GetGlobal()->SetAdBlockService(
std::move(adblock_service));
Expand Down
Loading

0 comments on commit db5da23

Please sign in to comment.