From 17760b88b01826628abccc10b490a341db640b9f Mon Sep 17 00:00:00 2001 From: bridiver <34129+bridiver@users.noreply.github.com> Date: Sat, 5 Oct 2024 15:48:07 -0700 Subject: [PATCH] remove //brave/components/brave_ads/browser check_includes = false --- browser/brave_ads/ads_service_delegate.cc | 135 +++++++++++++ browser/brave_ads/ads_service_delegate.h | 78 ++++++++ browser/brave_ads/ads_service_factory.cc | 15 +- .../background_helper/background_helper.cc | 2 +- .../background_helper_android.h | 2 +- .../background_helper_holder.cc | 2 +- .../background_helper_linux.h | 2 +- .../background_helper/background_helper_mac.h | 2 +- .../background_helper/background_helper_win.h | 2 +- .../notification_helper_impl_android.cc | 2 +- ...search_result_ad_tab_helper_browsertest.cc | 2 +- browser/brave_ads/sources.gni | 3 +- .../tabs/ads_tab_helper_browsertest.cc | 2 +- browser/widevine/BUILD.gn | 9 +- components/brave_ads/browser/BUILD.gn | 20 +- components/brave_ads/browser/DEPS | 33 +--- components/brave_ads/browser/ads_service.cc | 2 +- components/brave_ads/browser/ads_service.h | 36 +++- .../brave_ads/browser/ads_service_impl.cc | 187 ++++++------------ .../brave_ads/browser/ads_service_impl.h | 21 +- .../brave_ads/browser/ads_service_mock.cc | 2 +- .../brave_ads/browser/ads_service_mock.h | 2 +- .../browser/application_state/BUILD.gn | 10 + .../application_state}/background_helper.h | 6 +- ...ative_search_result_ad_handler_unittest.cc | 2 +- .../browser/view_counter_service_unittest.cc | 2 +- 26 files changed, 381 insertions(+), 200 deletions(-) create mode 100644 browser/brave_ads/ads_service_delegate.cc create mode 100644 browser/brave_ads/ads_service_delegate.h create mode 100644 components/brave_ads/browser/application_state/BUILD.gn rename {browser/brave_ads/application_state/background_helper => components/brave_ads/browser/application_state}/background_helper.h (81%) diff --git a/browser/brave_ads/ads_service_delegate.cc b/browser/brave_ads/ads_service_delegate.cc new file mode 100644 index 000000000000..38fa1ec7fb40 --- /dev/null +++ b/browser/brave_ads/ads_service_delegate.cc @@ -0,0 +1,135 @@ +/* Copyright (c) 2024 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/browser/brave_ads/ads_service_delegate.h" + +#include + +#include "brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h" +#include "brave/browser/brave_ads/application_state/notification_helper/notification_helper.h" +#include "brave/browser/ui/brave_ads/notification_ad.h" +#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h" +#include "build/build_config.h" +#include "chrome/browser/notifications/notification_display_service.h" +#include "chrome/browser/profiles/profile.h" +#if BUILDFLAG(IS_ANDROID) +#include "brave/browser/notifications/brave_notification_platform_bridge_helper_android.h" +#include "chrome/browser/android/service_tab_launcher.h" +#include "content/public/browser/page_navigator.h" +#else +#include "chrome/browser/fullscreen.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/browser_navigator.h" +#endif + +namespace brave_ads { + +AdsServiceDelegate::AdsServiceDelegate( + Profile* profile, + brave_adaptive_captcha::BraveAdaptiveCaptchaService* + adaptive_captcha_service, + NotificationDisplayService* notification_display_service, + std::unique_ptr + notification_ad_platform_bridge) + : profile_(profile), + adaptive_captcha_service_(adaptive_captcha_service), + notification_display_service_(notification_display_service), + notification_ad_platform_bridge_( + std::move(notification_ad_platform_bridge)) {} + +AdsServiceDelegate::~AdsServiceDelegate() {} + +void AdsServiceDelegate::OpenNewTabWithUrl(const GURL& url) { +#if BUILDFLAG(IS_ANDROID) + // ServiceTabLauncher can currently only launch new tabs + const content::OpenURLParams params(url, content::Referrer(), + WindowOpenDisposition::NEW_FOREGROUND_TAB, + ui::PAGE_TRANSITION_LINK, true); + ServiceTabLauncher::GetInstance()->LaunchTab( + profile_, params, base::BindOnce([](content::WebContents*) {})); +#else + Browser* browser = chrome::FindTabbedBrowser(profile_, false); + if (!browser) { + browser = Browser::Create(Browser::CreateParams(profile_, true)); + } + NavigateParams nav_params(browser, url, ui::PAGE_TRANSITION_LINK); + nav_params.disposition = WindowOpenDisposition::SINGLETON_TAB; + nav_params.window_action = NavigateParams::SHOW_WINDOW; + nav_params.path_behavior = NavigateParams::RESPECT; + Navigate(&nav_params); +#endif +} + +void AdsServiceDelegate::InitNotificationHelper() { + NotificationHelper::GetInstance()->InitForProfile(profile_); +} + +bool AdsServiceDelegate:: + CanShowSystemNotificationsWhileBrowserIsBackgrounded() { + return NotificationHelper::GetInstance() + ->CanShowSystemNotificationsWhileBrowserIsBackgrounded(); +} + +bool AdsServiceDelegate::DoesSupportSystemNotifications() { + return NotificationHelper::GetInstance()->DoesSupportSystemNotifications(); +} + +bool AdsServiceDelegate::CanShowNotifications() { + return NotificationHelper::GetInstance()->CanShowNotifications(); +} + +bool AdsServiceDelegate::ShowOnboardingNotification() { + return NotificationHelper::GetInstance()->ShowOnboardingNotification(); +} + +void AdsServiceDelegate::ShowScheduledCaptcha(const std::string& payment_id, + const std::string& captcha_id) { + adaptive_captcha_service_->ShowScheduledCaptcha(payment_id, captcha_id); +} + +void AdsServiceDelegate::ClearScheduledCaptcha() { + adaptive_captcha_service_->ClearScheduledCaptcha(); +} + +void AdsServiceDelegate::SnoozeScheduledCaptcha() { + adaptive_captcha_service_->SnoozeScheduledCaptcha(); +} + +void AdsServiceDelegate::Display( + const message_center::Notification& notification) { + notification_display_service_->Display(NotificationHandler::Type::BRAVE_ADS, + notification, nullptr); +} + +void AdsServiceDelegate::Close(const std::string& notification_id) { + notification_display_service_->Close(NotificationHandler::Type::BRAVE_ADS, + notification_id); +} + +void AdsServiceDelegate::ShowNotificationAd(const std::string& id, + const std::u16string& title, + const std::u16string& body) { + notification_ad_platform_bridge_->ShowNotificationAd( + NotificationAd(id, title, body, nullptr)); +} + +void AdsServiceDelegate::CloseNotificationAd(const std::string& id) { + notification_ad_platform_bridge_->CloseNotificationAd(id); +} + +#if BUILDFLAG(IS_ANDROID) +void AdsServiceDelegate::MaybeRegenerateNotification( + const std::string& notification_id, + const GURL& service_worker_scope) { + BraveNotificationPlatformBridgeHelperAndroid::MaybeRegenerateNotification( + placement_id, url); +} +#else +bool AdsServiceDelegate::IsFullScreenMode() { + return ::IsFullScreenMode(); +} +#endif +} // namespace brave_ads diff --git a/browser/brave_ads/ads_service_delegate.h b/browser/brave_ads/ads_service_delegate.h new file mode 100644 index 000000000000..af38aaa4e204 --- /dev/null +++ b/browser/brave_ads/ads_service_delegate.h @@ -0,0 +1,78 @@ +/* Copyright (c) 2024 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_BROWSER_BRAVE_ADS_ADS_SERVICE_DELEGATE_H_ +#define BRAVE_BROWSER_BRAVE_ADS_ADS_SERVICE_DELEGATE_H_ + +#include +#include + +#include "base/memory/raw_ptr.h" +#include "brave/components/brave_ads/browser/ads_service.h" + +class Profile; + +namespace brave_adaptive_captcha { +class BraveAdaptiveCaptchaService; +} + +class NotificationDisplayService; + +namespace brave_ads { + +class NotificationAdPlatformBridge; + +// Singleton that owns all AdsService and associates them with Profiles. +class AdsServiceDelegate : public AdsService::Delegate { + public: + explicit AdsServiceDelegate( + Profile* profile, + brave_adaptive_captcha::BraveAdaptiveCaptchaService* + adaptive_captcha_service, + NotificationDisplayService* notification_display_service, + std::unique_ptr + notification_ad_platform_bridge); + ~AdsServiceDelegate() override; + AdsServiceDelegate(const AdsServiceDelegate&) = delete; + AdsServiceDelegate& operator=(const AdsServiceDelegate&) = delete; + + AdsServiceDelegate(AdsServiceDelegate&&) noexcept = delete; + AdsServiceDelegate& operator=(AdsServiceDelegate&&) noexcept = delete; + + // AdsService::Delegate implementation + void InitNotificationHelper() override; + bool CanShowSystemNotificationsWhileBrowserIsBackgrounded() override; + bool DoesSupportSystemNotifications() override; + bool CanShowNotifications() override; + bool ShowOnboardingNotification() override; + void ShowScheduledCaptcha(const std::string& payment_id, + const std::string& captcha_id) override; + void ClearScheduledCaptcha() override; + void SnoozeScheduledCaptcha() override; + void Display(const message_center::Notification& notification) override; + void Close(const std::string& notification_id) override; + void ShowNotificationAd(const std::string& id, + const std::u16string& title, + const std::u16string& body) override; + void CloseNotificationAd(const std::string& id) override; + void OpenNewTabWithUrl(const GURL& url) override; +#if BUILDFLAG(IS_ANDROID) + void MaybeRegenerateNotification(const std::string& notification_id, + const GURL& service_worker_scope) override; +#else + bool IsFullScreenMode() override; +#endif + private: + raw_ptr profile_; + raw_ptr + adaptive_captcha_service_; + raw_ptr notification_display_service_; + std::unique_ptr + notification_ad_platform_bridge_; +}; + +} // namespace brave_ads + +#endif // BRAVE_BROWSER_BRAVE_ADS_ADS_SERVICE_DELEGATE_H_ diff --git a/browser/brave_ads/ads_service_factory.cc b/browser/brave_ads/ads_service_factory.cc index e5e23cf8c164..ad0335bf2622 100644 --- a/browser/brave_ads/ads_service_factory.cc +++ b/browser/brave_ads/ads_service_factory.cc @@ -7,6 +7,8 @@ #include "base/no_destructor.h" #include "brave/browser/brave_adaptive_captcha/brave_adaptive_captcha_service_factory.h" +#include "brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h" +#include "brave/browser/brave_ads/ads_service_delegate.h" #include "brave/browser/brave_ads/device_id/device_id_impl.h" #include "brave/browser/brave_ads/services/bat_ads_service_factory_impl.h" #include "brave/browser/brave_ads/tooltips/ads_tooltips_delegate_impl.h" @@ -17,9 +19,11 @@ #include "brave/components/brave_ads/browser/ads_service_impl.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/history/history_service_factory.h" +#include "chrome/browser/notifications/notification_display_service.h" #include "chrome/browser/notifications/notification_display_service_factory.h" #include "chrome/browser/profiles/profile.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" +#include "content/public/browser/storage_partition.h" namespace brave_ads { @@ -64,10 +68,13 @@ AdsServiceFactory::CreateAdsTooltipsDelegate(Profile* profile) const { KeyedService* AdsServiceFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { auto* profile = Profile::FromBrowserContext(context); - auto* brave_adaptive_captcha_service = brave_adaptive_captcha::BraveAdaptiveCaptchaServiceFactory::GetInstance() ->GetForProfile(profile); + auto* display_service = NotificationDisplayService::GetForProfile(profile); + auto* delegate = new AdsServiceDelegate( + profile, brave_adaptive_captcha_service, display_service, + std::make_unique(*profile)); auto* history_service = HistoryServiceFactory::GetInstance()->GetForProfile( profile, ServiceAccessType::EXPLICIT_ACCESS); @@ -78,8 +85,10 @@ KeyedService* AdsServiceFactory::BuildServiceInstanceFor( std::unique_ptr ads_service = std::make_unique( - profile, g_browser_process->local_state(), - brave_adaptive_captcha_service, CreateAdsTooltipsDelegate(profile), + delegate, profile->GetPrefs(), g_browser_process->local_state(), + profile->GetDefaultStoragePartition() + ->GetURLLoaderFactoryForBrowserProcess(), + profile->GetPath(), CreateAdsTooltipsDelegate(profile), std::make_unique(), std::make_unique(), g_brave_browser_process->resource_component(), history_service, diff --git a/browser/brave_ads/application_state/background_helper/background_helper.cc b/browser/brave_ads/application_state/background_helper/background_helper.cc index e471bc0bc533..8802acc949c2 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper.cc +++ b/browser/brave_ads/application_state/background_helper/background_helper.cc @@ -3,7 +3,7 @@ * 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/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "brave/browser/brave_ads/application_state/background_helper/background_helper_holder.h" #include "build/build_config.h" // IWYU pragma: keep diff --git a/browser/brave_ads/application_state/background_helper/background_helper_android.h b/browser/brave_ads/application_state/background_helper/background_helper_android.h index d2a80fb76f7c..34276a097aae 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper_android.h +++ b/browser/brave_ads/application_state/background_helper/background_helper_android.h @@ -10,7 +10,7 @@ #include "base/android/application_status_listener.h" #include "base/memory/weak_ptr.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" namespace brave_ads { diff --git a/browser/brave_ads/application_state/background_helper/background_helper_holder.cc b/browser/brave_ads/application_state/background_helper/background_helper_holder.cc index 07aad0877684..300a7d2ad7af 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper_holder.cc +++ b/browser/brave_ads/application_state/background_helper/background_helper_holder.cc @@ -6,7 +6,7 @@ #include "brave/browser/brave_ads/application_state/background_helper/background_helper_holder.h" #include "base/no_destructor.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "build/build_config.h" // IWYU pragma: keep #if BUILDFLAG(IS_ANDROID) diff --git a/browser/brave_ads/application_state/background_helper/background_helper_linux.h b/browser/brave_ads/application_state/background_helper/background_helper_linux.h index a95dc2439f7d..47fe8739f4cd 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper_linux.h +++ b/browser/brave_ads/application_state/background_helper/background_helper_linux.h @@ -7,7 +7,7 @@ #define BRAVE_BROWSER_BRAVE_ADS_APPLICATION_STATE_BACKGROUND_HELPER_BACKGROUND_HELPER_LINUX_H_ #include "base/memory/weak_ptr.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "chrome/browser/ui/browser_list_observer.h" namespace brave_ads { diff --git a/browser/brave_ads/application_state/background_helper/background_helper_mac.h b/browser/brave_ads/application_state/background_helper/background_helper_mac.h index 5e6f70439b57..e89f6440e58a 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper_mac.h +++ b/browser/brave_ads/application_state/background_helper/background_helper_mac.h @@ -8,7 +8,7 @@ #include -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" namespace brave_ads { diff --git a/browser/brave_ads/application_state/background_helper/background_helper_win.h b/browser/brave_ads/application_state/background_helper/background_helper_win.h index a9b4af5df78f..f3e5a660cc21 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper_win.h +++ b/browser/brave_ads/application_state/background_helper/background_helper_win.h @@ -9,7 +9,7 @@ #include #include "base/win/windows_types.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "ui/gfx/win/singleton_hwnd_observer.h" namespace brave_ads { diff --git a/browser/brave_ads/application_state/notification_helper/notification_helper_impl_android.cc b/browser/brave_ads/application_state/notification_helper/notification_helper_impl_android.cc index 5d8d93982866..a035b7bf12b7 100644 --- a/browser/brave_ads/application_state/notification_helper/notification_helper_impl_android.cc +++ b/browser/brave_ads/application_state/notification_helper/notification_helper_impl_android.cc @@ -9,9 +9,9 @@ #include "base/system/sys_info.h" #include "brave/browser/brave_ads/android/jni_headers/BraveAdsSignupDialog_jni.h" #include "brave/browser/brave_ads/android/jni_headers/BraveAds_jni.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" #include "brave/build/android/jni_headers/BraveSiteChannelsManagerBridge_jni.h" #include "brave/components/brave_ads/browser/ad_units/notification_ad/custom_notification_ad_feature.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "chrome/browser/notifications/jni_headers/NotificationSystemStatusUtil_jni.h" #include "chrome/browser/notifications/notification_channels_provider_android.h" diff --git a/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_tab_helper_browsertest.cc b/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_tab_helper_browsertest.cc index 4c25ee55ab18..fb2cbb9db2cc 100644 --- a/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_tab_helper_browsertest.cc +++ b/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_tab_helper_browsertest.cc @@ -137,7 +137,7 @@ class BraveAdsCreativeSearchResultAdTabHelperTest base::test::ScopedFeatureList scoped_feature_list_; net::EmbeddedTestServer https_server_{ net::test_server::EmbeddedTestServer::TYPE_HTTPS}; - AdsServiceMock ads_service_mock_; + AdsServiceMock ads_service_mock_{nullptr}; }; IN_PROC_BROWSER_TEST_F(BraveAdsCreativeSearchResultAdTabHelperTest, diff --git a/browser/brave_ads/sources.gni b/browser/brave_ads/sources.gni index 8c7a3379aafe..4b65f80d7683 100644 --- a/browser/brave_ads/sources.gni +++ b/browser/brave_ads/sources.gni @@ -5,12 +5,13 @@ brave_browser_brave_ads_sources = [ "//brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h", + "//brave/browser/brave_ads/ads_service_delegate.cc", + "//brave/browser/brave_ads/ads_service_delegate.h", "//brave/browser/brave_ads/ads_service_factory.cc", "//brave/browser/brave_ads/ads_service_factory.h", "//brave/browser/brave_ads/analytics/p3a/brave_stats_helper.cc", "//brave/browser/brave_ads/analytics/p3a/brave_stats_helper.h", "//brave/browser/brave_ads/application_state/background_helper/background_helper.cc", - "//brave/browser/brave_ads/application_state/background_helper/background_helper.h", "//brave/browser/brave_ads/application_state/background_helper/background_helper_holder.cc", "//brave/browser/brave_ads/application_state/background_helper/background_helper_holder.h", "//brave/browser/brave_ads/application_state/notification_helper/notification_helper.cc", diff --git a/browser/brave_ads/tabs/ads_tab_helper_browsertest.cc b/browser/brave_ads/tabs/ads_tab_helper_browsertest.cc index d3cfcf4032c9..b01e7d8bf48b 100644 --- a/browser/brave_ads/tabs/ads_tab_helper_browsertest.cc +++ b/browser/brave_ads/tabs/ads_tab_helper_browsertest.cc @@ -98,7 +98,7 @@ class AdsTabHelperTest : public CertVerifierBrowserTest { private: net::EmbeddedTestServer https_server_{ net::test_server::EmbeddedTestServer::TYPE_HTTPS}; - AdsServiceMock ads_service_mock_; + AdsServiceMock ads_service_mock_{nullptr}; }; IN_PROC_BROWSER_TEST_F(AdsTabHelperTest, UserHasNotJoinedBraveRewards) { diff --git a/browser/widevine/BUILD.gn b/browser/widevine/BUILD.gn index f2f23ac00381..71a85e08f9a8 100644 --- a/browser/widevine/BUILD.gn +++ b/browser/widevine/BUILD.gn @@ -17,12 +17,15 @@ source_set("widevine") { deps = [ "//base", "//brave/app:brave_generated_resources_grit", + "//brave/components/brave_component_updater/browser", "//brave/components/constants", + "//brave/components/l10n/common", "//brave/components/widevine:constants", + "//chrome/browser:browser_process", + "//chrome/browser/profiles:profile", "//chrome/common", "//components/component_updater/", "//components/content_settings/core/common", - "//components/permissions", "//components/pref_registry", "//components/prefs", "//components/subresource_filter/content/browser", @@ -32,6 +35,10 @@ source_set("widevine") { "//third_party/widevine/cdm:buildflags", "//third_party/widevine/cdm:headers", "//ui/base", + ] + + public_deps = [ + "//components/permissions", "//url", ] diff --git a/components/brave_ads/browser/BUILD.gn b/components/brave_ads/browser/BUILD.gn index 6e5d8c311fa4..78beb71b898d 100644 --- a/components/brave_ads/browser/BUILD.gn +++ b/components/brave_ads/browser/BUILD.gn @@ -12,7 +12,6 @@ static_library("browser") { "ad_units/notification_ad/custom_notification_ad_feature.h", "ads_service.cc", "ads_service.h", - "ads_service_observer.h", "analytics/p2a/p2a.cc", "analytics/p2a/p2a.h", "analytics/p2a/p2a_constants.h", @@ -39,25 +38,28 @@ static_library("browser") { deps = [ "//base", "//brave/components/brave_adaptive_captcha", - "//brave/components/brave_ads/core", "//brave/components/brave_ads/resources", "//brave/components/brave_component_updater/browser", "//brave/components/l10n/common", + "//brave/components/ntp_background_images/common", "//brave/components/p3a_utils", "//brave/components/time_period_storage", - "//components/keyed_service/core", "//components/pref_registry", "//components/prefs", "//components/sessions", - "//sql", "//third_party/blink/public/common", + "//ui/base/idle", "//url", ] - public_deps = [ "//brave/components/brave_ads/core/mojom" ] - - # TODO(https://github.com/brave/brave-browser/issues/24163) - check_includes = false + public_deps = [ + "application_state", + "//brave/components/brave_ads/core", + "//brave/components/brave_ads/core/mojom", + "//brave/components/services/bat_ads/public/interfaces", + "//components/keyed_service/core", + "//mojo/public/cpp/bindings", + ] sources += [ "ads_service_impl.cc", @@ -74,8 +76,6 @@ static_library("browser") { "//brave/components/brave_news/common", "//brave/components/brave_rewards/browser", "//brave/components/brave_rewards/common", - "//chrome/browser/notifications", - "//chrome/browser/profiles:profile", "//components/history/core/browser", "//content/public/browser", "//mojo/public/cpp/bindings", diff --git a/components/brave_ads/browser/DEPS b/components/brave_ads/browser/DEPS index 7301248a9568..dbc656d9bf60 100644 --- a/components/brave_ads/browser/DEPS +++ b/components/brave_ads/browser/DEPS @@ -2,40 +2,9 @@ include_rules = [ "+brave/components/constants", "+content/public/browser", + "+mojo/public/cpp/bindings", "+services/network/public", "+third_party/blink/public", "+ui/base", "+ui/message_center/public", ] - -# Existing exceptions -specific_include_rules = { - "ads_service_impl.cc": [ - "+brave/browser/brave_ads/application_state/notification_helper/notification_helper.h", - "+brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h", - "+brave/browser/profiles/profile_util.h", - "+brave/common/brave_channel_info.h", - "+brave/browser/notifications/brave_notification_platform_bridge_helper_android.h", - "+chrome/common/chrome_paths.h", - "+chrome/browser/fullscreen.h", - "+chrome/browser/notifications/notification_display_service.h", - "+chrome/browser/profiles/profile_manager.h", - "+chrome/browser/profiles/profile.h", - "+chrome/browser/ui/browser.h", - "+chrome/browser/ui/browser_finder.h", - "+chrome/browser/ui/browser_navigator.h", - "+chrome/browser/ui/browser_navigator_params.h", - "+chrome/browser/first_run/first_run.h", - "+chrome/common/chrome_constants.h", - "+chrome/browser/android/service_tab_launcher.h", - "+chrome/browser/android/tab_android.h", - "+chrome/browser/ui/android/tab_model/tab_model_list.h", - ], - "ads_service_impl.h": [ - "+brave/browser/brave_ads/application_state/background_helper/background_helper.h", - "+chrome/browser/notifications/notification_handler.h", - "+mojo/public/cpp/bindings/associated_receiver.h", - "+mojo/public/cpp/bindings/associated_remote.h", - "+mojo/public/cpp/bindings/remote.h", - ], -} diff --git a/components/brave_ads/browser/ads_service.cc b/components/brave_ads/browser/ads_service.cc index b55f8d82156e..0e812c08e888 100644 --- a/components/brave_ads/browser/ads_service.cc +++ b/components/brave_ads/browser/ads_service.cc @@ -7,7 +7,7 @@ namespace brave_ads { -AdsService::AdsService() = default; +AdsService::AdsService(Delegate* delegate) : delegate_(delegate) {} AdsService::~AdsService() = default; diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index e00a661858c2..b9f19d705386 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -11,6 +11,7 @@ #include #include +#include "base/memory/raw_ptr.h" #include "base/time/time.h" #include "brave/components/brave_ads/browser/ads_service_callback.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" @@ -19,6 +20,7 @@ #include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h" #include "components/keyed_service/core/keyed_service.h" #include "mojo/public/cpp/bindings/pending_remote.h" +#include "ui/message_center/public/cpp/notification.h" class GURL; @@ -26,7 +28,36 @@ namespace brave_ads { class AdsService : public KeyedService { public: - AdsService(); + class Delegate { + public: + virtual void InitNotificationHelper() = 0; + virtual bool CanShowSystemNotificationsWhileBrowserIsBackgrounded() = 0; + virtual bool DoesSupportSystemNotifications() = 0; + virtual bool CanShowNotifications() = 0; + virtual bool ShowOnboardingNotification() = 0; + virtual void ShowScheduledCaptcha(const std::string& payment_id, + const std::string& captcha_id) = 0; + virtual void ClearScheduledCaptcha() = 0; + virtual void SnoozeScheduledCaptcha() = 0; + virtual void Display(const message_center::Notification& notification) = 0; + virtual void Close(const std::string& notification_id) = 0; + virtual void ShowNotificationAd(const std::string& id, + const std::u16string& title, + const std::u16string& body); + virtual void CloseNotificationAd(const std::string& id) = 0; + virtual void OpenNewTabWithUrl(const GURL& url) = 0; +#if BUILDFLAG(IS_ANDROID) + virtual void MaybeRegenerateNotification( + const std::string& notification_id, + const GURL& service_worker_scope) = 0; +#else + virtual bool IsFullScreenMode() = 0; +#endif + protected: + virtual ~Delegate() = default; + }; + + explicit AdsService(Delegate* delegate); AdsService(const AdsService&) = delete; AdsService& operator=(const AdsService&) = delete; @@ -254,6 +285,9 @@ class AdsService : public KeyedService { // Called when the user solves an adaptive captcha. virtual void NotifyDidSolveAdaptiveCaptcha() = 0; + + protected: + raw_ptr delegate_ = nullptr; // NOT OWNED }; } // namespace brave_ads diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 8ae3ee00bb5c..cff51c7406fc 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -27,9 +27,8 @@ #include "base/task/thread_pool.h" #include "base/time/time.h" #include "base/timer/timer.h" -#include "brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h" -#include "brave/browser/brave_ads/application_state/notification_helper/notification_helper.h" #include "brave/common/brave_channel_info.h" +#include "brave/components/brave_adaptive_captcha/pref_names.h" #include "brave/components/brave_ads/browser/ad_units/notification_ad/custom_notification_ad_feature.h" #include "brave/components/brave_ads/browser/analytics/p2a/p2a.h" #include "brave/components/brave_ads/browser/analytics/p3a/notification_ad.h" @@ -37,6 +36,7 @@ #include "brave/components/brave_ads/browser/component_updater/resource_component.h" #include "brave/components/brave_ads/browser/device_id/device_id.h" #include "brave/components/brave_ads/browser/reminder/reminder_util.h" +#include "brave/components/brave_ads/browser/tooltips/ads_tooltips_delegate.h" #include "brave/components/brave_ads/browser/user_engagement/ad_events/ad_event_cache_helper.h" #include "brave/components/brave_ads/core/public/ad_units/new_tab_page_ad/new_tab_page_ad_value_util.h" #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_feature.h" @@ -53,47 +53,27 @@ #include "brave/components/brave_news/common/pref_names.h" #include "brave/components/brave_rewards/browser/rewards_service.h" #include "brave/components/brave_rewards/common/mojom/rewards.mojom-forward.h" +#include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/l10n/common/country_code_util.h" #include "brave/components/l10n/common/locale_util.h" #include "brave/components/l10n/common/prefs.h" #include "brave/components/ntp_background_images/common/pref_names.h" #include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h" #include "build/build_config.h" // IWYU pragma: keep -#include "chrome/browser/notifications/notification_display_service.h" -#include "chrome/browser/profiles/profile.h" +#include "components/prefs/pref_service.h" #include "content/public/browser/browser_context.h" -#include "content/public/browser/storage_partition.h" #include "mojo/public/cpp/bindings/callback_helpers.h" #include "net/base/network_change_notifier.h" #include "services/network/public/cpp/resource_request.h" -#include "services/network/public/mojom/url_response_head.mojom.h" -#if !BUILDFLAG(IS_ANDROID) -#include "chrome/browser/fullscreen.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_finder.h" -#include "chrome/browser/ui/browser_navigator.h" -#include "chrome/browser/ui/browser_navigator_params.h" -#endif -#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h" -#include "brave/components/brave_adaptive_captcha/pref_names.h" -#include "brave/components/brave_ads/browser/tooltips/ads_tooltips_delegate.h" -#include "brave/components/brave_rewards/common/pref_names.h" -#include "components/prefs/pref_service.h" +#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/simple_url_loader.h" +#include "services/network/public/mojom/url_response_head.mojom.h" #include "ui/base/resource/resource_bundle.h" #include "ui/message_center/public/cpp/notification.h" #include "ui/message_center/public/cpp/notification_types.h" #include "ui/message_center/public/cpp/notifier_id.h" #include "url/gurl.h" -#if BUILDFLAG(IS_ANDROID) -#include "brave/browser/notifications/brave_notification_platform_bridge_helper_android.h" -#include "chrome/browser/android/service_tab_launcher.h" -#include "chrome/browser/android/tab_android.h" -#include "chrome/browser/ui/android/tab_model/tab_model_list.h" -#include "content/public/browser/page_navigator.h" -#endif - namespace brave_ads { namespace { @@ -198,37 +178,35 @@ void OnUrlLoaderResponseStartedCallback( } // namespace AdsServiceImpl::AdsServiceImpl( - Profile* profile, + Delegate* delegate, + PrefService* prefs, PrefService* local_state, - brave_adaptive_captcha::BraveAdaptiveCaptchaService* - adaptive_captcha_service, + scoped_refptr url_loader, + const base::FilePath& profile_path, std::unique_ptr ads_tooltips_delegate, std::unique_ptr device_id, std::unique_ptr bat_ads_service_factory, brave_ads::ResourceComponent* resource_component, history::HistoryService* history_service, brave_rewards::RewardsService* rewards_service) - : profile_(profile), + : AdsService(delegate), + prefs_(prefs), local_state_(local_state), + url_loader_(std::move(url_loader)), resource_component_(resource_component), history_service_(history_service), - adaptive_captcha_service_(adaptive_captcha_service), ads_tooltips_delegate_(std::move(ads_tooltips_delegate)), device_id_(std::move(device_id)), bat_ads_service_factory_(std::move(bat_ads_service_factory)), file_task_runner_(base::ThreadPool::CreateSequencedTaskRunner( {base::MayBlock(), base::TaskPriority::BEST_EFFORT, base::TaskShutdownBehavior::BLOCK_SHUTDOWN})), - ads_service_path_(profile_->GetPath().AppendASCII("ads_service")), - display_service_(NotificationDisplayService::GetForProfile(profile_)), + ads_service_path_(profile_path.AppendASCII("ads_service")), rewards_service_(rewards_service), bat_ads_client_associated_receiver_(this) { - CHECK(profile_); - CHECK(adaptive_captcha_service_); CHECK(device_id_); CHECK(bat_ads_service_factory_); CHECK(rewards_service_); - CHECK(profile->IsRegularProfile()); if (!history_service_ || !local_state_) { CHECK_IS_TEST(); @@ -282,10 +260,10 @@ void AdsServiceImpl::RegisterResourceComponents() const { void AdsServiceImpl::Migrate() { int64_t ads_per_hour = - profile_->GetPrefs()->GetInt64(prefs::kMaximumNotificationAdsPerHour); + prefs_->GetInt64(prefs::kMaximumNotificationAdsPerHour); if (ads_per_hour == 0) { - profile_->GetPrefs()->ClearPref(prefs::kMaximumNotificationAdsPerHour); - profile_->GetPrefs()->SetBoolean(prefs::kOptedInToNotificationAds, false); + prefs_->ClearPref(prefs::kMaximumNotificationAdsPerHour); + prefs_->SetBoolean(prefs::kOptedInToNotificationAds, false); } } @@ -305,38 +283,34 @@ void AdsServiceImpl::RegisterResourceComponentsForDefaultLanguageCode() const { } bool AdsServiceImpl::UserHasJoinedBraveRewards() const { - return profile_->GetPrefs()->GetBoolean(brave_rewards::prefs::kEnabled); + return prefs_->GetBoolean(brave_rewards::prefs::kEnabled); } bool AdsServiceImpl::UserHasOptedInToBraveNewsAds() const { - return profile_->GetPrefs()->GetBoolean( - brave_news::prefs::kBraveNewsOptedIn) && - profile_->GetPrefs()->GetBoolean( - brave_news::prefs::kNewTabPageShowToday); + return prefs_->GetBoolean(brave_news::prefs::kBraveNewsOptedIn) && + prefs_->GetBoolean(brave_news::prefs::kNewTabPageShowToday); } bool AdsServiceImpl::UserHasOptedInToNewTabPageAds() const { - return profile_->GetPrefs()->GetBoolean( + return prefs_->GetBoolean( ntp_background_images::prefs::kNewTabPageShowBackgroundImage) && - profile_->GetPrefs()->GetBoolean( - ntp_background_images::prefs:: - kNewTabPageShowSponsoredImagesBackgroundImage); + prefs_->GetBoolean(ntp_background_images::prefs:: + kNewTabPageShowSponsoredImagesBackgroundImage); } bool AdsServiceImpl::UserHasOptedInToNotificationAds() const { - return profile_->GetPrefs()->GetBoolean(brave_rewards::prefs::kEnabled) && - profile_->GetPrefs()->GetBoolean(prefs::kOptedInToNotificationAds); + return prefs_->GetBoolean(brave_rewards::prefs::kEnabled) && + prefs_->GetBoolean(prefs::kOptedInToNotificationAds); } bool AdsServiceImpl::UserHasOptedInToSearchResultAds() const { - return profile_->GetPrefs()->GetBoolean(prefs::kOptedInToSearchResultAds); + return prefs_->GetBoolean(prefs::kOptedInToSearchResultAds); } void AdsServiceImpl::InitializeNotificationsForCurrentProfile() { - NotificationHelper::GetInstance()->InitForProfile(profile_); + delegate_->InitNotificationHelper(); - RecordNotificationAdPositionMetric(ShouldShowCustomNotificationAds(), - profile_->GetPrefs()); + RecordNotificationAdPositionMetric(ShouldShowCustomNotificationAds(), prefs_); } void AdsServiceImpl::GetDeviceIdAndMaybeStartBatAdsService() { @@ -532,7 +506,7 @@ void AdsServiceImpl::ShutdownAndClearData() { VLOG(6) << "Clearing ads data"; - profile_->GetPrefs()->ClearPrefsWithPrefixSilently("brave.brave_ads"); + prefs_->ClearPrefsWithPrefixSilently("brave.brave_ads"); local_state_->ClearPref(brave_l10n::prefs::kCountryCode); file_task_runner_->PostTaskAndReplyWithResult( @@ -578,8 +552,7 @@ void AdsServiceImpl::SetFlags() { mojom::FlagsPtr mojom_flags = BuildFlags(); CHECK(mojom_flags); #if BUILDFLAG(IS_ANDROID) - if (profile_->GetPrefs()->GetBoolean( - brave_rewards::prefs::kUseRewardsStagingServer)) { + if (prefs_->GetBoolean(brave_rewards::prefs::kUseRewardsStagingServer)) { mojom_flags->environment_type = mojom::EnvironmentType::kStaging; } #endif // BUILDFLAG(IS_ANDROID) @@ -589,8 +562,7 @@ void AdsServiceImpl::SetFlags() { bool AdsServiceImpl::ShouldShowOnboardingNotification() { const bool should_show_onboarding_notification = - profile_->GetPrefs()->GetBoolean( - prefs::kShouldShowOnboardingNotification); + prefs_->GetBoolean(prefs::kShouldShowOnboardingNotification); return should_show_onboarding_notification && UserHasOptedInToNotificationAds() && CheckIfCanShowNotificationAds(); } @@ -600,7 +572,7 @@ void AdsServiceImpl::MaybeShowOnboardingNotification() { return; } - if (NotificationHelper::GetInstance()->ShowOnboardingNotification()) { + if (delegate_->ShowOnboardingNotification()) { SetProfilePref(prefs::kShouldShowOnboardingNotification, base::Value(false)); } @@ -620,7 +592,7 @@ void AdsServiceImpl::ShowReminder( } void AdsServiceImpl::CloseAdaptiveCaptcha() { - adaptive_captcha_service_->ClearScheduledCaptcha(); + delegate_->ClearScheduledCaptcha(); #if !BUILDFLAG(IS_ANDROID) ads_tooltips_delegate_->CloseCaptchaTooltip(); #endif // !BUILDFLAG(IS_ANDROID) @@ -640,7 +612,7 @@ void AdsServiceImpl::InitializeLocalStatePrefChangeRegistrar() { } void AdsServiceImpl::InitializePrefChangeRegistrar() { - pref_change_registrar_.Init(profile_->GetPrefs()); + pref_change_registrar_.Init(prefs_); InitializeBraveRewardsPrefChangeRegistrar(); InitializeSubdivisionTargetingPrefChangeRegistrar(); @@ -828,7 +800,7 @@ bool AdsServiceImpl::CheckIfCanShowNotificationAds() { return false; } - if (!NotificationHelper::GetInstance()->CanShowNotifications()) { + if (!delegate_->CanShowNotifications()) { return ShouldShowCustomNotificationAds(); } @@ -836,8 +808,7 @@ bool AdsServiceImpl::CheckIfCanShowNotificationAds() { } bool AdsServiceImpl::ShouldShowCustomNotificationAds() { - const bool can_show_native_notifications = - NotificationHelper::GetInstance()->CanShowNotifications(); + const bool can_show_native_notifications = delegate_->CanShowNotifications(); const bool can_fallback_to_custom_notification_ads = base::FeatureList::IsEnabled( @@ -857,8 +828,8 @@ bool AdsServiceImpl::ShouldShowCustomNotificationAds() { base::Value(true)); } - const bool did_fallback = profile_->GetPrefs()->GetBoolean( - prefs::kNotificationAdDidFallbackToCustom); + const bool did_fallback = + prefs_->GetBoolean(prefs::kNotificationAdDidFallbackToCustom); return should_show || should_fallback || did_fallback; } @@ -910,7 +881,7 @@ void AdsServiceImpl::NotificationAdTimedOut(const std::string& placement_id) { } if (!ShouldShowCustomNotificationAds() && - NotificationHelper::GetInstance()->DoesSupportSystemNotifications()) { + delegate_->DoesSupportSystemNotifications()) { bat_ads_associated_remote_->TriggerNotificationAdEvent( placement_id, mojom::NotificationAdEventType::kTimedOut, /*intentional*/ base::DoNothing()); @@ -928,7 +899,7 @@ void AdsServiceImpl::CloseAllNotificationAds() { } #endif - const auto& list = profile_->GetPrefs()->GetList(prefs::kNotificationAds); + const auto& list = prefs_->GetList(prefs::kNotificationAds); const base::circular_deque ads = NotificationAdsFromValue(list); @@ -937,7 +908,7 @@ void AdsServiceImpl::CloseAllNotificationAds() { CloseNotificationAd(ad.placement_id); } - profile_->GetPrefs()->SetList(prefs::kNotificationAds, {}); + prefs_->SetList(prefs::kNotificationAds, {}); } void AdsServiceImpl::PrefetchNewTabPageAdCallback( @@ -1003,25 +974,7 @@ void AdsServiceImpl::OpenNewTabWithUrl(const GURL& url) { return VLOG(1) << "Failed to open new tab due to invalid URL: " << url; } -#if BUILDFLAG(IS_ANDROID) - // ServiceTabLauncher can currently only launch new tabs - const content::OpenURLParams params(url, content::Referrer(), - WindowOpenDisposition::NEW_FOREGROUND_TAB, - ui::PAGE_TRANSITION_LINK, true); - ServiceTabLauncher::GetInstance()->LaunchTab( - profile_, params, base::BindOnce([](content::WebContents*) {})); -#else - Browser* browser = chrome::FindTabbedBrowser(profile_, false); - if (!browser) { - browser = Browser::Create(Browser::CreateParams(profile_, true)); - } - - NavigateParams nav_params(browser, url, ui::PAGE_TRANSITION_LINK); - nav_params.disposition = WindowOpenDisposition::SINGLETON_TAB; - nav_params.window_action = NavigateParams::SHOW_WINDOW; - nav_params.path_behavior = NavigateParams::RESPECT; - Navigate(&nav_params); -#endif + delegate_->OpenNewTabWithUrl(url); } void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& placement_id) { @@ -1071,16 +1024,15 @@ void AdsServiceImpl::URLRequestCallback( void AdsServiceImpl::ShowScheduledCaptchaCallback( const std::string& payment_id, const std::string& captcha_id) { - adaptive_captcha_service_->ShowScheduledCaptcha(payment_id, captcha_id); + delegate_->ShowScheduledCaptcha(payment_id, captcha_id); } void AdsServiceImpl::SnoozeScheduledCaptchaCallback() { - adaptive_captcha_service_->SnoozeScheduledCaptcha(); + delegate_->SnoozeScheduledCaptcha(); } void AdsServiceImpl::OnNotificationAdPositionChanged() { - RecordNotificationAdPositionMetric(ShouldShowCustomNotificationAds(), - profile_->GetPrefs()); + RecordNotificationAdPositionMetric(ShouldShowCustomNotificationAds(), prefs_); } void AdsServiceImpl::ShutdownAdsService() { @@ -1145,7 +1097,7 @@ bool AdsServiceImpl::IsBrowserUpgradeRequiredToServeAds() const { int64_t AdsServiceImpl::GetMaximumNotificationAdsPerHour() const { int64_t ads_per_hour = - profile_->GetPrefs()->GetInt64(prefs::kMaximumNotificationAdsPerHour); + prefs_->GetInt64(prefs::kMaximumNotificationAdsPerHour); if (ads_per_hour == -1) { ads_per_hour = kDefaultNotificationAdsPerHour.Get(); } @@ -1516,7 +1468,7 @@ void AdsServiceImpl::IsBrowserActive(IsBrowserActiveCallback callback) { void AdsServiceImpl::IsBrowserInFullScreenMode( IsBrowserInFullScreenModeCallback callback) { #if !BUILDFLAG(IS_ANDROID) - std::move(callback).Run(IsFullScreenMode()); + std::move(callback).Run(delegate_->IsFullScreenMode()); #else std::move(callback).Run(true); #endif @@ -1530,17 +1482,13 @@ void AdsServiceImpl::CanShowNotificationAds( void AdsServiceImpl::CanShowNotificationAdsWhileBrowserIsBackgrounded( CanShowNotificationAdsWhileBrowserIsBackgroundedCallback callback) { std::move(callback).Run( - NotificationHelper::GetInstance() - ->CanShowSystemNotificationsWhileBrowserIsBackgrounded()); + delegate_->CanShowSystemNotificationsWhileBrowserIsBackgrounded()); } void AdsServiceImpl::ShowNotificationAd(base::Value::Dict dict) { const NotificationAdInfo ad = NotificationAdFromValue(dict); if (ShouldShowCustomNotificationAds()) { - std::unique_ptr platform_bridge = - std::make_unique(*profile_); - std::u16string title; if (base::IsStringUTF8(ad.title)) { title = base::UTF8ToUTF16(ad.title); @@ -1551,8 +1499,7 @@ void AdsServiceImpl::ShowNotificationAd(base::Value::Dict dict) { body = base::UTF8ToUTF16(ad.body); } - const NotificationAd notification_ad(ad.placement_id, title, body, nullptr); - platform_bridge->ShowNotificationAd(notification_ad); + delegate_->ShowNotificationAd(ad.placement_id, title, body); } else { std::u16string title; if (base::IsStringUTF8(ad.title)) { @@ -1584,8 +1531,7 @@ void AdsServiceImpl::ShowNotificationAd(base::Value::Dict dict) { notification->set_never_timeout(true); #endif - display_service_->Display(NotificationHandler::Type::BRAVE_ADS, - *notification, /*metadata=*/nullptr); + delegate_->Display(*notification); } StartNotificationAdTimeOutTimer(ad.placement_id); @@ -1593,19 +1539,15 @@ void AdsServiceImpl::ShowNotificationAd(base::Value::Dict dict) { void AdsServiceImpl::CloseNotificationAd(const std::string& placement_id) { if (ShouldShowCustomNotificationAds()) { - std::unique_ptr platform_bridge = - std::make_unique(*profile_); - - platform_bridge->CloseNotificationAd(placement_id); + delegate_->CloseNotificationAd(placement_id); } else { #if BUILDFLAG(IS_ANDROID) const std::string brave_ads_url_prefix = kNotificationAdUrlPrefix; const GURL url = GURL(brave_ads_url_prefix.substr(0, brave_ads_url_prefix.size() - 1)); - BraveNotificationPlatformBridgeHelperAndroid::MaybeRegenerateNotification( - placement_id, url); + delegate_->MaybeRegenerateNotification(placement_id, url); #endif - display_service_->Close(NotificationHandler::Type::BRAVE_ADS, placement_id); + delegate_->Close(placement_id); } } @@ -1689,12 +1631,9 @@ void AdsServiceImpl::UrlRequest(mojom::UrlRequestInfoPtr mojom_url_request, auto url_loader_iter = url_loaders_.insert(url_loaders_.cend(), std::move(url_loader)); url_loader_iter->get()->DownloadToStringOfUnboundedSizeUntilCrashAndDie( - profile_->GetDefaultStoragePartition() - ->GetURLLoaderFactoryForBrowserProcess() - .get(), - base::BindOnce(&AdsServiceImpl::URLRequestCallback, - base::Unretained(this), url_loader_iter, - std::move(callback))); + url_loader_.get(), base::BindOnce(&AdsServiceImpl::URLRequestCallback, + base::Unretained(this), url_loader_iter, + std::move(callback))); } void AdsServiceImpl::Save(const std::string& name, @@ -1777,12 +1716,12 @@ void AdsServiceImpl::ShowScheduledCaptcha(const std::string& payment_id, #if BUILDFLAG(IS_ANDROID) ShowScheduledCaptchaCallback(payment_id, captcha_id); #else // BUILDFLAG(IS_ANDROID) - if (profile_->GetPrefs()->GetBoolean( + if (prefs_->GetBoolean( brave_adaptive_captcha::prefs::kScheduledCaptchaPaused)) { return VLOG(1) << "Ads paused; support intervention required"; } - const int snooze_count = profile_->GetPrefs()->GetInteger( + const int snooze_count = prefs_->GetInteger( brave_adaptive_captcha::prefs::kScheduledCaptchaSnoozeCount); CHECK(ads_tooltips_delegate_); @@ -1809,30 +1748,30 @@ void AdsServiceImpl::RunDBTransaction( void AdsServiceImpl::RecordP2AEvents(const std::vector& events) { for (const auto& event : events) { - RecordAndEmitP2AHistogramName(profile_->GetPrefs(), + RecordAndEmitP2AHistogramName(prefs_, /*name*/ event); } } void AdsServiceImpl::GetProfilePref(const std::string& path, GetProfilePrefCallback callback) { - std::move(callback).Run(profile_->GetPrefs()->GetValue(path).Clone()); + std::move(callback).Run(prefs_->GetValue(path).Clone()); } void AdsServiceImpl::SetProfilePref(const std::string& path, base::Value value) { - profile_->GetPrefs()->Set(path, value); + prefs_->Set(path, value); NotifyPrefChanged(path); } void AdsServiceImpl::ClearProfilePref(const std::string& path) { - profile_->GetPrefs()->ClearPref(path); + prefs_->ClearPref(path); NotifyPrefChanged(path); } void AdsServiceImpl::HasProfilePrefPath(const std::string& path, HasProfilePrefPathCallback callback) { - std::move(callback).Run(profile_->GetPrefs()->HasPrefPath(path)); + std::move(callback).Run(prefs_->HasPrefPath(path)); } void AdsServiceImpl::GetLocalStatePref(const std::string& path, diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 34111bc6ed2a..8631a6aee4f5 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -16,14 +16,15 @@ #include "base/files/file_path.h" #include "base/memory/raw_ptr.h" +#include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/task/cancelable_task_tracker.h" #include "base/threading/sequence_bound.h" #include "base/time/time.h" #include "base/timer/timer.h" -#include "brave/browser/brave_ads/application_state/background_helper/background_helper.h" #include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h" #include "brave/components/brave_ads/browser/ads_service.h" +#include "brave/components/brave_ads/browser/application_state/background_helper.h" #include "brave/components/brave_ads/browser/component_updater/resource_component_observer.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/ads_callback.h" @@ -38,9 +39,7 @@ #include "ui/base/idle/idle.h" class GURL; -class NotificationDisplayService; class PrefService; -class Profile; namespace base { class OneShotTimer; @@ -53,6 +52,7 @@ class RewardsService; namespace network { class SimpleURLLoader; +class SharedURLLoaderFactory; } // namespace network namespace brave_ads { @@ -72,10 +72,11 @@ class AdsServiceImpl final : public AdsService, public brave_rewards::RewardsServiceObserver { public: explicit AdsServiceImpl( - Profile* profile, + Delegate* delegate, + PrefService* prefs, PrefService* local_state, - brave_adaptive_captcha::BraveAdaptiveCaptchaService* - adaptive_captcha_service, + scoped_refptr url_loader, + const base::FilePath& profile_path, std::unique_ptr ads_tooltips_delegate, std::unique_ptr device_id, std::unique_ptr bat_ads_service_factory, @@ -435,18 +436,18 @@ class AdsServiceImpl final : public AdsService, SimpleURLLoaderList url_loaders_; - const raw_ptr profile_ = nullptr; // NOT OWNED + const raw_ptr prefs_ = nullptr; // NOT OWNED const raw_ptr local_state_ = nullptr; // NOT OWNED + scoped_refptr url_loader_ = nullptr; + const raw_ptr resource_component_ = nullptr; // NOT OWNED const raw_ptr history_service_ = nullptr; // NOT OWNED - const raw_ptr - adaptive_captcha_service_ = nullptr; // NOT OWNED const std::unique_ptr ads_tooltips_delegate_; const std::unique_ptr device_id_; @@ -457,8 +458,6 @@ class AdsServiceImpl final : public AdsService, const base::FilePath ads_service_path_; - const raw_ptr display_service_ = - nullptr; // NOT OWNED const raw_ptr rewards_service_{ nullptr}; // NOT OWNED diff --git a/components/brave_ads/browser/ads_service_mock.cc b/components/brave_ads/browser/ads_service_mock.cc index a441a12181ed..46aa4cc96a32 100644 --- a/components/brave_ads/browser/ads_service_mock.cc +++ b/components/brave_ads/browser/ads_service_mock.cc @@ -7,7 +7,7 @@ namespace brave_ads { -AdsServiceMock::AdsServiceMock() = default; +AdsServiceMock::AdsServiceMock(Delegate* delegate) : AdsService(delegate) {} AdsServiceMock::~AdsServiceMock() = default; diff --git a/components/brave_ads/browser/ads_service_mock.h b/components/brave_ads/browser/ads_service_mock.h index 6761465befc5..337d5fbd1382 100644 --- a/components/brave_ads/browser/ads_service_mock.h +++ b/components/brave_ads/browser/ads_service_mock.h @@ -19,7 +19,7 @@ namespace brave_ads { class AdsServiceMock : public AdsService { public: - AdsServiceMock(); + explicit AdsServiceMock(Delegate* delegate); AdsServiceMock(const AdsServiceMock&) = delete; AdsServiceMock& operator=(const AdsServiceMock&) = delete; diff --git a/components/brave_ads/browser/application_state/BUILD.gn b/components/brave_ads/browser/application_state/BUILD.gn new file mode 100644 index 000000000000..3d12f190e1f9 --- /dev/null +++ b/components/brave_ads/browser/application_state/BUILD.gn @@ -0,0 +1,10 @@ +# 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/. + +static_library("application_state") { + sources = [ "background_helper.h" ] + + public_deps = [ "//base" ] +} diff --git a/browser/brave_ads/application_state/background_helper/background_helper.h b/components/brave_ads/browser/application_state/background_helper.h similarity index 81% rename from browser/brave_ads/application_state/background_helper/background_helper.h rename to components/brave_ads/browser/application_state/background_helper.h index 48c7b126dafa..e538a9f4ed2c 100644 --- a/browser/brave_ads/application_state/background_helper/background_helper.h +++ b/components/brave_ads/browser/application_state/background_helper.h @@ -3,8 +3,8 @@ * 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_BROWSER_BRAVE_ADS_APPLICATION_STATE_BACKGROUND_HELPER_BACKGROUND_HELPER_H_ -#define BRAVE_BROWSER_BRAVE_ADS_APPLICATION_STATE_BACKGROUND_HELPER_BACKGROUND_HELPER_H_ +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_APPLICATION_STATE_BACKGROUND_HELPER_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_APPLICATION_STATE_BACKGROUND_HELPER_H_ #include "base/observer_list.h" @@ -49,4 +49,4 @@ class BackgroundHelper { } // namespace brave_ads -#endif // BRAVE_BROWSER_BRAVE_ADS_APPLICATION_STATE_BACKGROUND_HELPER_BACKGROUND_HELPER_H_ +#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_APPLICATION_STATE_BACKGROUND_HELPER_H_ diff --git a/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler_unittest.cc b/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler_unittest.cc index 20d523f0b50d..8af6ae36bcd4 100644 --- a/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler_unittest.cc +++ b/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler_unittest.cc @@ -88,7 +88,7 @@ class BraveAdsCreativeSearchResultAdHandlerTest : public ::testing::Test { } protected: - AdsServiceMock ads_service_mock_; + AdsServiceMock ads_service_mock_{nullptr}; base::test::ScopedFeatureList scoped_feature_list_; }; diff --git a/components/ntp_background_images/browser/view_counter_service_unittest.cc b/components/ntp_background_images/browser/view_counter_service_unittest.cc index 4c610f977e7d..4649bb82cb57 100644 --- a/components/ntp_background_images/browser/view_counter_service_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_service_unittest.cc @@ -266,7 +266,7 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { #endif std::unique_ptr view_counter_; - brave_ads::AdsServiceMock ads_service_mock_; + brave_ads::AdsServiceMock ads_service_mock_{nullptr}; }; TEST_F(NTPBackgroundImagesViewCounterTest, SINotActiveInitially) {