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] Follow up to #37390: Failing to Initialize ads due to corrupted confirmations.json #23083

Merged
merged 2 commits into from
Apr 15, 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
22 changes: 0 additions & 22 deletions components/brave_ads/core/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -741,28 +741,6 @@ static_library("internal") {
"legacy_migration/database/database_creation.h",
"legacy_migration/database/database_migration.cc",
"legacy_migration/database/database_migration.h",
"legacy_migration/rewards/legacy_rewards_migration.cc",
"legacy_migration/rewards/legacy_rewards_migration.h",
"legacy_migration/rewards/legacy_rewards_migration_payment_tokens_json_reader.cc",
"legacy_migration/rewards/legacy_rewards_migration_payment_tokens_json_reader.h",
"legacy_migration/rewards/legacy_rewards_migration_payment_tokens_json_reader_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_payment_tokens_json_reader_util.h",
"legacy_migration/rewards/legacy_rewards_migration_payments_json_reader.cc",
"legacy_migration/rewards/legacy_rewards_migration_payments_json_reader.h",
"legacy_migration/rewards/legacy_rewards_migration_payments_json_reader_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_payments_json_reader_util.h",
"legacy_migration/rewards/legacy_rewards_migration_payments_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_payments_util.h",
"legacy_migration/rewards/legacy_rewards_migration_transaction_constants.h",
"legacy_migration/rewards/legacy_rewards_migration_transaction_history_json_reader.cc",
"legacy_migration/rewards/legacy_rewards_migration_transaction_history_json_reader.h",
"legacy_migration/rewards/legacy_rewards_migration_transaction_history_json_reader_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_transaction_history_json_reader_util.h",
"legacy_migration/rewards/legacy_rewards_migration_transaction_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_transaction_util.h",
"legacy_migration/rewards/legacy_rewards_migration_util.cc",
"legacy_migration/rewards/legacy_rewards_migration_util.h",
"legacy_migration/rewards/payment_info.h",
"ml/data/data.cc",
"ml/data/data.h",
"ml/data/data_types.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h"
#include "brave/components/brave_ads/core/internal/common/logging_util.h"
#include "brave/components/brave_ads/core/internal/common/time/time_util.h"
#include "brave/components/brave_ads/core/internal/legacy_migration/rewards/legacy_rewards_migration_transaction_constants.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"

namespace brave_ads::database::table {
Expand Down Expand Up @@ -302,7 +301,6 @@ void Transactions::Reconcile(const PaymentTokenList& payment_tokens,
for (const auto& payment_token : payment_tokens) {
transaction_ids.push_back(payment_token.transaction_id);
}
transaction_ids.emplace_back(rewards::kMigrationUnreconciledTransactionId);

mojom::DBTransactionInfoPtr transaction = mojom::DBTransactionInfo::New();
mojom::DBCommandInfoPtr command = mojom::DBCommandInfo::New();
Expand Down Expand Up @@ -331,8 +329,6 @@ void Transactions::Reconcile(const PaymentTokenList& payment_tokens,
++index;
}

BindString(&*command, index, rewards::kMigrationUnreconciledTransactionId);

transaction->commands.push_back(std::move(command));

RunTransaction(std::move(transaction), std::move(callback));
Expand Down
17 changes: 0 additions & 17 deletions components/brave_ads/core/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "brave/components/brave_ads/core/internal/history/history_manager.h"
#include "brave/components/brave_ads/core/internal/legacy_migration/client/legacy_client_migration.h"
#include "brave/components/brave_ads/core/internal/legacy_migration/confirmations/legacy_confirmation_migration.h"
#include "brave/components/brave_ads/core/internal/legacy_migration/rewards/legacy_rewards_migration.h"
#include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_events.h"
#include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_info.h"
#include "brave/components/brave_ads/core/public/history/ad_content_value_util.h"
Expand Down Expand Up @@ -327,22 +326,6 @@ void AdsImpl::PurgeOrphanedAdEventsCallback(mojom::WalletInfoPtr wallet,
return FailedToInitialize(std::move(callback));
}

rewards::Migrate(base::BindOnce(&AdsImpl::MigrateRewardsStateCallback,
weak_factory_.GetWeakPtr(), std::move(wallet),
std::move(callback)));
}

void AdsImpl::MigrateRewardsStateCallback(mojom::WalletInfoPtr wallet,
InitializeCallback callback,
const bool success) {
if (!success) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Remove migration failure dumps.
base::debug::DumpWithoutCrashing();

return FailedToInitialize(std::move(callback));
}

MigrateClientState(base::BindOnce(&AdsImpl::MigrateClientStateCallback,
weak_factory_.GetWeakPtr(),
std::move(wallet), std::move(callback)));
Expand Down
3 changes: 0 additions & 3 deletions components/brave_ads/core/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ class AdsImpl final : public Ads {
void PurgeOrphanedAdEventsCallback(mojom::WalletInfoPtr wallet,
InitializeCallback callback,
bool success);
void MigrateRewardsStateCallback(mojom::WalletInfoPtr wallet,
InitializeCallback callback,
bool success);
void MigrateClientStateCallback(mojom::WalletInfoPtr wallet,
InitializeCallback callback,
bool success);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,32 +119,20 @@ bool ConfirmationStateManager::FromJson(const std::string& json) {
return false;
}

if (!ParseConfirmationTokensFromDictionary(*dict)) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Remove migration failure dumps.
base::debug::DumpWithoutCrashing();

BLOG(1, "Failed to parse confirmation tokens");
}
ParseConfirmationTokensFromDictionary(*dict);

if (!ParsePaymentTokensFromDictionary(*dict)) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Remove migration failure dumps.
base::debug::DumpWithoutCrashing();

BLOG(1, "Failed to parse payment tokens");
}
ParsePaymentTokensFromDictionary(*dict);

return true;
}

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

bool ConfirmationStateManager::ParseConfirmationTokensFromDictionary(
void ConfirmationStateManager::ParseConfirmationTokensFromDictionary(
const base::Value::Dict& dict) {
const auto* const list = dict.FindList("unblinded_tokens");
if (!list) {
return false;
return;
}

ConfirmationTokenList filtered_confirmation_tokens =
Expand All @@ -167,20 +155,13 @@ bool ConfirmationStateManager::ParseConfirmationTokensFromDictionary(
}

confirmation_tokens_.Set(filtered_confirmation_tokens);

return true;
}

bool ConfirmationStateManager::ParsePaymentTokensFromDictionary(
void ConfirmationStateManager::ParsePaymentTokensFromDictionary(
const base::Value::Dict& dict) {
const auto* const list = dict.FindList("unblinded_payment_tokens");
if (!list) {
return false;
if (const auto* const list = dict.FindList("unblinded_payment_tokens")) {
payment_tokens_.SetTokens(PaymentTokensFromValue(*list));
}

payment_tokens_.SetTokens(PaymentTokensFromValue(*list));

return true;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class ConfirmationStateManager final {
void LoadCallback(InitializeCallback callback,
const std::optional<std::string>& json);

bool ParseConfirmationTokensFromDictionary(const base::Value::Dict& dict);
void ParseConfirmationTokensFromDictionary(const base::Value::Dict& dict);

bool ParsePaymentTokensFromDictionary(const base::Value::Dict& dict);
void ParsePaymentTokensFromDictionary(const base::Value::Dict& dict);

bool is_initialized_ = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void MigrateClientState(InitializeCallback callback) {
!client.FromJson(mutable_json)) {
// The client state is corrupted, therefore, reset it to the
// default values for version 6.
BLOG(0,
"Client state is corrupted, resetting to default values");
mutable_json = "{}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void FailedToMigrate(InitializeCallback callback) {
}

void SuccessfullyMigrated(InitializeCallback callback) {
SetProfileBooleanPref("brave.brave_ads.state.has_migrated.confirmations.v7",
SetProfileBooleanPref("brave.brave_ads.state.has_migrated.confirmations.v8",
true);
SetProfileBooleanPref(prefs::kHasMigratedConfirmationState, true);
std::move(callback).Run(/*success=*/true);
Expand All @@ -54,11 +54,14 @@ void MigrateConfirmationState(InitializeCallback callback) {
std::string mutable_json = *json;

if (!GetProfileBooleanPref(
"brave.brave_ads.state.has_migrated.confirmations.v7") &&
"brave.brave_ads.state.has_migrated.confirmations.v8") &&
!ConfirmationStateManager::GetInstance().FromJson(
mutable_json)) {
// The confirmation state is corrupted, therefore, reset it to
// the default values for version 7.
// the default values for version 8.
BLOG(0,
"Confirmation state is corrupted, resetting to default "
"values");
mutable_json = "{}";
}

Expand All @@ -74,39 +77,59 @@ void MigrateConfirmationState(InitializeCallback callback) {

BLOG(1, "Migrating confirmation state");

const std::optional<ConfirmationList> confirmations =
json::reader::ReadConfirmations(mutable_json);
if (!confirmations) {
// Confirmation queue state does not exist.
return SuccessfullyMigrated(std::move(callback));
}

ConfirmationQueueItemList confirmation_queue_items;
for (const auto& confirmation : *confirmations) {
const ConfirmationQueueItemInfo confirmation_queue_item =
BuildConfirmationQueueItem(confirmation,
/*process_at=*/base::Time::Now());
confirmation_queue_items.push_back(confirmation_queue_item);
}

database::table::ConfirmationQueue database_table;
database_table.Save(
confirmation_queue_items,
base::BindOnce(
[](InitializeCallback callback, const bool success) {
if (!success) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Remove migration failure dumps.
base::debug::DumpWithoutCrashing();

BLOG(0, "Failed to save confirmation state");
return FailedToMigrate(std::move(callback));
}

BLOG(3, "Successfully migrated confirmation state");
SuccessfullyMigrated(std::move(callback));
},
std::move(callback)));
Save(kConfirmationStateFilename, mutable_json,
base::BindOnce(
[](const std::string& json, InitializeCallback callback,
const bool success) {
if (!success) {
BLOG(0, "Failed to save confirmation state");
return FailedToMigrate(std::move(callback));
}

BLOG(9, "Successfully saved confirmation state");

const std::optional<ConfirmationList> confirmations =
json::reader::ReadConfirmations(json);
if (!confirmations) {
// Confirmation queue state does not exist.
return SuccessfullyMigrated(std::move(callback));
}

ConfirmationQueueItemList confirmation_queue_items;
for (const auto& confirmation : *confirmations) {
const ConfirmationQueueItemInfo
confirmation_queue_item =
BuildConfirmationQueueItem(
confirmation,
/*process_at=*/base::Time::Now());
confirmation_queue_items.push_back(
confirmation_queue_item);
}

database::table::ConfirmationQueue database_table;
database_table.Save(
confirmation_queue_items,
base::BindOnce(
[](InitializeCallback callback,
const bool success) {
if (!success) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Remove migration failure dumps.
base::debug::DumpWithoutCrashing();

BLOG(0,
"Failed to save confirmation state");
return FailedToMigrate(std::move(callback));
}

BLOG(3,
"Successfully migrated confirmation "
"state");
SuccessfullyMigrated(std::move(callback));
},
std::move(callback)));
},
mutable_json, std::move(callback)));
},
std::move(callback)));
}
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading