From 600a294969d294b5abb329f1b32cd559bd770fdc Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 24 Jan 2019 18:36:53 +0100 Subject: [PATCH] Adds new flow Resolves https://github.com/brave/brave-core/pull/1457 --- browser/ui/webui/brave_rewards_ui.cc | 28 +++++- .../browser/publisher_info_database.cc | 32 +++++-- .../browser/publisher_info_database.h | 2 + .../browser/rewards_service_impl.cc | 86 ++++++++++++++++-- .../browser/rewards_service_impl.h | 10 ++- .../browser/rewards_service_observer.h | 3 + .../bat_ledger_client_mojo_proxy.cc | 11 +++ .../bat_ledger/bat_ledger_client_mojo_proxy.h | 3 + .../public/cpp/ledger_client_mojo_proxy.cc | 9 ++ .../public/cpp/ledger_client_mojo_proxy.h | 3 + .../public/interfaces/bat_ledger.mojom | 2 + .../include/bat/ledger/ledger.h | 6 +- .../include/bat/ledger/ledger_client.h | 3 + .../include/bat/ledger/publisher_info.h | 11 +++ .../src/bat/ledger/ledger.cc | 39 ++++++++ .../bat-native-ledger/src/bat_contribution.cc | 2 +- vendor/bat-native-ledger/src/bat_helper.cc | 14 +++ .../bat-native-ledger/src/bat_publishers.cc | 89 ++++++------------- vendor/bat-native-ledger/src/bat_publishers.h | 16 ++-- vendor/bat-native-ledger/src/ledger_impl.cc | 46 +++++----- vendor/bat-native-ledger/src/ledger_impl.h | 19 ++-- .../src/rapidjson_bat_helper.h | 1 + 22 files changed, 317 insertions(+), 118 deletions(-) diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index 3446ba0a30b5..add41df80ef5 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -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, @@ -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, @@ -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, @@ -550,11 +554,14 @@ void RewardsDOMHandler::GetAddresses(const base::ListValue* args) { void RewardsDOMHandler::OnAutoContributePropsReady( std::unique_ptr 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( @@ -889,6 +896,19 @@ void RewardsDOMHandler::OnRewardsMainEnabled( } } + +void RewardsDOMHandler::OnNormalizedPublisherList( + brave_rewards::RewardsService* rewards_service, + brave_rewards::ContentSiteList list) { + std::unique_ptr 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) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 58b296d603d4..00b335a2a564 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -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 info; @@ -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, diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 45a0cfe19244..c539c3d2d57e 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -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, diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 3dac16bcb541..48c284992524 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -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); @@ -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 +SaveNormalizedPublisherListOnFileTaskRunner(PublisherInfoDatabase* backend, + const ledger::PublisherInfoList& list) { + if (!backend) { + return nullptr; + } + + bool success = backend->InsertOrUpdateActivityInfos(list); + + if (!success) { + return nullptr; + } + + std::unique_ptr 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 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 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 diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 40f09af07a3b..eda1112fdc07 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -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); @@ -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 list); + // URLFetcherDelegate impl void OnURLFetchComplete(const net::URLFetcher* source) override; diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index 37e4d993feca..7d58fc47f893 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -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. diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc index 467c5c30d833..23458f1547a8 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -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(); } diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h index 7a06912c8ddb..eaba1cd668ac 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -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; diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc index d51499295286..daf8a89365a1 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc @@ -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 diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h index e0f36e545992..3795b47b72b5 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h @@ -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* diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index b8d82c6fde4e..f824f3a34962 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -164,4 +164,6 @@ interface BatLedgerClient { GetActivityInfoList(uint32 start, uint32 limit, string filter) => ( array publisher_info_list, uint32 next_record); + + SaveNormalizedPublisherList(string list); }; diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 7d795ce3ee89..507ba5efe535 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -121,9 +121,9 @@ class LEDGER_EXPORT Ledger { virtual std::string URIEncode(const std::string& value) = 0; virtual void SetPublisherInfo(std::unique_ptr publisher_info, - PublisherInfoCallback callback) = 0; - virtual void SetActivityInfo(std::unique_ptr publisher_info, - PublisherInfoCallback callback) = 0; + uint64_t window_id) = 0; + virtual void SetActivityInfo( + std::unique_ptr publisher_info) = 0; virtual void GetPublisherInfo(const std::string& publisher_key, PublisherInfoCallback callback) = 0; virtual void GetActivityInfo(const ledger::ActivityInfoFilter& filter, diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index c625893ac3ce..aa28e0ffb159 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -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 diff --git a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h index 6c164431c9c0..cc737aa67c43 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -137,6 +137,17 @@ LEDGER_EXPORT struct PublisherInfo { std::vector contributions; }; +LEDGER_EXPORT struct PublisherInfoListStruct { + PublisherInfoListStruct(); + ~PublisherInfoListStruct(); + PublisherInfoListStruct(const PublisherInfoListStruct& data); + + const std::string ToJson() const; + bool loadFromJson(const std::string& json); + + std::vector list_; +}; + using PublisherInfoList = std::vector; } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index df46502413bd..3183fe0bd603 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -745,4 +745,43 @@ bool PendingContributionList::loadFromJson(const std::string& json) { return !error; } +PublisherInfoListStruct::PublisherInfoListStruct () {} +PublisherInfoListStruct::~PublisherInfoListStruct () {} +PublisherInfoListStruct::PublisherInfoListStruct ( + const ledger::PublisherInfoListStruct &properties) { + list_ = properties.list_; +} + +const std::string PublisherInfoListStruct::ToJson() const { + std::string json; + braveledger_bat_helper::saveToJsonString(*this, json); + return json; +} + +bool PublisherInfoListStruct::loadFromJson(const std::string& json) { + rapidjson::Document d; + d.Parse(json.c_str()); + + // has parser errors or wrong types + bool error = d.HasParseError(); + + if (false == error) { + error = !(d.HasMember("list") && d["list"].IsArray()); + } + + if (false == error) { + for (const auto& g : d["list"].GetArray()) { + rapidjson::StringBuffer sb; + rapidjson::Writer writer(sb); + g.Accept(writer); + + PublisherInfo contribution; + contribution.loadFromJson(sb.GetString()); + list_.push_back(contribution); + } + } + + return !error; +} + } // ledger diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index 7e0fe904f3e0..325aa24398f6 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -173,7 +173,7 @@ void BatContribution::ReconcilePublisherList( if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { ledger::PublisherInfoList normalized_list; - ledger_->NormalizeContributeWinners(&normalized_list, false, list, 0); + ledger_->NormalizeContributeWinners(&normalized_list, list, 0); std::sort(normalized_list.begin(), normalized_list.end()); verified_list = GetVerifiedListAuto(viewing_id, normalized_list, budget); } else { diff --git a/vendor/bat-native-ledger/src/bat_helper.cc b/vendor/bat-native-ledger/src/bat_helper.cc index 58c617c42270..ea1ac34a1643 100644 --- a/vendor/bat-native-ledger/src/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat_helper.cc @@ -2769,4 +2769,18 @@ static bool ignore_ = false; writer.EndObject(); } + void saveToJson(JsonWriter& writer, + const ledger::PublisherInfoListStruct& publishers) { + writer.StartObject(); + + writer.String("list"); + writer.StartArray(); + for (const auto& publisher : publishers.list_) { + saveToJson(writer, publisher); + } + writer.EndArray(); + + writer.EndObject(); + } + } // namespace braveledger_bat_helper diff --git a/vendor/bat-native-ledger/src/bat_publishers.cc b/vendor/bat-native-ledger/src/bat_publishers.cc index bf8c6469be51..d94957af8744 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat_publishers.cc @@ -75,12 +75,6 @@ void BatPublishers::AddRecurringPayment(const std::string& publisher_id, const d saveState(); } -void onVisitSavedDummy(ledger::Result result, - std::unique_ptr publisher_info) { - - // onPublisherInfoUpdated will always be called by LedgerImpl so do nothing -} - void BatPublishers::saveVisit(const std::string& publisher_id, const ledger::VisitData& visit_data, const uint64_t& duration, @@ -254,8 +248,7 @@ void BatPublishers::saveVisitInternal( verified_new)) { panel_info = std::make_unique(*publisher_info); - ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&onVisitSavedDummy, _1, _2)); + ledger_->SetPublisherInfo(std::move(publisher_info)); } else if (!excluded && ledger_->GetAutoContribute() && min_duration_ok && @@ -267,8 +260,7 @@ void BatPublishers::saveVisitInternal( panel_info = std::make_unique(*publisher_info); - ledger_->SetActivityInfo(std::move(publisher_info), - std::bind(&onVisitSavedDummy, _1, _2)); + ledger_->SetActivityInfo(std::move(publisher_info)); } if (panel_info && window_id > 0) { @@ -305,8 +297,7 @@ void BatPublishers::onFetchFavIconDBResponse( std::unique_ptr panel_info = std::make_unique(*info); - ledger_->SetPublisherInfo(std::move(info), - std::bind(&onVisitSavedDummy, _1, _2)); + ledger_->SetPublisherInfo(std::move(info)); if (window_id > 0) { ledger::VisitData visit_data; @@ -321,20 +312,16 @@ void BatPublishers::onFetchFavIconDBResponse( } } -std::unique_ptr BatPublishers::onPublisherInfoUpdated( - ledger::Result result, std::unique_ptr info) { +void BatPublishers::OnPublisherInfoSaved( + uint64_t window_id, + ledger::Result result, + std::unique_ptr info) { if (result != ledger::Result::LEDGER_OK || !info.get()) { - // TODO error handling - return info; - } - - if (!isEligibleForContribution(*info)) { - return info; + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "Publisher info was not saved!"; } - synopsisNormalizer(); - - return info; + SynopsisNormalizer(); } void BatPublishers::setExclude(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& exclude) { @@ -382,23 +369,11 @@ void BatPublishers::onSetExcludeInternal( std::string publisherKey = publisher_info->id; - ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&BatPublishers::onSetPublisherInfo, - this, - _1, - _2)); + ledger_->SetPublisherInfo(std::move(publisher_info)); OnExcludedSitesChanged(publisherKey); } -void BatPublishers::onSetPublisherInfo(ledger::Result result, - std::unique_ptr publisher_info) { - if (result != ledger::Result::LEDGER_OK) { - return; - } - synopsisNormalizer(); -} - void BatPublishers::onSetPanelExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, uint64_t windowId, ledger::Result result, @@ -425,13 +400,7 @@ void BatPublishers::onSetPanelExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, ledger::VisitData visit_data; std::string publisherKey = publisher_info->id; - ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&BatPublishers::onPublisherActivity, - this, - _1, - _2, - windowId, - visit_data)); + ledger_->SetPublisherInfo(std::move(publisher_info), windowId); OnExcludedSitesChanged(publisherKey); } @@ -447,7 +416,7 @@ void BatPublishers::OnRestorePublishersInternal(bool success) { if (success) { setNumExcludedSites(0); OnExcludedSitesChanged(""); - synopsisNormalizer(); + SynopsisNormalizer(); } else { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Could not restore publishers."; @@ -456,11 +425,13 @@ void BatPublishers::OnRestorePublishersInternal(bool success) { void BatPublishers::setPublisherMinVisitTime(const uint64_t& duration) { // In seconds state_->min_publisher_duration_ = duration; + SynopsisNormalizer(); saveState(); } void BatPublishers::setPublisherMinVisits(const unsigned int& visits) { state_->min_visits_ = visits; + SynopsisNormalizer(); saveState(); } @@ -476,11 +447,13 @@ void BatPublishers::setNumExcludedSites(const unsigned int& amount) { void BatPublishers::setPublisherAllowNonVerified(const bool& allow) { state_->allow_non_verified_ = allow; + SynopsisNormalizer(); saveState(); } void BatPublishers::setPublisherAllowVideos(const bool& allow) { state_->allow_videos_ = allow; + SynopsisNormalizer(); saveState(); } @@ -510,16 +483,14 @@ bool BatPublishers::getPublisherAllowVideos() const { void BatPublishers::NormalizeContributeWinners( ledger::PublisherInfoList* newList, - bool saveData, const ledger::PublisherInfoList& list, uint32_t record) { - synopsisNormalizerInternal(newList, saveData, list, record); + synopsisNormalizerInternal(newList, list, record); } void BatPublishers::synopsisNormalizerInternal( ledger::PublisherInfoList* newList, - bool saveData, const ledger::PublisherInfoList& oldList, uint32_t /* next_record */) { // TODO SZ: We can pass non const value here to avoid copying @@ -527,6 +498,7 @@ void BatPublishers::synopsisNormalizerInternal( if (list.size() == 0) { return; } + double totalScores = 0.0; for (size_t i = 0; i < list.size(); i++) { totalScores += list[i].score; @@ -576,19 +548,13 @@ void BatPublishers::synopsisNormalizerInternal( list[i].percent = percents[currentValue]; list[i].weight = weights[currentValue]; currentValue++; - if (saveData) { - std::unique_ptr publisher_info; - publisher_info.reset(new ledger::PublisherInfo(list[i])); - ledger_->SetActivityInfo(std::move(publisher_info), - std::bind(&onVisitSavedDummy, _1, _2)); - } if (newList) { newList->push_back(list[i]); } } } -void BatPublishers::synopsisNormalizer() { +void BatPublishers::SynopsisNormalizer() { auto filter = CreateActivityFilter("", ledger::ACTIVITY_MONTH::ANY, -1, @@ -602,12 +568,15 @@ void BatPublishers::synopsisNormalizer() { 0, 0, filter, - std::bind(&BatPublishers::synopsisNormalizerInternal, - this, - nullptr, - true, - _1, - _2)); + std::bind(&BatPublishers::SynopsisNormalizerCallback, this, _1, _2)); +} + +void BatPublishers::SynopsisNormalizerCallback( + const ledger::PublisherInfoList& list, + uint32_t record) { + ledger::PublisherInfoList normalized_list; + synopsisNormalizerInternal(&normalized_list, list, 0); + ledger_->SaveNormalizedPublisherList(normalized_list); } bool BatPublishers::isVerified(const std::string& publisher_id) { diff --git a/vendor/bat-native-ledger/src/bat_publishers.h b/vendor/bat-native-ledger/src/bat_publishers.h index aca998ae3f48..21ece9f23c30 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.h +++ b/vendor/bat-native-ledger/src/bat_publishers.h @@ -73,7 +73,8 @@ class BatPublishers : public ledger::LedgerCallbackHandler { unsigned int getNumExcludedSites() const; bool getPublisherAllowVideos() const; - std::unique_ptr onPublisherInfoUpdated( + void OnPublisherInfoSaved( + uint64_t window_id, ledger::Result result, std::unique_ptr); std::string GetBalanceReportName(ledger::ACTIVITY_MONTH month, int year); @@ -125,7 +126,6 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void clearAllBalanceReports(); void NormalizeContributeWinners(ledger::PublisherInfoList* newList, - bool saveData, const ledger::PublisherInfoList& list, uint32_t /* next_record */); @@ -140,8 +140,6 @@ class BatPublishers : public ledger::LedgerCallbackHandler { // LedgerCallbackHandler impl void OnPublisherStateSaved(ledger::Result result) override; - void onSetPublisherInfo(ledger::Result result, - std::unique_ptr publisher_info); bool isEligibleForContribution(const ledger::PublisherInfo& info); bool isExcluded(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& excluded); @@ -183,9 +181,13 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void calcScoreConsts(); - void synopsisNormalizer(); - void synopsisNormalizerInternal(ledger::PublisherInfoList* newList, bool saveData, - const ledger::PublisherInfoList& list, uint32_t /* next_record */); + void SynopsisNormalizer(); + + void SynopsisNormalizerCallback(const ledger::PublisherInfoList& list, + uint32_t /* next_record */); + void synopsisNormalizerInternal(ledger::PublisherInfoList* newList, + const ledger::PublisherInfoList& list, + uint32_t /* next_record */); bool isPublisherVisible(const braveledger_bat_helper::PUBLISHER_ST& publisher_st); diff --git a/vendor/bat-native-ledger/src/ledger_impl.cc b/vendor/bat-native-ledger/src/ledger_impl.cc index ece3c99aa9df..28d06ebc21ee 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/ledger_impl.cc @@ -301,25 +301,32 @@ std::string LedgerImpl::URIEncode(const std::string& value) { return ledger_client_->URIEncode(value); } +void LedgerImpl::OnPublisherInfoSavedInternal( + uint64_t window_id, + ledger::Result result, + std::unique_ptr info) { + bat_publishers_->OnPublisherInfoSaved(window_id, result, std::move(info)); +} + void LedgerImpl::SetPublisherInfo(std::unique_ptr info, - ledger::PublisherInfoCallback callback) { + uint64_t window_id) { ledger_client_->SavePublisherInfo( std::move(info), - std::bind(&LedgerImpl::OnSetPublisherInfo, + std::bind(&LedgerImpl::OnPublisherInfoSavedInternal, this, - callback, + window_id, _1, _2)); } -void LedgerImpl::SetActivityInfo(std::unique_ptr info, - ledger::PublisherInfoCallback callback) { - ledger_client_->SaveActivityInfo(std::move(info), - std::bind(&LedgerImpl::OnSetPublisherInfo, - this, - callback, - _1, - _2)); +void LedgerImpl::SetActivityInfo(std::unique_ptr info) { + ledger_client_->SaveActivityInfo( + std::move(info), + std::bind(&LedgerImpl::OnPublisherInfoSavedInternal, + this, + 0, + _1, + _2)); } void LedgerImpl::SetMediaPublisherInfo(const std::string& media_key, @@ -362,13 +369,6 @@ void LedgerImpl::LoadNicewareList(ledger::GetNicewareListCallback callback) { ledger_client_->LoadNicewareList(callback); } -void LedgerImpl::OnSetPublisherInfo(ledger::PublisherInfoCallback callback, - ledger::Result result, - std::unique_ptr info) { - info = bat_publishers_->onPublisherInfoUpdated(result, std::move(info)); - callback(result, std::move(info)); -} - std::vector LedgerImpl::GetRecurringDonationPublisherInfo() { return bat_publishers_->GetRecurringDonationList(); } @@ -1102,10 +1102,9 @@ void LedgerImpl::SaveContributionInfo(const std::string& probi, void LedgerImpl::NormalizeContributeWinners( ledger::PublisherInfoList* newList, - bool saveData, const ledger::PublisherInfoList& list, uint32_t record) { - bat_publishers_->NormalizeContributeWinners(newList, saveData, list, record); + bat_publishers_->NormalizeContributeWinners(newList, list, record); } void LedgerImpl::SetTimer(uint64_t time_offset, uint32_t& timer_id) const { @@ -1135,4 +1134,11 @@ bool LedgerImpl::HasSufficientBalanceToReconcile() { return GetBalance() >= GetContributionAmount(); } +void LedgerImpl::SaveNormalizedPublisherList( + const ledger::PublisherInfoList& normalized_list) { + ledger::PublisherInfoListStruct list; + list.list_ = normalized_list; + ledger_client_->SaveNormalizedPublisherList(list); +} + } // namespace bat_ledger diff --git a/vendor/bat-native-ledger/src/ledger_impl.h b/vendor/bat-native-ledger/src/ledger_impl.h index a885213fc091..eb66c0b3aeb8 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.h +++ b/vendor/bat-native-ledger/src/ledger_impl.h @@ -55,9 +55,9 @@ class LedgerImpl : public ledger::Ledger, bool CreateWallet() override; void SetPublisherInfo(std::unique_ptr publisher_info, - ledger::PublisherInfoCallback callback) override; - void SetActivityInfo(std::unique_ptr publisher_info, - ledger::PublisherInfoCallback callback) override; + uint64_t window_id = 0) override; + void SetActivityInfo( + std::unique_ptr publisher_info) override; void GetPublisherInfo(const std::string& publisher_key, ledger::PublisherInfoCallback callback) override; void GetActivityInfo(const ledger::ActivityInfoFilter& filter, @@ -284,7 +284,6 @@ class LedgerImpl : public ledger::Ledger, void NormalizeContributeWinners( ledger::PublisherInfoList* newList, - bool saveData, const ledger::PublisherInfoList& list, uint32_t /* next_record */); @@ -298,6 +297,9 @@ class LedgerImpl : public ledger::Ledger, double GetDefaultContributionAmount() override; bool HasSufficientBalanceToReconcile() override; + void SaveNormalizedPublisherList( + const ledger::PublisherInfoList& normalized_list); + private: void AddRecurringPayment(const std::string& publisher_id, const double& value) override; void OnLoad(const ledger::VisitData& visit_data, const uint64_t& current_time) override; @@ -324,10 +326,6 @@ class LedgerImpl : public ledger::Ledger, void OnTimer(uint32_t timer_id) override; - void OnSetPublisherInfo(ledger::PublisherInfoCallback callback, - ledger::Result result, - std::unique_ptr info); - void saveVisitCallback(const std::string& publisher, uint64_t verifiedTimestamp); @@ -346,6 +344,11 @@ class LedgerImpl : public ledger::Ledger, const std::string& data) override; uint64_t retryRequestSetup(uint64_t min_time, uint64_t max_time); + void OnPublisherInfoSavedInternal( + uint64_t window_id, + ledger::Result result, + std::unique_ptr info); + ledger::LedgerClient* ledger_client_; std::unique_ptr bat_client_; std::unique_ptr bat_publishers_; diff --git a/vendor/bat-native-ledger/src/rapidjson_bat_helper.h b/vendor/bat-native-ledger/src/rapidjson_bat_helper.h index 98a8967410a2..65fb785f3e93 100644 --- a/vendor/bat-native-ledger/src/rapidjson_bat_helper.h +++ b/vendor/bat-native-ledger/src/rapidjson_bat_helper.h @@ -64,6 +64,7 @@ void saveToJson(JsonWriter & writer, const ledger::VisitData&); void saveToJson(JsonWriter & writer, const ledger::WalletInfo&); void saveToJson(JsonWriter & writer, const ledger::PendingContribution&); void saveToJson(JsonWriter & writer, const ledger::PendingContributionList&); +void saveToJson(JsonWriter & writer, const ledger::PublisherInfoListStruct&); template void saveToJsonString(const T& t, std::string& json) {