| 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" |
| 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 11 #include "chrome/browser/banners/app_banner_data_fetcher.h" | 13 #include "chrome/browser/banners/app_banner_data_fetcher.h" |
| 12 #include "chrome/browser/banners/app_banner_metrics.h" | 14 #include "chrome/browser/banners/app_banner_metrics.h" |
| 13 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 17 #include "components/content_settings/core/common/content_settings_pattern.h" | 19 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 18 #include "components/rappor/rappor_utils.h" | 20 #include "components/rappor/rappor_utils.h" |
| 19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 20 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
| 21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // Max number of apps (including ServiceWorker based web apps) that a particular | 27 // Max number of apps (including ServiceWorker based web apps) that a particular |
| 26 // site may show a banner for. | 28 // site may show a banner for. |
| 27 const size_t kMaxAppsPerSite = 3; | 29 const size_t kMaxAppsPerSite = 3; |
| 28 | 30 |
| 29 // Oldest could show banner event we care about, in days. | 31 // Oldest could show banner event we care about, in days. |
| 30 const unsigned int kOldestCouldShowBannerEventInDays = 14; | 32 const unsigned int kOldestCouldShowBannerEventInDays = 14; |
| 31 | 33 |
| 32 // Total site engagements where a banner could have been shown before | 34 // Total site engagements where a banner could have been shown before |
| 33 // a banner will actually be triggered. | 35 // a banner will actually be triggered. |
| 34 const double kTotalEngagementToTrigger = 2; | 36 const double kTotalEngagementToTrigger = 2; |
| 35 | 37 |
| 36 // Number of days that showing the banner will prevent it being seen again for. | 38 // Number of days that showing the banner will prevent it being seen again for. |
| 37 const unsigned int kMinimumDaysBetweenBannerShows = 60; | 39 const unsigned int kMinimumDaysBetweenBannerShows = 60; |
| 38 | 40 |
| 41 const unsigned int kNumberOfMinutesInADay = 1440; |
| 42 |
| 43 // Number of minutes between visits that will trigger a could show banner event. |
| 44 // Defaults to the number of minutes in a day. |
| 45 unsigned int gMinimumMinutesBetweenVisits = kNumberOfMinutesInADay; |
| 46 |
| 39 // Number of days that the banner being blocked will prevent it being seen again | 47 // Number of days that the banner being blocked will prevent it being seen again |
| 40 // for. | 48 // for. |
| 41 const unsigned int kMinimumBannerBlockedToBannerShown = 90; | 49 const unsigned int kMinimumBannerBlockedToBannerShown = 90; |
| 42 | 50 |
| 43 // Dictionary keys to use for the events. | 51 // Dictionary keys to use for the events. |
| 44 const char* kBannerEventKeys[] = { | 52 const char* kBannerEventKeys[] = { |
| 45 "couldShowBannerEvents", | 53 "couldShowBannerEvents", |
| 46 "didShowBannerEvent", | 54 "didShowBannerEvent", |
| 47 "didBlockBannerEvent", | 55 "didBlockBannerEvent", |
| 48 "didAddToHomescreenEvent", | 56 "didAddToHomescreenEvent", |
| 49 }; | 57 }; |
| 50 | 58 |
| 51 // Keys to use when storing BannerEvent structs. | 59 // Keys to use when storing BannerEvent structs. |
| 52 const char kBannerTimeKey[] = "time"; | 60 const char kBannerTimeKey[] = "time"; |
| 53 const char kBannerEngagementKey[] = "engagement"; | 61 const char kBannerEngagementKey[] = "engagement"; |
| 54 | 62 |
| 55 // Engagement weight assigned to direct and indirect navigations. | 63 // Engagement weight assigned to direct and indirect navigations. |
| 56 // By default, a direct navigation is a page visit via ui::PAGE_TRANSITION_TYPED | 64 // 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 | 65 // or ui::PAGE_TRANSITION_GENERATED. |
| 58 // all other navigations. | |
| 59 double kDirectNavigationEngagement = 1; | 66 double kDirectNavigationEngagement = 1; |
| 60 double kIndirectNavigationEnagagement = 1; | 67 double kIndirectNavigationEnagagement = 1; |
| 61 | 68 |
| 62 scoped_ptr<base::DictionaryValue> GetOriginDict( | 69 scoped_ptr<base::DictionaryValue> GetOriginDict( |
| 63 HostContentSettingsMap* settings, | 70 HostContentSettingsMap* settings, |
| 64 const GURL& origin_url) { | 71 const GURL& origin_url) { |
| 65 if (!settings) | 72 if (!settings) |
| 66 return scoped_ptr<base::DictionaryValue>(); | 73 return scoped_ptr<base::DictionaryValue>(); |
| 67 | 74 |
| 68 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( | 75 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 double engagement = GetEventEngagement(transition_type); | 241 double engagement = GetEventEngagement(transition_type); |
| 235 | 242 |
| 236 base::ListValue* could_show_list = nullptr; | 243 base::ListValue* could_show_list = nullptr; |
| 237 if (!app_dict->GetList(event_key, &could_show_list)) { | 244 if (!app_dict->GetList(event_key, &could_show_list)) { |
| 238 could_show_list = new base::ListValue(); | 245 could_show_list = new base::ListValue(); |
| 239 app_dict->Set(event_key, make_scoped_ptr(could_show_list)); | 246 app_dict->Set(event_key, make_scoped_ptr(could_show_list)); |
| 240 } | 247 } |
| 241 | 248 |
| 242 // Trim any items that are older than we should care about. For comparisons | 249 // Trim any items that are older than we should care about. For comparisons |
| 243 // the times are converted to local dates. | 250 // the times are converted to local dates. |
| 244 base::Time date = time.LocalMidnight(); | 251 base::Time date = BucketTimeToResolution(time, gMinimumMinutesBetweenVisits); |
| 245 base::ValueVector::iterator it = could_show_list->begin(); | 252 base::ValueVector::iterator it = could_show_list->begin(); |
| 246 while (it != could_show_list->end()) { | 253 while (it != could_show_list->end()) { |
| 247 if ((*it)->IsType(base::Value::TYPE_DICTIONARY)) { | 254 if ((*it)->IsType(base::Value::TYPE_DICTIONARY)) { |
| 248 base::DictionaryValue* internal_value; | 255 base::DictionaryValue* internal_value; |
| 249 double internal_date; | 256 double internal_date; |
| 250 (*it)->GetAsDictionary(&internal_value); | 257 (*it)->GetAsDictionary(&internal_value); |
| 251 | 258 |
| 252 if (internal_value->GetDouble(kBannerTimeKey, &internal_date)) { | 259 if (internal_value->GetDouble(kBannerTimeKey, &internal_date)) { |
| 253 base::Time other_date = | 260 base::Time other_date = |
| 254 base::Time::FromInternalValue(internal_date).LocalMidnight(); | 261 BucketTimeToResolution(base::Time::FromInternalValue(internal_date), |
| 262 gMinimumMinutesBetweenVisits); |
| 255 if (other_date == date) { | 263 if (other_date == date) { |
| 256 double other_engagement = 0; | 264 double other_engagement = 0; |
| 257 if (internal_value->GetDouble(kBannerEngagementKey, | 265 if (internal_value->GetDouble(kBannerEngagementKey, |
| 258 &other_engagement) && | 266 &other_engagement) && |
| 259 other_engagement >= engagement) { | 267 other_engagement >= engagement) { |
| 260 // This date has already been added, but with an equal or higher | 268 // This date has already been added, but with an equal or higher |
| 261 // engagement. Don't add the date again. If the conditional fails, | 269 // 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. | 270 // fall to the end of the loop where the existing entry is deleted. |
| 263 return; | 271 return; |
| 264 } | 272 } |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 return base::Time(); | 434 return base::Time(); |
| 427 | 435 |
| 428 return base::Time::FromInternalValue(internal_time); | 436 return base::Time::FromInternalValue(internal_time); |
| 429 } | 437 } |
| 430 | 438 |
| 431 void AppBannerSettingsHelper::SetEngagementWeights(double direct_engagement, | 439 void AppBannerSettingsHelper::SetEngagementWeights(double direct_engagement, |
| 432 double indirect_engagement) { | 440 double indirect_engagement) { |
| 433 kDirectNavigationEngagement = direct_engagement; | 441 kDirectNavigationEngagement = direct_engagement; |
| 434 kIndirectNavigationEnagagement = indirect_engagement; | 442 kIndirectNavigationEnagagement = indirect_engagement; |
| 435 } | 443 } |
| 444 |
| 445 void AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits( |
| 446 unsigned int minutes) { |
| 447 gMinimumMinutesBetweenVisits = minutes; |
| 448 } |
| 449 |
| 450 // Given a time, returns that time scoped to the nearest minute resolution |
| 451 // locally. For example, if the resolution is one hour, this function will |
| 452 // return the time to the closest (previous) hour in the local time zone. |
| 453 base::Time AppBannerSettingsHelper::BucketTimeToResolution( |
| 454 base::Time time, |
| 455 unsigned int minutes) { |
| 456 // Only support resolutions smaller than or equal to one day. Enforce |
| 457 // that resolutions divide evenly into one day. Otherwise, default to a |
| 458 // day resolution (each time converted to midnight local time). |
| 459 if (minutes == 0 || minutes > kNumberOfMinutesInADay || |
| 460 kNumberOfMinutesInADay % minutes != 0) { |
| 461 return time.LocalMidnight(); |
| 462 } |
| 463 |
| 464 // Extract the number of minutes past midnight in local time. Divide that |
| 465 // number by the resolution size, and return the time converted to local |
| 466 // midnight with the resulting truncated number added. |
| 467 base::Time::Exploded exploded; |
| 468 time.LocalExplode(&exploded); |
| 469 int total_minutes = exploded.hour * 60 + exploded.minute; |
| 470 |
| 471 // Use truncating integer division here. |
| 472 return time.LocalMidnight() + |
| 473 base::TimeDelta::FromMinutes((total_minutes / minutes) * minutes); |
| 474 } |
| 475 |
| 476 void AppBannerSettingsHelper::UpdateMinutesBetweenVisits() { |
| 477 std::string minutes_between_visits = |
| 478 base::FieldTrialList::FindFullName("AppBannersMinutesBetweenVisits"); |
| 479 int minimum_minutes = 0; |
| 480 if (base::StringToInt(minutes_between_visits, &minimum_minutes)) |
| 481 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(minimum_minutes); |
| 482 } |
| OLD | NEW |