| 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 = 0.5; | 61 const double SiteEngagementScore::kNavigationPoints = 1; |
| 62 const double SiteEngagementScore::kUserInputPoints = 0.05; | |
| 63 const int SiteEngagementScore::kDecayPeriodInDays = 7; | 62 const int SiteEngagementScore::kDecayPeriodInDays = 7; |
| 64 const double SiteEngagementScore::kDecayPoints = 5; | 63 const double SiteEngagementScore::kDecayPoints = 5; |
| 65 | 64 |
| 66 SiteEngagementScore::SiteEngagementScore( | 65 SiteEngagementScore::SiteEngagementScore( |
| 67 base::Clock* clock, | 66 base::Clock* clock, |
| 68 const base::DictionaryValue& score_dict) | 67 const base::DictionaryValue& score_dict) |
| 69 : SiteEngagementScore(clock) { | 68 : SiteEngagementScore(clock) { |
| 70 score_dict.GetDouble(kRawScoreKey, &raw_score_); | 69 score_dict.GetDouble(kRawScoreKey, &raw_score_); |
| 71 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); | 70 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); |
| 72 double internal_time; | 71 double internal_time; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 switches::kEnableSiteEngagementService); | 160 switches::kEnableSiteEngagementService); |
| 162 } | 161 } |
| 163 | 162 |
| 164 SiteEngagementService::SiteEngagementService(Profile* profile) | 163 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 165 : profile_(profile) { | 164 : profile_(profile) { |
| 166 } | 165 } |
| 167 | 166 |
| 168 SiteEngagementService::~SiteEngagementService() { | 167 SiteEngagementService::~SiteEngagementService() { |
| 169 } | 168 } |
| 170 | 169 |
| 171 void SiteEngagementService::HandleNavigation(const GURL& url, | 170 void SiteEngagementService::HandleNavigation(const GURL& url) { |
| 172 ui::PageTransition transition) { | 171 HostContentSettingsMap* settings_map = |
| 173 AddPoints(url, SiteEngagementScore::kNavigationPoints); | 172 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 173 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 } |
| 174 } | 188 } |
| 175 | 189 |
| 176 void SiteEngagementService::HandleUserInput(const GURL& url) { | 190 int SiteEngagementService::GetScore(const GURL& url) { |
| 177 AddPoints(url, SiteEngagementScore::kUserInputPoints); | |
| 178 } | |
| 179 | |
| 180 double SiteEngagementService::GetScore(const GURL& url) { | |
| 181 HostContentSettingsMap* settings_map = | 191 HostContentSettingsMap* settings_map = |
| 182 HostContentSettingsMapFactory::GetForProfile(profile_); | 192 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 183 scoped_ptr<base::DictionaryValue> score_dict = | 193 scoped_ptr<base::DictionaryValue> score_dict = |
| 184 GetScoreDictForOrigin(settings_map, url); | 194 GetScoreDictForOrigin(settings_map, url); |
| 185 SiteEngagementScore score(&clock_, *score_dict); | 195 SiteEngagementScore score(&clock_, *score_dict); |
| 186 | 196 |
| 187 return score.Score(); | 197 return score.Score(); |
| 188 } | 198 } |
| 189 | 199 |
| 190 double SiteEngagementService::GetTotalEngagementPoints() { | 200 int SiteEngagementService::GetTotalEngagementPoints() { |
| 191 HostContentSettingsMap* settings_map = | 201 HostContentSettingsMap* settings_map = |
| 192 HostContentSettingsMapFactory::GetForProfile(profile_); | 202 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 193 ContentSettingsForOneType engagement_settings; | 203 ContentSettingsForOneType engagement_settings; |
| 194 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 204 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 195 std::string(), &engagement_settings); | 205 std::string(), &engagement_settings); |
| 196 double total_score = 0; | 206 int total_score = 0; |
| 197 for (const auto& site : engagement_settings) { | 207 for (const auto& site : engagement_settings) { |
| 198 GURL origin(site.primary_pattern.ToString()); | 208 GURL origin(site.primary_pattern.ToString()); |
| 199 if (!origin.is_valid()) | 209 if (!origin.is_valid()) |
| 200 continue; | 210 continue; |
| 201 | 211 |
| 202 scoped_ptr<base::DictionaryValue> score_dict = | 212 scoped_ptr<base::DictionaryValue> score_dict = |
| 203 GetScoreDictForOrigin(settings_map, origin); | 213 GetScoreDictForOrigin(settings_map, origin); |
| 204 SiteEngagementScore score(&clock_, *score_dict); | 214 SiteEngagementScore score(&clock_, *score_dict); |
| 205 total_score += score.Score(); | 215 total_score += score.Score(); |
| 206 } | 216 } |
| 207 return total_score; | 217 return total_score; |
| 208 } | 218 } |
| 209 | 219 |
| 210 std::map<GURL, double> SiteEngagementService::GetScoreMap() { | 220 std::map<GURL, int> SiteEngagementService::GetScoreMap() { |
| 211 std::map<GURL, double> score_map; | 221 std::map<GURL, int> score_map; |
| 212 HostContentSettingsMap* settings_map = | 222 HostContentSettingsMap* settings_map = |
| 213 HostContentSettingsMapFactory::GetForProfile(profile_); | 223 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 214 ContentSettingsForOneType engagement_settings; | 224 ContentSettingsForOneType engagement_settings; |
| 215 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 225 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 216 std::string(), &engagement_settings); | 226 std::string(), &engagement_settings); |
| 217 for (const auto& site : engagement_settings) { | 227 for (const auto& site : engagement_settings) { |
| 218 GURL origin(site.primary_pattern.ToString()); | 228 GURL origin(site.primary_pattern.ToString()); |
| 219 if (!origin.is_valid()) | 229 if (!origin.is_valid()) |
| 220 continue; | 230 continue; |
| 221 | 231 |
| 222 scoped_ptr<base::DictionaryValue> score_dict = | 232 scoped_ptr<base::DictionaryValue> score_dict = |
| 223 GetScoreDictForOrigin(settings_map, origin); | 233 GetScoreDictForOrigin(settings_map, origin); |
| 224 SiteEngagementScore score(&clock_, *score_dict); | 234 SiteEngagementScore score(&clock_, *score_dict); |
| 225 score_map[origin] = score.Score(); | 235 score_map[origin] = score.Score(); |
| 226 } | 236 } |
| 227 return score_map; | 237 return score_map; |
| 228 } | 238 } |
| 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 |