diff --git a/include/bat/ledger/ledger_callback_handler.h b/include/bat/ledger/ledger_callback_handler.h index b3f2a1071ad9..c10d981590e3 100644 --- a/include/bat/ledger/ledger_callback_handler.h +++ b/include/bat/ledger/ledger_callback_handler.h @@ -14,6 +14,10 @@ namespace ledger { LEDGER_EXPORT enum Result { OK = 0, ERROR = 1, + NO_PUBLISHER_STATE = 2, + NO_LEDGER_STATE = 3, + INVALID_PUBLISHER_STATE = 4, + INVALID_LEDGER_STATE = 5, // some more useful result codes should go here }; diff --git a/src/bat_client.cc b/src/bat_client.cc index 5653baacde46..a6f6d0195a57 100644 --- a/src/bat_client.cc +++ b/src/bat_client.cc @@ -50,14 +50,17 @@ std::string BatClient::buildURL(const std::string& path, const std::string& pref return url + prefix + path; } -void BatClient::loadState(const std::string& data) { +bool BatClient::loadState(const std::string& data) { braveledger_bat_helper::CLIENT_STATE_ST state; - braveledger_bat_helper::loadFromJson(state, data.c_str()); + if (!braveledger_bat_helper::loadFromJson(state, data.c_str())) + return false; + LOG(ERROR) << "!!!bat address == " << state.walletInfo_.addressBAT_; LOG(ERROR) << "!!!card address == " << state.walletInfo_.addressCARD_ID_; state_.reset(new braveledger_bat_helper::CLIENT_STATE_ST(state)); + return true; } void BatClient::registerPersona() { diff --git a/src/bat_client.h b/src/bat_client.h index 679bb36a386e..57d98a263359 100644 --- a/src/bat_client.h +++ b/src/bat_client.h @@ -24,7 +24,7 @@ class BatClient { explicit BatClient(bat_ledger::LedgerImpl* ledger); ~BatClient(); - void loadState(const std::string& data); + bool loadState(const std::string& data); void registerPersona(); void requestCredentialsCallback(bool result, const std::string& response); void registerPersonaCallback(bool result, const std::string& response); diff --git a/src/bat_publishers.cc b/src/bat_publishers.cc index d31d5ddc6326..f87d8abb7648 100644 --- a/src/bat_publishers.cc +++ b/src/bat_publishers.cc @@ -285,11 +285,14 @@ void BatPublishers::saveState() { ledger_->SavePublisherState(data, this); } -void BatPublishers::loadState(const std::string& data) { +bool BatPublishers::loadState(const std::string& data) { braveledger_bat_helper::PUBLISHER_STATE_ST state; - braveledger_bat_helper::loadFromJson(state, data.c_str()); + if (!braveledger_bat_helper::loadFromJson(state, data.c_str())) + return false; + state_.reset(new braveledger_bat_helper::PUBLISHER_STATE_ST(state)); calcScoreConsts(); + return true; } void BatPublishers::OnPublisherStateSaved(ledger::Result result) { diff --git a/src/bat_publishers.h b/src/bat_publishers.h index 5faf5b365975..60f80d3257d5 100644 --- a/src/bat_publishers.h +++ b/src/bat_publishers.h @@ -32,7 +32,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { ~BatPublishers() override; - void loadState(const std::string& data); + bool loadState(const std::string& data); void saveVisit(const ledger::VisitData& visit_data, const uint64_t& duration); diff --git a/src/ledger_impl.cc b/src/ledger_impl.cc index 101fce954880..8ff63c19ed2c 100644 --- a/src/ledger_impl.cc +++ b/src/ledger_impl.cc @@ -33,7 +33,7 @@ LedgerImpl::~LedgerImpl() { } void LedgerImpl::Initialize() { - LoadPublisherState(this); + LoadLedgerState(this); } void LedgerImpl::CreateWallet() { @@ -105,15 +105,15 @@ void LedgerImpl::OnBackground(uint32_t tab_id, const uint64_t& current_time) { } void LedgerImpl::OnMediaStart(uint32_t tab_id, const uint64_t& current_time) { - // TODO + // TODO } void LedgerImpl::OnMediaStop(uint32_t tab_id, const uint64_t& current_time) { - // TODO + // TODO } void LedgerImpl::OnXHRLoad(uint32_t tab_id, const std::string& url) { - // TODO + // TODO //LOG(ERROR) << "!!!LedgerImpl::OnXHRLoad " << url; } @@ -123,10 +123,15 @@ void LedgerImpl::LoadLedgerState(ledger::LedgerCallbackHandler* handler) { void LedgerImpl::OnLedgerStateLoaded(ledger::Result result, const std::string& data) { - if (result == ledger::Result::OK) - bat_client_->loadState(data); + if (result == ledger::Result::OK) { + if (!bat_client_->loadState(data)) { + OnWalletInitialized(ledger::Result::INVALID_LEDGER_STATE); + } + } else { + OnWalletInitialized(result); + } - OnWalletInitialized(result); + LoadPublisherState(this); } void LedgerImpl::LoadPublisherState(ledger::LedgerCallbackHandler* handler) { @@ -135,13 +140,13 @@ void LedgerImpl::LoadPublisherState(ledger::LedgerCallbackHandler* handler) { void LedgerImpl::OnPublisherStateLoaded(ledger::Result result, const std::string& data) { - if (result != ledger::Result::OK) { - OnWalletInitialized(result); - return; + if (result == ledger::Result::OK) { + if (!bat_publishers_->loadState(data)) { + result = ledger::Result::INVALID_PUBLISHER_STATE; + } } - bat_publishers_->loadState(data); - LoadLedgerState(this); + OnWalletInitialized(result); } void LedgerImpl::SaveLedgerState(const std::string& data) {