diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 8b83ecd38f4c..dd0d4d49eecb 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -569,6 +569,10 @@ void AdsServiceImpl::OnInitialize( } void AdsServiceImpl::ShutdownBatAds() { + if (!connected()) { + return; + } + VLOG(1) << "Shutting down ads"; bat_ads_->Shutdown(base::BindOnce(&AdsServiceImpl::OnShutdownBatAds, @@ -650,10 +654,6 @@ void AdsServiceImpl::Start() { } void AdsServiceImpl::Stop() { - if (!connected()) { - return; - } - ShutdownBatAds(); } @@ -848,10 +848,14 @@ void AdsServiceImpl::OnViewAdNotification( ads::AdNotificationInfo notification; notification.FromJson(json); + OpenNewTabWithUrl(notification.target_url); + + if (!connected()) { + return; + } + bat_ads_->OnAdNotificationEvent(notification.uuid, ads::AdNotificationEventType::kClicked); - - OpenNewTabWithUrl(notification.target_url); } void AdsServiceImpl::RetryViewingAdNotification( @@ -876,6 +880,10 @@ void AdsServiceImpl::ClearAdsServiceForNotificationHandler() { void AdsServiceImpl::OpenNewTabWithUrl( const std::string& url) { + if (g_brave_browser_process->IsShuttingDown()) { + return; + } + GURL gurl(url); if (!gurl.is_valid()) { VLOG(0) << "Failed to open new tab due to invalid URL: " << url;