Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: chrome/browser/engagement/site_engagement_score.cc

Issue 2788413003: Add SiteEngagementService::GetAllDetails(), to return detailed scores. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 double SiteEngagementScore::GetScore() const {
274 return std::min(DecayedScore() + BonusScore(), kMaxPoints); 274 return GetDetails().score;
275 }
276
277 mojom::SiteEngagementDetails SiteEngagementScore::GetDetails() const {
278 mojom::SiteEngagementDetails engagement;
279 engagement.origin = origin_;
280 engagement.base_score = DecayedScore();
281 engagement.installed_bonus = BonusIfShortcutLaunched();
282 engagement.notifications_bonus = BonusIfHasNotifications();
283 engagement.score =
284 std::min(engagement.base_score + engagement.installed_bonus +
285 engagement.notifications_bonus,
286 kMaxPoints);
287 return engagement;
275 } 288 }
276 289
277 void SiteEngagementScore::Commit() { 290 void SiteEngagementScore::Commit() {
278 DCHECK(settings_map_); 291 DCHECK(settings_map_);
279 if (!UpdateScoreDict(score_dict_.get())) 292 if (!UpdateScoreDict(score_dict_.get()))
280 return; 293 return;
281 294
282 settings_map_->SetWebsiteSettingDefaultScope( 295 settings_map_->SetWebsiteSettingDefaultScope(
283 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), 296 origin_, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
284 std::move(score_dict_)); 297 std::move(score_dict_));
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 int hours_since_engagement = 406 int hours_since_engagement =
394 (clock_->Now() - last_engagement_time_).InHours(); 407 (clock_->Now() - last_engagement_time_).InHours();
395 if (hours_since_engagement < 0) 408 if (hours_since_engagement < 0)
396 return raw_score_; 409 return raw_score_;
397 410
398 int periods = hours_since_engagement / GetDecayPeriodInHours(); 411 int periods = hours_since_engagement / GetDecayPeriodInHours();
399 return std::max(0.0, raw_score_ * pow(GetDecayProportion(), periods) - 412 return std::max(0.0, raw_score_ * pow(GetDecayProportion(), periods) -
400 periods * GetDecayPoints()); 413 periods * GetDecayPoints());
401 } 414 }
402 415
403 double SiteEngagementScore::BonusScore() const { 416 double SiteEngagementScore::BonusIfShortcutLaunched() const {
404 double bonus = 0;
405 int days_since_shortcut_launch = 417 int days_since_shortcut_launch =
406 (clock_->Now() - last_shortcut_launch_time_).InDays(); 418 (clock_->Now() - last_shortcut_launch_time_).InDays();
407 if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch) 419 if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch)
408 bonus += GetWebAppInstalledPoints(); 420 return GetWebAppInstalledPoints();
421 return 0;
422 }
409 423
424 double SiteEngagementScore::BonusIfHasNotifications() const {
410 // TODO(dominickn, raymes): call PermissionManager::GetPermissionStatus when 425 // TODO(dominickn, raymes): call PermissionManager::GetPermissionStatus when
411 // the PermissionManager is thread-safe. 426 // the PermissionManager is thread-safe.
412 if (settings_map_ && settings_map_->GetContentSetting( 427 if (settings_map_ && settings_map_->GetContentSetting(
413 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 428 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
414 std::string()) == CONTENT_SETTING_ALLOW) { 429 std::string()) == CONTENT_SETTING_ALLOW) {
415 bonus += GetNotificationPermissionPoints(); 430 return GetNotificationPermissionPoints();
416 } 431 }
417 432
418 return bonus; 433 return 0;
419 } 434 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698