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" | 11 #include "base/metrics/field_trial.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" |
13 #include "chrome/browser/banners/app_banner_data_fetcher.h" | 14 #include "chrome/browser/banners/app_banner_data_fetcher.h" |
14 #include "chrome/browser/banners/app_banner_metrics.h" | 15 #include "chrome/browser/banners/app_banner_metrics.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
19 #include "components/content_settings/core/common/content_settings_pattern.h" | 20 #include "components/content_settings/core/common/content_settings_pattern.h" |
20 #include "components/rappor/rappor_utils.h" | 21 #include "components/rappor/rappor_utils.h" |
21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 if (ui::PageTransitionCoreTypeIs(transition_type, | 103 if (ui::PageTransitionCoreTypeIs(transition_type, |
103 ui::PAGE_TRANSITION_TYPED) || | 104 ui::PAGE_TRANSITION_TYPED) || |
104 ui::PageTransitionCoreTypeIs(transition_type, | 105 ui::PageTransitionCoreTypeIs(transition_type, |
105 ui::PAGE_TRANSITION_GENERATED)) { | 106 ui::PAGE_TRANSITION_GENERATED)) { |
106 return kDirectNavigationEngagement; | 107 return kDirectNavigationEngagement; |
107 } else { | 108 } else { |
108 return kIndirectNavigationEnagagement; | 109 return kIndirectNavigationEnagagement; |
109 } | 110 } |
110 } | 111 } |
111 | 112 |
| 113 // Queries a field trial for updates to the default engagement values assigned |
| 114 // to direct and indirect navigations. |
| 115 void UpdateEngagementWeights() { |
| 116 // Expect a field trial value of "X:Y", where X is the direct engagement |
| 117 // value and Y is the indirect engagement value. |
| 118 std::string weights = |
| 119 base::FieldTrialList::FindFullName("AppBannersEngagementWeights"); |
| 120 if (weights.empty()) |
| 121 return; |
| 122 |
| 123 std::vector<std::string> tokens = base::SplitString( |
| 124 weights, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 125 if (tokens.size() == 2) { |
| 126 double direct_engagement = -1; |
| 127 double indirect_engagement = -1; |
| 128 |
| 129 // Ensure that we get valid doubles from the field trial, and that both |
| 130 // values are greater than or equal to zero and less than or equal to the |
| 131 // total engagement required to trigger the banner. |
| 132 if (base::StringToDouble(tokens[0], &direct_engagement) && |
| 133 base::StringToDouble(tokens[1], &indirect_engagement) && |
| 134 direct_engagement >= 0 && indirect_engagement >= 0 && |
| 135 direct_engagement <= kTotalEngagementToTrigger && |
| 136 indirect_engagement <= kTotalEngagementToTrigger) { |
| 137 AppBannerSettingsHelper::SetEngagementWeights(direct_engagement, |
| 138 indirect_engagement); |
| 139 } |
| 140 } |
| 141 } |
| 142 |
| 143 // Queries a field trial for updates to the default number of minutes between |
| 144 // site visits counted for the purposes of displaying a banner. |
| 145 void UpdateMinutesBetweenVisits() { |
| 146 std::string minutes_between_visits = |
| 147 base::FieldTrialList::FindFullName("AppBannersMinutesBetweenVisits"); |
| 148 if (minutes_between_visits.empty()) |
| 149 return; |
| 150 |
| 151 int minimum_minutes = 0; |
| 152 if (base::StringToInt(minutes_between_visits, &minimum_minutes)) |
| 153 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(minimum_minutes); |
| 154 } |
| 155 |
112 } // namespace | 156 } // namespace |
113 | 157 |
114 void AppBannerSettingsHelper::ClearHistoryForURLs( | 158 void AppBannerSettingsHelper::ClearHistoryForURLs( |
115 Profile* profile, | 159 Profile* profile, |
116 const std::set<GURL>& origin_urls) { | 160 const std::set<GURL>& origin_urls) { |
117 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); | 161 HostContentSettingsMap* settings = profile->GetHostContentSettingsMap(); |
118 for (const GURL& origin_url : origin_urls) { | 162 for (const GURL& origin_url : origin_urls) { |
119 ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); | 163 ContentSettingsPattern pattern(ContentSettingsPattern::FromURL(origin_url)); |
120 if (!pattern.IsValid()) | 164 if (!pattern.IsValid()) |
121 continue; | 165 continue; |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 | 493 |
450 // Given a time, returns that time scoped to the nearest minute resolution | 494 // 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 | 495 // 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. | 496 // return the time to the closest (previous) hour in the local time zone. |
453 base::Time AppBannerSettingsHelper::BucketTimeToResolution( | 497 base::Time AppBannerSettingsHelper::BucketTimeToResolution( |
454 base::Time time, | 498 base::Time time, |
455 unsigned int minutes) { | 499 unsigned int minutes) { |
456 // Only support resolutions smaller than or equal to one day. Enforce | 500 // Only support resolutions smaller than or equal to one day. Enforce |
457 // that resolutions divide evenly into one day. Otherwise, default to a | 501 // that resolutions divide evenly into one day. Otherwise, default to a |
458 // day resolution (each time converted to midnight local time). | 502 // day resolution (each time converted to midnight local time). |
459 if (minutes == 0 || minutes > kNumberOfMinutesInADay || | 503 if (minutes == 0 || minutes >= kNumberOfMinutesInADay || |
460 kNumberOfMinutesInADay % minutes != 0) { | 504 kNumberOfMinutesInADay % minutes != 0) { |
461 return time.LocalMidnight(); | 505 return time.LocalMidnight(); |
462 } | 506 } |
463 | 507 |
464 // Extract the number of minutes past midnight in local time. Divide that | 508 // 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 | 509 // number by the resolution size, and return the time converted to local |
466 // midnight with the resulting truncated number added. | 510 // midnight with the resulting truncated number added. |
467 base::Time::Exploded exploded; | 511 base::Time::Exploded exploded; |
468 time.LocalExplode(&exploded); | 512 time.LocalExplode(&exploded); |
469 int total_minutes = exploded.hour * 60 + exploded.minute; | 513 int total_minutes = exploded.hour * 60 + exploded.minute; |
470 | 514 |
471 // Use truncating integer division here. | 515 // Use truncating integer division here. |
472 return time.LocalMidnight() + | 516 return time.LocalMidnight() + |
473 base::TimeDelta::FromMinutes((total_minutes / minutes) * minutes); | 517 base::TimeDelta::FromMinutes((total_minutes / minutes) * minutes); |
474 } | 518 } |
475 | 519 |
476 void AppBannerSettingsHelper::UpdateMinutesBetweenVisits() { | 520 void AppBannerSettingsHelper::UpdateFromFieldTrial() { |
477 std::string minutes_between_visits = | 521 UpdateEngagementWeights(); |
478 base::FieldTrialList::FindFullName("AppBannersMinutesBetweenVisits"); | 522 UpdateMinutesBetweenVisits(); |
479 int minimum_minutes = 0; | |
480 if (base::StringToInt(minutes_between_visits, &minimum_minutes)) | |
481 AppBannerSettingsHelper::SetMinimumMinutesBetweenVisits(minimum_minutes); | |
482 } | 523 } |
OLD | NEW |