Skip to content

Commit

Permalink
Add "ads enabled" local state pref which reflects enabled setting of …
Browse files Browse the repository at this point in the history
…last profile
  • Loading branch information
DJAndries committed Nov 18, 2022
1 parent 8e3d245 commit edcb918
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion browser/brave_ads/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/components/brave_federated/brave_federated_service.h"
#include "brave/components/brave_federated/data_store_service.h"
#include "brave/components/brave_federated/notification_ad_task_constants.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
Expand Down Expand Up @@ -99,7 +100,7 @@ KeyedService* AdsServiceFactory::BuildServiceInstanceFor(

std::unique_ptr<AdsServiceImpl> ads_service =
std::make_unique<AdsServiceImpl>(
profile,
g_browser_process->local_state(), profile,
#if BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
brave_adaptive_captcha_service, CreateAdsTooltipsDelegate(profile),
#endif
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void AdsService::RemoveObserver(AdsServiceObserver* observer) {
observers_.RemoveObserver(observer);
}

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

void AdsService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(prefs::kAdsWereDisabled, false);
Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class GURL;

class PrefRegistrySimple;
namespace user_prefs {
class PrefRegistrySyncable;
} // namespace user_prefs
Expand All @@ -45,6 +46,7 @@ class AdsService : public KeyedService {
void RemoveObserver(AdsServiceObserver* observer);

// static
static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

// Returns |true| if the user's locale supports ads.
Expand Down
17 changes: 16 additions & 1 deletion components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ void OnLogTrainingInstance(const bool success) {
} // namespace

AdsServiceImpl::AdsServiceImpl(
PrefService* local_state,
Profile* profile,
#if BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
brave_adaptive_captcha::BraveAdaptiveCaptchaService*
Expand All @@ -349,7 +350,8 @@ AdsServiceImpl::AdsServiceImpl(
history::HistoryService* history_service,
brave_rewards::RewardsService* rewards_service,
brave_federated::AsyncDataStore* notification_ad_timing_data_store)
: profile_(profile),
: local_state_(local_state),
profile_(profile),
history_service_(history_service),
#if BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
adaptive_captcha_service_(adaptive_captcha_service),
Expand All @@ -364,6 +366,7 @@ AdsServiceImpl::AdsServiceImpl(
rewards_service_(rewards_service),
notification_ad_timing_data_store_(notification_ad_timing_data_store),
bat_ads_client_(new bat_ads::AdsClientMojoBridge(this)) {
DCHECK(local_state_);
DCHECK(profile_);
#if BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
DCHECK(adaptive_captcha_service_);
Expand All @@ -380,6 +383,8 @@ AdsServiceImpl::AdsServiceImpl(
MigrateConfirmationState();

rewards_service_->AddObserver(this);

CopyEnabledPrefToLocalState();
}

AdsServiceImpl::~AdsServiceImpl() {
Expand Down Expand Up @@ -665,6 +670,14 @@ void AdsServiceImpl::CloseAdaptiveCaptcha() {
#endif // BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
}

void AdsServiceImpl::CopyEnabledPrefToLocalState() {
// Copying enabled pref to local state so the stats updater does not depend on
// the profile
local_state_->SetBoolean(
ads::prefs::kEnabledForLastProfile,
profile_->GetPrefs()->GetBoolean(ads::prefs::kEnabled));
}

void AdsServiceImpl::InitializePrefChangeRegistrar() {
pref_change_registrar_.Init(profile_->GetPrefs());

Expand Down Expand Up @@ -695,6 +708,8 @@ void AdsServiceImpl::OnEnabledPrefChanged() {
}

MaybeStartBatAdsService();

CopyEnabledPrefToLocalState();
}

void AdsServiceImpl::OnIdleTimeThresholdPrefChanged() {
Expand Down
5 changes: 5 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
class GURL;

class NotificationDisplayService;
class PrefService;
class Profile;

namespace ads {
Expand Down Expand Up @@ -81,6 +82,7 @@ class AdsServiceImpl : public AdsService,
public base::SupportsWeakPtr<AdsServiceImpl> {
public:
explicit AdsServiceImpl(
PrefService* local_state,
Profile* profile,
#if BUILDFLAG(BRAVE_ADAPTIVE_CAPTCHA_ENABLED)
brave_adaptive_captcha::BraveAdaptiveCaptchaService*
Expand Down Expand Up @@ -146,6 +148,8 @@ class AdsServiceImpl : public AdsService,

void CloseAdaptiveCaptcha();

void CopyEnabledPrefToLocalState();

void InitializePrefChangeRegistrar();
void OnEnabledPrefChanged();
void OnIdleTimeThresholdPrefChanged();
Expand Down Expand Up @@ -458,6 +462,7 @@ class AdsServiceImpl : public AdsService,

SimpleURLLoaderList url_loaders_;

const raw_ptr<PrefService> local_state_ = nullptr; // NOT OWNED
const raw_ptr<Profile> profile_ = nullptr; // NOT OWNED

const raw_ptr<history::HistoryService> history_service_ =
Expand Down
1 change: 1 addition & 0 deletions components/brave_ads/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace ads::prefs {

// Stores whether Brave ads is enabled or disabled
const char kEnabled[] = "brave.brave_ads.enabled";
const char kEnabledForLastProfile[] = "brave.brave_ads.enabled_last_profile";

// Stores the maximum number of notification ads per hour
const char kMaximumNotificationAdsPerHour[] = "brave.brave_ads.ads_per_hour";
Expand Down
1 change: 1 addition & 0 deletions components/brave_ads/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace ads::prefs {

// Brave Ads enabled/disabled pref
extern const char kEnabled[];
extern const char kEnabledForLastProfile[];

// Notification prefs
extern const char kMaximumNotificationAdsPerHour[];
Expand Down

0 comments on commit edcb918

Please sign in to comment.