| 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) { |
| 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 } |
| 219 | 209 |
| 220 std::map<GURL, int> SiteEngagementService::GetScoreMap() { | 210 std::map<GURL, double> SiteEngagementService::GetScoreMap() { |
| 221 std::map<GURL, int> score_map; | 211 std::map<GURL, double> score_map; |
| 222 HostContentSettingsMap* settings_map = | 212 HostContentSettingsMap* settings_map = |
| 223 HostContentSettingsMapFactory::GetForProfile(profile_); | 213 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 224 ContentSettingsForOneType engagement_settings; | 214 ContentSettingsForOneType engagement_settings; |
| 225 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 215 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 226 std::string(), &engagement_settings); | 216 std::string(), &engagement_settings); |
| 227 for (const auto& site : engagement_settings) { | 217 for (const auto& site : engagement_settings) { |
| 228 GURL origin(site.primary_pattern.ToString()); | 218 GURL origin(site.primary_pattern.ToString()); |
| 229 if (!origin.is_valid()) | 219 if (!origin.is_valid()) |
| 230 continue; | 220 continue; |
| 231 | 221 |
| 232 scoped_ptr<base::DictionaryValue> score_dict = | 222 scoped_ptr<base::DictionaryValue> score_dict = |
| 233 GetScoreDictForOrigin(settings_map, origin); | 223 GetScoreDictForOrigin(settings_map, origin); |
| 234 SiteEngagementScore score(&clock_, *score_dict); | 224 SiteEngagementScore score(&clock_, *score_dict); |
| 235 score_map[origin] = score.Score(); | 225 score_map[origin] = score.Score(); |
| 236 } | 226 } |
| 237 return score_map; | 227 return score_map; |
| 238 } | 228 } |
| 229 |
| 230 void SiteEngagementService::AddPoints(const GURL& url, double points) { |
| 231 HostContentSettingsMap* settings_map = |
| 232 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 233 scoped_ptr<base::DictionaryValue> score_dict = |
| 234 GetScoreDictForOrigin(settings_map, url); |
| 235 SiteEngagementScore score(&clock_, *score_dict); |
| 236 |
| 237 score.AddPoints(points); |
| 238 if (score.UpdateScoreDict(score_dict.get())) { |
| 239 ContentSettingsPattern pattern( |
| 240 ContentSettingsPattern::FromURLNoWildcard(url)); |
| 241 if (!pattern.IsValid()) |
| 242 return; |
| 243 |
| 244 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
| 245 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 246 std::string(), score_dict.release()); |
| 247 } |
| 248 } |
| OLD | NEW |