OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_score.h" | 5 #include "chrome/browser/engagement/site_engagement_score.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
15 #include "chrome/browser/engagement/site_engagement_metrics.h" | 16 #include "chrome/browser/engagement/site_engagement_metrics.h" |
16 #include "chrome/browser/profiles/profile.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/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Delta within which to consider scores equal. | 22 // Delta within which to consider scores equal. |
23 const double kScoreDelta = 0.001; | 23 const double kScoreDelta = 0.001; |
24 | 24 |
25 // Delta within which to consider internal time values equal. Internal time | 25 // Delta within which to consider internal time values equal. Internal time |
26 // values are in microseconds, so this delta comes out at one second. | 26 // values are in microseconds, so this delta comes out at one second. |
27 const double kTimeDelta = 1000000; | 27 const double kTimeDelta = 1000000; |
28 | 28 |
29 // Number of days after the last launch of an origin from an installed shortcut | 29 // Number of days after the last launch of an origin from an installed shortcut |
30 // for which WEB_APP_INSTALLED_POINTS will be added to the engagement score. | 30 // for which WEB_APP_INSTALLED_POINTS will be added to the engagement score. |
31 const int kMaxDaysSinceShortcutLaunch = 10; | 31 const int kMaxDaysSinceShortcutLaunch = 10; |
32 | 32 |
33 bool DoublesConsideredDifferent(double value1, double value2, double delta) { | 33 bool DoublesConsideredDifferent(double value1, double value2, double delta) { |
34 double abs_difference = fabs(value1 - value2); | 34 double abs_difference = fabs(value1 - value2); |
35 return abs_difference > delta; | 35 return abs_difference > delta; |
36 } | 36 } |
37 | 37 |
38 std::unique_ptr<base::DictionaryValue> GetScoreDictForOrigin( | 38 std::unique_ptr<base::DictionaryValue> GetScoreDictForSettings( |
39 Profile* profile, | 39 const HostContentSettingsMap* settings, |
40 const GURL& origin_url) { | 40 const GURL& origin_url) { |
41 HostContentSettingsMap* settings = | |
42 HostContentSettingsMapFactory::GetForProfile(profile); | |
43 HostContentSettingsMap* fallback_settings = | |
44 profile->IsOffTheRecord() ? HostContentSettingsMapFactory::GetForProfile( | |
45 profile->GetOriginalProfile()) | |
46 : nullptr; | |
47 | |
48 if (!settings) | 41 if (!settings) |
49 return std::unique_ptr<base::DictionaryValue>(); | |
50 | |
51 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting( | |
52 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
53 std::string(), NULL); | |
54 if (!value.get() && fallback_settings) { | |
55 value = fallback_settings->GetWebsiteSetting( | |
56 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | |
57 std::string(), NULL); | |
58 } | |
59 | |
60 if (!value.get()) | |
61 return base::MakeUnique<base::DictionaryValue>(); | 42 return base::MakeUnique<base::DictionaryValue>(); |
62 | 43 |
63 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | 44 std::unique_ptr<base::DictionaryValue> value = |
64 return base::MakeUnique<base::DictionaryValue>(); | 45 base::DictionaryValue::From(settings->GetWebsiteSetting( |
| 46 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
| 47 std::string(), NULL)); |
65 | 48 |
66 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); | 49 if (value.get()) |
| 50 return value; |
| 51 |
| 52 return base::MakeUnique<base::DictionaryValue>(); |
67 } | 53 } |
68 | 54 |
69 } // namespace | 55 } // namespace |
70 | 56 |
71 const double SiteEngagementScore::kMaxPoints = 100; | 57 const double SiteEngagementScore::kMaxPoints = 100; |
72 | 58 |
73 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 59 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
74 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 60 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
75 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 61 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
76 const char* SiteEngagementScore::kLastShortcutLaunchTimeKey = | 62 const char* SiteEngagementScore::kLastShortcutLaunchTimeKey = |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 return; | 171 return; |
186 } | 172 } |
187 } | 173 } |
188 | 174 |
189 // Once we're sure everything is valid, assign the variation to the param | 175 // Once we're sure everything is valid, assign the variation to the param |
190 // values array. | 176 // values array. |
191 for (int i = 0; i < MAX_VARIATION; ++i) | 177 for (int i = 0; i < MAX_VARIATION; ++i) |
192 SiteEngagementScore::GetParamValues()[i].second = param_vals[i]; | 178 SiteEngagementScore::GetParamValues()[i].second = param_vals[i]; |
193 } | 179 } |
194 | 180 |
195 SiteEngagementScore::SiteEngagementScore(base::Clock* clock, | 181 SiteEngagementScore::SiteEngagementScore( |
196 const GURL& origin, | 182 base::Clock* clock, |
197 Profile* profile) | 183 const GURL& origin, |
198 : SiteEngagementScore(clock, GetScoreDictForOrigin(profile, origin)) { | 184 HostContentSettingsMap* settings) |
199 origin_ = origin; | 185 : SiteEngagementScore( |
200 profile_ = profile; | 186 clock, |
| 187 origin, |
| 188 GetScoreDictForSettings(settings, origin)) { |
| 189 settings_map_ = settings; |
201 } | 190 } |
202 | 191 |
203 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default; | 192 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default; |
204 | 193 |
205 SiteEngagementScore::~SiteEngagementScore() {} | 194 SiteEngagementScore::~SiteEngagementScore() {} |
206 | 195 |
207 SiteEngagementScore& SiteEngagementScore::operator=( | 196 SiteEngagementScore& SiteEngagementScore::operator=( |
208 SiteEngagementScore&& other) = default; | 197 SiteEngagementScore&& other) = default; |
209 | 198 |
210 void SiteEngagementScore::AddPoints(double points) { | 199 void SiteEngagementScore::AddPoints(double points) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 raw_score_ += to_add; | 231 raw_score_ += to_add; |
243 | 232 |
244 last_engagement_time_ = now; | 233 last_engagement_time_ = now; |
245 } | 234 } |
246 | 235 |
247 double SiteEngagementScore::GetScore() const { | 236 double SiteEngagementScore::GetScore() const { |
248 return std::min(DecayedScore() + BonusScore(), kMaxPoints); | 237 return std::min(DecayedScore() + BonusScore(), kMaxPoints); |
249 } | 238 } |
250 | 239 |
251 void SiteEngagementScore::Commit() { | 240 void SiteEngagementScore::Commit() { |
| 241 DCHECK(settings_map_); |
252 if (!UpdateScoreDict(score_dict_.get())) | 242 if (!UpdateScoreDict(score_dict_.get())) |
253 return; | 243 return; |
254 | 244 |
255 HostContentSettingsMapFactory::GetForProfile(profile_) | 245 settings_map_->SetWebsiteSettingDefaultScope( |
256 ->SetWebsiteSettingDefaultScope(origin_, GURL(), | 246 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
257 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 247 std::move(score_dict_)); |
258 std::string(), std::move(score_dict_)); | |
259 } | 248 } |
260 | 249 |
261 bool SiteEngagementScore::MaxPointsPerDayAdded() const { | 250 bool SiteEngagementScore::MaxPointsPerDayAdded() const { |
262 if (!last_engagement_time_.is_null() && | 251 if (!last_engagement_time_.is_null() && |
263 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 252 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
264 return false; | 253 return false; |
265 } | 254 } |
266 | 255 |
267 return points_added_today_ == GetMaxPointsPerDay(); | 256 return points_added_today_ == GetMaxPointsPerDay(); |
268 } | 257 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 score_dict->SetDouble(kLastEngagementTimeKey, | 296 score_dict->SetDouble(kLastEngagementTimeKey, |
308 last_engagement_time_.ToInternalValue()); | 297 last_engagement_time_.ToInternalValue()); |
309 score_dict->SetDouble(kLastShortcutLaunchTimeKey, | 298 score_dict->SetDouble(kLastShortcutLaunchTimeKey, |
310 last_shortcut_launch_time_.ToInternalValue()); | 299 last_shortcut_launch_time_.ToInternalValue()); |
311 | 300 |
312 return true; | 301 return true; |
313 } | 302 } |
314 | 303 |
315 SiteEngagementScore::SiteEngagementScore( | 304 SiteEngagementScore::SiteEngagementScore( |
316 base::Clock* clock, | 305 base::Clock* clock, |
| 306 const GURL& origin, |
317 std::unique_ptr<base::DictionaryValue> score_dict) | 307 std::unique_ptr<base::DictionaryValue> score_dict) |
318 : clock_(clock), | 308 : clock_(clock), |
319 raw_score_(0), | 309 raw_score_(0), |
320 points_added_today_(0), | 310 points_added_today_(0), |
321 last_engagement_time_(), | 311 last_engagement_time_(), |
322 last_shortcut_launch_time_(), | 312 last_shortcut_launch_time_(), |
323 score_dict_(score_dict.release()) { | 313 score_dict_(score_dict.release()), |
| 314 origin_(origin) { |
324 if (!score_dict_) | 315 if (!score_dict_) |
325 return; | 316 return; |
326 | 317 |
327 score_dict_->GetDouble(kRawScoreKey, &raw_score_); | 318 score_dict_->GetDouble(kRawScoreKey, &raw_score_); |
328 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_); | 319 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_); |
329 | 320 |
330 double internal_time; | 321 double internal_time; |
331 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time)) | 322 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time)) |
332 last_engagement_time_ = base::Time::FromInternalValue(internal_time); | 323 last_engagement_time_ = base::Time::FromInternalValue(internal_time); |
333 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time)) | 324 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time)) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50; | 363 GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50; |
373 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1; | 364 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1; |
374 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72; | 365 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72; |
375 | 366 |
376 // This is set to values that avoid interference with tests and are set when | 367 // This is set to values that avoid interference with tests and are set when |
377 // testing these features. | 368 // testing these features. |
378 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0; | 369 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0; |
379 GetParamValues()[DECAY_PROPORTION].second = 1; | 370 GetParamValues()[DECAY_PROPORTION].second = 1; |
380 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0; | 371 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0; |
381 } | 372 } |
OLD | NEW |