diff --git a/browser/notifications/ads_notification_handler.cc b/browser/notifications/ads_notification_handler.cc index 660a2fc5482c..24b801c5a8f4 100644 --- a/browser/notifications/ads_notification_handler.cc +++ b/browser/notifications/ads_notification_handler.cc @@ -7,8 +7,10 @@ #include +#include "base/functional/callback.h" #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 +30,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 +68,8 @@ void AdsNotificationHandler::OnClick(Profile* profile, return; } + did_click_notification_ad_ = true; + ads_service->OnNotificationAdClicked(id); } @@ -65,6 +80,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