Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ads] Fixes earnings in rewards non-custodian state count towards earnings in rewards custodian state #26235

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ void AdsServiceImpl::NotifyAdsServiceInitialized() const {
}
}

void AdsServiceImpl::ShutdownAndClearData() {
void AdsServiceImpl::ShutdownClearDataAndMaybeRestart() {
ShutdownAdsService();

VLOG(6) << "Clearing ads data";
Expand All @@ -525,11 +525,12 @@ void AdsServiceImpl::ShutdownAndClearData() {

file_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&DeletePathOnFileTaskRunner, ads_service_path_),
base::BindOnce(&AdsServiceImpl::ShutdownAndClearDataCallback,
base::BindOnce(&AdsServiceImpl::ShutdownClearDataAndMaybeRestartCallback,
weak_ptr_factory_.GetWeakPtr()));
}

void AdsServiceImpl::ShutdownAndClearDataCallback(const bool success) {
void AdsServiceImpl::ShutdownClearDataAndMaybeRestartCallback(
const bool success) {
if (!success) {
VLOG(0) << "Failed to clear ads data";
} else {
Expand Down Expand Up @@ -1157,7 +1158,7 @@ void AdsServiceImpl::OnNotificationAdClicked(const std::string& placement_id) {

void AdsServiceImpl::ClearData() {
UMA_HISTOGRAM_BOOLEAN(kClearDataHistogramName, true);
ShutdownAndClearData();
ShutdownClearDataAndMaybeRestart();
}

void AdsServiceImpl::GetDiagnostics(GetDiagnosticsCallback callback) {
Expand Down Expand Up @@ -1903,14 +1904,14 @@ void AdsServiceImpl::OnRewardsWalletCreated() {
}

void AdsServiceImpl::OnExternalWalletConnected() {
SetProfilePref(prefs::kShouldMigrateVerifiedRewardsUser, base::Value(true));

ShowReminder(mojom::ReminderType::kExternalWalletConnected);

ShutdownClearDataAndMaybeRestart();
}

void AdsServiceImpl::OnCompleteReset(const bool success) {
if (success) {
ShutdownAndClearData();
ShutdownClearDataAndMaybeRestart();
}
}

Expand Down
6 changes: 4 additions & 2 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ class AdsServiceImpl final : public AdsService,

void NotifyAdsServiceInitialized() const;

void ShutdownAndClearData();
void ShutdownAndClearDataCallback(bool success);
void ShutdownClearDataAndMaybeRestart();
void ShutdownClearDataAndMaybeRestartCallback(bool success);

void OnExternalWalletConnectedCallback(bool success);

void SetSysInfo();
void SetBuildChannel();
Expand Down
1 change: 0 additions & 1 deletion components/brave_ads/core/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ static_library("internal") {
"account/user_data/user_data_info.h",
"account/user_rewards/user_rewards.cc",
"account/user_rewards/user_rewards.h",
"account/user_rewards/user_rewards_delegate.h",
"account/user_rewards/user_rewards_util.cc",
"account/user_rewards/user_rewards_util.h",
"account/utility/redeem_confirmation/non_reward/redeem_non_reward_confirmation.cc",
Expand Down
5 changes: 0 additions & 5 deletions components/brave_ads/core/internal/account/account.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ void Account::MaybeInitializeUserRewards() {
// destroyed.

user_rewards_ = std::make_unique<UserRewards>(*wallet_);
user_rewards_->SetDelegate(this);

user_rewards_->FetchIssuers();

Expand Down Expand Up @@ -295,8 +294,4 @@ void Account::OnFailedToConfirm(const ConfirmationInfo& /*confirmation*/) {
MaybeRefillConfirmationTokens();
}

void Account::OnDidMigrateVerifiedRewardsUser() {
InitializeConfirmations();
}

} // namespace brave_ads
7 changes: 1 addition & 6 deletions components/brave_ads/core/internal/account/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "brave/components/brave_ads/core/internal/account/account_observer.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmations_delegate.h"
#include "brave/components/brave_ads/core/internal/account/user_rewards/user_rewards.h"
#include "brave/components/brave_ads/core/internal/account/user_rewards/user_rewards_delegate.h"
#include "brave/components/brave_ads/core/internal/account/wallet/wallet_info.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h"
#include "brave/components/brave_ads/core/public/ads_callback.h"
Expand All @@ -28,8 +27,7 @@ class Confirmations;
struct TransactionInfo;

class Account final : public AdsClientNotifierObserver,
public ConfirmationDelegate,
public UserRewardsDelegate {
public ConfirmationDelegate {
public:
Account();

Expand Down Expand Up @@ -118,9 +116,6 @@ class Account final : public AdsClientNotifierObserver,
void OnDidConfirm(const ConfirmationInfo& confirmation) override;
void OnFailedToConfirm(const ConfirmationInfo& confirmation) override;

// UserRewardsDelegate:
void OnDidMigrateVerifiedRewardsUser() override;

base::ObserverList<AccountObserver> observers_;

std::unique_ptr<Confirmations> confirmations_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
#include <string>

#include "base/base64url.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmation_info.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/payload/confirmation_payload_json_writer.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_info.h"
#include "brave/components/brave_ads/core/internal/account/tokens/confirmation_tokens/confirmation_tokens_util.h"
#include "brave/components/brave_ads/core/internal/account/tokens/payment_tokens/payment_token_util.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/unblinded_token.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_key.h"
#include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/verification_signature.h"
Expand Down Expand Up @@ -87,13 +83,4 @@ bool IsValid(const ConfirmationInfo& confirmation) {
return Verify(confirmation);
}

void ResetTokens() {
database::table::ConfirmationQueue database_table;
database_table.DeleteAll(/*intentional*/ base::DoNothing());

RemoveAllConfirmationTokens();

RemoveAllPaymentTokens();
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ struct ConfirmationInfo;

[[nodiscard]] bool IsValid(const ConfirmationInfo& confirmation);

void ResetTokens();

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_CONFIRMATIONS_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@

#include <optional>

#include "base/test/mock_callback.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmation_info.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/non_reward/non_reward_confirmation_test_util.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_test_util.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_confirmation_test_util.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_confirmation_util.h"
#include "brave/components/brave_ads/core/internal/account/tokens/confirmation_tokens/confirmation_tokens_test_util.h"
#include "brave/components/brave_ads/core/internal/account/tokens/confirmation_tokens/confirmation_tokens_util.h"
#include "brave/components/brave_ads/core/internal/account/tokens/payment_tokens/payment_token_util.h"
#include "brave/components/brave_ads/core/internal/account/tokens/token_generator_test_util.h"
#include "brave/components/brave_ads/core/internal/common/test/test_base.h"
#include "brave/components/brave_ads/core/internal/settings/settings_test_util.h"
Expand Down Expand Up @@ -63,43 +60,4 @@ TEST_F(BraveAdsConfirmationsUtilTest, IsConfirmationNotValid) {
EXPECT_FALSE(IsValid(confirmation));
}

TEST_F(BraveAdsConfirmationsUtilTest, ResetTokens) {
// Arrange
test::MockTokenGenerator(/*count=*/1);
test::RefillConfirmationTokens(/*count=*/1);

const std::optional<ConfirmationInfo> confirmation =
test::BuildRewardConfirmation(/*should_generate_random_uuids=*/false);
ASSERT_TRUE(confirmation);
test::BuildAndSaveConfirmationQueueItems(*confirmation, /*count=*/1);

// Act
ResetTokens();

// Assert
base::MockCallback<database::table::GetConfirmationQueueCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/true,
/*confirmation_queue_items=*/::testing::IsEmpty()));
confirmation_queue_database_table_.GetAll(callback.Get());

EXPECT_TRUE(ConfirmationTokensIsEmpty());

EXPECT_TRUE(PaymentTokensIsEmpty());
}

TEST_F(BraveAdsConfirmationsUtilTest, ResetIfNoTokens) {
// Act
ResetTokens();

// Assert
base::MockCallback<database::table::GetConfirmationQueueCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/true,
/*confirmation_queue_items=*/::testing::IsEmpty()));
confirmation_queue_database_table_.GetAll(callback.Get());

EXPECT_TRUE(ConfirmationTokensIsEmpty());

EXPECT_TRUE(PaymentTokensIsEmpty());
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ std::optional<IssuersInfo> GetIssuers() {
return issuers;
}

void ResetIssuers() {
ClearProfilePref(prefs::kIssuerPing);
ClearProfilePref(prefs::kIssuers);
}

bool IsIssuersValid(const IssuersInfo& issuers) {
return IsConfirmationTokenIssuerValid(issuers) &&
IsPaymentTokenIssuerValid(issuers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct IssuersInfo;

void SetIssuers(const IssuersInfo& issuers);
std::optional<IssuersInfo> GetIssuers();
void ResetIssuers();

bool IsIssuersValid(const IssuersInfo& issuers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "brave/components/brave_ads/core/internal/account/confirmations/confirmations_util.h"
#include "brave/components/brave_ads/core/internal/account/issuers/issuers_info.h"
#include "brave/components/brave_ads/core/internal/account/issuers/issuers_util.h"
#include "brave/components/brave_ads/core/internal/account/issuers/url_request/issuers_url_request.h"
Expand All @@ -19,9 +18,7 @@
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/common/logging_util.h"
#include "brave/components/brave_ads/core/internal/common/time/time_formatting_util.h"
#include "brave/components/brave_ads/core/internal/prefs/pref_util.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"

namespace brave_ads {

Expand All @@ -37,8 +34,6 @@ UserRewards::UserRewards(WalletInfo wallet) : wallet_(std::move(wallet)) {

UserRewards::~UserRewards() {
GetAdsClient()->RemoveObserver(this);

delegate_ = nullptr;
}

void UserRewards::FetchIssuers() {
Expand All @@ -55,39 +50,10 @@ void UserRewards::MaybeRedeemPaymentTokens() {

///////////////////////////////////////////////////////////////////////////////

void UserRewards::MaybeMigrateVerifiedRewardsUser() {
if (!ShouldMigrateVerifiedRewardsUser()) {
return;
}

BLOG(1, "Migrate verified rewards user");

ResetTokens();

ResetIssuers();
FetchIssuers();

SetProfileBooleanPref(prefs::kShouldMigrateVerifiedRewardsUser, false);

NotifyDidMigrateVerifiedRewardsUser();
}

void UserRewards::NotifyDidMigrateVerifiedRewardsUser() const {
if (delegate_) {
delegate_->OnDidMigrateVerifiedRewardsUser();
}
}

void UserRewards::OnNotifyDidSolveAdaptiveCaptcha() {
MaybeRefillConfirmationTokens();
}

void UserRewards::OnNotifyPrefDidChange(const std::string& path) {
if (path == prefs::kShouldMigrateVerifiedRewardsUser) {
MaybeMigrateVerifiedRewardsUser();
}
}

void UserRewards::OnDidFetchIssuers(const IssuersInfo& issuers) {
if (!IsIssuersValid(issuers)) {
return BLOG(0, "Invalid issuers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

#include <string>

#include "base/memory/raw_ptr.h"
#include "brave/components/brave_ads/core/internal/account/issuers/url_request/issuers_url_request.h"
#include "brave/components/brave_ads/core/internal/account/issuers/url_request/issuers_url_request_delegate.h"
#include "brave/components/brave_ads/core/internal/account/tokens/payment_tokens/payment_token_info.h"
#include "brave/components/brave_ads/core/internal/account/transactions/transactions_database_table.h"
#include "brave/components/brave_ads/core/internal/account/user_rewards/user_rewards_delegate.h"
#include "brave/components/brave_ads/core/internal/account/utility/redeem_payment_tokens/redeem_payment_tokens.h"
#include "brave/components/brave_ads/core/internal/account/utility/redeem_payment_tokens/redeem_payment_tokens_delegate.h"
#include "brave/components/brave_ads/core/internal/account/utility/refill_confirmation_tokens/refill_confirmation_tokens.h"
Expand All @@ -40,25 +38,15 @@ class UserRewards final : public AdsClientNotifierObserver,

~UserRewards() override;

void SetDelegate(UserRewardsDelegate* delegate) {
CHECK_EQ(delegate_, nullptr);
delegate_ = delegate;
}

void FetchIssuers();

void MaybeRefillConfirmationTokens();

void MaybeRedeemPaymentTokens();

private:
void MaybeMigrateVerifiedRewardsUser();

void NotifyDidMigrateVerifiedRewardsUser() const;

// AdsClientNotifierObserver:
void OnNotifyDidSolveAdaptiveCaptcha() override;
void OnNotifyPrefDidChange(const std::string& path) override;

// IssuersUrlRequestDelegate:
void OnDidFetchIssuers(const IssuersInfo& issuers) override;
Expand All @@ -76,8 +64,6 @@ class UserRewards final : public AdsClientNotifierObserver,
void OnCaptchaRequiredToRefillConfirmationTokens(
const std::string& captcha_id) override;

raw_ptr<UserRewardsDelegate> delegate_ = nullptr;

IssuersUrlRequest issuers_url_request_;
RefillConfirmationTokens refill_confirmation_tokens_;
RedeemPaymentTokens redeem_payment_tokens_;
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading