diff --git a/browser/brave_ads/analytics/p3a/brave_stats_helper.cc b/browser/brave_ads/analytics/p3a/brave_stats_helper.cc index da6dbd3bc3af..bab2df7767a3 100644 --- a/browser/brave_ads/analytics/p3a/brave_stats_helper.cc +++ b/browser/brave_ads/analytics/p3a/brave_stats_helper.cc @@ -39,11 +39,7 @@ BraveStatsHelper::BraveStatsHelper() profile_manager_observer_.Observe(profile_manager_); } -BraveStatsHelper::~BraveStatsHelper() { - if (current_profile_) { - current_profile_->RemoveObserver(this); - } -} +BraveStatsHelper::~BraveStatsHelper() = default; void BraveStatsHelper::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { registry->RegisterBooleanPref(prefs::kEnabledForLastProfile, false); @@ -65,11 +61,10 @@ void BraveStatsHelper::OnProfileAdded(Profile* profile) { } void BraveStatsHelper::OnProfileWillBeDestroyed(Profile* profile) { - if (profile != current_profile_) { + if (!current_profile_observation_.IsObservingSource(profile)) { return; } - profile->RemoveObserver(this); - current_profile_ = nullptr; + current_profile_observation_.Reset(); #if !BUILDFLAG(IS_ANDROID) last_used_profile_pref_change_registrar_.RemoveAll(); #endif @@ -77,13 +72,12 @@ void BraveStatsHelper::OnProfileWillBeDestroyed(Profile* profile) { } void BraveStatsHelper::OnProfileManagerDestroying() { - if (current_profile_ != nullptr) { + if (current_profile_observation_.IsObserving()) { #if !BUILDFLAG(IS_ANDROID) last_used_profile_pref_change_registrar_.RemoveAll(); #endif ads_enabled_pref_change_registrar_.RemoveAll(); - current_profile_->RemoveObserver(this); - current_profile_ = nullptr; + current_profile_observation_.Reset(); } profile_manager_observer_.Reset(); } @@ -109,12 +103,10 @@ PrefService* BraveStatsHelper::GetLastUsedProfilePrefs() { if (profile == nullptr || profile->IsOffTheRecord()) { return nullptr; } - if (current_profile_ != nullptr) { - current_profile_->RemoveObserver(this); - current_profile_ = nullptr; + if (current_profile_observation_.IsObserving()) { + current_profile_observation_.Reset(); } - current_profile_ = profile; - profile->AddObserver(this); + current_profile_observation_.Observe(profile); return profile->GetPrefs(); #endif } diff --git a/browser/brave_ads/analytics/p3a/brave_stats_helper.h b/browser/brave_ads/analytics/p3a/brave_stats_helper.h index adbcd078cfc7..4c5d9a8b3da0 100644 --- a/browser/brave_ads/analytics/p3a/brave_stats_helper.h +++ b/browser/brave_ads/analytics/p3a/brave_stats_helper.h @@ -48,7 +48,8 @@ class BraveStatsHelper : public ProfileManagerObserver, public ProfileObserver { PrefChangeRegistrar last_used_profile_pref_change_registrar_; #endif PrefChangeRegistrar ads_enabled_pref_change_registrar_; - raw_ptr current_profile_ = nullptr; + base::ScopedObservation + current_profile_observation_{this}; base::ScopedObservation profile_manager_observer_{this}; diff --git a/browser/ui/views/brave_ads/notification_ad_popup.cc b/browser/ui/views/brave_ads/notification_ad_popup.cc index 70e081a4563d..df6942d7740c 100644 --- a/browser/ui/views/brave_ads/notification_ad_popup.cc +++ b/browser/ui/views/brave_ads/notification_ad_popup.cc @@ -100,18 +100,13 @@ NotificationAdPopup::NotificationAdPopup( display::Screen* screen = display::Screen::GetScreen(); if (screen) { - screen->AddObserver(this); + screen_observation_.Observe(screen); } FadeIn(); } -NotificationAdPopup::~NotificationAdPopup() { - display::Screen* screen = display::Screen::GetScreen(); - if (screen) { - screen->RemoveObserver(this); - } -} +NotificationAdPopup::~NotificationAdPopup() = default; // static void NotificationAdPopup::SetDisableFadeInAnimationForTesting( diff --git a/browser/ui/views/brave_ads/notification_ad_popup.h b/browser/ui/views/brave_ads/notification_ad_popup.h index 38c245ea1011..952e68933b3c 100644 --- a/browser/ui/views/brave_ads/notification_ad_popup.h +++ b/browser/ui/views/brave_ads/notification_ad_popup.h @@ -22,6 +22,10 @@ class Profile; +namespace display { +class Screen; +} // namespace display + namespace gfx { class LinearAnimation; class Insets; @@ -157,6 +161,9 @@ class NotificationAdPopup : public views::WidgetDelegateView, base::ScopedObservation widget_observation_{this}; + + base::ScopedObservation + screen_observation_{this}; }; } // namespace brave_ads diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index cabf87404516..9fc1217e3886 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -194,7 +194,6 @@ AdsServiceImpl::AdsServiceImpl( local_state_(local_state), url_loader_(std::move(url_loader)), channel_name_(channel_name), - resource_component_(resource_component), history_service_(history_service), ads_tooltips_delegate_(std::move(ads_tooltips_delegate)), device_id_(std::move(device_id)), @@ -203,16 +202,17 @@ AdsServiceImpl::AdsServiceImpl( {base::MayBlock(), base::TaskPriority::BEST_EFFORT, base::TaskShutdownBehavior::BLOCK_SHUTDOWN})), ads_service_path_(profile_path.AppendASCII("ads_service")), - rewards_service_(rewards_service), bat_ads_client_associated_receiver_(this) { CHECK(device_id_); CHECK(bat_ads_service_factory_); - CHECK(rewards_service_); + CHECK(rewards_service); if (!history_service_ || !local_state_) { CHECK_IS_TEST(); } + rewards_service_observation_.Observe(rewards_service); + if (CanStartBatAdsService()) { bat_ads_client_notifier_pending_receiver_ = bat_ads_client_notifier_remote_.BindNewPipeAndPassReceiver(); @@ -224,24 +224,14 @@ AdsServiceImpl::AdsServiceImpl( GetDeviceIdAndMaybeStartBatAdsService(); - if (resource_component_) { - resource_component_->AddObserver(this); + if (resource_component) { + resource_component_observation_.Observe(resource_component); } else { CHECK_IS_TEST(); } - - rewards_service_->AddObserver(this); } -AdsServiceImpl::~AdsServiceImpl() { - if (resource_component_) { - resource_component_->RemoveObserver(this); - } - - bat_ads_observer_receiver_.reset(); - - rewards_service_->RemoveObserver(this); -} +AdsServiceImpl::~AdsServiceImpl() = default; /////////////////////////////////////////////////////////////////////////////// @@ -249,7 +239,7 @@ bool AdsServiceImpl::IsBatAdsServiceBound() const { return bat_ads_service_remote_.is_bound(); } -void AdsServiceImpl::RegisterResourceComponents() const { +void AdsServiceImpl::RegisterResourceComponents() { RegisterResourceComponentsForCurrentCountryCode(); if (UserHasOptedInToNotificationAds()) { @@ -268,18 +258,19 @@ void AdsServiceImpl::Migrate() { } } -void AdsServiceImpl::RegisterResourceComponentsForCurrentCountryCode() const { - if (resource_component_) { - const std::string country_code = brave_l10n::GetCountryCode(local_state_); - resource_component_->RegisterComponentForCountryCode(country_code); +void AdsServiceImpl::RegisterResourceComponentsForCurrentCountryCode() { + if (resource_component_observation_.IsObserving()) { + resource_component_observation_.GetSource() + ->RegisterComponentForCountryCode( + brave_l10n::GetCountryCode(local_state_)); } } -void AdsServiceImpl::RegisterResourceComponentsForDefaultLanguageCode() const { - if (resource_component_) { - const std::string& locale = brave_l10n::GetDefaultLocaleString(); - const std::string language_code = brave_l10n::GetISOLanguageCode(locale); - resource_component_->RegisterComponentForLanguageCode(language_code); +void AdsServiceImpl::RegisterResourceComponentsForDefaultLanguageCode() { + if (resource_component_observation_.IsObserving()) { + resource_component_observation_.GetSource() + ->RegisterComponentForLanguageCode(brave_l10n::GetISOLanguageCode( + brave_l10n::GetDefaultLocaleString())); } } @@ -434,7 +425,7 @@ void AdsServiceImpl::InitializeDatabase() { void AdsServiceImpl::InitializeRewardsWallet( const size_t current_start_number) { - rewards_service_->GetRewardsWallet( + rewards_service_observation_.GetSource()->GetRewardsWallet( base::BindOnce(&AdsServiceImpl::InitializeRewardsWalletCallback, weak_ptr_factory_.GetWeakPtr(), current_start_number)); } @@ -730,7 +721,7 @@ void AdsServiceImpl::NotifyPrefChanged(const std::string& path) const { } void AdsServiceImpl::GetRewardsWallet() { - rewards_service_->GetRewardsWallet( + rewards_service_observation_.GetSource()->GetRewardsWallet( base::BindOnce(&AdsServiceImpl::NotifyRewardsWalletDidUpdate, weak_ptr_factory_.GetWeakPtr())); } @@ -1664,12 +1655,12 @@ void AdsServiceImpl::LoadResourceComponent( const std::string& id, const int version, LoadResourceComponentCallback callback) { - if (!resource_component_) { + if (!resource_component_observation_.IsObserving()) { return std::move(callback).Run({}); } std::optional file_path = - resource_component_->MaybeGetPath(id, version); + resource_component_observation_.GetSource()->MaybeGetPath(id, version); if (!file_path) { return std::move(callback).Run({}); } @@ -1811,7 +1802,8 @@ void AdsServiceImpl::Log(const std::string& file, const int32_t line, const int32_t verbose_level, const std::string& message) { - rewards_service_->WriteDiagnosticLog(file, line, verbose_level, message); + rewards_service_observation_.GetSource()->WriteDiagnosticLog( + file, line, verbose_level, message); const int vlog_level = ::logging::GetVlogLevelHelper(file.c_str(), file.length()); diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 8aaba5c4d888..5622e6f477d5 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -99,9 +99,9 @@ class AdsServiceImpl final : public AdsService, bool IsBatAdsServiceBound() const; - void RegisterResourceComponents() const; - void RegisterResourceComponentsForCurrentCountryCode() const; - void RegisterResourceComponentsForDefaultLanguageCode() const; + void RegisterResourceComponents(); + void RegisterResourceComponentsForCurrentCountryCode(); + void RegisterResourceComponentsForDefaultLanguageCode(); void Migrate(); @@ -449,8 +449,9 @@ class AdsServiceImpl final : public AdsService, const std::string channel_name_; - const raw_ptr resource_component_ = - nullptr; // NOT OWNED + base::ScopedObservation + resource_component_observation_{this}; const raw_ptr history_service_ = nullptr; // NOT OWNED @@ -465,8 +466,9 @@ class AdsServiceImpl final : public AdsService, const base::FilePath ads_service_path_; - const raw_ptr rewards_service_{ - nullptr}; // NOT OWNED + base::ScopedObservation + rewards_service_observation_{this}; mojo::Receiver bat_ads_observer_receiver_{ this};