Skip to content

Commit

Permalink
Uplift of #8036 to 1.20.x
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing authored and justnom committed Feb 23, 2021
1 parent 4287d43 commit e3adc79
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ class RewardsService : public KeyedService {

virtual void SetAdsEnabled(const bool is_enabled) = 0;

virtual bool IsRewardsEnabled() const = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
24 changes: 24 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e3adc79

Please sign in to comment.