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

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

Issue 2788413003: Add SiteEngagementService::GetAllDetails(), to return detailed scores. (Closed)
Patch Set: Fix notifications permission logic & test 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 28 matching lines...) Expand all
39 39
40 std::unique_ptr<base::DictionaryValue> GetScoreDictForSettings( 40 std::unique_ptr<base::DictionaryValue> GetScoreDictForSettings(
41 const HostContentSettingsMap* settings, 41 const HostContentSettingsMap* settings,
42 const GURL& origin_url) { 42 const GURL& origin_url) {
43 if (!settings) 43 if (!settings)
44 return base::MakeUnique<base::DictionaryValue>(); 44 return base::MakeUnique<base::DictionaryValue>();
45 45
46 std::unique_ptr<base::DictionaryValue> value = 46 std::unique_ptr<base::DictionaryValue> value =
47 base::DictionaryValue::From(settings->GetWebsiteSetting( 47 base::DictionaryValue::From(settings->GetWebsiteSetting(
48 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 48 origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
49 std::string(), NULL)); 49 content_settings::ResourceIdentifier(), NULL));
50 50
51 if (value.get()) 51 if (value.get())
52 return value; 52 return value;
53 53
54 return base::MakeUnique<base::DictionaryValue>(); 54 return base::MakeUnique<base::DictionaryValue>();
55 } 55 }
56 56
57 } // namespace 57 } // namespace
58 58
59 const double SiteEngagementScore::kMaxPoints = 100; 59 const double SiteEngagementScore::kMaxPoints = 100;
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 double SiteEngagementScore::GetTotalScore() const {
274 return std::min(DecayedScore() + BonusScore(), kMaxPoints); 274 return std::min(
275 DecayedScore() + BonusIfShortcutLaunched() + BonusIfHasNotifications(),
276 kMaxPoints);
277 }
278
279 mojom::SiteEngagementDetails SiteEngagementScore::GetDetails() const {
280 mojom::SiteEngagementDetails engagement;
281 engagement.origin = origin_;
282 engagement.base_score = DecayedScore();
283 engagement.installed_bonus = BonusIfShortcutLaunched();
284 engagement.notifications_bonus = BonusIfHasNotifications();
285 engagement.total_score = GetTotalScore();
286 return engagement;
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,
284 std::move(score_dict_)); 296 content_settings::ResourceIdentifier(), 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 = GetTotalScore();
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
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)
408 bonus += GetWebAppInstalledPoints(); 419 return GetWebAppInstalledPoints();
420 return 0;
421 }
409 422
423 double SiteEngagementScore::BonusIfHasNotifications() const {
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_ &&
413 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 427 settings_map_->GetContentSetting(
414 std::string()) == CONTENT_SETTING_ALLOW) { 428 origin_, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
415 bonus += GetNotificationPermissionPoints(); 429 content_settings::ResourceIdentifier()) == CONTENT_SETTING_ALLOW) {
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