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/memory/scoped_ptr.h"
11 #include "base/time/default_clock.h"
12 #include "base/time/time.h"
13 #include "components/keyed_service/core/keyed_service.h"
16 class DictionaryValue
;
22 class SiteEngagementScore
{
24 // Keys used in the content settings dictionary.
25 static const char* kRawScoreKey
;
26 static const char* kPointsAddedTodayKey
;
27 static const char* kLastEngagementTimeKey
;
29 // The maximum number of points that are allowed.
30 static const double kMaxPoints
;
32 // The maximum number of points that can be accrued in one day.
33 static const double kMaxPointsPerDay
;
35 // The number of points given for a navigation.
36 static const double kNavigationPoints
;
38 // Decaying works by removing a portion of the score periodically. This
39 // constant determines how often that happens.
40 static const int kDecayPeriodInDays
;
42 // How much the score decays after every kDecayPeriodInDays.
43 static const double kDecayPoints
;
45 // The SiteEngagementService does not take ownership of |clock|. It is the
46 // responsibility of the caller to make sure |clock| outlives this
47 // SiteEngagementScore.
48 SiteEngagementScore(base::Clock
* clock
,
49 const base::DictionaryValue
& score_dict
);
50 ~SiteEngagementScore();
53 void AddPoints(double points
);
55 // Updates the content settings dictionary |score_dict| with the current score
56 // fields. Returns true if |score_dict| changed, otherwise return false.
57 bool UpdateScoreDict(base::DictionaryValue
* score_dict
);
60 friend class SiteEngagementScoreTest
;
62 // This version of the constructor is used in unit tests.
63 explicit SiteEngagementScore(base::Clock
* clock
);
65 // Determine the score, accounting for any decay.
66 double DecayedScore() const;
68 // The clock used to vend times. Enables time travelling in tests. Owned by
69 // the SiteEngagementService.
72 // |raw_score_| is the score before any decay is applied.
75 // The points added 'today' are tracked to avoid adding more than
76 // kMaxPointsPerDay on any one day. 'Today' is defined in local time.
77 double points_added_today_
;
79 // The last time the score was updated for engagement. Used in conjunction
80 // with |points_added_today_| to avoid adding more than kMaxPointsPerDay on
82 base::Time last_engagement_time_
;
84 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore
);
87 // Stores and retrieves the engagement score of an origin.
89 // An engagement score is a positive integer that represents how much a user has
90 // engaged with an origin - the higher it is, the more engagement the user has
91 // had with this site recently.
93 // Positive user activity, such as visiting the origin often and adding it to
94 // the homescreen, will increase the site engagement score. Negative activity,
95 // such as rejecting permission prompts or not responding to notifications, will
96 // decrease the site engagement score.
97 class SiteEngagementService
: public KeyedService
{
99 static SiteEngagementService
* Get(Profile
* profile
);
101 // Returns whether or not the SiteEngagementService is enabled.
102 static bool IsEnabled();
104 explicit SiteEngagementService(Profile
* profile
);
105 ~SiteEngagementService() override
;
107 // Update the karma score of the origin matching |url| for user navigation.
108 void HandleNavigation(const GURL
& url
);
110 // Returns a non-negative integer representing the engagement score of the
111 // origin for this URL.
112 int GetScore(const GURL
& url
);
114 // Returns the sum of engagement points awarded to all sites.
115 int GetTotalEngagementPoints();
120 // The clock used to vend times.
121 base::DefaultClock clock_
;
123 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService
);
126 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_