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>(); | 42 return base::MakeUnique<base::DictionaryValue>(); |
50 | 43 |
51 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting( | 44 std::unique_ptr<base::Value> value = settings->GetWebsiteSetting( |
52 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 45 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, |
53 std::string(), NULL); | 46 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 | 47 |
60 if (!value.get()) | 48 if (!value.get() || !value->IsType(base::Value::TYPE_DICTIONARY)) |
Bernhard Bauer
2016/09/15 15:39:54
You could get rid of this check now if you instead
dominickn
2016/09/16 00:26:01
Done.
Bernhard Bauer
2016/09/16 10:32:38
Sorry, what I meant was:
DictionaryValue::From() i
dominickn
2016/09/19 00:59:56
Done.
| |
61 return base::MakeUnique<base::DictionaryValue>(); | 49 return base::MakeUnique<base::DictionaryValue>(); |
62 | 50 |
63 if (!value->IsType(base::Value::TYPE_DICTIONARY)) | 51 return base::DictionaryValue::From(std::move(value)); |
64 return base::MakeUnique<base::DictionaryValue>(); | |
65 | |
66 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); | |
67 } | 52 } |
68 | 53 |
69 } // namespace | 54 } // namespace |
70 | 55 |
71 const double SiteEngagementScore::kMaxPoints = 100; | 56 const double SiteEngagementScore::kMaxPoints = 100; |
72 | 57 |
73 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; | 58 const char* SiteEngagementScore::kRawScoreKey = "rawScore"; |
74 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; | 59 const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday"; |
75 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; | 60 const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime"; |
76 const char* SiteEngagementScore::kLastShortcutLaunchTimeKey = | 61 const char* SiteEngagementScore::kLastShortcutLaunchTimeKey = |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 return; | 170 return; |
186 } | 171 } |
187 } | 172 } |
188 | 173 |
189 // Once we're sure everything is valid, assign the variation to the param | 174 // Once we're sure everything is valid, assign the variation to the param |
190 // values array. | 175 // values array. |
191 for (int i = 0; i < MAX_VARIATION; ++i) | 176 for (int i = 0; i < MAX_VARIATION; ++i) |
192 SiteEngagementScore::GetParamValues()[i].second = param_vals[i]; | 177 SiteEngagementScore::GetParamValues()[i].second = param_vals[i]; |
193 } | 178 } |
194 | 179 |
195 SiteEngagementScore::SiteEngagementScore(base::Clock* clock, | 180 SiteEngagementScore::SiteEngagementScore( |
196 const GURL& origin, | 181 base::Clock* clock, |
197 Profile* profile) | 182 const GURL& origin, |
198 : SiteEngagementScore(clock, GetScoreDictForOrigin(profile, origin)) { | 183 HostContentSettingsMap* settings) |
199 origin_ = origin; | 184 : SiteEngagementScore( |
200 profile_ = profile; | 185 clock, |
186 origin, | |
187 GetScoreDictForSettings(settings, origin)) { | |
188 settings_map_ = settings; | |
201 } | 189 } |
202 | 190 |
203 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default; | 191 SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default; |
204 | 192 |
205 SiteEngagementScore::~SiteEngagementScore() {} | 193 SiteEngagementScore::~SiteEngagementScore() {} |
206 | 194 |
207 SiteEngagementScore& SiteEngagementScore::operator=( | 195 SiteEngagementScore& SiteEngagementScore::operator=( |
208 SiteEngagementScore&& other) = default; | 196 SiteEngagementScore&& other) = default; |
209 | 197 |
210 void SiteEngagementScore::AddPoints(double points) { | 198 void SiteEngagementScore::AddPoints(double points) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 raw_score_ += to_add; | 230 raw_score_ += to_add; |
243 | 231 |
244 last_engagement_time_ = now; | 232 last_engagement_time_ = now; |
245 } | 233 } |
246 | 234 |
247 double SiteEngagementScore::GetScore() const { | 235 double SiteEngagementScore::GetScore() const { |
248 return std::min(DecayedScore() + BonusScore(), kMaxPoints); | 236 return std::min(DecayedScore() + BonusScore(), kMaxPoints); |
249 } | 237 } |
250 | 238 |
251 void SiteEngagementScore::Commit() { | 239 void SiteEngagementScore::Commit() { |
240 DCHECK(settings_map_); | |
252 if (!UpdateScoreDict(score_dict_.get())) | 241 if (!UpdateScoreDict(score_dict_.get())) |
253 return; | 242 return; |
254 | 243 |
255 HostContentSettingsMapFactory::GetForProfile(profile_) | 244 settings_map_->SetWebsiteSettingDefaultScope( |
256 ->SetWebsiteSettingDefaultScope(origin_, GURL(), | 245 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
257 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 246 std::move(score_dict_)); |
258 std::string(), std::move(score_dict_)); | |
259 } | 247 } |
260 | 248 |
261 bool SiteEngagementScore::MaxPointsPerDayAdded() const { | 249 bool SiteEngagementScore::MaxPointsPerDayAdded() const { |
262 if (!last_engagement_time_.is_null() && | 250 if (!last_engagement_time_.is_null() && |
263 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { | 251 clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) { |
264 return false; | 252 return false; |
265 } | 253 } |
266 | 254 |
267 return points_added_today_ == GetMaxPointsPerDay(); | 255 return points_added_today_ == GetMaxPointsPerDay(); |
268 } | 256 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 score_dict->SetDouble(kLastEngagementTimeKey, | 295 score_dict->SetDouble(kLastEngagementTimeKey, |
308 last_engagement_time_.ToInternalValue()); | 296 last_engagement_time_.ToInternalValue()); |
309 score_dict->SetDouble(kLastShortcutLaunchTimeKey, | 297 score_dict->SetDouble(kLastShortcutLaunchTimeKey, |
310 last_shortcut_launch_time_.ToInternalValue()); | 298 last_shortcut_launch_time_.ToInternalValue()); |
311 | 299 |
312 return true; | 300 return true; |
313 } | 301 } |
314 | 302 |
315 SiteEngagementScore::SiteEngagementScore( | 303 SiteEngagementScore::SiteEngagementScore( |
316 base::Clock* clock, | 304 base::Clock* clock, |
305 const GURL& origin, | |
317 std::unique_ptr<base::DictionaryValue> score_dict) | 306 std::unique_ptr<base::DictionaryValue> score_dict) |
318 : clock_(clock), | 307 : clock_(clock), |
319 raw_score_(0), | 308 raw_score_(0), |
320 points_added_today_(0), | 309 points_added_today_(0), |
321 last_engagement_time_(), | 310 last_engagement_time_(), |
322 last_shortcut_launch_time_(), | 311 last_shortcut_launch_time_(), |
323 score_dict_(score_dict.release()) { | 312 score_dict_(score_dict.release()), |
313 origin_(origin) { | |
324 if (!score_dict_) | 314 if (!score_dict_) |
325 return; | 315 return; |
326 | 316 |
327 score_dict_->GetDouble(kRawScoreKey, &raw_score_); | 317 score_dict_->GetDouble(kRawScoreKey, &raw_score_); |
328 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_); | 318 score_dict_->GetDouble(kPointsAddedTodayKey, &points_added_today_); |
329 | 319 |
330 double internal_time; | 320 double internal_time; |
331 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time)) | 321 if (score_dict_->GetDouble(kLastEngagementTimeKey, &internal_time)) |
332 last_engagement_time_ = base::Time::FromInternalValue(internal_time); | 322 last_engagement_time_ = base::Time::FromInternalValue(internal_time); |
333 if (score_dict_->GetDouble(kLastShortcutLaunchTimeKey, &internal_time)) | 323 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; | 362 GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50; |
373 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1; | 363 GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1; |
374 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72; | 364 GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72; |
375 | 365 |
376 // This is set to values that avoid interference with tests and are set when | 366 // This is set to values that avoid interference with tests and are set when |
377 // testing these features. | 367 // testing these features. |
378 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0; | 368 GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0; |
379 GetParamValues()[DECAY_PROPORTION].second = 1; | 369 GetParamValues()[DECAY_PROPORTION].second = 1; |
380 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0; | 370 GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0; |
381 } | 371 } |
OLD | NEW |