| 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 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 // Delta within which to consider internal time values equal. Internal time | 26 // Delta within which to consider internal time values equal. Internal time |
| 27 // values are in microseconds, so this delta comes out at one second. | 27 // values are in microseconds, so this delta comes out at one second. |
| 28 const double kTimeDelta = 1000000; | 28 const double kTimeDelta = 1000000; |
| 29 | 29 |
| 30 bool DoublesConsideredDifferent(double value1, double value2, double delta) { | 30 bool DoublesConsideredDifferent(double value1, double value2, double delta) { |
| 31 double abs_difference = fabs(value1 - value2); | 31 double abs_difference = fabs(value1 - value2); |
| 32 return abs_difference > delta; | 32 return abs_difference > delta; |
| 33 } | 33 } |
| 34 | 34 |
| 35 scoped_ptr<base::DictionaryValue> GetOriginDict( | 35 scoped_ptr<base::DictionaryValue> GetScoreDictForOrigin( |
| 36 HostContentSettingsMap* settings, | 36 HostContentSettingsMap* settings, |
| 37 const GURL& origin_url) { | 37 const GURL& origin_url) { |
| 38 if (!settings) | 38 if (!settings) |
| 39 return scoped_ptr<base::DictionaryValue>(); | 39 return scoped_ptr<base::DictionaryValue>(); |
| 40 | 40 |
| 41 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( | 41 scoped_ptr<base::Value> value = settings->GetWebsiteSetting( |
| 42 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 42 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 43 std::string(), NULL); | 43 std::string(), NULL); |
| 44 if (!value.get()) | 44 if (!value.get()) |
| 45 return make_scoped_ptr(new base::DictionaryValue()); | 45 return make_scoped_ptr(new base::DictionaryValue()); |
| 46 | 46 |
| 47 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | 47 if (!value->IsType(base::Value::TYPE_DICTIONARY)) |
| 48 return make_scoped_ptr(new base::DictionaryValue()); | 48 return make_scoped_ptr(new base::DictionaryValue()); |
| 49 | 49 |
| 50 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 50 return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
| 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 = 1; |
| 62 const int SiteEngagementScore::kDecayPeriodInDays = 7; | 62 const int SiteEngagementScore::kDecayPeriodInDays = 7; |
| 63 const double SiteEngagementScore::kDecayPoints = 5; | 63 const double SiteEngagementScore::kDecayPoints = 5; |
| 64 | 64 |
| 65 SiteEngagementScore::SiteEngagementScore(base::Clock* clock, | 65 SiteEngagementScore::SiteEngagementScore( |
| 66 const base::DictionaryValue& settings) | 66 base::Clock* clock, |
| 67 const base::DictionaryValue& score_dict) |
| 67 : SiteEngagementScore(clock) { | 68 : SiteEngagementScore(clock) { |
| 68 settings.GetDouble(kRawScoreKey, &raw_score_); | 69 score_dict.GetDouble(kRawScoreKey, &raw_score_); |
| 69 settings.GetDouble(kPointsAddedTodayKey, &points_added_today_); | 70 score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_); |
| 70 double internal_time; | 71 double internal_time; |
| 71 if (settings.GetDouble(kLastEngagementTimeKey, &internal_time)) | 72 if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time)) |
| 72 last_engagement_time_ = base::Time::FromInternalValue(internal_time); | 73 last_engagement_time_ = base::Time::FromInternalValue(internal_time); |
| 73 } | 74 } |
| 74 | 75 |
| 75 SiteEngagementScore::~SiteEngagementScore() { | 76 SiteEngagementScore::~SiteEngagementScore() { |
| 76 } | 77 } |
| 77 | 78 |
| 78 double SiteEngagementScore::Score() const { | 79 double SiteEngagementScore::Score() const { |
| 79 return DecayedScore(); | 80 return DecayedScore(); |
| 80 } | 81 } |
| 81 | 82 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 double to_add = | 94 double to_add = |
| 94 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); | 95 std::min(kMaxPoints - raw_score_, kMaxPointsPerDay - points_added_today_); |
| 95 to_add = std::min(to_add, points); | 96 to_add = std::min(to_add, points); |
| 96 | 97 |
| 97 points_added_today_ += to_add; | 98 points_added_today_ += to_add; |
| 98 raw_score_ += to_add; | 99 raw_score_ += to_add; |
| 99 | 100 |
| 100 last_engagement_time_ = now; | 101 last_engagement_time_ = now; |
| 101 } | 102 } |
| 102 | 103 |
| 103 bool SiteEngagementScore::UpdateSettings(base::DictionaryValue* settings) { | 104 bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) { |
| 104 double raw_score_orig = 0; | 105 double raw_score_orig = 0; |
| 105 double points_added_today_orig = 0; | 106 double points_added_today_orig = 0; |
| 106 double last_engagement_time_internal_orig = 0; | 107 double last_engagement_time_internal_orig = 0; |
| 107 | 108 |
| 108 settings->GetDouble(kRawScoreKey, &raw_score_orig); | 109 score_dict->GetDouble(kRawScoreKey, &raw_score_orig); |
| 109 settings->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); | 110 score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig); |
| 110 settings->GetDouble(kLastEngagementTimeKey, | 111 score_dict->GetDouble(kLastEngagementTimeKey, |
| 111 &last_engagement_time_internal_orig); | 112 &last_engagement_time_internal_orig); |
| 112 bool changed = | 113 bool changed = |
| 113 DoublesConsideredDifferent(raw_score_orig, raw_score_, kScoreDelta) || | 114 DoublesConsideredDifferent(raw_score_orig, raw_score_, kScoreDelta) || |
| 114 DoublesConsideredDifferent(points_added_today_orig, points_added_today_, | 115 DoublesConsideredDifferent(points_added_today_orig, points_added_today_, |
| 115 kScoreDelta) || | 116 kScoreDelta) || |
| 116 DoublesConsideredDifferent(last_engagement_time_internal_orig, | 117 DoublesConsideredDifferent(last_engagement_time_internal_orig, |
| 117 last_engagement_time_.ToInternalValue(), | 118 last_engagement_time_.ToInternalValue(), |
| 118 kTimeDelta); | 119 kTimeDelta); |
| 119 | 120 |
| 120 if (!changed) | 121 if (!changed) |
| 121 return false; | 122 return false; |
| 122 | 123 |
| 123 settings->SetDouble(kRawScoreKey, raw_score_); | 124 score_dict->SetDouble(kRawScoreKey, raw_score_); |
| 124 settings->SetDouble(kPointsAddedTodayKey, points_added_today_); | 125 score_dict->SetDouble(kPointsAddedTodayKey, points_added_today_); |
| 125 settings->SetDouble(kLastEngagementTimeKey, | 126 score_dict->SetDouble(kLastEngagementTimeKey, |
| 126 last_engagement_time_.ToInternalValue()); | 127 last_engagement_time_.ToInternalValue()); |
| 127 | 128 |
| 128 return true; | 129 return true; |
| 129 } | 130 } |
| 130 | 131 |
| 131 SiteEngagementScore::SiteEngagementScore(base::Clock* clock) | 132 SiteEngagementScore::SiteEngagementScore(base::Clock* clock) |
| 132 : clock_(clock), | 133 : clock_(clock), |
| 133 raw_score_(0), | 134 raw_score_(0), |
| 134 points_added_today_(0), | 135 points_added_today_(0), |
| 135 last_engagement_time_() {} | 136 last_engagement_time_() {} |
| (...skipping 25 matching lines...) Expand all Loading... |
| 161 | 162 |
| 162 SiteEngagementService::SiteEngagementService(Profile* profile) | 163 SiteEngagementService::SiteEngagementService(Profile* profile) |
| 163 : profile_(profile) { | 164 : profile_(profile) { |
| 164 } | 165 } |
| 165 | 166 |
| 166 SiteEngagementService::~SiteEngagementService() { | 167 SiteEngagementService::~SiteEngagementService() { |
| 167 } | 168 } |
| 168 | 169 |
| 169 void SiteEngagementService::HandleNavigation(const GURL& url) { | 170 void SiteEngagementService::HandleNavigation(const GURL& url) { |
| 170 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); | 171 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); |
| 171 scoped_ptr<base::DictionaryValue> settings = GetOriginDict(settings_map, url); | 172 scoped_ptr<base::DictionaryValue> score_dict = |
| 172 SiteEngagementScore score(&clock_, *settings); | 173 GetScoreDictForOrigin(settings_map, url); |
| 174 SiteEngagementScore score(&clock_, *score_dict); |
| 173 | 175 |
| 174 score.AddPoints(SiteEngagementScore::kNavigationPoints); | 176 score.AddPoints(SiteEngagementScore::kNavigationPoints); |
| 175 if (score.UpdateSettings(settings.get())) { | 177 if (score.UpdateScoreDict(score_dict.get())) { |
| 176 ContentSettingsPattern pattern( | 178 ContentSettingsPattern pattern( |
| 177 ContentSettingsPattern::FromURLNoWildcard(url)); | 179 ContentSettingsPattern::FromURLNoWildcard(url)); |
| 178 if (!pattern.IsValid()) | 180 if (!pattern.IsValid()) |
| 179 return; | 181 return; |
| 180 | 182 |
| 181 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), | 183 settings_map->SetWebsiteSetting(pattern, ContentSettingsPattern::Wildcard(), |
| 182 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 184 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 183 std::string(), settings.release()); | 185 std::string(), score_dict.release()); |
| 184 } | 186 } |
| 185 } | 187 } |
| 186 | 188 |
| 187 int SiteEngagementService::GetScore(const GURL& url) { | 189 int SiteEngagementService::GetScore(const GURL& url) { |
| 188 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); | 190 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); |
| 189 scoped_ptr<base::DictionaryValue> settings = GetOriginDict(settings_map, url); | 191 scoped_ptr<base::DictionaryValue> score_dict = |
| 190 SiteEngagementScore score(&clock_, *settings); | 192 GetScoreDictForOrigin(settings_map, url); |
| 193 SiteEngagementScore score(&clock_, *score_dict); |
| 191 | 194 |
| 192 return score.Score(); | 195 return score.Score(); |
| 193 } | 196 } |
| 194 | 197 |
| 198 int SiteEngagementService::GetTotalEngagementPoints() { |
| 199 HostContentSettingsMap* settings_map = profile_->GetHostContentSettingsMap(); |
| 200 ContentSettingsForOneType engagement_settings; |
| 201 settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 202 std::string(), &engagement_settings); |
| 203 int total_score = 0; |
| 204 for (const auto& site : engagement_settings) { |
| 205 GURL origin(site.primary_pattern.ToString()); |
| 206 if (!origin.is_valid()) |
| 207 continue; |
| 208 |
| 209 scoped_ptr<base::DictionaryValue> score_dict = |
| 210 GetScoreDictForOrigin(settings_map, origin); |
| 211 SiteEngagementScore score(&clock_, *score_dict); |
| 212 total_score += score.Score(); |
| 213 } |
| 214 return total_score; |
| 215 } |
| OLD | NEW |