diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 8edf02c92d65..9af6d4861bd0 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -216,17 +216,6 @@ bool ResetOnFileTaskRunner(const base::FilePath& path) { return base::DeleteFile(path, false); } -bool ResetOnFilesTaskRunner(const std::vector& paths) { - bool res = true; - for (size_t i = 0; i < paths.size(); i++) { - if (!base::DeleteFileRecursively(paths[i])) { - res = false; - } - } - - return res; -} - void EnsureRewardsBaseDirectoryExists(const base::FilePath& path) { if (!DirectoryExists(path)) base::CreateDirectory(path); @@ -1458,7 +1447,7 @@ void RewardsServiceImpl::SetRewardsMainEnabled(bool enabled) { if (!enabled) { RecordRewardsDisabledForSomeMetrics(); - StopLedger(); + StopLedger(base::DoNothing()); } } @@ -1471,7 +1460,7 @@ void RewardsServiceImpl::EnableGreaseLion(const bool enabled) { #endif } -void RewardsServiceImpl::StopLedger() { +void RewardsServiceImpl::StopLedger(StopLedgerCallback callback) { BLOG(1, "Shutting down ledger process"); if (!Connected()) { BLOG(1, "Ledger process not running"); @@ -1479,17 +1468,62 @@ void RewardsServiceImpl::StopLedger() { return; } - bat_ledger_->Shutdown( - base::BindOnce(&RewardsServiceImpl::OnStopLedger, AsWeakPtr())); + bat_ledger_->Shutdown(base::BindOnce( + &RewardsServiceImpl::OnStopLedger, + AsWeakPtr(), + std::move(callback))); } -void RewardsServiceImpl::OnStopLedger(const ledger::Result result) { +void RewardsServiceImpl::OnStopLedger( + StopLedgerCallback callback, + const ledger::Result result) { BLOG_IF( 1, result != ledger::Result::LEDGER_OK, "Ledger process was not shut down successfully"); Reset(); BLOG(1, "Successfully shutdown ledger"); + std::move(callback).Run(result); +} + +void RewardsServiceImpl::OnStopLedgerForCompleteReset( + SuccessCallback callback, + const ledger::Result result) { + profile_->GetPrefs()->ClearPrefsWithPrefixSilently(pref_prefix); + + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::BindOnce( + &RewardsServiceImpl::ResetOnFilesTaskRunner, + base::Unretained(this)), + base::BindOnce( + &RewardsServiceImpl::OnCompleteReset, + AsWeakPtr(), + std::move(callback))); +} + +bool RewardsServiceImpl::ResetOnFilesTaskRunner() { + // Close any open files before deleting them (required on Windows) + diagnostic_log_.Close(); + + const std::vector paths = { + ledger_state_path_, + publisher_state_path_, + publisher_info_db_path_, + diagnostic_log_path_, + publisher_list_path_, + rewards_base_path_ + }; + + bool res = true; + for (size_t i = 0; i < paths.size(); i++) { + if (!base::DeleteFileRecursively(paths[i])) { + res = false; + } + } + + return res; } void RewardsServiceImpl::Reset() { @@ -2428,6 +2462,10 @@ void RewardsServiceImpl::DiagnosticLog( return; } + if (resetting_rewards_) { + return; + } + if (profile_->IsOffTheRecord()) { return; } @@ -3845,41 +3883,33 @@ void RewardsServiceImpl::ClearAllNotifications() { } void RewardsServiceImpl::CompleteReset(SuccessCallback callback) { + resetting_rewards_ = true; + + auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_); + if (ads_service) { + ads_service->ResetAllState(); + } + + StopNotificationTimers(); notification_service_->DeleteAllNotifications(true); - std::vector paths; - paths.push_back(ledger_state_path_); - paths.push_back(publisher_state_path_); - paths.push_back(publisher_info_db_path_); - paths.push_back(diagnostic_log_path_); - paths.push_back(publisher_list_path_); - paths.push_back(rewards_base_path_); - base::PostTaskAndReplyWithResult( - file_task_runner_.get(), - FROM_HERE, - base::BindOnce(&ResetOnFilesTaskRunner, paths), + auto stop_callback = base::BindOnce( - &RewardsServiceImpl::OnCompleteReset, + &RewardsServiceImpl::OnStopLedgerForCompleteReset, AsWeakPtr(), - std::move(callback))); + std::move(callback)); + StopLedger(std::move(stop_callback)); } void RewardsServiceImpl::OnCompleteReset( SuccessCallback callback, const bool success) { - profile_->GetPrefs()->ClearPrefsWithPrefixSilently(pref_prefix); - - auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_); - if (ads_service) { - ads_service->ResetAllState(); - } + resetting_rewards_ = false; for (auto& observer : observers_) { observer.OnCompleteReset(success); } - StopLedger(); - StopNotificationTimers(); std::move(callback).Run(true); } diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 9f9ed79ee4ae..3a519eba6929 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -89,6 +89,8 @@ using ExternalWalletAuthorizationCallback = const ledger::Result, const std::map&)>; +using StopLedgerCallback = base::OnceCallback; + class RewardsServiceImpl : public RewardsService, public ledger::LedgerClient, public base::SupportsWeakPtr { @@ -330,12 +332,19 @@ class RewardsServiceImpl : public RewardsService, void EnableGreaseLion(const bool enabled); - void StopLedger(); + void StopLedger(StopLedgerCallback callback); - void OnStopLedger(const ledger::Result result); + void OnStopLedger( + StopLedgerCallback callback, + const ledger::Result result); + void OnStopLedgerForCompleteReset( + SuccessCallback callback, + const ledger::Result result); void Reset(); + bool ResetOnFilesTaskRunner(); + void OnCreate(); void OnResult(ledger::ResultCallback callback, const ledger::Result result); @@ -761,6 +770,7 @@ class RewardsServiceImpl : public RewardsService, bool reset_states_; bool is_wallet_initialized_ = false; bool ledger_for_testing_ = false; + bool resetting_rewards_ = false; GetTestResponseCallback test_response_callback_;