Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ads] Fixes System Notification ad is clicked and dismissed at the same time on Linux #26251

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading