Skip to content

Commit

Permalink
[ads] Fixes System Notification ad is clicked and dismissed at the sa…
Browse files Browse the repository at this point in the history
…me time on Linux
  • Loading branch information
aseren committed Oct 25, 2024
1 parent 144493f commit 4acb03e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 18 additions & 1 deletion browser/notifications/ads_notification_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <optional>

#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 {
Expand All @@ -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);
}

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

did_click_notification_ad_ = true;

ads_service->OnNotificationAdClicked(id);
}

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

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 4acb03e

Please sign in to comment.