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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 } | 51 } |
| 52 | 52 |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 55 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
| 56 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 56 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
| 57 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 57 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
| 58 | 58 |
| 59 const double SiteEngagementScore::kMaxPoints = 100; | 59 const double SiteEngagementScore::kMaxPoints = 100; |
| 60 const double SiteEngagementScore::kMaxPointsPerDay = 5; | 60 const double SiteEngagementScore::kMaxPointsPerDay = 5; |
| 61 const double SiteEngagementScore::kNavigationPoints = 1; | 61 const double SiteEngagementScore::kNavigationPoints = 0.5; |
| 62 const double SiteEngagementScore::kUserInputPoints = 0.05; | |
| 62 const int SiteEngagementScore::kDecayPeriodInDays = 7; | 63 const int SiteEngagementScore::kDecayPeriodInDays = 7; |
| 63 const double SiteEngagementScore::kDecayPoints = 5; | 64 const double SiteEngagementScore::kDecayPoints = 5; |
| 64 | 65 |
| 65 SiteEngagementScore::SiteEngagementScore( | 66 SiteEngagementScore::SiteEngagementScore( |
| 66 base::Clock* clock, | 67 base::Clock* clock, |
| 67 const base::DictionaryValue& score_dict) | 68 const base::DictionaryValue& score_dict) |
| 68 : SiteEngagementScore(clock) { | 69 : SiteEngagementScore(clock) { |
| 69 score_dict.GetDouble(kRawScoreKey, &raw_score_); | 70 score_dict.GetDouble(kRawScoreKey, &raw_score_); |
| 70 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); | 71 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); |
| 71 double internal_time; | 72 double internal_time; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 switches::kEnableSiteEngagementService); | 161 switches::kEnableSiteEngagementService); |
| 161 } | 162 } |
| 162 | 163 |
| 163 SiteEngagementService::SiteEngagementService(Profile* profile) | 164 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 164 : profile_(profile) { | 165 : profile_(profile) { |
| 165 } | 166 } |
| 166 | 167 |
| 167 SiteEngagementService::~SiteEngagementService() { | 168 SiteEngagementService::~SiteEngagementService() { |
| 168 } | 169 } |
| 169 | 170 |
| 170 void SiteEngagementService::HandleNavigation(const GURL& url) { | 171 void SiteEngagementService::HandleNavigation(const GURL& url, |
| 172 ui::PageTransition transition) { | |
|
calamity
2015/09/22 02:57:21
Why is |transition| here?
dominickn
2015/09/23 00:06:44
It's for when/if we want to count different naviga
calamity
2015/09/24 03:50:53
Acknowledged.
| |
| 173 AddPoints(url, SiteEngagementScore::kNavigationPoints); | |
| 174 } | |
| 175 | |
| 176 void SiteEngagementService::HandleUserInput(const GURL& url) { | |
| 177 AddPoints(url, SiteEngagementScore::kUserInputPoints); | |
| 178 } | |
| 179 | |
| 180 double SiteEngagementService::GetScore(const GURL& url) { | |
| 171 HostContentSettingsMap* settings_map = | 181 HostContentSettingsMap* settings_map = |
| 172 HostContentSettingsMapFactory::GetForProfile(profile_); | 182 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 173 scoped_ptr<base::DictionaryValue> score_dict = | 183 scoped_ptr<base::DictionaryValue> score_dict = |
| 174 GetScoreDictForOrigin(settings_map, url); | |
| 175 SiteEngagementScore score(&clock_, *score_dict); | |
| 176 | |
| 177 score.AddPoints(SiteEngagementScore::kNavigationPoints); | |
| 178 if (score.UpdateScoreDict(score_dict.get())) { | |
| 179 ContentSettingsPattern pattern( | |
| 180 ContentSettingsPattern::FromURLNoWildcard(url)); | |
| 181 if (!pattern.IsValid()) | |
| 182 return; | |
| 183 | |
| 184 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | |
| 185 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
| 186 std::string(), score_dict.release()); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 int SiteEngagementService::GetScore(const GURL& url) { | |
| 191 HostContentSettingsMap* settings_map = | |
| 192 HostContentSettingsMapFactory::GetForProfile(profile_); | |
| 193 scoped_ptr<base::DictionaryValue> score_dict = | |
| 194 GetScoreDictForOrigin(settings_map, url); | 184 GetScoreDictForOrigin(settings_map, url); |
| 195 SiteEngagementScore score(&clock_, *score_dict); | 185 SiteEngagementScore score(&clock_, *score_dict); |
| 196 | 186 |
| 197 return score.Score(); | 187 return score.Score(); |
| 198 } | 188 } |
| 199 | 189 |
| 200 int SiteEngagementService::GetTotalEngagementPoints() { | 190 double SiteEngagementService::GetTotalEngagementPoints() { |
| 201 HostContentSettingsMap* settings_map = | 191 HostContentSettingsMap* settings_map = |
| 202 HostContentSettingsMapFactory::GetForProfile(profile_); | 192 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 203 ContentSettingsForOneType engagement_settings; | 193 ContentSettingsForOneType engagement_settings; |
| 204 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 194 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 205 std::string(), &engagement_settings); | 195 std::string(), &engagement_settings); |
| 206 int total_score = 0; | 196 double total_score = 0; |
| 207 for (const auto& site : engagement_settings) { | 197 for (const auto& site : engagement_settings) { |
| 208 GURL origin(site.primary_pattern.ToString()); | 198 GURL origin(site.primary_pattern.ToString()); |
| 209 if (!origin.is_valid()) | 199 if (!origin.is_valid()) |
| 210 continue; | 200 continue; |
| 211 | 201 |
| 212 scoped_ptr<base::DictionaryValue> score_dict = | 202 scoped_ptr<base::DictionaryValue> score_dict = |
| 213 GetScoreDictForOrigin(settings_map, origin); | 203 GetScoreDictForOrigin(settings_map, origin); |
| 214 SiteEngagementScore score(&clock_, *score_dict); | 204 SiteEngagementScore score(&clock_, *score_dict); |
| 215 total_score += score.Score(); | 205 total_score += score.Score(); |
| 216 } | 206 } |
| 217 return total_score; | 207 return total_score; |
| 218 } | 208 } |
| 209 | |
| 210 void SiteEngagementService::AddPoints(const GURL& url, double points) { | |
| 211 HostContentSettingsMap* settings_map = | |
| 212 HostContentSettingsMapFactory::GetForProfile(profile_); | |
| 213 scoped_ptr<base::DictionaryValue> score_dict = | |
| 214 GetScoreDictForOrigin(settings_map, url); | |
| 215 SiteEngagementScore score(&clock_, *score_dict); | |
| 216 | |
| 217 score.AddPoints(points); | |
| 218 if (score.UpdateScoreDict(score_dict.get())) { | |
| 219 ContentSettingsPattern pattern( | |
| 220 ContentSettingsPattern::FromURLNoWildcard(url)); | |
| 221 if (!pattern.IsValid()) | |
| 222 return; | |
| 223 | |
| 224 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | |
| 225 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
| 226 std::string(), score_dict.release()); | |
| 227 } | |
| 228 } | |
| OLD | NEW |