| 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/engagement/site_engagement_helper.h" | 14 #include "chrome/browser/engagement/site_engagement_helper.h" |
| 14 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 15 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/common/content_settings_pattern.h" | 18 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Delta within which to consider scores equal. | 23 // Delta within which to consider scores equal. |
| 24 const double kScoreDelta = 0.001; | 24 const double kScoreDelta = 0.001; |
| 25 | 25 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 161 } |
| 162 | 162 |
| 163 SiteEngagementService::SiteEngagementService(Profile* profile) | 163 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 164 : profile_(profile) { | 164 : profile_(profile) { |
| 165 } | 165 } |
| 166 | 166 |
| 167 SiteEngagementService::~SiteEngagementService() { | 167 SiteEngagementService::~SiteEngagementService() { |
| 168 } | 168 } |
| 169 | 169 |
| 170 void SiteEngagementService::HandleNavigation(const GURL& url) { | 170 void SiteEngagementService::HandleNavigation(const GURL& url) { |
| 171 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); | 171 HostContentSettingsMap* settings_map = |
| 172 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 172 scoped_ptr<base::DictionaryValue> score_dict = | 173 scoped_ptr<base::DictionaryValue> score_dict = |
| 173 GetScoreDictForOrigin(settings_map, url); | 174 GetScoreDictForOrigin(settings_map, url); |
| 174 SiteEngagementScore score(&clock_, *score_dict); | 175 SiteEngagementScore score(&clock_, *score_dict); |
| 175 | 176 |
| 176 score.AddPoints(SiteEngagementScore::kNavigationPoints); | 177 score.AddPoints(SiteEngagementScore::kNavigationPoints); |
| 177 if (score.UpdateScoreDict(score_dict.get())) { | 178 if (score.UpdateScoreDict(score_dict.get())) { |
| 178 ContentSettingsPattern pattern( | 179 ContentSettingsPattern pattern( |
| 179 ContentSettingsPattern::FromURLNoWildcard(url)); | 180 ContentSettingsPattern::FromURLNoWildcard(url)); |
| 180 if (!pattern.IsValid()) | 181 if (!pattern.IsValid()) |
| 181 return; | 182 return; |
| 182 | 183 |
| 183 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | 184 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
| 184 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 185 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 185 std::string(), score_dict.release()); | 186 std::string(), score_dict.release()); |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 | 189 |
| 189 int SiteEngagementService::GetScore(const GURL& url) { | 190 int SiteEngagementService::GetScore(const GURL& url) { |
| 190 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); | 191 HostContentSettingsMap* settings_map = |
| 192 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 191 scoped_ptr<base::DictionaryValue> score_dict = | 193 scoped_ptr<base::DictionaryValue> score_dict = |
| 192 GetScoreDictForOrigin(settings_map, url); | 194 GetScoreDictForOrigin(settings_map, url); |
| 193 SiteEngagementScore score(&clock_, *score_dict); | 195 SiteEngagementScore score(&clock_, *score_dict); |
| 194 | 196 |
| 195 return score.Score(); | 197 return score.Score(); |
| 196 } | 198 } |
| 197 | 199 |
| 198 int SiteEngagementService::GetTotalEngagementPoints() { | 200 int SiteEngagementService::GetTotalEngagementPoints() { |
| 199 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); | 201 HostContentSettingsMap* settings_map = |
| 202 HostContentSettingsMapFactory::GetForProfile(profile_); |
| 200 ContentSettingsForOneType engagement_settings; | 203 ContentSettingsForOneType engagement_settings; |
| 201 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 204 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 202 std::string(), &engagement_settings); | 205 std::string(), &engagement_settings); |
| 203 int total_score = 0; | 206 int total_score = 0; |
| 204 for (const auto& site : engagement_settings) { | 207 for (const auto& site : engagement_settings) { |
| 205 GURL origin(site.primary_pattern.ToString()); | 208 GURL origin(site.primary_pattern.ToString()); |
| 206 if (!origin.is_valid()) | 209 if (!origin.is_valid()) |
| 207 continue; | 210 continue; |
| 208 | 211 |
| 209 scoped_ptr<base::DictionaryValue> score_dict = | 212 scoped_ptr<base::DictionaryValue> score_dict = |
| 210 GetScoreDictForOrigin(settings_map, origin); | 213 GetScoreDictForOrigin(settings_map, origin); |
| 211 SiteEngagementScore score(&clock_, *score_dict); | 214 SiteEngagementScore score(&clock_, *score_dict); |
| 212 total_score += score.Score(); | 215 total_score += score.Score(); |
| 213 } | 216 } |
| 214 return total_score; | 217 return total_score; |
| 215 } | 218 } |
| OLD | NEW |