From 177b00a2b0b8a931a033a0fd9bd12d1284e71138 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 16 Nov 2018 11:18:35 +0100 Subject: [PATCH 01/14] Refactor database - removes category column - adds visits column Resolves https://github.com/brave/brave-browser/issues/2163 --- .../browser/publisher_info_database.cc | 109 ++++++++++++------ 1 file changed, 74 insertions(+), 35 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 385251f2a6a3..58d1333bad2c 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -22,7 +22,7 @@ namespace brave_rewards { namespace { -const int kCurrentVersionNumber = 3; +const int kCurrentVersionNumber = 4; const int kCompatibleVersionNumber = 1; } // namespace @@ -154,10 +154,10 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() { "(" "publisher_id LONGVARCHAR NOT NULL," "duration INTEGER DEFAULT 0 NOT NULL," + "visits INTEGER DEFAULT 0 NOT NULL," "score DOUBLE DEFAULT 0 NOT NULL," "percent INTEGER DEFAULT 0 NOT NULL," "weight DOUBLE DEFAULT 0 NOT NULL," - "category INTEGER NOT NULL," "month INTEGER NOT NULL," "year INTEGER NOT NULL," "reconcile_stamp INTEGER DEFAULT 0 NOT NULL," @@ -265,14 +265,13 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( sql::Statement activity_get( db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE " - "publisher_id=? AND category=? " - "AND month=? AND year=? AND reconcile_stamp=?")); + "publisher_id=? AND month=? " + "AND year=? AND reconcile_stamp=?")); activity_get.BindString(0, info.id); - activity_get.BindInt(1, info.category); - activity_get.BindInt(2, info.month); - activity_get.BindInt(3, info.year); - activity_get.BindInt64(4, info.reconcile_stamp); + activity_get.BindInt(1, info.month); + activity_get.BindInt(2, info.year); + activity_get.BindInt64(3, info.reconcile_stamp); if (activity_get.Step()) { sql::Statement activity_info_update( @@ -280,18 +279,17 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( "UPDATE activity_info SET " "duration=?, score=?, percent=?, " "weight=? WHERE " - "publisher_id=? AND category=? " - "AND month=? AND year=? AND reconcile_stamp=?")); + "publisher_id=? AND month=? " + "AND year=? AND reconcile_stamp=?")); activity_info_update.BindInt64(0, (int)info.duration); activity_info_update.BindDouble(1, info.score); activity_info_update.BindInt64(2, (int)info.percent); activity_info_update.BindDouble(3, info.weight); activity_info_update.BindString(4, info.id); - activity_info_update.BindInt(5, info.category); - activity_info_update.BindInt(6, info.month); - activity_info_update.BindInt(7, info.year); - activity_info_update.BindInt64(8, info.reconcile_stamp); + activity_info_update.BindInt(5, info.month); + activity_info_update.BindInt(6, info.year); + activity_info_update.BindInt64(7, info.reconcile_stamp); return activity_info_update.Run(); } @@ -300,7 +298,7 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( GetDB().GetCachedStatement(SQL_FROM_HERE, "INSERT INTO activity_info " "(publisher_id, duration, score, percent, " - "weight, category, month, year, reconcile_stamp) " + "weight, month, year, reconcile_stamp) " "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); activity_info_insert.BindString(0, info.id); @@ -308,10 +306,9 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( activity_info_insert.BindDouble(2, info.score); activity_info_insert.BindInt64(3, (int)info.percent); activity_info_insert.BindDouble(4, info.weight); - activity_info_insert.BindInt(5, info.category); - activity_info_insert.BindInt(6, info.month); - activity_info_insert.BindInt(7, info.year); - activity_info_insert.BindInt64(8, info.reconcile_stamp); + activity_info_insert.BindInt(5, info.month); + activity_info_insert.BindInt(6, info.year); + activity_info_insert.BindInt64(7, info.reconcile_stamp); return activity_info_insert.Run(); } @@ -386,7 +383,7 @@ bool PublisherInfoDatabase::Find(int start, return false; std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, ai.percent, " - "ai.weight, pi.verified, pi.excluded, ai.category, ai.month, ai.year, pi.name, " + "ai.weight, pi.verified, pi.excluded, ai.month, ai.year, pi.name, " "pi.url, pi.provider, pi.favIcon, ai.reconcile_stamp " "FROM activity_info AS ai " "INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id " @@ -401,8 +398,8 @@ bool PublisherInfoDatabase::Find(int start, while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); ledger::PUBLISHER_MONTH month( - static_cast(info_sql.ColumnInt(8))); - int year(info_sql.ColumnInt(9)); + static_cast(info_sql.ColumnInt(7))); + int year(info_sql.ColumnInt(8)); ledger::PublisherInfo info(id, month, year); info.duration = info_sql.ColumnInt64(1); @@ -411,15 +408,13 @@ bool PublisherInfoDatabase::Find(int start, info.percent = info_sql.ColumnInt64(3); info.weight = info_sql.ColumnDouble(4); info.verified = info_sql.ColumnBool(5); - info.name = info_sql.ColumnString(10); - info.url = info_sql.ColumnString(11); - info.provider = info_sql.ColumnString(12); - info.favicon_url = info_sql.ColumnString(13); - info.reconcile_stamp = info_sql.ColumnInt64(14); + info.name = info_sql.ColumnString(9); + info.url = info_sql.ColumnString(10); + info.provider = info_sql.ColumnString(11); + info.favicon_url = info_sql.ColumnString(12); + info.reconcile_stamp = info_sql.ColumnInt64(13); info.excluded = static_cast(info_sql.ColumnInt(6)); - info.category = - static_cast(info_sql.ColumnInt(7)); list->push_back(info); } @@ -461,9 +456,6 @@ std::string PublisherInfoDatabase::BuildClauses(int start, if (!filter.id.empty()) clauses += " AND ai.publisher_id = ?"; - if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES) - clauses += " AND ai.category = ?"; - if (filter.month != ledger::PUBLISHER_MONTH::ANY) clauses += " AND ai.month = ?"; @@ -515,9 +507,6 @@ void PublisherInfoDatabase::BindFilter(sql::Statement& statement, if (!filter.id.empty()) statement.BindString(column++, filter.id); - if (filter.category != ledger::PUBLISHER_CATEGORY::ALL_CATEGORIES) - statement.BindInt(column++, filter.category); - if (filter.month != ledger::PUBLISHER_MONTH::ANY) statement.BindInt(column++, filter.month); @@ -865,6 +854,49 @@ bool PublisherInfoDatabase::MigrateV2toV3() { return CreatePendingContributionsIndex(); } +bool PublisherInfoDatabase::MigrateV3toV4() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + // Activity info + const char* activity = "activity_info"; + if (GetDB().DoesTableExist(activity)) { + std::string sql = "ALTER TABLE activity_info RENAME TO activity_info_old;"; + + if (!GetDB().Execute(sql.c_str())) { + return false; + } + + if (!CreateActivityInfoTable()) { + return false; + } + + if (!CreateActivityInfoIndex()) { + return false; + } + + std::string columns = "publisher_id, " + "duration, " + "score, " + "percent, " + "weight, " + "month, " + "year, " + "reconcile_stamp"; + + sql = "PRAGMA foreign_keys=off;"; + sql.append("INSERT INTO activity_info (" + columns + ") " + "SELECT " + columns + " " + "FROM activity_info_old;"); + sql.append("UPDATE activity_info SET visits=5;"); + sql.append("DROP TABLE activity_info_old;"); + sql.append("PRAGMA foreign_keys=on;"); + + return GetDB().Execute(sql.c_str()); + } + + return false; +} + sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -891,6 +923,13 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() { } } + // to version 4 + if (old_version < 4 && cur_version < 5) { + if (!MigrateV2toV3()) { + LOG(ERROR) << "DB: Error with MigrateV3toV4"; + } + } + meta_table_.SetVersionNumber(cur_version); return sql::INIT_OK; } From a2803c2f05b739e88aa962f91813adba883f336a Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 09:56:36 +0100 Subject: [PATCH 02/14] Reordered database files --- .../browser/publisher_info_database.cc | 731 +++++++++--------- .../browser/publisher_info_database.h | 54 +- .../browser/rewards_service_impl.cc | 8 +- 3 files changed, 423 insertions(+), 370 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 58d1333bad2c..aee8ebefe95d 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -39,19 +39,24 @@ PublisherInfoDatabase::~PublisherInfoDatabase() { bool PublisherInfoDatabase::Init() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (initialized_) + if (initialized_) { return true; + } - if (!db_.Open(db_path_)) + if (!db_.Open(db_path_)) { return false; + } // TODO - add error delegate sql::Transaction committer(&db_); - if (!committer.Begin()) + if (!committer.Begin()) { return false; + } - if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) + if (!meta_table_.Init(&db_, GetCurrentVersion(), kCompatibleVersionNumber)) { return false; + } + if (!CreatePublisherInfoTable() || !CreateContributionInfoTable() || !CreateActivityInfoTable() || @@ -59,6 +64,7 @@ bool PublisherInfoDatabase::Init() { !CreateRecurringDonationTable() || !CreatePendingContributionsTable()) return false; + } CreateContributionInfoIndex(); CreateActivityInfoIndex(); @@ -67,11 +73,13 @@ bool PublisherInfoDatabase::Init() { // Version check. sql::InitStatus version_status = EnsureCurrentVersion(); - if (version_status != sql::INIT_OK) + if (version_status != sql::INIT_OK) { return version_status; + } - if (!committer.Commit()) + if (!committer.Commit()) { return false; + } memory_pressure_listener_.reset(new base::MemoryPressureListener( base::Bind(&PublisherInfoDatabase::OnMemoryPressure, @@ -81,15 +89,20 @@ bool PublisherInfoDatabase::Init() { return initialized_; } +/** + * + * CONTRIBUTION INFO + * + */ + bool PublisherInfoDatabase::CreateContributionInfoTable() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); const char* name = "contribution_info"; - if (GetDB().DoesTableExist(name)) + if (GetDB().DoesTableExist(name)) { return true; + } - // Note: revise implementation for InsertOrUpdatePublisherInfo() if you add - // any new constraints to the schema. std::string sql; sql.append("CREATE TABLE "); sql.append(name); @@ -105,6 +118,7 @@ bool PublisherInfoDatabase::CreateContributionInfoTable() { " FOREIGN KEY (publisher_id)" " REFERENCES publisher_info (publisher_id)" " ON DELETE CASCADE)"); + return GetDB().Execute(sql.c_str()); } @@ -116,14 +130,89 @@ bool PublisherInfoDatabase::CreateContributionInfoIndex() { "ON contribution_info (publisher_id)"); } +bool PublisherInfoDatabase::InsertContributionInfo( + const brave_rewards::ContributionInfo& info) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return false; + } + + sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT INTO contribution_info " + "(publisher_id, probi, date, " + "category, month, year) " + "VALUES (?, ?, ?, ?, ?, ?)")); + + statement.BindString(0, info.publisher_key); + statement.BindString(1, info.probi); + statement.BindInt64(2, info.date); + statement.BindInt(3, info.category); + statement.BindInt(4, info.month); + statement.BindInt(5, info.year); + + return statement.Run(); +} + +void PublisherInfoDatabase::GetTips(ledger::PublisherInfoList* list, + ledger::PUBLISHER_MONTH month, + int year) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return; + } + + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " + "ci.probi, ci.date, pi.verified, pi.provider " + "FROM contribution_info as ci " + "INNER JOIN publisher_info AS pi ON ci.publisher_id = pi.publisher_id " + "AND ci.month = ? AND ci.year = ? " + "AND (ci.category = ? OR ci.category = ?)")); + + info_sql.BindInt(0, month); + info_sql.BindInt(1, year); + info_sql.BindInt(2, ledger::PUBLISHER_CATEGORY::DIRECT_DONATION); + info_sql.BindInt(3, ledger::PUBLISHER_CATEGORY::TIPPING); + + while (info_sql.Step()) { + std::string id(info_sql.ColumnString(0)); + + ledger::PublisherInfo publisher(id, ledger::PUBLISHER_MONTH::ANY, -1); + + 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); + } +} + +/** + * + * PUBLISHER INFO + * + */ + bool PublisherInfoDatabase::CreatePublisherInfoTable() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); const char* name = "publisher_info"; - if (GetDB().DoesTableExist(name)) + if (GetDB().DoesTableExist(name)) { return true; + } - // Update InsertOrUpdatePublisherInfo() if you add anything here std::string sql; sql.append("CREATE TABLE "); sql.append(name); @@ -136,17 +225,23 @@ bool PublisherInfoDatabase::CreatePublisherInfoTable() { "favIcon TEXT NOT NULL," "url TEXT NOT NULL," "provider TEXT NOT NULL)"); + return GetDB().Execute(sql.c_str()); } +/** + * + * ACTIVITY INFO + * + */ bool PublisherInfoDatabase::CreateActivityInfoTable() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); const char* name = "activity_info"; - if (GetDB().DoesTableExist(name)) + if (GetDB().DoesTableExist(name)) { return true; + } - // Update InsertOrUpdatePublisherInfo() if you add anything here std::string sql; sql.append("CREATE TABLE "); sql.append(name); @@ -165,6 +260,7 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() { " FOREIGN KEY (publisher_id)" " REFERENCES publisher_info (publisher_id)" " ON DELETE CASCADE)"); + return GetDB().Execute(sql.c_str()); } @@ -176,224 +272,120 @@ bool PublisherInfoDatabase::CreateActivityInfoIndex() { "ON activity_info (publisher_id)"); } -bool PublisherInfoDatabase::CreateMediaPublisherInfoTable() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - const char* name = "media_publisher_info"; - if (GetDB().DoesTableExist(name)) - return true; - - // Update InsertOrUpdateMediaPublisherInfo() if you add anything here - std::string sql; - sql.append("CREATE TABLE "); - sql.append(name); - sql.append( - "(" - "media_key TEXT NOT NULL PRIMARY KEY UNIQUE," - "publisher_id LONGVARCHAR NOT NULL," - "CONSTRAINT fk_media_publisher_info_publisher_id" - " FOREIGN KEY (publisher_id)" - " REFERENCES publisher_info (publisher_id)" - " ON DELETE CASCADE)"); - return GetDB().Execute(sql.c_str()); -} - -bool PublisherInfoDatabase::CreateRecurringDonationTable() { +bool PublisherInfoDatabase::GetPublisherActivityList( + int start, + int limit, + const ledger::PublisherInfoFilter& filter, + ledger::PublisherInfoList* list) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - const char* name = "recurring_donation"; - if (GetDB().DoesTableExist(name)) - return true; - - // Note: revise implementation for InsertOrUpdatePublisherInfo() if you add - // any new constraints to the schema. - std::string sql; - sql.append("CREATE TABLE "); - sql.append(name); - sql.append( - "(" - "publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE," - "amount DOUBLE DEFAULT 0 NOT NULL," - "added_date INTEGER DEFAULT 0 NOT NULL," - "CONSTRAINT fk_recurring_donation_publisher_id" - " FOREIGN KEY (publisher_id)" - " REFERENCES publisher_info (publisher_id)" - " ON DELETE CASCADE)"); - return GetDB().Execute(sql.c_str()); -} - -bool PublisherInfoDatabase::CreateRecurringDonationIndex() { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - return GetDB().Execute( - "CREATE INDEX IF NOT EXISTS recurring_donation_publisher_id_index " - "ON recurring_donation (publisher_id)"); -} - -bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( - const ledger::PublisherInfo& info) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + CHECK(list); bool initialized = Init(); DCHECK(initialized); - if (!initialized) - return false; - - sql::Statement publisher_info_statement( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT OR REPLACE INTO publisher_info " - "(publisher_id, verified, excluded, " - "name, url, provider, favIcon) " - "VALUES (?, ?, ?, ?, ?, ?, ?)")); - - publisher_info_statement.BindString(0, info.id); - publisher_info_statement.BindBool(1, info.verified); - publisher_info_statement.BindInt(2, static_cast(info.excluded)); - publisher_info_statement.BindString(3, info.name); - publisher_info_statement.BindString(4, info.url); - publisher_info_statement.BindString(5, info.provider); - publisher_info_statement.BindString(6, info.favicon_url); - - if (!publisher_info_statement.Run()) { + if (!initialized) { return false; } - if (info.month == ledger::PUBLISHER_MONTH::ANY || info.year == -1) { - return true; + std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, " + "ai.percent, ai.weight, pi.verified, pi.excluded, " + "ai.month, ai.year, pi.name, pi.url, pi.provider, " + "pi.favIcon, ai.reconcile_stamp " + "FROM activity_info AS ai " + "INNER JOIN publisher_info AS pi " + "ON ai.publisher_id = pi.publisher_id " + "WHERE 1 = 1"; + + if (!filter.id.empty()) { + query += " AND ai.publisher_id = ?"; } - sql::Statement activity_get( - db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE " - "publisher_id=? AND month=? " - "AND year=? AND reconcile_stamp=?")); - - activity_get.BindString(0, info.id); - activity_get.BindInt(1, info.month); - activity_get.BindInt(2, info.year); - activity_get.BindInt64(3, info.reconcile_stamp); - - if (activity_get.Step()) { - sql::Statement activity_info_update( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "UPDATE activity_info SET " - "duration=?, score=?, percent=?, " - "weight=? WHERE " - "publisher_id=? AND month=? " - "AND year=? AND reconcile_stamp=?")); - - activity_info_update.BindInt64(0, (int)info.duration); - activity_info_update.BindDouble(1, info.score); - activity_info_update.BindInt64(2, (int)info.percent); - activity_info_update.BindDouble(3, info.weight); - activity_info_update.BindString(4, info.id); - activity_info_update.BindInt(5, info.month); - activity_info_update.BindInt(6, info.year); - activity_info_update.BindInt64(7, info.reconcile_stamp); - - return activity_info_update.Run(); + if (filter.month != ledger::PUBLISHER_MONTH::ANY) { + query += " AND ai.month = ?"; } - sql::Statement activity_info_insert( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT INTO activity_info " - "(publisher_id, duration, score, percent, " - "weight, month, year, reconcile_stamp) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); - - activity_info_insert.BindString(0, info.id); - activity_info_insert.BindInt64(1, (int)info.duration); - activity_info_insert.BindDouble(2, info.score); - activity_info_insert.BindInt64(3, (int)info.percent); - activity_info_insert.BindDouble(4, info.weight); - activity_info_insert.BindInt(5, info.month); - activity_info_insert.BindInt(6, info.year); - activity_info_insert.BindInt64(7, info.reconcile_stamp); - - return activity_info_insert.Run(); -} - -bool PublisherInfoDatabase::InsertOrUpdateMediaPublisherInfo( - const std::string& media_key, const std::string& publisher_id) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - bool initialized = Init(); - DCHECK(initialized); + if (filter.year > 0) { + query += " AND ai.year = ?"; + } - if (!initialized) - return false; + if (filter.reconcile_stamp > 0) { + query += " AND ai.reconcile_stamp = ?"; + } - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT OR REPLACE INTO media_publisher_info " - "(media_key, publisher_id) " - "VALUES (?, ?)")); + if (filter.min_duration > 0) { + query += " AND ai.duration >= ?"; + } - statement.BindString(0, media_key); - statement.BindString(1, publisher_id); + if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && + filter.excluded != + ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + query += " AND pi.excluded = ?"; + } - return statement.Run(); -} + if (filter.excluded == + ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + query += " AND pi.excluded != ?"; + } -std::unique_ptr -PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + if (filter.percent > 0) { + clauses += " AND ai.percent >= ?"; + } - bool initialized = Init(); - DCHECK(initialized); + if (!filter.non_verified) { + clauses += " AND pi.verified = 1"; + } - std::unique_ptr info; + for (const auto& it : filter.order_by) { + query += " ORDER BY " + it.first; + query += (it.second ? " ASC" : " DESC"); + } - if (!initialized) - return info; + if (limit > 0) { + query += " LIMIT " + std::to_string(limit); - sql::Statement info_sql( - db_.GetUniqueStatement("SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " - "pi.provider, pi.verified, pi.excluded " - "FROM media_publisher_info as mpi " - "INNER JOIN publisher_info AS pi ON mpi.publisher_id = pi.publisher_id " - "WHERE mpi.media_key=?")); + if (start > 1) { + query += " OFFSET " + std::to_string(start); + } + } - info_sql.BindString(0, media_key); + sql::Statement info_sql(db_.GetUniqueStatement(query.c_str())); - if (info_sql.Step()) { - info.reset(new ledger::PublisherInfo()); - info->id = info_sql.ColumnString(0); - info->name = info_sql.ColumnString(1); - info->url = info_sql.ColumnString(2); - info->favicon_url = info_sql.ColumnString(3); - info->provider = info_sql.ColumnString(4); - info->verified = info_sql.ColumnBool(5); - info->excluded = static_cast(info_sql.ColumnInt(6)); + int column = 0; + if (!filter.id.empty()) { + info_sql.BindString(column++, filter.id); } - return info; -} -bool PublisherInfoDatabase::Find(int start, - int limit, - const ledger::PublisherInfoFilter& filter, - ledger::PublisherInfoList* list) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - CHECK(list); + if (filter.month != ledger::PUBLISHER_MONTH::ANY) { + info_sql.BindInt(column++, filter.month); + } - bool initialized = Init(); - DCHECK(initialized); + if (filter.year > 0) { + info_sql.BindInt(column++, filter.year); + } - if (!initialized) - return false; + if (filter.reconcile_stamp > 0) { + info_sql.BindInt64(column++, filter.reconcile_stamp); + } - std::string query = "SELECT ai.publisher_id, ai.duration, ai.score, ai.percent, " - "ai.weight, pi.verified, pi.excluded, ai.month, ai.year, pi.name, " - "pi.url, pi.provider, pi.favIcon, ai.reconcile_stamp " - "FROM activity_info AS ai " - "INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id " - "WHERE 1 = 1"; + if (filter.min_duration > 0) { + info_sql.BindInt(column++, filter.min_duration); + } - query+= BuildClauses(start, limit, filter); + if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && + filter.excluded != + ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + info_sql.BindInt(column++, filter.excluded); + } - sql::Statement info_sql(db_.GetUniqueStatement(query.c_str())); + if (filter.excluded == + ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + info_sql.BindInt(column++, ledger::PUBLISHER_EXCLUDE::EXCLUDED); + } - BindFilter(info_sql, filter); + if (filter.percent > 0) { + info_sql.BindInt(column++, filter.percent); + } while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); @@ -414,7 +406,8 @@ bool PublisherInfoDatabase::Find(int start, info.favicon_url = info_sql.ColumnString(12); info.reconcile_stamp = info_sql.ColumnInt64(13); - info.excluded = static_cast(info_sql.ColumnInt(6)); + info.excluded = static_cast( + info_sql.ColumnInt(6)); list->push_back(info); } @@ -422,151 +415,144 @@ bool PublisherInfoDatabase::Find(int start, return list; } -int PublisherInfoDatabase::Count(const ledger::PublisherInfoFilter& filter) { +/** + * + * MEDIA PUBLISHER INFO + * + */ +bool PublisherInfoDatabase::CreateMediaPublisherInfoTable() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - bool initialized = Init(); - DCHECK(initialized); - - if (!initialized) - return false; - - std::string query = "SELECT COUNT(ai.publisher_id) " - "FROM activity_info AS ai " - "INNER JOIN publisher_info AS pi ON ai.publisher_id = pi.publisher_id " - "WHERE 1 = 1"; - - query+= BuildClauses(0, 0, filter); - - sql::Statement publisher_count(db_.GetUniqueStatement(query.c_str())); - - BindFilter(publisher_count, filter); + const char* name = "media_publisher_info"; + if (GetDB().DoesTableExist(name)) { + return true; + } - if (!publisher_count.Step()) - return 0; + std::string sql; + sql.append("CREATE TABLE "); + sql.append(name); + sql.append( + "(" + "media_key TEXT NOT NULL PRIMARY KEY UNIQUE," + "publisher_id LONGVARCHAR NOT NULL," + "CONSTRAINT fk_media_publisher_info_publisher_id" + " FOREIGN KEY (publisher_id)" + " REFERENCES publisher_info (publisher_id)" + " ON DELETE CASCADE)"); - return publisher_count.ColumnInt(0); + return GetDB().Execute(sql.c_str()); } -std::string PublisherInfoDatabase::BuildClauses(int start, - int limit, - const ledger::PublisherInfoFilter& filter) { - std::string clauses = ""; +bool PublisherInfoDatabase::InsertOrUpdateMediaPublisherInfo( + const std::string& media_key, const std::string& publisher_id) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (!filter.id.empty()) - clauses += " AND ai.publisher_id = ?"; + bool initialized = Init(); + DCHECK(initialized); - if (filter.month != ledger::PUBLISHER_MONTH::ANY) - clauses += " AND ai.month = ?"; + if (!initialized) { + return false; + } - if (filter.year > 0) - clauses += " AND ai.year = ?"; + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, + "INSERT OR REPLACE INTO media_publisher_info " + "(media_key, publisher_id) " + "VALUES (?, ?)")); - if (filter.reconcile_stamp > 0) - clauses += " AND ai.reconcile_stamp = ?"; + statement.BindString(0, media_key); + statement.BindString(1, publisher_id); - if (filter.min_duration > 0) - clauses += " AND ai.duration >= ?"; + return statement.Run(); +} - if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && - filter.excluded != - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) - clauses += " AND pi.excluded = ?"; +std::unique_ptr +PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (filter.excluded == - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) - clauses += " AND pi.excluded != ?"; + bool initialized = Init(); + DCHECK(initialized); - if (filter.percent > 0) { - clauses += " AND ai.percent >= ?"; - } + std::unique_ptr info; - if (!filter.non_verified) { - clauses += " AND pi.verified = 1"; + if (!initialized) { + return info; } - for (const auto& it : filter.order_by) { - clauses += " ORDER BY " + it.first; - clauses += (it.second ? " ASC" : " DESC"); - } + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " + "pi.provider, pi.verified, pi.excluded " + "FROM media_publisher_info as mpi " + "INNER JOIN publisher_info AS pi ON mpi.publisher_id = pi.publisher_id " + "WHERE mpi.media_key=?")); - if (limit > 0) { - clauses += " LIMIT " + std::to_string(limit); + info_sql.BindString(0, media_key); - if (start > 1) { - clauses += " OFFSET " + std::to_string(start); - } + if (info_sql.Step()) { + info.reset(new ledger::PublisherInfo()); + info->id = info_sql.ColumnString(0); + info->name = info_sql.ColumnString(1); + info->url = info_sql.ColumnString(2); + info->favicon_url = info_sql.ColumnString(3); + info->provider = info_sql.ColumnString(4); + info->verified = info_sql.ColumnBool(5); + info->excluded = static_cast( + info_sql.ColumnInt(6)); } - return clauses; + return info; } -void PublisherInfoDatabase::BindFilter(sql::Statement& statement, - const ledger::PublisherInfoFilter& filter) { - int column = 0; - if (!filter.id.empty()) - statement.BindString(column++, filter.id); - - if (filter.month != ledger::PUBLISHER_MONTH::ANY) - statement.BindInt(column++, filter.month); - - if (filter.year > 0) - statement.BindInt(column++, filter.year); - - if (filter.reconcile_stamp > 0) - statement.BindInt64(column++, filter.reconcile_stamp); - - if (filter.min_duration > 0) - statement.BindInt(column++, filter.min_duration); +/** + * + * RECURRING DONATION + * + */ +bool PublisherInfoDatabase::CreateRecurringDonationTable() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && - filter.excluded != - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) - statement.BindInt(column++, filter.excluded); + const char* name = "recurring_donation"; + if (GetDB().DoesTableExist(name)) { + return true; + } - if (filter.excluded == - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) - statement.BindInt(column++, ledger::PUBLISHER_EXCLUDE::EXCLUDED); + std::string sql; + sql.append("CREATE TABLE "); + sql.append(name); + sql.append( + "(" + "publisher_id LONGVARCHAR NOT NULL PRIMARY KEY UNIQUE," + "amount DOUBLE DEFAULT 0 NOT NULL," + "added_date INTEGER DEFAULT 0 NOT NULL," + "CONSTRAINT fk_recurring_donation_publisher_id" + " FOREIGN KEY (publisher_id)" + " REFERENCES publisher_info (publisher_id)" + " ON DELETE CASCADE)"); - if (filter.percent > 0) - statement.BindInt(column++, filter.percent); + return GetDB().Execute(sql.c_str()); } -bool PublisherInfoDatabase::InsertContributionInfo(const brave_rewards::ContributionInfo& info) { +bool PublisherInfoDatabase::CreateRecurringDonationIndex() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - bool initialized = Init(); - DCHECK(initialized); - - if (!initialized) - return false; - - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT INTO contribution_info " - "(publisher_id, probi, date, " - "category, month, year) " - "VALUES (?, ?, ?, ?, ?, ?)")); - - statement.BindString(0, info.publisher_key); - statement.BindString(1, info.probi); - statement.BindInt64(2, info.date); - statement.BindInt(3, info.category); - statement.BindInt(4, info.month); - statement.BindInt(5, info.year); - - return statement.Run(); + return GetDB().Execute( + "CREATE INDEX IF NOT EXISTS recurring_donation_publisher_id_index " + "ON recurring_donation (publisher_id)"); } -bool PublisherInfoDatabase::InsertOrUpdateRecurringDonation(const brave_rewards::RecurringDonation& info) { +bool PublisherInfoDatabase::InsertOrUpdateRecurringDonation( + const brave_rewards::RecurringDonation& info) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); DCHECK(initialized); - if (!initialized) + if (!initialized) { return false; + } - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, "INSERT OR REPLACE INTO recurring_donation " "(publisher_id, amount, added_date) " "VALUES (?, ?, ?)")); @@ -578,20 +564,22 @@ bool PublisherInfoDatabase::InsertOrUpdateRecurringDonation(const brave_rewards: return statement.Run(); } -void PublisherInfoDatabase::GetRecurringDonations(ledger::PublisherInfoList* list) { +void PublisherInfoDatabase::GetRecurringDonations( + ledger::PublisherInfoList* list) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); DCHECK(initialized); - if (!initialized) + if (!initialized) { return; + } - sql::Statement info_sql( - db_.GetUniqueStatement("SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " - "rd.amount, rd.added_date, pi.verified, pi.provider " - "FROM recurring_donation as rd " - "INNER JOIN publisher_info AS pi ON rd.publisher_id = pi.publisher_id ")); + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " + "rd.amount, rd.added_date, pi.verified, pi.provider " + "FROM recurring_donation as rd " + "INNER JOIN publisher_info AS pi ON rd.publisher_id = pi.publisher_id ")); while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); @@ -610,46 +598,30 @@ void PublisherInfoDatabase::GetRecurringDonations(ledger::PublisherInfoList* lis } } -void PublisherInfoDatabase::GetTips(ledger::PublisherInfoList* list, ledger::PUBLISHER_MONTH month, int year) { +bool PublisherInfoDatabase::RemoveRecurring(const std::string& publisher_key) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); DCHECK(initialized); - if (!initialized) - return; - - sql::Statement info_sql( - db_.GetUniqueStatement("SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, " - "ci.probi, ci.date, pi.verified, pi.provider " - "FROM contribution_info as ci " - "INNER JOIN publisher_info AS pi ON ci.publisher_id = pi.publisher_id " - "AND ci.month = ? AND ci.year = ? " - "AND (ci.category = ? OR ci.category = ?)")); + if (!initialized) { + return false; + } - info_sql.BindInt(0, month); - info_sql.BindInt(1, year); - info_sql.BindInt(2, ledger::PUBLISHER_CATEGORY::DIRECT_DONATION); - info_sql.BindInt(3, ledger::PUBLISHER_CATEGORY::TIPPING); + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, + "DELETE FROM recurring_donation WHERE publisher_id = ?")); - while (info_sql.Step()) { - std::string id(info_sql.ColumnString(0)); + statement.BindString(0, publisher_key); - ledger::PublisherInfo publisher(id, ledger::PUBLISHER_MONTH::ANY, -1); + return statement.Run(); +} - 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); - } -} -bool PublisherInfoDatabase::RemoveRecurring(const std::string& publisher_key) { +bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( + const ledger::PublisherInfo& info) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool initialized = Init(); @@ -658,12 +630,77 @@ bool PublisherInfoDatabase::RemoveRecurring(const std::string& publisher_key) { if (!initialized) return false; - sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, - "DELETE FROM recurring_donation WHERE publisher_id = ?")); + sql::Statement publisher_info_statement( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT OR REPLACE INTO publisher_info " + "(publisher_id, verified, excluded, " + "name, url, provider, favIcon) " + "VALUES (?, ?, ?, ?, ?, ?, ?)")); - statement.BindString(0, publisher_key); + publisher_info_statement.BindString(0, info.id); + publisher_info_statement.BindBool(1, info.verified); + publisher_info_statement.BindInt(2, static_cast(info.excluded)); + publisher_info_statement.BindString(3, info.name); + publisher_info_statement.BindString(4, info.url); + publisher_info_statement.BindString(5, info.provider); + publisher_info_statement.BindString(6, info.favicon_url); - return statement.Run(); + if (!publisher_info_statement.Run()) { + return false; + } + + if (info.month == ledger::PUBLISHER_MONTH::ANY || info.year == -1) { + return true; + } + + sql::Statement activity_get( + db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE " + "publisher_id=? AND month=? " + "AND year=? AND reconcile_stamp=?")); + + activity_get.BindString(0, info.id); + activity_get.BindInt(1, info.month); + activity_get.BindInt(2, info.year); + activity_get.BindInt64(3, info.reconcile_stamp); + + if (activity_get.Step()) { + sql::Statement activity_info_update( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "UPDATE activity_info SET " + "duration=?, score=?, percent=?, " + "weight=? WHERE " + "publisher_id=? AND month=? " + "AND year=? AND reconcile_stamp=?")); + + activity_info_update.BindInt64(0, static_cast(info.duration)); + activity_info_update.BindDouble(1, info.score); + activity_info_update.BindInt64(2, static_cast(info.percent)); + activity_info_update.BindDouble(3, info.weight); + activity_info_update.BindString(4, info.id); + activity_info_update.BindInt(5, info.month); + activity_info_update.BindInt(6, info.year); + activity_info_update.BindInt64(7, info.reconcile_stamp); + + return activity_info_update.Run(); + } + + sql::Statement activity_info_insert( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT INTO activity_info " + "(publisher_id, duration, score, percent, " + "weight, month, year, reconcile_stamp) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); + + activity_info_insert.BindString(0, info.id); + activity_info_insert.BindInt64(1, static_cast(info.duration)); + activity_info_insert.BindDouble(2, info.score); + activity_info_insert.BindInt64(3, static_cast(info.percent)); + activity_info_insert.BindDouble(4, info.weight); + activity_info_insert.BindInt(5, info.month); + activity_info_insert.BindInt(6, info.year); + activity_info_insert.BindInt64(7, info.reconcile_stamp); + + return activity_info_insert.Run(); } bool PublisherInfoDatabase::CreatePendingContributionsTable() { @@ -934,4 +971,4 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() { return sql::INIT_OK; } -} // namespace history +} // namespace brave_rewards diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 76ea55f790bd..4d07ed6268d6 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -36,25 +36,35 @@ class PublisherInfoDatabase { db_.set_error_callback(error_callback); } - bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); - bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id); bool InsertContributionInfo(const brave_rewards::ContributionInfo& info); - bool InsertOrUpdateRecurringDonation(const brave_rewards::RecurringDonation& info); - bool Find(int start, - int limit, - const ledger::PublisherInfoFilter& filter, - ledger::PublisherInfoList* list); - int Count(const ledger::PublisherInfoFilter& filter); + void GetTips(ledger::PublisherInfoList* list, + ledger::PUBLISHER_MONTH month, + int year); + + bool GetPublisherActivityList(int start, + int limit, + const ledger::PublisherInfoFilter& filter, + ledger::PublisherInfoList* list); + + bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key, + const std::string& publisher_id); std::unique_ptr GetMediaPublisherInfo( const std::string& media_key); + + bool InsertOrUpdateRecurringDonation( + const brave_rewards::RecurringDonation& info); + void GetRecurringDonations(ledger::PublisherInfoList* list); - void GetTips(ledger::PublisherInfoList* list, ledger::PUBLISHER_MONTH month, int year); + bool RemoveRecurring(const std::string& publisher_key); bool InsertPendingContribution(const ledger::PendingContributionList& list); double GetReservedAmount(); + bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); + + // Returns the current version of the publisher info database static int GetCurrentVersion(); @@ -66,32 +76,38 @@ class PublisherInfoDatabase { private: bool Init(); - void OnMemoryPressure( - base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); + bool CreateContributionInfoTable(); + + bool CreateContributionInfoIndex(); + bool CreatePublisherInfoTable(); - bool CreateMediaPublisherInfoTable(); + bool CreateActivityInfoTable(); - bool CreateContributionInfoIndex(); + bool CreateActivityInfoIndex(); + + bool CreateMediaPublisherInfoTable(); + bool CreateRecurringDonationTable(); + bool CreateRecurringDonationIndex(); bool CreatePendingContributionsTable(); bool CreatePendingContributionsIndex(); - std::string BuildClauses(int start, - int limit, - const ledger::PublisherInfoFilter& filter); - void BindFilter(sql::Statement& statement, - const ledger::PublisherInfoFilter& filter); + void OnMemoryPressure( + base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); sql::Database& GetDB(); + sql::MetaTable& GetMetaTable(); - sql::InitStatus EnsureCurrentVersion(); bool MigrateV1toV2(); + bool MigrateV2toV3(); + sql::InitStatus EnsureCurrentVersion(); + sql::Database db_; sql::MetaTable meta_table_; const base::FilePath db_path_; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 593ab15f1439..7b3ef5eefe36 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -198,7 +198,7 @@ bool SavePublisherInfoOnFileTaskRunner( return false; } -ledger::PublisherInfoList LoadPublisherInfoListOnFileTaskRunner( +ledger::PublisherInfoList GetPublisherActivityListOnFileTaskRunner( uint32_t start, uint32_t limit, ledger::PublisherInfoFilter filter, @@ -207,7 +207,7 @@ ledger::PublisherInfoList LoadPublisherInfoListOnFileTaskRunner( if (!backend) return list; - ignore_result(backend->Find(start, limit, filter, &list)); + ignore_result(backend->GetPublisherActivityList(start, limit, filter, &list)); return list; } @@ -929,7 +929,7 @@ void RewardsServiceImpl::LoadPublisherInfo( ledger::PublisherInfoFilter filter, ledger::PublisherInfoCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&LoadPublisherInfoListOnFileTaskRunner, + base::Bind(&GetPublisherActivityListOnFileTaskRunner, // set limit to 2 to make sure there is // only 1 valid result for the filter 0, 2, filter, publisher_info_backend_.get()), @@ -965,7 +965,7 @@ void RewardsServiceImpl::LoadPublisherInfoList( ledger::PublisherInfoFilter filter, ledger::PublisherInfoListCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&LoadPublisherInfoListOnFileTaskRunner, + base::Bind(&GetPublisherActivityListOnFileTaskRunner, start, limit, filter, publisher_info_backend_.get()), base::Bind(&RewardsServiceImpl::OnPublisherInfoListLoaded, From 92c8ae9a1946e941d023ef4b5fef692d718a967b Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 10:22:20 +0100 Subject: [PATCH 03/14] Adds publisher info insert; Optimizes activity table --- .../browser/publisher_info_database.cc | 171 +++++++++--------- .../browser/publisher_info_database.h | 6 +- .../browser/rewards_service_impl.cc | 2 +- 3 files changed, 89 insertions(+), 90 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index aee8ebefe95d..b7fb887efd90 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -229,6 +229,34 @@ bool PublisherInfoDatabase::CreatePublisherInfoTable() { return GetDB().Execute(sql.c_str()); } +bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( + const ledger::PublisherInfo& info) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) + return false; + + sql::Statement publisher_info_statement( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT OR REPLACE INTO publisher_info " + "(publisher_id, verified, excluded, " + "name, url, provider, favIcon) " + "VALUES (?, ?, ?, ?, ?, ?, ?)")); + + publisher_info_statement.BindString(0, info.id); + publisher_info_statement.BindBool(1, info.verified); + publisher_info_statement.BindInt(2, static_cast(info.excluded)); + publisher_info_statement.BindString(3, info.name); + publisher_info_statement.BindString(4, info.url); + publisher_info_statement.BindString(5, info.provider); + publisher_info_statement.BindString(6, info.favicon_url); + + return publisher_info_statement.Run(); +} + /** * * ACTIVITY INFO @@ -256,6 +284,8 @@ bool PublisherInfoDatabase::CreateActivityInfoTable() { "month INTEGER NOT NULL," "year INTEGER NOT NULL," "reconcile_stamp INTEGER DEFAULT 0 NOT NULL," + "CONSTRAINT activity_unique " + "UNIQUE (publisher_id, month, year, reconcile_stamp) " "CONSTRAINT fk_activity_info_publisher_id" " FOREIGN KEY (publisher_id)" " REFERENCES publisher_info (publisher_id)" @@ -272,6 +302,55 @@ bool PublisherInfoDatabase::CreateActivityInfoIndex() { "ON activity_info (publisher_id)"); } +bool PublisherInfoDatabase::InsertOrUpdateActivityInfo( + const ledger::PublisherInfo& info) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) + return false; + + // Insert publisher if it doesn't exist + sql::Statement publisher_info_statement( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT OR IGNORE INTO publisher_info " + "(publisher_id, verified, excluded, " + "name, url, provider, favIcon) " + "VALUES (?, ?, ?, ?, ?, ?, ?)")); + + publisher_info_statement.BindString(0, info.id); + publisher_info_statement.BindBool(1, info.verified); + publisher_info_statement.BindInt(2, static_cast(info.excluded)); + publisher_info_statement.BindString(3, info.name); + publisher_info_statement.BindString(4, info.url); + publisher_info_statement.BindString(5, info.provider); + publisher_info_statement.BindString(6, info.favicon_url); + + if (!publisher_info_statement.Run()) { + return false; + } + + sql::Statement activity_info_insert( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "INSERT OR REPLACE INTO activity_info " + "(publisher_id, duration, score, percent, " + "weight, month, year, reconcile_stamp) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); + + activity_info_insert.BindString(0, info.id); + activity_info_insert.BindInt64(1, static_cast(info.duration)); + activity_info_insert.BindDouble(2, info.score); + activity_info_insert.BindInt64(3, static_cast(info.percent)); + activity_info_insert.BindDouble(4, info.weight); + activity_info_insert.BindInt(5, info.month); + activity_info_insert.BindInt(6, info.year); + activity_info_insert.BindInt64(7, info.reconcile_stamp); + + return activity_info_insert.Run(); +} + bool PublisherInfoDatabase::GetPublisherActivityList( int start, int limit, @@ -617,92 +696,11 @@ bool PublisherInfoDatabase::RemoveRecurring(const std::string& publisher_key) { return statement.Run(); } -//// - - -bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( - const ledger::PublisherInfo& info) { - DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - - bool initialized = Init(); - DCHECK(initialized); - - if (!initialized) - return false; - - sql::Statement publisher_info_statement( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT OR REPLACE INTO publisher_info " - "(publisher_id, verified, excluded, " - "name, url, provider, favIcon) " - "VALUES (?, ?, ?, ?, ?, ?, ?)")); - - publisher_info_statement.BindString(0, info.id); - publisher_info_statement.BindBool(1, info.verified); - publisher_info_statement.BindInt(2, static_cast(info.excluded)); - publisher_info_statement.BindString(3, info.name); - publisher_info_statement.BindString(4, info.url); - publisher_info_statement.BindString(5, info.provider); - publisher_info_statement.BindString(6, info.favicon_url); - - if (!publisher_info_statement.Run()) { - return false; - } - - if (info.month == ledger::PUBLISHER_MONTH::ANY || info.year == -1) { - return true; - } - - sql::Statement activity_get( - db_.GetUniqueStatement("SELECT publisher_id FROM activity_info WHERE " - "publisher_id=? AND month=? " - "AND year=? AND reconcile_stamp=?")); - - activity_get.BindString(0, info.id); - activity_get.BindInt(1, info.month); - activity_get.BindInt(2, info.year); - activity_get.BindInt64(3, info.reconcile_stamp); - - if (activity_get.Step()) { - sql::Statement activity_info_update( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "UPDATE activity_info SET " - "duration=?, score=?, percent=?, " - "weight=? WHERE " - "publisher_id=? AND month=? " - "AND year=? AND reconcile_stamp=?")); - - activity_info_update.BindInt64(0, static_cast(info.duration)); - activity_info_update.BindDouble(1, info.score); - activity_info_update.BindInt64(2, static_cast(info.percent)); - activity_info_update.BindDouble(3, info.weight); - activity_info_update.BindString(4, info.id); - activity_info_update.BindInt(5, info.month); - activity_info_update.BindInt(6, info.year); - activity_info_update.BindInt64(7, info.reconcile_stamp); - - return activity_info_update.Run(); - } - - sql::Statement activity_info_insert( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT INTO activity_info " - "(publisher_id, duration, score, percent, " - "weight, month, year, reconcile_stamp) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); - - activity_info_insert.BindString(0, info.id); - activity_info_insert.BindInt64(1, static_cast(info.duration)); - activity_info_insert.BindDouble(2, info.score); - activity_info_insert.BindInt64(3, static_cast(info.percent)); - activity_info_insert.BindDouble(4, info.weight); - activity_info_insert.BindInt(5, info.month); - activity_info_insert.BindInt(6, info.year); - activity_info_insert.BindInt64(7, info.reconcile_stamp); - - return activity_info_insert.Run(); -} - +/** + * + * PENDING CONTRIBUTION + * + */ bool PublisherInfoDatabase::CreatePendingContributionsTable() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -793,7 +791,6 @@ double PublisherInfoDatabase::GetReservedAmount() { return amount; } - // static int PublisherInfoDatabase::GetCurrentVersion() { return kCurrentVersionNumber; diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 4d07ed6268d6..d2df596fe7a7 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -42,6 +42,10 @@ class PublisherInfoDatabase { ledger::PUBLISHER_MONTH month, int year); + bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); + + bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info); + bool GetPublisherActivityList(int start, int limit, const ledger::PublisherInfoFilter& filter, @@ -62,8 +66,6 @@ class PublisherInfoDatabase { bool InsertPendingContribution(const ledger::PendingContributionList& list); double GetReservedAmount(); - bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); - // Returns the current version of the publisher info database static int GetCurrentVersion(); diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 7b3ef5eefe36..884b165e8779 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -192,7 +192,7 @@ LoadMediaPublisherInfoListOnFileTaskRunner( bool SavePublisherInfoOnFileTaskRunner( const ledger::PublisherInfo publisher_info, PublisherInfoDatabase* backend) { - if (backend && backend->InsertOrUpdatePublisherInfo(publisher_info)) + if (backend && backend->InsertOrUpdateActivityInfo(publisher_info)) return true; return false; From a56251aac10f35981cd748ae4d968c8dd6b4244f Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 11:06:45 +0100 Subject: [PATCH 04/14] Adds getter for publisher info into the code --- .../browser/publisher_info_database.cc | 42 +++++++++++++++-- .../browser/publisher_info_database.h | 3 ++ .../browser/rewards_service_impl.cc | 47 +++++++++++++++---- .../browser/rewards_service_impl.h | 9 +++- 4 files changed, 87 insertions(+), 14 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index b7fb887efd90..c00db9fd05bf 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -257,6 +257,41 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( return publisher_info_statement.Run(); } +std::unique_ptr +PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return nullptr; + } + + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT publisher_id, name, url, favIcon, provider, verified, excluded " + "FROM publisher_info WHERE publisher_id=?")); + + info_sql.BindString(0, publisher_key); + + if (info_sql.Step()) { + std::unique_ptr info; + info.reset(new ledger::PublisherInfo()); + info->id = info_sql.ColumnString(0); + info->name = info_sql.ColumnString(1); + info->url = info_sql.ColumnString(2); + info->favicon_url = info_sql.ColumnString(3); + info->provider = info_sql.ColumnString(4); + info->verified = info_sql.ColumnBool(5); + info->excluded = static_cast( + info_sql.ColumnInt(6)); + + return info; + } + + return nullptr; +} + /** * * ACTIVITY INFO @@ -552,10 +587,8 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { bool initialized = Init(); DCHECK(initialized); - std::unique_ptr info; - if (!initialized) { - return info; + return nullptr; } sql::Statement info_sql(db_.GetUniqueStatement( @@ -568,6 +601,7 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { info_sql.BindString(0, media_key); if (info_sql.Step()) { + std::unique_ptr info; info.reset(new ledger::PublisherInfo()); info->id = info_sql.ColumnString(0); info->name = info_sql.ColumnString(1); @@ -579,7 +613,7 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { info_sql.ColumnInt(6)); } - return info; + return nullptr; } /** diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index d2df596fe7a7..4ff0d10c6c66 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -44,6 +44,9 @@ class PublisherInfoDatabase { bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); + std::unique_ptr GetPublisherInfo( + const std::string& media_key); + bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info); bool GetPublisherActivityList(int start, diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 884b165e8779..05dc22b349ca 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -178,15 +178,24 @@ bool SaveMediaPublisherInfoOnFileTaskRunner( } std::unique_ptr -LoadMediaPublisherInfoListOnFileTaskRunner( +LoadPublisherInfoOnFileTaskRunner( + const std::string publisher_key, + PublisherInfoDatabase* backend) { + if (!backend) + return nullptr; + + return backend->GetPublisherInfo(publisher_key); +} + +std::unique_ptr +LoadMediaPublisherInfoOnFileTaskRunner( const std::string media_key, PublisherInfoDatabase* backend) { std::unique_ptr info; if (!backend) return info; - info = backend->GetMediaPublisherInfo(media_key); - return info; + return backend->GetMediaPublisherInfo(media_key); } bool SavePublisherInfoOnFileTaskRunner( @@ -592,11 +601,33 @@ void RewardsServiceImpl::OnXHRLoad(SessionID tab_id, data.ToJson()); } +void RewardsServiceImpl::LoadPublisherInfo( + const std::string& publisher_key, + ledger::PublisherInfoCallback callback) { + base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, + base::Bind(&LoadPublisherInfoOnFileTaskRunner, + publisher_key, publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnPublisherInfoLoaded, + AsWeakPtr(), + callback)); +} + +void RewardsServiceImpl::OnPublisherInfoLoaded( + ledger::PublisherInfoCallback callback, + std::unique_ptr info) { + if (!info) { + callback(ledger::Result::NOT_FOUND, nullptr); + return; + } + + callback(ledger::Result::LEDGER_OK, std::move(info)); +} + void RewardsServiceImpl::LoadMediaPublisherInfo( const std::string& media_key, ledger::PublisherInfoCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&LoadMediaPublisherInfoListOnFileTaskRunner, + base::Bind(&LoadMediaPublisherInfoOnFileTaskRunner, media_key, publisher_info_backend_.get()), base::Bind(&RewardsServiceImpl::OnMediaPublisherInfoLoaded, AsWeakPtr(), @@ -610,7 +641,7 @@ void RewardsServiceImpl::OnMediaPublisherInfoLoaded( return; if (!info) { - callback(ledger::Result::NOT_FOUND, std::move(info)); + callback(ledger::Result::NOT_FOUND, nullptr); return; } @@ -925,7 +956,7 @@ void RewardsServiceImpl::OnPublisherInfoSaved( TriggerOnContentSiteUpdated(); } -void RewardsServiceImpl::LoadPublisherInfo( +void RewardsServiceImpl::LoadActivityInfo( ledger::PublisherInfoFilter filter, ledger::PublisherInfoCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, @@ -933,12 +964,12 @@ void RewardsServiceImpl::LoadPublisherInfo( // set limit to 2 to make sure there is // only 1 valid result for the filter 0, 2, filter, publisher_info_backend_.get()), - base::Bind(&RewardsServiceImpl::OnPublisherInfoLoaded, + base::Bind(&RewardsServiceImpl::OnActivityInfoLoaded, AsWeakPtr(), callback)); } -void RewardsServiceImpl::OnPublisherInfoLoaded( +void RewardsServiceImpl::OnActivityInfoLoaded( ledger::PublisherInfoCallback callback, const ledger::PublisherInfoList list) { if (!Connected()) { diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index b2aca5521e4d..4595420f2572 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -126,6 +126,9 @@ class RewardsServiceImpl : public RewardsService, const GetPublisherAllowNonVerifiedCallback& callback) override; void GetPublisherAllowVideos( const GetPublisherAllowVideosCallback& callback) override; + void LoadPublisherInfo( + const std::string& publisher_key, + ledger::PublisherInfoCallback callback) override; void LoadMediaPublisherInfo( const std::string& media_key, ledger::PublisherInfoCallback callback) override; @@ -195,9 +198,11 @@ class RewardsServiceImpl : public RewardsService, void OnPublisherInfoSaved(ledger::PublisherInfoCallback callback, std::unique_ptr info, bool success); - void OnPublisherInfoLoaded(ledger::PublisherInfoCallback callback, + void OnActivityInfoLoaded(ledger::PublisherInfoCallback callback, const ledger::PublisherInfoList list); void OnMediaPublisherInfoSaved(bool success); + void OnPublisherInfoLoaded(ledger::PublisherInfoCallback callback, + std::unique_ptr info); void OnMediaPublisherInfoLoaded(ledger::PublisherInfoCallback callback, std::unique_ptr info); void OnPublisherInfoListLoaded(uint32_t start, @@ -256,7 +261,7 @@ class RewardsServiceImpl : public RewardsService, void SavePublisherInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; - void LoadPublisherInfo(ledger::PublisherInfoFilter filter, + void LoadActivityInfo(ledger::PublisherInfoFilter filter, ledger::PublisherInfoCallback callback) override; void LoadPublisherInfoList( uint32_t start, From 35095aaa9ef62611013f3ed39cd54418958d5192 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 11:38:55 +0100 Subject: [PATCH 05/14] Adds publisher info save --- .../browser/rewards_service_impl.cc | 37 ++++++++++++++++++- .../browser/rewards_service_impl.h | 5 +++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 05dc22b349ca..f139978d00f1 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -201,6 +201,15 @@ LoadMediaPublisherInfoOnFileTaskRunner( bool SavePublisherInfoOnFileTaskRunner( const ledger::PublisherInfo publisher_info, PublisherInfoDatabase* backend) { + if (backend && backend->InsertOrUpdatePublisherInfo(publisher_info)) + return true; + + return false; +} + +bool SaveActivityInfoOnFileTaskRunner( + const ledger::PublisherInfo publisher_info, + PublisherInfoDatabase* backend) { if (backend && backend->InsertOrUpdateActivityInfo(publisher_info)) return true; @@ -925,8 +934,7 @@ void RewardsServiceImpl::LoadNicewareList( LOG(ERROR) << "Failed to read in niceware list"; } callback(data.empty() ? ledger::Result::LEDGER_ERROR - : ledger::Result::LEDGER_OK, - data); + : ledger::Result::LEDGER_OK, data); } void RewardsServiceImpl::SavePublisherInfo( @@ -956,6 +964,31 @@ void RewardsServiceImpl::OnPublisherInfoSaved( TriggerOnContentSiteUpdated(); } +void RewardsServiceImpl::SaveActivityInfo( + std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback) { + ledger::PublisherInfo info_copy = *publisher_info; + base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, + base::Bind(&SaveActivityInfoOnFileTaskRunner, + info_copy, + publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnActivityInfoSaved, + AsWeakPtr(), + callback, + base::Passed(std::move(publisher_info)))); + +} + +void RewardsServiceImpl::OnActivityInfoSaved( + ledger::PublisherInfoCallback callback, + std::unique_ptr info, + bool success) { + callback(success ? ledger::Result::LEDGER_OK + : ledger::Result::LEDGER_ERROR, std::move(info)); + + TriggerOnContentSiteUpdated(); +} + void RewardsServiceImpl::LoadActivityInfo( ledger::PublisherInfoFilter filter, ledger::PublisherInfoCallback callback) { diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 4595420f2572..ef4a8d5eb0d9 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -198,6 +198,9 @@ class RewardsServiceImpl : public RewardsService, void OnPublisherInfoSaved(ledger::PublisherInfoCallback callback, std::unique_ptr info, bool success); + void OnActivityInfoSaved(ledger::PublisherInfoCallback callback, + std::unique_ptr info, + bool success); void OnActivityInfoLoaded(ledger::PublisherInfoCallback callback, const ledger::PublisherInfoList list); void OnMediaPublisherInfoSaved(bool success); @@ -261,6 +264,8 @@ class RewardsServiceImpl : public RewardsService, void SavePublisherInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; + void SaveActivityInfo(std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback) override; void LoadActivityInfo(ledger::PublisherInfoFilter filter, ledger::PublisherInfoCallback callback) override; void LoadPublisherInfoList( From abc237e4c069fcea54f490b2a25a111136714159 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 12:15:33 +0100 Subject: [PATCH 06/14] Removes category from activity; Renames filters to activity --- .../browser/publisher_info_database.cc | 32 +++++++++---------- .../browser/publisher_info_database.h | 4 +-- .../brave_rewards/browser/rewards_service.h | 4 +-- .../browser/rewards_service_impl.cc | 32 +++++++++---------- .../browser/rewards_service_impl.h | 14 ++++---- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index c00db9fd05bf..ab62ba8c9c3f 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -158,7 +158,7 @@ bool PublisherInfoDatabase::InsertContributionInfo( } void PublisherInfoDatabase::GetTips(ledger::PublisherInfoList* list, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -179,13 +179,13 @@ void PublisherInfoDatabase::GetTips(ledger::PublisherInfoList* list, info_sql.BindInt(0, month); info_sql.BindInt(1, year); - info_sql.BindInt(2, ledger::PUBLISHER_CATEGORY::DIRECT_DONATION); - info_sql.BindInt(3, ledger::PUBLISHER_CATEGORY::TIPPING); + info_sql.BindInt(2, ledger::REWARDS_CATEGORY::DIRECT_DONATION); + info_sql.BindInt(3, ledger::REWARDS_CATEGORY::TIPPING); while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); - ledger::PublisherInfo publisher(id, ledger::PUBLISHER_MONTH::ANY, -1); + ledger::PublisherInfo publisher(id, ledger::ACTIVITY_MONTH::ANY, -1); publisher.name = info_sql.ColumnString(1); publisher.url = info_sql.ColumnString(2); @@ -389,7 +389,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo( bool PublisherInfoDatabase::GetPublisherActivityList( int start, int limit, - const ledger::PublisherInfoFilter& filter, + const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoList* list) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -415,7 +415,7 @@ bool PublisherInfoDatabase::GetPublisherActivityList( query += " AND ai.publisher_id = ?"; } - if (filter.month != ledger::PUBLISHER_MONTH::ANY) { + if (filter.month != ledger::ACTIVITY_MONTH::ANY) { query += " AND ai.month = ?"; } @@ -431,14 +431,14 @@ bool PublisherInfoDatabase::GetPublisherActivityList( query += " AND ai.duration >= ?"; } - if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && + if (filter.excluded != ledger::EXCLUDE_FILTER::FILTER_ALL && filter.excluded != - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { query += " AND pi.excluded = ?"; } if (filter.excluded == - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { query += " AND pi.excluded != ?"; } @@ -470,7 +470,7 @@ bool PublisherInfoDatabase::GetPublisherActivityList( info_sql.BindString(column++, filter.id); } - if (filter.month != ledger::PUBLISHER_MONTH::ANY) { + if (filter.month != ledger::ACTIVITY_MONTH::ANY) { info_sql.BindInt(column++, filter.month); } @@ -486,14 +486,14 @@ bool PublisherInfoDatabase::GetPublisherActivityList( info_sql.BindInt(column++, filter.min_duration); } - if (filter.excluded != ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL && + if (filter.excluded != ledger::EXCLUDE_FILTER::FILTER_ALL && filter.excluded != - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { info_sql.BindInt(column++, filter.excluded); } if (filter.excluded == - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED) { info_sql.BindInt(column++, ledger::PUBLISHER_EXCLUDE::EXCLUDED); } @@ -503,8 +503,8 @@ bool PublisherInfoDatabase::GetPublisherActivityList( while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); - ledger::PUBLISHER_MONTH month( - static_cast(info_sql.ColumnInt(7))); + ledger::ACTIVITY_MONTH month( + static_cast(info_sql.ColumnInt(7))); int year(info_sql.ColumnInt(8)); ledger::PublisherInfo info(id, month, year); @@ -697,7 +697,7 @@ void PublisherInfoDatabase::GetRecurringDonations( while (info_sql.Step()) { std::string id(info_sql.ColumnString(0)); - ledger::PublisherInfo publisher(id, ledger::PUBLISHER_MONTH::ANY, -1); + ledger::PublisherInfo publisher(id, ledger::ACTIVITY_MONTH::ANY, -1); publisher.name = info_sql.ColumnString(1); publisher.url = info_sql.ColumnString(2); diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 4ff0d10c6c66..fb5a9ad9eef6 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -39,7 +39,7 @@ class PublisherInfoDatabase { bool InsertContributionInfo(const brave_rewards::ContributionInfo& info); void GetTips(ledger::PublisherInfoList* list, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year); bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); @@ -51,7 +51,7 @@ class PublisherInfoDatabase { bool GetPublisherActivityList(int start, int limit, - const ledger::PublisherInfoFilter& filter, + const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoList* list); bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key, diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 8b8cc520e6bb..732a7da42f90 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -38,7 +38,7 @@ bool IsMediaLink(const GURL& url, class RewardsNotificationService; class RewardsServiceObserver; -using GetCurrentContributeListCallback = +using GetContentSiteListCallback = base::Callback, uint32_t /* next_record */)>; using GetAllBalanceReportsCallback = base::Callback site_list(new ContentSiteList); @@ -421,18 +421,18 @@ void RewardsServiceImpl::CreateWallet() { } } -void RewardsServiceImpl::GetCurrentContributeList( +void RewardsServiceImpl::GetContentSiteList( uint32_t start, uint32_t limit, uint64_t min_visit_time, uint64_t reconcile_stamp, bool allow_non_verified, - const GetCurrentContributeListCallback& callback) { + const GetContentSiteListCallback& callback) { if (!Connected()) { return; } - ledger::PublisherInfoFilter filter; + ledger::ActivityInfoFilter filter; filter.category = ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE; filter.month = ledger::PUBLISHER_MONTH::ANY; filter.year = -1; @@ -440,7 +440,7 @@ void RewardsServiceImpl::GetCurrentContributeList( filter.order_by.push_back(std::pair("ai.percent", false)); filter.reconcile_stamp = reconcile_stamp; filter.excluded = - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED; + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED; filter.percent = 1; filter.non_verified = allow_non_verified; @@ -454,7 +454,7 @@ void RewardsServiceImpl::GetCurrentContributeList( void RewardsServiceImpl::OnGetPublisherInfoList( uint32_t start, uint32_t limit, - const GetCurrentContributeListCallback& callback, + const GetContentSiteListCallback& callback, const std::vector& publisher_info_list, uint32_t next_record) { ledger::PublisherInfoList list; @@ -802,7 +802,7 @@ void RewardsServiceImpl::OnGrantFinish(ledger::Result result, void RewardsServiceImpl::OnReconcileComplete(ledger::Result result, const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi) { if (result == ledger::Result::LEDGER_OK) { auto now = base::Time::Now(); @@ -990,7 +990,7 @@ void RewardsServiceImpl::OnActivityInfoSaved( } void RewardsServiceImpl::LoadActivityInfo( - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, base::Bind(&GetPublisherActivityListOnFileTaskRunner, @@ -1026,7 +1026,7 @@ void RewardsServiceImpl::OnActivityInfoLoaded( void RewardsServiceImpl::LoadPublisherInfoList( uint32_t start, uint32_t limit, - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, base::Bind(&GetPublisherActivityListOnFileTaskRunner, @@ -1810,7 +1810,7 @@ void RewardsServiceImpl::OnDonate(const std::string& publisher_key, int amount, ledger::PublisherInfo publisher( publisher_key, - ledger::PUBLISHER_MONTH::ANY, + ledger::ACTIVITY_MONTH::ANY, -1); bat_ledger_->DoDirectDonation(publisher.ToJson(), amount, "BAT"); @@ -1824,8 +1824,8 @@ bool SaveContributionInfoOnFileTaskRunner(const brave_rewards::ContributionInfo return false; } -void RewardsServiceImpl::OnContributionInfoSaved(const ledger::PUBLISHER_CATEGORY category, bool success) { - if (success && category == ledger::PUBLISHER_CATEGORY::DIRECT_DONATION) { +void RewardsServiceImpl::OnContributionInfoSaved(const ledger::REWARDS_CATEGORY category, bool success) { + if (success && category == ledger::REWARDS_CATEGORY::DIRECT_DONATION) { TipsUpdated(); } } @@ -1835,7 +1835,7 @@ void RewardsServiceImpl::SaveContributionInfo(const std::string& probi, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) { + const ledger::REWARDS_CATEGORY category) { brave_rewards::ContributionInfo info; info.probi = probi; diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index ef4a8d5eb0d9..d07b6707762d 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -95,7 +95,7 @@ class RewardsServiceImpl : public RewardsService, uint64_t min_visit_time, uint64_t reconcile_stamp, bool allow_non_verified, - const GetCurrentContributeListCallback& callback) override; + const GetContentSiteListCallback& callback) override; void OnLoad(SessionID tab_id, const GURL& url) override; void OnUnload(SessionID tab_id) override; void OnShow(SessionID tab_id) override; @@ -223,7 +223,7 @@ class RewardsServiceImpl : public RewardsService, std::unique_ptr info); void OnDonate(const std::string& publisher_key, int amount, bool recurring, const ledger::PublisherInfo* publisher_info = NULL) override; - void OnContributionInfoSaved(const ledger::PUBLISHER_CATEGORY category, bool success); + void OnContributionInfoSaved(const ledger::REWARDS_CATEGORY category, bool success); void OnRecurringDonationSaved(bool success); void SaveRecurringDonation(const std::string& publisher_key, const int amount); void OnRecurringDonationsData(const ledger::PublisherInfoListCallback callback, @@ -251,7 +251,7 @@ class RewardsServiceImpl : public RewardsService, const std::vector& grants) override; void OnReconcileComplete(ledger::Result result, const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi) override; void OnGrantFinish(ledger::Result result, const ledger::Grant& grant) override; @@ -266,12 +266,12 @@ class RewardsServiceImpl : public RewardsService, ledger::PublisherInfoCallback callback) override; void SaveActivityInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; - void LoadActivityInfo(ledger::PublisherInfoFilter filter, + void LoadActivityInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; void LoadPublisherInfoList( uint32_t start, uint32_t limit, - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) override; void SavePublishersList(const std::string& publishers_list, ledger::LedgerCallbackHandler* handler) override; @@ -313,7 +313,7 @@ class RewardsServiceImpl : public RewardsService, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) override; + const ledger::REWARDS_CATEGORY category) override; void GetRecurringDonations(ledger::PublisherInfoListCallback callback) override; std::unique_ptr Log( const char* file, @@ -339,7 +339,7 @@ class RewardsServiceImpl : public RewardsService, // Mojo Proxy methods void OnPublisherBannerMojoProxy(const std::string& banner); void OnGetPublisherInfoList(uint32_t start, uint32_t limit, - const GetCurrentContributeListCallback& callback, + const GetContentSiteListCallback& callback, const std::vector& publisher_info_list, uint32_t next_record); void OnGetAllBalanceReports( From 2ff4bfba59d0e81ba395dbcce3b2c184e6afb2dd Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 12:32:25 +0100 Subject: [PATCH 07/14] Renames GetPublisherInfoList into GetActivityInfoList --- .../browser/publisher_info_database.cc | 2 +- .../browser/publisher_info_database.h | 2 +- .../brave_rewards/browser/rewards_service_impl.cc | 15 ++++++++++----- .../brave_rewards/browser/rewards_service_impl.h | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index ab62ba8c9c3f..98e66d0e5127 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -386,7 +386,7 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo( return activity_info_insert.Run(); } -bool PublisherInfoDatabase::GetPublisherActivityList( +bool PublisherInfoDatabase::GetActivityList( int start, int limit, const ledger::ActivityInfoFilter& filter, diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index fb5a9ad9eef6..de7ca71d732e 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -49,7 +49,7 @@ class PublisherInfoDatabase { bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info); - bool GetPublisherActivityList(int start, + bool GetActivityList(int start, int limit, const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoList* list); diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index c26691f9aace..ed6ebc088983 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -216,7 +216,7 @@ bool SaveActivityInfoOnFileTaskRunner( return false; } -ledger::PublisherInfoList GetPublisherActivityListOnFileTaskRunner( +ledger::PublisherInfoList GetActivityListOnFileTaskRunner( uint32_t start, uint32_t limit, ledger::ActivityInfoFilter filter, @@ -225,7 +225,7 @@ ledger::PublisherInfoList GetPublisherActivityListOnFileTaskRunner( if (!backend) return list; - ignore_result(backend->GetPublisherActivityList(start, limit, filter, &list)); + ignore_result(backend->GetActivityList(start, limit, filter, &list)); return list; } @@ -444,7 +444,7 @@ void RewardsServiceImpl::GetContentSiteList( filter.percent = 1; filter.non_verified = allow_non_verified; - bat_ledger_->GetPublisherInfoList(start, limit, + bat_ledger_->GetActivityInfoList(start, limit, filter.ToJson(), base::BindOnce(&RewardsServiceImpl::OnGetPublisherInfoList, AsWeakPtr(), start, @@ -993,7 +993,7 @@ void RewardsServiceImpl::LoadActivityInfo( ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&GetPublisherActivityListOnFileTaskRunner, + base::Bind(&GetActivityListOnFileTaskRunner, // set limit to 2 to make sure there is // only 1 valid result for the filter 0, 2, filter, publisher_info_backend_.get()), @@ -1023,13 +1023,18 @@ void RewardsServiceImpl::OnActivityInfoLoaded( std::make_unique(list[0])); } +void RewardsServiceImpl::LoadActivityInfoList( + uint32_t start, + uint32_t limit, + ledger::ActivityInfoFilter filter, + ledger::PublisherInfoListCallback callback) { void RewardsServiceImpl::LoadPublisherInfoList( uint32_t start, uint32_t limit, ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, - base::Bind(&GetPublisherActivityListOnFileTaskRunner, + base::Bind(&GetActivityListOnFileTaskRunner, start, limit, filter, publisher_info_backend_.get()), base::Bind(&RewardsServiceImpl::OnPublisherInfoListLoaded, diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index d07b6707762d..b5c181912463 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -268,7 +268,7 @@ class RewardsServiceImpl : public RewardsService, ledger::PublisherInfoCallback callback) override; void LoadActivityInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; - void LoadPublisherInfoList( + void LoadActivityInfoList( uint32_t start, uint32_t limit, ledger::ActivityInfoFilter filter, From 9a74576a5d5793a74975f1a79da290079510faef Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 20 Nov 2018 20:03:59 +0100 Subject: [PATCH 08/14] Refactors restore flow --- .../browser/publisher_info_database.cc | 21 ++++++++++++++ .../browser/publisher_info_database.h | 4 ++- .../browser/rewards_service_impl.cc | 29 +++++++++++++++++++ .../browser/rewards_service_impl.h | 3 ++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 98e66d0e5127..521d45b8f5d6 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -292,6 +292,27 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { return nullptr; } +bool PublisherInfoDatabase::RestorePublishers() { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return false; + } + + sql::Statement restore_q(db_.GetUniqueStatement( + "UPDATE publisher_info SET excluded=? WHERE excluded=?")); + + restore_q.BindInt(0, static_cast( + ledger::PUBLISHER_EXCLUDE::DEFAULT)); + restore_q.BindInt(1, static_cast( + ledger::PUBLISHER_EXCLUDE::EXCLUDED)); + + return restore_q.Run(); +} + /** * * ACTIVITY INFO diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index de7ca71d732e..8259e7bfb62b 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -44,9 +44,11 @@ class PublisherInfoDatabase { bool InsertOrUpdatePublisherInfo(const ledger::PublisherInfo& info); - std::unique_ptr GetPublisherInfo( + std::unique_ptr GetPublisherInfo( const std::string& media_key); + bool RestorePublishers(); + bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info); bool GetActivityList(int start, diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index ed6ebc088983..99f668fb3f8f 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2307,4 +2307,33 @@ void RewardsServiceImpl::GetPendingContributionsTotal( callback); } +bool RestorePublisherOnFileTaskRunner(PublisherInfoDatabase* backend) { + if (!backend) { + return false; + } + + return backend->RestorePublishers(); +} + +void RewardsServiceImpl::OnRestorePublishers(ledger::OnRestoreCallback callback) { + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::Bind(&RestorePublisherOnFileTaskRunner, + publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnRestorePublishersInternal, + AsWeakPtr(), + callback)); +} + +void RewardsServiceImpl::OnRestorePublishersInternal( + ledger::OnRestoreCallback callback, + bool result) { + callback(result); + + if (result) { + TriggerOnContentSiteUpdated(); + } +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index b5c181912463..2c8452b69024 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -240,6 +240,8 @@ class RewardsServiceImpl : public RewardsService, uint64_t windowId); void MaybeShowBackupNotification(uint64_t boot_stamp); void MaybeShowAddFundsNotification(uint64_t reconcile_stamp); + void OnRestorePublishersInternal(ledger::OnRestoreCallback callback, + bool result); // ledger::LedgerClient std::string GenerateGUID() const override; @@ -319,6 +321,7 @@ class RewardsServiceImpl : public RewardsService, const char* file, int line, const ledger::LogLevel log_level) const override; + void OnRestorePublishers(ledger::OnRestoreCallback callback) override; void SavePendingContribution( const ledger::PendingContributionList& list) override; From d815fd855c6d58067fe731bb36759b5a7b4c756a Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 22 Nov 2018 09:03:39 +0100 Subject: [PATCH 09/14] Adds custom loader for the panel --- .../browser/publisher_info_database.cc | 49 ++++++++++++++++++- .../browser/publisher_info_database.h | 9 ++-- .../browser/rewards_service_impl.cc | 37 +++++++++++++- .../browser/rewards_service_impl.h | 5 ++ .../reducers/rewards_panel_reducer.ts | 21 ++++---- .../brave_rewards/components/panel.tsx | 3 +- components/definitions/rewardsExtensions.d.ts | 17 ++++--- 7 files changed, 116 insertions(+), 25 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 521d45b8f5d6..d2a348322026 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -292,6 +292,50 @@ PublisherInfoDatabase::GetPublisherInfo(const std::string& publisher_key) { return nullptr; } +std::unique_ptr +PublisherInfoDatabase::GetPanelPublisher( + const ledger::ActivityInfoFilter& filter) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized) { + return nullptr; + } + + sql::Statement info_sql(db_.GetUniqueStatement( + "SELECT pi.publisher_id, pi.name, pi.url, pi.favIcon, pi.provider, " + "pi.verified, pi.excluded, ai.percent FROM publisher_info AS pi " + "LEFT JOIN activity_info AS ai ON pi.publisher_id = ai.publisher_id " + "WHERE pi.publisher_id=? AND ((ai.month = ? " + "AND ai.year = ? AND ai.reconcile_stamp = ?) OR " + "ai.percent IS NULL) LIMIT 1")); + + info_sql.BindString(0, filter.id); + info_sql.BindInt(1, filter.month); + info_sql.BindInt(2, filter.year); + info_sql.BindInt64(3, filter.reconcile_stamp); + + if (info_sql.Step()) { + std::unique_ptr info; + info.reset(new ledger::PublisherInfo()); + info->id = info_sql.ColumnString(0); + info->name = info_sql.ColumnString(1); + info->url = info_sql.ColumnString(2); + info->favicon_url = info_sql.ColumnString(3); + info->provider = info_sql.ColumnString(4); + info->verified = info_sql.ColumnBool(5); + info->excluded = static_cast( + info_sql.ColumnInt(6)); + info->percent = info_sql.ColumnInt(7); + + return info; + } + + return nullptr; +} + bool PublisherInfoDatabase::RestorePublishers() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); @@ -622,8 +666,7 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { info_sql.BindString(0, media_key); if (info_sql.Step()) { - std::unique_ptr info; - info.reset(new ledger::PublisherInfo()); + std::unique_ptr info(new ledger::PublisherInfo()); info->id = info_sql.ColumnString(0); info->name = info_sql.ColumnString(1); info->url = info_sql.ColumnString(2); @@ -632,6 +675,8 @@ PublisherInfoDatabase::GetMediaPublisherInfo(const std::string& media_key) { info->verified = info_sql.ColumnBool(5); info->excluded = static_cast( info_sql.ColumnInt(6)); + + return info; } return nullptr; diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 8259e7bfb62b..06e0d9120ece 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -47,14 +47,17 @@ class PublisherInfoDatabase { std::unique_ptr GetPublisherInfo( const std::string& media_key); + std::unique_ptr GetPanelPublisher( + const ledger::ActivityInfoFilter& filter); + bool RestorePublishers(); bool InsertOrUpdateActivityInfo(const ledger::PublisherInfo& info); bool GetActivityList(int start, - int limit, - const ledger::ActivityInfoFilter& filter, - ledger::PublisherInfoList* list); + int limit, + const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoList* list); bool InsertOrUpdateMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id); diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 99f668fb3f8f..35393f599147 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -229,6 +229,17 @@ ledger::PublisherInfoList GetActivityListOnFileTaskRunner( return list; } +std::unique_ptr GetPanelPublisherInfoOnFileTaskRunner( + ledger::ActivityInfoFilter filter, + PublisherInfoDatabase* backend) { + ledger::PublisherInfoList list; + if (!backend) { + return nullptr; + } + + return backend->GetPanelPublisher(filter); +} + // `callback` has a WeakPtr so this won't crash if the file finishes // writing after RewardsServiceImpl has been destroyed void PostWriteCallback( @@ -1023,6 +1034,30 @@ void RewardsServiceImpl::OnActivityInfoLoaded( std::make_unique(list[0])); } +void RewardsServiceImpl::LoadPanelPublisherInfo( + ledger::ActivityInfoFilter filter, + ledger::PublisherInfoCallback callback) { + base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, + base::Bind(&GetPanelPublisherInfoOnFileTaskRunner, + filter, + publisher_info_backend_.get()), + base::Bind(&RewardsServiceImpl::OnPanelPublisherInfoLoaded, + AsWeakPtr(), + callback)); +} + +void RewardsServiceImpl::OnPanelPublisherInfoLoaded( + ledger::PublisherInfoCallback callback, + std::unique_ptr publisher_info) { + if (!publisher_info) { + callback(ledger::Result::NOT_FOUND, + std::unique_ptr()); + return; + } + + callback(ledger::Result::LEDGER_OK, std::move(publisher_info)); +} + void RewardsServiceImpl::LoadActivityInfoList( uint32_t start, uint32_t limit, @@ -2206,7 +2241,7 @@ void RewardsServiceImpl::OnDonate( ledger::PublisherInfo info; info.id = publisher_key; - info.month = ledger::PUBLISHER_MONTH::ANY; + info.month = ledger::ACTIVITY_MONTH::ANY; info.year = -1; info.verified = site->verified; info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 2c8452b69024..0fd0a93d1205 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -270,6 +270,8 @@ class RewardsServiceImpl : public RewardsService, ledger::PublisherInfoCallback callback) override; void LoadActivityInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; + void LoadPanelPublisherInfo(ledger::ActivityInfoFilter filter, + ledger::PublisherInfoCallback callback) override; void LoadActivityInfoList( uint32_t start, uint32_t limit, @@ -322,6 +324,9 @@ class RewardsServiceImpl : public RewardsService, int line, const ledger::LogLevel log_level) const override; void OnRestorePublishers(ledger::OnRestoreCallback callback) override; + void OnPanelPublisherInfoLoaded( + ledger::PublisherInfoCallback callback, + std::unique_ptr publisher_info); void SavePendingContribution( const ledger::PendingContributionList& list) override; diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts b/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts index c1dd1e9eb912..44687bbcd2c6 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts @@ -25,8 +25,6 @@ const getWindowId = (id: number) => { return `id_${id}` } -let currentPublishers: string[] = [] - export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, action: any) => { if (state === undefined) { state = storage.load() @@ -73,16 +71,19 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a break } - chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '') const id = getWindowId(tab.windowId) - let publishers: Record = state.publishers + const publishers: Record = state.publishers + const publisher = publishers[id] + chrome.braveRewards.getPublisherData(tab.windowId, tab.url, tab.favIconUrl || '') + if (!publisher || (publisher && publisher.tabUrl !== tab.url)) { + if (publisher) { + delete publishers[id] + } - if (publishers[id] && currentPublishers[id] !== tab.url) { - delete publishers[id] + publishers[id] = { + tabUrl: tab.url + } } - - currentPublishers[id] = tab.url - state = { ...state, publishers @@ -98,7 +99,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a if (publisher && !publisher.publisher_key) { delete publishers[id] } else { - publishers[id] = publisher + publishers[id] = { ...publishers[id], ...publisher } } state = { diff --git a/components/brave_rewards/resources/extension/brave_rewards/components/panel.tsx b/components/brave_rewards/resources/extension/brave_rewards/components/panel.tsx index 7c5bd697c5d1..cc6fa1724815 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/components/panel.tsx +++ b/components/brave_rewards/resources/extension/brave_rewards/components/panel.tsx @@ -188,9 +188,10 @@ export class Panel extends React.Component { return } const tabId = tabs[0].id - if (tabId === undefined || !publisher) { + if (tabId === undefined || !publisher || !publisher.publisher_key) { return } + chrome.braveRewards.donateToSite(tabId, publisher.publisher_key) window.close() }) diff --git a/components/definitions/rewardsExtensions.d.ts b/components/definitions/rewardsExtensions.d.ts index ceeeca73a0ee..1cd27753a782 100644 --- a/components/definitions/rewardsExtensions.d.ts +++ b/components/definitions/rewardsExtensions.d.ts @@ -22,14 +22,15 @@ declare namespace RewardsExtension { } interface Publisher { - excluded: boolean - favicon_url: string - publisher_key: string - name: string - percentage: number - provider: string - url: string - verified: boolean + excluded?: boolean + favicon_url?: string + publisher_key?: string + name?: string + percentage?: number + provider?: string + tabUrl?: string + url?: string + verified?: boolean } export interface Grant { From e78dbe2431cd9ca12ba307414af3271aab5989ea Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 5 Dec 2018 12:40:17 +0100 Subject: [PATCH 10/14] Fixes merge problem --- .../browser/rewards_service_impl.cc | 5 ----- .../ui/reducers/publishers_reducer.ts | 20 ++++++++++--------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 35393f599147..d18d2b320f13 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -1063,11 +1063,6 @@ void RewardsServiceImpl::LoadActivityInfoList( uint32_t limit, ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) { -void RewardsServiceImpl::LoadPublisherInfoList( - uint32_t start, - uint32_t limit, - ledger::ActivityInfoFilter filter, - ledger::PublisherInfoListCallback callback) { base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE, base::Bind(&GetActivityListOnFileTaskRunner, start, limit, filter, diff --git a/components/brave_rewards/resources/ui/reducers/publishers_reducer.ts b/components/brave_rewards/resources/ui/reducers/publishers_reducer.ts index 5d264611d65d..d62e4c9b1e56 100644 --- a/components/brave_rewards/resources/ui/reducers/publishers_reducer.ts +++ b/components/brave_rewards/resources/ui/reducers/publishers_reducer.ts @@ -34,15 +34,17 @@ const publishersReducer: Reducer = (state: Rewards.St state.numExcludedSites = parseInt(newNum, 10) - if (previousNum < newNum) { - // On a new excluded publisher, add to excluded state - if (!state.excluded.includes(publisherKey)) { - state.excluded.push(publisherKey) - } - } else { - // Remove the publisher from excluded if it has been re-included - if (state.excluded.includes(publisherKey)) { - state.excluded = state.excluded.filter((key: string) => key !== publisherKey) + if (publisherKey.length > 0) { + if (previousNum < newNum) { + // On a new excluded publisher, add to excluded state + if (!state.excluded.includes(publisherKey)) { + state.excluded.push(publisherKey) + } + } else { + // Remove the publisher from excluded if it has been re-included + if (state.excluded.includes(publisherKey)) { + state.excluded = state.excluded.filter((key: string) => key !== publisherKey) + } } } From e271498b35cfaf524b5c300e49f741c1064e25ff Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Mon, 17 Dec 2018 09:33:18 +0100 Subject: [PATCH 11/14] Ports ledger cahnges from seperate repo into core Original implementation: https://github.com/brave-intl/bat-native-ledger/pull/183 --- .../include/bat/ledger/ledger.h | 34 ++- .../include/bat/ledger/ledger_client.h | 19 +- .../include/bat/ledger/publisher_info.h | 24 +- .../src/bat/ledger/ledger.cc | 40 ++- .../bat-native-ledger/src/bat_contribution.cc | 53 ++-- .../bat-native-ledger/src/bat_contribution.h | 8 +- vendor/bat-native-ledger/src/bat_get_media.cc | 17 +- .../bat-native-ledger/src/bat_publishers.cc | 266 +++++++++--------- vendor/bat-native-ledger/src/bat_publishers.h | 36 ++- vendor/bat-native-ledger/src/ledger_impl.cc | 97 ++++--- vendor/bat-native-ledger/src/ledger_impl.h | 43 +-- 11 files changed, 348 insertions(+), 289 deletions(-) diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 21abe661e01e..578ee2f4cf7d 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -28,7 +28,7 @@ LEDGER_EXPORT struct VisitData { const std::string& _domain, const std::string& _path, uint32_t _tab_id, - PUBLISHER_MONTH _local_month, + ACTIVITY_MONTH _local_month, int _local_year, const std::string& name, const std::string& url, @@ -44,7 +44,7 @@ LEDGER_EXPORT struct VisitData { std::string domain; std::string path; uint32_t tab_id; - PUBLISHER_MONTH local_month; + ACTIVITY_MONTH local_month; int local_year; std::string name; std::string url; @@ -57,8 +57,8 @@ LEDGER_EXPORT struct PaymentData { PaymentData(const std::string& _publisher_id, const double& _value, const int64_t& _timestamp, - PUBLISHER_CATEGORY _category, - PUBLISHER_MONTH _local_month, + REWARDS_CATEGORY _category, + ACTIVITY_MONTH _local_month, int _local_year); PaymentData(const PaymentData& data); ~PaymentData(); @@ -66,8 +66,8 @@ LEDGER_EXPORT struct PaymentData { std::string publisher_id; double value; int64_t timestamp; - PUBLISHER_CATEGORY category; - PUBLISHER_MONTH local_month; + REWARDS_CATEGORY category; + ACTIVITY_MONTH local_month; int local_year; }; @@ -122,15 +122,19 @@ class LEDGER_EXPORT Ledger { virtual void SetPublisherInfo(std::unique_ptr publisher_info, PublisherInfoCallback callback) = 0; - virtual void GetPublisherInfo(const ledger::PublisherInfoFilter& filter, + virtual void SetActivityInfo(std::unique_ptr publisher_info, + PublisherInfoCallback callback) = 0; + virtual void GetPublisherInfo(const std::string& publisher_key, + PublisherInfoCallback callback) = 0; + virtual void GetActivityInfo(const ledger::ActivityInfoFilter& filter, PublisherInfoCallback callback) = 0; virtual void SetMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id) = 0; virtual void GetMediaPublisherInfo(const std::string& media_key, PublisherInfoCallback callback) = 0; virtual std::vector GetRecurringDonationPublisherInfo() = 0; - virtual void GetPublisherInfoList(uint32_t start, uint32_t limit, - const ledger::PublisherInfoFilter& filter, + virtual void GetActivityInfoList(uint32_t start, uint32_t limit, + const ledger::ActivityInfoFilter& filter, PublisherInfoListCallback callback) = 0; virtual void SetRewardsMainEnabled(bool enabled) = 0; @@ -141,7 +145,7 @@ class LEDGER_EXPORT Ledger { virtual void SetContributionAmount(double amount) = 0; virtual void SetUserChangedContribution() = 0; virtual void SetAutoContribute(bool enabled) = 0; - virtual void SetBalanceReport(PUBLISHER_MONTH month, + virtual void SetBalanceReport(ACTIVITY_MONTH month, int year, const ledger::BalanceReportInfo& report_info) = 0; @@ -164,7 +168,7 @@ class LEDGER_EXPORT Ledger { virtual void SolveGrantCaptcha(const std::string& solution) const = 0; virtual void GetGrantCaptcha() const = 0; virtual std::string GetWalletPassphrase() const = 0; - virtual bool GetBalanceReport(PUBLISHER_MONTH month, + virtual bool GetBalanceReport(ACTIVITY_MONTH month, int year, ledger::BalanceReportInfo* report_info) const = 0; virtual std::map GetAllBalanceReports() const = 0; @@ -181,16 +185,16 @@ class LEDGER_EXPORT Ledger { virtual void RestorePublishers() = 0; virtual bool IsWalletCreated() const = 0; virtual void GetPublisherActivityFromUrl(uint64_t windowId, const ledger::VisitData& visit_data) = 0; - virtual void SetBalanceReportItem(PUBLISHER_MONTH month, + virtual void SetBalanceReportItem(ACTIVITY_MONTH month, int year, ledger::ReportType type, const std::string& probi) = 0; virtual void GetPublisherBanner(const std::string& publisher_id, ledger::PublisherBannerCallback callback) = 0; virtual double GetBalance() = 0; virtual void OnReconcileCompleteSuccess(const std::string& viewing_id, - const ledger::PUBLISHER_CATEGORY category, + const ledger::REWARDS_CATEGORY category, const std::string& probi, - const ledger::PUBLISHER_MONTH month, + const ledger::ACTIVITY_MONTH month, const int year, const uint32_t date) = 0; virtual void RemoveRecurring(const std::string& publisher_key) = 0; @@ -202,4 +206,4 @@ class LEDGER_EXPORT Ledger { } // namespace ledger -#endif // BAT_LEDGER_LEDGER_H_ +#endif // BAT_LEDGER_LEDGER_H_ \ No newline at end of file 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 e132b21978bf..aa104e4976f9 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -56,6 +56,7 @@ using RecurringRemoveCallback = std::function; using FetchIconCallback = std::function; using LoadURLCallback = std::function& headers)>; +using OnRestoreCallback = std::function; class LEDGER_EXPORT LedgerClient { public: @@ -69,7 +70,7 @@ class LEDGER_EXPORT LedgerClient { std::unique_ptr) = 0; virtual void OnReconcileComplete(Result result, const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi) = 0; virtual void LoadLedgerState(LedgerCallbackHandler* handler) = 0; @@ -89,14 +90,20 @@ class LEDGER_EXPORT LedgerClient { virtual void SavePublisherInfo(std::unique_ptr publisher_info, PublisherInfoCallback callback) = 0; - virtual void LoadPublisherInfo(PublisherInfoFilter filter, + virtual void SaveActivityInfo(std::unique_ptr publisher_info, PublisherInfoCallback callback) = 0; + virtual void LoadPublisherInfo(const std::string& publisher_key, + PublisherInfoCallback callback) = 0; + virtual void LoadActivityInfo(ActivityInfoFilter filter, + PublisherInfoCallback callback) = 0; + virtual void LoadPanelPublisherInfo(ActivityInfoFilter filter, + PublisherInfoCallback callback) = 0; virtual void LoadMediaPublisherInfo(const std::string& media_key, PublisherInfoCallback callback) = 0; virtual void SaveMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id) = 0; - virtual void LoadPublisherInfoList(uint32_t start, uint32_t limit, - PublisherInfoFilter filter, + virtual void LoadActivityInfoList(uint32_t start, uint32_t limit, + ActivityInfoFilter filter, PublisherInfoListCallback callback) = 0; // TODO this can be removed @@ -118,7 +125,7 @@ class LEDGER_EXPORT LedgerClient { const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) = 0; + const ledger::REWARDS_CATEGORY category) = 0; virtual void GetRecurringDonations(ledger::PublisherInfoListCallback callback) = 0; virtual void OnRemoveRecurring(const std::string& publisher_key, ledger::RecurringRemoveCallback callback) = 0; @@ -148,6 +155,8 @@ class LEDGER_EXPORT LedgerClient { const char* file, int line, const ledger::LogLevel log_level) const = 0; + + virtual void OnRestorePublishers(ledger::OnRestoreCallback callback) = 0; }; } // namespace ledger diff --git a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h index a52291678110..ff0c83c8fe6d 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -13,7 +13,7 @@ namespace ledger { -LEDGER_EXPORT enum PUBLISHER_CATEGORY { +LEDGER_EXPORT enum REWARDS_CATEGORY { AUTO_CONTRIBUTE = 1 << 1, // 2 TIPPING = 1 << 2, DIRECT_DONATION = 1 << 3, // 8 @@ -21,7 +21,7 @@ LEDGER_EXPORT enum PUBLISHER_CATEGORY { ALL_CATEGORIES = (1 << 5) - 1, }; -LEDGER_EXPORT enum PUBLISHER_MONTH { +LEDGER_EXPORT enum ACTIVITY_MONTH { ANY = -1, JANUARY = 1, FEBRUARY = 2, @@ -44,7 +44,7 @@ LEDGER_EXPORT enum PUBLISHER_EXCLUDE { INCLUDED = 2 // user manually changed it to include and is overriding server flags }; -LEDGER_EXPORT enum PUBLISHER_EXCLUDE_FILTER { +LEDGER_EXPORT enum EXCLUDE_FILTER { FILTER_ALL = -1, FILTER_DEFAULT = 0, FILTER_EXCLUDED = 1, @@ -52,19 +52,19 @@ LEDGER_EXPORT enum PUBLISHER_EXCLUDE_FILTER { FILTER_ALL_EXCEPT_EXCLUDED = 3 }; -LEDGER_EXPORT struct PublisherInfoFilter { - PublisherInfoFilter(); - PublisherInfoFilter(const PublisherInfoFilter& filter); - ~PublisherInfoFilter(); +LEDGER_EXPORT struct ActivityInfoFilter { + ActivityInfoFilter(); + ActivityInfoFilter(const ActivityInfoFilter& filter); + ~ActivityInfoFilter(); const std::string ToJson() const; bool loadFromJson(const std::string& json); std::string id; int category; - PUBLISHER_MONTH month; + ACTIVITY_MONTH month; int year; - PUBLISHER_EXCLUDE_FILTER excluded; + EXCLUDE_FILTER excluded; uint32_t percent; std::vector> order_by; uint64_t min_duration; @@ -108,7 +108,7 @@ LEDGER_EXPORT struct PublisherBanner { LEDGER_EXPORT struct PublisherInfo { PublisherInfo(); - PublisherInfo(const std::string& publisher_id, PUBLISHER_MONTH month, int year); + PublisherInfo(const std::string& publisher_id, ACTIVITY_MONTH month, int year); PublisherInfo(const PublisherInfo& info); ~PublisherInfo(); @@ -125,8 +125,8 @@ LEDGER_EXPORT struct PublisherInfo { uint32_t percent; double weight; PUBLISHER_EXCLUDE excluded; - PUBLISHER_CATEGORY category; - PUBLISHER_MONTH month; + REWARDS_CATEGORY category; + ACTIVITY_MONTH month; int year; uint64_t reconcile_stamp; bool verified; diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 36e34777c28d..9024c8e535f5 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -25,7 +25,7 @@ VisitData::VisitData(const std::string& _tld, const std::string& _domain, const std::string& _path, uint32_t _tab_id, - PUBLISHER_MONTH _local_month, + ACTIVITY_MONTH _local_month, int _local_year, const std::string& _name, const std::string& _url, @@ -100,13 +100,13 @@ bool VisitData::loadFromJson(const std::string& json) { PaymentData::PaymentData(): value(0), timestamp(0), - category(PUBLISHER_CATEGORY::TIPPING) {} + category(REWARDS_CATEGORY::TIPPING) {} PaymentData::PaymentData(const std::string& _publisher_id, const double& _value, const int64_t& _timestamp, - PUBLISHER_CATEGORY _category, - PUBLISHER_MONTH _local_month, + REWARDS_CATEGORY _category, + ACTIVITY_MONTH _local_month, int _local_year): publisher_id(_publisher_id), value(_value), @@ -125,17 +125,22 @@ PaymentData::PaymentData(const PaymentData& data): PaymentData::~PaymentData() {} -PublisherInfoFilter::PublisherInfoFilter() : - category(PUBLISHER_CATEGORY::ALL_CATEGORIES), - month(PUBLISHER_MONTH::ANY), +ActivityInfoFilter::ActivityInfoFilter() : + category(REWARDS_CATEGORY::ALL_CATEGORIES), + month(ACTIVITY_MONTH::ANY), year(-1), - excluded(PUBLISHER_EXCLUDE_FILTER::FILTER_DEFAULT), + excluded(EXCLUDE_FILTER::FILTER_DEFAULT), percent(0), min_duration(0), +<<<<<<< HEAD reconcile_stamp(0), non_verified(true) {} PublisherInfoFilter::PublisherInfoFilter(const PublisherInfoFilter& filter) : +======= + reconcile_stamp(0) {} +ActivityInfoFilter::ActivityInfoFilter(const ActivityInfoFilter& filter) : +>>>>>>> Ports ledger cahnges from seperate repo into core id(filter.id), category(filter.category), month(filter.month), @@ -144,10 +149,15 @@ PublisherInfoFilter::PublisherInfoFilter(const PublisherInfoFilter& filter) : percent(filter.percent), order_by(filter.order_by), min_duration(filter.min_duration), +<<<<<<< HEAD reconcile_stamp(filter.reconcile_stamp), non_verified(filter.non_verified) {} PublisherInfoFilter::~PublisherInfoFilter() {} +======= + reconcile_stamp(filter.reconcile_stamp) {} +ActivityInfoFilter::~ActivityInfoFilter() {} +>>>>>>> Ports ledger cahnges from seperate repo into core const std::string PublisherInfoFilter::ToJson() const { std::string json; @@ -263,8 +273,8 @@ PublisherInfo::PublisherInfo() : percent(0u), weight(.0), excluded(PUBLISHER_EXCLUDE::DEFAULT), - category(PUBLISHER_CATEGORY::AUTO_CONTRIBUTE), - month(PUBLISHER_MONTH::ANY), + category(REWARDS_CATEGORY::AUTO_CONTRIBUTE), + month(ACTIVITY_MONTH::ANY), year(-1), reconcile_stamp(0), verified(false), @@ -274,7 +284,7 @@ PublisherInfo::PublisherInfo() : favicon_url("") {} PublisherInfo::PublisherInfo(const std::string& publisher_id, - PUBLISHER_MONTH _month, + ACTIVITY_MONTH _month, int _year) : id(publisher_id), duration(0u), @@ -283,7 +293,7 @@ PublisherInfo::PublisherInfo(const std::string& publisher_id, percent(0u), weight(.0), excluded(PUBLISHER_EXCLUDE::DEFAULT), - category(PUBLISHER_CATEGORY::AUTO_CONTRIBUTE), + category(REWARDS_CATEGORY::AUTO_CONTRIBUTE), month(_month), year(_year), reconcile_stamp(0), @@ -319,9 +329,10 @@ bool PublisherInfo::operator<(const PublisherInfo& rhs) const { } bool PublisherInfo::is_valid() const { - return !id.empty() && year > 0 && month != PUBLISHER_MONTH::ANY; + return !id.empty() && year > 0 && month != ACTIVITY_MONTH::ANY; } +<<<<<<< HEAD const std::string PublisherInfo::ToJson() const { std::string json; braveledger_bat_helper::saveToJsonString(*this, json); @@ -387,6 +398,9 @@ bool PublisherInfo::loadFromJson(const std::string& json) { } const PublisherInfo invalid("", PUBLISHER_MONTH::ANY, -1); +======= +const PublisherInfo invalid("", ACTIVITY_MONTH::ANY, -1); +>>>>>>> Ports ledger cahnges from seperate repo into core const std::string ContributionInfo::ToJson() const { std::string json; diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index 81f2ea6bb717..09c561d073b8 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -164,7 +164,7 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListRecurring( } void BatContribution::ReconcilePublisherList( - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const ledger::PublisherInfoList& list, uint32_t next_record) { std::string viewing_id = ledger_->GenerateGUID(); @@ -209,8 +209,11 @@ void BatContribution::OnTimerReconcile() { } ledger_->GetRecurringDonations( - std::bind(&BatContribution::ReconcilePublisherList, this, - ledger::PUBLISHER_CATEGORY::RECURRING_DONATION, _1, _2)); + std::bind(&BatContribution::ReconcilePublisherList, + this, + ledger::REWARDS_CATEGORY::RECURRING_DONATION, + std::placeholders::_1, + std::placeholders::_2)); } bool BatContribution::ShouldStartAutoContribute() { @@ -228,12 +231,12 @@ void BatContribution::StartAutoContribute() { } uint64_t current_reconcile_stamp = ledger_->GetReconcileStamp(); - ledger::PublisherInfoFilter filter = ledger_->CreatePublisherFilter( + ledger::ActivityInfoFilter filter = ledger_->CreateActivityFilter( "", - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, + ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE, + ledger::ACTIVITY_MONTH::ANY, -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, true, current_reconcile_stamp, ledger_->GetPublisherAllowNonVerified()); @@ -243,14 +246,14 @@ void BatContribution::StartAutoContribute() { filter, std::bind(&BatContribution::ReconcilePublisherList, this, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - _1, - _2)); + ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE, + std::placeholders::_1, + std::placeholders::_2)); } void BatContribution::StartReconcile( const std::string& viewing_id, - const ledger::PUBLISHER_CATEGORY category, + const ledger::REWARDS_CATEGORY category, const braveledger_bat_helper::PublisherList& list, const braveledger_bat_helper::Directions& directions, double budget) { @@ -265,7 +268,7 @@ void BatContribution::StartReconcile( double fee = .0; double balance = ledger_->GetBalance(); - if (category == ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE) { + if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { if (list.size() == 0 || budget > balance || budget == 0) { if (list.size() == 0 || budget == 0) { @@ -291,7 +294,7 @@ void BatContribution::StartReconcile( fee = budget; } - if (category == ledger::PUBLISHER_CATEGORY::RECURRING_DONATION) { + if (category == ledger::REWARDS_CATEGORY::RECURRING_DONATION) { double ac_amount = ledger_->GetContributionAmount(); // don't use ac amount if ac is disabled @@ -311,7 +314,7 @@ void BatContribution::StartReconcile( "You do not have enough funds to do recurring and auto contribution"; OnReconcileComplete(ledger::Result::NOT_ENOUGH_FUNDS, viewing_id, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE); + ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE); return; } @@ -319,7 +322,7 @@ void BatContribution::StartReconcile( fee = budget; } - if (category == ledger::PUBLISHER_CATEGORY::DIRECT_DONATION) { + if (category == ledger::REWARDS_CATEGORY::DIRECT_DONATION) { for (const auto& direction : directions) { if (direction.publisher_key_.empty()) { BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << @@ -757,12 +760,12 @@ void BatContribution::OnReconcileComplete(ledger::Result result, int category, const std::string& probi) { // Start the timer again if it wasn't a direct donation - if (category == ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE) { + if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { ResetReconcileStamp(); } // Trigger auto contribute after recurring donation - if (category == ledger::PUBLISHER_CATEGORY::RECURRING_DONATION) { + if (category == ledger::REWARDS_CATEGORY::RECURRING_DONATION) { StartAutoContribute(); } @@ -798,17 +801,17 @@ void BatContribution::GetReconcileWinners(const std::string& viewing_id) { const auto reconcile = ledger_->GetReconcileById(viewing_id); switch (reconcile.category_) { - case ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE: { + case ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE: { GetContributeWinners(ballots_count, viewing_id, reconcile.list_); break; } - case ledger::PUBLISHER_CATEGORY::RECURRING_DONATION: { + case ledger::REWARDS_CATEGORY::RECURRING_DONATION: { GetDonationWinners(ballots_count, viewing_id, reconcile.list_); break; } - case ledger::PUBLISHER_CATEGORY::DIRECT_DONATION: { + case ledger::REWARDS_CATEGORY::DIRECT_DONATION: { // Direct one-time contribution braveledger_bat_helper::WINNERS_ST winner; winner.votes_ = ballots_count; @@ -1407,12 +1410,12 @@ void BatContribution::SetTimer(uint32_t& timer_id, uint64_t start_timer_in) { void BatContribution::OnReconcileCompleteSuccess( const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, uint32_t date) { - if (category == ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE) { + if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { ledger_->SetBalanceReportItem(month, year, ledger::ReportType::AUTO_CONTRIBUTION, @@ -1421,7 +1424,7 @@ void BatContribution::OnReconcileCompleteSuccess( return; } - if (category == ledger::PUBLISHER_CATEGORY::DIRECT_DONATION) { + if (category == ledger::REWARDS_CATEGORY::DIRECT_DONATION) { ledger_->SetBalanceReportItem(month, year, ledger::ReportType::DONATION, @@ -1440,7 +1443,7 @@ void BatContribution::OnReconcileCompleteSuccess( return; } - if (category == ledger::PUBLISHER_CATEGORY::RECURRING_DONATION) { + if (category == ledger::REWARDS_CATEGORY::RECURRING_DONATION) { auto reconcile = ledger_->GetReconcileById(viewing_id); ledger_->SetBalanceReportItem(month, year, diff --git a/vendor/bat-native-ledger/src/bat_contribution.h b/vendor/bat-native-ledger/src/bat_contribution.h index 081b7da65170..069fe0f35e1b 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.h +++ b/vendor/bat-native-ledger/src/bat_contribution.h @@ -110,7 +110,7 @@ class BatContribution { // We determinate which contribution we want to do and do appropriate actions void StartReconcile( const std::string &viewing_id, - const ledger::PUBLISHER_CATEGORY category, + const ledger::REWARDS_CATEGORY category, const braveledger_bat_helper::PublisherList& list, const braveledger_bat_helper::Directions& directions = {}, double budget = 0); @@ -124,9 +124,9 @@ class BatContribution { // Does final stage in contribution // Sets reports and contribution info void OnReconcileCompleteSuccess(const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, uint32_t date); @@ -150,7 +150,7 @@ class BatContribution { double& budget); // Entry point for contribution where we have publisher info list - void ReconcilePublisherList(ledger::PUBLISHER_CATEGORY category, + void ReconcilePublisherList(ledger::REWARDS_CATEGORY category, const ledger::PublisherInfoList& list, uint32_t next_record); diff --git a/vendor/bat-native-ledger/src/bat_get_media.cc b/vendor/bat-native-ledger/src/bat_get_media.cc index c8b9f59104ae..d4f08112258b 100644 --- a/vendor/bat-native-ledger/src/bat_get_media.cc +++ b/vendor/bat-native-ledger/src/bat_get_media.cc @@ -106,7 +106,7 @@ void BatGetMedia::getPublisherInfoDataCallback(const std::string& mediaId, return; } - if (!publisher_info.get()) { + if (!publisher_info && !publisher_info.get()) { std::string mediaURL = getMediaURL(mediaId, providerName); if (providerName == YOUTUBE_MEDIA_TYPE) { auto callback = std::bind( @@ -549,12 +549,12 @@ void BatGetMedia::onMediaUserActivity(ledger::Result result, const std::string& providerType, const std::string& media_key) { if (result != ledger::Result::LEDGER_OK && - result != ledger::Result::NOT_FOUND) { + result != ledger::Result::NOT_FOUND) { onMediaActivityError(visit_data, providerType, windowId); return; } - if (result == ledger::Result::NOT_FOUND) { + if (!info || result == ledger::Result::NOT_FOUND) { fetchDataFromUrl(visit_data.url, std::bind(&BatGetMedia::onGetChannelIdFromUserPage, this, windowId, @@ -591,16 +591,15 @@ void BatGetMedia::fetchPublisherDataFromDB(uint64_t windowId, const ledger::VisitData& visit_data, const std::string& providerType, const std::string& publisher_key) { - auto filter = ledger_->CreatePublisherFilter( + auto filter = ledger_->CreateActivityFilter( publisher_key, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, + ledger::ACTIVITY_MONTH::ANY, -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, + ledger::EXCLUDE_FILTER::FILTER_ALL, false, ledger_->GetReconcileStamp(), true); - ledger_->GetPublisherInfo(filter, + ledger_->GetPanelPublisherInfo(filter, std::bind(&BatGetMedia::onFetchPublisherFromDBResponse, this, _1, _2, windowId, visit_data, providerType, publisher_key)); } @@ -702,7 +701,7 @@ void BatGetMedia::onMediaPublisherActivity(ledger::Result result, return; } - if (result == ledger::Result::NOT_FOUND) { + if (!info || result == ledger::Result::NOT_FOUND) { ledger::TwitchEventInfo twitchEventInfo; getPublisherInfoDataCallback(media_id, media_key, diff --git a/vendor/bat-native-ledger/src/bat_publishers.cc b/vendor/bat-native-ledger/src/bat_publishers.cc index 64c4ae479ff4..8014e15b57da 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat_publishers.cc @@ -88,10 +88,9 @@ void BatPublishers::saveVisit(const std::string& publisher_id, } auto filter = CreatePublisherFilter(publisher_id, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, + ledger::ACTIVITY_MONTH::ANY, -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, + ledger::EXCLUDE_FILTER::FILTER_ALL, false, ledger_->GetReconcileStamp(), true); @@ -103,32 +102,28 @@ void BatPublishers::saveVisit(const std::string& publisher_id, 0, _1, _2); - ledger_->GetPublisherInfo(filter, callbackGetPublishers); + ledger_->GetActivityInfo(filter, callbackGetPublishers); } -ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( +ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( const std::string &publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year) { - return CreatePublisherFilter(publisher_id, - category, + return CreateActivityFilter(publisher_id, month, year, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, + ledger::EXCLUDE_FILTER::FILTER_ALL, true, 0, true); } -ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( +ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded) { - return CreatePublisherFilter(publisher_id, - category, + ledger::EXCLUDE_FILTER excluded) { + return CreateActivityFilter(publisher_id, month, year, excluded, @@ -137,34 +132,30 @@ ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( true); } -ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( +ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( const std::string &publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, bool min_duration) { - return CreatePublisherFilter(publisher_id, - category, + return CreateActivityFilter(publisher_id, month, year, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, + ledger::EXCLUDE_FILTER::FILTER_ALL, min_duration, 0, true); } -ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( +ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded, + ledger::EXCLUDE_FILTER excluded, bool min_duration, const uint64_t& currentReconcileStamp, bool non_verified) { - ledger::PublisherInfoFilter filter; + ledger::ActivityInfoFilter filter; filter.id = publisher_id; - filter.category = category; filter.month = month; filter.year = year; filter.excluded = excluded; @@ -176,7 +167,7 @@ ledger::PublisherInfoFilter BatPublishers::CreatePublisherFilter( } std::string BatPublishers::GetBalanceReportName( - const ledger::PUBLISHER_MONTH month, + const ledger::ACTIVITY_MONTH month, int year) { return std::to_string(year) + "_" + std::to_string(month); } @@ -211,15 +202,6 @@ void BatPublishers::saveVisitInternal( visit_data.local_year)); } - // set duration to 0 if you don't have sufficient visit time - // or if you set ac to only verified and site is not verified - // or if auto contribute if off - if ((!ignoreMinTime(publisher_id) && duration < getPublisherMinVisitTime()) || - (!ledger_->GetPublisherAllowNonVerified() && !verified) || - !ledger_->GetAutoContribute()) { - duration = 0; - } - std::string fav_icon = visit_data.favicon_url; if (verified && fav_icon.length() > 0) { ledger_->FetchFavIcon(fav_icon, @@ -234,28 +216,54 @@ void BatPublishers::saveVisitInternal( publisher_info->name = visit_data.name; publisher_info->provider = visit_data.provider; publisher_info->url = visit_data.url; - publisher_info->visits += 1; - publisher_info->category = ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE; - if (!isExcluded(publisher_info->id, publisher_info->excluded)) { - publisher_info->duration += duration; - } else { + publisher_info->verified = verified; + + bool excluded = isExcluded(publisher_info->id, publisher_info->excluded); + bool ignore_time = ignoreMinTime(publisher_id); + if (duration == 0) { + ignore_time = false; + } + + std::unique_ptr panel_info = nullptr; + + if (excluded) { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED; - if (new_visit) { - publisher_info->duration = 0; // don't log auto-excluded - } } - publisher_info->score += concaveScore(duration); - publisher_info->verified = verified; - publisher_info->reconcile_stamp = ledger_->GetReconcileStamp(); - auto media_info = std::make_unique(*publisher_info); + // for new visits that are excluded or are not long enough or ac is off + bool min_duration_new = duration < getPublisherMinVisitTime() && + !ignore_time; + bool min_duration_ok = duration > getPublisherMinVisitTime() || ignore_time; + bool verified_new = !ledger_->GetPublisherAllowNonVerified() && !verified; + bool verified_old = ((!ledger_->GetPublisherAllowNonVerified() && verified) || + ledger_->GetPublisherAllowNonVerified()); - ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&onVisitSavedDummy, _1, _2)); + if (new_visit && + (excluded || + !saveVisitAllowed() || + min_duration_new || + verified_new || + !ledger_->GetAutoContribute())) { + panel_info = std::make_unique(*publisher_info); + ledger_->SetPublisherInfo(std::move(publisher_info), + std::bind(&onVisitSavedDummy, _1, _2)); + } else if (!excluded && + ledger_->GetAutoContribute() && + min_duration_ok || + verified_old) { + publisher_info->visits += 1; + publisher_info->duration += duration; + publisher_info->score += concaveScore(duration); + publisher_info->reconcile_stamp = ledger_->GetReconcileStamp(); + + panel_info = std::make_unique(*publisher_info); + ledger_->SetActivityInfo(std::move(publisher_info), + std::bind(&onVisitSavedDummy, _1, _2)); + } - if (window_id > 0) { + if (panel_info && window_id > 0) { onPublisherActivity(ledger::Result::LEDGER_OK, - std::move(media_info), + std::move(panel_info), window_id, visit_data); } @@ -310,38 +318,29 @@ std::unique_ptr BatPublishers::onPublisherInfoUpdated( } void BatPublishers::setExclude(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& exclude) { - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = CreatePublisherFilter(publisher_id, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, - -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, - false, - currentReconcileStamp, - true); - ledger_->GetPublisherInfo(filter, std::bind(&BatPublishers::onSetExcludeInternal, - this, exclude, _1, _2)); + ledger_->GetPublisherInfo(publisher_id, + std::bind(&BatPublishers::onSetExcludeInternal, + this, + exclude, + _1, + _2)); } void BatPublishers::setPanelExclude(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId) { - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = CreatePublisherFilter(publisher_id, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, - -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, - false, - currentReconcileStamp, - true); - ledger_->GetPublisherInfo(filter, std::bind( - &BatPublishers::onSetPanelExcludeInternal, - this, exclude, windowId, _1, _2)); + ledger_->GetPublisherInfo(publisher_id, + std::bind(&BatPublishers::onSetPanelExcludeInternal, + this, + exclude, + windowId, + _1, + _2)); } -void BatPublishers::onSetExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, - ledger::Result result, - std::unique_ptr publisher_info) { +void BatPublishers::onSetExcludeInternal( + ledger::PUBLISHER_EXCLUDE exclude, + ledger::Result result, + std::unique_ptr publisher_info) { if (result != ledger::Result::LEDGER_OK && result != ledger::Result::NOT_FOUND) { return; @@ -352,20 +351,22 @@ void BatPublishers::onSetExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, return; } - publisher_info->year = -1; if (publisher_info->excluded == ledger::PUBLISHER_EXCLUDE::DEFAULT || publisher_info->excluded == ledger::PUBLISHER_EXCLUDE::INCLUDED) { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED; } else { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::INCLUDED; } - publisher_info->month = ledger::PUBLISHER_MONTH::ANY; + setNumExcludedSitesInternal(exclude); std::string publisherKey = publisher_info->id; ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&BatPublishers::onSetPublisherInfo, this, _1, _2)); + std::bind(&BatPublishers::onSetPublisherInfo, + this, + _1, + _2)); OnExcludedSitesChanged(publisherKey); } @@ -392,49 +393,43 @@ void BatPublishers::onSetPanelExcludeInternal(ledger::PUBLISHER_EXCLUDE exclude, return; } - publisher_info->year = -1; if (publisher_info->excluded == ledger::PUBLISHER_EXCLUDE::DEFAULT || publisher_info->excluded == ledger::PUBLISHER_EXCLUDE::INCLUDED) { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::EXCLUDED; } else { publisher_info->excluded = ledger::PUBLISHER_EXCLUDE::INCLUDED; } - publisher_info->month = ledger::PUBLISHER_MONTH::ANY; + setNumExcludedSitesInternal(exclude); ledger::VisitData visit_data; std::string publisherKey = publisher_info->id; ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&BatPublishers::onPublisherActivity, this, _1, _2, - windowId, visit_data)); + std::bind(&BatPublishers::onPublisherActivity, + this, + _1, + _2, + windowId, + visit_data)); OnExcludedSitesChanged(publisherKey); } -void BatPublishers::restorePublishers() { - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = CreatePublisherFilter("", - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, - -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_EXCLUDED, - false, - currentReconcileStamp, - true); - ledger_->GetPublisherInfoList(0, 0, filter, std::bind(&BatPublishers::onRestorePublishersInternal, - this, _1, _2)); +void BatPublishers::RestorePublishers() { + ledger_->OnRestorePublishers( + std::bind(&BatPublishers::OnRestorePublishersInternal, + this, + _1)); } -void BatPublishers::onRestorePublishersInternal(const ledger::PublisherInfoList& publisherInfoList, uint32_t /* next_record */) { - if (publisherInfoList.size() == 0) { - return; - } - - for (size_t i = 0; i < publisherInfoList.size(); i++) { - // Set to PUBLISHER_EXCLUDE::DEFAULT (0) - setExclude(publisherInfoList[i].id, - ledger::PUBLISHER_EXCLUDE::DEFAULT); +void BatPublishers::OnRestorePublishersInternal(bool success) { + if (success) { + setNumExcludedSites(0); + OnExcludedSitesChanged(""); + } else { + BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << + "Could not restore publishers."; } } @@ -563,8 +558,8 @@ void BatPublishers::synopsisNormalizerInternal( if (saveData) { std::unique_ptr publisher_info; publisher_info.reset(new ledger::PublisherInfo(list[i])); - ledger_->SetPublisherInfo(std::move(publisher_info), - std::bind(&onVisitSavedDummy, _1, _2)); + ledger_->SetActivityInfo(std::move(publisher_info), + std::bind(&onVisitSavedDummy, _1, _2)); } if (newList) { newList->push_back(list[i]); @@ -574,17 +569,24 @@ void BatPublishers::synopsisNormalizerInternal( void BatPublishers::synopsisNormalizer(const ledger::PublisherInfo& info) { auto filter = CreatePublisherFilter("", - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, + ledger::ACTIVITY_MONTH::ANY, -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, + ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, true, ledger_->GetReconcileStamp(), ledger_->GetPublisherAllowNonVerified()); // TODO SZ: We pull the whole list currently, I don't think it consumes lots of RAM, but could. // We need to limit it and iterate. - ledger_->GetPublisherInfoList(0, 0, filter, std::bind(&BatPublishers::synopsisNormalizerInternal, this, - nullptr, true, _1, _2)); + ledger_->GetActivityInfoList( + 0, + 0, + filter, + std::bind(&BatPublishers::synopsisNormalizerInternal, + this, + nullptr, + true, + _1, + _2)); } bool BatPublishers::isVerified(const std::string& publisher_id) { @@ -643,7 +645,7 @@ void BatPublishers::clearAllBalanceReports() { saveState(); } -void BatPublishers::setBalanceReport(ledger::PUBLISHER_MONTH month, +void BatPublishers::setBalanceReport(ledger::ACTIVITY_MONTH month, int year, const ledger::BalanceReportInfo& report_info) { braveledger_bat_helper::REPORT_BALANCE_ST report_balance; @@ -669,7 +671,7 @@ void BatPublishers::setBalanceReport(ledger::PUBLISHER_MONTH month, saveState(); } -bool BatPublishers::getBalanceReport(ledger::PUBLISHER_MONTH month, +bool BatPublishers::getBalanceReport(ledger::ACTIVITY_MONTH month, int year, ledger::BalanceReportInfo* report_info) { std::string name = GetBalanceReportName(month, year); @@ -803,10 +805,9 @@ void BatPublishers::getPublisherActivityFromUrl(uint64_t windowId, const ledger: } auto filter = CreatePublisherFilter(visit_data.domain, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, + ledger::ACTIVITY_MONTH::ANY, -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, + ledger::EXCLUDE_FILTER::FILTER_ALL, false, ledger_->GetReconcileStamp(), true); @@ -820,8 +821,13 @@ void BatPublishers::getPublisherActivityFromUrl(uint64_t windowId, const ledger: new_data.url = visit_data.url; new_data.favicon_url = ""; - ledger_->GetPublisherInfo(filter, - std::bind(&BatPublishers::onPublisherActivity, this, _1, _2, windowId, new_data)); + ledger_->GetPanelPublisherInfo(filter, + std::bind(&BatPublishers::onPublisherActivity, + this, + _1, + _2, + windowId, + new_data)); } void BatPublishers::onPublisherActivity(ledger::Result result, @@ -846,7 +852,7 @@ void BatPublishers::OnExcludedSitesChanged(const std::string& publisher_id) { ledger_->OnExcludedSitesChanged(publisher_id); } -void BatPublishers::setBalanceReportItem(ledger::PUBLISHER_MONTH month, +void BatPublishers::setBalanceReportItem(ledger::ACTIVITY_MONTH month, int year, ledger::ReportType type, const std::string& probi) { @@ -899,16 +905,6 @@ void BatPublishers::getPublisherBanner(const std::string& publisher_id, } } - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = CreatePublisherFilter(publisher_id, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, - -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, - false, - currentReconcileStamp, - true); - ledger::PublisherInfoCallback callbackGetPublisher = std::bind(&BatPublishers::onPublisherBanner, this, callback, @@ -916,7 +912,7 @@ void BatPublishers::getPublisherBanner(const std::string& publisher_id, _1, _2); - ledger_->GetPublisherInfo(filter, callbackGetPublisher); + ledger_->GetPublisherInfo(publisher_id, callbackGetPublisher); } void BatPublishers::onPublisherBanner(ledger::PublisherBannerCallback callback, @@ -926,7 +922,7 @@ void BatPublishers::onPublisherBanner(ledger::PublisherBannerCallback callback, auto new_banner = std::make_unique(banner); - if (result != ledger::Result::LEDGER_OK) { + if (!publisher_info || result != ledger::Result::LEDGER_OK) { callback(std::move(new_banner)); return; } diff --git a/vendor/bat-native-ledger/src/bat_publishers.h b/vendor/bat-native-ledger/src/bat_publishers.h index e701bf3d4135..44bb9196d0b3 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.h +++ b/vendor/bat-native-ledger/src/bat_publishers.h @@ -54,14 +54,14 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void setPanelExclude(const std::string& publisher_id, const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId); - void restorePublishers(); + void RestorePublishers(); void setPublisherAllowNonVerified(const bool& allow); void setPublisherAllowVideos(const bool& allow); - void setBalanceReport(ledger::PUBLISHER_MONTH month, + void setBalanceReport(ledger::ACTIVITY_MONTH month, int year, const ledger::BalanceReportInfo& report_info); - bool getBalanceReport(ledger::PUBLISHER_MONTH month, + bool getBalanceReport(ledger::ACTIVITY_MONTH month, int year, ledger::BalanceReportInfo* report_info); std::map getAllBalanceReports(); @@ -76,7 +76,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { std::unique_ptr onPublisherInfoUpdated( ledger::Result result, std::unique_ptr); - std::string GetBalanceReportName(ledger::PUBLISHER_MONTH month, int year); + std::string GetBalanceReportName(ledger::ACTIVITY_MONTH month, int year); std::vector GetRecurringDonationList(); void RefreshPublishersList(const std::string & pubs_list); @@ -89,37 +89,33 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void getPublisherBanner(const std::string& publisher_id, ledger::PublisherBannerCallback callback); - void setBalanceReportItem(ledger::PUBLISHER_MONTH month, + void setBalanceReportItem(ledger::ACTIVITY_MONTH month, int year, ledger::ReportType type, const std::string& probi); - ledger::PublisherInfoFilter CreatePublisherFilter( + ledger::ActivityInfoFilter CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year); - ledger::PublisherInfoFilter CreatePublisherFilter( + ledger::ActivityInfoFilter CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded); + ledger::EXCLUDE_FILTER excluded); - ledger::PublisherInfoFilter CreatePublisherFilter( + ledger::ActivityInfoFilter CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, bool min_duration); - ledger::PublisherInfoFilter CreatePublisherFilter( + ledger::ActivityInfoFilter CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded, + ledger::EXCLUDE_FILTER excluded, bool min_duration, const uint64_t& currentReconcileStamp, bool non_verified); @@ -174,7 +170,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler { ledger::Result result, std::unique_ptr publisher_info); - void onRestorePublishersInternal(const ledger::PublisherInfoList& publisherInfoList, uint32_t /* next_record */); + void OnRestorePublishersInternal(bool success); double concaveScore(const uint64_t& duration); diff --git a/vendor/bat-native-ledger/src/ledger_impl.cc b/vendor/bat-native-ledger/src/ledger_impl.cc index da0b9fe69937..882641e2c7d8 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/ledger_impl.cc @@ -298,8 +298,23 @@ std::string LedgerImpl::URIEncode(const std::string& value) { void LedgerImpl::SetPublisherInfo(std::unique_ptr info, ledger::PublisherInfoCallback callback) { - ledger_client_->SavePublisherInfo(std::move(info), - std::bind(&LedgerImpl::OnSetPublisherInfo, this, callback, _1, _2)); + ledger_client_->SavePublisherInfo( + std::move(info), + std::bind(&LedgerImpl::OnSetPublisherInfo, + this, + callback, + _1, + _2)); +} + +void LedgerImpl::SetActivityInfo(std::unique_ptr info, + ledger::PublisherInfoCallback callback) { + ledger_client_->SaveActivityInfo(std::move(info), + std::bind(&LedgerImpl::OnSetPublisherInfo, + this, + callback, + _1, + _2)); } void LedgerImpl::SetMediaPublisherInfo(const std::string& media_key, @@ -328,7 +343,11 @@ void LedgerImpl::SetPublisherPanelExclude(const std::string& publisher_id, } void LedgerImpl::RestorePublishers() { - bat_publishers_->restorePublishers(); + bat_publishers_->RestorePublishers(); +} + +void LedgerImpl::OnRestorePublishers(ledger::OnRestoreCallback callback) { + ledger_client_->OnRestorePublishers(callback); } void LedgerImpl::LoadNicewareList(ledger::GetNicewareListCallback callback) { @@ -346,10 +365,20 @@ std::vector LedgerImpl::GetRecurringDonationPublisherI return bat_publishers_->GetRecurringDonationList(); } -void LedgerImpl::GetPublisherInfo( - const ledger::PublisherInfoFilter& filter, +void LedgerImpl::GetPublisherInfo(const std::string& publisher_key, + ledger::PublisherInfoCallback callback) { + ledger_client_->LoadPublisherInfo(publisher_key, callback); +} + +void LedgerImpl::GetActivityInfo(const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoCallback callback) { + ledger_client_->LoadActivityInfo(filter, callback); +} + +void LedgerImpl::GetPanelPublisherInfo( + const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoCallback callback) { - ledger_client_->LoadPublisherInfo(filter, callback); + ledger_client_->LoadPanelPublisherInfo(filter, callback); } void LedgerImpl::GetMediaPublisherInfo(const std::string& media_key, @@ -357,10 +386,12 @@ void LedgerImpl::GetMediaPublisherInfo(const std::string& media_key, ledger_client_->LoadMediaPublisherInfo(media_key, callback); } -void LedgerImpl::GetPublisherInfoList(uint32_t start, uint32_t limit, - const ledger::PublisherInfoFilter& filter, - ledger::PublisherInfoListCallback callback) { - ledger_client_->LoadPublisherInfoList(start, limit, filter, callback); +void LedgerImpl::GetActivityInfoList( + uint32_t start, + uint32_t limit, + const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoListCallback callback) { + ledger_client_->LoadActivityInfoList(start, limit, filter, callback); } void LedgerImpl::SetRewardsMainEnabled(bool enabled) { @@ -477,7 +508,7 @@ void LedgerImpl::OnReconcileComplete(ledger::Result result, ledger_client_->OnReconcileComplete( result, viewing_id, - (ledger::PUBLISHER_CATEGORY)reconcile.category_, + (ledger::REWARDS_CATEGORY)reconcile.category_, probi); } @@ -586,7 +617,7 @@ void LedgerImpl::OnGrantFinish(ledger::Result result, const braveledger_bat_help ledger_client_->OnGrantFinish(result, newGrant); } -bool LedgerImpl::GetBalanceReport(ledger::PUBLISHER_MONTH month, +bool LedgerImpl::GetBalanceReport(ledger::ACTIVITY_MONTH month, int year, ledger::BalanceReportInfo* report_info) const { return bat_publishers_->getBalanceReport(month, year, report_info); @@ -596,7 +627,7 @@ std::map LedgerImpl::GetAllBalanceReport return bat_publishers_->getAllBalanceReports(); } -void LedgerImpl::SetBalanceReport(ledger::PUBLISHER_MONTH month, +void LedgerImpl::SetBalanceReport(ledger::ACTIVITY_MONTH month, int year, const ledger::BalanceReportInfo& report_info) { bat_publishers_->setBalanceReport(month, year, report_info); @@ -639,7 +670,7 @@ void LedgerImpl::DoDirectDonation(const ledger::PublisherInfo& publisher, auto direction_list = std::vector { direction }; braveledger_bat_helper::PublisherList list; bat_contribution_->StartReconcile(GenerateGUID(), - ledger::PUBLISHER_CATEGORY::DIRECT_DONATION, + ledger::REWARDS_CATEGORY::DIRECT_DONATION, list, direction_list); } @@ -785,7 +816,7 @@ void LedgerImpl::OnExcludedSitesChanged(const std::string& publisher_id) { ledger_client_->OnExcludedSitesChanged(publisher_id); } -void LedgerImpl::SetBalanceReportItem(ledger::PUBLISHER_MONTH month, +void LedgerImpl::SetBalanceReportItem(ledger::ACTIVITY_MONTH month, int year, ledger::ReportType type, const std::string& probi) { @@ -807,13 +838,13 @@ double LedgerImpl::GetBalance() { return bat_state_->GetBalance(); } -void LedgerImpl::OnReconcileCompleteSuccess(const std::string& viewing_id, - const ledger::PUBLISHER_CATEGORY category, - const std::string& probi, - const ledger::PUBLISHER_MONTH month, - const int year, - const uint32_t date) { - +void LedgerImpl::OnReconcileCompleteSuccess( + const std::string& viewing_id, + const ledger::REWARDS_CATEGORY category, + const std::string& probi, + const ledger::ACTIVITY_MONTH month, + const int year, + const uint32_t date) { bat_contribution_->OnReconcileCompleteSuccess(viewing_id, category, probi, @@ -838,23 +869,21 @@ void LedgerImpl::OnRemovedRecurring(ledger::Result result) { } } -ledger::PublisherInfoFilter LedgerImpl::CreatePublisherFilter( +ledger::PublisherInfoFilter LedgerImpl::CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, ledger::PUBLISHER_EXCLUDE_FILTER excluded, bool min_duration, const uint64_t& currentReconcileStamp, bool non_verified) { - return bat_publishers_->CreatePublisherFilter(publisher_id, - category, - month, - year, - excluded, - min_duration, - currentReconcileStamp, - non_verified); + return bat_publishers_->CreateActivityFilter(publisher_id, + month, + year, + excluded, + min_duration, + currentReconcileStamp, + non_verified); } @@ -1046,7 +1075,7 @@ void LedgerImpl::SaveContributionInfo(const std::string& probi, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) { + const ledger::REWARDS_CATEGORY category) { ledger_client_->SaveContributionInfo(probi, month, year, diff --git a/vendor/bat-native-ledger/src/ledger_impl.h b/vendor/bat-native-ledger/src/ledger_impl.h index d6b4e3c40e75..ec5dd324939a 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.h +++ b/vendor/bat-native-ledger/src/ledger_impl.h @@ -56,18 +56,27 @@ class LedgerImpl : public ledger::Ledger, void SetPublisherInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; - void GetPublisherInfo(const ledger::PublisherInfoFilter& filter, + void SetActivityInfo(std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback) override; + void GetPublisherInfo(const std::string& publisher_key, ledger::PublisherInfoCallback callback) override; + void GetActivityInfo(const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoCallback callback) override; + void GetPanelPublisherInfo(const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoCallback callback); void GetMediaPublisherInfo(const std::string& media_key, - ledger::PublisherInfoCallback callback) override; + ledger::PublisherInfoCallback callback) override; void SetMediaPublisherInfo(const std::string& media_key, - const std::string& publisher_id) override; + const std::string& publisher_id) override; std::vector GetRecurringDonationPublisherInfo() override; - void GetPublisherInfoList(uint32_t start, uint32_t limit, - const ledger::PublisherInfoFilter& filter, - ledger::PublisherInfoListCallback callback) override; + void GetActivityInfoList(uint32_t start, + uint32_t limit, + const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoListCallback callback) override; - void DoDirectDonation(const ledger::PublisherInfo& publisher, int amount, const std::string& currency) override; + void DoDirectDonation(const ledger::PublisherInfo& publisher, + int amount, + const std::string& currency) override; void SetRewardsMainEnabled(bool enabled) override; void SetPublisherMinVisitTime(uint64_t duration_in_seconds) override; @@ -78,7 +87,7 @@ class LedgerImpl : public ledger::Ledger, void SetUserChangedContribution() override; bool GetUserChangedContribution(); void SetAutoContribute(bool enabled) override; - void SetBalanceReport(ledger::PUBLISHER_MONTH month, + void SetBalanceReport(ledger::ACTIVITY_MONTH month, int year, const ledger::BalanceReportInfo& report_info) override; @@ -98,7 +107,7 @@ class LedgerImpl : public ledger::Ledger, bool GetPublisherAllowVideos() const override; double GetContributionAmount() const override; bool GetAutoContribute() const override; - bool GetBalanceReport(ledger::PUBLISHER_MONTH month, + bool GetBalanceReport(ledger::ACTIVITY_MONTH month, int year, ledger::BalanceReportInfo* report_info) const override; std::map GetAllBalanceReports() const override; @@ -157,6 +166,7 @@ class LedgerImpl : public ledger::Ledger, const ledger::PUBLISHER_EXCLUDE& exclude, uint64_t windowId) override; void RestorePublishers() override; + void OnRestorePublishers(ledger::OnRestoreCallback callback); bool IsWalletCreated() const override; void GetPublisherActivityFromUrl(uint64_t windowId, const ledger::VisitData& visit_data) override; void GetMediaActivityFromUrl(uint64_t windowId, @@ -166,7 +176,7 @@ class LedgerImpl : public ledger::Ledger, std::unique_ptr info, uint64_t windowId); void OnExcludedSitesChanged(const std::string& publisher_id); - void SetBalanceReportItem(ledger::PUBLISHER_MONTH month, + void SetBalanceReportItem(ledger::ACTIVITY_MONTH month, int year, ledger::ReportType type, const std::string& probi) override; @@ -180,19 +190,18 @@ class LedgerImpl : public ledger::Ledger, ledger::PublisherBannerCallback callback) override; double GetBalance() override; void OnReconcileCompleteSuccess(const std::string& viewing_id, - const ledger::PUBLISHER_CATEGORY category, + const ledger::REWARDS_CATEGORY category, const std::string& probi, - const ledger::PUBLISHER_MONTH month, + const ledger::ACTIVITY_MONTH month, const int year, const uint32_t date) override; void GetRecurringDonations(ledger::PublisherInfoListCallback callback); void RemoveRecurring(const std::string& publisher_key) override; - ledger::PublisherInfoFilter CreatePublisherFilter( + ledger::ActivityInfoFilter CreateActivityFilter( const std::string& publisher_id, - ledger::PUBLISHER_CATEGORY category, - ledger::PUBLISHER_MONTH month, + ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded, + ledger::EXCLUDE_FILTER excluded, bool min_duration, const uint64_t& currentReconcileStamp, bool non_verified); @@ -266,7 +275,7 @@ class LedgerImpl : public ledger::Ledger, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category); + const ledger::REWARDS_CATEGORY category); void NormalizeContributeWinners( ledger::PublisherInfoList* newList, From eafb91b503bc145f1ac8f89474acb42e558010d7 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Mon, 10 Dec 2018 16:31:02 +0000 Subject: [PATCH 12/14] PublisherInfo database tests --- browser/ui/webui/brave_rewards_ui.cc | 8 +- .../browser/publisher_info_database.cc | 9 +- .../browser/publisher_info_database.h | 7 +- .../publisher_info_database_unittest.cc | 106 +++++++++ .../rewards_notification_service_impl.cc | 2 +- .../brave_rewards/browser/rewards_service.h | 2 +- .../browser/rewards_service_impl.cc | 3 +- .../browser/rewards_service_impl.h | 2 +- components/brave_rewards/browser/test_util.h | 209 ++++++++++++++++++ .../bat_ledger_client_mojo_proxy.cc | 10 +- .../bat_ledger/bat_ledger_client_mojo_proxy.h | 8 +- .../services/bat_ledger/bat_ledger_impl.cc | 10 +- .../public/cpp/ledger_client_mojo_proxy.cc | 8 +- .../reducers/rewards_panel_reducer_test.ts | 69 ++++-- test/BUILD.gn | 1 + .../include/bat/ledger/publisher_info.h | 1 - .../src/bat/ledger/ledger.cc | 28 +-- .../bat-native-ledger/src/bat_contribution.cc | 3 +- vendor/bat-native-ledger/src/bat_helper.cc | 5 +- .../bat-native-ledger/src/bat_publishers.cc | 48 ++-- vendor/bat-native-ledger/src/ledger_impl.cc | 4 +- .../src/rapidjson_bat_helper.h | 4 +- 22 files changed, 446 insertions(+), 101 deletions(-) create mode 100644 components/brave_rewards/browser/publisher_info_database_unittest.cc diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index d25c659503d7..993e717ce500 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -65,7 +65,7 @@ class RewardsDOMHandler : public WebUIMessageHandler, void GetReconcileStamp(const base::ListValue* args); void GetAddresses(const base::ListValue* args); void SaveSetting(const base::ListValue* args); - void OnGetCurrentContributeList( + void OnContentSiteList( std::unique_ptr, uint32_t record); void OnGetAllBalanceReports( @@ -543,10 +543,10 @@ void RewardsDOMHandler::GetAddresses(const base::ListValue* args) { void RewardsDOMHandler::OnAutoContributePropsReady( std::unique_ptr props) { - rewards_service_->GetCurrentContributeList(0, 0, + rewards_service_->GetContentSiteList(0, 0, props->contribution_min_time, props->reconcile_stamp, props->contribution_non_verified, - base::Bind(&RewardsDOMHandler::OnGetCurrentContributeList, + base::Bind(&RewardsDOMHandler::OnContentSiteList, weak_factory_.GetWeakPtr())); } @@ -674,7 +674,7 @@ void RewardsDOMHandler::RestorePublishers(const base::ListValue *args) { } } -void RewardsDOMHandler::OnGetCurrentContributeList( +void RewardsDOMHandler::OnContentSiteList( std::unique_ptr list, uint32_t record) { if (web_ui()->CanCallJavascript()) { diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index d2a348322026..283f8d823e3c 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -37,15 +37,20 @@ PublisherInfoDatabase::~PublisherInfoDatabase() { } bool PublisherInfoDatabase::Init() { + LOG(ERROR) << "INIT 0"; DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + LOG(ERROR) << "INIT 1"; + if (initialized_) { return true; } + LOG(ERROR) << "INIT 2" << db_path_; if (!db_.Open(db_path_)) { return false; } + LOG(ERROR) << "INIT 3"; // TODO - add error delegate sql::Transaction committer(&db_); @@ -508,11 +513,11 @@ bool PublisherInfoDatabase::GetActivityList( } if (filter.percent > 0) { - clauses += " AND ai.percent >= ?"; + query += " AND ai.percent >= ?"; } if (!filter.non_verified) { - clauses += " AND pi.verified = 1"; + query += " AND pi.verified = 1"; } for (const auto& it : filter.order_by) { diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 06e0d9120ece..c7475bf7da12 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -84,9 +84,12 @@ class PublisherInfoDatabase { std::string GetDiagnosticInfo(int extended_error, sql::Statement* statement); - private: + sql::Database& GetDB(); + bool Init(); + private: + bool CreateContributionInfoTable(); bool CreateContributionInfoIndex(); @@ -108,8 +111,6 @@ class PublisherInfoDatabase { void OnMemoryPressure( base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); - sql::Database& GetDB(); - sql::MetaTable& GetMetaTable(); bool MigrateV1toV2(); diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc new file mode 100644 index 000000000000..db5a3cbd28cb --- /dev/null +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -0,0 +1,106 @@ +/* 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/. */ + +#include +#include + +#include "brave/components/brave_rewards/browser/publisher_info_database.h" + +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/files/scoped_temp_dir.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "sql/database.h" +#include "sql/statement.h" +#include "sql/transaction.h" +#include "third_party/sqlite/sqlite3.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +// npm run test -- brave_unit_tests --filter=PublisherInfoDatabaseTest.* + +namespace brave_rewards { + +// Test data directory, relative to source root +const base::FilePath::CharType kTestDataRelativePath[] = + FILE_PATH_LITERAL("brave/vendor/bat-native-ledger/test/data"); + +class PublisherInfoDatabaseTest : public ::testing::Test { + protected: + PublisherInfoDatabaseTest() { + // You can do set-up work for each test here + } + + ~PublisherInfoDatabaseTest() override { + // You can do clean-up work that doesn't throw exceptions here + } + + // If the constructor and destructor are not enough for setting up and + // cleaning up each test, you can use the following methods + + void SetUp() override { + // Code here will be called immediately after the constructor (right before + // each test) + + base::ScopedTempDir temp_dir; + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); + ASSERT_TRUE(base::DirectoryExists(temp_dir.GetPath())); + base::FilePath db_file = + temp_dir.GetPath().AppendASCII("PublisherInfoDatabaseTest.db"); + + base::FilePath temp_file; + ASSERT_TRUE( + base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file)); + + publisher_info_database_ = std::make_unique(temp_file); + ASSERT_NE(publisher_info_database_, nullptr); + } + + void TearDown() override { + // Code here will be called immediately after each test (right before the + // destructor) + } + + // Objects declared here can be used by all tests in the test case + std::string GetMockDataPath(const std::string& filename) { + base::FilePath path(kTestDataRelativePath); + path = path.AppendASCII(filename); + return path.value(); + } + + sql::Database& GetDB() { + return publisher_info_database_->GetDB(); + } + + std::unique_ptr publisher_info_database_; +}; + +TEST_F(PublisherInfoDatabaseTest, InsertContributionInfo) { + ContributionInfo info; + info.probi = "12345678901234567890123456789012345678901234"; + info.month = ledger::ACTIVITY_MONTH::JANUARY; + info.year = 1970; + info.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE; + info.date = base::Time::Now().ToJsTime(); + info.publisher_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + bool success = publisher_info_database_->InsertContributionInfo(info); + EXPECT_TRUE(success); + + std::string query = "SELECT * FROM contribution_info WHERE publisher_id=?"; + sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); + + info_sql.BindString(0, info.publisher_key); + + EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(info_sql.ColumnString(0), info.publisher_key); + EXPECT_EQ(info_sql.ColumnString(1), info.probi); + EXPECT_EQ(info_sql.ColumnInt64(2), info.date); + EXPECT_EQ(info_sql.ColumnInt(3), info.category); + EXPECT_EQ(info_sql.ColumnInt(4), info.month); + EXPECT_EQ(info_sql.ColumnInt(5), info.year); +} + +} // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_notification_service_impl.cc b/components/brave_rewards/browser/rewards_notification_service_impl.cc index 1e9b335ccb41..218aae176d22 100644 --- a/components/brave_rewards/browser/rewards_notification_service_impl.cc +++ b/components/brave_rewards/browser/rewards_notification_service_impl.cc @@ -312,7 +312,7 @@ void RewardsNotificationServiceImpl::OnReconcileComplete( const std::string& probi) { if ((result == ledger::Result::LEDGER_OK && category == - std::to_string(ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE)) || + std::to_string(ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE)) || result == ledger::Result::LEDGER_ERROR || result == ledger::Result::NOT_ENOUGH_FUNDS || result == ledger::Result::TIP_ERROR) { diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 732a7da42f90..9ba514111219 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -67,7 +67,7 @@ class RewardsService : public KeyedService { virtual void CreateWallet() = 0; virtual void FetchWalletProperties() = 0; - virtual void GetCurrentContributeList( + virtual void GetContentSiteList( uint32_t start, uint32_t limit, uint64_t min_visit_time, diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index d18d2b320f13..b6cc13e97601 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -444,8 +444,7 @@ void RewardsServiceImpl::GetContentSiteList( } ledger::ActivityInfoFilter filter; - filter.category = ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE; - filter.month = ledger::PUBLISHER_MONTH::ANY; + filter.month = ledger::ACTIVITY_MONTH::ANY; filter.year = -1; filter.min_duration = min_visit_time; filter.order_by.push_back(std::pair("ai.percent", false)); diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 0fd0a93d1205..1217e9076caa 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -89,7 +89,7 @@ class RewardsServiceImpl : public RewardsService, void GetNumExcludedSites( const GetNumExcludedSitesCallback& callback) override; void RecoverWallet(const std::string passPhrase) const override; - void GetCurrentContributeList( + void GetContentSiteList( uint32_t start, uint32_t limit, uint64_t min_visit_time, diff --git a/components/brave_rewards/browser/test_util.h b/components/brave_rewards/browser/test_util.h index ecbd65934593..1f40c7bf5efa 100644 --- a/components/brave_rewards/browser/test_util.h +++ b/components/brave_rewards/browser/test_util.h @@ -14,6 +14,215 @@ class Profile; namespace brave_rewards { +class MockLedgerClient : public ledger::Ledger { + public: + MockLedgerClient(); + ~MockLedgerClient() override; + + MOCK_METHOD2(OnLoad, void(const ledger::VisitData& visit_data, + const uint64_t& current_time)); + + MOCK_METHOD0(Initialize, void()); + + MOCK_METHOD0(CreateWallet, bool()); + + MOCK_METHOD1(MakePayment, void(const ledger::PaymentData& payment_data)); + + MOCK_METHOD2(AddRecurringPayment, void(const std::string& publisher_id, + const double& value)); + + MOCK_METHOD3(DoDirectDonation, void(const ledger::PublisherInfo& publisher, + const int amount, + const std::string& currency)); + + MOCK_METHOD2(OnUnload, void(uint32_t tab_id, const uint64_t& current_time)); + + MOCK_METHOD2(OnShow, void(uint32_t tab_id, const uint64_t& current_time)); + + MOCK_METHOD2(OnHide, void(uint32_t tab_id, const uint64_t& current_time)); + + MOCK_METHOD2(OnForeground, void(uint32_t tab_id, + const uint64_t& current_time)); + + MOCK_METHOD2(OnBackground, void(uint32_t tab_id, + const uint64_t& current_time)); + + MOCK_METHOD2(OnMediaStart, void(uint32_t tab_id, + const uint64_t& current_time)); + + MOCK_METHOD2(OnMediaStop, void(uint32_t tab_id, + const uint64_t& current_time)); + MOCK_METHOD6(OnXHRLoad, void( + uint32_t tab_id, + const std::string& url, + const std::map& parts, + const std::string& first_party_url, + const std::string& referrer, + const ledger::VisitData& visit_data)); + + MOCK_METHOD5(OnPostData, void( + const std::string& url, + const std::string& first_party_url, + const std::string& referrer, + const std::string& post_data, + const ledger::VisitData& visit_data)); + + MOCK_METHOD1(OnTimer, void(uint32_t timer_id)); + + MOCK_METHOD1(URIEncode, std::string(const std::string& value)); + + MOCK_METHOD2(SetPublisherInfo, + void(std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback)); + + MOCK_METHOD2(SetActivityInfo, + void(std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback)); + + MOCK_METHOD2(GetPublisherInfo, void(const std::string& publisher_key, + ledger::PublisherInfoCallback callback)); + + MOCK_METHOD2(GetActivityInfo, void(const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoCallback callback)); + + MOCK_METHOD2(SetMediaPublisherInfo, void(const std::string& media_key, + const std::string& publisher_id)); + + MOCK_METHOD2(GetMediaPublisherInfo, void( + const std::string& media_key, + ledger::PublisherInfoCallback callback)); + + MOCK_METHOD4(GetActivityInfoList, void( + uint32_t start, + uint32_t limit, + const ledger::ActivityInfoFilter &filter, + ledger::PublisherInfoListCallback callback)); + + MOCK_METHOD0(GetRecurringDonationPublisherInfo, + std::vector()); + + MOCK_METHOD4(GetPublisherInfoList, void( + uint32_t start, + uint32_t limit, + const ledger::ActivityInfoFilter& filter, + ledger::PublisherInfoListCallback callback)); + + MOCK_METHOD1(SetRewardsMainEnabled, void(bool enabled)); + + MOCK_METHOD1(SetPublisherMinVisitTime, void(uint64_t duration_in_seconds)); + + MOCK_METHOD1(SetPublisherMinVisits, void(unsigned int visits)); + + MOCK_METHOD1(SetPublisherAllowNonVerified, void(bool allow)); + + MOCK_METHOD1(SetPublisherAllowVideos, void(bool allow)); + + MOCK_METHOD1(SetContributionAmount, void(double amount)); + + MOCK_METHOD0(SetUserChangedContribution, void()); + + MOCK_METHOD1(SetAutoContribute, void(bool enabled)); + + MOCK_METHOD3(SetBalanceReport, void( + ledger::ACTIVITY_MONTH month, + int year, + const ledger::BalanceReportInfo& report_info)); + + MOCK_CONST_METHOD0(GetBATAddress, const std::string&()); + + MOCK_CONST_METHOD0(GetBTCAddress, const std::string&()); + + MOCK_CONST_METHOD0(GetETHAddress, const std::string&()); + + MOCK_CONST_METHOD0(GetLTCAddress, const std::string&()); + + MOCK_CONST_METHOD0(GetReconcileStamp, uint64_t()); + + MOCK_CONST_METHOD0(GetRewardsMainEnabled, bool()); + + MOCK_CONST_METHOD0(GetPublisherMinVisitTime, uint64_t()); + + MOCK_CONST_METHOD0(GetPublisherMinVisits, unsigned int()); + + MOCK_CONST_METHOD0(GetNumExcludedSites, unsigned int()); + + MOCK_CONST_METHOD0(GetPublisherAllowNonVerified, bool()); + + MOCK_CONST_METHOD0(GetPublisherAllowVideos, bool()); + + MOCK_CONST_METHOD0(GetContributionAmount, double()); + + MOCK_CONST_METHOD0(GetAutoContribute, bool()); + + MOCK_CONST_METHOD0(FetchWalletProperties, void()); + + MOCK_CONST_METHOD2(FetchGrant, void(const std::string& lang, + const std::string& paymentId)); + + MOCK_CONST_METHOD1(SolveGrantCaptcha, void(const std::string& solution)); + + MOCK_CONST_METHOD0(GetGrantCaptcha, void()); + + MOCK_CONST_METHOD0(GetWalletPassphrase, std::string()); + + MOCK_CONST_METHOD3(GetBalanceReport, bool( + ledger::ACTIVITY_MONTH month, + int year, + ledger::BalanceReportInfo* report_info)); + + MOCK_CONST_METHOD0(GetAllBalanceReports, + std::map()); + + MOCK_CONST_METHOD1(RecoverWallet, void(const std::string& passPhrase)); + + MOCK_METHOD4(SaveMediaVisit, void(const std::string& publisher_id, + const ledger::VisitData& visit_data, + const uint64_t& duration, + const uint64_t window_id)); + + MOCK_METHOD2(SetPublisherExclude, void( + const std::string& publisher_id, + const ledger::PUBLISHER_EXCLUDE& exclude)); + + MOCK_METHOD3(SetPublisherPanelExclude, void( + const std::string& publisher_id, + const ledger::PUBLISHER_EXCLUDE& exclude, + uint64_t windowId)); + + MOCK_METHOD0(RestorePublishers, void()); + + MOCK_CONST_METHOD0(IsWalletCreated, bool()); + + MOCK_METHOD2(GetPublisherActivityFromUrl, void( + uint64_t windowId, + const ledger::VisitData& visit_data)); + + MOCK_METHOD4(SetBalanceReportItem, void(ledger::ACTIVITY_MONTH month, + int year, + ledger::ReportType type, + const std::string& probi)); + + MOCK_METHOD2(GetPublisherBanner, void( + const std::string& publisher_id, + ledger::PublisherBannerCallback callback)); + + MOCK_METHOD0(GetBalance, double()); + + MOCK_METHOD6(OnReconcileCompleteSuccess, void( + const std::string& viewing_id, + const ledger::REWARDS_CATEGORY category, + const std::string& probi, + const ledger::ACTIVITY_MONTH month, + const int year, + const uint32_t date)); + + MOCK_METHOD1(RemoveRecurring, void(const std::string& publisher_key)); + + MOCK_METHOD0(GetDefaultContributionAmount, double()); + + MOCK_CONST_METHOD0(GetBootStamp, uint64_t()); +}; + std::unique_ptr CreateBraveRewardsProfile(const base::FilePath& path); } // namespace brave_rewards 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 bb2cca4e40bb..be58566938ab 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -19,7 +19,7 @@ ledger::Result ToLedgerResult(int32_t result) { return (ledger::Result)result; } -int32_t ToMojomPublisherCategory(ledger::PUBLISHER_CATEGORY category) { +int32_t ToMojomPublisherCategory(ledger::REWARDS_CATEGORY category) { return (int32_t)category; } @@ -144,7 +144,7 @@ void BatLedgerClientMojoProxy::OnRecoverWallet(ledger::Result result, void BatLedgerClientMojoProxy::OnReconcileComplete(ledger::Result result, const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi) { if (!Connected()) return; @@ -311,7 +311,7 @@ void OnLoadPublisherInfo(const ledger::PublisherInfoCallback& callback, } void BatLedgerClientMojoProxy::LoadPublisherInfo( - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) { if (!Connected()) { callback(ledger::Result::LEDGER_ERROR, @@ -340,7 +340,7 @@ void OnLoadPublisherInfoList(const ledger::PublisherInfoListCallback& callback, void BatLedgerClientMojoProxy::LoadPublisherInfoList( uint32_t start, uint32_t limit, - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) { if (!Connected()) { callback(std::vector(), 0); @@ -484,7 +484,7 @@ void BatLedgerClientMojoProxy::SaveContributionInfo(const std::string& probi, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) { + const ledger::REWARDS_CATEGORY category) { if (!Connected()) return; 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 2b8a16f5be0b..5bf88c359372 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -34,7 +34,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, const std::vector& grants) override; void OnReconcileComplete(ledger::Result result, const std::string& viewing_id, - ledger::PUBLISHER_CATEGORY category, + ledger::REWARDS_CATEGORY category, const std::string& probi) override; void OnGrantFinish(ledger::Result result, const ledger::Grant& grant) override; @@ -47,12 +47,12 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void SavePublisherInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; - void LoadPublisherInfo(ledger::PublisherInfoFilter filter, + void LoadPublisherInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; void LoadPublisherInfoList( uint32_t start, uint32_t limit, - ledger::PublisherInfoFilter filter, + ledger::ActivityInfoFilter filter, ledger::PublisherInfoListCallback callback) override; void SavePublishersList(const std::string& publishers_list, ledger::LedgerCallbackHandler* handler) override; @@ -78,7 +78,7 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, const int year, const uint32_t date, const std::string& publisher_key, - const ledger::PUBLISHER_CATEGORY category) override; + const ledger::REWARDS_CATEGORY category) override; void GetRecurringDonations(ledger::PublisherInfoListCallback callback) override; std::unique_ptr Log(const char* file, int line, ledger::LogLevel level) const override; void LoadMediaPublisherInfo( diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index 5887b2e1839e..b038f36487ba 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -18,16 +18,16 @@ ledger::PUBLISHER_EXCLUDE ToLedgerPublisherExclude(int32_t exclude) { return (ledger::PUBLISHER_EXCLUDE)exclude; } -ledger::PUBLISHER_MONTH ToLedgerPublisherMonth(int32_t month) { - return (ledger::PUBLISHER_MONTH)month; +ledger::ACTIVITY_MONTH ToLedgerPublisherMonth(int32_t month) { + return (ledger::ACTIVITY_MONTH)month; } ledger::ReportType ToLedgerReportType(int32_t type) { return (ledger::ReportType)type; } -ledger::PUBLISHER_CATEGORY ToLedgerPublisherCategory(int32_t category) { - return (ledger::PUBLISHER_CATEGORY)category; +ledger::REWARDS_CATEGORY ToLedgerPublisherCategory(int32_t category) { + return (ledger::REWARDS_CATEGORY)category; } } // anonymous namespace @@ -318,7 +318,7 @@ void BatLedgerImpl::GetPublisherInfoList(uint32_t start, uint32_t limit, // delete in OnGetPublisherInfoList auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - ledger::PublisherInfoFilter publisher_info_filter; + ledger::ActivityInfoFilter publisher_info_filter; publisher_info_filter.loadFromJson(filter); ledger_->GetPublisherInfoList(start, limit, publisher_info_filter, std::bind(BatLedgerImpl::OnGetPublisherInfoList, holder, _1, _2)); 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 a6b7d0cb145a..80dcd0facf9d 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 @@ -21,8 +21,8 @@ ledger::Result ToLedgerResult(int32_t result) { return (ledger::Result)result; } -ledger::PUBLISHER_CATEGORY ToLedgerPublisherCategory(int32_t category) { - return (ledger::PUBLISHER_CATEGORY)category; +ledger::REWARDS_CATEGORY ToLedgerPublisherCategory(int32_t category) { + return (ledger::REWARDS_CATEGORY)category; } ledger::Grant ToLedgerGrant(const std::string& grant_json) { @@ -272,7 +272,7 @@ void LedgerClientMojoProxy::LoadPublisherInfo( // deleted in OnLoadPublisherInfo auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - ledger::PublisherInfoFilter publisher_info_filter; + ledger::ActivityInfoFilter publisher_info_filter; publisher_info_filter.loadFromJson(filter); ledger_client_->LoadPublisherInfo(publisher_info_filter, std::bind(LedgerClientMojoProxy::OnLoadPublisherInfo, holder, _1, _2)); @@ -300,7 +300,7 @@ void LedgerClientMojoProxy::LoadPublisherInfoList(uint32_t start, // deleted in OnLoadPublisherInfoList auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - ledger::PublisherInfoFilter publisher_info_filter; + ledger::ActivityInfoFilter publisher_info_filter; publisher_info_filter.loadFromJson(filter); ledger_client_->LoadPublisherInfoList(start, limit, publisher_info_filter, std::bind(LedgerClientMojoProxy::OnLoadPublisherInfoList, diff --git a/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts b/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts index a1b4eefcac46..18fb93099216 100644 --- a/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts +++ b/components/test/brave_rewards/extension/brave_rewards/background/reducers/rewards_panel_reducer_test.ts @@ -21,15 +21,8 @@ describe('rewards panel reducer', () => { describe('ON_TAB_RETRIEVED', () => { describe('persist publisher info', () => { - it.skip('url is the same', () => { + it('url is the same', () => { const initState: Rewards.State = { ...defaultState, walletCreated: true } - const expectedState1: Rewards.State = { ...defaultState, walletCreated: true } - const expectedState2: Rewards.State = { - ...defaultState, - walletCreated: true, - publishers: { 1: 'clifton.io' } - } - const payload = { tab: { url: 'https://clifton.io', @@ -40,6 +33,16 @@ describe('rewards panel reducer', () => { } // first visit + const expectedState1: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://clifton.io' + } + } + } + let state = reducers({ rewardsPanelData: initState }, { type: types.ON_TAB_RETRIEVED, payload @@ -48,9 +51,25 @@ describe('rewards panel reducer', () => { expect(state.rewardsPanelData).toEqual(expectedState1) // imitates ON_PUBLISHER_DATA - state.rewardsPanelData.publishers = { id_1: 'clifton.io' } + state.rewardsPanelData.publishers = { + id_1: { + tabUrl: 'https://clifton.io', + name: 'Clifton' + } + } // second visit + const expectedState2: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://clifton.io', + name: 'Clifton' + } + } + } + state = reducers(state, { type: types.ON_TAB_RETRIEVED, payload @@ -61,9 +80,18 @@ describe('rewards panel reducer', () => { it('url is not the same', () => { const initState: Rewards.State = { ...defaultState, walletCreated: true } - const expectedState: Rewards.State = { ...defaultState, walletCreated: true } // first visit + const expectedState1: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://clifton.io' + } + } + } + let state = reducers({ rewardsPanelData: initState }, { type: types.ON_TAB_RETRIEVED, payload: { @@ -76,12 +104,27 @@ describe('rewards panel reducer', () => { } }) - expect(state.rewardsPanelData).toEqual(expectedState) + expect(state.rewardsPanelData).toEqual(expectedState1) // imitates ON_PUBLISHER_DATA - state.rewardsPanelData.publishers = { id_1: 'clifton.io' } + state.rewardsPanelData.publishers = { + id_1: { + tabUrl: 'clifton.io', + name: 'Clifton' + } + } // second visit + const expectedState2: Rewards.State = { + ...defaultState, + walletCreated: true, + publishers: { + id_1: { + tabUrl: 'https://brave.com' + } + } + } + state = reducers(state, { type: types.ON_TAB_RETRIEVED, payload: { @@ -94,7 +137,7 @@ describe('rewards panel reducer', () => { } }) - expect(state.rewardsPanelData).toEqual(expectedState) + expect(state.rewardsPanelData).toEqual(expectedState2) }) }) }) diff --git a/test/BUILD.gn b/test/BUILD.gn index 593301f4ebc0..3f4477fed2fd 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -87,6 +87,7 @@ test("brave_unit_tests") { sources += [ "//brave/vendor/bat-native-ledger/src/bat_get_media_unittest.cc", "//brave/vendor/bat-native-ledger/src/test/niceware_partial_unittest.cc", + "//brave/components/brave_rewards/browser/publisher_info_database_unittest.cc", "//brave/vendor/bat-native-usermodel/test/usermodel_unittest.cc", "//brave/components/brave_rewards/browser/rewards_service_impl_unittest.cc", ] 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 ff0c83c8fe6d..6c164431c9c0 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -61,7 +61,6 @@ LEDGER_EXPORT struct ActivityInfoFilter { bool loadFromJson(const std::string& json); std::string id; - int category; ACTIVITY_MONTH month; int year; EXCLUDE_FILTER excluded; diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 9024c8e535f5..9e07957d611e 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -86,7 +86,7 @@ bool VisitData::loadFromJson(const std::string& json) { domain = d["domain"].GetString(); path = d["path"].GetString(); tab_id = d["tab_id"].GetUint(); - local_month = (PUBLISHER_MONTH)d["local_month"].GetInt(); + local_month = (ACTIVITY_MONTH)d["local_month"].GetInt(); local_year = d["local_year"].GetInt(); name = d["name"].GetString(); url = d["url"].GetString(); @@ -126,46 +126,34 @@ PaymentData::PaymentData(const PaymentData& data): PaymentData::~PaymentData() {} ActivityInfoFilter::ActivityInfoFilter() : - category(REWARDS_CATEGORY::ALL_CATEGORIES), month(ACTIVITY_MONTH::ANY), year(-1), excluded(EXCLUDE_FILTER::FILTER_DEFAULT), percent(0), min_duration(0), -<<<<<<< HEAD reconcile_stamp(0), non_verified(true) {} -PublisherInfoFilter::PublisherInfoFilter(const PublisherInfoFilter& filter) : -======= - reconcile_stamp(0) {} ActivityInfoFilter::ActivityInfoFilter(const ActivityInfoFilter& filter) : ->>>>>>> Ports ledger cahnges from seperate repo into core id(filter.id), - category(filter.category), month(filter.month), year(filter.year), excluded(filter.excluded), percent(filter.percent), order_by(filter.order_by), min_duration(filter.min_duration), -<<<<<<< HEAD reconcile_stamp(filter.reconcile_stamp), non_verified(filter.non_verified) {} -PublisherInfoFilter::~PublisherInfoFilter() {} -======= - reconcile_stamp(filter.reconcile_stamp) {} ActivityInfoFilter::~ActivityInfoFilter() {} ->>>>>>> Ports ledger cahnges from seperate repo into core -const std::string PublisherInfoFilter::ToJson() const { +const std::string ActivityInfoFilter::ToJson() const { std::string json; braveledger_bat_helper::saveToJsonString(*this, json); return json; } -bool PublisherInfoFilter::loadFromJson(const std::string& json) { +bool ActivityInfoFilter::loadFromJson(const std::string& json) { rapidjson::Document d; d.Parse(json.c_str()); @@ -187,9 +175,9 @@ bool PublisherInfoFilter::loadFromJson(const std::string& json) { if (false == error) { id = d["id"].GetString(); category = d["category"].GetInt(); - month = (PUBLISHER_MONTH)d["month"].GetInt(); + month = (ACTIVITY_MONTH)d["month"].GetInt(); year = d["year"].GetInt(); - excluded = (PUBLISHER_EXCLUDE_FILTER)d["excluded"].GetInt(); + excluded = (EXCLUDE_FILTER)d["excluded"].GetInt(); percent = d["percent"].GetUint(); min_duration = d["min_duration"].GetUint64(); reconcile_stamp = d["reconcile_stamp"].GetUint64(); @@ -373,8 +361,8 @@ bool PublisherInfo::loadFromJson(const std::string& json) { percent = d["percent"].GetUint(); weight = d["weight"].GetDouble(); excluded = (PUBLISHER_EXCLUDE)d["excluded"].GetInt(); - category = (PUBLISHER_CATEGORY)d["category"].GetInt(); - month = (PUBLISHER_MONTH)d["month"].GetInt(); + category = (REWARDS_CATEGORY)d["category"].GetInt(); + month = (ACTIVITY_MONTH)d["month"].GetInt(); year = d["year"].GetInt(); reconcile_stamp = d["reconcile_stamp"].GetUint64(); verified = d["verified"].GetBool(); @@ -397,7 +385,7 @@ bool PublisherInfo::loadFromJson(const std::string& json) { return !error; } -const PublisherInfo invalid("", PUBLISHER_MONTH::ANY, -1); +const PublisherInfo invalid("", ACTIVITY_MONTH::ANY, -1); ======= const PublisherInfo invalid("", ACTIVITY_MONTH::ANY, -1); >>>>>>> Ports ledger cahnges from seperate repo into core diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index 09c561d073b8..56292fdff776 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -233,14 +233,13 @@ void BatContribution::StartAutoContribute() { uint64_t current_reconcile_stamp = ledger_->GetReconcileStamp(); ledger::ActivityInfoFilter filter = ledger_->CreateActivityFilter( "", - ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE, ledger::ACTIVITY_MONTH::ANY, -1, ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, true, current_reconcile_stamp, ledger_->GetPublisherAllowNonVerified()); - ledger_->GetPublisherInfoList( + ledger_->GetActivityInfoList( 0, 0, filter, diff --git a/vendor/bat-native-ledger/src/bat_helper.cc b/vendor/bat-native-ledger/src/bat_helper.cc index c95207a9467b..a5af942031ed 100644 --- a/vendor/bat-native-ledger/src/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat_helper.cc @@ -2617,15 +2617,12 @@ static bool ignore_ = false; } void saveToJson(JsonWriter & writer, - const ledger::PublisherInfoFilter& info) { + const ledger::ActivityInfoFilter& info) { writer.StartObject(); writer.String("id"); writer.String(info.id.c_str()); - writer.String("category"); - writer.Int(info.category); - writer.String("month"); writer.Int(info.month); diff --git a/vendor/bat-native-ledger/src/bat_publishers.cc b/vendor/bat-native-ledger/src/bat_publishers.cc index 8014e15b57da..2bfea6dceb5d 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat_publishers.cc @@ -95,7 +95,8 @@ void BatPublishers::saveVisit(const std::string& publisher_id, ledger_->GetReconcileStamp(), true); - ledger::PublisherInfoCallback callbackGetPublishers = std::bind(&BatPublishers::saveVisitInternal, this, + ledger::PublisherInfoCallback callbackGetPublishers = + std::bind(&BatPublishers::saveVisitInternal, this, publisher_id, visit_data, duration, @@ -110,12 +111,12 @@ ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( ledger::ACTIVITY_MONTH month, int year) { return CreateActivityFilter(publisher_id, - month, - year, - ledger::EXCLUDE_FILTER::FILTER_ALL, - true, - 0, - true); + month, + year, + ledger::EXCLUDE_FILTER::FILTER_ALL, + true, + 0, + true); } ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( @@ -124,12 +125,12 @@ ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( int year, ledger::EXCLUDE_FILTER excluded) { return CreateActivityFilter(publisher_id, - month, - year, - excluded, - true, - 0, - true); + month, + year, + excluded, + true, + 0, + true); } ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( @@ -138,12 +139,12 @@ ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( int year, bool min_duration) { return CreateActivityFilter(publisher_id, - month, - year, - ledger::EXCLUDE_FILTER::FILTER_ALL, - min_duration, - 0, - true); + month, + year, + ledger::EXCLUDE_FILTER::FILTER_ALL, + min_duration, + 0, + true); } ledger::ActivityInfoFilter BatPublishers::CreateActivityFilter( @@ -249,7 +250,7 @@ void BatPublishers::saveVisitInternal( std::bind(&onVisitSavedDummy, _1, _2)); } else if (!excluded && ledger_->GetAutoContribute() && - min_duration_ok || + min_duration_ok && verified_old) { publisher_info->visits += 1; publisher_info->duration += duration; @@ -568,7 +569,7 @@ void BatPublishers::synopsisNormalizerInternal( } void BatPublishers::synopsisNormalizer(const ledger::PublisherInfo& info) { - auto filter = CreatePublisherFilter("", + auto filter = CreateActivityFilter("", ledger::ACTIVITY_MONTH::ANY, -1, ledger::EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED, @@ -745,9 +746,6 @@ void BatPublishers::OnPublisherStateSaved(ledger::Result result) { // TODO - error handling return; } - // SZ: We don't need to normalize on state save, all normalizing is done on AUTO_CONTRIBUTE publishers - // save visit - //synopsisNormalizer(); } std::vector BatPublishers::GetRecurringDonationList() { @@ -804,7 +802,7 @@ void BatPublishers::getPublisherActivityFromUrl(uint64_t windowId, const ledger: return; } - auto filter = CreatePublisherFilter(visit_data.domain, + auto filter = CreateActivityFilter(visit_data.domain, ledger::ACTIVITY_MONTH::ANY, -1, ledger::EXCLUDE_FILTER::FILTER_ALL, diff --git a/vendor/bat-native-ledger/src/ledger_impl.cc b/vendor/bat-native-ledger/src/ledger_impl.cc index 882641e2c7d8..c146b6f786fa 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/ledger_impl.cc @@ -869,11 +869,11 @@ void LedgerImpl::OnRemovedRecurring(ledger::Result result) { } } -ledger::PublisherInfoFilter LedgerImpl::CreateActivityFilter( +ledger::ActivityInfoFilter LedgerImpl::CreateActivityFilter( const std::string& publisher_id, ledger::ACTIVITY_MONTH month, int year, - ledger::PUBLISHER_EXCLUDE_FILTER excluded, + ledger::EXCLUDE_FILTER excluded, bool min_duration, const uint64_t& currentReconcileStamp, bool non_verified) { diff --git a/vendor/bat-native-ledger/src/rapidjson_bat_helper.h b/vendor/bat-native-ledger/src/rapidjson_bat_helper.h index c973d55c1ab4..98a8967410a2 100644 --- a/vendor/bat-native-ledger/src/rapidjson_bat_helper.h +++ b/vendor/bat-native-ledger/src/rapidjson_bat_helper.h @@ -18,7 +18,7 @@ struct ContributionInfo; struct Grant; struct PublisherBanner; struct PublisherInfo; -struct PublisherInfoFilter; +struct ActivityInfoFilter; struct VisitData; struct WalletInfo; @@ -59,7 +59,7 @@ 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::PublisherInfoFilter&); +void saveToJson(JsonWriter & writer, const ledger::ActivityInfoFilter&); void saveToJson(JsonWriter & writer, const ledger::VisitData&); void saveToJson(JsonWriter & writer, const ledger::WalletInfo&); void saveToJson(JsonWriter & writer, const ledger::PendingContribution&); From d76269c9335ba3d508c3f6eea8396e3cbe8c8d8c Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Thu, 3 Jan 2019 13:33:13 +0100 Subject: [PATCH 13/14] Merge fixes --- .../browser/publisher_info_database.cc | 9 +- .../browser/publisher_info_database.h | 2 + .../browser/rewards_service_impl.cc | 51 ++---- .../browser/rewards_service_impl.h | 10 +- components/brave_rewards/browser/test_util.h | 2 +- .../bat_ledger_client_mojo_proxy.cc | 127 ++++++++++++-- .../bat_ledger/bat_ledger_client_mojo_proxy.h | 22 ++- .../services/bat_ledger/bat_ledger_impl.cc | 27 --- .../services/bat_ledger/bat_ledger_impl.h | 8 - .../public/cpp/ledger_client_mojo_proxy.cc | 140 +++++++++++++--- .../public/cpp/ledger_client_mojo_proxy.h | 47 +++++- .../public/interfaces/bat_ledger.mojom | 19 ++- .../include/bat/ledger/ledger_client.h | 2 +- .../include/bat/ledger/pending_contribution.h | 2 +- .../src/bat/ledger/ledger.cc | 8 +- .../bat-native-ledger/src/bat_contribution.cc | 156 ++++++++++++------ .../bat-native-ledger/src/bat_publishers.cc | 18 +- vendor/bat-native-ledger/src/bat_publishers.h | 1 - vendor/bat-native-ledger/src/ledger_impl.cc | 4 +- 19 files changed, 431 insertions(+), 224 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 283f8d823e3c..7de1927fc7d8 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -37,20 +37,15 @@ PublisherInfoDatabase::~PublisherInfoDatabase() { } bool PublisherInfoDatabase::Init() { - LOG(ERROR) << "INIT 0"; DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); - LOG(ERROR) << "INIT 1"; - if (initialized_) { return true; } - LOG(ERROR) << "INIT 2" << db_path_; if (!db_.Open(db_path_)) { return false; } - LOG(ERROR) << "INIT 3"; // TODO - add error delegate sql::Transaction committer(&db_); @@ -67,7 +62,7 @@ bool PublisherInfoDatabase::Init() { !CreateActivityInfoTable() || !CreateMediaPublisherInfoTable() || !CreateRecurringDonationTable() || - !CreatePendingContributionsTable()) + !CreatePendingContributionsTable()) { return false; } @@ -1064,7 +1059,7 @@ sql::InitStatus PublisherInfoDatabase::EnsureCurrentVersion() { // to version 4 if (old_version < 4 && cur_version < 5) { - if (!MigrateV2toV3()) { + if (!MigrateV3toV4()) { LOG(ERROR) << "DB: Error with MigrateV3toV4"; } } diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index c7475bf7da12..45a0cfe19244 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -117,6 +117,8 @@ class PublisherInfoDatabase { bool MigrateV2toV3(); + bool MigrateV3toV4(); + sql::InitStatus EnsureCurrentVersion(); sql::Database db_; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index b6cc13e97601..7d441a5426aa 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -252,20 +252,6 @@ void PostWriteCallback( base::Bind(callback, write_success)); } -void GetContentSiteListInternal( - uint32_t start, - uint32_t limit, - const GetContentSiteListCallback& callback, - const ledger::PublisherInfoList& publisher_list, - uint32_t next_record) { - std::unique_ptr site_list(new ContentSiteList); - for (ledger::PublisherInfoList::const_iterator it = - publisher_list.begin(); it != publisher_list.end(); ++it) { - site_list->push_back(PublisherInfoToContentSite(*it)); - } - callback.Run(std::move(site_list), next_record); -} - time_t GetCurrentTimestamp() { return base::Time::NowFromSystemTime().ToTimeT(); } @@ -439,10 +425,6 @@ void RewardsServiceImpl::GetContentSiteList( uint64_t reconcile_stamp, bool allow_non_verified, const GetContentSiteListCallback& callback) { - if (!Connected()) { - return; - } - ledger::ActivityInfoFilter filter; filter.month = ledger::ACTIVITY_MONTH::ANY; filter.year = -1; @@ -454,29 +436,26 @@ void RewardsServiceImpl::GetContentSiteList( filter.percent = 1; filter.non_verified = allow_non_verified; - bat_ledger_->GetActivityInfoList(start, limit, - filter.ToJson(), - base::BindOnce(&RewardsServiceImpl::OnGetPublisherInfoList, AsWeakPtr(), - start, - limit, - callback)); + GetActivityInfoList(start, limit, + filter, + std::bind(&RewardsServiceImpl::OnGetContentSiteList, + this, + callback, + std::placeholders::_1, + std::placeholders::_2)); } -void RewardsServiceImpl::OnGetPublisherInfoList( - uint32_t start, uint32_t limit, +void RewardsServiceImpl::OnGetContentSiteList( const GetContentSiteListCallback& callback, - const std::vector& publisher_info_list, + const ledger::PublisherInfoList& list, uint32_t next_record) { - ledger::PublisherInfoList list; - - for (const auto& i : publisher_info_list) { - ledger::PublisherInfo info; - info.loadFromJson(i); - list.push_back(info); + std::unique_ptr site_list(new ContentSiteList); + for (ledger::PublisherInfoList::const_iterator it = + list.begin(); it != list.end(); ++it) { + site_list->push_back(PublisherInfoToContentSite(*it)); } - GetContentSiteListInternal(start, limit, callback, std::move(list), - next_record); + callback.Run(std::move(site_list), next_record); } void RewardsServiceImpl::OnLoad(SessionID tab_id, const GURL& url) { @@ -1057,7 +1036,7 @@ void RewardsServiceImpl::OnPanelPublisherInfoLoaded( callback(ledger::Result::LEDGER_OK, std::move(publisher_info)); } -void RewardsServiceImpl::LoadActivityInfoList( +void RewardsServiceImpl::GetActivityInfoList( uint32_t start, uint32_t limit, ledger::ActivityInfoFilter filter, diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 1217e9076caa..4461d9c88627 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -96,6 +96,10 @@ class RewardsServiceImpl : public RewardsService, uint64_t reconcile_stamp, bool allow_non_verified, const GetContentSiteListCallback& callback) override; + void OnGetContentSiteList( + const GetContentSiteListCallback& callback, + const ledger::PublisherInfoList& list, + uint32_t next_record); void OnLoad(SessionID tab_id, const GURL& url) override; void OnUnload(SessionID tab_id) override; void OnShow(SessionID tab_id) override; @@ -272,7 +276,7 @@ class RewardsServiceImpl : public RewardsService, ledger::PublisherInfoCallback callback) override; void LoadPanelPublisherInfo(ledger::ActivityInfoFilter filter, ledger::PublisherInfoCallback callback) override; - void LoadActivityInfoList( + void GetActivityInfoList( uint32_t start, uint32_t limit, ledger::ActivityInfoFilter filter, @@ -346,10 +350,6 @@ class RewardsServiceImpl : public RewardsService, // Mojo Proxy methods void OnPublisherBannerMojoProxy(const std::string& banner); - void OnGetPublisherInfoList(uint32_t start, uint32_t limit, - const GetContentSiteListCallback& callback, - const std::vector& publisher_info_list, - uint32_t next_record); void OnGetAllBalanceReports( const GetAllBalanceReportsCallback& callback, const base::flat_map& json_reports); diff --git a/components/brave_rewards/browser/test_util.h b/components/brave_rewards/browser/test_util.h index 1f40c7bf5efa..8f42676074de 100644 --- a/components/brave_rewards/browser/test_util.h +++ b/components/brave_rewards/browser/test_util.h @@ -101,7 +101,7 @@ class MockLedgerClient : public ledger::Ledger { MOCK_METHOD0(GetRecurringDonationPublisherInfo, std::vector()); - MOCK_METHOD4(GetPublisherInfoList, void( + MOCK_METHOD4(GetActivityInfoList, void( uint32_t start, uint32_t limit, const ledger::ActivityInfoFilter& filter, 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 be58566938ab..e2be5e50ba00 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -4,6 +4,8 @@ #include "brave/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h" +#include + #include "base/logging.h" #include "mojo/public/cpp/bindings/map.h" @@ -311,7 +313,7 @@ void OnLoadPublisherInfo(const ledger::PublisherInfoCallback& callback, } void BatLedgerClientMojoProxy::LoadPublisherInfo( - ledger::ActivityInfoFilter filter, + const std::string& publisher_key, ledger::PublisherInfoCallback callback) { if (!Connected()) { callback(ledger::Result::LEDGER_ERROR, @@ -319,36 +321,33 @@ void BatLedgerClientMojoProxy::LoadPublisherInfo( return; } - bat_ledger_client_->LoadPublisherInfo(filter.ToJson(), + bat_ledger_client_->LoadPublisherInfo(publisher_key, base::BindOnce(&OnLoadPublisherInfo, std::move(callback))); } -void OnLoadPublisherInfoList(const ledger::PublisherInfoListCallback& callback, - const std::vector& publisher_info_list, - uint32_t next_record) { - ledger::PublisherInfoList list; +void OnLoadPanelPublisherInfo(const ledger::PublisherInfoCallback& callback, + int32_t result, const std::string& publisher_info) { + std::unique_ptr info; - for (const auto& publisher_info : publisher_info_list) { - ledger::PublisherInfo info; - info.loadFromJson(publisher_info); - list.push_back(info); + if (!publisher_info.empty()) { + info.reset(new ledger::PublisherInfo()); + info->loadFromJson(publisher_info); } - callback(list, next_record); + callback(ToLedgerResult(result), std::move(info)); } -void BatLedgerClientMojoProxy::LoadPublisherInfoList( - uint32_t start, - uint32_t limit, +void BatLedgerClientMojoProxy::LoadPanelPublisherInfo( ledger::ActivityInfoFilter filter, - ledger::PublisherInfoListCallback callback) { + ledger::PublisherInfoCallback callback) { if (!Connected()) { - callback(std::vector(), 0); + callback(ledger::Result::LEDGER_ERROR, + std::unique_ptr()); return; } - bat_ledger_client_->LoadPublisherInfoList(start, limit, filter.ToJson(), - base::BindOnce(&OnLoadPublisherInfoList, std::move(callback))); + bat_ledger_client_->LoadPanelPublisherInfo(filter.ToJson(), + base::BindOnce(&OnLoadPanelPublisherInfo, std::move(callback))); } void OnLoadMediaPublisherInfo(const ledger::PublisherInfoCallback& callback, @@ -548,6 +547,98 @@ 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 BatLedgerClientMojoProxy::LoadActivityInfo( + ledger::ActivityInfoFilter filter, + ledger::PublisherInfoCallback callback) { + if (!Connected()) { + callback(ledger::Result::LEDGER_ERROR, + std::unique_ptr()); + return; + } + + bat_ledger_client_->LoadPublisherInfo(filter.ToJson(), + 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 BatLedgerClientMojoProxy::SaveActivityInfo( + std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback) { + if (!Connected()) { + callback(ledger::Result::LEDGER_ERROR, + std::unique_ptr()); + return; + } + + std::string json_info = publisher_info ? publisher_info->ToJson() : ""; + bat_ledger_client_->SaveActivityInfo(json_info, + base::BindOnce(&OnSaveActivityInfo, std::move(callback))); +} + +void OnRestorePublishersDone(const ledger::OnRestoreCallback& callback, + bool result) { + callback(result); +} + +void BatLedgerClientMojoProxy::OnRestorePublishers( + ledger::OnRestoreCallback callback) { + if (!Connected()) { + callback(false); + return; + } + + bat_ledger_client_->OnRestorePublishers( + base::BindOnce(&OnRestorePublishersDone, std::move(callback))); +} + +void OnGetActivityInfoList(const ledger::PublisherInfoListCallback& callback, + const std::vector& 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); +} + +void BatLedgerClientMojoProxy::GetActivityInfoList(uint32_t start, + uint32_t limit, + ledger::ActivityInfoFilter filter, + ledger::PublisherInfoListCallback callback) { + if (!Connected()) { + callback(std::vector(), 0); + return; + } + + bat_ledger_client_->GetActivityInfoList(start, + limit, + filter.ToJson(), + base::BindOnce(&OnGetActivityInfoList, std::move(callback))); +} + bool BatLedgerClientMojoProxy::Connected() const { return bat_ledger_client_.is_bound(); } diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h index 5bf88c359372..916ab58d0aeb 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -47,13 +47,10 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void SavePublisherInfo(std::unique_ptr publisher_info, ledger::PublisherInfoCallback callback) override; - void LoadPublisherInfo(ledger::ActivityInfoFilter filter, + void LoadPublisherInfo(const std::string& publisher_key, ledger::PublisherInfoCallback callback) override; - void LoadPublisherInfoList( - uint32_t start, - uint32_t limit, - ledger::ActivityInfoFilter filter, - ledger::PublisherInfoListCallback callback) override; + void LoadPanelPublisherInfo(ledger::ActivityInfoFilter filter, + ledger::PublisherInfoCallback callback) override; void SavePublishersList(const std::string& publishers_list, ledger::LedgerCallbackHandler* handler) override; void SetTimer(uint64_t time_offset, uint32_t& timer_id) override; @@ -98,6 +95,19 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, void SavePendingContribution( const ledger::PendingContributionList& list) override; + void LoadActivityInfo(ledger::ActivityInfoFilter filter, + ledger::PublisherInfoCallback callback) override; + + void SaveActivityInfo(std::unique_ptr publisher_info, + ledger::PublisherInfoCallback callback) override; + + void OnRestorePublishers(ledger::OnRestoreCallback callback) override; + + void GetActivityInfoList(uint32_t start, + uint32_t limit, + ledger::ActivityInfoFilter filter, + ledger::PublisherInfoListCallback callback) override; + private: bool Connected() const; diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index b038f36487ba..f78082170456 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -297,33 +297,6 @@ void BatLedgerImpl::GetPublisherBanner(const std::string& publisher_id, std::bind(BatLedgerImpl::OnGetPublisherBanner, holder, _1)); } -// static -void BatLedgerImpl::OnGetPublisherInfoList( - CallbackHolder* holder, - const ledger::PublisherInfoList& list, - uint32_t next_record) { - std::vector publisher_info_list; - for (const auto& info : list) { - publisher_info_list.push_back(info.ToJson()); - } - - if (holder->is_valid()) - std::move(holder->get()).Run(publisher_info_list, next_record); - delete holder; -} - -void BatLedgerImpl::GetPublisherInfoList(uint32_t start, uint32_t limit, - const std::string& filter, - GetPublisherInfoListCallback callback) { - // delete in OnGetPublisherInfoList - auto* holder = new CallbackHolder( - AsWeakPtr(), std::move(callback)); - ledger::ActivityInfoFilter publisher_info_filter; - publisher_info_filter.loadFromJson(filter); - ledger_->GetPublisherInfoList(start, limit, publisher_info_filter, - std::bind(BatLedgerImpl::OnGetPublisherInfoList, holder, _1, _2)); -} - void BatLedgerImpl::GetContributionAmount( GetContributionAmountCallback callback) { std::move(callback).Run(ledger_->GetContributionAmount()); diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index 8d88c568bc01..b55c7efb49c6 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -106,9 +106,6 @@ class BatLedgerImpl : public mojom::BatLedger, GetContributionAmountCallback callback) override; void GetPublisherBanner(const std::string& publisher_id, GetPublisherBannerCallback callback) override; - void GetPublisherInfoList(uint32_t start, uint32_t limit, - const std::string& filter, - GetPublisherInfoListCallback callback) override; void DoDirectDonation(const std::string& publisher_info, int32_t amount, const std::string& currency) override; @@ -144,11 +141,6 @@ class BatLedgerImpl : public mojom::BatLedger, CallbackHolder* holder, std::unique_ptr banner); - static void OnGetPublisherInfoList( - CallbackHolder* holder, - const ledger::PublisherInfoList& list, - uint32_t next_record); - 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 80dcd0facf9d..33079b93b8f9 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 @@ -267,43 +267,34 @@ void LedgerClientMojoProxy::OnLoadPublisherInfo( } void LedgerClientMojoProxy::LoadPublisherInfo( - const std::string& filter, + const std::string& publisher_key, LoadPublisherInfoCallback callback) { // deleted in OnLoadPublisherInfo auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); - ledger::ActivityInfoFilter publisher_info_filter; - publisher_info_filter.loadFromJson(filter); - ledger_client_->LoadPublisherInfo(publisher_info_filter, + ledger_client_->LoadPublisherInfo(publisher_key, std::bind(LedgerClientMojoProxy::OnLoadPublisherInfo, holder, _1, _2)); } // static -void LedgerClientMojoProxy::OnLoadPublisherInfoList( - CallbackHolder* holder, - const ledger::PublisherInfoList& list, - uint32_t next_record) { - std::vector publisher_info_list; - for (const auto& info : list) { - publisher_info_list.push_back(info.ToJson()); - } - +void LedgerClientMojoProxy::OnLoadPanelPublisherInfo( + CallbackHolder* holder, + ledger::Result result, std::unique_ptr info) { + std::string json_info = info ? info->ToJson() : ""; if (holder->is_valid()) - std::move(holder->get()).Run(publisher_info_list, next_record); + std::move(holder->get()).Run(ToMojomResult(result), json_info); delete holder; } -void LedgerClientMojoProxy::LoadPublisherInfoList(uint32_t start, - uint32_t limit, - const std::string& filter, - LoadPublisherInfoListCallback callback) { - // deleted in OnLoadPublisherInfoList - auto* holder = new CallbackHolder( +void LedgerClientMojoProxy::LoadPanelPublisherInfo(const std::string& filter, + LoadPanelPublisherInfoCallback callback) { + // deleted in OnLoadPanelPublisherInfo + auto* holder = new CallbackHolder( AsWeakPtr(), std::move(callback)); ledger::ActivityInfoFilter publisher_info_filter; publisher_info_filter.loadFromJson(filter); - ledger_client_->LoadPublisherInfoList(start, limit, publisher_info_filter, - std::bind(LedgerClientMojoProxy::OnLoadPublisherInfoList, + ledger_client_->LoadPanelPublisherInfo(publisher_info_filter, + std::bind(LedgerClientMojoProxy::OnLoadPanelPublisherInfo, holder, _1, _2)); } @@ -498,4 +489,109 @@ void LedgerClientMojoProxy::SavePendingContribution(const std::string& list) { ledger_client_->SavePendingContribution(contribution_list); } +// static +void LedgerClientMojoProxy::OnLoadActivityInfo( + CallbackHolder* holder, + ledger::Result result, + std::unique_ptr info) { + std::string json_info = info ? info->ToJson() : ""; + if (holder->is_valid()) + std::move(holder->get()).Run(ToMojomResult(result), json_info); + delete holder; +} + +void LedgerClientMojoProxy::LoadActivityInfo( + const std::string& filter, + LoadActivityInfoCallback callback) { + // deleted in OnLoadActivityInfo + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + ledger::ActivityInfoFilter publisher_info_filter; + publisher_info_filter.loadFromJson(filter); + ledger_client_->LoadActivityInfo(publisher_info_filter, + std::bind(LedgerClientMojoProxy::OnLoadActivityInfo, holder, _1, _2)); +} + +// static +void LedgerClientMojoProxy::OnSaveActivityInfo( + CallbackHolder* holder, + ledger::Result result, + std::unique_ptr info) { + std::string json_info = info ? info->ToJson() : ""; + if (holder->is_valid()) + std::move(holder->get()).Run(ToMojomResult(result), json_info); + delete holder; +} + +void LedgerClientMojoProxy::SaveActivityInfo( + const std::string& 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), + std::bind(LedgerClientMojoProxy::OnSaveActivityInfo, holder, _1, _2)); +} + +// static +void LedgerClientMojoProxy::OnRestorePublishersDone( + CallbackHolder* holder, + bool result) { + if (holder->is_valid()) + std::move(holder->get()).Run(result); + delete holder; +} + +void LedgerClientMojoProxy::OnRestorePublishers( + OnRestorePublishersCallback callback) { + // deleted in OnRestorePublishersDone + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger_client_->OnRestorePublishers( + std::bind(LedgerClientMojoProxy::OnRestorePublishersDone, holder, _1)); +} + +// static +void LedgerClientMojoProxy::OnGetActivityInfoList( + CallbackHolder* holder, + const 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); + delete holder; +} + +void LedgerClientMojoProxy::GetActivityInfoList(uint32_t start, + uint32_t limit, + const std::string& filter, + GetActivityInfoListCallback callback) { + // deleted in OnGetActivityInfoList + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + ledger::ActivityInfoFilter publisher_info_filter; + publisher_info_filter.loadFromJson(filter); + + ledger_client_->GetActivityInfoList(start, + limit, + publisher_info_filter, + std::bind(LedgerClientMojoProxy::OnGetActivityInfoList, + holder, + _1, + _2)); + +} + } // namespace bat_ledger diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h index b0d75d3d4569..4896331f53dc 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 @@ -44,11 +44,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void SavePublisherInfo(const std::string& publisher_info, SavePublisherInfoCallback callback) override; - void LoadPublisherInfo(const std::string& filter, + void LoadPublisherInfo(const std::string& publisher_key, LoadPublisherInfoCallback callback) override; - void LoadPublisherInfoList(uint32_t start, uint32_t limit, - const std::string& filter, - LoadPublisherInfoListCallback callback) override; + void LoadPanelPublisherInfo(const std::string& filter, + LoadPanelPublisherInfoCallback callback) override; void LoadMediaPublisherInfo(const std::string& media_key, LoadMediaPublisherInfoCallback callback) override; @@ -90,6 +89,19 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, void SavePendingContribution( const std::string& list) override; + void LoadActivityInfo(const std::string& filter, + LoadActivityInfoCallback callback) override; + + void SaveActivityInfo(const std::string& publisher_info, + SaveActivityInfoCallback callback) override; + + void OnRestorePublishers(OnRestorePublishersCallback callback) override; + + void GetActivityInfoList(uint32_t start, + uint32_t limit, + const std::string& filter, + GetActivityInfoListCallback callback) override; + private: // workaround to pass base::OnceCallback into std::bind // also serves as a wrapper for passing ledger::LedgerCallbackHandler* @@ -129,10 +141,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, ledger::Result result, std::unique_ptr info); - static void OnLoadPublisherInfoList( - CallbackHolder* holder, - const ledger::PublisherInfoList& list, - uint32_t next_record); + static void OnLoadPanelPublisherInfo( + CallbackHolder* holder, + ledger::Result result, + std::unique_ptr info); static void OnLoadMediaPublisherInfo( CallbackHolder* holder, @@ -162,6 +174,25 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, bool success, const std::string& response, const std::map& headers); + static void OnLoadActivityInfo( + CallbackHolder* holder, + ledger::Result result, + std::unique_ptr info); + + static void OnSaveActivityInfo( + CallbackHolder* holder, + ledger::Result result, + std::unique_ptr info); + + static void OnRestorePublishersDone( + CallbackHolder* holder, + bool result); + + static void OnGetActivityInfoList( + CallbackHolder* holder, + const ledger::PublisherInfoList& publisher_info_list, + uint32_t next_record); + ledger::LedgerClient* ledger_client_; DISALLOW_COPY_AND_ASSIGN(LedgerClientMojoProxy); diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index ed4064cee78f..ba4d1fe587eb 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -31,9 +31,6 @@ interface BatLedger { GetAutoContribute() => (bool auto_contribute); GetReconcileStamp() => (uint64 reconcile_stamp); - GetPublisherInfoList(uint32 start, uint32 limit, string filter) => ( - array publisher_info_list, uint32 next_record); - OnLoad(string visit_data, uint64 current_time); OnUnload(uint32 tab_id, uint64 current_time); OnShow(uint32 tab_id, uint64 current_time); @@ -120,9 +117,9 @@ interface BatLedgerClient { SavePublisherInfo(string publisher_info) => (int32 result, string publisher_info); - LoadPublisherInfo(string filter) => (int32 result, string publisher_info); - LoadPublisherInfoList(uint32 start, uint32 limit, string filter) => ( - array publisher_info_list, uint32 next_record); + 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); @@ -156,4 +153,14 @@ interface BatLedgerClient { uint64 window_id); SavePendingContribution(string list); + + LoadActivityInfo(string filter) => (int32 result, string publisher_info); + + SaveActivityInfo(string publisher_info) => (int32 result, + string publisher_info); + + OnRestorePublishers() => (bool result); + + GetActivityInfoList(uint32 start, uint32 limit, string filter) => ( + array publisher_info_list, uint32 next_record); }; 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 aa104e4976f9..2547294b4e96 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -102,7 +102,7 @@ class LEDGER_EXPORT LedgerClient { PublisherInfoCallback callback) = 0; virtual void SaveMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id) = 0; - virtual void LoadActivityInfoList(uint32_t start, uint32_t limit, + virtual void GetActivityInfoList(uint32_t start, uint32_t limit, ActivityInfoFilter filter, PublisherInfoListCallback callback) = 0; diff --git a/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h b/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h index a5782edef381..491fb1aafab9 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h +++ b/vendor/bat-native-ledger/include/bat/ledger/pending_contribution.h @@ -24,7 +24,7 @@ LEDGER_EXPORT struct PendingContribution { double amount = 0; uint64_t added_date = 0; std::string viewing_id; - PUBLISHER_CATEGORY category; + REWARDS_CATEGORY category; }; LEDGER_EXPORT struct PendingContributionList { diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index 9e07957d611e..6f83e513fdbc 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -161,7 +161,6 @@ bool ActivityInfoFilter::loadFromJson(const std::string& json) { bool error = d.HasParseError(); if (false == error) { error = !(d.HasMember("id") && d["id"].IsString() && - d.HasMember("category") && d["category"].IsInt() && d.HasMember("month") && d["month"].IsInt() && d.HasMember("year") && d["year"].IsInt() && d.HasMember("excluded") && d["excluded"].IsInt() && @@ -174,7 +173,6 @@ bool ActivityInfoFilter::loadFromJson(const std::string& json) { if (false == error) { id = d["id"].GetString(); - category = d["category"].GetInt(); month = (ACTIVITY_MONTH)d["month"].GetInt(); year = d["year"].GetInt(); excluded = (EXCLUDE_FILTER)d["excluded"].GetInt(); @@ -320,7 +318,6 @@ bool PublisherInfo::is_valid() const { return !id.empty() && year > 0 && month != ACTIVITY_MONTH::ANY; } -<<<<<<< HEAD const std::string PublisherInfo::ToJson() const { std::string json; braveledger_bat_helper::saveToJsonString(*this, json); @@ -386,9 +383,6 @@ bool PublisherInfo::loadFromJson(const std::string& json) { } const PublisherInfo invalid("", ACTIVITY_MONTH::ANY, -1); -======= -const PublisherInfo invalid("", ACTIVITY_MONTH::ANY, -1); ->>>>>>> Ports ledger cahnges from seperate repo into core const std::string ContributionInfo::ToJson() const { std::string json; @@ -702,7 +696,7 @@ bool PendingContribution::loadFromJson(const std::string& json) { amount = d["amount"].GetDouble(); added_date = d["added_date"].GetUint64(); viewing_id = d["viewing_id"].GetString(); - category = static_cast(d["category"].GetInt()); + category = static_cast(d["category"].GetInt()); } return !error; diff --git a/vendor/bat-native-ledger/src/bat_contribution.cc b/vendor/bat-native-ledger/src/bat_contribution.cc index 56292fdff776..7e0fe904f3e0 100644 --- a/vendor/bat-native-ledger/src/bat_contribution.cc +++ b/vendor/bat-native-ledger/src/bat_contribution.cc @@ -7,19 +7,18 @@ #include #include #include +#include #include "anon/anon.h" #include "bat_contribution.h" #include "ledger_impl.h" #include "rapidjson_bat_helper.h" -using namespace std::placeholders; - namespace braveledger_bat_contribution { static bool winners_votes_compare( const braveledger_bat_helper::WINNERS_ST& first, - const braveledger_bat_helper::WINNERS_ST& second){ + const braveledger_bat_helper::WINNERS_ST& second) { return (first.votes_ < second.votes_); } @@ -46,7 +45,8 @@ void BatContribution::OnStartUp() { for (const auto& value : currentReconciles) { braveledger_bat_helper::CURRENT_RECONCILE reconcile = value.second; - if (reconcile.retry_step_ == braveledger_bat_helper::ContributionRetry::STEP_FINAL) { + if (reconcile.retry_step_ == + braveledger_bat_helper::ContributionRetry::STEP_FINAL) { ledger_->RemoveReconcileById(reconcile.viewingId_); } else { DoRetry(reconcile.viewingId_); @@ -115,7 +115,7 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListAuto( (static_cast(publisher.percent) / 100) * ac_amount; contribution.publisher_key = publisher.id; contribution.viewing_id = viewing_id; - contribution.category = ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE; + contribution.category = ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE; non_verified_bat += contribution.amount; non_verified.list_.push_back(contribution); @@ -150,7 +150,7 @@ ledger::PublisherInfoList BatContribution::GetVerifiedListRecurring( contribution.amount = publisher.weight; contribution.publisher_key = publisher.id; contribution.viewing_id = viewing_id; - contribution.category = ledger::PUBLISHER_CATEGORY::RECURRING_DONATION; + contribution.category = ledger::REWARDS_CATEGORY::RECURRING_DONATION; non_verified.list_.push_back(contribution); } @@ -171,7 +171,7 @@ void BatContribution::ReconcilePublisherList( ledger::PublisherInfoList verified_list; double budget = 0.0; - if (category == ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE) { + if (category == ledger::REWARDS_CATEGORY::AUTO_CONTRIBUTE) { ledger::PublisherInfoList normalized_list; ledger_->NormalizeContributeWinners(&normalized_list, false, list, 0); std::sort(normalized_list.begin(), normalized_list.end()); @@ -365,13 +365,18 @@ void BatContribution::StartReconcile( } void BatContribution::Reconcile(const std::string& viewing_id) { - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_RECONCILE); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_RECONCILE); std::string url = braveledger_bat_helper::buildURL( (std::string)RECONCILE_CONTRIBUTION + ledger_->GetUserId(), PREFIX_V2); - auto callback = std::bind(&BatContribution::ReconcileCallback, this, - viewing_id, _1, _2, _3); + auto callback = std::bind(&BatContribution::ReconcileCallback, + this, + viewing_id, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL(url, std::vector(), "", @@ -390,7 +395,8 @@ void BatContribution::ReconcileCallback( auto reconcile = ledger_->GetReconcileById(viewing_id); if (!result || reconcile.viewingId_.empty()) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_RECONCILE, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_RECONCILE, + viewing_id); return; } @@ -399,7 +405,8 @@ void BatContribution::ReconcileCallback( response, reconcile.surveyorInfo_.surveyorId_); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_RECONCILE, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_RECONCILE, + viewing_id); return; } @@ -415,8 +422,9 @@ void BatContribution::ReconcileCallback( } void BatContribution::CurrentReconcile(const std::string& viewing_id) { - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_CURRENT); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_CURRENT); std::ostringstream amount; auto reconcile = ledger_->GetReconcileById(viewing_id); @@ -431,8 +439,12 @@ void BatContribution::CurrentReconcile(const std::string& viewing_id) { "&altcurrency=" + currency; - auto callback =std::bind(&BatContribution::CurrentReconcileCallback, this, - viewing_id, _1, _2, _3); + auto callback = std::bind(&BatContribution::CurrentReconcileCallback, + this, + viewing_id, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL( braveledger_bat_helper::buildURL(path, PREFIX_V2), std::vector(), @@ -450,7 +462,8 @@ void BatContribution::CurrentReconcileCallback( ledger_->LogResponse(__func__, result, response, headers); if (!result) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, + viewing_id); return; } @@ -459,14 +472,16 @@ void BatContribution::CurrentReconcileCallback( bool success = braveledger_bat_helper::getJSONRates(response, reconcile.rates_); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, + viewing_id); return; } braveledger_bat_helper::UNSIGNED_TX unsigned_tx; success = braveledger_bat_helper::getJSONUnsignedTx(response, unsigned_tx); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, + viewing_id); return; } @@ -474,7 +489,8 @@ void BatContribution::CurrentReconcileCallback( unsigned_tx.currency_.empty() && unsigned_tx.destination_.empty()) { // We don't have any unsigned transactions - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_CURRENT, + viewing_id); return; } @@ -494,8 +510,9 @@ void BatContribution::CurrentReconcileCallback( } void BatContribution::ReconcilePayload(const std::string& viewing_id) { - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD); auto reconcile = ledger_->GetReconcileById(viewing_id); braveledger_bat_helper::WALLET_INFO_ST wallet_info = ledger_->GetWalletInfo(); @@ -546,8 +563,12 @@ void BatContribution::ReconcilePayload(const std::string& viewing_id) { wallet_header.push_back("Content-Type: application/json; charset=UTF-8"); std::string path = (std::string)WALLET_PROPERTIES + ledger_->GetPaymentId(); - auto callback = std::bind(&BatContribution::ReconcilePayloadCallback, this, - viewing_id, _1, _2, _3); + auto callback = std::bind(&BatContribution::ReconcilePayloadCallback, + this, + viewing_id, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL( braveledger_bat_helper::buildURL(path, PREFIX_V2), wallet_header, @@ -565,7 +586,8 @@ void BatContribution::ReconcilePayloadCallback( ledger_->LogResponse(__func__, result, response, headers); if (!result) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD, + viewing_id); return; } @@ -575,7 +597,8 @@ void BatContribution::ReconcilePayloadCallback( bool success = braveledger_bat_helper::getJSONTransaction(response, transaction); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_PAYLOAD, + viewing_id); return; } @@ -593,10 +616,15 @@ void BatContribution::ReconcilePayloadCallback( } void BatContribution::RegisterViewing(const std::string& viewing_id) { - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_REGISTER); - auto callback = std::bind(&BatContribution::RegisterViewingCallback, this, - viewing_id, _1, _2, _3); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_REGISTER); + auto callback = std::bind(&BatContribution::RegisterViewingCallback, + this, + viewing_id, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL( braveledger_bat_helper::buildURL( (std::string)REGISTER_VIEWING, PREFIX_V2), @@ -615,7 +643,8 @@ void BatContribution::RegisterViewingCallback( ledger_->LogResponse(__func__, result, response, headers); if (!result) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_REGISTER, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_REGISTER, + viewing_id); return; } @@ -626,7 +655,8 @@ void BatContribution::RegisterViewingCallback( reconcile.registrarVK_); DCHECK(!reconcile.registrarVK_.empty()); if (!success || reconcile.registrarVK_.empty()) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_REGISTER, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_REGISTER, + viewing_id); return; } @@ -653,8 +683,9 @@ void BatContribution::RegisterViewingCallback( } void BatContribution::ViewingCredentials(const std::string& viewing_id) { - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_VIEWING); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_VIEWING); auto reconcile = ledger_->GetReconcileById(viewing_id); std::string keys[1] = {"proof"}; @@ -669,8 +700,12 @@ void BatContribution::ViewingCredentials(const std::string& viewing_id) { reconcile.anonizeViewingId_, PREFIX_V2); - auto callback = std::bind(&BatContribution::ViewingCredentialsCallback, this, - viewing_id, _1, _2, _3); + auto callback = std::bind(&BatContribution::ViewingCredentialsCallback, + this, + viewing_id, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL(url, std::vector(), proof_stringified, @@ -687,7 +722,8 @@ void BatContribution::ViewingCredentialsCallback( ledger_->LogResponse(__func__, result, response, headers); if (!result) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, + viewing_id); return; } @@ -698,7 +734,8 @@ void BatContribution::ViewingCredentialsCallback( response, verification); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, + viewing_id); return; } @@ -726,7 +763,8 @@ void BatContribution::ViewingCredentialsCallback( response, surveyors); if (!success) { - AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, viewing_id); + AddRetry(braveledger_bat_helper::ContributionRetry::STEP_VIEWING, + viewing_id); return; } @@ -845,8 +883,9 @@ void BatContribution::GetContributeWinners( } braveledger_bat_helper::WINNERS_ST winner; - winner.votes_ = (unsigned int)std::lround( - (double) item.percent_* (double)ballots / 100.0); + winner.votes_ = static_cast(std::lround( + static_cast(item.percent_) * + static_cast(ballots) / 100.0)); total_votes += winner.votes_; winner.publisher_data_.id_ = item.id_; @@ -888,7 +927,8 @@ void BatContribution::GetDonationWinners( braveledger_bat_helper::WINNERS_ST winner; double percent = item.weight_ / reconcile.fee_; - winner.votes_ = (unsigned int)std::lround(percent * (double)ballots); + winner.votes_ = static_cast(std::lround(percent * + static_cast(ballots))); total_votes += winner.votes_; winner.publisher_data_.id_ = item.id_; winner.publisher_data_.duration_ = 0; @@ -927,8 +967,9 @@ void BatContribution::VotePublishers( VotePublisher(publishers[i], viewing_id); } - ledger_->AddReconcileStep(viewing_id, - braveledger_bat_helper::ContributionRetry::STEP_FINAL); + ledger_->AddReconcileStep( + viewing_id, + braveledger_bat_helper::ContributionRetry::STEP_FINAL); PrepareBallots(); } @@ -1020,8 +1061,11 @@ void BatContribution::PrepareBatch( "/" + transaction.anonizeViewingId_, PREFIX_V2); - auto callback = std::bind(&BatContribution::PrepareBatchCallback, this, - _1, _2, _3); + auto callback = std::bind(&BatContribution::PrepareBatchCallback, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL(url, std::vector(), "", @@ -1281,8 +1325,12 @@ void BatContribution::VoteBatch() { std::string url = braveledger_bat_helper::buildURL( (std::string)SURVEYOR_BATCH_VOTING , PREFIX_V2); - auto callback = std::bind(&BatContribution::VoteBatchCallback, this, - batch_votes.publisher_, _1, _2, _3); + auto callback = std::bind(&BatContribution::VoteBatchCallback, + this, + batch_votes.publisher_, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3); ledger_->LoadURL(url, std::vector(), payload, @@ -1372,7 +1420,7 @@ void BatContribution::OnTimer(uint32_t timer_id) { return; } - for(std::pair const& value: retry_timers_) { + for (std::pair const& value : retry_timers_) { if (value.second == timer_id) { std::string viewing_id = value.first; DoRetry(viewing_id); @@ -1510,7 +1558,7 @@ uint64_t BatContribution::GetRetryTimer( reconcile.retry_step_ = step; if (phase == 1) { - // TODO get size from the list + // TODO(nejczdovc) get size from the list if (reconcile.retry_level_ < 5) { if (ledger::short_retries) { return phase_one_debug_timers[reconcile.retry_level_]; @@ -1524,7 +1572,7 @@ uint64_t BatContribution::GetRetryTimer( } if (phase == 2) { - // TODO get size from the list + // TODO(nejczdovc) get size from the list if (reconcile.retry_level_ > 2) { if (ledger::short_retries) { return phase_two_debug_timers[2]; @@ -1537,14 +1585,14 @@ uint64_t BatContribution::GetRetryTimer( } else { return phase_two_timers[reconcile.retry_level_]; } - } } return 0; } -int BatContribution::GetRetryPhase(braveledger_bat_helper::ContributionRetry step) { +int BatContribution::GetRetryPhase( + braveledger_bat_helper::ContributionRetry step) { int phase = 0; switch (step) { diff --git a/vendor/bat-native-ledger/src/bat_publishers.cc b/vendor/bat-native-ledger/src/bat_publishers.cc index 2bfea6dceb5d..aeb9f6bf0b5f 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat_publishers.cc @@ -87,7 +87,7 @@ void BatPublishers::saveVisit(const std::string& publisher_id, return; } - auto filter = CreatePublisherFilter(publisher_id, + auto filter = CreateActivityFilter(publisher_id, ledger::ACTIVITY_MONTH::ANY, -1, ledger::EXCLUDE_FILTER::FILTER_ALL, @@ -241,10 +241,9 @@ void BatPublishers::saveVisitInternal( if (new_visit && (excluded || - !saveVisitAllowed() || + !ledger_->GetAutoContribute() || min_duration_new || - verified_new || - !ledger_->GetAutoContribute())) { + verified_new)) { panel_info = std::make_unique(*publisher_info); ledger_->SetPublisherInfo(std::move(publisher_info), std::bind(&onVisitSavedDummy, _1, _2)); @@ -273,16 +272,7 @@ void BatPublishers::saveVisitInternal( void BatPublishers::onFetchFavIcon(const std::string& publisher_key, bool success, const std::string& favicon_url) { - uint64_t currentReconcileStamp = ledger_->GetReconcileStamp(); - auto filter = ledger_->CreatePublisherFilter(publisher_key, - ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE, - ledger::PUBLISHER_MONTH::ANY, - -1, - ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL, - false, - currentReconcileStamp, - true); - ledger_->GetPublisherInfo(filter, + ledger_->GetPublisherInfo(publisher_key, std::bind(&BatPublishers::onFetchFavIconDBResponse, this, _1, _2, favicon_url)); } diff --git a/vendor/bat-native-ledger/src/bat_publishers.h b/vendor/bat-native-ledger/src/bat_publishers.h index 44bb9196d0b3..c383e8018441 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.h +++ b/vendor/bat-native-ledger/src/bat_publishers.h @@ -38,7 +38,6 @@ class BatPublishers : public ledger::LedgerCallbackHandler { void saveVisit(const std::string& publisher_id, const ledger::VisitData& visit_data, const uint64_t& duration); - bool saveVisitAllowed() const; void AddRecurringPayment(const std::string& publisher_id, const double& value); diff --git a/vendor/bat-native-ledger/src/ledger_impl.cc b/vendor/bat-native-ledger/src/ledger_impl.cc index c146b6f786fa..f3198e65b5d4 100644 --- a/vendor/bat-native-ledger/src/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/ledger_impl.cc @@ -391,7 +391,7 @@ void LedgerImpl::GetActivityInfoList( uint32_t limit, const ledger::ActivityInfoFilter& filter, ledger::PublisherInfoListCallback callback) { - ledger_client_->LoadActivityInfoList(start, limit, filter, callback); + ledger_client_->GetActivityInfoList(start, limit, filter, callback); } void LedgerImpl::SetRewardsMainEnabled(bool enabled) { @@ -656,7 +656,7 @@ void LedgerImpl::DoDirectDonation(const ledger::PublisherInfo& publisher, ledger::PendingContribution contribution; contribution.publisher_key = publisher.id; contribution.amount = amount; - contribution.category = ledger::PUBLISHER_CATEGORY::DIRECT_DONATION; + contribution.category = ledger::REWARDS_CATEGORY::DIRECT_DONATION; ledger::PendingContributionList list; list.list_ = std::vector { contribution }; From e3c864e08db2875049ea46c487f4ad7ac4f2322b Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 15 Jan 2019 06:45:55 +0100 Subject: [PATCH 14/14] Fixes tests --- .../browser/publisher_info_database.cc | 40 +- .../publisher_info_database_unittest.cc | 372 ++++++++++++++++-- components/brave_rewards/browser/test_util.h | 209 ---------- 3 files changed, 355 insertions(+), 266 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 7de1927fc7d8..7530d511bc36 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -17,6 +17,7 @@ #include "sql/statement.h" #include "sql/transaction.h" #include "content_site.h" +#include "recurring_donation.h" namespace brave_rewards { @@ -236,8 +237,9 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( bool initialized = Init(); DCHECK(initialized); - if (!initialized) + if (!initialized || info.id.empty()) { return false; + } sql::Statement publisher_info_statement( GetDB().GetCachedStatement(SQL_FROM_HERE, @@ -409,35 +411,26 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo( bool initialized = Init(); DCHECK(initialized); - if (!initialized) + if (!initialized) { return false; + } - // Insert publisher if it doesn't exist - sql::Statement publisher_info_statement( - GetDB().GetCachedStatement(SQL_FROM_HERE, - "INSERT OR IGNORE INTO publisher_info " - "(publisher_id, verified, excluded, " - "name, url, provider, favIcon) " - "VALUES (?, ?, ?, ?, ?, ?, ?)")); - - publisher_info_statement.BindString(0, info.id); - publisher_info_statement.BindBool(1, info.verified); - publisher_info_statement.BindInt(2, static_cast(info.excluded)); - publisher_info_statement.BindString(3, info.name); - publisher_info_statement.BindString(4, info.url); - publisher_info_statement.BindString(5, info.provider); - publisher_info_statement.BindString(6, info.favicon_url); + LOG(ERROR) << "NEJC 1"; - if (!publisher_info_statement.Run()) { + if (!InsertOrUpdatePublisherInfo(info)) { return false; } + LOG(ERROR) << "NEJC 2"; + + LOG(ERROR) << info.year; + sql::Statement activity_info_insert( GetDB().GetCachedStatement(SQL_FROM_HERE, "INSERT OR REPLACE INTO activity_info " "(publisher_id, duration, score, percent, " - "weight, month, year, reconcile_stamp) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?)")); + "weight, month, year, reconcile_stamp, visits) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")); activity_info_insert.BindString(0, info.id); activity_info_insert.BindInt64(1, static_cast(info.duration)); @@ -447,6 +440,9 @@ bool PublisherInfoDatabase::InsertOrUpdateActivityInfo( activity_info_insert.BindInt(5, info.month); activity_info_insert.BindInt(6, info.year); activity_info_insert.BindInt64(7, info.reconcile_stamp); + activity_info_insert.BindInt64(8, info.visits); + + LOG(ERROR) << "NEJC 3"; return activity_info_insert.Run(); } @@ -629,7 +625,7 @@ bool PublisherInfoDatabase::InsertOrUpdateMediaPublisherInfo( bool initialized = Init(); DCHECK(initialized); - if (!initialized) { + if (!initialized || media_key.empty() || publisher_id.empty()) { return false; } @@ -726,7 +722,7 @@ bool PublisherInfoDatabase::InsertOrUpdateRecurringDonation( bool initialized = Init(); DCHECK(initialized); - if (!initialized) { + if (!initialized || info.publisher_key.empty()) { return false; } diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index db5a3cbd28cb..14966c230bdb 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -14,7 +14,6 @@ #include "base/strings/utf_string_conversions.h" #include "sql/database.h" #include "sql/statement.h" -#include "sql/transaction.h" #include "third_party/sqlite/sqlite3.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -23,61 +22,51 @@ namespace brave_rewards { -// Test data directory, relative to source root -const base::FilePath::CharType kTestDataRelativePath[] = - FILE_PATH_LITERAL("brave/vendor/bat-native-ledger/test/data"); - class PublisherInfoDatabaseTest : public ::testing::Test { protected: PublisherInfoDatabaseTest() { - // You can do set-up work for each test here } ~PublisherInfoDatabaseTest() override { - // You can do clean-up work that doesn't throw exceptions here } - // If the constructor and destructor are not enough for setting up and - // cleaning up each test, you can use the following methods - - void SetUp() override { - // Code here will be called immediately after the constructor (right before - // each test) - - base::ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - ASSERT_TRUE(base::DirectoryExists(temp_dir.GetPath())); - base::FilePath db_file = - temp_dir.GetPath().AppendASCII("PublisherInfoDatabaseTest.db"); + sql::Database& GetDB() { + return publisher_info_database_->GetDB(); + } - base::FilePath temp_file; - ASSERT_TRUE( - base::CreateTemporaryFileInDir(temp_dir.GetPath(), &temp_file)); + void CreateTempDatabase(base::ScopedTempDir* temp_dir, + base::FilePath* db_file) { + ASSERT_TRUE(temp_dir->CreateUniqueTempDir()); + *db_file = temp_dir->GetPath().AppendASCII("PublisherInfoDatabaseTest.db"); + sql::Database::Delete(*db_file); - publisher_info_database_ = std::make_unique(temp_file); + publisher_info_database_ = + std::make_unique(*db_file); ASSERT_NE(publisher_info_database_, nullptr); } - void TearDown() override { - // Code here will be called immediately after each test (right before the - // destructor) - } + int CountTableRows(const std::string& table) { + std::string sql = "SELECT COUNT(*) FROM " + table; + sql::Statement s(GetDB().GetUniqueStatement(sql.c_str())); - // Objects declared here can be used by all tests in the test case - std::string GetMockDataPath(const std::string& filename) { - base::FilePath path(kTestDataRelativePath); - path = path.AppendASCII(filename); - return path.value(); - } + if (!s.Step()) { + return -1; + } - sql::Database& GetDB() { - return publisher_info_database_->GetDB(); + return static_cast(s.ColumnInt64(0)); } std::unique_ptr publisher_info_database_; }; TEST_F(PublisherInfoDatabaseTest, InsertContributionInfo) { + /** + * Good path + */ + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + ContributionInfo info; info.probi = "12345678901234567890123456789012345678901234"; info.month = ledger::ACTIVITY_MONTH::JANUARY; @@ -95,6 +84,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertContributionInfo) { info_sql.BindString(0, info.publisher_key); EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(CountTableRows("contribution_info"), 1); EXPECT_EQ(info_sql.ColumnString(0), info.publisher_key); EXPECT_EQ(info_sql.ColumnString(1), info.probi); EXPECT_EQ(info_sql.ColumnInt64(2), info.date); @@ -103,4 +93,316 @@ TEST_F(PublisherInfoDatabaseTest, InsertContributionInfo) { EXPECT_EQ(info_sql.ColumnInt(5), info.year); } +TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { + /** + * Good path + */ + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + ledger::PublisherInfo info; + info.id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + info.verified = false; + info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; + info.name = "name"; + info.url = "https://brave.com"; + info.provider = ""; + info.favicon_url = ""; + + bool success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + std::string query = "SELECT * FROM publisher_info WHERE publisher_id=?"; + sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); + + info_sql.BindString(0, info.id); + + EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(CountTableRows("publisher_info"), 1); + EXPECT_EQ(info_sql.ColumnString(0), info.id); + EXPECT_EQ(info_sql.ColumnBool(1), info.verified); + EXPECT_EQ(static_cast(info_sql.ColumnInt(2)), + info.excluded); + EXPECT_EQ(info_sql.ColumnString(3), info.name); + EXPECT_EQ(info_sql.ColumnString(4), info.favicon_url); + EXPECT_EQ(info_sql.ColumnString(5), info.url); + EXPECT_EQ(info_sql.ColumnString(6), info.provider); + + /** + * Make sure that second insert is update and not insert + */ + info.verified = true; + info.excluded = ledger::PUBLISHER_EXCLUDE::ALL; + info.name = "updated"; + info.url = "https://clifton.com"; + info.provider = ""; + info.favicon_url = "1"; + + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + query = "SELECT * FROM publisher_info WHERE publisher_id=?"; + sql::Statement info_sql_1(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_1.BindString(0, info.id); + + EXPECT_TRUE(info_sql_1.Step()); + EXPECT_EQ(CountTableRows("publisher_info"), 1); + EXPECT_EQ(info_sql_1.ColumnString(0), info.id); + EXPECT_EQ(info_sql_1.ColumnBool(1), info.verified); + EXPECT_EQ(static_cast(info_sql_1.ColumnInt(2)), + info.excluded); + EXPECT_EQ(info_sql_1.ColumnString(3), info.name); + EXPECT_EQ(info_sql_1.ColumnString(4), info.favicon_url); + EXPECT_EQ(info_sql_1.ColumnString(5), info.url); + EXPECT_EQ(info_sql_1.ColumnString(6), info.provider); + + /** + * Publisher key is missing + */ + info.id = ""; + + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_FALSE(success); + + query = "SELECT * FROM publisher_info WHERE publisher_id=?"; + sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_2.BindString(0, info.id); + + EXPECT_FALSE(info_sql_2.Step()); +} + +TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfo) { + /** + * Good path + */ + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + ledger::PublisherInfo info; + info.id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + info.verified = false; + info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; + info.name = "name"; + info.url = "https://brave.com"; + info.provider = ""; + info.favicon_url = ""; + info.duration = 10; + info.score = 1.1; + info.percent = 100; + info.weight = 1.5; + info.month = ledger::ACTIVITY_MONTH::JANUARY; + info.year = 1970; + info.reconcile_stamp = 0; + info.visits = 1; + + bool success = publisher_info_database_->InsertOrUpdateActivityInfo(info); + EXPECT_TRUE(success); + + std::string query = "SELECT * FROM activity_info WHERE publisher_id=?"; + sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); + + info_sql.BindString(0, info.id); + + EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(CountTableRows("activity_info"), 1); + EXPECT_EQ(info_sql.ColumnString(0), info.id); + EXPECT_EQ(static_cast(info_sql.ColumnInt64(1)), info.duration); + EXPECT_EQ(info_sql.ColumnInt64(2), info.visits); + EXPECT_EQ(info_sql.ColumnDouble(3), info.score); + EXPECT_EQ(info_sql.ColumnInt64(4), info.percent); + EXPECT_EQ(info_sql.ColumnDouble(5), info.weight); + EXPECT_EQ(info_sql.ColumnInt(6), info.month); + EXPECT_EQ(info_sql.ColumnInt(7), info.year); + EXPECT_EQ(static_cast(info_sql.ColumnInt64(8)), + info.reconcile_stamp); + + /** + * Make sure that second insert is update and not insert, + * month, year and stamp is unique key + */ + info.verified = true; + info.excluded = ledger::PUBLISHER_EXCLUDE::ALL; + info.name = "update"; + info.url = "https://slo-tech.com"; + info.provider = "1"; + info.favicon_url = "1"; + info.duration = 11; + info.score = 2.1; + info.percent = 200; + info.weight = 2.5; + info.visits = 2; + + success = publisher_info_database_->InsertOrUpdateActivityInfo(info); + EXPECT_TRUE(success); + + query = "SELECT * FROM activity_info WHERE publisher_id=?"; + sql::Statement info_sql_1(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_1.BindString(0, info.id); + + EXPECT_TRUE(info_sql_1.Step()); + EXPECT_EQ(CountTableRows("activity_info"), 1); + EXPECT_EQ(info_sql_1.ColumnString(0), info.id); + EXPECT_EQ(static_cast(info_sql_1.ColumnInt64(1)), info.duration); + EXPECT_EQ(info_sql_1.ColumnInt64(2), info.visits); + EXPECT_EQ(info_sql_1.ColumnDouble(3), info.score); + EXPECT_EQ(info_sql_1.ColumnInt64(4), info.percent); + EXPECT_EQ(info_sql_1.ColumnDouble(5), info.weight); + EXPECT_EQ(info_sql_1.ColumnInt(6), info.month); + EXPECT_EQ(info_sql_1.ColumnInt(7), info.year); + EXPECT_EQ(static_cast(info_sql_1.ColumnInt64(8)), + info.reconcile_stamp); + +} + +TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateMediaPublisherInfo) { + /** + * Good path + */ + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + std::string publisher_id = "id"; + std::string media_key = "key"; + + bool success = publisher_info_database_->InsertOrUpdateMediaPublisherInfo( + media_key, + publisher_id); + EXPECT_TRUE(success); + + std::string query = + "SELECT * FROM media_publisher_info WHERE media_key=?"; + sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); + + info_sql.BindString(0, media_key); + + EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(CountTableRows("media_publisher_info"), 1); + EXPECT_EQ(info_sql.ColumnString(0), media_key); + EXPECT_EQ(info_sql.ColumnString(1), publisher_id); + + /** + * Make sure that second insert is update and not insert + */ + publisher_id = "id_new"; + + success = publisher_info_database_->InsertOrUpdateMediaPublisherInfo( + media_key, + publisher_id); + EXPECT_TRUE(success); + + query = "SELECT * FROM media_publisher_info WHERE media_key=?"; + sql::Statement info_sql_1(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_1.BindString(0, media_key); + + EXPECT_TRUE(info_sql_1.Step()); + EXPECT_EQ(CountTableRows("media_publisher_info"), 1); + EXPECT_EQ(info_sql_1.ColumnString(0), media_key); + EXPECT_EQ(info_sql_1.ColumnString(1), publisher_id); + + /** + * Publisher key is missing + */ + media_key = "missing"; + success = publisher_info_database_->InsertOrUpdateMediaPublisherInfo( + media_key, + ""); + EXPECT_FALSE(success); + + query = "SELECT * FROM media_publisher_info WHERE media_key=?"; + sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_2.BindString(0, media_key); + + EXPECT_FALSE(info_sql_2.Step()); + + /** + * Media key is missing + */ + publisher_id = "new_stuff"; + success = publisher_info_database_->InsertOrUpdateMediaPublisherInfo( + "", + publisher_id); + EXPECT_FALSE(success); + + query = "SELECT * FROM media_publisher_info WHERE publisher_id=?"; + sql::Statement info_sql_3(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_3.BindString(0, publisher_id); + + EXPECT_FALSE(info_sql_3.Step()); +} + +TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateRecurringDonation) { + /** + * Good path + */ + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + brave_rewards::RecurringDonation info; + info.publisher_key = "key"; + info.amount = 20; + info.added_date = base::Time::Now().ToJsTime(); + + bool success = publisher_info_database_->InsertOrUpdateRecurringDonation( + info); + EXPECT_TRUE(success); + + std::string query = "SELECT * FROM recurring_donation WHERE publisher_id=?"; + sql::Statement info_sql(GetDB().GetUniqueStatement(query.c_str())); + + info_sql.BindString(0, info.publisher_key); + + EXPECT_TRUE(info_sql.Step()); + EXPECT_EQ(CountTableRows("recurring_donation"), 1); + EXPECT_EQ(info_sql.ColumnString(0), info.publisher_key); + EXPECT_EQ(info_sql.ColumnDouble(1), info.amount); + EXPECT_EQ(info_sql.ColumnInt64(2), info.added_date); + + /** + * Make sure that second insert is update and not insert + */ + info.amount = 30; + + success = publisher_info_database_->InsertOrUpdateRecurringDonation(info); + EXPECT_TRUE(success); + + query ="SELECT * FROM recurring_donation WHERE publisher_id=?"; + sql::Statement info_sql_1(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_1.BindString(0, info.publisher_key); + + EXPECT_TRUE(info_sql_1.Step()); + EXPECT_EQ(CountTableRows("recurring_donation"), 1); + EXPECT_EQ(info_sql_1.ColumnString(0), info.publisher_key); + EXPECT_EQ(info_sql_1.ColumnDouble(1), info.amount); + EXPECT_EQ(info_sql_1.ColumnInt64(2), info.added_date); + + /** + * Publisher key is missing + */ + info.publisher_key = ""; + success = publisher_info_database_->InsertOrUpdateRecurringDonation(info); + EXPECT_FALSE(success); + + query = "SELECT * FROM recurring_donation WHERE publisher_id=?"; + sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); + + info_sql_2.BindString(0, info.publisher_key); + + EXPECT_FALSE(info_sql_2.Step()); +} + +TEST_F(PublisherInfoDatabaseTest, InsertPendingContribution) { + +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/test_util.h b/components/brave_rewards/browser/test_util.h index 8f42676074de..ecbd65934593 100644 --- a/components/brave_rewards/browser/test_util.h +++ b/components/brave_rewards/browser/test_util.h @@ -14,215 +14,6 @@ class Profile; namespace brave_rewards { -class MockLedgerClient : public ledger::Ledger { - public: - MockLedgerClient(); - ~MockLedgerClient() override; - - MOCK_METHOD2(OnLoad, void(const ledger::VisitData& visit_data, - const uint64_t& current_time)); - - MOCK_METHOD0(Initialize, void()); - - MOCK_METHOD0(CreateWallet, bool()); - - MOCK_METHOD1(MakePayment, void(const ledger::PaymentData& payment_data)); - - MOCK_METHOD2(AddRecurringPayment, void(const std::string& publisher_id, - const double& value)); - - MOCK_METHOD3(DoDirectDonation, void(const ledger::PublisherInfo& publisher, - const int amount, - const std::string& currency)); - - MOCK_METHOD2(OnUnload, void(uint32_t tab_id, const uint64_t& current_time)); - - MOCK_METHOD2(OnShow, void(uint32_t tab_id, const uint64_t& current_time)); - - MOCK_METHOD2(OnHide, void(uint32_t tab_id, const uint64_t& current_time)); - - MOCK_METHOD2(OnForeground, void(uint32_t tab_id, - const uint64_t& current_time)); - - MOCK_METHOD2(OnBackground, void(uint32_t tab_id, - const uint64_t& current_time)); - - MOCK_METHOD2(OnMediaStart, void(uint32_t tab_id, - const uint64_t& current_time)); - - MOCK_METHOD2(OnMediaStop, void(uint32_t tab_id, - const uint64_t& current_time)); - MOCK_METHOD6(OnXHRLoad, void( - uint32_t tab_id, - const std::string& url, - const std::map& parts, - const std::string& first_party_url, - const std::string& referrer, - const ledger::VisitData& visit_data)); - - MOCK_METHOD5(OnPostData, void( - const std::string& url, - const std::string& first_party_url, - const std::string& referrer, - const std::string& post_data, - const ledger::VisitData& visit_data)); - - MOCK_METHOD1(OnTimer, void(uint32_t timer_id)); - - MOCK_METHOD1(URIEncode, std::string(const std::string& value)); - - MOCK_METHOD2(SetPublisherInfo, - void(std::unique_ptr publisher_info, - ledger::PublisherInfoCallback callback)); - - MOCK_METHOD2(SetActivityInfo, - void(std::unique_ptr publisher_info, - ledger::PublisherInfoCallback callback)); - - MOCK_METHOD2(GetPublisherInfo, void(const std::string& publisher_key, - ledger::PublisherInfoCallback callback)); - - MOCK_METHOD2(GetActivityInfo, void(const ledger::ActivityInfoFilter& filter, - ledger::PublisherInfoCallback callback)); - - MOCK_METHOD2(SetMediaPublisherInfo, void(const std::string& media_key, - const std::string& publisher_id)); - - MOCK_METHOD2(GetMediaPublisherInfo, void( - const std::string& media_key, - ledger::PublisherInfoCallback callback)); - - MOCK_METHOD4(GetActivityInfoList, void( - uint32_t start, - uint32_t limit, - const ledger::ActivityInfoFilter &filter, - ledger::PublisherInfoListCallback callback)); - - MOCK_METHOD0(GetRecurringDonationPublisherInfo, - std::vector()); - - MOCK_METHOD4(GetActivityInfoList, void( - uint32_t start, - uint32_t limit, - const ledger::ActivityInfoFilter& filter, - ledger::PublisherInfoListCallback callback)); - - MOCK_METHOD1(SetRewardsMainEnabled, void(bool enabled)); - - MOCK_METHOD1(SetPublisherMinVisitTime, void(uint64_t duration_in_seconds)); - - MOCK_METHOD1(SetPublisherMinVisits, void(unsigned int visits)); - - MOCK_METHOD1(SetPublisherAllowNonVerified, void(bool allow)); - - MOCK_METHOD1(SetPublisherAllowVideos, void(bool allow)); - - MOCK_METHOD1(SetContributionAmount, void(double amount)); - - MOCK_METHOD0(SetUserChangedContribution, void()); - - MOCK_METHOD1(SetAutoContribute, void(bool enabled)); - - MOCK_METHOD3(SetBalanceReport, void( - ledger::ACTIVITY_MONTH month, - int year, - const ledger::BalanceReportInfo& report_info)); - - MOCK_CONST_METHOD0(GetBATAddress, const std::string&()); - - MOCK_CONST_METHOD0(GetBTCAddress, const std::string&()); - - MOCK_CONST_METHOD0(GetETHAddress, const std::string&()); - - MOCK_CONST_METHOD0(GetLTCAddress, const std::string&()); - - MOCK_CONST_METHOD0(GetReconcileStamp, uint64_t()); - - MOCK_CONST_METHOD0(GetRewardsMainEnabled, bool()); - - MOCK_CONST_METHOD0(GetPublisherMinVisitTime, uint64_t()); - - MOCK_CONST_METHOD0(GetPublisherMinVisits, unsigned int()); - - MOCK_CONST_METHOD0(GetNumExcludedSites, unsigned int()); - - MOCK_CONST_METHOD0(GetPublisherAllowNonVerified, bool()); - - MOCK_CONST_METHOD0(GetPublisherAllowVideos, bool()); - - MOCK_CONST_METHOD0(GetContributionAmount, double()); - - MOCK_CONST_METHOD0(GetAutoContribute, bool()); - - MOCK_CONST_METHOD0(FetchWalletProperties, void()); - - MOCK_CONST_METHOD2(FetchGrant, void(const std::string& lang, - const std::string& paymentId)); - - MOCK_CONST_METHOD1(SolveGrantCaptcha, void(const std::string& solution)); - - MOCK_CONST_METHOD0(GetGrantCaptcha, void()); - - MOCK_CONST_METHOD0(GetWalletPassphrase, std::string()); - - MOCK_CONST_METHOD3(GetBalanceReport, bool( - ledger::ACTIVITY_MONTH month, - int year, - ledger::BalanceReportInfo* report_info)); - - MOCK_CONST_METHOD0(GetAllBalanceReports, - std::map()); - - MOCK_CONST_METHOD1(RecoverWallet, void(const std::string& passPhrase)); - - MOCK_METHOD4(SaveMediaVisit, void(const std::string& publisher_id, - const ledger::VisitData& visit_data, - const uint64_t& duration, - const uint64_t window_id)); - - MOCK_METHOD2(SetPublisherExclude, void( - const std::string& publisher_id, - const ledger::PUBLISHER_EXCLUDE& exclude)); - - MOCK_METHOD3(SetPublisherPanelExclude, void( - const std::string& publisher_id, - const ledger::PUBLISHER_EXCLUDE& exclude, - uint64_t windowId)); - - MOCK_METHOD0(RestorePublishers, void()); - - MOCK_CONST_METHOD0(IsWalletCreated, bool()); - - MOCK_METHOD2(GetPublisherActivityFromUrl, void( - uint64_t windowId, - const ledger::VisitData& visit_data)); - - MOCK_METHOD4(SetBalanceReportItem, void(ledger::ACTIVITY_MONTH month, - int year, - ledger::ReportType type, - const std::string& probi)); - - MOCK_METHOD2(GetPublisherBanner, void( - const std::string& publisher_id, - ledger::PublisherBannerCallback callback)); - - MOCK_METHOD0(GetBalance, double()); - - MOCK_METHOD6(OnReconcileCompleteSuccess, void( - const std::string& viewing_id, - const ledger::REWARDS_CATEGORY category, - const std::string& probi, - const ledger::ACTIVITY_MONTH month, - const int year, - const uint32_t date)); - - MOCK_METHOD1(RemoveRecurring, void(const std::string& publisher_key)); - - MOCK_METHOD0(GetDefaultContributionAmount, double()); - - MOCK_CONST_METHOD0(GetBootStamp, uint64_t()); -}; - std::unique_ptr CreateBraveRewardsProfile(const base::FilePath& path); } // namespace brave_rewards