Skip to content

Commit

Permalink
Add stats updater helper profile observer
Browse files Browse the repository at this point in the history
  • Loading branch information
DJAndries committed Jan 18, 2023
1 parent c593c58 commit 4f5e980
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
3 changes: 1 addition & 2 deletions browser/brave_ads/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ KeyedService* AdsServiceFactory::BuildServiceInstanceFor(

std::unique_ptr<AdsServiceImpl> ads_service =
std::make_unique<AdsServiceImpl>(
profile,
brave_adaptive_captcha_service,
profile, brave_adaptive_captcha_service,
CreateAdsTooltipsDelegate(profile), std::make_unique<DeviceIdImpl>(),
history_service, rewards_service, notification_ad_async_data_store);
return ads_service.release();
Expand Down
40 changes: 34 additions & 6 deletions browser/brave_ads/brave_stats_updater_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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) {
Expand All @@ -51,15 +53,35 @@ 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();
}

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;
Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion browser/brave_ads/brave_stats_updater_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -35,6 +41,7 @@ class BraveStatsUpdaterHelper : public ProfileManagerObserver {
PrefChangeRegistrar last_used_profile_pref_change_registrar_;
#endif
PrefChangeRegistrar ads_enabled_pref_change_registrar_;
raw_ptr<Profile> current_profile_;

base::ScopedObservation<ProfileManager, ProfileManagerObserver>
profile_manager_observer_{this};
Expand Down
18 changes: 11 additions & 7 deletions browser/brave_ads/brave_stats_updater_helper_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

#include <memory>

#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"
Expand All @@ -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 {
Expand Down Expand Up @@ -61,7 +64,7 @@ class BraveStatsUpdaterHelperTest : public PlatformBrowserTest {
std::unique_ptr<BraveStatsUpdaterHelper> brave_stats_updater_helper_;
};

IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperTest,
IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest,
PrimaryProfileEnabledUpdate) {
Profile* primary_profile = profile_manager_->GetPrimaryUserProfile();

Expand All @@ -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);

Expand All @@ -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);

Expand Down
1 change: 0 additions & 1 deletion browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_referrals/referrals_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ReferralsServiceDelegate::GetFirstRunSentinelCreationTimeCallback() {
#endif

void ReferralsServiceDelegate::OnProfileAdded(Profile* profile) {
if (profile != ProfileManager::GetPrimaryUserProfile())
if (profile != ProfileManager::GetLastUsedProfileIfLoaded())
return;

service_->Start();
Expand Down

0 comments on commit 4f5e980

Please sign in to comment.