From 01966ece5b9d93a7388ff89e02331a0f9a8af649 Mon Sep 17 00:00:00 2001 From: bridiver Date: Thu, 20 Dec 2018 19:10:06 -0700 Subject: [PATCH] ads should timeout after 2 min in both banner and alert style notification preferences fix https://github.com/brave/brave-browser/issues/2681 --- .../brave_ads/browser/ad_notification.cc | 5 +++ .../brave_ads/browser/ads_service_impl.cc | 37 ++++++++++++++++--- .../brave_ads/browser/ads_service_impl.h | 4 ++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/components/brave_ads/browser/ad_notification.cc b/components/brave_ads/browser/ad_notification.cc index c8ce82451096..785f76a6b80d 100644 --- a/components/brave_ads/browser/ad_notification.cc +++ b/components/brave_ads/browser/ad_notification.cc @@ -52,6 +52,11 @@ std::unique_ptr CreateAdNotification( kNotifierId), notification_data, nullptr); +#if !defined(OS_MACOSX) || defined(OFFICIAL_BUILD) + // set_never_timeout uses an XPC service which requires signing + // so for now we don't set this for macos dev builds + notification->set_never_timeout(true); +#endif return notification; } diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 47f9b665b238..2e6ee8e1d60f 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -559,6 +559,25 @@ void AdsServiceImpl::ShowNotification( display_service_->Display(NotificationHandler::Type::BRAVE_ADS, *notification); + + uint32_t timer_id = next_timer_id(); + + timers_[timer_id] = std::make_unique(); + timers_[timer_id]->Start(FROM_HERE, + base::TimeDelta::FromSeconds(120), + base::BindOnce( + &AdsServiceImpl::NotificationTimedOut, AsWeakPtr(), + timer_id, notification_id)); +} + +void AdsServiceImpl::NotificationTimedOut(uint32_t timer_id, + const std::string& notification_id) { + timers_.erase(timer_id); + if (notification_ids_.find(notification_id) != notification_ids_.end()) { + display_service_->Close(NotificationHandler::Type::BRAVE_ADS, + notification_id); + OnClose(profile_, GURL(), notification_id, false, base::OnceClosure()); + } } void AdsServiceImpl::Save(const std::string& name, @@ -724,7 +743,8 @@ void AdsServiceImpl::OnClose(Profile* profile, } } - std::move(completed_closure).Run(); + if (completed_closure) + std::move(completed_closure).Run(); } void AdsServiceImpl::OpenSettings(Profile* profile, @@ -938,19 +958,24 @@ void AdsServiceImpl::EventLog(const std::string& json) { VLOG(0) << "AdsService Event Log: " << json; } -uint32_t AdsServiceImpl::SetTimer(const uint64_t time_offset) { +uint32_t AdsServiceImpl::next_timer_id() { if (next_timer_id_ == std::numeric_limits::max()) next_timer_id_ = 1; else ++next_timer_id_; + return next_timer_id_; +} + +uint32_t AdsServiceImpl::SetTimer(const uint64_t time_offset) { + uint32_t timer_id = next_timer_id(); - timers_[next_timer_id_] = std::make_unique(); - timers_[next_timer_id_]->Start(FROM_HERE, + timers_[timer_id] = std::make_unique(); + timers_[timer_id]->Start(FROM_HERE, base::TimeDelta::FromSeconds(time_offset), base::BindOnce( - &AdsServiceImpl::OnTimer, AsWeakPtr(), next_timer_id_)); + &AdsServiceImpl::OnTimer, AsWeakPtr(), timer_id)); - return next_timer_id_; + return timer_id; } void AdsServiceImpl::KillTimer(uint32_t timer_id) { diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 88dffa57ba74..90445b5496b6 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -170,6 +170,10 @@ class AdsServiceImpl : public AdsService, void OnCreate(); void OnInitialize(); void MaybeStart(bool restart); + void NotificationTimedOut(uint32_t timer_id, + const std::string& notification_id); + + uint32_t next_timer_id(); // are we still connected to the ads lib bool connected();