Chromium Code Reviews| 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 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // Global bool to ensure we only update the parameters from variations once. | 30 // Global bool to ensure we only update the parameters from variations once. |
| 31 bool g_updated_from_variations = false; | 31 bool g_updated_from_variations = false; |
| 32 | 32 |
| 33 // Keys used in the variations params. | 33 // Keys used in the variations params. |
| 34 const char kEngagementParams[] = "SiteEngagement"; | 34 const char kEngagementParams[] = "SiteEngagement"; |
| 35 const char kMaxPointsPerDayParam[] = "max_points_per_day"; | 35 const char kMaxPointsPerDayParam[] = "max_points_per_day"; |
| 36 const char kNavigationPointsParam[] = "navigation_points"; | 36 const char kNavigationPointsParam[] = "navigation_points"; |
| 37 const char kUserInputPointsParam[] = "user_input_points"; | 37 const char kUserInputPointsParam[] = "user_input_points"; |
| 38 const char kVisibleMediaPlayingPointsParam[] = "visible_media_playing"; | |
| 39 const char kHiddenMediaPlayingPointsParam[] = "hidden_media_playing"; | |
|
calamity
2015/11/04 02:08:10
nit: These should both have _points suffixed.
dominickn
2015/11/04 04:20:32
Done.
| |
| 38 const char kDecayPeriodInDaysParam[] = "decay_period_in_days"; | 40 const char kDecayPeriodInDaysParam[] = "decay_period_in_days"; |
| 39 const char kDecayPointsParam[] = "decay_points"; | 41 const char kDecayPointsParam[] = "decay_points"; |
| 40 | 42 |
| 41 // Length of time between metrics logging. | 43 // Length of time between metrics logging. |
| 42 const base::TimeDelta metrics_interval = base::TimeDelta::FromMinutes(60); | 44 const base::TimeDelta metrics_interval = base::TimeDelta::FromMinutes(60); |
| 43 | 45 |
| 44 // Delta within which to consider scores equal. | 46 // Delta within which to consider scores equal. |
| 45 const double kScoreDelta = 0.001; | 47 const double kScoreDelta = 0.001; |
| 46 | 48 |
| 47 // Delta within which to consider internal time values equal. Internal time | 49 // Delta within which to consider internal time values equal. Internal time |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 | 97 |
| 96 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 98 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
| 97 } | 99 } |
| 98 | 100 |
| 99 } // namespace | 101 } // namespace |
| 100 | 102 |
| 101 const double SiteEngagementScore::kMaxPoints = 100; | 103 const double SiteEngagementScore::kMaxPoints = 100; |
| 102 double SiteEngagementScore::g_max_points_per_day = 5; | 104 double SiteEngagementScore::g_max_points_per_day = 5; |
| 103 double SiteEngagementScore::g_navigation_points = 0.5; | 105 double SiteEngagementScore::g_navigation_points = 0.5; |
| 104 double SiteEngagementScore::g_user_input_points = 0.05; | 106 double SiteEngagementScore::g_user_input_points = 0.05; |
| 107 double SiteEngagementScore::g_visible_media_playing_points = 0.02; | |
| 108 double SiteEngagementScore::g_hidden_media_playing_points = 0.01; | |
| 105 int SiteEngagementScore::g_decay_period_in_days = 7; | 109 int SiteEngagementScore::g_decay_period_in_days = 7; |
| 106 double SiteEngagementScore::g_decay_points = 5; | 110 double SiteEngagementScore::g_decay_points = 5; |
| 107 | 111 |
| 108 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 112 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
| 109 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 113 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
| 110 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 114 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
| 111 | 115 |
| 112 void SiteEngagementScore::UpdateFromVariations() { | 116 void SiteEngagementScore::UpdateFromVariations() { |
| 113 std::string max_points_per_day_param = variations::GetVariationParamValue( | 117 std::string max_points_per_day_param = variations::GetVariationParamValue( |
| 114 kEngagementParams, kMaxPointsPerDayParam); | 118 kEngagementParams, kMaxPointsPerDayParam); |
| 115 std::string navigation_points_param = variations::GetVariationParamValue( | 119 std::string navigation_points_param = variations::GetVariationParamValue( |
| 116 kEngagementParams, kNavigationPointsParam); | 120 kEngagementParams, kNavigationPointsParam); |
| 117 std::string user_input_points_param = variations::GetVariationParamValue( | 121 std::string user_input_points_param = variations::GetVariationParamValue( |
| 118 kEngagementParams, kUserInputPointsParam); | 122 kEngagementParams, kUserInputPointsParam); |
| 123 std::string visible_media_playing_points_param = | |
| 124 variations::GetVariationParamValue(kEngagementParams, | |
| 125 kVisibleMediaPlayingPointsParam); | |
| 126 std::string hidden_media_playing_points_param = | |
| 127 variations::GetVariationParamValue(kEngagementParams, | |
| 128 kHiddenMediaPlayingPointsParam); | |
| 119 std::string decay_period_in_days_param = variations::GetVariationParamValue( | 129 std::string decay_period_in_days_param = variations::GetVariationParamValue( |
| 120 kEngagementParams, kDecayPeriodInDaysParam); | 130 kEngagementParams, kDecayPeriodInDaysParam); |
| 121 std::string decay_points_param = variations::GetVariationParamValue( | 131 std::string decay_points_param = variations::GetVariationParamValue( |
| 122 kEngagementParams, kDecayPointsParam); | 132 kEngagementParams, kDecayPointsParam); |
| 123 | 133 |
| 124 if (!max_points_per_day_param.empty() && !navigation_points_param.empty() && | 134 if (!max_points_per_day_param.empty() && !navigation_points_param.empty() && |
| 125 !user_input_points_param.empty() && !decay_period_in_days_param.empty() && | 135 !user_input_points_param.empty() && |
| 126 !decay_points_param.empty()) { | 136 !visible_media_playing_points_param.empty() && |
| 137 !hidden_media_playing_points_param.empty() && | |
| 138 !decay_period_in_days_param.empty() && !decay_points_param.empty()) { | |
| 127 double max_points_per_day = 0; | 139 double max_points_per_day = 0; |
| 128 double navigation_points = 0; | 140 double navigation_points = 0; |
| 129 double user_input_points = 0; | 141 double user_input_points = 0; |
| 142 double visible_media_playing_points = 0; | |
| 143 double hidden_media_playing_points = 0; | |
| 130 int decay_period_in_days = 0; | 144 int decay_period_in_days = 0; |
| 131 double decay_points = 0; | 145 double decay_points = 0; |
| 132 | 146 |
| 133 if (base::StringToDouble(max_points_per_day_param, &max_points_per_day) && | 147 if (base::StringToDouble(max_points_per_day_param, &max_points_per_day) && |
| 134 base::StringToDouble(navigation_points_param, &navigation_points) && | 148 base::StringToDouble(navigation_points_param, &navigation_points) && |
| 135 base::StringToDouble(user_input_points_param, &user_input_points) && | 149 base::StringToDouble(user_input_points_param, &user_input_points) && |
| 150 base::StringToDouble(visible_media_playing_points_param, | |
| 151 &visible_media_playing_points) && | |
| 152 base::StringToDouble(hidden_media_playing_points_param, | |
| 153 &hidden_media_playing_points) && | |
| 136 base::StringToInt(decay_period_in_days_param, &decay_period_in_days) && | 154 base::StringToInt(decay_period_in_days_param, &decay_period_in_days) && |
| 137 base::StringToDouble(decay_points_param, &decay_points) && | 155 base::StringToDouble(decay_points_param, &decay_points) && |
| 138 max_points_per_day >= navigation_points && | 156 max_points_per_day >= navigation_points && |
| 139 max_points_per_day >= user_input_points && navigation_points >= 0 && | 157 max_points_per_day >= user_input_points && navigation_points >= 0 && |
| 140 user_input_points >= 0 && decay_period_in_days > 0 && | 158 user_input_points >= 0 && decay_period_in_days > 0 && |
| 141 decay_points >= 0) { | 159 decay_points >= 0) { |
| 142 g_max_points_per_day = max_points_per_day; | 160 g_max_points_per_day = max_points_per_day; |
| 143 g_navigation_points = navigation_points; | 161 g_navigation_points = navigation_points; |
| 144 g_user_input_points = user_input_points; | 162 g_user_input_points = user_input_points; |
| 163 g_visible_media_playing_points = visible_media_playing_points; | |
| 164 g_hidden_media_playing_points = hidden_media_playing_points; | |
| 145 g_decay_period_in_days = decay_period_in_days; | 165 g_decay_period_in_days = decay_period_in_days; |
| 146 g_decay_points = decay_points; | 166 g_decay_points = decay_points; |
| 147 } | 167 } |
| 148 } | 168 } |
| 149 } | 169 } |
| 150 | 170 |
| 151 SiteEngagementScore::SiteEngagementScore( | 171 SiteEngagementScore::SiteEngagementScore( |
| 152 base::Clock* clock, | 172 base::Clock* clock, |
| 153 const base::DictionaryValue& score_dict) | 173 const base::DictionaryValue& score_dict) |
| 154 : SiteEngagementScore(clock) { | 174 : SiteEngagementScore(clock) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 } | 331 } |
| 312 | 332 |
| 313 void SiteEngagementService::HandleUserInput( | 333 void SiteEngagementService::HandleUserInput( |
| 314 const GURL& url, | 334 const GURL& url, |
| 315 SiteEngagementMetrics::EngagementType type) { | 335 SiteEngagementMetrics::EngagementType type) { |
| 316 SiteEngagementMetrics::RecordEngagement(type); | 336 SiteEngagementMetrics::RecordEngagement(type); |
| 317 AddPoints(url, SiteEngagementScore::g_user_input_points); | 337 AddPoints(url, SiteEngagementScore::g_user_input_points); |
| 318 RecordMetrics(); | 338 RecordMetrics(); |
| 319 } | 339 } |
| 320 | 340 |
| 341 void SiteEngagementService::HandleMediaPlaying(const GURL& url, | |
| 342 bool is_hidden) { | |
| 343 SiteEngagementMetrics::RecordEngagement( | |
| 344 is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN | |
| 345 : SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE); | |
| 346 AddPoints(url, is_hidden | |
| 347 ? SiteEngagementScore::g_hidden_media_playing_points | |
| 348 : SiteEngagementScore::g_visible_media_playing_points); | |
| 349 RecordMetrics(); | |
| 350 } | |
| 351 | |
| 321 double SiteEngagementService::GetScore(const GURL& url) { | 352 double SiteEngagementService::GetScore(const GURL& url) { |
| 322 HostContentSettingsMap* settings_map = | 353 HostContentSettingsMap* settings_map = |
| 323 HostContentSettingsMapFactory::GetForProfile(profile_); | 354 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 324 scoped_ptr<base::DictionaryValue> score_dict = | 355 scoped_ptr<base::DictionaryValue> score_dict = |
| 325 GetScoreDictForOrigin(settings_map, url); | 356 GetScoreDictForOrigin(settings_map, url); |
| 326 SiteEngagementScore score(clock_.get(), *score_dict); | 357 SiteEngagementScore score(clock_.get(), *score_dict); |
| 327 | 358 |
| 328 return score.Score(); | 359 return score.Score(); |
| 329 } | 360 } |
| 330 | 361 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 491 int SiteEngagementService::OriginsWithMaxEngagement( | 522 int SiteEngagementService::OriginsWithMaxEngagement( |
| 492 std::map<GURL, double>& score_map) { | 523 std::map<GURL, double>& score_map) { |
| 493 int total_origins = 0; | 524 int total_origins = 0; |
| 494 | 525 |
| 495 for (const auto& value : score_map) | 526 for (const auto& value : score_map) |
| 496 if (value.second == SiteEngagementScore::kMaxPoints) | 527 if (value.second == SiteEngagementScore::kMaxPoints) |
| 497 ++total_origins; | 528 ++total_origins; |
| 498 | 529 |
| 499 return total_origins; | 530 return total_origins; |
| 500 } | 531 } |
| OLD | NEW |