diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index 371df0c507d3..f86fb7553a03 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -108,14 +108,13 @@ class MockRewardsService : public RewardsService { MOCK_METHOD2(GetPublisherBanner, void(const std::string&, brave_rewards::GetPublisherBannerCallback)); + MOCK_METHOD3(OnTip, void(const std::string&, + int, + bool)); MOCK_METHOD4(OnTip, void(const std::string&, - int, - bool, - const ledger::PublisherInfo*)); - MOCK_METHOD4(OnTip, void(const std::string&, - int, - bool, - std::unique_ptr)); + int, + bool, + std::unique_ptr)); MOCK_METHOD1(RemoveRecurringTip, void(const std::string&)); MOCK_METHOD1(GetRecurringTipsUI, void(brave_rewards::GetRecurringTipsCallback)); diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.cc b/components/brave_rewards/browser/extension_rewards_service_observer.cc index e487d5287253..bf0ff3ce7666 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.cc +++ b/components/brave_rewards/browser/extension_rewards_service_observer.cc @@ -126,20 +126,15 @@ void ExtensionRewardsServiceObserver::OnGetCurrentBalanceReport( void ExtensionRewardsServiceObserver::OnPanelPublisherInfo( RewardsService* rewards_service, int error_code, - std::unique_ptr info, + const ledger::PublisherInfo* info, uint64_t windowId) { auto* event_router = extensions::EventRouter::Get(profile_); - if (!event_router) { + if (!event_router || !info) { return; } extensions::api::brave_rewards::OnPublisherData::Publisher publisher; - if (!info.get()) { - info.reset(new ledger::PublisherInfo()); - info->id = ""; - } - publisher.percentage = info->percent; publisher.verified = info->verified; publisher.excluded = info->excluded == ledger::PUBLISHER_EXCLUDE::EXCLUDED; diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.h b/components/brave_rewards/browser/extension_rewards_service_observer.h index 1db9dcbad35b..f53da74bb0ec 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.h +++ b/components/brave_rewards/browser/extension_rewards_service_observer.h @@ -52,7 +52,7 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver, void OnPanelPublisherInfo( RewardsService* rewards_service, int error_code, - std::unique_ptr info, + const ledger::PublisherInfo* info, uint64_t windowId) override; void OnGrant(RewardsService* rewards_service, unsigned int result, diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 3e9719987845..ed53d8315705 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -8,6 +8,7 @@ #include #include +#include #include "base/bind.h" #include "base/command_line.h" @@ -185,19 +186,18 @@ void PublisherInfoDatabase::GetOneTimeTips(ledger::PublisherInfoList* list, info_sql.BindInt(2, ledger::REWARDS_CATEGORY::ONE_TIME_TIP); while (info_sql.Step()) { - std::string id(info_sql.ColumnString(0)); + auto publisher = ledger::PublisherInfo::New(); - ledger::PublisherInfo publisher(id); + publisher->id = info_sql.ColumnString(0); + publisher->name = info_sql.ColumnString(1); + publisher->url = info_sql.ColumnString(2); + publisher->favicon_url = info_sql.ColumnString(3); + publisher->weight = info_sql.ColumnDouble(4); + publisher->reconcile_stamp = info_sql.ColumnInt64(5); + publisher->verified = info_sql.ColumnBool(6); + publisher->provider = info_sql.ColumnString(7); - publisher.name = info_sql.ColumnString(1); - publisher.url = info_sql.ColumnString(2); - publisher.favicon_url = info_sql.ColumnString(3); - publisher.weight = info_sql.ColumnDouble(4); - publisher.reconcile_stamp = info_sql.ColumnInt64(5); - publisher.verified = info_sql.ColumnBool(6); - publisher.provider = info_sql.ColumnString(7); - - list->push_back(publisher); + list->push_back(std::move(publisher)); } } @@ -288,7 +288,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( return transaction.Commit(); } -std::unique_ptr +ledger::PublisherInfoPtr PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -306,8 +306,7 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { info_sql.BindString(0, publisher_key); if (info_sql.Step()) { - std::unique_ptr info; - info.reset(new ledger::PublisherInfo()); + auto info = ledger::PublisherInfo::New(); info->id = info_sql.ColumnString(0); info->name = info_sql.ColumnString(1); info->url = info_sql.ColumnString(2); @@ -323,7 +322,7 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { return nullptr; } -std::unique_ptr +ledger::PublisherInfoPtr PublisherInfoDatabase::GetPanelPublisher( const ledger::ActivityInfoFilter& filter) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -349,8 +348,7 @@ PublisherInfoDatabase::GetPanelPublisher( info_sql.BindString(2, filter.id); if (info_sql.Step()) { - std::unique_ptr info; - info.reset(new ledger::PublisherInfo()); + auto info = ledger::PublisherInfo::New(); info->id = info_sql.ColumnString(0); info->name = info_sql.ColumnString(1); info->url = info_sql.ColumnString(2); @@ -504,7 +502,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfos( } for (const auto& info : list) { - if (!InsertOrUpdateActivityInfo(info)) { + if (!InsertOrUpdateActivityInfo(*info)) { transaction.Rollback(); return false; } @@ -621,24 +619,23 @@ bool PublisherInfoDatabase::GetActivityList( } while (info_sql.Step()) { - std::string id(info_sql.ColumnString(0)); - - ledger::PublisherInfo info(id); - info.duration = info_sql.ColumnInt64(1); - info.score = info_sql.ColumnDouble(2); - info.percent = info_sql.ColumnInt64(3); - info.weight = info_sql.ColumnDouble(4); - info.verified = info_sql.ColumnBool(5); - info.excluded = static_cast( + auto info = ledger::PublisherInfo::New(); + info->id = info_sql.ColumnString(0); + info->duration = info_sql.ColumnInt64(1); + info->score = info_sql.ColumnDouble(2); + info->percent = info_sql.ColumnInt64(3); + info->weight = info_sql.ColumnDouble(4); + info->verified = info_sql.ColumnBool(5); + info->excluded = static_cast( info_sql.ColumnInt(6)); - info.name = info_sql.ColumnString(7); - info.url = info_sql.ColumnString(8); - info.provider = info_sql.ColumnString(9); - info.favicon_url = info_sql.ColumnString(10); - info.reconcile_stamp = info_sql.ColumnInt64(11); - info.visits = info_sql.ColumnInt(12); + info->name = info_sql.ColumnString(7); + info->url = info_sql.ColumnString(8); + info->provider = info_sql.ColumnString(9); + info->favicon_url = info_sql.ColumnString(10); + info->reconcile_stamp = info_sql.ColumnInt64(11); + info->visits = info_sql.ColumnInt(12); - list->push_back(info); + list->push_back(std::move(info)); } return true; @@ -718,7 +715,7 @@ bool PublisherInfoDatabase::InsertOrUpdateMediaPublisherInfo( return statement.Run(); } -std::unique_ptr +ledger::PublisherInfoPtr PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -739,7 +736,7 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { info_sql.BindString(0, media_key); if (info_sql.Step()) { - std::unique_ptr info(new ledger::PublisherInfo()); + auto info = ledger::PublisherInfo::New(); info->id = info_sql.ColumnString(0); info->name = info_sql.ColumnString(1); info->url = info_sql.ColumnString(2); @@ -835,18 +832,17 @@ void PublisherInfoDatabase::GetRecurringTips( "INNER JOIN publisher_info AS pi ON rd.publisher_id = pi.publisher_id ")); while (info_sql.Step()) { - std::string id(info_sql.ColumnString(0)); - - ledger::PublisherInfo publisher(id); - publisher.name = info_sql.ColumnString(1); - publisher.url = info_sql.ColumnString(2); - publisher.favicon_url = info_sql.ColumnString(3); - publisher.weight = info_sql.ColumnDouble(4); - publisher.reconcile_stamp = info_sql.ColumnInt64(5); - publisher.verified = info_sql.ColumnBool(6); - publisher.provider = info_sql.ColumnString(7); - - list->push_back(publisher); + auto publisher = ledger::PublisherInfo::New(); + publisher->id = info_sql.ColumnString(0); + publisher->name = info_sql.ColumnString(1); + publisher->url = info_sql.ColumnString(2); + publisher->favicon_url = info_sql.ColumnString(3); + publisher->weight = info_sql.ColumnDouble(4); + publisher->reconcile_stamp = info_sql.ColumnInt64(5); + publisher->verified = info_sql.ColumnBool(6); + publisher->provider = info_sql.ColumnString(7); + + list->push_back(std::move(publisher)); } } diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 4a2696691273..5e1bd88bf2fc 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -46,10 +46,10 @@ class PublisherInfoDatabase { bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); - std::unique_ptr GetPublisherInfo( + ledger::PublisherInfoPtr GetPublisherInfo( const std::string& media_key); - std::unique_ptr GetPanelPublisher( + ledger::PublisherInfoPtr GetPanelPublisher( const ledger::ActivityInfoFilter& filter); bool RestorePublishers(); @@ -68,7 +68,7 @@ class PublisherInfoDatabase { bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id); - std::unique_ptr GetMediaPublisherInfo( + ledger::PublisherInfoPtr GetMediaPublisherInfo( const std::string& media_key); bool InsertOrUpdateRecurringTip( diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 1ed276b14ef2..61f52b9d391a 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include "brave/components/brave_rewards/browser/publisher_info_database.h" @@ -529,31 +530,33 @@ TEST_F(PublisherInfoDatabaseTest, GetPanelPublisher) { * Publisher ID is missing */ ledger::ActivityInfoFilter filter_1; - EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), nullptr); + EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_1), + static_cast(nullptr)); /** * Empty table */ ledger::ActivityInfoFilter filter_2; filter_2.id = "test"; - EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), nullptr); + EXPECT_EQ(publisher_info_database_->GetPanelPublisher(filter_2), + static_cast(nullptr)); /** * Still get data if reconcile stamp is not found */ - ledger::PublisherInfo info_1; - info_1.id = "page.com"; - info_1.url = "https://page.com"; - info_1.percent = 11; - info_1.reconcile_stamp = 9; + auto info_1 = ledger::PublisherInfo::New(); + info_1->id = "page.com"; + info_1->url = "https://page.com"; + info_1->percent = 11; + info_1->reconcile_stamp = 9; - bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info_1); + bool success = publisher_info_database_->InsertOrUpdateActivityInfo(*info_1); EXPECT_TRUE(success); ledger::ActivityInfoFilter filter_4; filter_4.id = "page.com"; filter_4.reconcile_stamp = 10; - std::unique_ptr result = + ledger::PublisherInfoPtr result = publisher_info_database_->GetPanelPublisher(filter_4); EXPECT_TRUE(result); EXPECT_EQ(result->id, "page.com"); @@ -568,21 +571,21 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfos) { /** * Good path */ - ledger::PublisherInfo info_1; - info_1.id = "brave.com"; - info_1.url = "https://brave.com"; - info_1.percent = 11; - info_1.reconcile_stamp = 10; - - ledger::PublisherInfo info_2; - info_2.id = "clifton.io"; - info_2.url = "https://clifton.io"; - info_2.percent = 11; - info_2.reconcile_stamp = 10; + auto info_1 = ledger::PublisherInfo::New(); + info_1->id = "brave.com"; + info_1->url = "https://brave.com"; + info_1->percent = 11; + info_1->reconcile_stamp = 10; + + auto info_2 = ledger::PublisherInfo::New(); + info_2->id = "clifton.io"; + info_2->url = "https://clifton.io"; + info_2->percent = 11; + info_2->reconcile_stamp = 10; ledger::PublisherInfoList list; - list.push_back(info_1); - list.push_back(info_2); + list.push_back(std::move(info_1)); + list.push_back(std::move(info_2)); bool success = publisher_info_database_->InsertOrUpdateActivityInfos(list); EXPECT_TRUE(success); @@ -599,13 +602,13 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfos) { * One publisher has empty ID */ - ledger::PublisherInfo info_3; - info_3.id = ""; - info_3.url = "https://page.io"; - info_3.percent = 11; - info_3.reconcile_stamp = 10; + auto info_3 = ledger::PublisherInfo::New(); + info_3->id = ""; + info_3->url = "https://page.io"; + info_3->percent = 11; + info_3->reconcile_stamp = 10; - list.push_back(info_3); + list.push_back(std::move(info_3)); success = publisher_info_database_->InsertOrUpdateActivityInfos(list); EXPECT_FALSE(success); @@ -747,8 +750,8 @@ TEST_F(PublisherInfoDatabaseTest, GetActivityList) { &list_1)); EXPECT_EQ(static_cast(list_1.size()), 2); - EXPECT_EQ(list_1.at(0).id, "publisher_2"); - EXPECT_EQ(list_1.at(1).id, "publisher_6"); + EXPECT_EQ(list_1.at(0)->id, "publisher_2"); + EXPECT_EQ(list_1.at(1)->id, "publisher_6"); /** * Get verified publishers @@ -763,8 +766,8 @@ TEST_F(PublisherInfoDatabaseTest, GetActivityList) { &list_2)); EXPECT_EQ(static_cast(list_2.size()), 2); - EXPECT_EQ(list_2.at(0).id, "publisher_3"); - EXPECT_EQ(list_2.at(1).id, "publisher_6"); + EXPECT_EQ(list_2.at(0)->id, "publisher_3"); + EXPECT_EQ(list_2.at(1)->id, "publisher_6"); /** * Get all publishers that are not excluded @@ -778,11 +781,11 @@ TEST_F(PublisherInfoDatabaseTest, GetActivityList) { &list_3)); EXPECT_EQ(static_cast(list_3.size()), 5); - EXPECT_EQ(list_3.at(0).id, "publisher_1"); - EXPECT_EQ(list_3.at(1).id, "publisher_2"); - EXPECT_EQ(list_3.at(2).id, "publisher_3"); - EXPECT_EQ(list_3.at(3).id, "publisher_5"); - EXPECT_EQ(list_3.at(4).id, "publisher_6"); + EXPECT_EQ(list_3.at(0)->id, "publisher_1"); + EXPECT_EQ(list_3.at(1)->id, "publisher_2"); + EXPECT_EQ(list_3.at(2)->id, "publisher_3"); + EXPECT_EQ(list_3.at(3)->id, "publisher_5"); + EXPECT_EQ(list_3.at(4)->id, "publisher_6"); /** * Get publisher with min_visits @@ -797,8 +800,8 @@ TEST_F(PublisherInfoDatabaseTest, GetActivityList) { &list_4)); EXPECT_EQ(static_cast(list_4.size()), 2); - EXPECT_EQ(list_4.at(0).id, "publisher_5"); - EXPECT_EQ(list_4.at(1).id, "publisher_6"); + EXPECT_EQ(list_4.at(0)->id, "publisher_5"); + EXPECT_EQ(list_4.at(1)->id, "publisher_6"); } @@ -813,10 +816,10 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv3tov4) { EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); EXPECT_EQ(static_cast(list.size()), 2); - EXPECT_EQ(list.at(0).id, "slo-tech.com"); - EXPECT_EQ(list.at(0).visits, 5u); - EXPECT_EQ(list.at(1).id, "brave.com"); - EXPECT_EQ(list.at(1).visits, 5u); + EXPECT_EQ(list.at(0)->id, "slo-tech.com"); + EXPECT_EQ(list.at(0)->visits, 5u); + EXPECT_EQ(list.at(1)->id, "brave.com"); + EXPECT_EQ(list.at(1)->visits, 5u); EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 4); const std::string schema = publisher_info_database_->GetSchema(); @@ -834,12 +837,12 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv4tov5) { EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); EXPECT_EQ(static_cast(list.size()), 3); - EXPECT_EQ(list.at(0).id, "brave.com"); - EXPECT_EQ(list.at(0).visits, 1u); - EXPECT_EQ(list.at(1).id, "slo-tech.com"); - EXPECT_EQ(list.at(1).visits, 1u); - EXPECT_EQ(list.at(2).id, "basicattentiontoken.org"); - EXPECT_EQ(list.at(2).visits, 3u); + EXPECT_EQ(list.at(0)->id, "brave.com"); + EXPECT_EQ(list.at(0)->visits, 1u); + EXPECT_EQ(list.at(1)->id, "slo-tech.com"); + EXPECT_EQ(list.at(1)->visits, 1u); + EXPECT_EQ(list.at(2)->id, "basicattentiontoken.org"); + EXPECT_EQ(list.at(2)->visits, 3u); EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 5); const std::string schema = publisher_info_database_->GetSchema(); @@ -857,29 +860,29 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv5tov6) { EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); EXPECT_EQ(static_cast(list.size()), 3); - EXPECT_EQ(list.at(0).id, "basicattentiontoken.org"); - EXPECT_EQ(list.at(0).duration, 31u); - EXPECT_EQ(list.at(0).visits, 1u); - EXPECT_NEAR(list.at(0).score, 1.1358598545838, 0.001f); - EXPECT_EQ(list.at(0).percent, 26u); - EXPECT_NEAR(list.at(0).weight, 25.919327084376, 0.001f); - EXPECT_EQ(list.at(0).reconcile_stamp, 1553423066u); - - EXPECT_EQ(list.at(1).id, "brave.com"); - EXPECT_EQ(list.at(1).duration, 20u); - EXPECT_EQ(list.at(1).visits, 2u); - EXPECT_NEAR(list.at(1).score, 1.07471534438942, 0.001f); - EXPECT_EQ(list.at(1).percent, 25u); - EXPECT_NEAR(list.at(1).weight, 24.5240629127033, 0.001f); - EXPECT_EQ(list.at(1).reconcile_stamp, 1553423066u); - - EXPECT_EQ(list.at(2).id, "slo-tech.com"); - EXPECT_EQ(list.at(2).duration, 44u); - EXPECT_EQ(list.at(2).visits, 2u); - EXPECT_NEAR(list.at(2).score, 2.1717139356, 0.001f); - EXPECT_EQ(list.at(2).percent, 49u); - EXPECT_NEAR(list.at(2).weight, 49.556610002920678, 0.001f); - EXPECT_EQ(list.at(2).reconcile_stamp, 1553423066u); + EXPECT_EQ(list.at(0)->id, "basicattentiontoken.org"); + EXPECT_EQ(list.at(0)->duration, 31u); + EXPECT_EQ(list.at(0)->visits, 1u); + EXPECT_NEAR(list.at(0)->score, 1.1358598545838, 0.001f); + EXPECT_EQ(list.at(0)->percent, 26u); + EXPECT_NEAR(list.at(0)->weight, 25.919327084376, 0.001f); + EXPECT_EQ(list.at(0)->reconcile_stamp, 1553423066u); + + EXPECT_EQ(list.at(1)->id, "brave.com"); + EXPECT_EQ(list.at(1)->duration, 20u); + EXPECT_EQ(list.at(1)->visits, 2u); + EXPECT_NEAR(list.at(1)->score, 1.07471534438942, 0.001f); + EXPECT_EQ(list.at(1)->percent, 25u); + EXPECT_NEAR(list.at(1)->weight, 24.5240629127033, 0.001f); + EXPECT_EQ(list.at(1)->reconcile_stamp, 1553423066u); + + EXPECT_EQ(list.at(2)->id, "slo-tech.com"); + EXPECT_EQ(list.at(2)->duration, 44u); + EXPECT_EQ(list.at(2)->visits, 2u); + EXPECT_NEAR(list.at(2)->score, 2.1717139356, 0.001f); + EXPECT_EQ(list.at(2)->percent, 49u); + EXPECT_NEAR(list.at(2)->weight, 49.556610002920678, 0.001f); + EXPECT_EQ(list.at(2)->reconcile_stamp, 1553423066u); EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 6); @@ -898,18 +901,18 @@ TEST_F(PublisherInfoDatabaseTest, Migrationv4tov6) { EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); EXPECT_EQ(static_cast(list.size()), 3); - EXPECT_EQ(list.at(0).id, "basicattentiontoken.org"); - EXPECT_EQ(list.at(0).duration, 15u); - EXPECT_EQ(list.at(0).visits, 3u); - EXPECT_EQ(list.at(0).reconcile_stamp, 1552214829u); - EXPECT_EQ(list.at(1).id, "brave.com"); - EXPECT_EQ(list.at(1).duration, 10u); - EXPECT_EQ(list.at(1).visits, 1u); - EXPECT_EQ(list.at(1).reconcile_stamp, 1552214829u); - EXPECT_EQ(list.at(2).id, "slo-tech.com"); - EXPECT_EQ(list.at(2).duration, 12u); - EXPECT_EQ(list.at(2).visits, 1u); - EXPECT_EQ(list.at(2).reconcile_stamp, 1552214829u); + EXPECT_EQ(list.at(0)->id, "basicattentiontoken.org"); + EXPECT_EQ(list.at(0)->duration, 15u); + EXPECT_EQ(list.at(0)->visits, 3u); + EXPECT_EQ(list.at(0)->reconcile_stamp, 1552214829u); + EXPECT_EQ(list.at(1)->id, "brave.com"); + EXPECT_EQ(list.at(1)->duration, 10u); + EXPECT_EQ(list.at(1)->visits, 1u); + EXPECT_EQ(list.at(1)->reconcile_stamp, 1552214829u); + EXPECT_EQ(list.at(2)->id, "slo-tech.com"); + EXPECT_EQ(list.at(2)->duration, 12u); + EXPECT_EQ(list.at(2)->visits, 1u); + EXPECT_EQ(list.at(2)->reconcile_stamp, 1552214829u); EXPECT_EQ(publisher_info_database_->GetTableVersionNumber(), 6); } @@ -998,9 +1001,9 @@ TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) { EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); EXPECT_EQ(static_cast(list.size()), 2); - EXPECT_EQ(list.at(0).id, "publisher_1"); - EXPECT_EQ(list.at(0).reconcile_stamp, 1u); - EXPECT_EQ(list.at(1).id, "publisher_2"); + EXPECT_EQ(list.at(0)->id, "publisher_1"); + EXPECT_EQ(list.at(0)->reconcile_stamp, 1u); + EXPECT_EQ(list.at(1)->id, "publisher_2"); } } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index e37f006ae6a4..0547ffd02318 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -31,10 +31,6 @@ struct IssuersInfo; struct NotificationInfo; } -namespace ledger { -struct PublisherInfo; -} - namespace content { class NavigationHandle; } @@ -164,8 +160,9 @@ class RewardsService : public KeyedService { const GetContributionAmountCallback& callback) = 0; virtual void GetPublisherBanner(const std::string& publisher_id, GetPublisherBannerCallback callback) = 0; - virtual void OnTip(const std::string& publisher_key, int amount, - bool recurring, const ledger::PublisherInfo* publisher_info = NULL) = 0; + virtual void OnTip(const std::string& publisher_key, + int amount, + bool recurring) = 0; virtual void OnTip(const std::string& publisher_key, int amount, bool recurring, std::unique_ptr site) = 0; virtual void RemoveRecurringTip(const std::string& publisher_key) = 0; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 91933c929265..2355b8dd35ca 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -205,7 +205,7 @@ bool SaveMediaPublisherInfoOnFileTaskRunner( return false; } -std::unique_ptr +ledger::PublisherInfoPtr LoadPublisherInfoOnFileTaskRunner( const std::string publisher_key, PublisherInfoDatabase* backend) { @@ -215,11 +215,11 @@ LoadPublisherInfoOnFileTaskRunner( return backend->GetPublisherInfo(publisher_key); } -std::unique_ptr +ledger::PublisherInfoPtr LoadMediaPublisherInfoOnFileTaskRunner( const std::string media_key, PublisherInfoDatabase* backend) { - std::unique_ptr info; + ledger::PublisherInfoPtr info; if (!backend) return info; @@ -227,18 +227,20 @@ LoadMediaPublisherInfoOnFileTaskRunner( } bool SavePublisherInfoOnFileTaskRunner( - const ledger::PublisherInfo publisher_info, + ledger::PublisherInfoPtr publisher_info, PublisherInfoDatabase* backend) { - if (backend && backend->InsertOrUpdatePublisherInfo(publisher_info)) + if (backend && + backend->InsertOrUpdatePublisherInfo(*publisher_info)) return true; return false; } bool SaveActivityInfoOnFileTaskRunner( - const ledger::PublisherInfo publisher_info, + ledger::PublisherInfoPtr publisher_info, PublisherInfoDatabase* backend) { - if (backend && backend->InsertOrUpdateActivityInfo(publisher_info)) + if (backend && + backend->InsertOrUpdateActivityInfo(*publisher_info)) return true; return false; @@ -257,7 +259,7 @@ ledger::PublisherInfoList GetActivityListOnFileTaskRunner( return list; } -std::unique_ptr GetPanelPublisherInfoOnFileTaskRunner( +ledger::PublisherInfoPtr GetPanelPublisherInfoOnFileTaskRunner( ledger::ActivityInfoFilter filter, PublisherInfoDatabase* backend) { ledger::PublisherInfoList list; @@ -553,14 +555,12 @@ void RewardsServiceImpl::GetContentSiteList( void RewardsServiceImpl::OnGetContentSiteList( const GetContentSiteListCallback& callback, - const std::vector& json_list, + ledger::PublisherInfoList list, uint32_t next_record) { std::unique_ptr site_list(new ContentSiteList); - for (auto &json_publisher : json_list) { - ledger::PublisherInfo publisher; - publisher.loadFromJson(json_publisher); - site_list->push_back(PublisherInfoToContentSite(publisher)); + for (auto &publisher : list) { + site_list->push_back(PublisherInfoToContentSite(*publisher)); } callback.Run(std::move(site_list), next_record); @@ -718,7 +718,7 @@ void RewardsServiceImpl::LoadPublisherInfo( void RewardsServiceImpl::OnPublisherInfoLoaded( ledger::PublisherInfoCallback callback, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { if (!info) { callback(ledger::Result::NOT_FOUND, nullptr); return; @@ -740,7 +740,7 @@ void RewardsServiceImpl::LoadMediaPublisherInfo( void RewardsServiceImpl::OnMediaPublisherInfoLoaded( ledger::PublisherInfoCallback callback, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { if (!Connected()) return; @@ -1109,22 +1109,22 @@ void RewardsServiceImpl::LoadNicewareList( } void RewardsServiceImpl::SavePublisherInfo( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) { - ledger::PublisherInfo info_copy = *publisher_info; + ledger::PublisherInfoPtr copy = publisher_info->Clone(); base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&SavePublisherInfoOnFileTaskRunner, - info_copy, + base::BindOnce(&SavePublisherInfoOnFileTaskRunner, + std::move(copy), publisher_info_backend_.get()), - base::Bind(&RewardsServiceImpl::OnPublisherInfoSaved, + base::BindOnce(&RewardsServiceImpl::OnPublisherInfoSaved, AsWeakPtr(), callback, - base::Passed(std::move(publisher_info)))); + std::move(publisher_info))); } void RewardsServiceImpl::OnPublisherInfoSaved( ledger::PublisherInfoCallback callback, - std::unique_ptr info, + ledger::PublisherInfoPtr info, bool success) { if (Connected()) { callback(success ? ledger::Result::LEDGER_OK @@ -1133,22 +1133,22 @@ void RewardsServiceImpl::OnPublisherInfoSaved( } void RewardsServiceImpl::SaveActivityInfo( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) { - ledger::PublisherInfo info_copy = *publisher_info; + ledger::PublisherInfoPtr copy = publisher_info->Clone(); base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&SaveActivityInfoOnFileTaskRunner, - info_copy, + base::BindOnce(&SaveActivityInfoOnFileTaskRunner, + std::move(copy), publisher_info_backend_.get()), - base::Bind(&RewardsServiceImpl::OnActivityInfoSaved, + base::BindOnce(&RewardsServiceImpl::OnActivityInfoSaved, AsWeakPtr(), callback, - base::Passed(std::move(publisher_info)))); + std::move(publisher_info))); } void RewardsServiceImpl::OnActivityInfoSaved( ledger::PublisherInfoCallback callback, - std::unique_ptr info, + ledger::PublisherInfoPtr info, bool success) { if (Connected()) { callback(success ? ledger::Result::LEDGER_OK @@ -1173,21 +1173,14 @@ void RewardsServiceImpl::LoadActivityInfo( void RewardsServiceImpl::OnPublisherActivityInfoLoaded( ledger::PublisherInfoCallback callback, uint32_t result, - const std::string& info_json) { - std::unique_ptr publisher; - - if (!info_json.empty()) { - publisher = std::make_unique(); - publisher->loadFromJson(info_json); - } - + ledger::PublisherInfoPtr publisher) { callback(static_cast(result), std::move(publisher)); } void RewardsServiceImpl::OnActivityInfoLoaded( ledger::PublisherInfoCallback callback, const std::string& publisher_key, - const ledger::PublisherInfoList& list) { + ledger::PublisherInfoList list) { if (!Connected()) { return; } @@ -1203,13 +1196,12 @@ void RewardsServiceImpl::OnActivityInfoLoaded( callback)); return; } else if (list.size() > 1) { - callback(ledger::Result::TOO_MANY_RESULTS, - std::unique_ptr()); + callback(ledger::Result::TOO_MANY_RESULTS, nullptr); return; } callback(ledger::Result::LEDGER_OK, - std::make_unique(list[0])); + std::move(list[0])); } void RewardsServiceImpl::LoadPanelPublisherInfo( @@ -1226,10 +1218,9 @@ void RewardsServiceImpl::LoadPanelPublisherInfo( void RewardsServiceImpl::OnPanelPublisherInfoLoaded( ledger::PublisherInfoCallback callback, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { if (!publisher_info) { - callback(ledger::Result::NOT_FOUND, - std::unique_ptr()); + callback(ledger::Result::NOT_FOUND, nullptr); return; } @@ -1256,7 +1247,7 @@ void RewardsServiceImpl::OnPublisherInfoListLoaded( uint32_t start, uint32_t limit, ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list) { + ledger::PublisherInfoList list) { if (!Connected()) { return; } @@ -1265,7 +1256,7 @@ void RewardsServiceImpl::OnPublisherInfoListLoaded( if (list.size() == limit) next_record = start + limit + 1; - callback(std::cref(list), next_record); + callback(std::move(list), next_record); } void RewardsServiceImpl::LoadURL( @@ -1977,7 +1968,7 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl( GetDomainAndRegistry(origin.host(), INCLUDE_PRIVATE_REGISTRIES); if (baseDomain == "") { - std::unique_ptr info; + ledger::PublisherInfoPtr info; OnPanelPublisherInfo(ledger::Result::NOT_FOUND, std::move(info), windowId); return; } @@ -2013,7 +2004,7 @@ void RewardsServiceImpl::OnExcludedSitesChanged( void RewardsServiceImpl::OnPanelPublisherInfo( ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t windowId) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { @@ -2023,7 +2014,7 @@ void RewardsServiceImpl::OnPanelPublisherInfo( for (auto& observer : private_observers_) observer.OnPanelPublisherInfo(this, result, - std::move(info), + info.get(), windowId); } @@ -2154,18 +2145,18 @@ void RewardsServiceImpl::OnPublisherBanner( } void RewardsServiceImpl::OnTipPublisherInfoSaved(ledger::Result result, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { } -void RewardsServiceImpl::OnTip(const std::string& publisher_key, int amount, - bool recurring, const ledger::PublisherInfo* publisher_info) { +void RewardsServiceImpl::OnTip(const std::string& publisher_key, + int amount, + bool recurring, + ledger::PublisherInfoPtr publisher_info) { if (recurring) { // TODO(nejczdovc): this needs to be wired through ledger code // If caller provided publisher info, save it to `publisher_info` table if (publisher_info) { - auto publisher_copy = std::make_unique( - *publisher_info); - SavePublisherInfo(std::move(publisher_copy), + SavePublisherInfo(std::move(publisher_info), std::bind(&RewardsServiceImpl::OnTipPublisherInfoSaved, this, _1, _2)); } @@ -2177,9 +2168,7 @@ void RewardsServiceImpl::OnTip(const std::string& publisher_key, int amount, if (!Connected()) return; - ledger::PublisherInfo publisher(publisher_key); - - bat_ledger_->DoDirectTip(publisher.ToJson(), amount, "BAT"); + bat_ledger_->DoDirectTip(publisher_key, amount, "BAT"); } bool SaveContributionInfoOnFileTaskRunner( @@ -2266,15 +2255,13 @@ ledger::PublisherInfoList GetRecurringTipsOnFileTaskRunner( void RewardsServiceImpl::OnGetRecurringTipsUI( GetRecurringTipsCallback callback, - const std::vector& json_list) { + ledger::PublisherInfoList list) { std::unique_ptr new_list( new brave_rewards::ContentSiteList); - for (auto &json_publisher : json_list) { - ledger::PublisherInfo publisher; - publisher.loadFromJson(json_publisher); - brave_rewards::ContentSite site = PublisherInfoToContentSite(publisher); - site.percentage = publisher.weight; + for (auto& publisher : list) { + brave_rewards::ContentSite site = PublisherInfoToContentSite(*publisher); + site.percentage = publisher->weight; new_list->push_back(site); } @@ -2291,12 +2278,12 @@ void RewardsServiceImpl::GetRecurringTipsUI( void RewardsServiceImpl::OnGetRecurringTips( const ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list) { + ledger::PublisherInfoList list) { if (!Connected()) { return; } - callback(list, 0); + callback(std::move(list), 0); } void RewardsServiceImpl::GetRecurringTips( @@ -2311,15 +2298,13 @@ void RewardsServiceImpl::GetRecurringTips( void RewardsServiceImpl::OnGetOneTimeTipsUI( GetRecurringTipsCallback callback, - const std::vector& json_list) { + ledger::PublisherInfoList list) { std::unique_ptr new_list( new brave_rewards::ContentSiteList); - for (auto &json_publisher : json_list) { - ledger::PublisherInfo publisher; - publisher.loadFromJson(json_publisher); - brave_rewards::ContentSite site = PublisherInfoToContentSite(publisher); - site.percentage = publisher.weight; + for (auto& publisher : list) { + brave_rewards::ContentSite site = PublisherInfoToContentSite(*publisher); + site.percentage = publisher->weight; new_list->push_back(site); } @@ -2358,12 +2343,12 @@ void RewardsServiceImpl::GetOneTimeTips( void RewardsServiceImpl::OnGetOneTimeTips( ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list) { + ledger::PublisherInfoList list) { if (!Connected()) { return; } - callback(list, 0); + callback(std::move(list), 0); } void RewardsServiceImpl::RemoveRecurringTip(const std::string& publisher_key) { @@ -2632,6 +2617,14 @@ void RewardsServiceImpl::GetRewardsInternalsInfo( AsWeakPtr(), std::move(callback))); } +void RewardsServiceImpl::OnTip( + const std::string& publisher_key, + int amount, + bool recurring) { + OnTip(publisher_key, amount, recurring, + static_cast(nullptr)); +} + void RewardsServiceImpl::OnTip( const std::string& publisher_key, int amount, @@ -2642,16 +2635,16 @@ void RewardsServiceImpl::OnTip( return; } - ledger::PublisherInfo info; - info.id = publisher_key; - info.verified = site->verified; - info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; - info.name = site->name; - info.url = site->url; - info.provider = site->provider; - info.favicon_url = site->favicon_url; + ledger::PublisherInfoPtr info; + info->id = publisher_key; + info->verified = site->verified; + info->excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; + info->name = site->name; + info->url = site->url; + info->provider = site->provider; + info->favicon_url = site->favicon_url; - OnTip(publisher_key, amount, recurring, &info); + OnTip(publisher_key, amount, recurring, std::move(info)); } bool RewardsServiceImpl::Connected() const { @@ -2782,65 +2775,47 @@ void RewardsServiceImpl::OnRestorePublishersInternal( callback(result); } -std::unique_ptr -SaveNormalizedPublisherListOnFileTaskRunner(PublisherInfoDatabase* backend, - const ledger::PublisherInfoList& list) { +bool SaveNormalizedPublisherListOnFileTaskRunner( + PublisherInfoDatabase* backend, + 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 false; } - return new_list; + return backend->InsertOrUpdateActivityInfos(list); } void RewardsServiceImpl::SaveNormalizedPublisherList( - const ledger::PublisherInfoListStruct& list) { - if (list.list.size() == 0) { - std::unique_ptr empty_list( - new ledger::PublisherInfoList); - OnPublisherListNormalizedSaved(std::move(empty_list)); - return; + ledger::PublisherInfoList list) { + ContentSiteList site_list; + for (const auto& publisher : list) { + if (publisher->percent >= 1) { + site_list.push_back(PublisherInfoToContentSite(*publisher)); + } } + // TODO(bridiver) - this doesn't belong here + std::sort(site_list.begin(), site_list.end()); base::PostTaskAndReplyWithResult( - file_task_runner_.get(), - FROM_HERE, - base::Bind(&SaveNormalizedPublisherListOnFileTaskRunner, - publisher_info_backend_.get(), - list.list), - base::Bind(&RewardsServiceImpl::OnPublisherListNormalizedSaved, - AsWeakPtr())); + file_task_runner_.get(), + FROM_HERE, + base::BindOnce(&SaveNormalizedPublisherListOnFileTaskRunner, + publisher_info_backend_.get(), + std::move(list)), + base::BindOnce(&RewardsServiceImpl::OnPublisherListNormalizedSaved, + AsWeakPtr(), + std::move(site_list))); } void RewardsServiceImpl::OnPublisherListNormalizedSaved( - std::unique_ptr list) { - if (!list) { + ContentSiteList site_list, + bool success) { + if (!success) { LOG(ERROR) << "Problem saving normalized publishers " "in SaveNormalizedPublisherList"; return; } - ContentSiteList site_list; - for (auto& publisher : *list) { - if (publisher.percent >= 1) { - site_list.push_back(PublisherInfoToContentSite(publisher)); - } - } - - sort(site_list.begin(), site_list.end()); - for (auto& observer : observers_) { observer.OnPublisherListNormalized(this, site_list); } diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 7680582b7c2e..52056fe6fcd2 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -113,7 +113,7 @@ class RewardsServiceImpl : public RewardsService, const GetContentSiteListCallback& callback) override; void OnGetContentSiteList( const GetContentSiteListCallback& callback, - const std::vector& json_list, + ledger::PublisherInfoList list, uint32_t next_record); void OnLoad(SessionID tab_id, const GURL& url) override; void OnUnload(SessionID tab_id) override; @@ -173,7 +173,7 @@ class RewardsServiceImpl : public RewardsService, void RemoveRecurringTip(const std::string& publisher_key) override; void OnGetRecurringTipsUI( GetRecurringTipsCallback callback, - const std::vector& json_list); + ledger::PublisherInfoList list); void GetRecurringTipsUI(GetRecurringTipsCallback callback) override; void GetOneTimeTips( ledger::PublisherInfoListCallback callback) override; @@ -247,23 +247,23 @@ class RewardsServiceImpl : public RewardsService, void TriggerOnGrantFinish(ledger::Result result, const ledger::Grant& grant); void TriggerOnRewardsMainEnabled(bool rewards_main_enabled); void OnPublisherInfoSaved(ledger::PublisherInfoCallback callback, - std::unique_ptr info, + ledger::PublisherInfoPtr info, bool success); void OnActivityInfoSaved(ledger::PublisherInfoCallback callback, - std::unique_ptr info, + ledger::PublisherInfoPtr info, bool success); void OnActivityInfoLoaded(ledger::PublisherInfoCallback callback, const std::string& publisher_key, - const ledger::PublisherInfoList& list); + ledger::PublisherInfoList list); void OnMediaPublisherInfoSaved(bool success); void OnPublisherInfoLoaded(ledger::PublisherInfoCallback callback, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void OnMediaPublisherInfoLoaded(ledger::PublisherInfoCallback callback, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void OnPublisherInfoListLoaded(uint32_t start, uint32_t limit, ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list); + ledger::PublisherInfoList list); void OnPublishersListSaved(ledger::LedgerCallbackHandler* handler, bool success); void OnTimer(uint32_t timer_id); @@ -275,19 +275,19 @@ class RewardsServiceImpl : public RewardsService, void OnResetState(ledger::OnResetCallback callback, bool success); void OnTipPublisherInfoSaved(ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void OnTip(const std::string& publisher_key, - int amount, - bool recurring, - const ledger::PublisherInfo* publisher_info = NULL) override; + int amount, + bool recurring, + ledger::PublisherInfoPtr publisher_info); void OnContributionInfoSaved(const ledger::REWARDS_CATEGORY category, bool success); void OnRecurringTipSaved(bool success); void OnGetRecurringTips( const ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list); + ledger::PublisherInfoList list); void OnGetOneTimeTips(ledger::PublisherInfoListCallback callback, - const ledger::PublisherInfoList& list); + ledger::PublisherInfoList list); void OnRemovedRecurringTip(ledger::RecurringRemoveCallback callback, bool success); void OnRemoveRecurring(const std::string& publisher_key, @@ -296,10 +296,13 @@ class RewardsServiceImpl : public RewardsService, const ledger::BalanceReportInfo& report); void MaybeShowBackupNotification(uint64_t boot_stamp); void MaybeShowAddFundsNotification(uint64_t reconcile_stamp); - void OnPublisherListNormalizedSaved( - std::unique_ptr list); + void OnPublisherListNormalizedSaved(ContentSiteList site_list, + bool success); void OnWalletProperties(ledger::Result result, std::unique_ptr info) override; + void OnTip(const std::string& publisher_key, + int amount, + bool recurring) override; void OnTip(const std::string& publisher_key, int amount, bool recurring, std::unique_ptr site) override; @@ -312,11 +315,11 @@ class RewardsServiceImpl : public RewardsService, bool result); void OnGetOneTimeTipsUI(GetRecurringTipsCallback callback, - const std::vector& json_list); + ledger::PublisherInfoList list); void OnPublisherActivityInfoLoaded(ledger::PublisherInfoCallback callback, uint32_t result, - const std::string& info_json); + ledger::PublisherInfoPtr info); // ledger::LedgerClient std::string GenerateGUID() const override; @@ -340,9 +343,9 @@ class RewardsServiceImpl : public RewardsService, ledger::LedgerCallbackHandler* handler) override; void SavePublisherState(const std::string& publisher_state, ledger::LedgerCallbackHandler* handler) override; - void SavePublisherInfo(std::unique_ptr publisher_info, + void SavePublisherInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) override; - void SaveActivityInfo(std::unique_ptr publisher_info, + void SaveActivityInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) override; void LoadActivityInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; @@ -380,8 +383,8 @@ class RewardsServiceImpl : public RewardsService, void OnExcludedSitesChanged(const std::string& publisher_id, ledger::PUBLISHER_EXCLUDE exclude) override; void OnPanelPublisherInfo(ledger::Result result, - std::unique_ptr info, - uint64_t window_id) override; + ledger::PublisherInfoPtr info, + uint64_t window_id) override; void FetchFavIcon(const std::string& url, const std::string& favicon_key, ledger::FetchIconCallback callback) override; @@ -422,7 +425,7 @@ class RewardsServiceImpl : public RewardsService, void OnRestorePublishers(ledger::OnRestoreCallback callback) override; void OnPanelPublisherInfoLoaded( ledger::PublisherInfoCallback callback, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void SavePendingContribution( const ledger::PendingContributionList& list) override; @@ -433,7 +436,7 @@ class RewardsServiceImpl : public RewardsService, bool result); void SaveNormalizedPublisherList( - const ledger::PublisherInfoListStruct& list) override; + ledger::PublisherInfoList list) override; void GetExcludedPublishersNumberDB( ledger::GetExcludedPublishersNumberDBCallback callback) override; diff --git a/components/brave_rewards/browser/rewards_service_impl_unittest.cc b/components/brave_rewards/browser/rewards_service_impl_unittest.cc index 218ef51dc7ce..ae98d58af2cf 100644 --- a/components/brave_rewards/browser/rewards_service_impl_unittest.cc +++ b/components/brave_rewards/browser/rewards_service_impl_unittest.cc @@ -53,7 +53,7 @@ class MockRewardsServiceObserver : public RewardsServiceObserver { MOCK_METHOD2(OnPublisherBanner, void(RewardsService*, const brave_rewards::PublisherBanner)); MOCK_METHOD4(OnPanelPublisherInfo, - void(RewardsService*, int, ledger::PublisherInfo*, uint64_t)); + void(RewardsService*, int, ledger::PublisherInfoPtr, uint64_t)); }; class RewardsServiceTest : public testing::Test { diff --git a/components/brave_rewards/browser/rewards_service_private_observer.h b/components/brave_rewards/browser/rewards_service_private_observer.h index baefe6247d6d..75e531aecda6 100644 --- a/components/brave_rewards/browser/rewards_service_private_observer.h +++ b/components/brave_rewards/browser/rewards_service_private_observer.h @@ -1,9 +1,10 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_PRIVATE_OBSERVER_H_ -#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_PRIVATE_OBSERVER_H_ +#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_REWARDS_SERVICE_PRIVATE_OBSERVER_H_ +#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_REWARDS_SERVICE_PRIVATE_OBSERVER_H_ #include "base/observer_list_types.h" #include "bat/ledger/publisher_info.h" @@ -22,7 +23,7 @@ class RewardsServicePrivateObserver : public base::CheckedObserver { virtual void OnPanelPublisherInfo( RewardsService* rewards_service, int error_code, - std::unique_ptr info, + const ledger::PublisherInfo* info, uint64_t windowId) {} virtual void OnRewardsMainEnabled( RewardsService* rewards_service, @@ -31,4 +32,4 @@ class RewardsServicePrivateObserver : public base::CheckedObserver { } // namespace brave_rewards -#endif // BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_PRIVATE_OBSERVER_H_ +#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_REWARDS_SERVICE_PRIVATE_OBSERVER_H_ 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 bbfbb9229ae0..2d71117bc3ac 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -323,46 +323,36 @@ void BatLedgerClientMojoProxy::SavePublishersList( AsWeakPtr(), base::Unretained(handler))); } -void OnSavePublisherInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - callback(ToLedgerResult(result), std::move(info)); +void OnSavePublisherInfo( + const ledger::PublisherInfoCallback& callback, + int32_t result, + ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::SavePublisherInfo( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); + callback(ledger::Result::LEDGER_ERROR, nullptr); return; } - std::string json_info = publisher_info ? publisher_info->ToJson() : ""; - bat_ledger_client_->SavePublisherInfo(json_info, + bat_ledger_client_->SavePublisherInfo( + std::move(publisher_info), base::BindOnce(&OnSavePublisherInfo, std::move(callback))); } void OnLoadPublisherInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - callback(ToLedgerResult(result), std::move(info)); + int32_t result, ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::LoadPublisherInfo( const std::string& publisher_key, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); + callback(ledger::Result::LEDGER_ERROR, nullptr); return; } @@ -370,24 +360,17 @@ void BatLedgerClientMojoProxy::LoadPublisherInfo( base::BindOnce(&OnLoadPublisherInfo, std::move(callback))); } -void OnLoadPanelPublisherInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - - callback(ToLedgerResult(result), std::move(info)); +void OnLoadPanelPublisherInfo( + const ledger::PublisherInfoCallback& callback, + int32_t result, + ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::LoadPanelPublisherInfo( ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); return; } @@ -395,22 +378,18 @@ void BatLedgerClientMojoProxy::LoadPanelPublisherInfo( base::BindOnce(&OnLoadPanelPublisherInfo, std::move(callback))); } -void OnLoadMediaPublisherInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - callback(ToLedgerResult(result), std::move(info)); +void OnLoadMediaPublisherInfo( + const ledger::PublisherInfoCallback& callback, + int32_t result, + ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::LoadMediaPublisherInfo( const std::string& media_key, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); + callback(ledger::Result::LEDGER_ERROR, nullptr); return; } @@ -445,16 +424,16 @@ void BatLedgerClientMojoProxy::OnExcludedSitesChanged( bat_ledger_client_->OnExcludedSitesChanged(publisher_id, exclude); } -void BatLedgerClientMojoProxy::OnPanelPublisherInfo(ledger::Result result, - std::unique_ptr info, +void BatLedgerClientMojoProxy::OnPanelPublisherInfo( + ledger::Result result, + ledger::PublisherInfoPtr info, uint64_t windowId) { if (!Connected()) { return; } - std::string json_info = info ? info->ToJson() : ""; - bat_ledger_client_->OnPanelPublisherInfo(ToMojomResult(result), - json_info, windowId); + bat_ledger_client_->OnPanelPublisherInfo( + ToMojomResult(result), std::move(info), windowId); } void OnFetchFavIcon(const ledger::FetchIconCallback& callback, @@ -475,23 +454,15 @@ void BatLedgerClientMojoProxy::FetchFavIcon(const std::string& url, } void OnGetRecurringTips(const ledger::PublisherInfoListCallback& callback, - const std::vector& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - ledger::PublisherInfoList list; - - for (const auto& publisher_info : publisher_info_list) { - ledger::PublisherInfo info; - info.loadFromJson(publisher_info); - list.push_back(info); - } - - callback(list, next_record); + callback(std::move(publisher_info_list), next_record); } void BatLedgerClientMojoProxy::GetRecurringTips( ledger::PublisherInfoListCallback callback) { if (!Connected()) { - callback(std::vector(), 0); + callback(ledger::PublisherInfoList(), 0); return; } @@ -500,23 +471,15 @@ void BatLedgerClientMojoProxy::GetRecurringTips( } void OnGetOneTimeTips(const ledger::PublisherInfoListCallback& callback, - const std::vector& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - ledger::PublisherInfoList list; - - for (const auto& publisher_info : publisher_info_list) { - ledger::PublisherInfo info; - info.loadFromJson(publisher_info); - list.push_back(info); - } - - callback(list, next_record); + callback(std::move(publisher_info_list), next_record); } void BatLedgerClientMojoProxy::GetOneTimeTips( ledger::PublisherInfoListCallback callback) { if (!Connected()) { - callback(std::vector(), 0); + callback(ledger::PublisherInfoList(), 0); return; } @@ -612,22 +575,18 @@ void BatLedgerClientMojoProxy::SavePendingContribution( bat_ledger_client_->SavePendingContribution(list.ToJson()); } -void OnLoadActivityInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - callback(ToLedgerResult(result), std::move(info)); +void OnLoadActivityInfo( + const ledger::PublisherInfoCallback& callback, + int32_t result, + ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::LoadActivityInfo( ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); + callback(ledger::Result::LEDGER_ERROR, nullptr); return; } @@ -635,27 +594,23 @@ void BatLedgerClientMojoProxy::LoadActivityInfo( base::BindOnce(&OnLoadActivityInfo, std::move(callback))); } -void OnSaveActivityInfo(const ledger::PublisherInfoCallback& callback, - int32_t result, const std::string& publisher_info) { - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - callback(ToLedgerResult(result), std::move(info)); +void OnSaveActivityInfo( + const ledger::PublisherInfoCallback& callback, + int32_t result, + ledger::PublisherInfoPtr publisher_info) { + callback(ToLedgerResult(result), std::move(publisher_info)); } void BatLedgerClientMojoProxy::SaveActivityInfo( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR, - std::unique_ptr()); + callback(ledger::Result::LEDGER_ERROR, nullptr); return; } - std::string json_info = publisher_info ? publisher_info->ToJson() : ""; - bat_ledger_client_->SaveActivityInfo(json_info, + bat_ledger_client_->SaveActivityInfo( + std::move(publisher_info), base::BindOnce(&OnSaveActivityInfo, std::move(callback))); } @@ -676,17 +631,9 @@ void BatLedgerClientMojoProxy::OnRestorePublishers( } void OnGetActivityInfoList(const ledger::PublisherInfoListCallback& callback, - const std::vector& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - ledger::PublisherInfoList list; - - for (const auto& publisher_info : publisher_info_list) { - ledger::PublisherInfo info; - info.loadFromJson(publisher_info); - list.push_back(info); - } - - callback(list, next_record); + callback(std::move(publisher_info_list), next_record); } void BatLedgerClientMojoProxy::GetActivityInfoList(uint32_t start, @@ -694,7 +641,7 @@ void BatLedgerClientMojoProxy::GetActivityInfoList(uint32_t start, ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) { if (!Connected()) { - callback(std::vector(), 0); + callback(ledger::PublisherInfoList(), 0); return; } @@ -706,12 +653,12 @@ void BatLedgerClientMojoProxy::GetActivityInfoList(uint32_t start, void BatLedgerClientMojoProxy::SaveNormalizedPublisherList( - const ledger::PublisherInfoListStruct& normalized_list) { + ledger::PublisherInfoList normalized_list) { if (!Connected()) { return; } - bat_ledger_client_->SaveNormalizedPublisherList(normalized_list.ToJson()); + bat_ledger_client_->SaveNormalizedPublisherList(std::move(normalized_list)); } void BatLedgerClientMojoProxy::SaveState( 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 2a1c9995ea8f..9e47c7eebb50 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -51,7 +51,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void SavePublisherState(const std::string& publisher_state, ledger::LedgerCallbackHandler* handler) override; - void SavePublisherInfo(std::unique_ptr publisher_info, + void SavePublisherInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) override; void LoadPublisherInfo(const std::string& publisher_key, ledger::PublisherInfoCallback callback) override; @@ -73,8 +73,8 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void OnExcludedSitesChanged(const std::string& publisher_id, ledger::PUBLISHER_EXCLUDE exclude) override; void OnPanelPublisherInfo(ledger::Result result, - std::unique_ptr info, - uint64_t windowId) override; + ledger::PublisherInfoPtr info, + uint64_t windowId) override; void FetchFavIcon(const std::string& url, const std::string& favicon_key, ledger::FetchIconCallback callback) override; @@ -112,7 +112,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void LoadActivityInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; - void SaveActivityInfo(std::unique_ptr publisher_info, + void SaveActivityInfo(ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback) override; void OnRestorePublishers(ledger::OnRestoreCallback callback) override; @@ -123,7 +123,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, ledger::PublisherInfoListCallback callback) override; void SaveNormalizedPublisherList( - const ledger::PublisherInfoListStruct& normalized_list) override; + ledger::PublisherInfoList normalized_list) override; void SaveState(const std::string& name, const std::string& value, diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index 7c90a9dbf6ab..eef82fd04a5e 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -344,12 +344,10 @@ void BatLedgerImpl::GetContributionAmount( std::move(callback).Run(ledger_->GetContributionAmount()); } -void BatLedgerImpl::DoDirectTip(const std::string& publisher_info, - int32_t amount, - const std::string& currency) { - ledger::PublisherInfo info; - if (info.loadFromJson(publisher_info)) - ledger_->DoDirectTip(info, amount, currency); +void BatLedgerImpl::DoDirectTip(const std::string& publisher_id, + int32_t amount, + const std::string& currency) { + ledger_->DoDirectTip(publisher_id, amount, currency); } void BatLedgerImpl::RemoveRecurringTip(const std::string& publisher_key) { @@ -427,17 +425,11 @@ void BatLedgerImpl::GetRewardsInternalsInfo( // static void BatLedgerImpl::OnGetRecurringTips( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num) { + if (holder->is_valid()) + std::move(holder->get()).Run(std::move(list)); - std::vector json_list; - for (auto const& item : list) { - json_list.push_back(item.ToJson()); - } - - if (holder->is_valid()) { - std::move(holder->get()).Run(json_list); - } delete holder; } @@ -452,17 +444,11 @@ void BatLedgerImpl::GetRecurringTips(GetRecurringTipsCallback callback) { // static void BatLedgerImpl::OnGetOneTimeTips( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num) { + if (holder->is_valid()) + std::move(holder->get()).Run(std::move(list)); - std::vector json_list; - for (auto const& item : list) { - json_list.push_back(item.ToJson()); - } - - if (holder->is_valid()) { - std::move(holder->get()).Run(json_list); - } delete holder; } @@ -477,16 +463,10 @@ void BatLedgerImpl::GetOneTimeTips(GetOneTimeTipsCallback callback) { // static void BatLedgerImpl::OnGetActivityInfoList( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num) { - std::vector json_list; - for (auto const& item : list) { - json_list.push_back(item.ToJson()); - } - - if (holder->is_valid()) { - std::move(holder->get()).Run(json_list, num); - } + if (holder->is_valid()) + std::move(holder->get()).Run(std::move(list), num); delete holder; } @@ -513,15 +493,9 @@ void BatLedgerImpl::GetActivityInfoList( void BatLedgerImpl::OnLoadPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string publisher; - if (info) { - publisher = info->ToJson(); - } - - if (holder->is_valid()) { - std::move(holder->get()).Run(result, publisher); - } + ledger::PublisherInfoPtr publisher_info) { + if (holder->is_valid()) + std::move(holder->get()).Run(result, std::move(publisher_info)); delete holder; } @@ -540,9 +514,9 @@ void BatLedgerImpl::LoadPublisherInfo( void BatLedgerImpl::OnRefreshPublisher( CallbackHolder* holder, bool verified) { - if (holder->is_valid()) { + if (holder->is_valid()) std::move(holder->get()).Run(verified); - } + delete holder; } diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index 461ab05e5d5f..f29f3a1b091d 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -117,8 +117,9 @@ class BatLedgerImpl : public mojom::BatLedger, void GetPublisherBanner(const std::string& publisher_id, GetPublisherBannerCallback callback) override; - void DoDirectTip(const std::string& publisher_info, int32_t amount, - const std::string& currency) override; + void DoDirectTip(const std::string& publisher_id, + int32_t amount, + const std::string& currency) override; void RemoveRecurringTip(const std::string& publisher_key) override; void GetBootStamp(GetBootStampCallback callback) override; @@ -196,12 +197,12 @@ class BatLedgerImpl : public mojom::BatLedger, static void OnGetRecurringTips( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num); static void OnGetOneTimeTips( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num); static void OnRefreshPublisher( CallbackHolder* holder, @@ -209,13 +210,13 @@ class BatLedgerImpl : public mojom::BatLedger, static void OnGetActivityInfoList( CallbackHolder* holder, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t num); static void OnLoadPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); std::unique_ptr bat_ledger_client_mojo_proxy_; std::unique_ptr ledger_; 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 083bc8e88935..76796c264fd6 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 @@ -235,26 +235,19 @@ void LedgerClientMojoProxy::OnGrantFinish(int32_t result, void LedgerClientMojoProxy::OnSavePublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } void LedgerClientMojoProxy::SavePublisherInfo( - const std::string& publisher_info, + ledger::PublisherInfoPtr publisher_info, SavePublisherInfoCallback callback) { // deleted in OnSavePublisherInfo auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - - ledger_client_->SavePublisherInfo(std::move(info), + ledger_client_->SavePublisherInfo(std::move(publisher_info), std::bind(LedgerClientMojoProxy::OnSavePublisherInfo, holder, _1, _2)); } @@ -262,10 +255,9 @@ void LedgerClientMojoProxy::SavePublisherInfo( void LedgerClientMojoProxy::OnLoadPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } @@ -282,10 +274,10 @@ void LedgerClientMojoProxy::LoadPublisherInfo( // static void LedgerClientMojoProxy::OnLoadPanelPublisherInfo( CallbackHolder* holder, - ledger::Result result, std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::Result result, + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } @@ -305,10 +297,9 @@ void LedgerClientMojoProxy::LoadPanelPublisherInfo(const std::string& filter, void LedgerClientMojoProxy::OnLoadMediaPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } @@ -342,13 +333,10 @@ void LedgerClientMojoProxy::OnExcludedSitesChanged( static_cast(exclude)); } -void LedgerClientMojoProxy::OnPanelPublisherInfo(int32_t result, - const std::string& info, uint64_t window_id) { - std::unique_ptr publisher_info; - if (!info.empty()) { - publisher_info.reset(new ledger::PublisherInfo()); - publisher_info->loadFromJson(info); - } +void LedgerClientMojoProxy::OnPanelPublisherInfo( + int32_t result, + ledger::PublisherInfoPtr publisher_info, + uint64_t window_id) { ledger_client_->OnPanelPublisherInfo(ToLedgerResult(result), std::move(publisher_info), window_id); } @@ -374,15 +362,10 @@ void LedgerClientMojoProxy::FetchFavIcon(const std::string& url, // static void LedgerClientMojoProxy::OnGetRecurringTips( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - std::vector list; - for (const auto& publisher_info : publisher_info_list) { - list.push_back(publisher_info.ToJson()); - } - if (holder->is_valid()) - std::move(holder->get()).Run(list, next_record); + std::move(holder->get()).Run(std::move(publisher_info_list), next_record); delete holder; } @@ -498,10 +481,9 @@ void LedgerClientMojoProxy::SavePendingContribution(const std::string& list) { void LedgerClientMojoProxy::OnLoadActivityInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } @@ -521,26 +503,20 @@ void LedgerClientMojoProxy::LoadActivityInfo( void LedgerClientMojoProxy::OnSaveActivityInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info) { - std::string json_info = info ? info->ToJson() : ""; + ledger::PublisherInfoPtr info) { if (holder->is_valid()) - std::move(holder->get()).Run(ToMojomResult(result), json_info); + std::move(holder->get()).Run(ToMojomResult(result), std::move(info)); delete holder; } void LedgerClientMojoProxy::SaveActivityInfo( - const std::string& publisher_info, + ledger::PublisherInfoPtr publisher_info, SaveActivityInfoCallback callback) { // deleted in OnSaveActivityInfo auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - std::unique_ptr info; - if (!publisher_info.empty()) { - info.reset(new ledger::PublisherInfo()); - info->loadFromJson(publisher_info); - } - ledger_client_->SaveActivityInfo(std::move(info), + ledger_client_->SaveActivityInfo(std::move(publisher_info), std::bind(LedgerClientMojoProxy::OnSaveActivityInfo, holder, _1, _2)); } @@ -566,15 +542,10 @@ void LedgerClientMojoProxy::OnRestorePublishers( // static void LedgerClientMojoProxy::OnGetActivityInfoList( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - std::vector list; - for (const auto& publisher_info : publisher_info_list) { - list.push_back(publisher_info.ToJson()); - } - if (holder->is_valid()) - std::move(holder->get()).Run(list, next_record); + std::move(holder->get()).Run(std::move(publisher_info_list), next_record); delete holder; } @@ -599,12 +570,8 @@ 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); + ledger::PublisherInfoList list) { + ledger_client_->SaveNormalizedPublisherList(std::move(list)); } // static @@ -702,15 +669,10 @@ void LedgerClientMojoProxy::GetExcludedPublishersNumberDB( // static void LedgerClientMojoProxy::OnGetOneTimeTips( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record) { - std::vector list; - for (const auto& publisher_info : publisher_info_list) { - list.push_back(publisher_info.ToJson()); - } - if (holder->is_valid()) - std::move(holder->get()).Run(list, next_record); + std::move(holder->get()).Run(std::move(publisher_info_list), next_record); delete holder; } 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 aece00b44980..fe3775742aba 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 @@ -47,7 +47,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void SavePublishersList(const std::string& publishers_list, SavePublishersListCallback callback) override; - void SavePublisherInfo(const std::string& publisher_info, + void SavePublisherInfo(ledger::PublisherInfoPtr publisher_info, SavePublisherInfoCallback callback) override; void LoadPublisherInfo(const std::string& publisher_key, LoadPublisherInfoCallback callback) override; @@ -68,8 +68,9 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void SetTimer(uint64_t time_offset, SetTimerCallback callback) override; void KillTimer(const uint32_t timer_id) override; - void OnPanelPublisherInfo(int32_t result, const std::string& info, - uint64_t window_id) override; + void OnPanelPublisherInfo(int32_t result, + ledger::PublisherInfoPtr info, + uint64_t window_id) override; void OnExcludedSitesChanged(const std::string& publisher_id, int exclude) override; void SaveContributionInfo(const std::string& probi, int32_t month, @@ -99,7 +100,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void LoadActivityInfo(const std::string& filter, LoadActivityInfoCallback callback) override; - void SaveActivityInfo(const std::string& publisher_info, + void SaveActivityInfo(ledger::PublisherInfoPtr publisher_info, SaveActivityInfoCallback callback) override; void OnRestorePublishers(OnRestorePublishersCallback callback) override; @@ -110,7 +111,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, GetActivityInfoListCallback callback) override; void SaveNormalizedPublisherList( - const std::string& normalized_list) override; + ledger::PublisherInfoList normalized_list) override; void SaveState(const std::string& name, const std::string& value, SaveStateCallback callback) override; @@ -158,22 +159,22 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, static void OnSavePublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnLoadPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnLoadPanelPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnLoadMediaPublisherInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnFetchFavIcon( CallbackHolder* holder, @@ -182,7 +183,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, static void OnGetRecurringTips( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record); static void OnLoadNicewareList( @@ -201,12 +202,12 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, static void OnLoadActivityInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnSaveActivityInfo( CallbackHolder* holder, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); static void OnRestorePublishersDone( CallbackHolder* holder, @@ -214,7 +215,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, static void OnGetActivityInfoList( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record); static void OnSaveState( @@ -236,7 +237,7 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, static void OnGetOneTimeTips( CallbackHolder* holder, - const ledger::PublisherInfoList& publisher_info_list, + ledger::PublisherInfoList publisher_info_list, uint32_t next_record); ledger::LedgerClient* ledger_client_; diff --git a/components/services/bat_ledger/public/interfaces/BUILD.gn b/components/services/bat_ledger/public/interfaces/BUILD.gn index d085ff3d4fb0..e94900f20907 100644 --- a/components/services/bat_ledger/public/interfaces/BUILD.gn +++ b/components/services/bat_ledger/public/interfaces/BUILD.gn @@ -10,4 +10,8 @@ mojom("interfaces") { public_deps = [ "//mojo/public/mojom/base", ] + + deps = [ + "//brave/vendor/bat-native-ledger/include/bat/ledger/public/interfaces" + ] } diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index d6b0fdb6c6a7..000195a383b9 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -3,6 +3,8 @@ // You can obtain one at http://mozilla.org/MPL/2.0/. module bat_ledger.mojom; +import "brave/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom"; + const string kServiceName = "bat_ledger"; interface BatLedgerService { @@ -88,7 +90,9 @@ interface BatLedger { GetContributionAmount() => (double contribution_amount); GetPublisherBanner(string publisher_id) => (string banner); - DoDirectTip(string publisher_info, int32 amount, string currency); + DoDirectTip(string publisher_id, + int32 amount, + string currency); RemoveRecurringTip(string publisher_key); GetBootStamp() => (uint64 boot_stamp); @@ -103,13 +107,14 @@ interface BatLedger { GetTransactionHistoryForThisCycle() => (string transactions); GetRewardsInternalsInfo() => (string info); - GetRecurringTips() => (array list); - GetOneTimeTips() => (array list); + GetRecurringTips() => (array list); + GetOneTimeTips() => (array list); GetActivityInfoList(uint32 start, uint32 limit, string json_filter) => - (array list, uint32 number); + (array list, uint32 number); - LoadPublisherInfo(string publisher_key) => (uint32 result, string info); + LoadPublisherInfo(string publisher_key) => + (uint32 result, ledger.mojom.PublisherInfo? info); RefreshPublisher(string publisher_key) => (bool verified); StartAutoContribute(); @@ -134,20 +139,25 @@ interface BatLedgerClient { string probi); OnGrantFinish(int32 result, string grant); - SavePublisherInfo(string publisher_info) => (int32 result, - string publisher_info); - LoadPublisherInfo(string publisher_key) => (int32 result, string publisher_info); - LoadPanelPublisherInfo(string filter) => (int32 result, - string publisher_info); - LoadMediaPublisherInfo(string media_key) => (int32 result, - string publisher_info); - - OnPanelPublisherInfo(int32 result, string info, uint64 window_id); + SavePublisherInfo(ledger.mojom.PublisherInfo publisher_info) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); + LoadPublisherInfo(string publisher_key) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); + LoadPanelPublisherInfo(string filter) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); + LoadMediaPublisherInfo(string media_key) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); + + OnPanelPublisherInfo(int32 result, + ledger.mojom.PublisherInfo publisher_info, + uint64 window_id); FetchFavIcon(string url, string favicon_key) => (bool success, string favicon_url); - GetRecurringTips() => (array publisher_info_list, + GetRecurringTips() => ( + array publisher_info_list, uint32 next_record); - GetOneTimeTips() => (array publisher_info_list, + GetOneTimeTips() => ( + array publisher_info_list, uint32 next_record); LoadNicewareList() => (int32 result, string data); @@ -172,17 +182,18 @@ interface BatLedgerClient { SavePendingContribution(string list); - LoadActivityInfo(string filter) => (int32 result, string publisher_info); + LoadActivityInfo(string filter) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); - SaveActivityInfo(string publisher_info) => (int32 result, - string publisher_info); + SaveActivityInfo(ledger.mojom.PublisherInfo publisher_info) => + (int32 result, ledger.mojom.PublisherInfo? publisher_info); OnRestorePublishers() => (bool result); - GetActivityInfoList(uint32 start, uint32 limit, string filter) => ( - array publisher_info_list, uint32 next_record); + GetActivityInfoList(uint32 start, uint32 limit, string filter) => + (array publisher_info_list, uint32 next_record); - SaveNormalizedPublisherList(string list); + SaveNormalizedPublisherList(array list); SaveState(string name, string value) => (int32 result); LoadState(string name) => (int32 result, string value); diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h index 6e500805dcd4..d7c8c06e596c 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h @@ -95,11 +95,11 @@ class MockConfirmationsClient : public ConfirmationsClient { ledger::GetNicewareListCallback callback)); MOCK_METHOD2(SavePublisherInfo, void( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback)); MOCK_METHOD2(SaveActivityInfo, void( - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, ledger::PublisherInfoCallback callback)); MOCK_METHOD2(LoadPublisherInfo, void( @@ -155,7 +155,7 @@ class MockConfirmationsClient : public ConfirmationsClient { MOCK_METHOD3(OnPanelPublisherInfo, void( ledger::Result result, - std::unique_ptr, + ledger::PublisherInfoPtr, uint64_t windowId)); MOCK_METHOD2(OnExcludedSitesChanged, void( @@ -238,7 +238,7 @@ class MockConfirmationsClient : public ConfirmationsClient { ledger::OnRestoreCallback callback)); MOCK_METHOD1(SaveNormalizedPublisherList, void( - const ledger::PublisherInfoListStruct& normalized_list)); + ledger::PublisherInfoList normalized_list)); MOCK_METHOD1(SetConfirmationsIsReady, void( const bool is_ready)); diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 6b8461d4580d..97eb7594f35d 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -13,7 +13,10 @@ dep_base = rebase_path("../..", "//") config("external_config") { visibility = [ ":*" ] - include_dirs = [ "include" ] + include_dirs = [ + "include", + "$target_gen_dir/include", + ] } config("internal_config") { @@ -77,6 +80,10 @@ source_set("headers") { "include/bat/ledger/transaction_info.h", "include/bat/ledger/transactions_info.h", ] + + public_deps = [ + "include/bat/ledger/public/interfaces" + ] } source_set("ledger") { diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index f1139bf1bf73..799f15afac62 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -88,9 +88,9 @@ class LEDGER_EXPORT Ledger { virtual void AddRecurringPayment(const std::string& publisher_id, const double& value) = 0; - virtual void DoDirectTip(const PublisherInfo& publisher, - int amount, - const std::string& currency) = 0; + virtual void DoDirectTip(const std::string& publisher_id, + int amount, + const std::string& currency) = 0; virtual void OnLoad(const VisitData& visit_data, const uint64_t& current_time) = 0; @@ -129,11 +129,9 @@ class LEDGER_EXPORT Ledger { virtual std::string URIEncode(const std::string& value) = 0; - virtual void SetPublisherInfo( - std::unique_ptr publisher_info) = 0; + virtual void SetPublisherInfo(PublisherInfoPtr publisher_info) = 0; - virtual void SetActivityInfo( - std::unique_ptr publisher_info) = 0; + virtual void SetActivityInfo(PublisherInfoPtr publisher_info) = 0; virtual void GetPublisherInfo(const std::string& publisher_key, PublisherInfoCallback callback) = 0; 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 1365b26a95f9..356c409855e0 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -52,10 +52,10 @@ class LEDGER_EXPORT LogStream { }; using PublisherInfoCallback = - std::function)>; + std::function; // TODO(nejczdovc) we should be providing result back as well using PublisherInfoListCallback = - std::function; + std::function; using GetNicewareListCallback = std::function; using RecurringRemoveCallback = std::function; @@ -103,10 +103,10 @@ class LEDGER_EXPORT LedgerClient { virtual void LoadNicewareList(ledger::GetNicewareListCallback callback) = 0; - virtual void SavePublisherInfo(std::unique_ptr publisher_info, - PublisherInfoCallback callback) = 0; + virtual void SavePublisherInfo(PublisherInfoPtr publisher_info, + PublisherInfoCallback callback) = 0; - virtual void SaveActivityInfo(std::unique_ptr publisher_info, + virtual void SaveActivityInfo(PublisherInfoPtr publisher_info, PublisherInfoCallback callback) = 0; virtual void LoadPublisherInfo(const std::string& publisher_key, @@ -125,8 +125,8 @@ class LEDGER_EXPORT LedgerClient { const std::string& publisher_id) = 0; virtual void GetActivityInfoList(uint32_t start, uint32_t limit, - ActivityInfoFilter filter, - PublisherInfoListCallback callback) = 0; + ActivityInfoFilter filter, + PublisherInfoListCallback callback) = 0; // TODO(anyone) this can be removed virtual void FetchGrants(const std::string& lang, @@ -148,7 +148,7 @@ class LEDGER_EXPORT LedgerClient { const ledger::Grant& grant) = 0; virtual void OnPanelPublisherInfo(Result result, - std::unique_ptr, + ledger::PublisherInfoPtr publisher_info, uint64_t windowId) = 0; virtual void OnExcludedSitesChanged(const std::string& publisher_id, @@ -207,7 +207,7 @@ class LEDGER_EXPORT LedgerClient { virtual void OnRestorePublishers(ledger::OnRestoreCallback callback) = 0; virtual void SaveNormalizedPublisherList( - const ledger::PublisherInfoListStruct& normalized_list) = 0; + ledger::PublisherInfoList normalized_list) = 0; virtual void SaveState(const std::string& name, const std::string& value, diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/BUILD.gn b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/BUILD.gn new file mode 100644 index 000000000000..3a5b78184fac --- /dev/null +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/BUILD.gn @@ -0,0 +1,11 @@ +import("//mojo/public/tools/bindings/mojom.gni") + +mojom("interfaces") { + sources = [ + "ledger.mojom", + ] + + public_deps = [ + "//mojo/public/mojom/base", + ] +} diff --git a/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom new file mode 100644 index 000000000000..61f7fc953478 --- /dev/null +++ b/vendor/bat-native-ledger/include/bat/ledger/public/interfaces/ledger.mojom @@ -0,0 +1,29 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. +module ledger.mojom; + +struct ContributionInfo { + string publisher; // Filled only for recurrent tips + double value; + uint64 date; +}; + +struct PublisherInfo { + string id; + uint64 duration; + double score; + uint32 visits; + uint32 percent; + double weight; + int32 excluded; + int32 category; + uint64 reconcile_stamp; + bool verified; + string name; + string url; + string provider; + string favicon_url; + + array contributions; +}; 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 9cea7b4013ca..5dda34682fd6 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -12,9 +12,14 @@ #include #include "bat/ledger/export.h" +#include "bat/ledger/public/interfaces/ledger.mojom.h" namespace ledger { +using PublisherInfo = mojom::PublisherInfo; +using PublisherInfoPtr = mojom::PublisherInfoPtr; +using PublisherInfoList = std::vector; + const char _clear_favicon[] = "clear"; LEDGER_EXPORT enum REWARDS_CATEGORY { @@ -107,49 +112,6 @@ LEDGER_EXPORT struct PublisherBanner { bool verified; }; -LEDGER_EXPORT struct PublisherInfo { - PublisherInfo(); - PublisherInfo(const std::string& publisher_id); - PublisherInfo(const PublisherInfo& info); - ~PublisherInfo(); - - bool operator<(const PublisherInfo& rhs) const; - bool is_valid() const; - - const std::string ToJson() const; - bool loadFromJson(const std::string& json); - - std::string id; - uint64_t duration; - double score; - uint32_t visits; - uint32_t percent; - double weight; - PUBLISHER_EXCLUDE excluded; - REWARDS_CATEGORY category; - uint64_t reconcile_stamp; - bool verified; - std::string name; - std::string url; - std::string provider; - std::string favicon_url; - - 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 #endif // BAT_LEDGER_PUBLISHER_INFO_HANDLER_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.cc index 7246248ec08d..f4dacc09b28c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.cc @@ -93,7 +93,7 @@ std::string BatContribution::GetAnonizeProof( ledger::PublisherInfoList BatContribution::GetVerifiedListAuto( const std::string& viewing_id, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, double* budget) { ledger::PublisherInfoList verified; ledger::PublisherInfoList temp; @@ -103,28 +103,27 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListAuto( double non_verified_bat = 0.0; double ac_amount = ledger_->GetContributionAmount(); - for (const auto& publisher : list) { - if (publisher.verified) { - verified.push_back(publisher); - verified_total += publisher.percent; + for (const auto& publisher : *list) { + if (publisher->verified) { + verified.push_back(publisher->Clone()); + verified_total += publisher->percent; } else { - temp.push_back(publisher); + temp.push_back(publisher->Clone()); } } // verified publishers - for (auto publisher : verified) { - ledger::PendingContribution contribution; - publisher.percent = static_cast( - static_cast(publisher.percent) / verified_total) * 100; + for (auto& publisher : verified) { + publisher->percent = static_cast( + static_cast(publisher->percent) / verified_total) * 100; } // non-verified publishers for (const auto& publisher : temp) { ledger::PendingContribution contribution; contribution.amount = - (static_cast(publisher.percent) / 100) * ac_amount; - contribution.publisher_key = publisher.id; + (static_cast(publisher->percent) / 100) * ac_amount; + contribution.publisher_key = publisher->id; contribution.viewing_id = viewing_id; contribution.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE; @@ -143,23 +142,23 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListAuto( ledger::PublisherInfoList BatContribution::GetVerifiedListRecurring( const std::string& viewing_id, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, double* budget) { ledger::PublisherInfoList verified; ledger::PendingContributionList non_verified; - for (const auto& publisher : list) { - if (publisher.id.empty()) { + for (const auto& publisher : *list) { + if (publisher->id.empty()) { continue; } - if (publisher.verified) { - verified.push_back(publisher); - *budget += publisher.weight; + if (publisher->verified) { + verified.push_back(publisher->Clone()); + *budget += publisher->weight; } else { ledger::PendingContribution contribution; - contribution.amount = publisher.weight; - contribution.publisher_key = publisher.id; + contribution.amount = publisher->weight; + contribution.publisher_key = publisher->id; contribution.viewing_id = viewing_id; contribution.category = ledger::REWARDS_CATEGORY::RECURRING_TIP; @@ -176,7 +175,7 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListRecurring( void BatContribution::ReconcilePublisherList( ledger::REWARDS_CATEGORY category, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t next_record) { std::string viewing_id = ledger_->GenerateGUID(); ledger::PublisherInfoList verified_list; @@ -184,24 +183,23 @@ void BatContribution::ReconcilePublisherList( if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { ledger::PublisherInfoList normalized_list; - ledger_->NormalizeContributeWinners(&normalized_list, list, 0); - std::sort(normalized_list.begin(), normalized_list.end()); - verified_list = GetVerifiedListAuto(viewing_id, normalized_list, &budget); + ledger_->NormalizeContributeWinners(&normalized_list, &list, 0); + verified_list = GetVerifiedListAuto(viewing_id, &normalized_list, &budget); } else { - verified_list = GetVerifiedListRecurring(viewing_id, list, &budget); + verified_list = GetVerifiedListRecurring(viewing_id, &list, &budget); } braveledger_bat_helper::PublisherList new_list; for (const auto &publisher : verified_list) { braveledger_bat_helper::PUBLISHER_ST new_publisher; - new_publisher.id_ = publisher.id; - new_publisher.percent_ = publisher.percent; - new_publisher.weight_ = publisher.weight; - new_publisher.duration_ = publisher.duration; - new_publisher.score_ = publisher.score; - new_publisher.visits_ = publisher.visits; - new_publisher.verified_ = publisher.verified; + new_publisher.id_ = publisher->id; + new_publisher.percent_ = publisher->percent; + new_publisher.weight_ = publisher->weight; + new_publisher.duration_ = publisher->duration; + new_publisher.score_ = publisher->score; + new_publisher.visits_ = publisher->visits; + new_publisher.verified_ = publisher->verified; new_list.push_back(new_publisher); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.h index c2979f40db1a..40eb0204a6f6 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_contribution.h @@ -159,19 +159,19 @@ class BatContribution { // save unverified to the db ledger::PublisherInfoList GetVerifiedListAuto( const std::string& viewing_id, - const ledger::PublisherInfoList& all, + const ledger::PublisherInfoList* all, double* budget); // RECURRING DONTAIONS: from the list gets only verified publishers and // save unverified to the db ledger::PublisherInfoList GetVerifiedListRecurring( const std::string& viewing_id, - const ledger::PublisherInfoList& all, + const ledger::PublisherInfoList* all, double* budget); // Entry point for contribution where we have publisher info list void ReconcilePublisherList(ledger::REWARDS_CATEGORY category, - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t next_record); // Resets reconcile stamps diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc index dee27b976c73..67243122211a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc @@ -2719,76 +2719,6 @@ void saveToJson(JsonWriter* writer, const ledger::Grant& grant) { writer->EndObject(); } -void saveToJson(JsonWriter* writer, const ledger::PublisherInfo& info) { - writer->StartObject(); - - writer->String("id"); - writer->String(info.id.c_str()); - - writer->String("duration"); - writer->Uint64(info.duration); - - writer->String("score"); - writer->Double(info.score); - - writer->String("visits"); - writer->Uint(info.visits); - - writer->String("percent"); - writer->Uint(info.percent); - - writer->String("weight"); - writer->Double(info.weight); - - writer->String("excluded"); - writer->Int(info.excluded); - - writer->String("category"); - writer->Int(info.category); - - writer->String("reconcile_stamp"); - writer->Uint64(info.reconcile_stamp); - - writer->String("verified"); - writer->Bool(info.verified); - - writer->String("name"); - writer->String(info.name.c_str()); - - writer->String("url"); - writer->String(info.url.c_str()); - - writer->String("provider"); - writer->String(info.provider.c_str()); - - writer->String("favicon_url"); - writer->String(info.favicon_url.c_str()); - - writer->String("contributions"); - writer->StartArray(); - for (auto & contribution : info.contributions) { - saveToJson(writer, contribution); - } - writer->EndArray(); - - writer->EndObject(); -} - -void saveToJson(JsonWriter* writer, const ledger::ContributionInfo& info) { - writer->StartObject(); - - writer->String("publisher"); - writer->String(info.publisher.c_str()); - - writer->String("value"); - writer->Double(info.value); - - writer->String("date"); - writer->Uint64(info.date); - - writer->EndObject(); -} - void saveToJson(JsonWriter* writer, const ledger::PublisherBanner& banner) { writer->StartObject(); @@ -2982,20 +2912,6 @@ void saveToJson(JsonWriter* writer, 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(); -} - void saveToJson(JsonWriter* writer, const ledger::ReconcileInfo& data) { writer->StartObject(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc index 765c2c128a6b..f29590fb1934 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.cc @@ -138,7 +138,7 @@ void BatPublishers::saveVisitInternal( uint64_t duration, uint64_t window_id, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { DCHECK(result != ledger::Result::TOO_MANY_RESULTS); if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { @@ -148,9 +148,10 @@ void BatPublishers::saveVisitInternal( bool verified = isVerified(publisher_id); bool new_visit = false; - if (!publisher_info.get()) { + if (!publisher_info) { new_visit = true; - publisher_info.reset(new ledger::PublisherInfo(publisher_id)); + publisher_info = ledger::PublisherInfo::New(); + publisher_info->id = publisher_id; } std::string fav_icon = visit_data.favicon_url; @@ -176,13 +177,15 @@ void BatPublishers::saveVisitInternal( publisher_info->url = visit_data.url; publisher_info->verified = verified; - bool excluded = isExcluded(publisher_info->id, publisher_info->excluded); + bool excluded = isExcluded( + publisher_info->id, + static_cast(publisher_info->excluded)); bool ignore_time = ignoreMinTime(publisher_id); if (duration == 0) { ignore_time = false; } - std::unique_ptr panel_info = nullptr; + ledger::PublisherInfoPtr panel_info = nullptr; if (excluded) { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED; @@ -201,7 +204,7 @@ void BatPublishers::saveVisitInternal( !ledger_->GetAutoContribute() || min_duration_new || verified_new)) { - panel_info = std::make_unique(*publisher_info); + panel_info = publisher_info->Clone(); ledger_->SetPublisherInfo(std::move(publisher_info)); } else if (!excluded && @@ -213,7 +216,7 @@ void BatPublishers::saveVisitInternal( publisher_info->score += concaveScore(duration); publisher_info->reconcile_stamp = ledger_->GetReconcileStamp(); - panel_info = std::make_unique(*publisher_info); + panel_info = publisher_info->Clone(); ledger_->SetActivityInfo(std::move(publisher_info)); } @@ -251,14 +254,13 @@ void BatPublishers::onFetchFavIcon(const std::string& publisher_key, void BatPublishers::onFetchFavIconDBResponse( ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, const std::string& favicon_url, uint64_t window_id) { if (result == ledger::Result::LEDGER_OK && !favicon_url.empty()) { info->favicon_url = favicon_url; - std::unique_ptr panel_info = - std::make_unique(*info); + ledger::PublisherInfoPtr panel_info = info->Clone(); ledger_->SetPublisherInfo(std::move(info)); @@ -277,8 +279,8 @@ void BatPublishers::onFetchFavIconDBResponse( void BatPublishers::OnPublisherInfoSaved( ledger::Result result, - std::unique_ptr info) { - if (result != ledger::Result::LEDGER_OK || !info.get()) { + ledger::PublisherInfoPtr info) { + if (result != ledger::Result::LEDGER_OK || !info) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Publisher info was not saved!"; } @@ -299,7 +301,7 @@ void BatPublishers::setExclude(const std::string& publisher_id, void BatPublishers::onSetExcludeInternal( ledger::PUBLISHER_EXCLUDE exclude, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { return; @@ -400,7 +402,7 @@ void BatPublishers::SetMigrateScore(bool value) { void BatPublishers::NormalizeContributeWinners( ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, uint32_t record) { synopsisNormalizerInternal(newList, list, record); @@ -408,22 +410,20 @@ void BatPublishers::NormalizeContributeWinners( void BatPublishers::synopsisNormalizerInternal( ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& oldList, + const ledger::PublisherInfoList* list, uint32_t /* next_record */) { - // TODO(SZ): We can pass non const value here to avoid copying - ledger::PublisherInfoList list = oldList; - if (list.size() == 0) { + if (list->size() == 0) { return; } double totalScores = 0.0; - for (size_t i = 0; i < list.size(); i++) { + for (size_t i = 0; i < list->size(); i++) { // Check which would test uint problem from this issue // https://github.com/brave/brave-browser/issues/3134 if (GetMigrateScore()) { - list[i].score = concaveScore(list[i].duration); + (*list)[i]->score = concaveScore((*list)[i]->duration); } - totalScores += list[i].score; + totalScores += (*list)[i]->score; } if (GetMigrateScore()) { @@ -435,8 +435,8 @@ void BatPublishers::synopsisNormalizerInternal( std::vector realPercents; std::vector roundoffs; unsigned int totalPercents = 0; - for (size_t i = 0; i < list.size(); i++) { - double floatNumber = (list[i].score / totalScores) * 100.0; + for (size_t i = 0; i < list->size(); i++) { + double floatNumber = ((*list)[i]->score / totalScores) * 100.0; double roundNumber = (unsigned int)std::lround(floatNumber); realPercents.push_back(floatNumber); percents.push_back(roundNumber); @@ -477,12 +477,12 @@ void BatPublishers::synopsisNormalizerInternal( } } size_t currentValue = 0; - for (size_t i = 0; i < list.size(); i++) { - list[i].percent = percents[currentValue]; - list[i].weight = weights[currentValue]; + for (size_t i = 0; i < list->size(); i++) { + (*list)[i]->percent = percents[currentValue]; + (*list)[i]->weight = weights[currentValue]; currentValue++; if (newList) { - newList->push_back(list[i]); + newList->push_back((*list)[i]->Clone()); } } } @@ -505,11 +505,11 @@ void BatPublishers::SynopsisNormalizer() { } void BatPublishers::SynopsisNormalizerCallback( - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t record) { ledger::PublisherInfoList normalized_list; - synopsisNormalizerInternal(&normalized_list, list, 0); - ledger_->SaveNormalizedPublisherList(normalized_list); + synopsisNormalizerInternal(&normalized_list, &list, 0); + ledger_->SaveNormalizedPublisherList(std::move(normalized_list)); } bool BatPublishers::isVerified(const std::string& publisher_id) { @@ -737,7 +737,7 @@ void BatPublishers::getPublisherActivityFromUrl( void BatPublishers::OnPanelPublisherInfo( ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t windowId, const ledger::VisitData& visit_data) { if (result == ledger::Result::LEDGER_OK) { @@ -750,7 +750,7 @@ void BatPublishers::OnPanelPublisherInfo( 0, windowId, result, - std::move(info)); + nullptr); } } @@ -840,7 +840,7 @@ void BatPublishers::onPublisherBanner( ledger::PublisherBannerCallback callback, ledger::PublisherBanner banner, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { auto new_banner = std::make_unique(banner); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h index daba8b1348fd..d65dc4d4370c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers.h @@ -84,7 +84,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void OnPublisherInfoSaved( ledger::Result result, - std::unique_ptr); + ledger::PublisherInfoPtr); std::string GetBalanceReportName(ledger::ACTIVITY_MONTH month, int year); @@ -118,7 +118,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void clearAllBalanceReports(); void NormalizeContributeWinners(ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, uint32_t /* next_record */); bool isVerified(const std::string& publisher_id); @@ -131,7 +131,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void onPublisherActivitySave(uint64_t windowId, const ledger::VisitData& visit_data, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); // LedgerCallbackHandler impl void OnPublisherStateSaved(ledger::Result result) override; @@ -145,7 +145,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { uint64_t duration, uint64_t window_id, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void onFetchFavIcon(const std::string& publisher_key, uint64_t window_id, @@ -153,14 +153,14 @@ class BatPublishers : public ledger::LedgerCallbackHandler { const std::string& favicon_url); void onFetchFavIconDBResponse(ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, const std::string& favicon_url, uint64_t window_id); void onSetExcludeInternal( ledger::PUBLISHER_EXCLUDE exclude, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void OnRestorePublishersInternal(bool success); @@ -172,11 +172,11 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void SynopsisNormalizer(); - void SynopsisNormalizerCallback(const ledger::PublisherInfoList& list, + void SynopsisNormalizerCallback(ledger::PublisherInfoList list, uint32_t /* next_record */); void synopsisNormalizerInternal(ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, uint32_t /* next_record */); bool GetMigrateScore() const; @@ -188,7 +188,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void OnPanelPublisherInfo( ledger::Result result, - std::unique_ptr publisher_info, + ledger::PublisherInfoPtr publisher_info, uint64_t windowId, const ledger::VisitData& visit_data); @@ -198,7 +198,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void onPublisherBanner(ledger::PublisherBannerCallback callback, ledger::PublisherBanner banner, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); bat_ledger::LedgerImpl* ledger_; // NOT OWNED diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc index 0c270860bef5..c4377a5f9c85 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_publishers_unittest.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include + #include "bat/ledger/internal/bat_publishers.h" #include "bat/ledger/ledger.h" #include "testing/gtest/include/gtest/gtest.h" @@ -17,17 +19,18 @@ class BatPublishersTest : public testing::Test { ledger::PublisherInfoList* list) { double prev_score; for (int ix = 0; ix < 50; ix++) { - ledger::PublisherInfo info("example" + std::to_string(ix) + ".com"); - info.duration = 50; + ledger::PublisherInfoPtr info = ledger::PublisherInfo::New(); + info->id = "example" + std::to_string(ix) + ".com"; + info->duration = 50; if (ix == 0) { - info.score = 24; + info->score = 24; } else { - info.score = prev_score / 2; + info->score = prev_score / 2; } - prev_score = info.score; - info.reconcile_stamp = 0; - info.visits = 5; - list->push_back(info); + prev_score = info->score; + info->reconcile_stamp = 0; + info->visits = 5; + list->push_back(std::move(info)); } } }; @@ -122,28 +125,28 @@ TEST_F(BatPublishersTest, synopsisNormalizerInternal) { ledger::PublisherInfoList list; CreatePublisherInfoList(&list); bat_publishers->synopsisNormalizerInternal( - &new_list, list, 0); + &new_list, &list, 0); // simulate exclude and re-normalize new_list.erase(new_list.begin() + 3); ledger::PublisherInfoList new_list2; bat_publishers->synopsisNormalizerInternal( - &new_list2, new_list, 0); + &new_list2, &new_list, 0); new_list2.erase(new_list2.begin() + 4); ledger::PublisherInfoList new_list3; bat_publishers->synopsisNormalizerInternal( - &new_list3, new_list2, 0); + &new_list3, &new_list2, 0); new_list3.erase(new_list3.begin() + 5); ledger::PublisherInfoList new_list4; bat_publishers->synopsisNormalizerInternal( - &new_list4, new_list3, 0); + &new_list4, &new_list3, 0); new_list4.erase(new_list4.begin() + 6); ledger::PublisherInfoList new_list5; bat_publishers->synopsisNormalizerInternal( - &new_list5, new_list4, 0); - for (auto element : new_list5) { - ASSERT_GE((int32_t)element.percent, 0); - ASSERT_LE((int32_t)element.percent, 100); + &new_list5, &new_list4, 0); + for (const auto& element : new_list5) { + ASSERT_GE((int32_t)element->percent, 0); + ASSERT_LE((int32_t)element->percent, 100); } } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.cc index e2d5619bfcb5..bf9451dae100 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.cc @@ -7,6 +7,10 @@ #include "bat/ledger/internal/bignum.h" +extern "C" { +#include "relic.h" // NOLINT +} + namespace braveledger_bat_bignum { void prepareBigNum(bn_t& big_num, const std::string& probi) { // NOLINT diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.h index cdac8fa5839f..94c9de3d8562 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bignum.h @@ -8,20 +8,11 @@ #include -extern "C" { -#include "relic.h" // NOLINT -} - namespace braveledger_bat_bignum { - void prepareBigNum(bn_t& big_num, const std::string& probi); // NOLINT - - std::string bigNumToString(bn_t& number); // NOLINT - - std::string sum(const std::string& a_string, const std::string& b_string); - - std::string sub(const std::string& a_string, const std::string& b_string); - std::string mul(const std::string& a_string, const std::string& b_string); +std::string sum(const std::string& a_string, const std::string& b_string); +std::string sub(const std::string& a_string, const std::string& b_string); +std::string mul(const std::string& a_string, const std::string& b_string); } // namespace braveledger_bat_bignum 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 cdd15dc19982..8f2661786611 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 @@ -379,11 +379,11 @@ std::string LedgerImpl::URIEncode(const std::string& value) { void LedgerImpl::OnPublisherInfoSavedInternal( ledger::Result result, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { bat_publishers_->OnPublisherInfoSaved(result, std::move(info)); } -void LedgerImpl::SetPublisherInfo(std::unique_ptr info) { +void LedgerImpl::SetPublisherInfo(ledger::PublisherInfoPtr info) { if (info) { info->verified = bat_publishers_->isVerified(info->id); } @@ -396,7 +396,7 @@ void LedgerImpl::SetPublisherInfo(std::unique_ptr info) { _2)); } -void LedgerImpl::SetActivityInfo(std::unique_ptr info) { +void LedgerImpl::SetActivityInfo(ledger::PublisherInfoPtr info) { if (info) { info->verified = bat_publishers_->isVerified(info->id); } @@ -448,7 +448,7 @@ void LedgerImpl::LoadNicewareList(ledger::GetNicewareListCallback callback) { void LedgerImpl::ModifyPublisherVerified( ledger::Result result, - std::unique_ptr publisher, + ledger::PublisherInfoPtr publisher, ledger::PublisherInfoCallback callback) { if (publisher) { publisher->verified = bat_publishers_->isVerified(publisher->id); @@ -458,18 +458,18 @@ void LedgerImpl::ModifyPublisherVerified( } void LedgerImpl::ModifyPublisherListVerified( - const ledger::PublisherInfoList& list, + ledger::PublisherInfoList list, uint32_t record, ledger::PublisherInfoListCallback callback) { ledger::PublisherInfoList new_list; for (const auto& publisher : list) { - ledger::PublisherInfo info(publisher); - info.verified = bat_publishers_->isVerified(info.id); - new_list.push_back(info); + auto info = publisher->Clone(); + info->verified = bat_publishers_->isVerified(info->id); + new_list.push_back(std::move(info)); } - callback(new_list, record); + callback(std::move(new_list), record); } void LedgerImpl::GetPublisherInfo(const std::string& publisher_key, @@ -781,10 +781,10 @@ void LedgerImpl::SaveUnverifiedContribution( ledger_client_->SavePendingContribution(list); } -void LedgerImpl::DoDirectTip(const ledger::PublisherInfo& publisher, - int amount, - const std::string& currency) { - if (publisher.id.empty()) { +void LedgerImpl::DoDirectTip(const std::string& publisher_id, + int amount, + const std::string& currency) { + if (publisher_id.empty()) { BLOG(this, ledger::LogLevel::LOG_ERROR) << "Failed direct donation due to missing publisher id"; @@ -792,12 +792,12 @@ void LedgerImpl::DoDirectTip(const ledger::PublisherInfo& publisher, return; } - bool is_verified = bat_publishers_->isVerified(publisher.id); + bool is_verified = bat_publishers_->isVerified(publisher_id); // Save to the pending list if not verified if (!is_verified) { ledger::PendingContribution contribution; - contribution.publisher_key = publisher.id; + contribution.publisher_key = publisher_id; contribution.amount = amount; contribution.category = ledger::REWARDS_CATEGORY::ONE_TIME_TIP; @@ -809,7 +809,7 @@ void LedgerImpl::DoDirectTip(const ledger::PublisherInfo& publisher, return; } - auto direction = braveledger_bat_helper::RECONCILE_DIRECTION(publisher.id, + auto direction = braveledger_bat_helper::RECONCILE_DIRECTION(publisher_id, amount, currency); auto direction_list = @@ -1011,7 +1011,7 @@ void LedgerImpl::GetMediaActivityFromUrl( void LedgerImpl::OnPanelPublisherInfo( ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t windowId) { ledger_client_->OnPanelPublisherInfo(result, std::move(info), windowId); } @@ -1360,7 +1360,7 @@ void LedgerImpl::SaveContributionInfo( void LedgerImpl::NormalizeContributeWinners( ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, uint32_t record) { bat_publishers_->NormalizeContributeWinners(newList, list, record); } @@ -1395,10 +1395,8 @@ bool LedgerImpl::HasSufficientBalanceToReconcile() { } void LedgerImpl::SaveNormalizedPublisherList( - const ledger::PublisherInfoList& normalized_list) { - ledger::PublisherInfoListStruct list; - list.list = normalized_list; - ledger_client_->SaveNormalizedPublisherList(list); + ledger::PublisherInfoList list) { + ledger_client_->SaveNormalizedPublisherList(std::move(list)); } void LedgerImpl::GetAddressesForPaymentId( 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 16720ca78dc7..351ce3a79274 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 @@ -67,10 +67,10 @@ class LedgerImpl : public ledger::Ledger, bool CreateWallet() override; void SetPublisherInfo( - std::unique_ptr publisher_info) override; + ledger::PublisherInfoPtr publisher_info) override; void SetActivityInfo( - std::unique_ptr publisher_info) override; + ledger::PublisherInfoPtr publisher_info) override; void GetPublisherInfo(const std::string& publisher_key, ledger::PublisherInfoCallback callback) override; @@ -92,9 +92,9 @@ class LedgerImpl : public ledger::Ledger, const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoListCallback callback) override; - void DoDirectTip(const ledger::PublisherInfo& publisher, - int amount, - const std::string& currency) override; + void DoDirectTip(const std::string& publisher_id, + int amount, + const std::string& currency) override; void SetRewardsMainEnabled(bool enabled) override; @@ -258,7 +258,7 @@ class LedgerImpl : public ledger::Ledger, const std::string& publisher_blob); void OnPanelPublisherInfo(ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t windowId); void OnExcludedSitesChanged(const std::string& publisher_id, @@ -399,7 +399,7 @@ class LedgerImpl : public ledger::Ledger, void NormalizeContributeWinners( ledger::PublisherInfoList* newList, - const ledger::PublisherInfoList& list, + const ledger::PublisherInfoList* list, uint32_t /* next_record */); void SetTimer(uint64_t time_offset, uint32_t* timer_id) const; @@ -415,7 +415,7 @@ class LedgerImpl : public ledger::Ledger, bool HasSufficientBalanceToReconcile() override; void SaveNormalizedPublisherList( - const ledger::PublisherInfoList& normalized_list); + ledger::PublisherInfoList normalized_list); void GetAddressesForPaymentId(ledger::WalletAddressesCallback callback) override; @@ -485,11 +485,11 @@ class LedgerImpl : public ledger::Ledger, void ModifyPublisherVerified( ledger::Result result, - std::unique_ptr publisher, + ledger::PublisherInfoPtr publisher, ledger::PublisherInfoCallback callback); void ModifyPublisherListVerified( - const ledger::PublisherInfoList&, + ledger::PublisherInfoList, uint32_t record, ledger::PublisherInfoListCallback callback); @@ -511,7 +511,7 @@ class LedgerImpl : public ledger::Ledger, void OnPublisherInfoSavedInternal( ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void DownloadPublisherList( ledger::LoadURLCallback callback); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc index cf3fe4a306a6..51f6fbf24475 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.cc @@ -370,7 +370,7 @@ void MediaTwitch::OnMediaPublisherInfo( const uint64_t window_id, const std::string& user_id, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) @@ -378,7 +378,7 @@ void MediaTwitch::OnMediaPublisherInfo( return; } - if (!publisher_info && !publisher_info.get()) { + if (!publisher_info) { if (media_id.empty()) { return; } @@ -531,7 +531,7 @@ void MediaTwitch::OnMediaPublisherActivity( const std::string& media_id, const std::string& publisher_blob, ledger::Result result, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { OnMediaActivityError(visit_data, window_id); @@ -583,7 +583,7 @@ void MediaTwitch::OnPublisherInfo( const std::string& media_id, const std::string& publisher_blob, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { OnMediaActivityError(visit_data, window_id); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h index ec6dd6fbe51e..b3736aec1a6f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/twitch.h @@ -80,7 +80,7 @@ class MediaTwitch : public ledger::LedgerCallbackHandler { const uint64_t window_id, const std::string& user_id, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void FetchDataFromUrl( const std::string& url, @@ -104,7 +104,7 @@ class MediaTwitch : public ledger::LedgerCallbackHandler { const std::string& media_id, const std::string& publisher_blob, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void OnPublisherInfo( uint64_t window_id, @@ -113,7 +113,7 @@ class MediaTwitch : public ledger::LedgerCallbackHandler { const std::string& media_id, const std::string& publisher_blob, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void SavePublisherInfo(const uint64_t duration, const std::string& media_key, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.cc index 9e54d983e6e6..00d8647e6e86 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.cc @@ -382,7 +382,7 @@ void MediaYouTube::OnMediaPublisherInfo( const ledger::VisitData& visit_data, const uint64_t window_id, ledger::Result result, - std::unique_ptr publisher_info) { + ledger::PublisherInfoPtr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) @@ -390,7 +390,7 @@ void MediaYouTube::OnMediaPublisherInfo( return; } - if (!publisher_info && !publisher_info.get()) { + if (!publisher_info) { std::string media_url = GetVideoUrl(media_id); auto callback = std::bind( &MediaYouTube::OnEmbedResponse, @@ -588,7 +588,7 @@ void MediaYouTube::WatchPath(uint64_t window_id, void MediaYouTube::OnMediaPublisherActivity( ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t window_id, const ledger::VisitData& visit_data, const std::string& media_key, @@ -644,7 +644,7 @@ void MediaYouTube::OnPublisherPanleInfo( const std::string& publisher_key, bool is_custom_path, ledger::Result result, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { if (!info || result == ledger::Result::NOT_FOUND) { FetchDataFromUrl(visit_data.url, std::bind(&MediaYouTube::GetChannelHeadlineVideo, @@ -741,7 +741,7 @@ void MediaYouTube::OnUserActivity( const ledger::VisitData& visit_data, const std::string& media_key, ledger::Result result, - std::unique_ptr info) { + ledger::PublisherInfoPtr info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { OnMediaActivityError(visit_data, window_id); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.h b/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.h index ec38608ab4c4..3e16b2f7d264 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/media/youtube.h @@ -78,7 +78,7 @@ class MediaYouTube : public ledger::LedgerCallbackHandler { const ledger::VisitData& visit_data, uint64_t window_id, ledger::Result result, - std::unique_ptr publisher_info); + ledger::PublisherInfoPtr publisher_info); void OnEmbedResponse( const uint64_t duration, @@ -117,7 +117,7 @@ class MediaYouTube : public ledger::LedgerCallbackHandler { const ledger::VisitData& visit_data); void OnMediaPublisherActivity(ledger::Result result, - std::unique_ptr info, + ledger::PublisherInfoPtr info, uint64_t window_id, const ledger::VisitData& visit_data, const std::string& media_key, @@ -133,7 +133,7 @@ class MediaYouTube : public ledger::LedgerCallbackHandler { const std::string& publisher_key, bool is_custom_path, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void GetChannelHeadlineVideo( uint64_t window_id, @@ -153,7 +153,7 @@ class MediaYouTube : public ledger::LedgerCallbackHandler { const ledger::VisitData& visit_data, const std::string& media_key, ledger::Result result, - std::unique_ptr info); + ledger::PublisherInfoPtr info); void OnChannelIdForUser( uint64_t window_id, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h index 4cf82b3783d1..a70163f0d552 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/rapidjson_bat_helper.h @@ -17,18 +17,15 @@ namespace ledger { struct AutoContributeProps; struct BalanceReportInfo; -struct ContributionInfo; struct ReconcileInfo; struct Grant; struct PublisherBanner; -struct PublisherInfo; struct ActivityInfoFilter; struct RewardsInternalsInfo; struct VisitData; struct WalletInfo; struct PendingContribution; struct PendingContributionList; -struct PublisherInfoListStruct; } // namespace ledger @@ -67,10 +64,8 @@ void saveToJson(JsonWriter* writer, const WALLET_INFO_ST&); void saveToJson(JsonWriter* writer, const GRANTS_PROPERTIES_ST&); void saveToJson(JsonWriter* writer, const ledger::AutoContributeProps&); void saveToJson(JsonWriter* writer, const ledger::BalanceReportInfo&); -void saveToJson(JsonWriter* writer, const ledger::ContributionInfo&); void saveToJson(JsonWriter* writer, const ledger::Grant&); void saveToJson(JsonWriter* writer, const ledger::PublisherBanner&); -void saveToJson(JsonWriter* writer, const ledger::PublisherInfo&); void saveToJson(JsonWriter* writer, const ledger::ActivityInfoFilter&); void saveToJson(JsonWriter* writer, const ledger::ReconcileInfo&); void saveToJson(JsonWriter* writer, const ledger::RewardsInternalsInfo&); @@ -78,7 +73,6 @@ 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&); void saveToJson(JsonWriter* writer, const WALLET_PROPERTIES_ST&); void saveToJson(JsonWriter* writer, const GRANT&); diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 044420a4680b..79d78c59a5f4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -220,153 +220,6 @@ bool PublisherBanner::loadFromJson(const std::string& json) { return !error; } -PublisherInfo::PublisherInfo() : - duration(0u), - score(.0), - visits(0u), - percent(0u), - weight(.0), - excluded(PUBLISHER_EXCLUDE::DEFAULT), - category(REWARDS_CATEGORY::AUTO_CONTRIBUTE), - reconcile_stamp(0), - verified(false), - name(""), - url(""), - provider(""), - favicon_url("") {} - -PublisherInfo::PublisherInfo(const std::string& publisher_id) : - id(publisher_id), - duration(0u), - score(.0), - visits(0u), - percent(0u), - weight(.0), - excluded(PUBLISHER_EXCLUDE::DEFAULT), - category(REWARDS_CATEGORY::AUTO_CONTRIBUTE), - reconcile_stamp(0), - verified(false), - name(""), - url(""), - provider(""), - favicon_url("") {} - -PublisherInfo::PublisherInfo(const PublisherInfo& info) : - id(info.id), - duration(info.duration), - score(info.score), - visits(info.visits), - percent(info.percent), - weight(info.weight), - excluded(info.excluded), - category(info.category), - reconcile_stamp(info.reconcile_stamp), - verified(info.verified), - name(info.name), - url(info.url), - provider(info.provider), - favicon_url(info.favicon_url), - contributions(info.contributions) {} - -PublisherInfo::~PublisherInfo() {} - -bool PublisherInfo::operator<(const PublisherInfo& rhs) const { - return score > rhs.score; -} - -bool PublisherInfo::is_valid() const { - return !id.empty(); -} - -const std::string PublisherInfo::ToJson() const { - std::string json; - braveledger_bat_helper::saveToJsonString(*this, &json); - return json; -} - -bool PublisherInfo::loadFromJson(const std::string& json) { - rapidjson::Document d; - d.Parse(json.c_str()); - // has parser errors or wrong types - bool error = d.HasParseError(); - - if (!error) { - error = !(d.HasMember("id") && d["id"].IsString() && - d.HasMember("duration") && d["duration"].IsUint64() && - d.HasMember("score") && d["score"].IsDouble() && - d.HasMember("visits") && d["visits"].IsUint() && - d.HasMember("percent") && d["percent"].IsUint() && - d.HasMember("weight") && d["weight"].IsDouble() && - d.HasMember("excluded") && d["excluded"].IsInt() && - d.HasMember("category") && d["category"].IsInt() && - d.HasMember("reconcile_stamp") && d["reconcile_stamp"].IsUint64() && - d.HasMember("verified") && d["verified"].IsBool() && - d.HasMember("name") && d["name"].IsString() && - d.HasMember("url") && d["url"].IsString() && - d.HasMember("provider") && d["provider"].IsString() && - d.HasMember("favicon_url") && d["favicon_url"].IsString() && - d.HasMember("contributions") && d["contributions"].IsArray()); - } - - if (!error) { - id = d["id"].GetString(); - duration = d["duration"].GetUint64(); - score = d["score"].GetDouble(); - visits = d["visits"].GetUint(); - percent = d["percent"].GetUint(); - weight = d["weight"].GetDouble(); - excluded = (PUBLISHER_EXCLUDE)d["excluded"].GetInt(); - category = (REWARDS_CATEGORY)d["category"].GetInt(); - reconcile_stamp = d["reconcile_stamp"].GetUint64(); - verified = d["verified"].GetBool(); - name = d["name"].GetString(); - url = d["url"].GetString(); - provider = d["provider"].GetString(); - favicon_url = d["favicon_url"].GetString(); - - for (const auto& contribution : d["contributions"].GetArray()) { - rapidjson::StringBuffer sb; - rapidjson::Writer writer(sb); - contribution.Accept(writer); - - ContributionInfo info; - info.loadFromJson(sb.GetString()); - contributions.push_back(info); - } - } - - return !error; -} - -const PublisherInfo invalid(""); - -const std::string ContributionInfo::ToJson() const { - std::string json; - braveledger_bat_helper::saveToJsonString(*this, &json); - return json; -} - -bool ContributionInfo::loadFromJson(const std::string& json) { - rapidjson::Document d; - d.Parse(json.c_str()); - - // has parser errors or wrong types - bool error = d.HasParseError(); - if (!error) { - error = !(d.HasMember("publisher") && d["publisher"].IsString() && - d.HasMember("value") && d["value"].IsDouble() && - d.HasMember("date") && d["date"].IsUint64()); - } - - if (!error) { - publisher = d["publisher"].GetString(); - value = d["value"].GetDouble(); - date = d["date"].GetUint64(); - } - - return !error; -} - TwitchEventInfo::TwitchEventInfo() {} TwitchEventInfo::TwitchEventInfo(const TwitchEventInfo& info): @@ -807,43 +660,4 @@ 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 (!error) { - error = !(d.HasMember("list") && d["list"].IsArray()); - } - - if (!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; -} - } // namespace ledger