OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/banners/app_banner_settings_helper.h" | 5 #include "chrome/browser/banners/app_banner_settings_helper.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
29 // Oldest could show banner event we care about, in days. | 29 // Oldest could show banner event we care about, in days. |
30 const unsigned int kOldestCouldShowBannerEventInDays = 14; | 30 const unsigned int kOldestCouldShowBannerEventInDays = 14; |
31 | 31 |
32 // Total site engagements where a banner could have been shown before | 32 // Total site engagements where a banner could have been shown before |
33 // a banner will actually be triggered. | 33 // a banner will actually be triggered. |
34 const double kTotalEngagementToTrigger = 2; | 34 const double kTotalEngagementToTrigger = 2; |
35 | 35 |
36 // Number of days that showing the banner will prevent it being seen again for. | 36 // Number of days that showing the banner will prevent it being seen again for. |
37 const unsigned int kMinimumDaysBetweenBannerShows = 60; | 37 const unsigned int kMinimumDaysBetweenBannerShows = 60; |
38 | 38 |
39 const unsigned int kNumberOfMinutesInADay = 1440; | |
40 | |
41 // Number of minutes between visits that will trigger a could show banner event. | |
42 // Defaults to the number of minutes in a day. | |
43 unsigned int gMinimumMinutesBetweenVisits = kNumberOfMinutesInADay; | |
44 | |
39 // Number of days that the banner being blocked will prevent it being seen again | 45 // Number of days that the banner being blocked will prevent it being seen again |
40 // for. | 46 // for. |
41 const unsigned int kMinimumBannerBlockedToBannerShown = 90; | 47 const unsigned int kMinimumBannerBlockedToBannerShown = 90; |
42 | 48 |
43 // Dictionary keys to use for the events. | 49 // Dictionary keys to use for the events. |
44 const char* kBannerEventKeys[] = { | 50 const char* kBannerEventKeys[] = { |
45 "couldShowBannerEvents", | 51 "couldShowBannerEvents", |
46 "didShowBannerEvent", | 52 "didShowBannerEvent", |
47 "didBlockBannerEvent", | 53 "didBlockBannerEvent", |
48 "didAddToHomescreenEvent", | 54 "didAddToHomescreenEvent", |
49 }; | 55 }; |
50 | 56 |
51 // Keys to use when storing BannerEvent structs. | 57 // Keys to use when storing BannerEvent structs. |
52 const char kBannerTimeKey[] = "time"; | 58 const char kBannerTimeKey[] = "time"; |
53 const char kBannerEngagementKey[] = "engagement"; | 59 const char kBannerEngagementKey[] = "engagement"; |
54 | 60 |
55 // Engagement weight assigned to direct and indirect navigations. | 61 // Engagement weight assigned to direct and indirect navigations. |
56 // By default, a direct navigation is a page visit via ui::PAGE_TRANSITION_TYPED | 62 // By default, a direct navigation is a page visit via ui::PAGE_TRANSITION_TYPED |
57 // or ui::PAGE_TRANSITION_GENERATED. These are weighted twice the engagement of | 63 // or ui::PAGE_TRANSITION_GENERATED. |
58 // all other navigations. | |
59 double kDirectNavigationEngagement = 1; | 64 double kDirectNavigationEngagement = 1; |
60 double kIndirectNavigationEnagagement = 1; | 65 double kIndirectNavigationEnagagement = 1; |
61 | 66 |
62 scoped_ptr<base::DictionaryValue> GetOriginDict( | 67 scoped_ptr<base::DictionaryValue> GetOriginDict( |
63 HostContentSettingsMap* settings, | 68 HostContentSettingsMap* settings, |
64 const GURL& origin_url) { | 69 const GURL& origin_url) { |
65 if (!settings) | 70 if (!settings) |
66 return scoped_ptr<base::DictionaryValue>(); | 71 return scoped_ptr<base::DictionaryValue>(); |
67 | 72 |
68 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( | 73 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( |
(...skipping 26 matching lines...) Expand all Loading... | |
95 if (ui::PageTransitionCoreTypeIs(transition_type, | 100 if (ui::PageTransitionCoreTypeIs(transition_type, |
96 ui::PAGE_TRANSITION_TYPED) || | 101 ui::PAGE_TRANSITION_TYPED) || |
97 ui::PageTransitionCoreTypeIs(transition_type, | 102 ui::PageTransitionCoreTypeIs(transition_type, |
98 ui::PAGE_TRANSITION_GENERATED)) { | 103 ui::PAGE_TRANSITION_GENERATED)) { |
99 return kDirectNavigationEngagement; | 104 return kDirectNavigationEngagement; |
100 } else { | 105 } else { |
101 return kIndirectNavigationEnagagement; | 106 return kIndirectNavigationEnagagement; |
102 } | 107 } |
103 } | 108 } |
104 | 109 |
110 // Given a time, returns that time scoped to the nearest resolution locally. | |
111 // For example, if the resolution is one hour, then this function will return | |
112 // the time to the closest (previous) hour in the local time zone. | |
113 base::Time BucketTime(base::Time time, base::TimeDelta resolution) { | |
benwells
2015/08/24 06:55:52
Nit: I think it would be simpler if this just took
dominickn
2015/08/24 07:43:16
Done.
| |
114 // Only support resolutions smaller than or equal to one day. Enforce | |
115 // that resolutions divide evenly into one day. Otherwise, default to a | |
116 // day resolution (each time converted to midnight local time). | |
117 unsigned int resolution_minutes = resolution.InMinutes(); | |
118 if (resolution_minutes == 0 || resolution_minutes > kNumberOfMinutesInADay || | |
119 kNumberOfMinutesInADay % resolution_minutes != 0) { | |
120 return time.LocalMidnight(); | |
121 } | |
122 | |
123 // Extract the number of minutes past midnight in local time. Divide that | |
124 // number by the resolution size, and return the time converted to local | |
125 // midnight with the resulting truncated number added. | |
126 base::Time::Exploded exploded; | |
127 time.LocalExplode(&exploded); | |
128 int total_minutes = exploded.hour * 60 + exploded.minute; | |
129 | |
130 // Use truncating integer division here. | |
131 return time.LocalMidnight() + | |
132 base::TimeDelta::FromMinutes((total_minutes / resolution_minutes) * | |
133 resolution_minutes); | |
134 } | |
135 | |
105 } // namespace | 136 } // namespace |
106 | 137 |
107 void AppBannerSettingsHelper::ClearHistoryForURLs( | 138 void AppBannerSettingsHelper::ClearHistoryForURLs( |
108 Profile* profile, | 139 Profile* profile, |
109 const std::set<GURL>& origin_urls) { | 140 const std::set<GURL>& origin_urls) { |
110 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 141 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
111 for (const GURL& origin_url : origin_urls) { | 142 for (const GURL& origin_url : origin_urls) { |
112 ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); | 143 ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); |
113 if (!pattern.IsValid()) | 144 if (!pattern.IsValid()) |
114 continue; | 145 continue; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 double engagement = GetEventEngagement(transition_type); | 265 double engagement = GetEventEngagement(transition_type); |
235 | 266 |
236 base::ListValue* could_show_list = nullptr; | 267 base::ListValue* could_show_list = nullptr; |
237 if (!app_dict->GetList(event_key, &could_show_list)) { | 268 if (!app_dict->GetList(event_key, &could_show_list)) { |
238 could_show_list = new base::ListValue(); | 269 could_show_list = new base::ListValue(); |
239 app_dict->Set(event_key, make_scoped_ptr(could_show_list)); | 270 app_dict->Set(event_key, make_scoped_ptr(could_show_list)); |
240 } | 271 } |
241 | 272 |
242 // Trim any items that are older than we should care about. For comparisons | 273 // Trim any items that are older than we should care about. For comparisons |
243 // the times are converted to local dates. | 274 // the times are converted to local dates. |
244 base::Time date = time.LocalMidnight(); | 275 base::TimeDelta resolution = |
276 base::TimeDelta::FromMinutes(gMinimumMinutesBetweenVisits); | |
277 base::Time date = BucketTime(time, resolution); | |
245 base::ValueVector::iterator it = could_show_list->begin(); | 278 base::ValueVector::iterator it = could_show_list->begin(); |
246 while (it != could_show_list->end()) { | 279 while (it != could_show_list->end()) { |
247 if ((*it)->IsType(base::Value::TYPE_DICTIONARY)) { | 280 if ((*it)->IsType(base::Value::TYPE_DICTIONARY)) { |
248 base::DictionaryValue* internal_value; | 281 base::DictionaryValue* internal_value; |
249 double internal_date; | 282 double internal_date; |
250 (*it)->GetAsDictionary(&internal_value); | 283 (*it)->GetAsDictionary(&internal_value); |
251 | 284 |
252 if (internal_value->GetDouble(kBannerTimeKey, &internal_date)) { | 285 if (internal_value->GetDouble(kBannerTimeKey, &internal_date)) { |
253 base::Time other_date = | 286 base::Time other_date = BucketTime( |
254 base::Time::FromInternalValue(internal_date).LocalMidnight(); | 287 base::Time::FromInternalValue(internal_date), resolution); |
255 if (other_date == date) { | 288 if (other_date == date) { |
256 double other_engagement = 0; | 289 double other_engagement = 0; |
257 if (internal_value->GetDouble(kBannerEngagementKey, | 290 if (internal_value->GetDouble(kBannerEngagementKey, |
258 &other_engagement) && | 291 &other_engagement) && |
259 other_engagement >= engagement) { | 292 other_engagement >= engagement) { |
260 // This date has already been added, but with an equal or higher | 293 // This date has already been added, but with an equal or higher |
261 // engagement. Don't add the date again. If the conditional fails, | 294 // engagement. Don't add the date again. If the conditional fails, |
262 // fall to the end of the loop where the existing entry is deleted. | 295 // fall to the end of the loop where the existing entry is deleted. |
263 return; | 296 return; |
264 } | 297 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
426 return base::Time(); | 459 return base::Time(); |
427 | 460 |
428 return base::Time::FromInternalValue(internal_time); | 461 return base::Time::FromInternalValue(internal_time); |
429 } | 462 } |
430 | 463 |
431 void AppBannerSettingsHelper::SetEngagementWeights(double direct_engagement, | 464 void AppBannerSettingsHelper::SetEngagementWeights(double direct_engagement, |
432 double indirect_engagement) { | 465 double indirect_engagement) { |
433 kDirectNavigationEngagement = direct_engagement; | 466 kDirectNavigationEngagement = direct_engagement; |
434 kIndirectNavigationEnagagement = indirect_engagement; | 467 kIndirectNavigationEnagagement = indirect_engagement; |
435 } | 468 } |
469 | |
470 void AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits( | |
471 unsigned int minutes) { | |
472 gMinimumMinutesBetweenVisits = minutes; | |
473 } | |
474 | |
475 base::Time AppBannerSettingsHelper::BucketTimeToResolution( | |
476 base::Time time, | |
477 base::TimeDelta resolution) { | |
478 return BucketTime(time, resolution); | |
benwells
2015/08/24 06:55:52
Do you need a wrapper function for this? You could
dominickn
2015/08/24 07:43:16
Done.
| |
479 } | |
OLD | NEW |