From ba2ea467934309332f1f2d005a8f3e582e64d0b6 Mon Sep 17 00:00:00 2001 From: Aleksei Seren Date: Fri, 25 Oct 2024 17:57:10 -0500 Subject: [PATCH] [ads] Fixes System Notification ad is clicked and dismissed at the same time on Linux --- .../notifications/ads_notification_handler.cc | 18 +++++++++++++++++- .../notifications/ads_notification_handler.h | 9 +++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/browser/notifications/ads_notification_handler.cc b/browser/notifications/ads_notification_handler.cc index 660a2fc5482c..edf57a24c79e 100644 --- a/browser/notifications/ads_notification_handler.cc +++ b/browser/notifications/ads_notification_handler.cc @@ -9,6 +9,7 @@ #include "brave/browser/brave_ads/ads_service_factory.h" #include "brave/components/brave_ads/browser/ads_service.h" +#include "build/build_config.h" #include "url/gurl.h" namespace brave_ads { @@ -28,19 +29,30 @@ void AdsNotificationHandler::OnShow(Profile* profile, const std::string& id) { return; } + did_click_notification_ad_ = false; + ads_service->OnNotificationAdShown(id); } void AdsNotificationHandler::OnClose(Profile* profile, const GURL& origin, const std::string& id, - bool by_user, + const bool by_user, base::OnceClosure completed_closure) { AdsService* ads_service = AdsServiceFactory::GetForProfile(profile); if (!ads_service) { return; } +#if BUILDFLAG(IS_LINUX) + if (did_click_notification_ad_) { + // On Linux, clicking the notification triggers both 'clicked' and 'closed' + // events. To avoid redundant event handling, we suppress the 'closed' event + // if the notification ad was clicked. + return; + } +#endif // BUILDFLAG(IS_LINUX) + ads_service->OnNotificationAdClosed(id, by_user); } @@ -55,6 +67,8 @@ void AdsNotificationHandler::OnClick(Profile* profile, return; } + did_click_notification_ad_ = true; + ads_service->OnNotificationAdClicked(id); } @@ -65,6 +79,8 @@ void AdsNotificationHandler::OpenSettings(Profile* profile, return; } + did_click_notification_ad_ = true; + CHECK(origin.has_query()); const std::string id = origin.query(); diff --git a/browser/notifications/ads_notification_handler.h b/browser/notifications/ads_notification_handler.h index 89408669267a..8559469f51c5 100644 --- a/browser/notifications/ads_notification_handler.h +++ b/browser/notifications/ads_notification_handler.h @@ -21,6 +21,12 @@ namespace brave_ads { class AdsNotificationHandler : public NotificationHandler { public: explicit AdsNotificationHandler(Profile& profile); + + AdsNotificationHandler(const AdsNotificationHandler&) = delete; + AdsNotificationHandler& operator=(const AdsNotificationHandler&) = delete; + AdsNotificationHandler(AdsNotificationHandler&&) = delete; + AdsNotificationHandler& operator=(AdsNotificationHandler&&) = delete; + ~AdsNotificationHandler() override; // NotificationHandler: @@ -41,8 +47,7 @@ class AdsNotificationHandler : public NotificationHandler { private: raw_ref profile_; - AdsNotificationHandler(const AdsNotificationHandler&) = delete; - AdsNotificationHandler& operator=(const AdsNotificationHandler&) = delete; + bool did_click_notification_ad_ = false; }; } // namespace brave_ads