From e3adc79a35379c4aa46ce5204d82ca0d2ae9361c Mon Sep 17 00:00:00 2001 From: zenparsing Date: Mon, 22 Feb 2021 13:19:08 -0500 Subject: [PATCH] Uplift of #8036 to 1.20.x --- .../browser/ads_service_impl_unittest.cc | 2 ++ .../brave_rewards/browser/rewards_service.h | 2 ++ .../browser/rewards_service_impl.cc | 24 +++++++++++++++++++ .../browser/rewards_service_impl.h | 2 ++ 4 files changed, 30 insertions(+) diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index fce994f596df..8e89a1a739f7 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -251,6 +251,8 @@ class MockRewardsService : public RewardsService { MOCK_METHOD1(SetAdsEnabled, void(const bool is_enabled)); + + MOCK_CONST_METHOD0(IsRewardsEnabled, bool()); }; class AdsServiceTest : public testing::Test { diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index d346f9033c41..95b87d3a8622 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -371,6 +371,8 @@ class RewardsService : public KeyedService { virtual void SetAdsEnabled(const bool is_enabled) = 0; + virtual bool IsRewardsEnabled() const = 0; + protected: base::ObserverList observers_; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index c6c6dd474940..9791cce6eba2 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -1922,6 +1922,11 @@ void RewardsServiceImpl::GetPublisherInfo( const std::string& publisher_key, GetPublisherInfoCallback callback) { if (!Connected()) { + if (!IsRewardsEnabled()) { + std::move(callback).Run(ledger::type::Result::LEDGER_ERROR, nullptr); + return; + } + StartProcess( base::BindOnce( &RewardsServiceImpl::OnStartProcessForGetPublisherInfo, @@ -1984,6 +1989,11 @@ void RewardsServiceImpl::SavePublisherInfo( ledger::type::PublisherInfoPtr publisher_info, SavePublisherInfoCallback callback) { if (!Connected()) { + if (!IsRewardsEnabled()) { + std::move(callback).Run(ledger::type::Result::LEDGER_ERROR); + return; + } + StartProcess( base::BindOnce( &RewardsServiceImpl::OnStartProcessForSavePublisherInfo, @@ -3488,6 +3498,20 @@ void RewardsServiceImpl::SetAdsEnabled(const bool is_enabled) { AsWeakPtr())); } +bool RewardsServiceImpl::IsRewardsEnabled() const { + if (profile_->GetPrefs()->GetBoolean(prefs::kEnabled)) + return true; + + if (profile_->GetPrefs()->GetBoolean(prefs::kAutoContributeEnabled)) + return true; + + auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_); + if (ads_service && ads_service->IsEnabled()) + return true; + + return false; +} + void RewardsServiceImpl::OnStartProcessForSetAdsEnabled( const ledger::type::Result result) { if (result != ledger::type::Result::LEDGER_OK) { diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 8f15027c8916..2c81807fdaf2 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -342,6 +342,8 @@ class RewardsServiceImpl : public RewardsService, void SetAdsEnabled(const bool is_enabled) override; + bool IsRewardsEnabled() const override; + // Testing methods void SetLedgerEnvForTesting(); void PrepareLedgerEnvForTesting();