Skip to content

Commit

Permalink
ads should timeout after 2 min in both banner and alert style notific…
Browse files Browse the repository at this point in the history
…ation preferences

fix brave/brave-browser#2681
  • Loading branch information
bridiver committed Dec 21, 2018
1 parent ed5c182 commit 01966ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions components/brave_ads/browser/ad_notification.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ std::unique_ptr<message_center::Notification> 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;
}
Expand Down
37 changes: 31 additions & 6 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<base::OneShotTimer>();
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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<uint32_t>::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<base::OneShotTimer>();
timers_[next_timer_id_]->Start(FROM_HERE,
timers_[timer_id] = std::make_unique<base::OneShotTimer>();
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) {
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 01966ec

Please sign in to comment.