Skip to content

Commit

Permalink
Implement debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgrim-brave committed Jul 2, 2021
1 parent 7bb1de4 commit 0f2c732
Show file tree
Hide file tree
Showing 30 changed files with 870 additions and 31 deletions.
5 changes: 5 additions & 0 deletions browser/brave_browser_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class GreaselionDownloadService;
#endif
} // namespace greaselion

namespace debounce {
class DebounceDownloadService;
} // namespace debounce

namespace ntp_background_images {
class NTPBackgroundImagesService;
} // namespace ntp_background_images
Expand Down Expand Up @@ -86,6 +90,7 @@ class BraveBrowserProcess {
virtual greaselion::GreaselionDownloadService*
greaselion_download_service() = 0;
#endif
virtual debounce::DebounceDownloadService* debounce_download_service() = 0;
virtual brave_shields::HTTPSEverywhereService* https_everywhere_service() = 0;
virtual brave_component_updater::LocalDataFilesService*
local_data_files_service() = 0;
Expand Down
33 changes: 22 additions & 11 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "brave/components/brave_sync/buildflags/buildflags.h"
#include "brave/components/brave_sync/network_time_helper.h"
#include "brave/components/debounce/browser/debounce_download_service.h"
#include "brave/components/ntp_background_images/browser/features.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/p3a/brave_p3a_service.h"
Expand All @@ -48,8 +49,8 @@
#include "services/network/public/cpp/shared_url_loader_factory.h"

#if BUILDFLAG(ENABLE_SYSTEM_NOTIFICATIONS)
#include "chrome/browser/notifications/notification_platform_bridge.h"
#include "brave/browser/notifications/brave_notification_platform_bridge.h"
#include "chrome/browser/notifications/notification_platform_bridge.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_REFERRALS)
Expand Down Expand Up @@ -91,8 +92,8 @@
#endif

using brave_component_updater::BraveComponent;
using ntp_background_images::features::kBraveNTPBrandedWallpaper;
using ntp_background_images::NTPBackgroundImagesService;
using ntp_background_images::features::kBraveNTPBrandedWallpaper;

namespace {

Expand Down Expand Up @@ -140,8 +141,8 @@ void BraveBrowserProcessImpl::Init() {
content::ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
ipfs::kIPNSScheme);
#endif
brave_component_updater::BraveOnDemandUpdater::GetInstance()->
RegisterOnDemandUpdateCallback(
brave_component_updater::BraveOnDemandUpdater::GetInstance()
->RegisterOnDemandUpdateCallback(
base::BindRepeating(&component_updater::BraveOnDemandUpdate));
UpdateBraveDarkMode();
pref_change_registrar_.Add(
Expand Down Expand Up @@ -188,6 +189,7 @@ void BraveBrowserProcessImpl::StartBraveServices() {
#if BUILDFLAG(ENABLE_GREASELION)
greaselion_download_service();
#endif
debounce_download_service();
#if BUILDFLAG(ENABLE_SPEEDREADER)
speedreader_rewriter_service();
#endif
Expand All @@ -198,8 +200,8 @@ void BraveBrowserProcessImpl::StartBraveServices() {
local_data_files_service()->Start();

#if BUILDFLAG(ENABLE_BRAVE_SYNC)
brave_sync::NetworkTimeHelper::GetInstance()
->SetNetworkTimeTracker(g_browser_process->network_time_tracker());
brave_sync::NetworkTimeHelper::GetInstance()->SetNetworkTimeTracker(
g_browser_process->network_time_tracker());
#endif
}

Expand Down Expand Up @@ -260,6 +262,16 @@ BraveBrowserProcessImpl::greaselion_download_service() {
}
#endif

debounce::DebounceDownloadService*
BraveBrowserProcessImpl::debounce_download_service() {
if (!debounce_download_service_) {
debounce_download_service_ =
std::make_unique<debounce::DebounceDownloadService>(
local_data_files_service());
}
return debounce_download_service_.get();
}

brave_shields::HTTPSEverywhereService*
BraveBrowserProcessImpl::https_everywhere_service() {
if (!https_everywhere_service_)
Expand Down Expand Up @@ -288,8 +300,7 @@ void BraveBrowserProcessImpl::OnBraveDarkModeChanged() {
}

#if BUILDFLAG(ENABLE_TOR)
tor::BraveTorClientUpdater*
BraveBrowserProcessImpl::tor_client_updater() {
tor::BraveTorClientUpdater* BraveBrowserProcessImpl::tor_client_updater() {
if (tor_client_updater_)
return tor_client_updater_.get();

Expand All @@ -305,7 +316,8 @@ void BraveBrowserProcessImpl::OnTorEnabledChanged() {
// Update all browsers' tor command status.
for (Browser* browser : *BrowserList::GetInstance()) {
static_cast<chrome::BraveBrowserCommandController*>(
browser->command_controller())->UpdateCommandForTor();
browser->command_controller())
->UpdateCommandForTor();
}
}
#endif
Expand Down Expand Up @@ -406,8 +418,7 @@ brave_ads::ResourceComponent* BraveBrowserProcessImpl::resource_component() {
#endif // BUILDFLAG(BRAVE_ADS_ENABLED)

#if BUILDFLAG(IPFS_ENABLED)
ipfs::BraveIpfsClientUpdater*
BraveBrowserProcessImpl::ipfs_client_updater() {
ipfs::BraveIpfsClientUpdater* BraveBrowserProcessImpl::ipfs_client_updater() {
if (ipfs_client_updater_)
return ipfs_client_updater_.get();

Expand Down
6 changes: 6 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class GreaselionDownloadService;
#endif
} // namespace greaselion

namespace debounce {
class DebounceDownloadService;
} // namespace debounce

namespace ntp_background_images {
class NTPBackgroundImagesService;
} // namespace ntp_background_images
Expand Down Expand Up @@ -97,6 +101,7 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess,
#if BUILDFLAG(ENABLE_GREASELION)
greaselion::GreaselionDownloadService* greaselion_download_service() override;
#endif
debounce::DebounceDownloadService* debounce_download_service() override;
brave_shields::HTTPSEverywhereService* https_everywhere_service() override;
brave_component_updater::LocalDataFilesService* local_data_files_service()
override;
Expand Down Expand Up @@ -152,6 +157,7 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess,
std::unique_ptr<greaselion::GreaselionDownloadService>
greaselion_download_service_;
#endif
std::unique_ptr<debounce::DebounceDownloadService> debounce_download_service_;
std::unique_ptr<brave_shields::HTTPSEverywhereService>
https_everywhere_service_;
std::unique_ptr<brave_stats::BraveStatsUpdater> brave_stats_updater_;
Expand Down
12 changes: 12 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "brave/browser/brave_browser_main_extra_parts.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/brave_shields/brave_shields_web_contents_observer.h"
#include "brave/browser/debounce/debounce_service_factory.h"
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
#include "brave/browser/net/brave_proxying_url_loader_factory.h"
#include "brave/browser/net/brave_proxying_web_socket.h"
Expand All @@ -41,6 +42,7 @@
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/cosmetic_filters/browser/cosmetic_filters_resources.h"
#include "brave/components/cosmetic_filters/common/cosmetic_filters.mojom.h"
#include "brave/components/debounce/browser/debounce_throttle.h"
#include "brave/components/decentralized_dns/buildflags/buildflags.h"
#include "brave/components/ftx/browser/buildflags/buildflags.h"
#include "brave/components/gemini/browser/buildflags/buildflags.h"
Expand Down Expand Up @@ -475,6 +477,16 @@ BraveContentBrowserClient::CreateURLLoaderThrottles(
result.push_back(std::move(throttle));
}
#endif // ENABLE_SPEEDREADER

auto* settings_map = HostContentSettingsMapFactory::GetForProfile(
Profile::FromBrowserContext(browser_context));
if (std::unique_ptr<blink::URLLoaderThrottle> debounce_throttle =
debounce::DebounceThrottle::MaybeCreateThrottleFor(
debounce::DebounceServiceFactory::GetForBrowserContext(
browser_context),
settings_map))
result.push_back(std::move(debounce_throttle));

return result;
}

Expand Down
2 changes: 2 additions & 0 deletions browser/browser_context_keyed_service_factories.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_shields/ad_block_pref_service_factory.h"
#include "brave/browser/brave_shields/cookie_pref_service_factory.h"
#include "brave/browser/debounce/debounce_service_factory.h"
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
#include "brave/browser/ntp_background_images/view_counter_service_factory.h"
#include "brave/browser/permissions/permission_lifetime_manager_factory.h"
Expand Down Expand Up @@ -58,6 +59,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
#endif
brave_shields::AdBlockPrefServiceFactory::GetInstance();
brave_shields::CookiePrefServiceFactory::GetInstance();
debounce::DebounceServiceFactory::GetInstance();
#if BUILDFLAG(ENABLE_GREASELION)
greaselion::GreaselionServiceFactory::GetInstance();
#endif
Expand Down
54 changes: 54 additions & 0 deletions browser/debounce/debounce_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* 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/debounce/debounce_service_factory.h"

#include <memory>
#include <string>

#include "base/memory/singleton.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/components/debounce/browser/debounce_service.h"
#include "brave/components/debounce/browser/debounce_service_impl.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"

namespace debounce {

// static
DebounceServiceFactory* DebounceServiceFactory::GetInstance() {
return base::Singleton<DebounceServiceFactory>::get();
}

DebounceService* DebounceServiceFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<DebounceService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

DebounceServiceFactory::DebounceServiceFactory()
: BrowserContextKeyedServiceFactory(
"DebounceService",
BrowserContextDependencyManager::GetInstance()) {}

DebounceServiceFactory::~DebounceServiceFactory() = default;

KeyedService* DebounceServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
debounce::DebounceDownloadService* download_service = nullptr;
// Brave browser process may be null if we are being created within a unit
// test.
if (g_brave_browser_process)
download_service = g_brave_browser_process->debounce_download_service();
std::unique_ptr<DebounceServiceImpl> debounce_service(
new DebounceServiceImpl(download_service));
return debounce_service.release();
}

bool DebounceServiceFactory::ServiceIsNULLWhileTesting() const {
return false;
}

} // namespace debounce
42 changes: 42 additions & 0 deletions browser/debounce/debounce_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* 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_DEBOUNCE_DEBOUNCE_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_DEBOUNCE_DEBOUNCE_SERVICE_FACTORY_H_

#include "base/macros.h"
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"

class Profile;

namespace debounce {

class DebounceService;

class DebounceServiceFactory : public BrowserContextKeyedServiceFactory {
public:
static DebounceService* GetForBrowserContext(
content::BrowserContext* context);
static DebounceServiceFactory* GetInstance();

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

DebounceServiceFactory();
~DebounceServiceFactory() override;

// BrowserContextKeyedServiceFactory:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
bool ServiceIsNULLWhileTesting() const override;

DebounceServiceFactory(const DebounceServiceFactory&) = delete;
DebounceServiceFactory& operator=(const DebounceServiceFactory&) = delete;
};

} // namespace debounce

#endif // BRAVE_BROWSER_DEBOUNCE_DEBOUNCE_SERVICE_FACTORY_H_
13 changes: 13 additions & 0 deletions browser/debounce/sources.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
brave_browser_debounce_sources = [
"//brave/browser/debounce/debounce_service_factory.cc",
"//brave/browser/debounce/debounce_service_factory.h",
]

brave_browser_debounce_deps = [
"//base",
"//brave/components/debounce/browser",
"//brave/components/debounce/common",
"//brave/extensions:common",
"//chrome/common",
"//components/keyed_service/content",
]
6 changes: 0 additions & 6 deletions browser/extensions/brave_base_local_data_files_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ void BaseLocalDataFilesBrowserTest::GetTestDataDir(
}

void BaseLocalDataFilesBrowserTest::MaybeInitEmbeddedTestServer() {
if (!embedded_test_server_directory())
return;

base::FilePath test_data_dir;
GetTestDataDir(&test_data_dir);
test_data_dir = test_data_dir.AppendASCII(embedded_test_server_directory());
Expand All @@ -57,9 +54,6 @@ void BaseLocalDataFilesBrowserTest::MaybeInitEmbeddedTestServer() {
}

void BaseLocalDataFilesBrowserTest::MaybeSetUpEmbeddedTestServerOnMainThread() {
if (!embedded_test_server_directory())
return;

host_resolver()->AddRule("*", "127.0.0.1");
}

Expand Down
3 changes: 3 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import("//brave/browser/brave_stats/sources.gni")
import("//brave/browser/browsing_data/sources.gni")
import("//brave/browser/component_updater/sources.gni")
import("//brave/browser/crypto_dot_com/sources.gni")
import("//brave/browser/debounce/sources.gni")
import("//brave/browser/decentralized_dns/sources.gni")
import("//brave/browser/download/sources.gni")
import("//brave/browser/ethereum_remote_client/buildflags/buildflags.gni")
Expand Down Expand Up @@ -337,6 +338,7 @@ brave_chrome_browser_sources += brave_browser_browsing_data_sources
brave_chrome_browser_sources += brave_browser_component_updater_sources
brave_chrome_browser_sources += brave_browser_crypto_dot_com_sources
brave_chrome_browser_sources += brave_browser_decentralized_dns_sources
brave_chrome_browser_sources += brave_browser_debounce_sources
brave_chrome_browser_sources += brave_browser_download_sources
brave_chrome_browser_sources += brave_browser_gemini_sources
brave_chrome_browser_sources += brave_browser_greaselion_sources
Expand All @@ -356,6 +358,7 @@ brave_chrome_browser_deps += brave_browser_brave_stats_updater_deps
brave_chrome_browser_deps += brave_browser_browsing_data_deps
brave_chrome_browser_deps += brave_browser_component_updater_deps
brave_chrome_browser_deps += brave_browser_crypto_dot_com_deps
brave_chrome_browser_deps += brave_browser_debounce_deps
brave_chrome_browser_deps += brave_browser_decentralized_dns_deps
brave_chrome_browser_deps += brave_browser_download_deps
brave_chrome_browser_deps += brave_browser_gemini_deps
Expand Down
12 changes: 9 additions & 3 deletions chromium_src/chrome/browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/components/brave_shields/common/features.h"
#include "brave/components/brave_sync/buildflags/buildflags.h"
#include "brave/components/brave_wallet/common/buildflags/buildflags.h"
#include "brave/components/debounce/common/features.h"
#include "brave/components/decentralized_dns/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ntp_background_images/browser/features.h"
Expand All @@ -31,6 +32,7 @@ using brave_shields::features::kBraveAdblockCosmeticFilteringNative;
using brave_shields::features::kBraveAdblockCspRules;
using brave_shields::features::kBraveDomainBlock;
using brave_shields::features::kBraveExtensionNetworkBlocking;
using debounce::features::kBraveDebounce;
using ntp_background_images::features::kBraveNTPBrandedWallpaper;
using ntp_background_images::features::kBraveNTPBrandedWallpaperDemo;
using ntp_background_images::features::kBraveNTPSuperReferralWallpaper;
Expand Down Expand Up @@ -153,10 +155,14 @@ using ntp_background_images::features::kBraveNTPSuperReferralWallpaper;
flag_descriptions::kBraveAdblockCspRulesName, \
flag_descriptions::kBraveAdblockCspRulesDescription, kOsAll, \
FEATURE_VALUE_TYPE(kBraveAdblockCspRules)}, \
{"brave-debounce", \
flag_descriptions::kBraveDebounceName, \
flag_descriptions::kBraveDebounceDescription, kOsAll, \
FEATURE_VALUE_TYPE(kBraveDebounce)}, \
{"brave-domain-block", \
flag_descriptions::kBraveDomainBlockName, \
flag_descriptions::kBraveDomainBlockDescription, kOsAll, \
FEATURE_VALUE_TYPE(kBraveDomainBlock)}, \
flag_descriptions::kBraveDomainBlockName, \
flag_descriptions::kBraveDomainBlockDescription, kOsAll, \
FEATURE_VALUE_TYPE(kBraveDomainBlock)}, \
{"brave-extension-network-blocking", \
flag_descriptions::kBraveExtensionNetworkBlockingName, \
flag_descriptions::kBraveExtensionNetworkBlockingDescription, kOsAll, \
Expand Down
Loading

0 comments on commit 0f2c732

Please sign in to comment.