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/time/clock.h" | 11 #include "base/time/clock.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
14 #include "chrome/browser/engagement/site_engagement_helper.h" | 14 #include "chrome/browser/engagement/site_engagement_helper.h" |
15 #include "chrome/browser/engagement/site_engagement_metrics.h" | |
15 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 16 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
18 #include "components/content_settings/core/common/content_settings_pattern.h" | 19 #include "components/content_settings/core/common/content_settings_pattern.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Delta within which to consider scores equal. | 24 // Delta within which to consider scores equal. |
24 const double kScoreDelta = 0.001; | 25 const double kScoreDelta = 0.001; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 double to_add = | 96 double to_add = |
96 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); | 97 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); |
97 to_add = std::min(to_add, points); | 98 to_add = std::min(to_add, points); |
98 | 99 |
99 points_added_today_ += to_add; | 100 points_added_today_ += to_add; |
100 raw_score_ += to_add; | 101 raw_score_ += to_add; |
101 | 102 |
102 last_engagement_time_ = now; | 103 last_engagement_time_ = now; |
103 } | 104 } |
104 | 105 |
106 bool SiteEngagementScore::MaxPointsPerDayAdded() { | |
107 if (!last_engagement_time_.is_null() && | |
108 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | |
109 points_added_today_ = 0; | |
110 } | |
111 | |
112 return points_added_today_ == kMaxPointsPerDay; | |
113 } | |
114 | |
105 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { | 115 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
106 double raw_score_orig = 0; | 116 double raw_score_orig = 0; |
107 double points_added_today_orig = 0; | 117 double points_added_today_orig = 0; |
108 double last_engagement_time_internal_orig = 0; | 118 double last_engagement_time_internal_orig = 0; |
109 | 119 |
110 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); | 120 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); |
111 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); | 121 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); |
112 score_dict->GetDouble(kLastEngagementTimeKey, | 122 score_dict->GetDouble(kLastEngagementTimeKey, |
113 &last_engagement_time_internal_orig); | 123 &last_engagement_time_internal_orig); |
114 bool changed = | 124 bool changed = |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 } | 166 } |
157 | 167 |
158 // static | 168 // static |
159 bool SiteEngagementService::IsEnabled() { | 169 bool SiteEngagementService::IsEnabled() { |
160 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 170 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
161 switches::kEnableSiteEngagementService); | 171 switches::kEnableSiteEngagementService); |
162 } | 172 } |
163 | 173 |
164 SiteEngagementService::SiteEngagementService(Profile* profile) | 174 SiteEngagementService::SiteEngagementService(Profile* profile) |
165 : profile_(profile) { | 175 : profile_(profile) { |
176 RecordStartupUmaStats(); | |
166 } | 177 } |
167 | 178 |
168 SiteEngagementService::~SiteEngagementService() { | 179 SiteEngagementService::~SiteEngagementService() { |
169 } | 180 } |
170 | 181 |
171 void SiteEngagementService::HandleNavigation(const GURL& url, | 182 void SiteEngagementService::HandleNavigation(const GURL& url, |
172 ui::PageTransition transition) { | 183 ui::PageTransition transition) { |
173 AddPoints(url, SiteEngagementScore::kNavigationPoints); | 184 AddPoints(url, SiteEngagementScore::kNavigationPoints); |
174 } | 185 } |
175 | 186 |
176 void SiteEngagementService::HandleUserInput(const GURL& url) { | 187 void SiteEngagementService::HandleUserInput(const GURL& url) { |
177 AddPoints(url, SiteEngagementScore::kUserInputPoints); | 188 AddPoints(url, SiteEngagementScore::kUserInputPoints); |
178 } | 189 } |
179 | 190 |
180 double SiteEngagementService::GetScore(const GURL& url) { | 191 double SiteEngagementService::GetScore(const GURL& url) { |
181 HostContentSettingsMap* settings_map = | 192 HostContentSettingsMap* settings_map = |
182 HostContentSettingsMapFactory::GetForProfile(profile_); | 193 HostContentSettingsMapFactory::GetForProfile(profile_); |
183 scoped_ptr<base::DictionaryValue> score_dict = | 194 scoped_ptr<base::DictionaryValue> score_dict = |
184 GetScoreDictForOrigin(settings_map, url); | 195 GetScoreDictForOrigin(settings_map, url); |
185 SiteEngagementScore score(&clock_, *score_dict); | 196 SiteEngagementScore score(&clock_, *score_dict); |
186 | 197 |
187 return score.Score(); | 198 return score.Score(); |
188 } | 199 } |
189 | 200 |
190 double SiteEngagementService::GetTotalEngagementPoints() { | 201 double SiteEngagementService::GetTotalEngagementPoints() { |
191 HostContentSettingsMap* settings_map = | 202 std::map<GURL, double> score_map = GetScoreMap(); |
192 HostContentSettingsMapFactory::GetForProfile(profile_); | 203 |
193 ContentSettingsForOneType engagement_settings; | |
194 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
195 std::string(), &engagement_settings); | |
196 double total_score = 0; | 204 double total_score = 0; |
197 for (const auto& site : engagement_settings) { | 205 for (const auto& value : score_map) |
198 GURL origin(site.primary_pattern.ToString()); | 206 total_score += value.second; |
199 if (!origin.is_valid()) | |
200 continue; | |
201 | 207 |
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; | 208 return total_score; |
208 } | 209 } |
209 | 210 |
210 std::map<GURL, double> SiteEngagementService::GetScoreMap() { | 211 std::map<GURL, double> SiteEngagementService::GetScoreMap() { |
211 std::map<GURL, double> score_map; | 212 std::map<GURL, double> score_map; |
212 HostContentSettingsMap* settings_map = | 213 HostContentSettingsMap* settings_map = |
213 HostContentSettingsMapFactory::GetForProfile(profile_); | 214 HostContentSettingsMapFactory::GetForProfile(profile_); |
214 ContentSettingsForOneType engagement_settings; | 215 ContentSettingsForOneType engagement_settings; |
215 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 216 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
216 std::string(), &engagement_settings); | 217 std::string(), &engagement_settings); |
(...skipping 21 matching lines...) Expand all Loading... | |
238 if (score.UpdateScoreDict(score_dict.get())) { | 239 if (score.UpdateScoreDict(score_dict.get())) { |
239 ContentSettingsPattern pattern( | 240 ContentSettingsPattern pattern( |
240 ContentSettingsPattern::FromURLNoWildcard(url)); | 241 ContentSettingsPattern::FromURLNoWildcard(url)); |
241 if (!pattern.IsValid()) | 242 if (!pattern.IsValid()) |
242 return; | 243 return; |
243 | 244 |
244 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | 245 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
245 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 246 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
246 std::string(), score_dict.release()); | 247 std::string(), score_dict.release()); |
247 } | 248 } |
249 | |
250 // If the origin has now received the maximum number of points it can today, | |
251 // record a UMA statistic of all of the origins with maxed daily engagement. | |
252 if (score.MaxPointsPerDayAdded()) { | |
253 SiteEngagementMetrics::RecordOriginsWithMaxDailyEngagement( | |
254 OriginsWithMaxDailyEngagement()); | |
255 } | |
256 | |
257 // If the origin has now received the maximum total number of points, | |
258 // record a UMA statistic of all of the origins with maximum total points. | |
259 if (score.Score() == SiteEngagementScore::kMaxPoints) { | |
260 SiteEngagementMetrics::RecordOriginsWithMaxEngagement( | |
261 OriginsWithMaxEngagement()); | |
calamity
2015/09/28 07:33:48
Hmm. Does this mean we will be re-recording these
dominickn
2015/09/29 02:29:36
Done.
| |
262 } | |
248 } | 263 } |
264 | |
265 int SiteEngagementService::OriginsWithMaxDailyEngagement() { | |
266 HostContentSettingsMap* settings_map = | |
267 HostContentSettingsMapFactory::GetForProfile(profile_); | |
268 ContentSettingsForOneType engagement_settings; | |
269 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
270 std::string(), &engagement_settings); | |
271 int total_origins = 0; | |
272 for (const auto& site : engagement_settings) { | |
273 GURL origin(site.primary_pattern.ToString()); | |
274 if (!origin.is_valid()) | |
275 continue; | |
276 | |
277 scoped_ptr<base::DictionaryValue> score_dict = | |
278 GetScoreDictForOrigin(settings_map, origin); | |
279 SiteEngagementScore score(&clock_, *score_dict); | |
280 if (score.MaxPointsPerDayAdded()) | |
281 total_origins += 1; | |
282 } | |
283 | |
284 return total_origins; | |
285 } | |
286 | |
287 int SiteEngagementService::OriginsWithMaxEngagement() { | |
288 std::map<GURL, double> score_map = GetScoreMap(); | |
289 int total_origins = 0; | |
290 | |
291 for (const auto& value : score_map) | |
292 if (value.second == SiteEngagementScore::kMaxPoints) | |
293 ++total_origins; | |
294 | |
295 return total_origins; | |
296 } | |
297 | |
298 void SiteEngagementService::RecordStartupUmaStats() { | |
299 std::map<GURL, double> score_map = GetScoreMap(); | |
300 | |
301 double total_score = 0; | |
302 for (const auto& value : score_map) | |
303 total_score += value.second; | |
304 | |
305 SiteEngagementMetrics::RecordTotalOriginsEngaged(score_map.size()); | |
306 SiteEngagementMetrics::RecordTotalSiteEngagement(total_score); | |
307 SiteEngagementMetrics::RecordEngagementByOrigin(score_map); | |
308 } | |
OLD | NEW |