Limit the window of time a promo can be visible after first view.

Notification promos currently have start, end times, and a number of maximum
views. However, the length of time a promo can be visible was not capped.
This change adds a flag to cap the length of time a promo is visible by
max_seconds_.

BUG=None
Test=None

Review URL: https://codereview.chromium.org/419033007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285803 0039d316-1c4b-4281-b951-d872f2087c98
parent 853703c5
...@@ -49,6 +49,8 @@ const char kPrefPromoIncrement[] = "increment"; ...@@ -49,6 +49,8 @@ const char kPrefPromoIncrement[] = "increment";
const char kPrefPromoIncrementFrequency[] = "increment_frequency"; const char kPrefPromoIncrementFrequency[] = "increment_frequency";
const char kPrefPromoIncrementMax[] = "increment_max"; const char kPrefPromoIncrementMax[] = "increment_max";
const char kPrefPromoMaxViews[] = "max_views"; const char kPrefPromoMaxViews[] = "max_views";
const char kPrefPromoMaxSeconds[] = "max_seconds";
const char kPrefPromoFirstViewTime[] = "first_view_time";
const char kPrefPromoGroup[] = "group"; const char kPrefPromoGroup[] = "group";
const char kPrefPromoViews[] = "views"; const char kPrefPromoViews[] = "views";
const char kPrefPromoClosed[] = "closed"; const char kPrefPromoClosed[] = "closed";
...@@ -199,6 +201,8 @@ NotificationPromo::NotificationPromo() ...@@ -199,6 +201,8 @@ NotificationPromo::NotificationPromo()
time_slice_(0), time_slice_(0),
max_group_(0), max_group_(0),
max_views_(0), max_views_(0),
max_seconds_(0),
first_view_time_(0),
group_(0), group_(0),
views_(0), views_(0),
closed_(false), closed_(false),
...@@ -284,6 +288,9 @@ void NotificationPromo::InitFromJson(const base::DictionaryValue& json, ...@@ -284,6 +288,9 @@ void NotificationPromo::InitFromJson(const base::DictionaryValue& json,
promo->GetInteger("max_views", &max_views_); promo->GetInteger("max_views", &max_views_);
DVLOG(1) << "max_views_ " << max_views_; DVLOG(1) << "max_views_ " << max_views_;
promo->GetInteger("max_seconds", &max_seconds_);
DVLOG(1) << "max_seconds_ " << max_seconds_;
CheckForNewNotification(); CheckForNewNotification();
} }
...@@ -341,6 +348,8 @@ void NotificationPromo::WritePrefs() { ...@@ -341,6 +348,8 @@ void NotificationPromo::WritePrefs() {
ntp_promo->SetInteger(kPrefPromoIncrementMax, max_group_); ntp_promo->SetInteger(kPrefPromoIncrementMax, max_group_);
ntp_promo->SetInteger(kPrefPromoMaxViews, max_views_); ntp_promo->SetInteger(kPrefPromoMaxViews, max_views_);
ntp_promo->SetInteger(kPrefPromoMaxSeconds, max_seconds_);
ntp_promo->SetDouble(kPrefPromoFirstViewTime, first_view_time_);
ntp_promo->SetInteger(kPrefPromoGroup, group_); ntp_promo->SetInteger(kPrefPromoGroup, group_);
ntp_promo->SetInteger(kPrefPromoViews, views_); ntp_promo->SetInteger(kPrefPromoViews, views_);
...@@ -388,6 +397,8 @@ void NotificationPromo::InitFromPrefs(PromoType promo_type) { ...@@ -388,6 +397,8 @@ void NotificationPromo::InitFromPrefs(PromoType promo_type) {
ntp_promo->GetInteger(kPrefPromoIncrementMax, &max_group_); ntp_promo->GetInteger(kPrefPromoIncrementMax, &max_group_);
ntp_promo->GetInteger(kPrefPromoMaxViews, &max_views_); ntp_promo->GetInteger(kPrefPromoMaxViews, &max_views_);
ntp_promo->GetInteger(kPrefPromoMaxSeconds, &max_seconds_);
ntp_promo->GetDouble(kPrefPromoFirstViewTime, &first_view_time_);
ntp_promo->GetInteger(kPrefPromoGroup, &group_); ntp_promo->GetInteger(kPrefPromoGroup, &group_);
ntp_promo->GetInteger(kPrefPromoViews, &views_); ntp_promo->GetInteger(kPrefPromoViews, &views_);
...@@ -412,6 +423,7 @@ bool NotificationPromo::CanShow() const { ...@@ -412,6 +423,7 @@ bool NotificationPromo::CanShow() const {
!promo_text_.empty() && !promo_text_.empty() &&
!ExceedsMaxGroup() && !ExceedsMaxGroup() &&
!ExceedsMaxViews() && !ExceedsMaxViews() &&
!ExceedsMaxSeconds() &&
CheckAppLauncher() && CheckAppLauncher() &&
base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() && base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() &&
base::Time::FromDoubleT(EndTime()) > base::Time::Now(); base::Time::FromDoubleT(EndTime()) > base::Time::Now();
...@@ -434,8 +446,11 @@ bool NotificationPromo::HandleViewed(PromoType promo_type) { ...@@ -434,8 +446,11 @@ bool NotificationPromo::HandleViewed(PromoType promo_type) {
NotificationPromo promo; NotificationPromo promo;
promo.InitFromPrefs(promo_type); promo.InitFromPrefs(promo_type);
++promo.views_; ++promo.views_;
if (promo.first_view_time_ == 0) {
promo.first_view_time_ = base::Time::Now().ToDoubleT();
}
promo.WritePrefs(); promo.WritePrefs();
return promo.ExceedsMaxViews(); return promo.ExceedsMaxViews() || promo.ExceedsMaxSeconds();
} }
bool NotificationPromo::ExceedsMaxGroup() const { bool NotificationPromo::ExceedsMaxGroup() const {
...@@ -446,6 +461,15 @@ bool NotificationPromo::ExceedsMaxViews() const { ...@@ -446,6 +461,15 @@ bool NotificationPromo::ExceedsMaxViews() const {
return (max_views_ == 0) ? false : views_ >= max_views_; return (max_views_ == 0) ? false : views_ >= max_views_;
} }
bool NotificationPromo::ExceedsMaxSeconds() const {
if (max_seconds_ == 0 || first_view_time_ == 0)
return false;
const base::Time last_view_time = base::Time::FromDoubleT(first_view_time_) +
base::TimeDelta::FromSeconds(max_seconds_);
return last_view_time < base::Time::Now();
}
// static // static
GURL NotificationPromo::PromoServerURL() { GURL NotificationPromo::PromoServerURL() {
GURL url(promo_server_url); GURL url(promo_server_url);
......
...@@ -96,6 +96,10 @@ class NotificationPromo { ...@@ -96,6 +96,10 @@ class NotificationPromo {
// When max_views_ is 0, we don't cap the number of views. // When max_views_ is 0, we don't cap the number of views.
bool ExceedsMaxViews() const; bool ExceedsMaxViews() const;
// Tests |first_view_time_| + |max_seconds_| and -now().
// When either is 0, we don't cap the number of seconds.
bool ExceedsMaxSeconds() const;
// Returns false if this promo should not be displayed because it is a promo // Returns false if this promo should not be displayed because it is a promo
// for the app launcher, and the user has already enabled the app launcher. // for the app launcher, and the user has already enabled the app launcher.
bool CheckAppLauncher() const; bool CheckAppLauncher() const;
...@@ -119,6 +123,13 @@ class NotificationPromo { ...@@ -119,6 +123,13 @@ class NotificationPromo {
// When max_views_ is 0, we don't cap the number of views. // When max_views_ is 0, we don't cap the number of views.
int max_views_; int max_views_;
// When max_seconds_ is 0 or not set, we don't cap the number of seconds a
// promo can be visible.
int max_seconds_;
// Set when the promo is viewed for the first time.
double first_view_time_;
int group_; int group_;
int views_; int views_;
bool closed_; bool closed_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment