OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/engagement/site_engagement_service.h" | 5 #include "chrome/browser/engagement/site_engagement_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/strings/string_number_conversions.h" | |
11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
12 #include "base/time/default_clock.h" | 13 #include "base/time/default_clock.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
15 #include "chrome/browser/engagement/site_engagement_helper.h" | 16 #include "chrome/browser/engagement/site_engagement_helper.h" |
16 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 17 #include "chrome/browser/engagement/site_engagement_service_factory.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" |
21 #include "components/variations/variations_associated_data.h" | |
20 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // Length of time between metrics logging. | 27 // Length of time between metrics logging. |
26 const base::TimeDelta metrics_interval = base::TimeDelta::FromMinutes(60); | 28 const base::TimeDelta metrics_interval = base::TimeDelta::FromMinutes(60); |
27 | 29 |
28 // Delta within which to consider scores equal. | 30 // Delta within which to consider scores equal. |
29 const double kScoreDelta = 0.001; | 31 const double kScoreDelta = 0.001; |
(...skipping 29 matching lines...) Expand all Loading... | |
59 return make_scoped_ptr(new base::DictionaryValue()); | 61 return make_scoped_ptr(new base::DictionaryValue()); |
60 | 62 |
61 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | 63 if (!value->IsType(base::Value::TYPE_DICTIONARY)) |
62 return make_scoped_ptr(new base::DictionaryValue()); | 64 return make_scoped_ptr(new base::DictionaryValue()); |
63 | 65 |
64 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 66 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
65 } | 67 } |
66 | 68 |
67 } // namespace | 69 } // namespace |
68 | 70 |
71 const double SiteEngagementScore::kMaxPoints = 100; | |
72 double SiteEngagementScore::gMaxPointsPerDay = 5; | |
73 double SiteEngagementScore::gNavigationPoints = 0.5; | |
74 double SiteEngagementScore::gUserInputPoints = 0.05; | |
75 int SiteEngagementScore::gDecayPeriodInDays = 7; | |
76 double SiteEngagementScore::gDecayPoints = 5; | |
77 | |
69 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 78 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
70 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 79 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
71 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 80 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
72 | 81 |
73 const double SiteEngagementScore::kMaxPoints = 100; | 82 const char* SiteEngagementScore::kEngagementParams = "SiteEngagementParams"; |
74 const double SiteEngagementScore::kMaxPointsPerDay = 5; | 83 const char* SiteEngagementScore::kMaxPointsPerDayParam = "max_points_per_day"; |
75 const double SiteEngagementScore::kNavigationPoints = 0.5; | 84 const char* SiteEngagementScore::kNavigationPointsParam = "navigation_points"; |
76 const double SiteEngagementScore::kUserInputPoints = 0.05; | 85 const char* SiteEngagementScore::kUserInputPointsParam = "user_input_points"; |
77 const int SiteEngagementScore::kDecayPeriodInDays = 7; | 86 const char* SiteEngagementScore::kDecayPeriodInDaysParam = |
78 const double SiteEngagementScore::kDecayPoints = 5; | 87 "decay_period_in_days"; |
88 const char* SiteEngagementScore::kDecayPointsParam = "decay_points"; | |
89 | |
90 // static | |
91 void SiteEngagementScore::UpdateFromVariations() { | |
92 std::string max_points_per_day_param = variations::GetVariationParamValue( | |
93 kEngagementParams, kMaxPointsPerDayParam); | |
94 std::string navigation_points_param = variations::GetVariationParamValue( | |
95 kEngagementParams, kNavigationPointsParam); | |
96 std::string user_input_points_param = variations::GetVariationParamValue( | |
97 kEngagementParams, kUserInputPointsParam); | |
98 std::string decay_period_in_days_param = variations::GetVariationParamValue( | |
99 kEngagementParams, kDecayPeriodInDaysParam); | |
100 std::string decay_points_param = variations::GetVariationParamValue( | |
101 kEngagementParams, kDecayPointsParam); | |
102 | |
103 if (!max_points_per_day_param.empty() && !navigation_points_param.empty() && | |
104 !user_input_points_param.empty() && !decay_period_in_days_param.empty() && | |
105 !decay_points_param.empty()) { | |
106 double max_points_per_day = 0; | |
107 double navigation_points = 0; | |
108 double user_input_points = 0; | |
109 int decay_period_in_days = 0; | |
110 double decay_points = 0; | |
111 | |
112 if (base::StringToDouble(max_points_per_day_param, &max_points_per_day) && | |
113 base::StringToDouble(navigation_points_param, &navigation_points) && | |
114 base::StringToDouble(user_input_points_param, &user_input_points) && | |
115 base::StringToInt(decay_period_in_days_param, &decay_period_in_days) && | |
116 base::StringToDouble(decay_points_param, &decay_points) && | |
117 max_points_per_day >= navigation_points && | |
118 max_points_per_day >= user_input_points && navigation_points >= 0 && | |
119 user_input_points >= 0 && decay_period_in_days > 0 && | |
120 decay_points >= 0) { | |
121 gMaxPointsPerDay = max_points_per_day; | |
122 gNavigationPoints = navigation_points; | |
123 gUserInputPoints = user_input_points; | |
124 gDecayPeriodInDays = decay_period_in_days; | |
125 gDecayPoints = decay_points; | |
126 } | |
127 } | |
128 } | |
79 | 129 |
80 SiteEngagementScore::SiteEngagementScore( | 130 SiteEngagementScore::SiteEngagementScore( |
81 base::Clock* clock, | 131 base::Clock* clock, |
82 const base::DictionaryValue& score_dict) | 132 const base::DictionaryValue& score_dict) |
83 : SiteEngagementScore(clock) { | 133 : SiteEngagementScore(clock) { |
84 score_dict.GetDouble(kRawScoreKey, &raw_score_); | 134 score_dict.GetDouble(kRawScoreKey, &raw_score_); |
85 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); | 135 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); |
86 double internal_time; | 136 double internal_time; |
87 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time)) | 137 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time)) |
88 last_engagement_time_ = base::Time::FromInternalValue(internal_time); | 138 last_engagement_time_ = base::Time::FromInternalValue(internal_time); |
(...skipping 11 matching lines...) Expand all Loading... | |
100 // since the last update. | 150 // since the last update. |
101 raw_score_ = DecayedScore(); | 151 raw_score_ = DecayedScore(); |
102 | 152 |
103 base::Time now = clock_->Now(); | 153 base::Time now = clock_->Now(); |
104 if (!last_engagement_time_.is_null() && | 154 if (!last_engagement_time_.is_null() && |
105 now.LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 155 now.LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
106 points_added_today_ = 0; | 156 points_added_today_ = 0; |
107 } | 157 } |
108 | 158 |
109 double to_add = | 159 double to_add = |
110 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); | 160 std::min(kMaxPoints - raw_score_, gMaxPointsPerDay - points_added_today_); |
111 to_add = std::min(to_add, points); | 161 to_add = std::min(to_add, points); |
112 | 162 |
113 points_added_today_ += to_add; | 163 points_added_today_ += to_add; |
114 raw_score_ += to_add; | 164 raw_score_ += to_add; |
115 | 165 |
116 last_engagement_time_ = now; | 166 last_engagement_time_ = now; |
117 } | 167 } |
118 | 168 |
119 bool SiteEngagementScore::MaxPointsPerDayAdded() { | 169 bool SiteEngagementScore::MaxPointsPerDayAdded() { |
120 if (!last_engagement_time_.is_null() && | 170 if (!last_engagement_time_.is_null() && |
121 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 171 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
122 return false; | 172 return false; |
123 } | 173 } |
124 | 174 |
125 return points_added_today_ == kMaxPointsPerDay; | 175 return points_added_today_ == gMaxPointsPerDay; |
126 } | 176 } |
127 | 177 |
128 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { | 178 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
129 double raw_score_orig = 0; | 179 double raw_score_orig = 0; |
130 double points_added_today_orig = 0; | 180 double points_added_today_orig = 0; |
131 double last_engagement_time_internal_orig = 0; | 181 double last_engagement_time_internal_orig = 0; |
132 | 182 |
133 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); | 183 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); |
134 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); | 184 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); |
135 score_dict->GetDouble(kLastEngagementTimeKey, | 185 score_dict->GetDouble(kLastEngagementTimeKey, |
(...skipping 25 matching lines...) Expand all Loading... | |
161 | 211 |
162 double SiteEngagementScore::DecayedScore() const { | 212 double SiteEngagementScore::DecayedScore() const { |
163 // Note that users can change their clock, so from this system's perspective | 213 // Note that users can change their clock, so from this system's perspective |
164 // time can go backwards. If that does happen and the system detects that the | 214 // time can go backwards. If that does happen and the system detects that the |
165 // current day is earlier than the last engagement, no decay (or growth) is | 215 // current day is earlier than the last engagement, no decay (or growth) is |
166 // applied. | 216 // applied. |
167 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays(); | 217 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays(); |
168 if (days_since_engagement < 0) | 218 if (days_since_engagement < 0) |
169 return raw_score_; | 219 return raw_score_; |
170 | 220 |
171 int periods = days_since_engagement / kDecayPeriodInDays; | 221 int periods = days_since_engagement / gDecayPeriodInDays; |
172 double decayed_score = raw_score_ - periods * kDecayPoints; | 222 double decayed_score = raw_score_ - periods * gDecayPoints; |
173 return std::max(0.0, decayed_score); | 223 return std::max(0.0, decayed_score); |
174 } | 224 } |
175 | 225 |
176 // static | 226 // static |
177 SiteEngagementService* SiteEngagementService::Get(Profile* profile) { | 227 SiteEngagementService* SiteEngagementService::Get(Profile* profile) { |
178 return SiteEngagementServiceFactory::GetForProfile(profile); | 228 return SiteEngagementServiceFactory::GetForProfile(profile); |
179 } | 229 } |
180 | 230 |
181 // static | 231 // static |
182 bool SiteEngagementService::IsEnabled() { | 232 bool SiteEngagementService::IsEnabled() { |
(...skipping 10 matching lines...) Expand all Loading... | |
193 weak_factory_.GetWeakPtr())); | 243 weak_factory_.GetWeakPtr())); |
194 } | 244 } |
195 | 245 |
196 SiteEngagementService::~SiteEngagementService() { | 246 SiteEngagementService::~SiteEngagementService() { |
197 } | 247 } |
198 | 248 |
199 void SiteEngagementService::HandleNavigation(const GURL& url, | 249 void SiteEngagementService::HandleNavigation(const GURL& url, |
200 ui::PageTransition transition) { | 250 ui::PageTransition transition) { |
201 SiteEngagementMetrics::RecordEngagement( | 251 SiteEngagementMetrics::RecordEngagement( |
202 SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); | 252 SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); |
203 AddPoints(url, SiteEngagementScore::kNavigationPoints); | 253 AddPoints(url, SiteEngagementScore::gNavigationPoints); |
204 | 254 |
205 if (clock_->Now() - last_metrics_time_ >= metrics_interval) | 255 if (clock_->Now() - last_metrics_time_ >= metrics_interval) |
206 RecordMetrics(); | 256 RecordMetrics(); |
207 } | 257 } |
208 | 258 |
209 void SiteEngagementService::HandleUserInput( | 259 void SiteEngagementService::HandleUserInput( |
210 const GURL& url, | 260 const GURL& url, |
211 SiteEngagementMetrics::EngagementType type) { | 261 SiteEngagementMetrics::EngagementType type) { |
212 SiteEngagementMetrics::RecordEngagement(type); | 262 SiteEngagementMetrics::RecordEngagement(type); |
213 AddPoints(url, SiteEngagementScore::kUserInputPoints); | 263 AddPoints(url, SiteEngagementScore::gUserInputPoints); |
214 | 264 |
215 if (clock_->Now() - last_metrics_time_ >= metrics_interval) | 265 if (clock_->Now() - last_metrics_time_ >= metrics_interval) |
216 RecordMetrics(); | 266 RecordMetrics(); |
217 } | 267 } |
218 | 268 |
219 double SiteEngagementService::GetScore(const GURL& url) { | 269 double SiteEngagementService::GetScore(const GURL& url) { |
220 HostContentSettingsMap* settings_map = | 270 HostContentSettingsMap* settings_map = |
221 HostContentSettingsMapFactory::GetForProfile(profile_); | 271 HostContentSettingsMapFactory::GetForProfile(profile_); |
222 scoped_ptr<base::DictionaryValue> score_dict = | 272 scoped_ptr<base::DictionaryValue> score_dict = |
223 GetScoreDictForOrigin(settings_map, url); | 273 GetScoreDictForOrigin(settings_map, url); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 return; | 326 return; |
277 | 327 |
278 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | 328 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
279 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 329 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
280 std::string(), score_dict.release()); | 330 std::string(), score_dict.release()); |
281 } | 331 } |
282 } | 332 } |
283 | 333 |
284 void SiteEngagementService::AfterStartupTask() { | 334 void SiteEngagementService::AfterStartupTask() { |
285 CleanupEngagementScores(); | 335 CleanupEngagementScores(); |
336 SiteEngagementScore::UpdateFromVariations(); | |
benwells
2015/10/02 01:37:42
I think this is the wrong place for this, as there
dominickn
2015/10/02 06:29:05
Done.
| |
286 RecordMetrics(); | 337 RecordMetrics(); |
287 } | 338 } |
288 | 339 |
289 void SiteEngagementService::CleanupEngagementScores() { | 340 void SiteEngagementService::CleanupEngagementScores() { |
290 HostContentSettingsMap* settings_map = | 341 HostContentSettingsMap* settings_map = |
291 HostContentSettingsMapFactory::GetForProfile(profile_); | 342 HostContentSettingsMapFactory::GetForProfile(profile_); |
292 scoped_ptr<ContentSettingsForOneType> engagement_settings = | 343 scoped_ptr<ContentSettingsForOneType> engagement_settings = |
293 GetEngagementContentSettings(settings_map); | 344 GetEngagementContentSettings(settings_map); |
294 | 345 |
295 for (const auto& site : *engagement_settings) { | 346 for (const auto& site : *engagement_settings) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 SiteEngagementMetrics::RecordAverageEngagement(average_engagement); | 413 SiteEngagementMetrics::RecordAverageEngagement(average_engagement); |
363 SiteEngagementMetrics::RecordEngagementScores(score_map); | 414 SiteEngagementMetrics::RecordEngagementScores(score_map); |
364 | 415 |
365 SiteEngagementMetrics::RecordOriginsWithMaxDailyEngagement( | 416 SiteEngagementMetrics::RecordOriginsWithMaxDailyEngagement( |
366 OriginsWithMaxDailyEngagement()); | 417 OriginsWithMaxDailyEngagement()); |
367 SiteEngagementMetrics::RecordOriginsWithMaxEngagement( | 418 SiteEngagementMetrics::RecordOriginsWithMaxEngagement( |
368 origins_with_max_engagement); | 419 origins_with_max_engagement); |
369 SiteEngagementMetrics::RecordPercentOriginsWithMaxEngagement( | 420 SiteEngagementMetrics::RecordPercentOriginsWithMaxEngagement( |
370 percent_origins_with_max_engagement); | 421 percent_origins_with_max_engagement); |
371 } | 422 } |
OLD | NEW |