Skip to content

Commit

Permalink
Merge pull request #26251 from brave/issues/41101
Browse files Browse the repository at this point in the history
[ads] Fixes System Notification ad is clicked and dismissed at the same time on Linux
  • Loading branch information
aseren authored Oct 26, 2024
2 parents 6d24e98 + ba2ea46 commit 25a2f70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 17 additions & 1 deletion browser/notifications/ads_notification_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

Expand All @@ -55,6 +67,8 @@ void AdsNotificationHandler::OnClick(Profile* profile,
return;
}

did_click_notification_ad_ = true;

ads_service->OnNotificationAdClicked(id);
}

Expand All @@ -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();

Expand Down
9 changes: 7 additions & 2 deletions browser/notifications/ads_notification_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -41,8 +47,7 @@ class AdsNotificationHandler : public NotificationHandler {
private:
raw_ref<Profile> profile_;

AdsNotificationHandler(const AdsNotificationHandler&) = delete;
AdsNotificationHandler& operator=(const AdsNotificationHandler&) = delete;
bool did_click_notification_ad_ = false;
};

} // namespace brave_ads
Expand Down

0 comments on commit 25a2f70

Please sign in to comment.