Skip to content

Commit

Permalink
Remove dependency on profile and use local state "ads enabled" pref i…
Browse files Browse the repository at this point in the history
…n stats updater
  • Loading branch information
DJAndries committed Nov 18, 2022
1 parent edcb918 commit df0f349
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 214 deletions.
3 changes: 3 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#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/buildflags/buildflags.h"
#include "brave/components/brave_search_conversion/p3a.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
Expand Down Expand Up @@ -130,6 +131,8 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
ntp_background_images::NTPP3AHelperImpl::RegisterLocalStatePrefs(registry);

core_metrics::CoreMetricsService::RegisterPrefs(registry);

brave_ads::AdsService::RegisterLocalStatePrefs(registry);
}

} // namespace brave
37 changes: 5 additions & 32 deletions browser/brave_stats/brave_stats_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "brave/components/version_info/version_info.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/channel_info.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
Expand Down Expand Up @@ -96,9 +95,7 @@ net::NetworkTrafficAnnotationTag AnonymousStatsAnnotation() {
} // anonymous namespace

BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service)
: pref_service_(pref_service),
testing_url_loader_factory_(nullptr),
testing_profile_prefs_(nullptr) {
: pref_service_(pref_service), testing_url_loader_factory_(nullptr) {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kBraveStatsUpdaterServer)) {
Expand All @@ -110,23 +107,11 @@ BraveStatsUpdater::BraveStatsUpdater(PrefService* pref_service)
usage_server_ = BUILDFLAG(BRAVE_USAGE_SERVER);
}

// Track initial profile creation
if (g_browser_process->profile_manager()) {
profile_manager_observer_.Observe(g_browser_process->profile_manager());
DCHECK_EQ(0U,
g_browser_process->profile_manager()->GetLoadedProfiles().size());
}
Start();
}

BraveStatsUpdater::~BraveStatsUpdater() = default;

void BraveStatsUpdater::OnProfileAdded(Profile* profile) {
if (profile == ProfileManager::GetPrimaryUserProfile()) {
profile_manager_observer_.Reset();
Start();
}
}

void BraveStatsUpdater::Start() {
// Startup timer, only initiated once we've checked for a promo
// code.
Expand Down Expand Up @@ -187,13 +172,6 @@ network::mojom::URLLoaderFactory* BraveStatsUpdater::GetURLLoaderFactory() {
->GetURLLoaderFactory();
}

PrefService* BraveStatsUpdater::GetProfilePrefs() {
if (testing_profile_prefs_ != nullptr) {
return testing_profile_prefs_;
}
return ProfileManager::GetPrimaryUserProfile()->GetPrefs();
}

// static
void BraveStatsUpdater::SetStatsUpdatedCallbackForTesting(
StatsUpdatedCallback* stats_updated_callback) {
Expand All @@ -216,10 +194,6 @@ void BraveStatsUpdater::SetUsageServerForTesting(
usage_server_ = usage_server;
}

void BraveStatsUpdater::SetProfilePrefsForTesting(raw_ptr<PrefService> prefs) {
testing_profile_prefs_ = prefs;
}

GURL BraveStatsUpdater::BuildStatsEndpoint(const std::string& path) {
return GURL(usage_server_ + path);
}
Expand Down Expand Up @@ -321,7 +295,7 @@ bool BraveStatsUpdater::IsReferralInitialized() {
}

bool BraveStatsUpdater::IsAdsEnabled() {
return GetProfilePrefs()->GetBoolean(ads::prefs::kEnabled);
return pref_service_->GetBoolean(ads::prefs::kEnabledForLastProfile);
}

bool BraveStatsUpdater::HasDoneThresholdPing() {
Expand Down Expand Up @@ -398,10 +372,9 @@ void BraveStatsUpdater::SendServerPing() {
auto traffic_annotation = AnonymousStatsAnnotation();
auto resource_request = std::make_unique<network::ResourceRequest>();

auto* profile_pref_service = GetProfilePrefs();
auto stats_updater_params =
std::make_unique<brave_stats::BraveStatsUpdaterParams>(
pref_service_, profile_pref_service, arch_);
std::make_unique<brave_stats::BraveStatsUpdaterParams>(pref_service_,
arch_);

auto endpoint = BuildStatsEndpoint(kBraveUsageStandardPath);
resource_request->url = GetUpdateURL(endpoint, *stats_updater_params);
Expand Down
15 changes: 2 additions & 13 deletions browser/brave_stats/brave_stats_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "brave/components/brave_stats/browser/brave_stats_updater_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"

class BraveStatsUpdaterBrowserTest;
class PrefChangeRegistrar;
class PrefRegistrySimple;
class PrefService;
class Profile;

namespace base {
class OneShotTimer;
Expand All @@ -42,12 +39,12 @@ namespace brave_stats {

class BraveStatsUpdaterParams;

class BraveStatsUpdater : public ProfileManagerObserver {
class BraveStatsUpdater {
public:
explicit BraveStatsUpdater(PrefService* pref_service);
BraveStatsUpdater(const BraveStatsUpdater&) = delete;
BraveStatsUpdater& operator=(const BraveStatsUpdater&) = delete;
~BraveStatsUpdater() override;
~BraveStatsUpdater();

void Start();
void Stop();
Expand All @@ -62,12 +59,8 @@ class BraveStatsUpdater : public ProfileManagerObserver {
void SetURLLoaderFactoryForTesting(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
void SetUsageServerForTesting(const std::string& usage_server);
void SetProfilePrefsForTesting(raw_ptr<PrefService> prefs);

private:
// ProfileManagerObserver
void OnProfileAdded(Profile* profile) override;

GURL BuildStatsEndpoint(const std::string& path);
void OnThresholdLoaderComplete(scoped_refptr<net::HttpResponseHeaders>);
// Invoked from SimpleURLLoader after download is complete.
Expand Down Expand Up @@ -97,7 +90,6 @@ class BraveStatsUpdater : public ProfileManagerObserver {
void DisableThresholdPing();

network::mojom::URLLoaderFactory* GetURLLoaderFactory();
PrefService* GetProfilePrefs();

friend class ::BraveStatsUpdaterBrowserTest;

Expand All @@ -111,11 +103,8 @@ class BraveStatsUpdater : public ProfileManagerObserver {
std::unique_ptr<base::RepeatingTimer> server_ping_periodic_timer_;
std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
base::RepeatingClosure stats_preconditions_barrier_;
base::ScopedObservation<ProfileManager, ProfileManagerObserver>
profile_manager_observer_{this};

scoped_refptr<network::SharedURLLoaderFactory> testing_url_loader_factory_;
raw_ptr<PrefService> testing_profile_prefs_;
};

// Registers the preferences used by BraveStatsUpdater
Expand Down
9 changes: 2 additions & 7 deletions browser/brave_stats/brave_stats_updater_params.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,20 @@ static constexpr base::TimeDelta g_dtoi_delete_delta =

BraveStatsUpdaterParams::BraveStatsUpdaterParams(
PrefService* stats_pref_service,
PrefService* profile_pref_service,
const ProcessArch arch)
: BraveStatsUpdaterParams(stats_pref_service,
profile_pref_service,
arch,
GetCurrentDateAsYMD(),
GetCurrentISOWeekNumber(),
GetCurrentMonth()) {}

BraveStatsUpdaterParams::BraveStatsUpdaterParams(
PrefService* stats_pref_service,
PrefService* profile_pref_service,
const ProcessArch arch,
const std::string& ymd,
int woy,
int month)
: stats_pref_service_(stats_pref_service),
profile_pref_service_(profile_pref_service),
arch_(arch),
ymd_(ymd),
woy_(woy),
Expand Down Expand Up @@ -92,7 +88,7 @@ std::string BraveStatsUpdaterParams::GetReferralCodeParam() const {

std::string BraveStatsUpdaterParams::GetAdsEnabledParam() const {
return BooleanToString(
profile_pref_service_->GetBoolean(ads::prefs::kEnabled));
stats_pref_service_->GetBoolean(ads::prefs::kEnabledForLastProfile));
}

std::string BraveStatsUpdaterParams::GetProcessArchParam() const {
Expand Down Expand Up @@ -120,8 +116,7 @@ void BraveStatsUpdaterParams::LoadPrefs() {
last_check_month_ = stats_pref_service_->GetInteger(kLastCheckMonth);
first_check_made_ = stats_pref_service_->GetBoolean(kFirstCheckMade);
week_of_installation_ = stats_pref_service_->GetString(kWeekOfInstallation);
wallet_last_unlocked_ =
profile_pref_service_->GetTime(kBraveWalletLastUnlockTime);
wallet_last_unlocked_ = base::Time();
last_reported_wallet_unlock_ =
stats_pref_service_->GetTime(kBraveWalletPingReportedUnlockTime);
if (week_of_installation_.empty())
Expand Down
3 changes: 0 additions & 3 deletions browser/brave_stats/brave_stats_updater_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ namespace brave_stats {
class BraveStatsUpdaterParams {
public:
explicit BraveStatsUpdaterParams(PrefService* stats_pref_service,
PrefService* profile_pref_service,
const ProcessArch arch);
BraveStatsUpdaterParams(PrefService* stats_pref_service,
PrefService* profile_pref_service,
const ProcessArch arch,
const std::string& ymd,
int woy,
Expand Down Expand Up @@ -65,7 +63,6 @@ class BraveStatsUpdaterParams {
FRIEND_TEST_ALL_PREFIXES(::BraveStatsUpdaterTest, UsageBitstringInactive);

raw_ptr<PrefService> stats_pref_service_ = nullptr;
raw_ptr<PrefService> profile_pref_service_ = nullptr;
ProcessArch arch_;
std::string ymd_;
int woy_;
Expand Down

0 comments on commit df0f349

Please sign in to comment.