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 | 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/values.h" | 13 #include "base/values.h" |
| 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 14 #include "chrome/browser/engagement/site_engagement_helper.h" | 15 #include "chrome/browser/engagement/site_engagement_helper.h" |
| 15 #include "chrome/browser/engagement/site_engagement_metrics.h" | 16 #include "chrome/browser/engagement/site_engagement_metrics.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 "url/gurl.h" | 22 #include "url/gurl.h" |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 // Delta within which to consider scores equal. | 26 // Delta within which to consider scores equal. |
| 25 const double kScoreDelta = 0.001; | 27 const double kScoreDelta = 0.001; |
| 26 | 28 |
| 27 // Delta within which to consider internal time values equal. Internal time | 29 // Delta within which to consider internal time values equal. Internal time |
| 28 // values are in microseconds, so this delta comes out at one second. | 30 // values are in microseconds, so this delta comes out at one second. |
| 29 const double kTimeDelta = 1000000; | 31 const double kTimeDelta = 1000000; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 50 | 52 |
| 51 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 53 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
| 52 } | 54 } |
| 53 | 55 |
| 54 } // namespace | 56 } // namespace |
| 55 | 57 |
| 56 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 58 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
| 57 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 59 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
| 58 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 60 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
| 59 | 61 |
| 62 const char* SiteEngagementScore::kEngagementParams = "SiteEngagementParams"; | |
| 63 const char* SiteEngagementScore::kMaxPointsPerDayParam = "max_points_per_day"; | |
| 64 const char* SiteEngagementScore::kNavigationPointsParam = "navigation_points"; | |
| 65 const char* SiteEngagementScore::kUserInputPointsParam = "user_input_points"; | |
| 66 const char* SiteEngagementScore::kDecayPeriodInDaysParam = | |
| 67 "decay_period_in_days"; | |
| 68 const char* SiteEngagementScore::kDecayPointsParam = "decay_points"; | |
| 69 | |
| 60 const double SiteEngagementScore::kMaxPoints = 100; | 70 const double SiteEngagementScore::kMaxPoints = 100; |
| 61 const double SiteEngagementScore::kMaxPointsPerDay = 5; | 71 double SiteEngagementScore::gMaxPointsPerDay = 5; |
| 62 const double SiteEngagementScore::kNavigationPoints = 0.5; | 72 double SiteEngagementScore::gNavigationPoints = 0.5; |
| 63 const double SiteEngagementScore::kUserInputPoints = 0.05; | 73 double SiteEngagementScore::gUserInputPoints = 0.05; |
| 64 const int SiteEngagementScore::kDecayPeriodInDays = 7; | 74 int SiteEngagementScore::gDecayPeriodInDays = 7; |
| 65 const double SiteEngagementScore::kDecayPoints = 5; | 75 double SiteEngagementScore::gDecayPoints = 5; |
| 76 | |
| 77 // static | |
| 78 void SiteEngagementScore::UpdateFromVariations() { | |
|
benwells
2015/09/29 00:40:31
Should something be calling this?
dominickn
2015/09/29 00:52:30
It will once calamity's clean up CL lands. Then I
benwells
2015/09/29 01:10:44
OK. Functions overwriting global variables feel we
| |
| 79 std::string max_points_per_day_param = variations::GetVariationParamValue( | |
| 80 kEngagementParams, kMaxPointsPerDayParam); | |
| 81 std::string navigation_points_param = variations::GetVariationParamValue( | |
| 82 kEngagementParams, kNavigationPointsParam); | |
| 83 std::string user_input_points_param = variations::GetVariationParamValue( | |
| 84 kEngagementParams, kUserInputPointsParam); | |
| 85 std::string decay_period_in_days_param = variations::GetVariationParamValue( | |
| 86 kEngagementParams, kDecayPeriodInDaysParam); | |
| 87 std::string decay_points_param = variations::GetVariationParamValue( | |
| 88 kEngagementParams, kDecayPointsParam); | |
| 89 | |
| 90 if (!max_points_per_day_param.empty() && !navigation_points_param.empty() && | |
| 91 !user_input_points_param.empty() && !decay_period_in_days_param.empty() && | |
| 92 !decay_points_param.empty()) { | |
| 93 double max_points_per_day = 0; | |
| 94 double navigation_points = 0; | |
| 95 double user_input_points = 0; | |
| 96 int decay_period_in_days = 0; | |
| 97 double decay_points = 0; | |
| 98 | |
| 99 if (base::StringToDouble(max_points_per_day_param, &max_points_per_day) && | |
| 100 base::StringToDouble(navigation_points_param, &navigation_points) && | |
| 101 base::StringToDouble(user_input_points_param, &user_input_points) && | |
| 102 base::StringToInt(decay_period_in_days_param, &decay_period_in_days) && | |
| 103 base::StringToDouble(decay_points_param, &decay_points) && | |
| 104 max_points_per_day >= navigation_points && | |
| 105 max_points_per_day >= user_input_points && navigation_points >= 0 && | |
| 106 user_input_points >= 0 && decay_period_in_days > 0 && | |
| 107 decay_points >= 0) { | |
| 108 gMaxPointsPerDay = max_points_per_day; | |
| 109 gNavigationPoints = navigation_points; | |
| 110 gUserInputPoints = user_input_points; | |
| 111 gDecayPeriodInDays = decay_period_in_days; | |
| 112 gDecayPoints = decay_points; | |
| 113 } | |
| 114 } | |
| 115 } | |
| 66 | 116 |
| 67 SiteEngagementScore::SiteEngagementScore( | 117 SiteEngagementScore::SiteEngagementScore( |
| 68 base::Clock* clock, | 118 base::Clock* clock, |
| 69 const base::DictionaryValue& score_dict) | 119 const base::DictionaryValue& score_dict) |
| 70 : SiteEngagementScore(clock) { | 120 : SiteEngagementScore(clock) { |
| 71 score_dict.GetDouble(kRawScoreKey, &raw_score_); | 121 score_dict.GetDouble(kRawScoreKey, &raw_score_); |
| 72 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); | 122 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); |
| 73 double internal_time; | 123 double internal_time; |
| 74 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time)) | 124 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time)) |
| 75 last_engagement_time_ = base::Time::FromInternalValue(internal_time); | 125 last_engagement_time_ = base::Time::FromInternalValue(internal_time); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 87 // since the last update. | 137 // since the last update. |
| 88 raw_score_ = DecayedScore(); | 138 raw_score_ = DecayedScore(); |
| 89 | 139 |
| 90 base::Time now = clock_->Now(); | 140 base::Time now = clock_->Now(); |
| 91 if (!last_engagement_time_.is_null() && | 141 if (!last_engagement_time_.is_null() && |
| 92 now.LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 142 now.LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
| 93 points_added_today_ = 0; | 143 points_added_today_ = 0; |
| 94 } | 144 } |
| 95 | 145 |
| 96 double to_add = | 146 double to_add = |
| 97 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); | 147 std::min(kMaxPoints - raw_score_, gMaxPointsPerDay - points_added_today_); |
| 98 to_add = std::min(to_add, points); | 148 to_add = std::min(to_add, points); |
| 99 | 149 |
| 100 points_added_today_ += to_add; | 150 points_added_today_ += to_add; |
| 101 raw_score_ += to_add; | 151 raw_score_ += to_add; |
| 102 | 152 |
| 103 last_engagement_time_ = now; | 153 last_engagement_time_ = now; |
| 104 } | 154 } |
| 105 | 155 |
| 106 bool SiteEngagementScore::MaxPointsPerDayAdded() { | 156 bool SiteEngagementScore::MaxPointsPerDayAdded() { |
| 107 if (!last_engagement_time_.is_null() && | 157 if (!last_engagement_time_.is_null() && |
| 108 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 158 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
| 109 points_added_today_ = 0; | 159 points_added_today_ = 0; |
| 110 } | 160 } |
| 111 | 161 |
| 112 return points_added_today_ == kMaxPointsPerDay; | 162 return points_added_today_ == gMaxPointsPerDay; |
| 113 } | 163 } |
| 114 | 164 |
| 115 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { | 165 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| 116 double raw_score_orig = 0; | 166 double raw_score_orig = 0; |
| 117 double points_added_today_orig = 0; | 167 double points_added_today_orig = 0; |
| 118 double last_engagement_time_internal_orig = 0; | 168 double last_engagement_time_internal_orig = 0; |
| 119 | 169 |
| 120 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); | 170 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); |
| 121 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); | 171 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); |
| 122 score_dict->GetDouble(kLastEngagementTimeKey, | 172 score_dict->GetDouble(kLastEngagementTimeKey, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 148 | 198 |
| 149 double SiteEngagementScore::DecayedScore() const { | 199 double SiteEngagementScore::DecayedScore() const { |
| 150 // Note that users can change their clock, so from this system's perspective | 200 // Note that users can change their clock, so from this system's perspective |
| 151 // time can go backwards. If that does happen and the system detects that the | 201 // time can go backwards. If that does happen and the system detects that the |
| 152 // current day is earlier than the last engagement, no decay (or growth) is | 202 // current day is earlier than the last engagement, no decay (or growth) is |
| 153 // applied. | 203 // applied. |
| 154 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays(); | 204 int days_since_engagement = (clock_->Now() - last_engagement_time_).InDays(); |
| 155 if (days_since_engagement < 0) | 205 if (days_since_engagement < 0) |
| 156 return raw_score_; | 206 return raw_score_; |
| 157 | 207 |
| 158 int periods = days_since_engagement / kDecayPeriodInDays; | 208 int periods = days_since_engagement / gDecayPeriodInDays; |
| 159 double decayed_score = raw_score_ - periods * kDecayPoints; | 209 double decayed_score = raw_score_ - periods * gDecayPoints; |
| 160 return std::max(0.0, decayed_score); | 210 return std::max(0.0, decayed_score); |
| 161 } | 211 } |
| 162 | 212 |
| 163 // static | 213 // static |
| 164 SiteEngagementService* SiteEngagementService::Get(Profile* profile) { | 214 SiteEngagementService* SiteEngagementService::Get(Profile* profile) { |
| 165 return SiteEngagementServiceFactory::GetForProfile(profile); | 215 return SiteEngagementServiceFactory::GetForProfile(profile); |
| 166 } | 216 } |
| 167 | 217 |
| 168 // static | 218 // static |
| 169 bool SiteEngagementService::IsEnabled() { | 219 bool SiteEngagementService::IsEnabled() { |
| 170 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 220 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 171 switches::kEnableSiteEngagementService); | 221 switches::kEnableSiteEngagementService); |
| 172 } | 222 } |
| 173 | 223 |
| 174 SiteEngagementService::SiteEngagementService(Profile* profile) | 224 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 175 : profile_(profile) { | 225 : profile_(profile) { |
| 176 RecordStartupUmaStats(); | 226 RecordStartupUmaStats(); |
| 177 } | 227 } |
| 178 | 228 |
| 179 SiteEngagementService::~SiteEngagementService() { | 229 SiteEngagementService::~SiteEngagementService() { |
| 180 } | 230 } |
| 181 | 231 |
| 182 void SiteEngagementService::HandleNavigation(const GURL& url, | 232 void SiteEngagementService::HandleNavigation(const GURL& url, |
| 183 ui::PageTransition transition) { | 233 ui::PageTransition transition) { |
| 184 AddPoints(url, SiteEngagementScore::kNavigationPoints); | 234 AddPoints(url, SiteEngagementScore::gNavigationPoints); |
| 185 } | 235 } |
| 186 | 236 |
| 187 void SiteEngagementService::HandleUserInput(const GURL& url) { | 237 void SiteEngagementService::HandleUserInput(const GURL& url) { |
| 188 AddPoints(url, SiteEngagementScore::kUserInputPoints); | 238 AddPoints(url, SiteEngagementScore::gUserInputPoints); |
| 189 } | 239 } |
| 190 | 240 |
| 191 double SiteEngagementService::GetScore(const GURL& url) { | 241 double SiteEngagementService::GetScore(const GURL& url) { |
| 192 HostContentSettingsMap* settings_map = | 242 HostContentSettingsMap* settings_map = |
| 193 HostContentSettingsMapFactory::GetForProfile(profile_); | 243 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 194 scoped_ptr<base::DictionaryValue> score_dict = | 244 scoped_ptr<base::DictionaryValue> score_dict = |
| 195 GetScoreDictForOrigin(settings_map, url); | 245 GetScoreDictForOrigin(settings_map, url); |
| 196 SiteEngagementScore score(&clock_, *score_dict); | 246 SiteEngagementScore score(&clock_, *score_dict); |
| 197 | 247 |
| 198 return score.Score(); | 248 return score.Score(); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 std::map<GURL, double> score_map = GetScoreMap(); | 349 std::map<GURL, double> score_map = GetScoreMap(); |
| 300 | 350 |
| 301 double total_score = 0; | 351 double total_score = 0; |
| 302 for (const auto& value : score_map) | 352 for (const auto& value : score_map) |
| 303 total_score += value.second; | 353 total_score += value.second; |
| 304 | 354 |
| 305 SiteEngagementMetrics::RecordTotalOriginsEngaged(score_map.size()); | 355 SiteEngagementMetrics::RecordTotalOriginsEngaged(score_map.size()); |
| 306 SiteEngagementMetrics::RecordTotalCumulativeSiteEngagement(total_score); | 356 SiteEngagementMetrics::RecordTotalCumulativeSiteEngagement(total_score); |
| 307 SiteEngagementMetrics::RecordEngagementPerOrigin(score_map); | 357 SiteEngagementMetrics::RecordEngagementPerOrigin(score_map); |
| 308 } | 358 } |
| OLD | NEW |