diff --git a/components/brave_rewards/browser/publisher_info_database.cc b/components/brave_rewards/browser/publisher_info_database.cc index 0110227310d8..ecee566af18a 100644 --- a/components/brave_rewards/browser/publisher_info_database.cc +++ b/components/brave_rewards/browser/publisher_info_database.cc @@ -645,6 +645,29 @@ bool PublisherInfoDatabase::GetActivityList( return true; } +bool PublisherInfoDatabase::DeleteActivityInfo( + const std::string& publisher_key, + uint64_t reconcile_stamp) { + DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); + + bool initialized = Init(); + DCHECK(initialized); + + if (!initialized || publisher_key.empty() || reconcile_stamp == 0) { + return false; + } + + sql::Statement statement(GetDB().GetCachedStatement( + SQL_FROM_HERE, + "DELETE FROM activity_info WHERE " + "publisher_id = ? AND reconcile_stamp = ?")); + + statement.BindString(0, publisher_key); + statement.BindInt64(1, reconcile_stamp); + + return statement.Run(); +} + /** * * MEDIA PUBLISHER INFO diff --git a/components/brave_rewards/browser/publisher_info_database.h b/components/brave_rewards/browser/publisher_info_database.h index 6e451e1ae9a8..f311bf46a125 100644 --- a/components/brave_rewards/browser/publisher_info_database.h +++ b/components/brave_rewards/browser/publisher_info_database.h @@ -84,6 +84,9 @@ class PublisherInfoDatabase { void SetTestingCurrentVersion(int value); + bool DeleteActivityInfo(const std::string& publisher_key, + uint64_t reconcile_stamp); + // Vacuums the database. This will cause sqlite to defragment and collect // unused space in the file. It can be VERY SLOW. void Vacuum(); diff --git a/components/brave_rewards/browser/publisher_info_database_unittest.cc b/components/brave_rewards/browser/publisher_info_database_unittest.cc index 66dfde35c23a..d9adbb707237 100644 --- a/components/brave_rewards/browser/publisher_info_database_unittest.cc +++ b/components/brave_rewards/browser/publisher_info_database_unittest.cc @@ -864,4 +864,55 @@ TEST_F(PublisherInfoDatabaseTest, GetExcludedPublishersCount) { EXPECT_EQ(publisher_info_database_->GetExcludedPublishersCount(), 2); } +TEST_F(PublisherInfoDatabaseTest, DeleteActivityInfo) { + base::ScopedTempDir temp_dir; + base::FilePath db_file; + CreateTempDatabase(&temp_dir, &db_file); + + ledger::PublisherInfo info; + info.id = "publisher_1"; + info.verified = true; + info.excluded = ledger::PUBLISHER_EXCLUDE::DEFAULT; + info.name = "publisher1"; + info.url = "https://publisher1.com"; + info.duration = 10; + info.score = 1.1; + info.percent = 33; + info.weight = 1.5; + info.reconcile_stamp = 1; + info.visits = 1; + + EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info)); + + info.reconcile_stamp = 2; + EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info)); + + info.id = "publisher_2"; + info.name = "publisher2"; + info.url = "https://publisher2.com"; + EXPECT_TRUE(publisher_info_database_->InsertOrUpdateActivityInfo(info)); + + // publisher key is missing + EXPECT_FALSE(publisher_info_database_->DeleteActivityInfo("", 2)); + + // reconcile stamp is missing + EXPECT_FALSE(publisher_info_database_->DeleteActivityInfo("publisher_1", 0)); + + // publisher doesn't exist + EXPECT_TRUE(publisher_info_database_->DeleteActivityInfo("publisher_3", 2)); + + // publisher is deleted + EXPECT_TRUE(publisher_info_database_->DeleteActivityInfo("publisher_1", 2)); + ledger::PublisherInfoList list; + ledger::ActivityInfoFilter filter; + filter.excluded = ledger::EXCLUDE_FILTER::FILTER_ALL; + EXPECT_TRUE(publisher_info_database_->GetActivityList(0, 0, filter, &list)); + EXPECT_EQ(static_cast(list.size()), 2); + + EXPECT_EQ(list.at(0).id, "publisher_1"); + EXPECT_EQ(list.at(0).reconcile_stamp, 1u); + EXPECT_EQ(list.at(1).id, "publisher_2"); + +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 61f7051cc8cd..d19f83526519 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -1903,8 +1903,14 @@ void RewardsServiceImpl::OnExcludedSitesChanged( ledger::PUBLISHER_EXCLUDE exclude) { bool excluded = exclude == ledger::PUBLISHER_EXCLUDE::EXCLUDED; - for (auto& observer : observers_) + + if (excluded) { + DeleteActivityInfo(publisher_id); + } + + for (auto& observer : observers_) { observer.OnExcludedSitesChanged(this, publisher_id, excluded); + } } void RewardsServiceImpl::OnPanelPublisherInfo( @@ -2729,4 +2735,46 @@ void RewardsServiceImpl::GetExcludedPublishersNumberDB( callback)); } +bool DeleteActivityInfoOnFileTaskRunner(PublisherInfoDatabase* backend, + const std::string& publisher_key, + uint64_t reconcile_stamp) { + if (backend && + backend->DeleteActivityInfo(publisher_key, reconcile_stamp)) { + return true; + } + + return false; +} + +void RewardsServiceImpl::DeleteActivityInfo(const std::string& publisher_key) { + GetReconcileStamp( + base::Bind(&RewardsServiceImpl::OnDeleteActivityInfoStamp, + AsWeakPtr(), + publisher_key)); +} + +void RewardsServiceImpl::OnDeleteActivityInfoStamp( + const std::string& publisher_key, + uint64_t reconcile_stamp) { + base::PostTaskAndReplyWithResult( + file_task_runner_.get(), + FROM_HERE, + base::Bind(&DeleteActivityInfoOnFileTaskRunner, + publisher_info_backend_.get(), + publisher_key, + reconcile_stamp), + base::Bind(&RewardsServiceImpl::OnDeleteActivityInfo, + AsWeakPtr(), + publisher_key)); +} + +void RewardsServiceImpl::OnDeleteActivityInfo( + const std::string& publisher_key, + bool result) { + if (!result) { + LOG(ERROR) << "Problem deleting activity info for " + << publisher_key; + } +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 0e71ad41cc42..a5652ecc5284 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -276,6 +276,14 @@ class RewardsServiceImpl : public RewardsService, void OnDonate(const std::string& publisher_key, int amount, bool recurring, std::unique_ptr site) override; + void DeleteActivityInfo(const std::string& publisher_key); + + void OnDeleteActivityInfoStamp(const std::string& publisher_key, + uint64_t reconcile_stamp); + + void OnDeleteActivityInfo(const std::string& publisher_key, + bool result); + // ledger::LedgerClient std::string GenerateGUID() const override; void OnWalletInitialized(ledger::Result result) override;