Skip to content

Commit

Permalink
Adds new flow
Browse files Browse the repository at this point in the history
Resolves #1457
  • Loading branch information
NejcZdovc committed Jan 29, 2019
1 parent 1745396 commit 600a294
Show file tree
Hide file tree
Showing 22 changed files with 317 additions and 118 deletions.
28 changes: 24 additions & 4 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnIsWalletCreated(bool created);
void GetPendingContributionsTotal(const base::ListValue* args);
void OnGetPendingContributionsTotal(double amount);
void OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) override;

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
Expand All @@ -116,7 +117,6 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGrantFinish(brave_rewards::RewardsService* rewards_service,
unsigned int result,
brave_rewards::Grant grant) override;
void OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) override;
void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service,
std::string publisher_id) override;
void OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
Expand All @@ -137,6 +137,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
brave_rewards::RewardsService* rewards_service,
bool rewards_main_enabled) override;

void OnNormalizedPublisherList(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList list) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -550,11 +554,14 @@ void RewardsDOMHandler::GetAddresses(const base::ListValue* args) {

void RewardsDOMHandler::OnAutoContributePropsReady(
std::unique_ptr<brave_rewards::AutoContributeProps> props) {
rewards_service_->GetContentSiteList(0, 0,
props->contribution_min_time, props->reconcile_stamp,
rewards_service_->GetContentSiteList(
0,
0,
props->contribution_min_time,
props->reconcile_stamp,
props->contribution_non_verified,
base::Bind(&RewardsDOMHandler::OnContentSiteList,
weak_factory_.GetWeakPtr()));
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnContentSiteUpdated(
Expand Down Expand Up @@ -889,6 +896,19 @@ void RewardsDOMHandler::OnRewardsMainEnabled(
}
}


void RewardsDOMHandler::OnNormalizedPublisherList(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {
std::unique_ptr<brave_rewards::ContentSiteList> site_list(
new brave_rewards::ContentSiteList);
for (auto& publisher : list) {
site_list->push_back(publisher);
}

OnContentSiteList(std::move(site_list), 0);
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
32 changes: 26 additions & 6 deletions components/brave_rewards/browser/publisher_info_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,11 @@ PublisherInfoDatabase::GetPanelPublisher(
"SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, "
"pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi "
"LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id "
"WHERE pi.publisher_id=? AND ((ai.month = ? "
"AND ai.year = ? AND ai.reconcile_stamp = ?) OR "
"ai.percent IS NULL) LIMIT 1"));
"WHERE pi.publisher_id=? AND "
"(ai.reconcile_stamp = ? OR ai.percent IS NULL) LIMIT 1"));

info_sql.BindString(0, filter.id);
info_sql.BindInt(1, filter.month);
info_sql.BindInt(2, filter.year);
info_sql.BindInt64(3, filter.reconcile_stamp);
info_sql.BindInt64(1, filter.reconcile_stamp);

if (info_sql.Step()) {
std::unique_ptr<ledger::PublisherInfo> info;
Expand Down Expand Up @@ -439,6 +436,29 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo(
return activity_info_insert.Run();
}

bool PublisherInfoDatabase::InsertOrUpdateActivityInfos(
const ledger::PublisherInfoList& list) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

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

if (!initialized) {
return false;
}

sql::Transaction transaction(&GetDB());
if (!transaction.Begin()) {
return false;
}

for (const auto& info : list) {
InsertOrUpdateActivityInfo(info);
}

return transaction.Commit();
}

bool PublisherInfoDatabase::GetActivityList(
int start,
int limit,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/publisher_info_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class PublisherInfoDatabase {

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

bool InsertOrUpdateActivityInfos(const ledger::PublisherInfoList& list);

bool GetActivityList(int start,
int limit,
const ledger::ActivityInfoFilter& filter,
Expand Down
86 changes: 78 additions & 8 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1471,12 +1471,6 @@ void RewardsServiceImpl::SetAutoContribute(bool enabled) const {
bat_ledger_->SetAutoContribute(enabled);
}

void RewardsServiceImpl::TriggerOnContentSiteUpdated() {
// Should only be called from one point
for (auto& observer : observers_)
observer.OnContentSiteUpdated(this);
}

void RewardsServiceImpl::TriggerOnRewardsMainEnabled(bool rewards_main_enabled) {
for (auto& observer : observers_)
observer.OnRewardsMainEnabled(this, rewards_main_enabled);
Expand Down Expand Up @@ -2337,13 +2331,89 @@ bool RestorePublisherOnFileTaskRunner(PublisherInfoDatabase* backend) {
return backend->RestorePublishers();
}

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

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

std::unique_ptr<ledger::PublisherInfoList>
SaveNormalizedPublisherListOnFileTaskRunner(PublisherInfoDatabase* backend,
const ledger::PublisherInfoList& list) {
if (!backend) {
return nullptr;
}

bool success = backend->InsertOrUpdateActivityInfos(list);

if (!success) {
return nullptr;
}

std::unique_ptr<ledger::PublisherInfoList> new_list(
new ledger::PublisherInfoList);

for (auto& publisher : list) {
new_list->push_back(publisher);
}

return new_list;
}

void RewardsServiceImpl::SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& list) {

if (list.list_.size() == 0) {
std::unique_ptr<ledger::PublisherInfoList> empty_list(
new ledger::PublisherInfoList);
OnNormalizedPublisherListSaved(std::move(empty_list));
return;
}

base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::Bind(&SaveNormalizedPublisherListOnFileTaskRunner,
publisher_info_backend_.get(),
list.list_),
base::Bind(&RewardsServiceImpl::OnNormalizedPublisherListSaved,
AsWeakPtr()));
}

bool sortPublisherByPercentage(ContentSite a, ContentSite b) {
return a.percentage > b.percentage;
}

void RewardsServiceImpl::OnNormalizedPublisherListSaved(
std::unique_ptr<ledger::PublisherInfoList> list) {
if (!list) {
LOG(ERROR) << "Problem saving normalized publishers "
"in SaveNormalizedPublisherList";
return;
}

ContentSiteList site_list;
for (auto& publisher : *list) {
site_list.push_back(PublisherInfoToContentSite(publisher));
}

sort(site_list.begin(), site_list.end(), sortPublisherByPercentage);

for (auto& observer : observers_) {
observer.OnNormalizedPublisherList(this, site_list);
}
}

} // namespace brave_rewards
10 changes: 9 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ class RewardsServiceImpl : public RewardsService,
void OnPublishersListSaved(ledger::LedgerCallbackHandler* handler,
bool success);
void OnTimer(uint32_t timer_id);
void TriggerOnContentSiteUpdated();
void OnPublisherListLoaded(ledger::LedgerCallbackHandler* handler,
const std::string& data);

Expand Down Expand Up @@ -335,6 +334,15 @@ class RewardsServiceImpl : public RewardsService,

void OnSavePendingContribution(ledger::Result result);

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

void SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& list) override;

void OnNormalizedPublisherListSaved(
std::unique_ptr<ledger::PublisherInfoList> list);

// URLFetcherDelegate impl
void OnURLFetchComplete(const net::URLFetcher* source) override;

Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class RewardsServiceObserver : public base::CheckedObserver {
virtual void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {};
virtual void OnNormalizedPublisherList(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {};
// DO NOT ADD ANY MORE METHODS HERE UNLESS IT IS A BROADCAST NOTIFICATION
// RewardsServiceObserver should not be used to return responses to the caller.
// Method calls on RewardsService should use callbacks to return responses.
Expand Down
11 changes: 11 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,17 @@ void BatLedgerClientMojoProxy::GetActivityInfoList(uint32_t start,
base::BindOnce(&OnGetActivityInfoList, std::move(callback)));
}



void BatLedgerClientMojoProxy::SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& normalized_list) {
if (!Connected()) {
return;
}

bat_ledger_client_->SaveNormalizedPublisherList(normalized_list.ToJson());
}

bool BatLedgerClientMojoProxy::Connected() const {
return bat_ledger_client_.is_bound();
}
Expand Down
3 changes: 3 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
ledger::ActivityInfoFilter filter,
ledger::PublisherInfoListCallback callback) override;

void SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& normalized_list) override;

private:
bool Connected() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,13 @@ void LedgerClientMojoProxy::GetActivityInfoList(uint32_t start,

}

void LedgerClientMojoProxy::SaveNormalizedPublisherList(
const std::string& normalized_list) {

ledger::PublisherInfoListStruct list;
list.loadFromJson(normalized_list);

ledger_client_->SaveNormalizedPublisherList(list);
}

} // namespace bat_ledger
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
const std::string& filter,
GetActivityInfoListCallback callback) override;

void SaveNormalizedPublisherList(
const std::string& normalized_list) override;

private:
// workaround to pass base::OnceCallback into std::bind
// also serves as a wrapper for passing ledger::LedgerCallbackHandler*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,6 @@ interface BatLedgerClient {

GetActivityInfoList(uint32 start, uint32 limit, string filter) => (
array<string> publisher_info_list, uint32 next_record);

SaveNormalizedPublisherList(string list);
};
6 changes: 3 additions & 3 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ class LEDGER_EXPORT Ledger {
virtual std::string URIEncode(const std::string& value) = 0;

virtual void SetPublisherInfo(std::unique_ptr<PublisherInfo> publisher_info,
PublisherInfoCallback callback) = 0;
virtual void SetActivityInfo(std::unique_ptr<PublisherInfo> publisher_info,
PublisherInfoCallback callback) = 0;
uint64_t window_id) = 0;
virtual void SetActivityInfo(
std::unique_ptr<PublisherInfo> publisher_info) = 0;
virtual void GetPublisherInfo(const std::string& publisher_key,
PublisherInfoCallback callback) = 0;
virtual void GetActivityInfo(const ledger::ActivityInfoFilter& filter,
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ class LEDGER_EXPORT LedgerClient {
const ledger::LogLevel log_level) const = 0;

virtual void OnRestorePublishers(ledger::OnRestoreCallback callback) = 0;

virtual void SaveNormalizedPublisherList(
const ledger::PublisherInfoListStruct& normalized_list) = 0;
};

} // namespace ledger
Expand Down
11 changes: 11 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/publisher_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ LEDGER_EXPORT struct PublisherInfo {
std::vector<ContributionInfo> contributions;
};

LEDGER_EXPORT struct PublisherInfoListStruct {
PublisherInfoListStruct();
~PublisherInfoListStruct();
PublisherInfoListStruct(const PublisherInfoListStruct& data);

const std::string ToJson() const;
bool loadFromJson(const std::string& json);

std::vector<PublisherInfo> list_;
};

using PublisherInfoList = std::vector<PublisherInfo>;

} // namespace ledger
Expand Down
Loading

0 comments on commit 600a294

Please sign in to comment.