diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index b4b213a8963d..c61854d40521 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -927,6 +927,8 @@ void RewardsDOMHandler::SaveAdsSetting(const base::ListValue* args) { ads_service_->SetAdsEnabled(value == "true"); } else if (key == "adsPerHour") { ads_service_->SetAdsPerHour(std::stoull(value)); + } else if (key == "adsEnabledMigrated") { + ads_service_->MigrateAdsEnabled(value == "true"); } base::ListValue* emptyArgs = nullptr; diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index 1f79210b81e0..6944e8311151 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -26,6 +26,7 @@ class AdsService : public KeyedService { virtual bool IsAdsEnabled() const = 0; virtual void SetAdsEnabled(const bool is_enabled) = 0; + virtual void MigrateAdsEnabled(const bool is_enabled) = 0; virtual uint64_t GetAdsPerHour() const = 0; virtual void SetAdsPerHour(const uint64_t ads_per_hour) = 0; diff --git a/components/brave_ads/browser/ads_service_factory.cc b/components/brave_ads/browser/ads_service_factory.cc index 741fc63636c5..bbe1dbdf57c2 100644 --- a/components/brave_ads/browser/ads_service_factory.cc +++ b/components/brave_ads/browser/ads_service_factory.cc @@ -96,6 +96,7 @@ void AdsServiceFactory::RegisterProfilePrefs( } registry->RegisterBooleanPref(prefs::kBraveAdsEnabled, false); + registry->RegisterBooleanPref(prefs::kBraveAdsEnabledMigrated, false); registry->RegisterUint64Pref(prefs::kBraveAdsPerHour, 2); diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 5022e5a2cb70..9b47bb8fb8e9 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -802,6 +802,17 @@ void AdsServiceImpl::SetAdsEnabled(const bool is_enabled) { profile_->GetPrefs()->SetBoolean(prefs::kBraveAdsEnabled, is_enabled); } +void AdsServiceImpl::MigrateAdsEnabled(const bool is_enabled) { + if (profile_->GetPrefs()->GetBoolean( + prefs::kBraveAdsEnabledMigrated)) { + return; + } + + SetAdsEnabled(is_enabled); + + profile_->GetPrefs()->SetBoolean(prefs::kBraveAdsEnabledMigrated, true); +} + uint64_t AdsServiceImpl::GetAdsPerHour() const { return profile_->GetPrefs()->GetUint64(prefs::kBraveAdsPerHour); } diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index f14f33d9ac33..7738ac1b0f1f 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -65,6 +65,7 @@ class AdsServiceImpl : public AdsService, bool IsSupportedRegion() const override; void SetAdsEnabled(const bool is_enabled) override; + void MigrateAdsEnabled(const bool is_enabled) override; void SetAdsPerHour(const uint64_t ads_per_hour) override; diff --git a/components/brave_ads/common/pref_names.cc b/components/brave_ads/common/pref_names.cc index eaad0db0bd96..8aa548a445e7 100644 --- a/components/brave_ads/common/pref_names.cc +++ b/components/brave_ads/common/pref_names.cc @@ -27,6 +27,8 @@ const int kBraveAdsPrefsCurrentVersion = 2; const char kBraveAdsPrefsVersion[] = "brave.brave_ads.prefs.version"; const char kBraveAdsPrefsMigratedFrom62[] = "brave.brave_ads.prefs.migrated.from_0_62.x"; +const char kBraveAdsEnabledMigrated[] = + "brave.brave_ads.prefs.enabled_migrated"; } // namespace prefs diff --git a/components/brave_ads/common/pref_names.h b/components/brave_ads/common/pref_names.h index 35ecdc2e6843..357e37732f1d 100644 --- a/components/brave_ads/common/pref_names.h +++ b/components/brave_ads/common/pref_names.h @@ -23,6 +23,7 @@ extern const int kBraveAdsPrefsDefaultVersion; extern const int kBraveAdsPrefsCurrentVersion; extern const char kBraveAdsPrefsVersion[]; extern const char kBraveAdsPrefsMigratedFrom62[]; +extern const char kBraveAdsEnabledMigrated[]; } // namespace prefs diff --git a/components/brave_rewards/resources/ui/components/app.tsx b/components/brave_rewards/resources/ui/components/app.tsx index 42d1de8b745b..2592456d844b 100644 --- a/components/brave_rewards/resources/ui/components/app.tsx +++ b/components/brave_rewards/resources/ui/components/app.tsx @@ -32,6 +32,15 @@ export class App extends React.Component { if (!this.props.rewardsData.walletCreated) { this.actions.checkWalletExistence() } + + if (this.props.rewardsData.enabledMain && + !this.props.rewardsData.enabledAdsMigrated) { + const { adsEnabled, adsIsSupported } = this.props.rewardsData.adsData + + if (adsIsSupported) { + this.props.actions.onAdsSettingSave('adsEnabledMigrated', adsEnabled) + } + } } componentDidUpdate (prevProps: Props, prevState: State) { diff --git a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts index 2113f76cf111..15ab52ad10ac 100644 --- a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts +++ b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts @@ -137,6 +137,10 @@ const rewardsReducer: Reducer = (state: Rewards.State if (key) { state[key] = value chrome.send('brave_rewards.saveAdsSetting', [key, value.toString()]) + + if (key === 'adsEnabledMigrated') { + state.enabledAdsMigrated = true + } } break } diff --git a/components/brave_rewards/resources/ui/storage.ts b/components/brave_rewards/resources/ui/storage.ts index b1675e9a9ec3..ad5abc110f03 100644 --- a/components/brave_rewards/resources/ui/storage.ts +++ b/components/brave_rewards/resources/ui/storage.ts @@ -11,6 +11,7 @@ export const defaultState: Rewards.State = { createdTimestamp: null, enabledMain: false, enabledAds: true, + enabledAdsMigrated: false, enabledContribute: true, firstLoad: null, walletCreated: false, diff --git a/components/definitions/rewards.d.ts b/components/definitions/rewards.d.ts index 3fd8dffd2748..8ad4e0507561 100644 --- a/components/definitions/rewards.d.ts +++ b/components/definitions/rewards.d.ts @@ -39,6 +39,7 @@ declare namespace Rewards { donationAbilityTwitter: boolean donationAbilityYT: boolean enabledAds: boolean + enabledAdsMigrated: boolean enabledContribute: boolean enabledMain: boolean firstLoad: boolean | null