Skip to content

Commit

Permalink
Refactors restore flow
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Nov 21, 2018
1 parent a416ec4 commit 8a5a24c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
21 changes: 21 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,27 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) {
return nullptr;
}

bool PublisherInfoDatabase::RestorePublishers() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
DCHECK(initialized);

if (!initialized) {
return false;
}

sql::Statement restore_q(db_.GetUniqueStatement(
"UPDATE publisher_info SET excluded=? WHERE excluded=?"));

restore_q.BindInt(0, static_cast<int>(
ledger::PUBLISHER_EXCLUDE::DEFAULT));
restore_q.BindInt(1, static_cast<int>(
ledger::PUBLISHER_EXCLUDE::EXCLUDED));

return restore_q.Run();
}

/**
*
* ACTIVITY INFO
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ class PublisherInfoDatabase {

bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info);

std::unique_ptr<ledger::PublisherInfo> GetPublisherInfo(
std::unique_ptr<ledger::PublisherInfo> GetPublisherInfo(
const std::string& media_key);

bool RestorePublishers();

bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info);

bool GetActivityList(int start,
Expand Down
29 changes: 29 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1661,4 +1661,33 @@ void RewardsServiceImpl::Log(ledger::LogLevel level, const std::string& text) {
VLOG(level) << text;
}

bool RestorePublisherOnFileTaskRunner(PublisherInfoDatabase* backend) {
if (!backend) {
return false;
}

return backend->RestorePublishers();
}

void RewardsServiceImpl::OnRestorePublishers(ledger::OnRestoreCallback callback) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::Bind(&RestorePublisherOnFileTaskRunner,
publisher_info_backend_.get()),
base::Bind(&RewardsServiceImpl::OnRestorePublishersInternal,
AsWeakPtr(),
callback));
}

void RewardsServiceImpl::OnRestorePublishersInternal(
ledger::OnRestoreCallback callback,
bool result) {
callback(result);

if (result) {
TriggerOnContentSiteUpdated();
}
}

} // namespace brave_rewards
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class RewardsServiceImpl : public RewardsService,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t windowId);
void OnRestorePublishersInternal(ledger::OnRestoreCallback callback,
bool result);

// ledger::LedgerClient
std::string GenerateGUID() const override;
Expand Down Expand Up @@ -272,6 +274,7 @@ class RewardsServiceImpl : public RewardsService,
const ledger::REWARDS_CATEGORY category) override;
void GetRecurringDonations(ledger::PublisherInfoListCallback callback) override;
void Log(ledger::LogLevel level, const std::string& text) override;
void OnRestorePublishers(ledger::OnRestoreCallback callback) override;

void OnIOTaskComplete(std::function<void(void)> callback);

Expand Down

0 comments on commit 8a5a24c

Please sign in to comment.