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 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 double to_add = std::min(kMaxPoints - raw_score_, | 263 double to_add = std::min(kMaxPoints - raw_score_, |
264 GetMaxPointsPerDay() - points_added_today_); | 264 GetMaxPointsPerDay() - points_added_today_); |
265 to_add = std::min(to_add, points); | 265 to_add = std::min(to_add, points); |
266 | 266 |
267 points_added_today_ += to_add; | 267 points_added_today_ += to_add; |
268 raw_score_ += to_add; | 268 raw_score_ += to_add; |
269 | 269 |
270 last_engagement_time_ = now; | 270 last_engagement_time_ = now; |
271 } | 271 } |
272 | 272 |
273 double SiteEngagementScore::GetScore() const { | 273 mojom::SiteEngagementDetails SiteEngagementScore::GetDetails() const { |
274 return std::min(DecayedScore() + BonusScore(), kMaxPoints); | 274 mojom::SiteEngagementDetails engagement; |
275 engagement.origin = origin_; | |
276 engagement.base_score = DecayedScore(); | |
277 engagement.installed_bonus = BonusIfShortcutLaunched(); | |
278 engagement.notifications_bonus = BonusIfHasNotifications(); | |
279 engagement.total_score = GetTotal(); | |
280 return engagement; | |
281 } | |
282 | |
283 double SiteEngagementScore::GetTotal() const { | |
Wez
2017/04/08 01:52:53
Renamed this to GetTotal, for clarity, and rearran
dominickn
2017/04/10 00:58:11
I'm not sure that GetTotal is more clear than GetS
Wez
2017/04/10 03:45:28
The class is SiteEngagementScore, so SiteEngagemen
dominickn
2017/04/10 04:41:26
Not fussed, it can stay.
| |
284 return std::min( | |
285 DecayedScore() + BonusIfShortcutLaunched() + BonusIfHasNotifications(), | |
286 kMaxPoints); | |
275 } | 287 } |
276 | 288 |
277 void SiteEngagementScore::Commit() { | 289 void SiteEngagementScore::Commit() { |
278 DCHECK(settings_map_); | 290 DCHECK(settings_map_); |
279 if (!UpdateScoreDict(score_dict_.get())) | 291 if (!UpdateScoreDict(score_dict_.get())) |
280 return; | 292 return; |
281 | 293 |
282 settings_map_->SetWebsiteSettingDefaultScope( | 294 settings_map_->SetWebsiteSettingDefaultScope( |
283 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), | 295 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
284 std::move(score_dict_)); | 296 std::move(score_dict_)); |
285 } | 297 } |
286 | 298 |
287 blink::mojom::EngagementLevel SiteEngagementScore::GetEngagementLevel() const { | 299 blink::mojom::EngagementLevel SiteEngagementScore::GetEngagementLevel() const { |
288 DCHECK_LT(GetMediumEngagementBoundary(), GetHighEngagementBoundary()); | 300 DCHECK_LT(GetMediumEngagementBoundary(), GetHighEngagementBoundary()); |
289 | 301 |
290 double score = GetScore(); | 302 double score = GetTotal(); |
291 if (score == 0) | 303 if (score == 0) |
292 return blink::mojom::EngagementLevel::NONE; | 304 return blink::mojom::EngagementLevel::NONE; |
293 | 305 |
294 if (score < 1) | 306 if (score < 1) |
295 return blink::mojom::EngagementLevel::MINIMAL; | 307 return blink::mojom::EngagementLevel::MINIMAL; |
296 | 308 |
297 if (score < GetMediumEngagementBoundary()) | 309 if (score < GetMediumEngagementBoundary()) |
298 return blink::mojom::EngagementLevel::LOW; | 310 return blink::mojom::EngagementLevel::LOW; |
299 | 311 |
300 if (score < GetHighEngagementBoundary()) | 312 if (score < GetHighEngagementBoundary()) |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 int hours_since_engagement = | 405 int hours_since_engagement = |
394 (clock_->Now() - last_engagement_time_).InHours(); | 406 (clock_->Now() - last_engagement_time_).InHours(); |
395 if (hours_since_engagement < 0) | 407 if (hours_since_engagement < 0) |
396 return raw_score_; | 408 return raw_score_; |
397 | 409 |
398 int periods = hours_since_engagement / GetDecayPeriodInHours(); | 410 int periods = hours_since_engagement / GetDecayPeriodInHours(); |
399 return std::max(0.0, raw_score_ * pow(GetDecayProportion(), periods) - | 411 return std::max(0.0, raw_score_ * pow(GetDecayProportion(), periods) - |
400 periods * GetDecayPoints()); | 412 periods * GetDecayPoints()); |
401 } | 413 } |
402 | 414 |
403 double SiteEngagementScore::BonusScore() const { | 415 double SiteEngagementScore::BonusIfShortcutLaunched() const { |
404 double bonus = 0; | |
405 int days_since_shortcut_launch = | 416 int days_since_shortcut_launch = |
406 (clock_->Now() - last_shortcut_launch_time_).InDays(); | 417 (clock_->Now() - last_shortcut_launch_time_).InDays(); |
407 if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch) | 418 if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch) |
Wez
2017/04/10 03:45:28
nit: While we'll never hit this in practice, this
| |
408 bonus += GetWebAppInstalledPoints(); | 419 return GetWebAppInstalledPoints(); |
420 return 0; | |
421 } | |
409 | 422 |
423 double SiteEngagementScore::BonusIfHasNotifications() const { | |
dominickn
2017/04/10 00:58:11
Nit: I'd call this BonusIfCanShowNotifications()
Wez
2017/04/10 03:45:28
Done.
| |
410 // TODO(dominickn, raymes): call PermissionManager::GetPermissionStatus when | 424 // TODO(dominickn, raymes): call PermissionManager::GetPermissionStatus when |
411 // the PermissionManager is thread-safe. | 425 // the PermissionManager is thread-safe. |
412 if (settings_map_ && settings_map_->GetContentSetting( | 426 if (settings_map_ && settings_map_->GetContentSetting( |
413 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 427 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
414 std::string()) == CONTENT_SETTING_ALLOW) { | 428 std::string()) == CONTENT_SETTING_ALLOW) { |
415 bonus += GetNotificationPermissionPoints(); | 429 return GetNotificationPermissionPoints(); |
416 } | 430 } |
417 | 431 |
418 return bonus; | 432 return 0; |
419 } | 433 } |
OLD | NEW |