From b2124db75edc80ae03910c7286e63227cc32b08c Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Thu, 14 Feb 2019 17:24:52 +0100 Subject: [PATCH] Merge pull request #1649 from brave/verified-image Fixes favicon image for verified youtube publishers --- .../browser/publisher_info_database.cc | 34 +++++++++++++-- .../publisher_info_database_unittest.cc | 43 ++++++++++++++++--- .../include/bat/ledger/publisher_info.h | 2 + .../bat-native-ledger/src/bat_publishers.cc | 2 +- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 399feba4b6a7..41c0bb6b79ee 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -242,12 +242,21 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( return false; } + sql::Transaction transaction(&GetDB()); + if (!transaction.Begin()) { + 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 (?, ?, ?, ?, ?, ?, ?)")); + "VALUES (?, ?, ?, ?, ?, ?, " + "(SELECT IFNULL( " + "(SELECT favicon FROM publisher_info " + "WHERE publisher_id = ?), \"\"))" + ")")); publisher_info_statement.BindString(0, info.id); publisher_info_statement.BindBool(1, info.verified); @@ -255,9 +264,28 @@ bool PublisherInfoDatabase::InsertOrUpdatePublisherInfo( 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); + publisher_info_statement.BindString(6, info.id); + + publisher_info_statement.Run(); + + std::string favicon = info.favicon_url; + if (!favicon.empty()) { + sql::Statement favicon_statement( + GetDB().GetCachedStatement(SQL_FROM_HERE, + "UPDATE publisher_info SET favIcon = ? " + "WHERE publisher_id = ?")); + + if (favicon == ledger::clear_favicon) { + favicon.clear(); + } + + favicon_statement.BindString(0, favicon); + favicon_statement.BindString(1, info.id); - return publisher_info_statement.Run(); + favicon_statement.Run(); + } + + return transaction.Commit(); } std::unique_ptr diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 9014d01b64ee..5e567a32faa6 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -101,6 +101,9 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { base::FilePath db_file; CreateTempDatabase(&temp_dir, &db_file); + + const std::string fav_icon = "1"; + ledger::PublisherInfo info; info.id = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; info.verified = false; @@ -108,7 +111,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { info.name = "name"; info.url = "https://brave.com"; info.provider = ""; - info.favicon_url = ""; + info.favicon_url = "0"; bool success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); EXPECT_TRUE(success); @@ -137,7 +140,7 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { info.name = "updated"; info.url = "https://clifton.com"; info.provider = ""; - info.favicon_url = "1"; + info.favicon_url = fav_icon; success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); EXPECT_TRUE(success); @@ -158,6 +161,36 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { EXPECT_EQ(info_sql_1.ColumnString(5), info.url); EXPECT_EQ(info_sql_1.ColumnString(6), info.provider); + /** + * If favicon is empty, don't update record + */ + info.name = "updated2"; + info.favicon_url = ""; + + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + query = "SELECT favicon, name FROM publisher_info WHERE publisher_id=?"; + sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); + info_sql_2.BindString(0, info.id); + EXPECT_TRUE(info_sql_2.Step()); + EXPECT_EQ(info_sql_2.ColumnString(0), fav_icon); + EXPECT_EQ(info_sql_2.ColumnString(1), info.name); + + /** + * If favicon is marked as clear, clear it + */ + info.favicon_url = ledger::clear_favicon; + + success = publisher_info_database_->InsertOrUpdatePublisherInfo(info); + EXPECT_TRUE(success); + + query = "SELECT favicon FROM publisher_info WHERE publisher_id=?"; + sql::Statement info_sql_3(GetDB().GetUniqueStatement(query.c_str())); + info_sql_3.BindString(0, info.id); + EXPECT_TRUE(info_sql_3.Step()); + EXPECT_EQ(info_sql_3.ColumnString(0), ""); + /** * Publisher key is missing */ @@ -167,11 +200,11 @@ TEST_F(PublisherInfoDatabaseTest, InsertOrUpdatePublisherInfo) { EXPECT_FALSE(success); query = "SELECT * FROM publisher_info WHERE publisher_id=?"; - sql::Statement info_sql_2(GetDB().GetUniqueStatement(query.c_str())); + sql::Statement info_sql_4(GetDB().GetUniqueStatement(query.c_str())); - info_sql_2.BindString(0, info.id); + info_sql_4.BindString(0, info.id); - EXPECT_FALSE(info_sql_2.Step()); + EXPECT_FALSE(info_sql_4.Step()); } TEST_F(PublisherInfoDatabaseTest, InsertOrUpdateActivityInfo) { 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 85fb6c83290d..bfdec0ffdd47 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h +++ b/vendor/bat-native-ledger/include/bat/ledger/publisher_info.h @@ -13,6 +13,8 @@ namespace ledger { +const std::string clear_favicon = "clear"; + LEDGER_EXPORT enum REWARDS_CATEGORY { AUTO_CONTRIBUTE = 1 << 1, // 2 TIPPING = 1 << 2, diff --git a/vendor/bat-native-ledger/src/bat_publishers.cc b/vendor/bat-native-ledger/src/bat_publishers.cc index e9e135cc8f25..ddeebaf33b86 100644 --- a/vendor/bat-native-ledger/src/bat_publishers.cc +++ b/vendor/bat-native-ledger/src/bat_publishers.cc @@ -222,7 +222,7 @@ void BatPublishers::saveVisitInternal( publisher_info->favicon_url = fav_icon; } } else { - publisher_info->favicon_url = std::string(); + publisher_info->favicon_url = ledger::clear_favicon; } publisher_info->name = visit_data.name;