diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index b0334b039565..241decf1d1dd 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -350,6 +350,7 @@ void AdsServiceImpl::StartBatAdsService() { } bat_ads_service_remote_->Create( + ads_service_path_, bat_ads_client_associated_receiver_.BindNewEndpointAndPassRemote(), bat_ads_associated_remote_.BindNewEndpointAndPassReceiver(), std::move(bat_ads_client_notifier_pending_receiver_), @@ -410,18 +411,9 @@ void AdsServiceImpl::Initialize(const size_t current_start_number) { return; } - InitializeDatabase(); - InitializeRewardsWallet(current_start_number); } -void AdsServiceImpl::InitializeDatabase() { - CHECK(!database_); - - database_ = base::SequenceBound( - file_task_runner_, ads_service_path_.AppendASCII(kDatabaseFilename)); -} - void AdsServiceImpl::InitializeRewardsWallet( const size_t current_start_number) { rewards_service_observation_.GetSource()->GetRewardsWallet( @@ -1100,8 +1092,6 @@ void AdsServiceImpl::ShutdownAdsService() { CloseAdaptiveCaptcha(); - database_.Reset(); - if (is_bat_ads_initialized_) { VLOG(2) << "Shutdown Bat Ads Service"; } @@ -1759,17 +1749,6 @@ void AdsServiceImpl::ShowScheduledCaptcha(const std::string& payment_id, #endif // !BUILDFLAG(IS_ANDROID) } -void AdsServiceImpl::RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - CHECK(mojom_db_transaction); - CHECK(database_); - - database_.AsyncCall(&Database::RunDBTransaction) - .WithArgs(std::move(mojom_db_transaction)) - .Then(std::move(callback)); -} - void AdsServiceImpl::RecordP2AEvents(const std::vector& events) { for (const auto& event : events) { RecordAndEmitP2AHistogramName(prefs_, diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index d133be6bfce6..20fb14daaab2 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -19,7 +19,6 @@ #include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/task/cancelable_task_tracker.h" -#include "base/threading/sequence_bound.h" #include "base/time/time.h" #include "base/timer/timer.h" #include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h" @@ -61,7 +60,6 @@ namespace brave_ads { class AdsServiceObserver; class AdsTooltipsDelegate; class BatAdsServiceFactory; -class Database; class DeviceId; class ResourceComponent; struct NewTabPageAdInfo; @@ -127,7 +125,6 @@ class AdsServiceImpl final : public AdsService, void InitializeBasePathDirectoryCallback(size_t current_start_number, bool success); void Initialize(size_t current_start_number); - void InitializeDatabase(); void InitializeRewardsWallet(size_t current_start_number); void InitializeRewardsWalletCallback( size_t current_start_number, @@ -370,9 +367,6 @@ class AdsServiceImpl final : public AdsService, void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) override; - void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) override; - // TODO(https://github.com/brave/brave-browser/issues/14666) Decouple P2A // business logic. void RecordP2AEvents(const std::vector& events) override; @@ -440,8 +434,6 @@ class AdsServiceImpl final : public AdsService, mojom::SysInfo sys_info_; - base::SequenceBound database_; - base::RepeatingTimer idle_state_timer_; ui::IdleState last_idle_state_ = ui::IdleState::IDLE_STATE_ACTIVE; base::TimeDelta last_idle_time_; diff --git a/components/brave_ads/core/internal/BUILD.gn b/components/brave_ads/core/internal/BUILD.gn index aabac2df6166..befdda93513d 100644 --- a/components/brave_ads/core/internal/BUILD.gn +++ b/components/brave_ads/core/internal/BUILD.gn @@ -586,6 +586,7 @@ static_library("internal") { "creatives/segments_database_table.h", "creatives/segments_database_util.cc", "creatives/segments_database_util.h", + "database/background_database_handler.cc", "database/database.cc", "database/database_maintenance.cc", "database/database_maintenance.h", @@ -594,6 +595,8 @@ static_library("internal") { "database/database_manager_observer.h", "database/database_table_interface.cc", "database/database_table_interface.h", + "database/test_database_handler.cc", + "database/test_database_handler.h", "deprecated/client/client_info.cc", "deprecated/client/client_info.h", "deprecated/client/client_state_manager.cc", diff --git a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc index 90da050a848a..137c13de57a2 100644 --- a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc +++ b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc @@ -18,7 +18,6 @@ #include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_builder_util.h" #include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_util.h" #include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_confirmation_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h" @@ -33,7 +32,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -396,9 +394,8 @@ void ConfirmationQueue::GetAll(GetConfirmationQueueCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void ConfirmationQueue::GetNext(GetConfirmationQueueCallback callback) const { @@ -433,9 +430,8 @@ void ConfirmationQueue::GetNext(GetConfirmationQueueCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } std::string ConfirmationQueue::GetTableName() const { diff --git a/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc b/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc index aaad8ed72505..0a830b9e9dc3 100644 --- a/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc +++ b/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc @@ -11,7 +11,6 @@ #include "base/check.h" #include "base/functional/bind.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" @@ -19,7 +18,6 @@ #include "brave/components/brave_ads/core/internal/common/logging_util.h" #include "brave/components/brave_ads/core/internal/common/time/time_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -191,10 +189,9 @@ void Deposits::GetForCreativeInstanceId(const std::string& creative_instance_id, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void Deposits::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc b/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc index 7a0f2e3a9380..2da75e8d15fa 100644 --- a/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc +++ b/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc @@ -242,9 +242,8 @@ void Transactions::GetForDateRange(const base::Time from_time, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void Transactions::Reconcile(const PaymentTokenList& payment_tokens, diff --git a/components/brave_ads/core/internal/ads.cc b/components/brave_ads/core/internal/ads.cc index 8a23294e7054..4cabb81b1976 100644 --- a/components/brave_ads/core/internal/ads.cc +++ b/components/brave_ads/core/internal/ads.cc @@ -5,15 +5,21 @@ #include "brave/components/brave_ads/core/public/ads.h" +#include + #include "brave/components/brave_ads/core/internal/account/tokens/token_generator.h" #include "brave/components/brave_ads/core/internal/ads_impl.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" namespace brave_ads { // static -std::unique_ptr Ads::CreateInstance(AdsClient& ads_client) { +std::unique_ptr Ads::CreateInstance( + AdsClient& ads_client, + std::unique_ptr database_handler) { return std::make_unique(ads_client, - std::make_unique()); + std::make_unique(), + std::move(database_handler)); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ads_client/ads_client_mock.h b/components/brave_ads/core/internal/ads_client/ads_client_mock.h index 0d12e56d6de0..5be914de3263 100644 --- a/components/brave_ads/core/internal/ads_client/ads_client_mock.h +++ b/components/brave_ads/core/internal/ads_client/ads_client_mock.h @@ -90,10 +90,6 @@ class AdsClientMock : public AdsClient { ShowScheduledCaptcha, (const std::string& payment_id, const std::string& captcha_id)); - MOCK_METHOD(void, - RunDBTransaction, - (mojom::DBTransactionInfoPtr, RunDBTransactionCallback)); - MOCK_METHOD(void, RecordP2AEvents, (const std::vector& events)); MOCK_METHOD(bool, FindProfilePref, (const std::string& path), (const)); diff --git a/components/brave_ads/core/internal/ads_impl.cc b/components/brave_ads/core/internal/ads_impl.cc index 33349fdb58c7..1b14655a27d9 100644 --- a/components/brave_ads/core/internal/ads_impl.cc +++ b/components/brave_ads/core/internal/ads_impl.cc @@ -5,6 +5,7 @@ #include "brave/components/brave_ads/core/internal/ads_impl.h" +#include #include #include @@ -26,6 +27,7 @@ #include "brave/components/brave_ads/core/internal/legacy_migration/confirmations/legacy_confirmation_migration.h" #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_events.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" #include "brave/components/brave_ads/core/public/service/ads_service_callback.h" namespace brave_ads { @@ -41,8 +43,11 @@ void FailedToInitialize(InitializeCallback callback) { } // namespace AdsImpl::AdsImpl(AdsClient& ads_client, - std::unique_ptr token_generator) - : global_state_(ads_client, std::move(token_generator)), + std::unique_ptr token_generator, + std::unique_ptr database_handler) + : global_state_(ads_client, + std::move(token_generator), + std::move(database_handler)), database_maintenance_(std::make_unique()) {} AdsImpl::~AdsImpl() = default; diff --git a/components/brave_ads/core/internal/ads_impl.h b/components/brave_ads/core/internal/ads_impl.h index 7d9e948aa14e..bc6e1c53fcfd 100644 --- a/components/brave_ads/core/internal/ads_impl.h +++ b/components/brave_ads/core/internal/ads_impl.h @@ -32,7 +32,8 @@ class Maintenance; class AdsImpl final : public Ads { public: AdsImpl(AdsClient& ads_client, - std::unique_ptr token_generator); + std::unique_ptr token_generator, + std::unique_ptr database_handler); AdsImpl(const AdsImpl&) = delete; AdsImpl& operator=(const AdsImpl&) = delete; diff --git a/components/brave_ads/core/internal/common/database/database_transaction_util.cc b/components/brave_ads/core/internal/common/database/database_transaction_util.cc index 0f047b7ee5d3..22edbc38e5b3 100644 --- a/components/brave_ads/core/internal/common/database/database_transaction_util.cc +++ b/components/brave_ads/core/internal/common/database/database_transaction_util.cc @@ -9,9 +9,10 @@ #include "base/functional/bind.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" +#include "brave/components/brave_ads/core/internal/database/database_manager.h" +#include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" namespace brave_ads::database { @@ -43,9 +44,15 @@ bool IsError( mojom::DBTransactionResultInfo::StatusCode::kSuccess; } +void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + ::brave_ads::RunDBTransactionCallback callback) { + GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction( + std::move(mojom_db_transaction), std::move(callback)); +} + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, ResultCallback callback) { - GetAdsClient().RunDBTransaction( + GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&RunDBTransactionCallback, std::move(callback))); } diff --git a/components/brave_ads/core/internal/common/database/database_transaction_util.h b/components/brave_ads/core/internal/common/database/database_transaction_util.h index 8d34e50aabc5..ba1705afcba3 100644 --- a/components/brave_ads/core/internal/common/database/database_transaction_util.h +++ b/components/brave_ads/core/internal/common/database/database_transaction_util.h @@ -22,6 +22,11 @@ bool IsSuccess( bool IsError( const mojom::DBTransactionResultInfoPtr& mojom_db_transaction_result); +// Run a database transaction. The callback takes one argument - +// `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. +void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback); + // Run a database transaction. void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, ResultCallback callback); diff --git a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc index 8d0e92621c20..0dd65504d559 100644 --- a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc +++ b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc @@ -224,20 +224,6 @@ void MockLoadDataResource(AdsClientMock& ads_client_mock) { })); } -void MockRunDBTransaction(AdsClientMock& ads_client_mock, Database& database) { - ON_CALL(ads_client_mock, RunDBTransaction) - .WillByDefault(::testing::Invoke( - [&database](mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - CHECK(mojom_db_transaction); - - mojom::DBTransactionResultInfoPtr mojom_db_transaction_result = - database.RunDBTransaction(std::move(mojom_db_transaction)); - - std::move(callback).Run(std::move(mojom_db_transaction_result)); - })); -} - void MockFindProfilePref(const AdsClientMock& ads_client_mock) { ON_CALL(ads_client_mock, FindProfilePref) .WillByDefault(::testing::Invoke([](const std::string& path) -> bool { diff --git a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h index 9218b670affd..5909648584a9 100644 --- a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h +++ b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h @@ -37,8 +37,6 @@ void MockLoadResourceComponent(AdsClientMock& ads_client_mock, void MockLoadDataResource(AdsClientMock& ads_client_mock); -void MockRunDBTransaction(AdsClientMock& ads_client_mock, Database& database); - void MockFindProfilePref(const AdsClientMock& ads_client_mock); void MockGetProfilePref(const AdsClientMock& ads_client_mock); void MockSetProfilePref(const AdsClientMock& ads_client_mock, diff --git a/components/brave_ads/core/internal/common/test/test_base.cc b/components/brave_ads/core/internal/common/test/test_base.cc index 6642dabfa75f..7f53383622f5 100644 --- a/components/brave_ads/core/internal/common/test/test_base.cc +++ b/components/brave_ads/core/internal/common/test/test_base.cc @@ -27,12 +27,12 @@ #include "brave/components/brave_ads/core/internal/common/test/test_types.h" #include "brave/components/brave_ads/core/internal/common/test/time_test_util.h" #include "brave/components/brave_ads/core/internal/database/database_manager.h" +#include "brave/components/brave_ads/core/internal/database/test_database_handler.h" #include "brave/components/brave_ads/core/internal/deprecated/client/client_state_manager.h" #include "brave/components/brave_ads/core/internal/deprecated/confirmations/confirmation_state_manager.h" #include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/public/ads.h" #include "brave/components/brave_ads/core/public/ads_constants.h" -#include "brave/components/brave_ads/core/public/database/database.h" namespace brave_ads::test { @@ -238,10 +238,6 @@ void TestBase::MockAdsClient() { MockLoadDataResource(ads_client_mock_); - database_ = - std::make_unique(ProfilePath().AppendASCII(kDatabaseFilename)); - MockRunDBTransaction(ads_client_mock_, *database_); - MockFindProfilePref(ads_client_mock_); MockGetProfilePref(ads_client_mock_); MockSetProfilePref(ads_client_mock_, *this); @@ -302,7 +298,9 @@ void TestBase::SetUpIntegrationTest() { << "SetUpIntegrationTest should only be called if SetUp is initialized " "for integration testing"; - ads_ = Ads::CreateInstance(ads_client_mock_); + ads_ = Ads::CreateInstance(ads_client_mock_, + std::make_unique( + ProfilePath().AppendASCII(kDatabaseFilename))); CHECK(ads_) << "Failed to create ads instance"; // Must be called after `Ads` is instantiated but prior to `Initialize`. @@ -332,7 +330,9 @@ void TestBase::SetUpUnitTest() { "SetUp is initialized for unit testing"; global_state_ = std::make_unique( - ads_client_mock_, std::make_unique()); + ads_client_mock_, std::make_unique(), + std::make_unique( + ProfilePath().AppendASCII(kDatabaseFilename))); // Must be called after `GlobalState` is instantiated but prior to // `MockDefaultAdsServiceState`. diff --git a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc index 8a2e4c78bfe2..4b9e00b8ac84 100644 --- a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc +++ b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc @@ -23,7 +23,6 @@ #include "brave/components/brave_ads/core/internal/common/time/time_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -220,9 +219,8 @@ void CreativeSetConversions::GetUnexpired( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void CreativeSetConversions::GetActive( @@ -252,9 +250,8 @@ void CreativeSetConversions::GetActive( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void CreativeSetConversions::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc b/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc index 1703459c221b..16daddf03520 100644 --- a/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc @@ -14,13 +14,11 @@ #include "base/functional/bind.h" #include "base/strings/strcat.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h" #include "brave/components/brave_ads/core/internal/common/logging_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -218,10 +216,9 @@ void CreativeAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } std::string CreativeAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc index a666eb079072..46841e4908f8 100644 --- a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc @@ -15,7 +15,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -26,7 +25,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -353,10 +351,9 @@ void CreativeInlineContentAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativeInlineContentAds::GetForSegmentsAndDimensions( @@ -425,10 +422,9 @@ void CreativeInlineContentAds::GetForSegmentsAndDimensions( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForSegmentsAndDimensionsCallback, segments, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForSegmentsAndDimensionsCallback, + segments, std::move(callback))); } void CreativeInlineContentAds::GetForDimensions( @@ -486,7 +482,7 @@ void CreativeInlineContentAds::GetForDimensions( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForDimensionsCallback, std::move(callback))); } @@ -539,9 +535,8 @@ void CreativeInlineContentAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeInlineContentAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc index b8d0c048c44e..6524ae164563 100644 --- a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc @@ -15,7 +15,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_split.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -26,7 +25,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/serving/targeting/condition_matcher/condition_matcher_util.h" #include "url/gurl.h" @@ -391,10 +389,9 @@ void CreativeNewTabPageAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativeNewTabPageAds::GetForSegments( @@ -464,7 +461,7 @@ void CreativeNewTabPageAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -520,9 +517,8 @@ void CreativeNewTabPageAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeNewTabPageAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc index b8b4b8059a1f..a324c78a740b 100644 --- a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc @@ -14,7 +14,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -25,7 +24,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -310,7 +308,7 @@ void CreativeNotificationAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -360,9 +358,8 @@ void CreativeNotificationAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeNotificationAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc index 6d79890904f9..249733e24a36 100644 --- a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc @@ -14,7 +14,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -25,7 +24,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -326,10 +324,9 @@ void CreativePromotedContentAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativePromotedContentAds::GetForSegments( @@ -394,7 +391,7 @@ void CreativePromotedContentAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -444,9 +441,8 @@ void CreativePromotedContentAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativePromotedContentAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/database/background_database_handler.cc b/components/brave_ads/core/internal/database/background_database_handler.cc new file mode 100644 index 000000000000..5489c2b0eade --- /dev/null +++ b/components/brave_ads/core/internal/database/background_database_handler.cc @@ -0,0 +1,39 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/core/public/database/background_database_handler.h" + +#include + +#include "base/check.h" +#include "base/task/task_traits.h" +#include "base/task/thread_pool.h" +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" +#include "brave/components/brave_ads/core/public/database/database.h" + +namespace brave_ads { + +BackgroundDatabaseHandler::BackgroundDatabaseHandler( + const base::FilePath& database_path) + : database_task_runner_(base::ThreadPool::CreateSequencedTaskRunner( + {base::MayBlock(), base::TaskPriority::BEST_EFFORT, + base::TaskShutdownBehavior::BLOCK_SHUTDOWN})), + database_(base::SequenceBound(database_task_runner_, + database_path)) {} + +BackgroundDatabaseHandler::~BackgroundDatabaseHandler() = default; + +void BackgroundDatabaseHandler::RunDBTransaction( + mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) { + CHECK(mojom_db_transaction); + CHECK(database_); + + database_.AsyncCall(&Database::RunDBTransaction) + .WithArgs(std::move(mojom_db_transaction)) + .Then(std::move(callback)); +} + +} // namespace brave_ads diff --git a/components/brave_ads/core/internal/database/database_manager.cc b/components/brave_ads/core/internal/database/database_manager.cc index 1c9e2ea66d29..f0e6ae5ecbe3 100644 --- a/components/brave_ads/core/internal/database/database_manager.cc +++ b/components/brave_ads/core/internal/database/database_manager.cc @@ -5,12 +5,12 @@ #include "brave/components/brave_ads/core/internal/database/database_manager.h" +#include #include #include "base/check_op.h" #include "base/debug/dump_without_crashing.h" #include "base/functional/bind.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h" #include "brave/components/brave_ads/core/internal/common/logging_util.h" #include "brave/components/brave_ads/core/internal/global_state/global_state.h" @@ -19,11 +19,13 @@ #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_migration.h" #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_raze.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" namespace brave_ads { -DatabaseManager::DatabaseManager() = default; +DatabaseManager::DatabaseManager( + std::unique_ptr database_handler) + : database_handler_(std::move(database_handler)) {} DatabaseManager::~DatabaseManager() = default; @@ -53,12 +55,22 @@ void DatabaseManager::CreateOrOpen(ResultCallback callback) { mojom_db_action->type = mojom::DBActionInfo::Type::kInitialize; mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&DatabaseManager::CreateOrOpenCallback, weak_factory_.GetWeakPtr(), std::move(callback))); } +void DatabaseManager::RunDBTransaction( + mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) { + CHECK(mojom_db_transaction); + CHECK(database_handler_); + + database_handler_->RunDBTransaction(std::move(mojom_db_transaction), + std::move(callback)); +} + /////////////////////////////////////////////////////////////////////////////// void DatabaseManager::CreateOrOpenCallback( diff --git a/components/brave_ads/core/internal/database/database_manager.h b/components/brave_ads/core/internal/database/database_manager.h index a2f33715739f..9f8fc4fed20e 100644 --- a/components/brave_ads/core/internal/database/database_manager.h +++ b/components/brave_ads/core/internal/database/database_manager.h @@ -6,16 +6,22 @@ #ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_MANAGER_H_ #define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_MANAGER_H_ +#include + #include "base/memory/weak_ptr.h" #include "base/observer_list.h" #include "brave/components/brave_ads/core/internal/database/database_manager_observer.h" +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" namespace brave_ads { +class DatabaseHandlerInterface; + class DatabaseManager final { public: - DatabaseManager(); + explicit DatabaseManager( + std::unique_ptr database_handler); DatabaseManager(const DatabaseManager&) = delete; DatabaseManager& operator=(const DatabaseManager&) = delete; @@ -33,6 +39,11 @@ class DatabaseManager final { // Create or open the database. void CreateOrOpen(ResultCallback callback); + // Run a database transaction. The callback takes one argument - + // `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback); + private: void CreateOrOpenCallback( ResultCallback callback, @@ -63,6 +74,8 @@ class DatabaseManager final { void NotifyFailedToMigrateDatabase(int from_version, int to_version) const; void NotifyDatabaseIsReady() const; + const std::unique_ptr database_handler_; + base::ObserverList observers_; base::WeakPtrFactory weak_factory_{this}; diff --git a/components/brave_ads/core/internal/database/test_database_handler.cc b/components/brave_ads/core/internal/database/test_database_handler.cc new file mode 100644 index 000000000000..2111843b03c6 --- /dev/null +++ b/components/brave_ads/core/internal/database/test_database_handler.cc @@ -0,0 +1,32 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/core/internal/database/test_database_handler.h" + +#include + +#include "base/check.h" +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" +#include "brave/components/brave_ads/core/public/database/database.h" + +namespace brave_ads::test { + +TestDatabaseHandler::TestDatabaseHandler(const base::FilePath& database_path) + : database_(database_path) {} + +TestDatabaseHandler::~TestDatabaseHandler() = default; + +void TestDatabaseHandler::RunDBTransaction( + mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) { + CHECK(mojom_db_transaction); + + mojom::DBTransactionResultInfoPtr mojom_db_transaction_result = + database_.RunDBTransaction(std::move(mojom_db_transaction)); + + std::move(callback).Run(std::move(mojom_db_transaction_result)); +} + +} // namespace brave_ads::test diff --git a/components/brave_ads/core/internal/database/test_database_handler.h b/components/brave_ads/core/internal/database/test_database_handler.h new file mode 100644 index 000000000000..47906a365da8 --- /dev/null +++ b/components/brave_ads/core/internal/database/test_database_handler.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_TEST_DATABASE_HANDLER_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_TEST_DATABASE_HANDLER_H_ + +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" +#include "brave/components/brave_ads/core/public/database/database.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" + +namespace brave_ads::test { + +class TestDatabaseHandler : public DatabaseHandlerInterface { + public: + explicit TestDatabaseHandler(const base::FilePath& database_path); + + TestDatabaseHandler(const TestDatabaseHandler&) = delete; + TestDatabaseHandler& operator=(const TestDatabaseHandler&) = delete; + + TestDatabaseHandler(TestDatabaseHandler&&) noexcept = delete; + TestDatabaseHandler& operator=(TestDatabaseHandler&&) noexcept = delete; + + ~TestDatabaseHandler() override; + + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) override; + + private: + Database database_; +}; + +} // namespace brave_ads::test + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_TEST_DATABASE_HANDLER_H_ diff --git a/components/brave_ads/core/internal/global_state/global_state.cc b/components/brave_ads/core/internal/global_state/global_state.cc index 57f93ceb808b..64a4b55bad22 100644 --- a/components/brave_ads/core/internal/global_state/global_state.cc +++ b/components/brave_ads/core/internal/global_state/global_state.cc @@ -23,19 +23,22 @@ #include "brave/components/brave_ads/core/internal/tabs/tab_manager.h" #include "brave/components/brave_ads/core/internal/user_attention/user_activity/user_activity_manager.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" namespace brave_ads { GlobalState::GlobalState( AdsClient& ads_client, - std::unique_ptr token_generator) + std::unique_ptr token_generator, + std::unique_ptr database_handler) : ads_client_(ads_client), global_state_holder_(std::make_unique(this)) { ads_notifier_manager_ = std::make_unique(); browser_manager_ = std::make_unique(); client_state_manager_ = std::make_unique(); confirmation_state_manager_ = std::make_unique(); - database_manager_ = std::make_unique(); + database_manager_ = + std::make_unique(std::move(database_handler)); diagnostic_manager_ = std::make_unique(); ad_history_manager_ = std::make_unique(); notification_ad_manager_ = std::make_unique(); diff --git a/components/brave_ads/core/internal/global_state/global_state.h b/components/brave_ads/core/internal/global_state/global_state.h index 09cf350da87b..1f2cfbb30f08 100644 --- a/components/brave_ads/core/internal/global_state/global_state.h +++ b/components/brave_ads/core/internal/global_state/global_state.h @@ -25,6 +25,7 @@ class AdsNotifierManager; class BrowserManager; class ClientStateManager; class ConfirmationStateManager; +class DatabaseHandlerInterface; class DatabaseManager; class DiagnosticManager; class GlobalStateHolder; @@ -35,9 +36,9 @@ class UserActivityManager; class GlobalState final { public: - explicit GlobalState( - AdsClient& ads_client, - std::unique_ptr token_generator); + GlobalState(AdsClient& ads_client, + std::unique_ptr token_generator, + std::unique_ptr database_handler); GlobalState(const GlobalState& other) = delete; GlobalState& operator=(const GlobalState& other) = delete; diff --git a/components/brave_ads/core/internal/history/ad_history_database_table.cc b/components/brave_ads/core/internal/history/ad_history_database_table.cc index e0a20250fc6f..e15786251275 100644 --- a/components/brave_ads/core/internal/history/ad_history_database_table.cc +++ b/components/brave_ads/core/internal/history/ad_history_database_table.cc @@ -12,7 +12,6 @@ #include "base/debug/dump_without_crashing.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -23,7 +22,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/history/ad_history_feature.h" namespace brave_ads::database::table { @@ -265,9 +263,8 @@ void AdHistory::GetForDateRange(const base::Time from_time, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::GetHighestRankedPlacementsForDateRange( @@ -369,9 +366,8 @@ void AdHistory::GetHighestRankedPlacementsForDateRange( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::GetForCreativeInstanceId( @@ -404,9 +400,8 @@ void AdHistory::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc index ad3fb277783a..c7bbd4ad11dc 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc @@ -11,7 +11,6 @@ #include "base/check.h" #include "base/functional/bind.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" @@ -22,7 +21,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -205,9 +203,8 @@ void AdEvents::GetAll(GetAdEventsCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::GetUnexpired(GetAdEventsCallback callback) const { @@ -245,9 +242,8 @@ void AdEvents::GetUnexpired(GetAdEventsCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::GetUnexpired(const mojom::AdType mojom_ad_type, @@ -289,9 +285,8 @@ void AdEvents::GetUnexpired(const mojom::AdType mojom_ad_type, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/public/BUILD.gn b/components/brave_ads/core/public/BUILD.gn index 56182e38b5a9..56a21fc1600d 100644 --- a/components/brave_ads/core/public/BUILD.gn +++ b/components/brave_ads/core/public/BUILD.gn @@ -31,7 +31,9 @@ source_set("headers") { "ads_feature.h", "ads_observer_interface.h", "ads_util.h", + "database/background_database_handler.h", "database/database.h", + "database/database_handler_interface.h", "export.h", "flags/flags_util.h", "history/ad_history_feature.h", diff --git a/components/brave_ads/core/public/ads.h b/components/brave_ads/core/public/ads.h index c7fecc71668a..8ee23b7e2144 100644 --- a/components/brave_ads/core/public/ads.h +++ b/components/brave_ads/core/public/ads.h @@ -22,6 +22,7 @@ class Time; namespace brave_ads { class AdsClient; +class DatabaseHandlerInterface; struct NotificationAdInfo; class ADS_EXPORT Ads { @@ -36,7 +37,9 @@ class ADS_EXPORT Ads { virtual ~Ads() = default; - static std::unique_ptr CreateInstance(AdsClient& ads_client); + static std::unique_ptr CreateInstance( + AdsClient& ads_client, + std::unique_ptr database_handler); virtual void AddObserver( std::unique_ptr ads_observer) = 0; diff --git a/components/brave_ads/core/public/ads_client/ads_client.h b/components/brave_ads/core/public/ads_client/ads_client.h index 8b502d425483..9fb73c92d585 100644 --- a/components/brave_ads/core/public/ads_client/ads_client.h +++ b/components/brave_ads/core/public/ads_client/ads_client.h @@ -115,12 +115,6 @@ class ADS_EXPORT AdsClient { virtual void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) = 0; - // Run a database transaction. The callback takes one argument - - // `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. - virtual void RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) = 0; - // Record P2A (Private Advertising Analytics) `events`. virtual void RecordP2AEvents(const std::vector& events) = 0; diff --git a/components/brave_ads/core/public/database/background_database_handler.h b/components/brave_ads/core/public/database/background_database_handler.h new file mode 100644 index 000000000000..9e388ba99d5f --- /dev/null +++ b/components/brave_ads/core/public/database/background_database_handler.h @@ -0,0 +1,45 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_BACKGROUND_DATABASE_HANDLER_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_BACKGROUND_DATABASE_HANDLER_H_ + +#include "base/task/sequenced_task_runner.h" +#include "base/threading/sequence_bound.h" +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" +#include "brave/components/brave_ads/core/public/database/database.h" +#include "brave/components/brave_ads/core/public/database/database_handler_interface.h" + +namespace brave_ads { + +class Database; + +class BackgroundDatabaseHandler : public DatabaseHandlerInterface { + public: + explicit BackgroundDatabaseHandler(const base::FilePath& database_path); + + BackgroundDatabaseHandler(const BackgroundDatabaseHandler&) = delete; + BackgroundDatabaseHandler& operator=(const BackgroundDatabaseHandler&) = + delete; + + BackgroundDatabaseHandler(BackgroundDatabaseHandler&&) noexcept = delete; + BackgroundDatabaseHandler& operator=(BackgroundDatabaseHandler&&) noexcept = + delete; + + ~BackgroundDatabaseHandler() override; + + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) override; + + private: + const scoped_refptr database_task_runner_; + + const base::SequenceBound database_; +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_BACKGROUND_DATABASE_HANDLER_H_ diff --git a/components/brave_ads/core/public/database/database_handler_interface.h b/components/brave_ads/core/public/database/database_handler_interface.h new file mode 100644 index 000000000000..f0034622ad74 --- /dev/null +++ b/components/brave_ads/core/public/database/database_handler_interface.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_HANDLER_INTERFACE_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_HANDLER_INTERFACE_H_ + +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" + +namespace brave_ads { + +class DatabaseHandlerInterface { + public: + virtual ~DatabaseHandlerInterface() = default; + + virtual void RunDBTransaction( + mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) = 0; +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_HANDLER_INTERFACE_H_ diff --git a/components/services/bat_ads/bat_ads_client_mojo_bridge.cc b/components/services/bat_ads/bat_ads_client_mojo_bridge.cc index 2369ebb1a513..c5e29f17f380 100644 --- a/components/services/bat_ads/bat_ads_client_mojo_bridge.cc +++ b/components/services/bat_ads/bat_ads_client_mojo_bridge.cc @@ -241,18 +241,6 @@ std::string BatAdsClientMojoBridge::LoadDataResource(const std::string& name) { return value; } -void BatAdsClientMojoBridge::RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) { - if (!bat_ads_client_associated_remote_.is_bound()) { - std::move(callback).Run(brave_ads::mojom::DBTransactionResultInfoPtr()); - return; - } - - bat_ads_client_associated_remote_->RunDBTransaction( - std::move(mojom_db_transaction), std::move(callback)); -} - void BatAdsClientMojoBridge::ShowScheduledCaptcha( const std::string& payment_id, const std::string& captcha_id) { diff --git a/components/services/bat_ads/bat_ads_client_mojo_bridge.h b/components/services/bat_ads/bat_ads_client_mojo_bridge.h index 49ebc02c5a86..0ccc3c662636 100644 --- a/components/services/bat_ads/bat_ads_client_mojo_bridge.h +++ b/components/services/bat_ads/bat_ads_client_mojo_bridge.h @@ -94,10 +94,6 @@ class BatAdsClientMojoBridge : public brave_ads::AdsClient { void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) override; - void RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) override; - void RecordP2AEvents(const std::vector& events) override; bool FindProfilePref(const std::string& path) const override; diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 26f4e14b5ab2..91531fb85cc0 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -18,7 +18,9 @@ #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_info.h" #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_value_util.h" #include "brave/components/brave_ads/core/public/ads.h" +#include "brave/components/brave_ads/core/public/ads_constants.h" #include "brave/components/brave_ads/core/public/ads_observer_interface.h" +#include "brave/components/brave_ads/core/public/database/background_database_handler.h" #include "brave/components/services/bat_ads/bat_ads_client_mojo_bridge.h" #include "brave/components/services/bat_ads/bat_ads_observer.h" @@ -27,13 +29,17 @@ namespace bat_ads { class BatAdsImpl::AdsInstance final { public: AdsInstance( + const base::FilePath& service_path, mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver client_notifier) : bat_ads_client_mojo_proxy_(std::make_unique( std::move(bat_ads_client_pending_associated_remote), std::move(client_notifier))), - ads_(brave_ads::Ads::CreateInstance(*bat_ads_client_mojo_proxy_)) {} + ads_(brave_ads::Ads::CreateInstance( + *bat_ads_client_mojo_proxy_, + std::make_unique( + service_path.AppendASCII(brave_ads::kDatabaseFilename)))) {} AdsInstance(const AdsInstance&) = delete; AdsInstance& operator=(const AdsInstance&) = delete; @@ -50,12 +56,14 @@ class BatAdsImpl::AdsInstance final { std::unique_ptr ads_; }; -BatAdsImpl::BatAdsImpl(mojo::PendingAssociatedRemote +BatAdsImpl::BatAdsImpl(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver bat_ads_client_notifier_pending_receiver) : ads_instance_(std::unique_ptr( - new AdsInstance(std::move(bat_ads_client_pending_associated_remote), + new AdsInstance(service_path, + std::move(bat_ads_client_pending_associated_remote), std::move(bat_ads_client_notifier_pending_receiver)), base::OnTaskRunnerDeleter( base::SequencedTaskRunner::GetCurrentDefault()))) {} diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index 2fbb639cf2c1..502d27f25113 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -25,7 +25,8 @@ class BatAdsClientMojoBridge; class BatAdsImpl : public mojom::BatAds { public: - BatAdsImpl(mojo::PendingAssociatedRemote + BatAdsImpl(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver bat_ads_client_notifier_pending_receiver); diff --git a/components/services/bat_ads/bat_ads_service_impl.cc b/components/services/bat_ads/bat_ads_service_impl.cc index e583ec2572a6..0faf10cfd212 100644 --- a/components/services/bat_ads/bat_ads_service_impl.cc +++ b/components/services/bat_ads/bat_ads_service_impl.cc @@ -8,6 +8,7 @@ #include #include +#include "base/files/file_path.h" #include "brave/components/services/bat_ads/bat_ads_impl.h" #include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h" #include "mojo/public/cpp/bindings/sync_call_restrictions.h" @@ -33,6 +34,7 @@ BatAdsServiceImpl::BatAdsServiceImpl(mojo::PendingReceiver BatAdsServiceImpl::~BatAdsServiceImpl() = default; void BatAdsServiceImpl::Create( + const base::FilePath& service_path, mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingAssociatedReceiver @@ -44,7 +46,7 @@ void BatAdsServiceImpl::Create( bat_ads_associated_receivers_.Add( std::make_unique( - std::move(bat_ads_client_pending_associated_remote), + service_path, std::move(bat_ads_client_pending_associated_remote), std::move(bat_ads_client_notifier_pending_receiver)), std::move(bat_ads_pending_associated_receiver)); diff --git a/components/services/bat_ads/bat_ads_service_impl.h b/components/services/bat_ads/bat_ads_service_impl.h index bc530fbf5907..bec5e2fc5486 100644 --- a/components/services/bat_ads/bat_ads_service_impl.h +++ b/components/services/bat_ads/bat_ads_service_impl.h @@ -35,7 +35,8 @@ class BatAdsServiceImpl : public mojom::BatAdsService { ~BatAdsServiceImpl() override; // BatAdsService: - void Create(mojo::PendingAssociatedRemote + void Create(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingAssociatedReceiver bat_ads_pending_associated_receiver, diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index 3ced4e7089ef..331fd69815cf 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -7,12 +7,14 @@ module bat_ads.mojom; import "brave/components/brave_ads/core/mojom/brave_ads.mojom"; import "mojo/public/mojom/base/big_string.mojom"; import "mojo/public/mojom/base/file.mojom"; +import "mojo/public/mojom/base/file_path.mojom"; import "mojo/public/mojom/base/time.mojom"; import "mojo/public/mojom/base/values.mojom"; import "url/mojom/url.mojom"; interface BatAdsService { - Create(pending_associated_remote + Create(mojo_base.mojom.FilePath service_path, + pending_associated_remote bat_ads_client_pending_associated_remote, pending_associated_receiver bat_ads_pending_associated_receiver, @@ -111,9 +113,6 @@ interface BatAdsClient { ShowScheduledCaptcha(string payment_id, string captcha_id); - RunDBTransaction(brave_ads.mojom.DBTransactionInfo mojom_db_transaction) => - (brave_ads.mojom.DBTransactionResultInfo mojom_db_transaction_result); - RecordP2AEvents(array events); [Sync] diff --git a/ios/browser/api/ads/ads_client_bridge.h b/ios/browser/api/ads/ads_client_bridge.h index f559ff705e09..5bdbfc843412 100644 --- a/ios/browser/api/ads/ads_client_bridge.h +++ b/ios/browser/api/ads/ads_client_bridge.h @@ -59,9 +59,6 @@ - (void)resetAdEventCacheForInstanceId:(const std::string&)id; - (void)UrlRequest:(brave_ads::mojom::UrlRequestInfoPtr)url_request callback:(brave_ads::UrlRequestCallback)callback; -- (void)runDBTransaction: - (brave_ads::mojom::DBTransactionInfoPtr)mojom_db_transaction - callback:(brave_ads::RunDBTransactionCallback)callback; - (void)setProfilePref:(const std::string&)path value:(base::Value)value; - (bool)findProfilePref:(const std::string&)path; - (std::optional)getProfilePref:(const std::string&)path; diff --git a/ios/browser/api/ads/ads_client_ios.h b/ios/browser/api/ads/ads_client_ios.h index 381a74ec3cc9..c9c87b7914a9 100644 --- a/ios/browser/api/ads/ads_client_ios.h +++ b/ios/browser/api/ads/ads_client_ios.h @@ -64,9 +64,6 @@ class AdsClientIOS : public brave_ads::AdsClient { const int line, const int verbose_level, const std::string& message) override; - void RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) override; void SetProfilePref(const std::string& path, base::Value value) override; bool FindProfilePref(const std::string& path) const override; std::optional GetProfilePref(const std::string& path) override; diff --git a/ios/browser/api/ads/ads_client_ios.mm b/ios/browser/api/ads/ads_client_ios.mm index 7eddfc0d0b8a..87b7c1073162 100644 --- a/ios/browser/api/ads/ads_client_ios.mm +++ b/ios/browser/api/ads/ads_client_ios.mm @@ -135,13 +135,6 @@ [bridge_ log:file line:line verboseLevel:verbose_level message:message]; } -void AdsClientIOS::RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) { - [bridge_ runDBTransaction:std::move(mojom_db_transaction) - callback:std::move(callback)]; -} - void AdsClientIOS::SetProfilePref(const std::string& path, base::Value value) { [bridge_ setProfilePref:path value:std::move(value)]; } diff --git a/ios/browser/api/ads/brave_ads.mm b/ios/browser/api/ads/brave_ads.mm index 2e79d72d853b..e1292b051fee 100644 --- a/ios/browser/api/ads/brave_ads.mm +++ b/ios/browser/api/ads/brave_ads.mm @@ -1407,25 +1407,6 @@ - (void)showScheduledCaptcha:(const std::string&)payment_id captchaId:base::SysUTF8ToNSString(captcha_id)]; } -- (void)runDBTransaction: - (brave_ads::mojom::DBTransactionInfoPtr)mojom_db_transaction - callback:(brave_ads::RunDBTransactionCallback)completion { - if (![self isServiceRunning]) { - return std::move(completion) - .Run(brave_ads::mojom::DBTransactionResultInfo::New()); - } - - adsService->RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce( - ^(brave_ads::RunDBTransactionCallback callback, - brave_ads::mojom::DBTransactionResultInfoPtr - mojom_db_transaction_result) { - std::move(callback).Run(std::move(mojom_db_transaction_result)); - }, - std::move(completion))); -} - - (void)recordP2AEvents:(const std::vector&)events { // TODO(https://github.com/brave/brave-browser/issues/33786): Unify Brave Ads // P3A analytics. diff --git a/ios/browser/brave_ads/ads_service_impl_ios.h b/ios/browser/brave_ads/ads_service_impl_ios.h index a60310981de4..3b828d2a131f 100644 --- a/ios/browser/brave_ads/ads_service_impl_ios.h +++ b/ios/browser/brave_ads/ads_service_impl_ios.h @@ -11,7 +11,6 @@ #include #include "base/memory/weak_ptr.h" -#include "base/threading/sequence_bound.h" #include "brave/components/brave_ads/core/browser/service/ads_service.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "brave/components/brave_ads/core/public/ads_callback.h" @@ -28,7 +27,6 @@ namespace brave_ads { class Ads; class AdsClient; -class Database; class AdsServiceImplIOS : public AdsService { public: @@ -166,7 +164,6 @@ class AdsServiceImplIOS : public AdsService { void InitializeAds(InitializeCallback callback); void InitializeAdsCallback(InitializeCallback callback, bool success); - void InitializeDatabase(); void ShutdownAdsCallback(ShutdownCallback callback, bool success); @@ -175,7 +172,7 @@ class AdsServiceImplIOS : public AdsService { const raw_ptr prefs_ = nullptr; // Not owned. - const scoped_refptr database_queue_; + const scoped_refptr file_task_runner_; base::FilePath storage_path_; std::unique_ptr ads_client_; @@ -183,8 +180,6 @@ class AdsServiceImplIOS : public AdsService { mojom::BuildChannelInfoPtr mojom_build_channel_; mojom::WalletInfoPtr mojom_wallet_; - base::SequenceBound database_; - std::unique_ptr ads_; base::WeakPtrFactory weak_ptr_factory_{this}; diff --git a/ios/browser/brave_ads/ads_service_impl_ios.mm b/ios/browser/brave_ads/ads_service_impl_ios.mm index f30dabbac47a..7ba1cc3edc15 100644 --- a/ios/browser/brave_ads/ads_service_impl_ios.mm +++ b/ios/browser/brave_ads/ads_service_impl_ios.mm @@ -23,7 +23,7 @@ #include "brave/components/brave_ads/core/public/ads.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/ads_constants.h" -#include "brave/components/brave_ads/core/public/database/database.h" +#include "brave/components/brave_ads/core/public/database/background_database_handler.h" #include "brave/components/brave_ads/core/public/flags/flags_util.h" #include "components/prefs/pref_service.h" #include "sql/database.h" @@ -40,7 +40,7 @@ AdsServiceImplIOS::AdsServiceImplIOS(PrefService* prefs) : AdsService(/*delegate=*/nullptr), prefs_(prefs), - database_queue_(base::ThreadPool::CreateSequencedTaskRunner( + file_task_runner_(base::ThreadPool::CreateSequencedTaskRunner( {base::MayBlock(), base::TaskPriority::USER_VISIBLE, base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) { CHECK(prefs_); @@ -82,14 +82,6 @@ std::move(callback))); } -void AdsServiceImplIOS::RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - database_.AsyncCall(&brave_ads::Database::RunDBTransaction) - .WithArgs(std::move(mojom_db_transaction)) - .Then(std::move(callback)); -} - void AdsServiceImplIOS::MaybeGetNotificationAd( const std::string& placement_id, MaybeGetNotificationAdCallback callback) { @@ -442,15 +434,14 @@ void AdsServiceImplIOS::Shutdown() { ads_.reset(); - database_.Reset(); } void AdsServiceImplIOS::InitializeAds(InitializeCallback callback) { CHECK(!IsInitialized()); - InitializeDatabase(); - - ads_ = Ads::CreateInstance(*ads_client_); + ads_ = Ads::CreateInstance( + *ads_client_, std::make_unique( + storage_path_.AppendASCII(kAdsDatabaseFilename))); ads_->SetSysInfo(mojom_sys_info_.Clone()); ads_->SetBuildChannel(mojom_build_channel_.Clone()); @@ -471,12 +462,6 @@ std::move(callback).Run(success); } -void AdsServiceImplIOS::InitializeDatabase() { - database_ = base::SequenceBound( - database_queue_, - base::FilePath(storage_path_.Append(kAdsDatabaseFilename))); -} - void AdsServiceImplIOS::ShutdownAdsCallback(ShutdownCallback callback, const bool success) { Shutdown(); @@ -495,7 +480,7 @@ return std::move(callback).Run(/*success=*/false); } - database_queue_->PostTaskAndReply( + file_task_runner_->PostTaskAndReply( FROM_HERE, base::BindOnce( [](const base::FilePath& storage_path) {