diff --git a/browser/brave_ads/ads_service_factory.cc b/browser/brave_ads/ads_service_factory.cc index 001ea98d3906..40d4a5140ca7 100644 --- a/browser/brave_ads/ads_service_factory.cc +++ b/browser/brave_ads/ads_service_factory.cc @@ -91,8 +91,7 @@ KeyedService* AdsServiceFactory::BuildServiceInstanceFor( std::unique_ptr ads_service = std::make_unique( - profile, - brave_adaptive_captcha_service, + profile, brave_adaptive_captcha_service, CreateAdsTooltipsDelegate(profile), std::make_unique(), history_service, rewards_service, notification_ad_async_data_store); return ads_service.release(); diff --git a/browser/brave_ads/brave_stats_updater_helper.cc b/browser/brave_ads/brave_stats_updater_helper.cc index 72e6e3d8693b..b5cc53804bc5 100644 --- a/browser/brave_ads/brave_stats_updater_helper.cc +++ b/browser/brave_ads/brave_stats_updater_helper.cc @@ -5,14 +5,12 @@ #include "brave/browser/brave_ads/brave_stats_updater_helper.h" +#include "base/files/file_path.h" #include "base/functional/bind.h" #include "brave/components/brave_ads/common/pref_names.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/profiles/profile_manager.h" #include "chrome/common/pref_names.h" #include "components/pref_registry/pref_registry_syncable.h" -#include "components/prefs/pref_service.h" namespace brave_ads { @@ -30,7 +28,11 @@ BraveStatsUpdaterHelper::BraveStatsUpdaterHelper() profile_manager_observer_.Observe(profile_manager_); } -BraveStatsUpdaterHelper::~BraveStatsUpdaterHelper() = default; +BraveStatsUpdaterHelper::~BraveStatsUpdaterHelper() { + if (current_profile_) { + current_profile_->RemoveObserver(this); + } +} void BraveStatsUpdaterHelper::RegisterLocalStatePrefs( PrefRegistrySimple* registry) { @@ -51,8 +53,27 @@ void BraveStatsUpdaterHelper::OnProfileAdded(Profile* profile) { } } -void BraveStatsUpdaterHelper::OnProfileManagerDestroying() { +void BraveStatsUpdaterHelper::OnProfileWillBeDestroyed(Profile* profile) { + if (profile != current_profile_) { + return; + } + profile->RemoveObserver(this); + current_profile_ = nullptr; +#if !BUILDFLAG(IS_ANDROID) + last_used_profile_pref_change_registrar_.RemoveAll(); +#endif ads_enabled_pref_change_registrar_.RemoveAll(); +} + +void BraveStatsUpdaterHelper::OnProfileManagerDestroying() { + if (current_profile_ != nullptr) { +#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; + } profile_manager_observer_.Reset(); } @@ -60,6 +81,7 @@ PrefService* BraveStatsUpdaterHelper::GetLastUsedProfilePrefs() { #if BUILDFLAG(IS_ANDROID) return ProfileManager::GetPrimaryUserProfile()->GetPrefs(); #else + base::FilePath last_used_profile_path = local_state_->GetFilePath(::prefs::kProfileLastUsed); Profile* profile; @@ -69,9 +91,15 @@ PrefService* BraveStatsUpdaterHelper::GetLastUsedProfilePrefs() { profile = profile_manager_->GetProfileByPath( profile_manager_->user_data_dir().Append(last_used_profile_path)); } - if (profile == nullptr) { + if (profile == nullptr || profile->IsOffTheRecord()) { return nullptr; } + if (current_profile_ != nullptr) { + current_profile_->RemoveObserver(this); + current_profile_ = nullptr; + } + current_profile_ = profile; + profile->AddObserver(this); return profile->GetPrefs(); #endif } diff --git a/browser/brave_ads/brave_stats_updater_helper.h b/browser/brave_ads/brave_stats_updater_helper.h index 847aec7906dd..750fc11b3055 100644 --- a/browser/brave_ads/brave_stats_updater_helper.h +++ b/browser/brave_ads/brave_stats_updater_helper.h @@ -7,16 +7,20 @@ #define BRAVE_BROWSER_BRAVE_ADS_BRAVE_STATS_UPDATER_HELPER_H_ #include "base/scoped_observation.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager_observer.h" +#include "chrome/browser/profiles/profile_observer.h" #include "components/prefs/pref_change_registrar.h" +#include "components/prefs/pref_service.h" class PrefRegistrySimple; class Profile; namespace brave_ads { -class BraveStatsUpdaterHelper : public ProfileManagerObserver { +class BraveStatsUpdaterHelper : public ProfileManagerObserver, + public ProfileObserver { public: BraveStatsUpdaterHelper(); ~BraveStatsUpdaterHelper() override; @@ -26,6 +30,8 @@ class BraveStatsUpdaterHelper : public ProfileManagerObserver { void OnProfileAdded(Profile* profile) override; void OnProfileManagerDestroying() override; + void OnProfileWillBeDestroyed(Profile* profile) override; + private: PrefService* GetLastUsedProfilePrefs(); void OnLastUsedProfileChanged(); @@ -35,6 +41,7 @@ class BraveStatsUpdaterHelper : public ProfileManagerObserver { PrefChangeRegistrar last_used_profile_pref_change_registrar_; #endif PrefChangeRegistrar ads_enabled_pref_change_registrar_; + raw_ptr current_profile_; base::ScopedObservation profile_manager_observer_{this}; diff --git a/browser/brave_ads/brave_stats_updater_helper_browsertest.cc b/browser/brave_ads/brave_stats_updater_helper_browsertest.cc index b7a998a4ed06..9a1dab2a8e05 100644 --- a/browser/brave_ads/brave_stats_updater_helper_browsertest.cc +++ b/browser/brave_ads/brave_stats_updater_helper_browsertest.cc @@ -7,9 +7,13 @@ #include +#include "base/files/file_path.h" #include "brave/components/brave_ads/common/pref_names.h" +#include "build/build_config.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_test_util.h" -#include "chrome/test/base/testing_browser_process.h" #include "components/prefs/pref_service.h" #include "content/public/test/browser_test.h" #include "content/public/test/browser_test_utils.h" @@ -18,15 +22,14 @@ #if BUILDFLAG(IS_ANDROID) #include "chrome/test/base/android/android_browser_test.h" #else -#include "chrome/browser/ui/browser.h" #include "chrome/test/base/in_process_browser_test.h" #endif namespace brave_ads { -class BraveStatsUpdaterHelperTest : public PlatformBrowserTest { +class BraveStatsUpdaterHelperBrowserTest : public PlatformBrowserTest { public: - BraveStatsUpdaterHelperTest() {} + BraveStatsUpdaterHelperBrowserTest() {} protected: void SetUpOnMainThread() override { @@ -61,7 +64,7 @@ class BraveStatsUpdaterHelperTest : public PlatformBrowserTest { std::unique_ptr brave_stats_updater_helper_; }; -IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, +IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest, PrimaryProfileEnabledUpdate) { Profile* primary_profile = profile_manager_->GetPrimaryUserProfile(); @@ -77,7 +80,7 @@ IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, } #if !BUILDFLAG(IS_ANDROID) -IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, ProfileSwitch) { +IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest, ProfileSwitch) { CreateMultipleProfiles(); profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true); @@ -92,7 +95,8 @@ IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, ProfileSwitch) { EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true); } -IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest, MultiProfileEnabledUpdate) { +IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest, + MultiProfileEnabledUpdate) { CreateMultipleProfiles(); profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true); diff --git a/browser/brave_local_state_prefs.cc b/browser/brave_local_state_prefs.cc index 099ae6565a4f..345f4a15670e 100644 --- a/browser/brave_local_state_prefs.cc +++ b/browser/brave_local_state_prefs.cc @@ -15,7 +15,6 @@ #include "brave/browser/ntp_background/ntp_p3a_helper_impl.h" #include "brave/browser/themes/brave_dark_mode_utils.h" #include "brave/components/brave_ads/browser/ads_service.h" -#include "brave/components/brave_referrals/buildflags/buildflags.h" #include "brave/components/brave_referrals/browser/brave_referrals_service.h" #include "brave/components/brave_search_conversion/p3a.h" #include "brave/components/brave_shields/browser/ad_block_service.h" diff --git a/browser/brave_referrals/referrals_service_delegate.cc b/browser/brave_referrals/referrals_service_delegate.cc index 9025e9317723..745a56f3b49a 100644 --- a/browser/brave_referrals/referrals_service_delegate.cc +++ b/browser/brave_referrals/referrals_service_delegate.cc @@ -48,7 +48,7 @@ ReferralsServiceDelegate::GetFirstRunSentinelCreationTimeCallback() { #endif void ReferralsServiceDelegate::OnProfileAdded(Profile* profile) { - if (profile != ProfileManager::GetPrimaryUserProfile()) + if (profile != ProfileManager::GetLastUsedProfileIfLoaded()) return; service_->Start();