Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on profile and use local state "ads enabled" pref in stats updater #16026

Merged
merged 7 commits into from
Jan 20, 2023
132 changes: 132 additions & 0 deletions browser/brave_ads/brave_stats_updater_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* Copyright (c) 2022 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/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/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"

namespace brave_ads {

BraveStatsUpdaterHelper::BraveStatsUpdaterHelper()
: local_state_(g_browser_process->local_state()),
profile_manager_(g_browser_process->profile_manager()) {
#if !BUILDFLAG(IS_ANDROID)
last_used_profile_pref_change_registrar_.Init(local_state_);
last_used_profile_pref_change_registrar_.Add(
::prefs::kProfileLastUsed,
base::BindRepeating(&BraveStatsUpdaterHelper::OnLastUsedProfileChanged,
base::Unretained(this)));
#endif

profile_manager_observer_.Observe(profile_manager_);
}

BraveStatsUpdaterHelper::~BraveStatsUpdaterHelper() {
if (current_profile_) {
current_profile_->RemoveObserver(this);
}
}

void BraveStatsUpdaterHelper::RegisterLocalStatePrefs(
PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(ads::prefs::kEnabledForLastProfile, false);
}

void BraveStatsUpdaterHelper::OnProfileAdded(Profile* profile) {
#if BUILDFLAG(IS_ANDROID)
if (profile == ProfileManager::GetPrimaryUserProfile()) {
#else
base::FilePath last_used_path =
local_state_->GetFilePath(::prefs::kProfileLastUsed);
if ((!last_used_path.empty() && profile->GetBaseName() == last_used_path) ||
(last_used_path.empty() &&
profile == ProfileManager::GetPrimaryUserProfile())) {
DJAndries marked this conversation as resolved.
Show resolved Hide resolved
#endif
OnLastUsedProfileChanged();
}
}

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;
if (last_used_profile_path.empty()) {
profile = profile_manager_->GetPrimaryUserProfile();
} else {
profile = profile_manager_->GetProfileByPath(
profile_manager_->user_data_dir().Append(last_used_profile_path));
}
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
}

void BraveStatsUpdaterHelper::OnLastUsedProfileChanged() {
PrefService* profile_prefs = GetLastUsedProfilePrefs();
if (profile_prefs == nullptr) {
return;
}
ads_enabled_pref_change_registrar_.RemoveAll();
ads_enabled_pref_change_registrar_.Init(profile_prefs);
ads_enabled_pref_change_registrar_.Add(
ads::prefs::kEnabled,
base::BindRepeating(&BraveStatsUpdaterHelper::UpdateLocalStateAdsEnabled,
base::Unretained(this)));
UpdateLocalStateAdsEnabled();
}

void BraveStatsUpdaterHelper::UpdateLocalStateAdsEnabled() {
PrefService* profile_prefs = GetLastUsedProfilePrefs();
if (profile_prefs == nullptr) {
return;
}
// Copying enabled pref to local state so the stats updater does not depend on
// the profile
local_state_->SetBoolean(ads::prefs::kEnabledForLastProfile,
profile_prefs->GetBoolean(ads::prefs::kEnabled));
}

} // namespace brave_ads
55 changes: 55 additions & 0 deletions browser/brave_ads/brave_stats_updater_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (c) 2022 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_BRAVE_STATS_UPDATER_HELPER_H_
#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,
public ProfileObserver {
public:
BraveStatsUpdaterHelper();
~BraveStatsUpdaterHelper() override;

static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);

void OnProfileAdded(Profile* profile) override;
void OnProfileManagerDestroying() override;

void OnProfileWillBeDestroyed(Profile* profile) override;

private:
PrefService* GetLastUsedProfilePrefs();
void OnLastUsedProfileChanged();
void UpdateLocalStateAdsEnabled();

#if !BUILDFLAG(IS_ANDROID)
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};

raw_ptr<PrefService> local_state_;
raw_ptr<ProfileManager> profile_manager_;
};

} // namespace brave_ads

#endif // BRAVE_BROWSER_BRAVE_ADS_BRAVE_STATS_UPDATER_HELPER_H_
118 changes: 118 additions & 0 deletions browser/brave_ads/brave_stats_updater_helper_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* Copyright (c) 2022 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/brave_stats_updater_helper.h"

#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 "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/test/base/android/android_browser_test.h"
#else
#include "chrome/test/base/in_process_browser_test.h"
#endif

namespace brave_ads {

class BraveStatsUpdaterHelperBrowserTest : public PlatformBrowserTest {
public:
BraveStatsUpdaterHelperBrowserTest() {}

protected:
void SetUpOnMainThread() override {
PlatformBrowserTest::SetUpOnMainThread();
profile_manager_ = g_browser_process->profile_manager();
local_state_ = g_browser_process->local_state();
brave_stats_updater_helper_ = std::make_unique<BraveStatsUpdaterHelper>();
}

void PostRunTestOnMainThread() override {
brave_stats_updater_helper_.reset();
PlatformBrowserTest::PostRunTestOnMainThread();
}

void CreateMultipleProfiles() {
profile_one_path_ = profile_manager_->GenerateNextProfileDirectoryPath();
profile_one_ = profiles::testing::CreateProfileSync(profile_manager_,
profile_one_path_);
profile_two_path_ = profile_manager_->GenerateNextProfileDirectoryPath();
profile_two_ = profiles::testing::CreateProfileSync(profile_manager_,
profile_two_path_);
}

base::FilePath profile_one_path_;
Profile* profile_one_;

base::FilePath profile_two_path_;
Profile* profile_two_;

ProfileManager* profile_manager_;
PrefService* local_state_;
std::unique_ptr<BraveStatsUpdaterHelper> brave_stats_updater_helper_;
};

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

EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

primary_profile->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

primary_profile->GetPrefs()->SetBoolean(ads::prefs::kEnabled, false);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);
}

#if !BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest, ProfileSwitch) {
CreateMultipleProfiles();
profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profiles::testing::SwitchToProfileSync(profile_two_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);
}

IN_PROC_BROWSER_TEST_F(BraveStatsUpdaterHelperBrowserTest,
MultiProfileEnabledUpdate) {
CreateMultipleProfiles();
profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);

profiles::testing::SwitchToProfileSync(profile_one_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profile_two_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, true);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);

profile_one_->GetPrefs()->SetBoolean(ads::prefs::kEnabled, false);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile),
false);

profiles::testing::SwitchToProfileSync(profile_two_path_);
EXPECT_EQ(local_state_->GetBoolean(ads::prefs::kEnabledForLastProfile), true);
}
#endif

} // namespace brave_ads
2 changes: 2 additions & 0 deletions browser/brave_ads/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ brave_browser_brave_ads_sources = [
"//brave/browser/brave_ads/background_helper/background_helper_holder.h",
"//brave/browser/brave_ads/brave_ads_host.cc",
"//brave/browser/brave_ads/brave_ads_host.h",
"//brave/browser/brave_ads/brave_stats_updater_helper.cc",
"//brave/browser/brave_ads/brave_stats_updater_helper.h",
"//brave/browser/brave_ads/notification_helper/notification_helper.cc",
"//brave/browser/brave_ads/notification_helper/notification_helper.h",
"//brave/browser/brave_ads/notification_helper/notification_helper_impl.cc",
Expand Down
11 changes: 11 additions & 0 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/bind.h"
#include "base/path_service.h"
#include "base/task/thread_pool.h"
#include "brave/browser/brave_ads/brave_stats_updater_helper.h"
#include "brave/browser/brave_referrals/referrals_service_delegate.h"
#include "brave/browser/brave_shields/ad_block_subscription_download_manager_getter.h"
#include "brave/browser/brave_stats/brave_stats_updater.h"
Expand Down Expand Up @@ -114,6 +115,9 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(StartupData* startup_data)
// early initialize referrals
brave_referrals_service();

// initialize ads stats updater helper
InitBraveStatsUpdaterHelper();

// early initialize brave stats
brave_stats_updater();

Expand Down Expand Up @@ -299,6 +303,13 @@ void BraveBrowserProcessImpl::OnBraveDarkModeChanged() {
UpdateBraveDarkMode();
}

void BraveBrowserProcessImpl::InitBraveStatsUpdaterHelper() {
if (!brave_stats_updater_helper_) {
brave_stats_updater_helper_ =
std::make_unique<brave_ads::BraveStatsUpdaterHelper>();
}
}

#if BUILDFLAG(ENABLE_TOR)
tor::BraveTorClientUpdater* BraveBrowserProcessImpl::tor_client_updater() {
if (tor_client_updater_)
Expand Down
5 changes: 5 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SpeedreaderRewriterService;
}

namespace brave_ads {
class BraveStatsUpdaterHelper;
class ResourceComponent;
}

Expand Down Expand Up @@ -143,6 +144,8 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess,
void UpdateBraveDarkMode();
void OnBraveDarkModeChanged();

void InitBraveStatsUpdaterHelper();

brave_component_updater::BraveComponent::Delegate*
brave_component_updater_delegate();

Expand Down Expand Up @@ -192,6 +195,8 @@ class BraveBrowserProcessImpl : public BraveBrowserProcess,

std::unique_ptr<brave::BraveFarblingService> brave_farbling_service_;
std::unique_ptr<misc_metrics::MenuMetrics> menu_metrics_;
std::unique_ptr<brave_ads::BraveStatsUpdaterHelper>
brave_stats_updater_helper_;

SEQUENCE_CHECKER(sequence_checker_);
};
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include <string>

#include "base/values.h"
#include "brave/browser/brave_ads/brave_stats_updater_helper.h"
#include "brave/browser/brave_stats/brave_stats_updater.h"
#include "brave/browser/metrics/buildflags/buildflags.h"
#include "brave/browser/metrics/metrics_reporting_util.h"
#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/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 Expand Up @@ -133,6 +135,7 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {

misc_metrics::MenuMetrics::RegisterPrefs(registry);
misc_metrics::PageMetricsService::RegisterPrefs(registry);
brave_ads::BraveStatsUpdaterHelper::RegisterLocalStatePrefs(registry);
}

} // namespace brave
Loading