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" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 double to_add = | 95 double to_add = |
| 96 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); | 96 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); |
| 97 to_add = std::min(to_add, points); | 97 to_add = std::min(to_add, points); |
| 98 | 98 |
| 99 points_added_today_ += to_add; | 99 points_added_today_ += to_add; |
| 100 raw_score_ += to_add; | 100 raw_score_ += to_add; |
| 101 | 101 |
| 102 last_engagement_time_ = now; | 102 last_engagement_time_ = now; |
| 103 } | 103 } |
| 104 | 104 |
| 105 bool SiteEngagementScore::MaxPointsPerDayAdded() { | |
| 106 if (!last_engagement_time_.is_null() && | |
| 107 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | |
| 108 points_added_today_ = 0; | |
|
benwells
2015/09/30 00:10:54
Why is this updated? When originally writing this
dominickn
2015/10/01 01:12:02
I added this update to ensure that day boundary ef
| |
| 109 } | |
| 110 | |
| 111 return points_added_today_ == kMaxPointsPerDay; | |
| 112 } | |
| 113 | |
| 105 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { | 114 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| 106 double raw_score_orig = 0; | 115 double raw_score_orig = 0; |
| 107 double points_added_today_orig = 0; | 116 double points_added_today_orig = 0; |
| 108 double last_engagement_time_internal_orig = 0; | 117 double last_engagement_time_internal_orig = 0; |
| 109 | 118 |
| 110 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); | 119 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); |
| 111 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); | 120 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); |
| 112 score_dict->GetDouble(kLastEngagementTimeKey, | 121 score_dict->GetDouble(kLastEngagementTimeKey, |
| 113 &last_engagement_time_internal_orig); | 122 &last_engagement_time_internal_orig); |
| 114 bool changed = | 123 bool changed = |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 } | 165 } |
| 157 | 166 |
| 158 // static | 167 // static |
| 159 bool SiteEngagementService::IsEnabled() { | 168 bool SiteEngagementService::IsEnabled() { |
| 160 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 169 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 161 switches::kEnableSiteEngagementService); | 170 switches::kEnableSiteEngagementService); |
| 162 } | 171 } |
| 163 | 172 |
| 164 SiteEngagementService::SiteEngagementService(Profile* profile) | 173 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 165 : profile_(profile) { | 174 : profile_(profile) { |
| 175 RecordStartupUmaStats(); | |
| 166 } | 176 } |
| 167 | 177 |
| 168 SiteEngagementService::~SiteEngagementService() { | 178 SiteEngagementService::~SiteEngagementService() { |
| 169 } | 179 } |
| 170 | 180 |
| 171 void SiteEngagementService::HandleNavigation(const GURL& url, | 181 void SiteEngagementService::HandleNavigation(const GURL& url, |
| 172 ui::PageTransition transition) { | 182 ui::PageTransition transition) { |
| 183 SiteEngagementMetrics::RecordEngagement( | |
| 184 SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); | |
| 173 AddPoints(url, SiteEngagementScore::kNavigationPoints); | 185 AddPoints(url, SiteEngagementScore::kNavigationPoints); |
| 174 } | 186 } |
| 175 | 187 |
| 176 void SiteEngagementService::HandleUserInput(const GURL& url) { | 188 void SiteEngagementService::HandleUserInput( |
| 189 const GURL& url, | |
| 190 SiteEngagementMetrics::EngagementType type) { | |
| 191 SiteEngagementMetrics::RecordEngagement(type); | |
| 177 AddPoints(url, SiteEngagementScore::kUserInputPoints); | 192 AddPoints(url, SiteEngagementScore::kUserInputPoints); |
| 178 } | 193 } |
| 179 | 194 |
| 180 double SiteEngagementService::GetScore(const GURL& url) { | 195 double SiteEngagementService::GetScore(const GURL& url) { |
| 181 HostContentSettingsMap* settings_map = | 196 HostContentSettingsMap* settings_map = |
| 182 HostContentSettingsMapFactory::GetForProfile(profile_); | 197 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 183 scoped_ptr<base::DictionaryValue> score_dict = | 198 scoped_ptr<base::DictionaryValue> score_dict = |
| 184 GetScoreDictForOrigin(settings_map, url); | 199 GetScoreDictForOrigin(settings_map, url); |
| 185 SiteEngagementScore score(&clock_, *score_dict); | 200 SiteEngagementScore score(&clock_, *score_dict); |
| 186 | 201 |
| 187 return score.Score(); | 202 return score.Score(); |
| 188 } | 203 } |
| 189 | 204 |
| 190 double SiteEngagementService::GetTotalEngagementPoints() { | 205 double SiteEngagementService::GetTotalEngagementPoints() { |
| 191 HostContentSettingsMap* settings_map = | 206 std::map<GURL, double> score_map = GetScoreMap(); |
| 192 HostContentSettingsMapFactory::GetForProfile(profile_); | 207 |
| 193 ContentSettingsForOneType engagement_settings; | |
| 194 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
| 195 std::string(), &engagement_settings); | |
| 196 double total_score = 0; | 208 double total_score = 0; |
| 197 for (const auto& site : engagement_settings) { | 209 for (const auto& value : score_map) |
| 198 GURL origin(site.primary_pattern.ToString()); | 210 total_score += value.second; |
| 199 if (!origin.is_valid()) | |
| 200 continue; | |
| 201 | 211 |
| 202 scoped_ptr<base::DictionaryValue> score_dict = | |
| 203 GetScoreDictForOrigin(settings_map, origin); | |
| 204 SiteEngagementScore score(&clock_, *score_dict); | |
| 205 total_score += score.Score(); | |
| 206 } | |
| 207 return total_score; | 212 return total_score; |
| 208 } | 213 } |
| 209 | 214 |
| 210 std::map<GURL, double> SiteEngagementService::GetScoreMap() { | 215 std::map<GURL, double> SiteEngagementService::GetScoreMap() { |
| 211 std::map<GURL, double> score_map; | 216 std::map<GURL, double> score_map; |
| 212 HostContentSettingsMap* settings_map = | 217 HostContentSettingsMap* settings_map = |
| 213 HostContentSettingsMapFactory::GetForProfile(profile_); | 218 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 214 ContentSettingsForOneType engagement_settings; | 219 ContentSettingsForOneType engagement_settings; |
| 215 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 220 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 216 std::string(), &engagement_settings); | 221 std::string(), &engagement_settings); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 227 return score_map; | 232 return score_map; |
| 228 } | 233 } |
| 229 | 234 |
| 230 void SiteEngagementService::AddPoints(const GURL& url, double points) { | 235 void SiteEngagementService::AddPoints(const GURL& url, double points) { |
| 231 HostContentSettingsMap* settings_map = | 236 HostContentSettingsMap* settings_map = |
| 232 HostContentSettingsMapFactory::GetForProfile(profile_); | 237 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 233 scoped_ptr<base::DictionaryValue> score_dict = | 238 scoped_ptr<base::DictionaryValue> score_dict = |
| 234 GetScoreDictForOrigin(settings_map, url); | 239 GetScoreDictForOrigin(settings_map, url); |
| 235 SiteEngagementScore score(&clock_, *score_dict); | 240 SiteEngagementScore score(&clock_, *score_dict); |
| 236 | 241 |
| 242 double old_score = score.Score(); | |
| 243 | |
| 237 score.AddPoints(points); | 244 score.AddPoints(points); |
| 238 if (score.UpdateScoreDict(score_dict.get())) { | 245 if (score.UpdateScoreDict(score_dict.get())) { |
| 239 ContentSettingsPattern pattern( | 246 ContentSettingsPattern pattern( |
| 240 ContentSettingsPattern::FromURLNoWildcard(url)); | 247 ContentSettingsPattern::FromURLNoWildcard(url)); |
| 241 if (!pattern.IsValid()) | 248 if (!pattern.IsValid()) |
| 242 return; | 249 return; |
| 243 | 250 |
| 244 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | 251 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
| 245 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 252 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 246 std::string(), score_dict.release()); | 253 std::string(), score_dict.release()); |
| 247 } | 254 } |
| 255 | |
| 256 // If the origin has now received the maximum number of points it can today, | |
| 257 // record a UMA statistic of all of the origins with maxed daily engagement. | |
| 258 // As this is recorded each time an origin reaches the maximum, a client with | |
| 259 // N maxed origins will have recorded a statistic for 1, 2, ..., N-1, N. | |
| 260 if (score.MaxPointsPerDayAdded()) { | |
| 261 SiteEngagementMetrics::RecordOriginsWithMaxDailyEngagement( | |
| 262 OriginsWithMaxDailyEngagement()); | |
| 263 } | |
| 264 | |
| 265 // If the origin has now received the maximum total number of points, | |
| 266 // record a UMA statistic of all of the origins with maximum total points. | |
| 267 double new_score = score.Score(); | |
| 268 if (old_score != new_score && new_score == SiteEngagementScore::kMaxPoints) { | |
| 269 SiteEngagementMetrics::RecordOriginsWithMaxEngagement( | |
| 270 OriginsWithMaxEngagement()); | |
| 271 } | |
| 248 } | 272 } |
| 273 | |
| 274 int SiteEngagementService::OriginsWithMaxDailyEngagement() { | |
|
benwells
2015/09/30 00:10:54
Is there a reason not to use GetScoreMap to implem
dominickn
2015/10/01 01:12:02
The main reason was to avoid a double-loop over al
| |
| 275 HostContentSettingsMap* settings_map = | |
| 276 HostContentSettingsMapFactory::GetForProfile(profile_); | |
| 277 ContentSettingsForOneType engagement_settings; | |
| 278 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
| 279 std::string(), &engagement_settings); | |
| 280 int total_origins = 0; | |
| 281 for (const auto& site : engagement_settings) { | |
| 282 GURL origin(site.primary_pattern.ToString()); | |
| 283 if (!origin.is_valid()) | |
| 284 continue; | |
| 285 | |
| 286 scoped_ptr<base::DictionaryValue> score_dict = | |
| 287 GetScoreDictForOrigin(settings_map, origin); | |
| 288 SiteEngagementScore score(&clock_, *score_dict); | |
| 289 if (score.MaxPointsPerDayAdded()) | |
| 290 total_origins += 1; | |
| 291 } | |
| 292 | |
| 293 return total_origins; | |
| 294 } | |
| 295 | |
| 296 int SiteEngagementService::OriginsWithMaxEngagement() { | |
| 297 std::map<GURL, double> score_map = GetScoreMap(); | |
| 298 int total_origins = 0; | |
| 299 | |
| 300 for (const auto& value : score_map) | |
| 301 if (value.second == SiteEngagementScore::kMaxPoints) | |
| 302 ++total_origins; | |
| 303 | |
| 304 return total_origins; | |
| 305 } | |
| 306 | |
| 307 void SiteEngagementService::RecordStartupUmaStats() { | |
| 308 std::map<GURL, double> score_map = GetScoreMap(); | |
| 309 | |
| 310 double total_score = 0; | |
| 311 for (const auto& value : score_map) | |
| 312 total_score += value.second; | |
| 313 | |
| 314 SiteEngagementMetrics::RecordTotalOriginsEngaged(score_map.size()); | |
| 315 SiteEngagementMetrics::RecordTotalSiteEngagement(total_score); | |
| 316 SiteEngagementMetrics::RecordEngagementScore(score_map); | |
| 317 } | |
| OLD | NEW |