From f7184a0eeac2f912f89a537ac13d2284950e9496 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Mon, 29 Jun 2020 18:48:15 +0200 Subject: [PATCH] Fixes initialization flow and how timers are triggered Resolves https://github.com/brave/brave-browser/issues/10510 --- .../src/bat/ledger/internal/ledger_impl.cc | 56 +++++++++++-------- .../src/bat/ledger/internal/ledger_impl.h | 13 ++++- .../bat/ledger/internal/state/state_util.cc | 8 ++- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index a1a904da37b6..1e0fa612ce03 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -99,28 +99,34 @@ LedgerImpl::~LedgerImpl() { } } -void LedgerImpl::OnWalletInitializedInternal( - ledger::Result result, +void LedgerImpl::OnInitialized( + const ledger::Result result, ledger::ResultCallback callback) { initializing_ = false; callback(result); - if (result == ledger::Result::LEDGER_OK || - result == ledger::Result::WALLET_CREATED) { + if (result == ledger::Result::LEDGER_OK) { initialized_ = true; - bat_publisher_->SetPublisherServerListTimer(GetRewardsMainEnabled()); - bat_contribution_->SetReconcileTimer(); - bat_promotion_->Refresh(false); - bat_contribution_->Initialize(); - bat_promotion_->Initialize(); - bat_api_->Initialize(); - braveledger_recovery::Check(this); - - SetConfirmationsWalletInfo(); + StartServices(); } else { BLOG(0, "Failed to initialize wallet " << result); } } +void LedgerImpl::StartServices() { + if (!IsWalletCreated()) { + return; + } + + bat_publisher_->SetPublisherServerListTimer(GetRewardsMainEnabled()); + bat_contribution_->SetReconcileTimer(); + bat_promotion_->Refresh(false); + bat_contribution_->Initialize(); + bat_promotion_->Initialize(); + bat_api_->Initialize(); + SetConfirmationsWalletInfo(); + braveledger_recovery::Check(this); +} + void LedgerImpl::Initialize( const bool execute_create_script, ledger::ResultCallback callback) { @@ -139,7 +145,7 @@ void LedgerImpl::InitializeDatabase( const bool execute_create_script, ledger::ResultCallback callback) { ledger::ResultCallback finish_callback = - std::bind(&LedgerImpl::OnWalletInitializedInternal, + std::bind(&LedgerImpl::OnInitialized, this, _1, std::move(callback)); @@ -262,16 +268,22 @@ bool LedgerImpl::IsConfirmationsRunning() { } void LedgerImpl::CreateWallet(ledger::ResultCallback callback) { - if (initializing_) { - return; - } - - initializing_ = true; - auto on_wallet = std::bind(&LedgerImpl::OnWalletInitializedInternal, + auto create_callback = std::bind(&LedgerImpl::OnCreateWallet, this, _1, - std::move(callback)); - bat_wallet_->CreateWalletIfNecessary(std::move(on_wallet)); + callback); + + bat_wallet_->CreateWalletIfNecessary(create_callback); +} + +void LedgerImpl::OnCreateWallet( + const ledger::Result result, + ledger::ResultCallback callback) { + if (result == ledger::Result::WALLET_CREATED) { + StartServices(); + } + + callback(result); } void LedgerImpl::OnLoad(ledger::VisitDataPtr visit_data, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 583e2605d197..71707c8e1b99 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -189,9 +189,6 @@ class LedgerImpl : public ledger::Ledger { void LoadPublisherState(ledger::OnLoadCallback callback); - void OnWalletInitializedInternal(ledger::Result result, - ledger::ResultCallback callback); - void GetRewardsParameters( ledger::GetRewardsParametersCallback callback) override; @@ -722,6 +719,12 @@ class LedgerImpl : public ledger::Ledger { ledger::GetCredsBatchListCallback callback); private: + void OnInitialized( + const ledger::Result result, + ledger::ResultCallback callback); + + void StartServices(); + void OnStateInitialized( const ledger::Result result, ledger::ResultCallback callback); @@ -747,6 +750,10 @@ class LedgerImpl : public ledger::Ledger { bool IsConfirmationsRunning(); + void OnCreateWallet( + const ledger::Result result, + ledger::ResultCallback callback); + void OnLoad(ledger::VisitDataPtr visit_data, const uint64_t& current_time) override; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc index f13cc2cb407e..e42e1385d81d 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc @@ -171,7 +171,13 @@ double GetAutoContributionAmount(bat_ledger::LedgerImpl* ledger) { uint64_t GetReconcileStamp(bat_ledger::LedgerImpl* ledger) { DCHECK(ledger); - return ledger->GetUint64State(ledger::kStateNextReconcileStamp); + auto stamp = ledger->GetUint64State(ledger::kStateNextReconcileStamp); + if (stamp == 0) { + ledger->ResetReconcileStamp(); + stamp = ledger->GetUint64State(ledger::kStateNextReconcileStamp); + } + + return stamp; } void SetReconcileStamp(