Skip to content

Commit

Permalink
Merge pull request #6274 from brave/fix-rewards-reset-windows
Browse files Browse the repository at this point in the history
Fix Rewards reset failing on Windows
  • Loading branch information
NejcZdovc authored Aug 3, 2020
2 parents c98d538 + eb52c3a commit db8bb27
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 39 deletions.
104 changes: 67 additions & 37 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,6 @@ bool ResetOnFileTaskRunner(const base::FilePath& path) {
return base::DeleteFile(path, false);
}

bool ResetOnFilesTaskRunner(const std::vector<base::FilePath>& 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);
Expand Down Expand Up @@ -1458,7 +1447,7 @@ void RewardsServiceImpl::SetRewardsMainEnabled(bool enabled) {

if (!enabled) {
RecordRewardsDisabledForSomeMetrics();
StopLedger();
StopLedger(base::DoNothing());
}
}

Expand All @@ -1471,25 +1460,70 @@ 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");
Reset();
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<base::FilePath> 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() {
Expand Down Expand Up @@ -2428,6 +2462,10 @@ void RewardsServiceImpl::DiagnosticLog(
return;
}

if (resetting_rewards_) {
return;
}

if (profile_->IsOffTheRecord()) {
return;
}
Expand Down Expand Up @@ -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<base::FilePath> 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);
}

Expand Down
14 changes: 12 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ using ExternalWalletAuthorizationCallback =
const ledger::Result,
const std::map<std::string, std::string>&)>;

using StopLedgerCallback = base::OnceCallback<void(ledger::Result)>;

class RewardsServiceImpl : public RewardsService,
public ledger::LedgerClient,
public base::SupportsWeakPtr<RewardsServiceImpl> {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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_;

Expand Down

0 comments on commit db8bb27

Please sign in to comment.