1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/macros.h"
10 #include "base/time/default_clock.h"
11 #include "base/time/time.h"
12 #include "components/keyed_service/core/keyed_service.h"
15 class DictionaryValue
;
21 class SiteEngagementScore
{
23 // Keys used in the content settings dictionary.
24 static const char* kRawScoreKey
;
25 static const char* kPointsAddedTodayKey
;
26 static const char* kLastEngagementTimeKey
;
28 // The maximum number of points that are allowed.
29 static const double kMaxPoints
;
31 // The maximum number of points that can be accrued in one day.
32 static const double kMaxPointsPerDay
;
34 // The number of points given for a navigation.
35 static const double kNavigationPoints
;
37 // Decaying works by removing a portion of the score periodically. This
38 // constant determines how often that happens.
39 static const int kDecayPeriodInDays
;
41 // How much the score decays after every kDecayPeriodInDays.
42 static const double kDecayPoints
;
44 // The SiteEngagementService does not take ownership of |clock|. It is the
45 // responsibility of the caller to make sure |clock| outlives this
46 // SiteEngagementScore.
47 SiteEngagementScore(base::Clock
* clock
,
48 const base::DictionaryValue
& score_dict
);
49 ~SiteEngagementScore();
52 void AddPoints(double points
);
54 // Updates the content settings dictionary |score_dict| with the current score
55 // fields. Returns true if |score_dict| changed, otherwise return false.
56 bool UpdateScoreDict(base::DictionaryValue
* score_dict
);
59 friend class SiteEngagementScoreTest
;
61 // This version of the constructor is used in unit tests.
62 explicit SiteEngagementScore(base::Clock
* clock
);
64 // Determine the score, accounting for any decay.
65 double DecayedScore() const;
67 // The clock used to vend times. Enables time travelling in tests. Owned by
68 // the SiteEngagementService.
71 // |raw_score_| is the score before any decay is applied.
74 // The points added 'today' are tracked to avoid adding more than
75 // kMaxPointsPerDay on any one day. 'Today' is defined in local time.
76 double points_added_today_
;
78 // The last time the score was updated for engagement. Used in conjunction
79 // with |points_added_today_| to avoid adding more than kMaxPointsPerDay on
81 base::Time last_engagement_time_
;
83 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore
);
86 class SiteEngagementScoreProvider
{
88 // Returns a non-negative integer representing the engagement score of the
89 // origin for this URL.
90 virtual int GetScore(const GURL
& url
) = 0;
92 // Returns the sum of engagement points awarded to all sites.
93 virtual int GetTotalEngagementPoints() = 0;
96 // Stores and retrieves the engagement score of an origin.
98 // An engagement score is a positive integer that represents how much a user has
99 // engaged with an origin - the higher it is, the more engagement the user has
100 // had with this site recently.
102 // Positive user activity, such as visiting the origin often and adding it to
103 // the homescreen, will increase the site engagement score. Negative activity,
104 // such as rejecting permission prompts or not responding to notifications, will
105 // decrease the site engagement score.
106 class SiteEngagementService
: public KeyedService
,
107 public SiteEngagementScoreProvider
{
109 static SiteEngagementService
* Get(Profile
* profile
);
111 // Returns whether or not the SiteEngagementService is enabled.
112 static bool IsEnabled();
114 explicit SiteEngagementService(Profile
* profile
);
115 ~SiteEngagementService() override
;
117 // Update the karma score of the origin matching |url| for user navigation.
118 void HandleNavigation(const GURL
& url
);
120 // Overridden from SiteEngagementScoreProvider:
121 int GetScore(const GURL
& url
) override
;
122 int GetTotalEngagementPoints() override
;
127 // The clock used to vend times.
128 base::DefaultClock clock_
;
130 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService
);
133 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_