Popular sites on the NTP: re-download popular suggestions once per Chrome run
[chromium-blink-merge.git] / chrome / browser / engagement / site_engagement_service.h
blob2ab67a749a1cfe4fa1129eb7cc1aa233cac16b54
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 <map>
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/time/default_clock.h"
13 #include "base/time/time.h"
14 #include "components/keyed_service/core/keyed_service.h"
16 namespace base {
17 class DictionaryValue;
20 class GURL;
21 class Profile;
23 class SiteEngagementScore {
24 public:
25 // Keys used in the content settings dictionary.
26 static const char* kRawScoreKey;
27 static const char* kPointsAddedTodayKey;
28 static const char* kLastEngagementTimeKey;
30 // The maximum number of points that are allowed.
31 static const double kMaxPoints;
33 // The maximum number of points that can be accrued in one day.
34 static const double kMaxPointsPerDay;
36 // The number of points given for a navigation.
37 static const double kNavigationPoints;
39 // Decaying works by removing a portion of the score periodically. This
40 // constant determines how often that happens.
41 static const int kDecayPeriodInDays;
43 // How much the score decays after every kDecayPeriodInDays.
44 static const double kDecayPoints;
46 // The SiteEngagementService does not take ownership of |clock|. It is the
47 // responsibility of the caller to make sure |clock| outlives this
48 // SiteEngagementScore.
49 SiteEngagementScore(base::Clock* clock,
50 const base::DictionaryValue& score_dict);
51 ~SiteEngagementScore();
53 double Score() const;
54 void AddPoints(double points);
56 // Updates the content settings dictionary |score_dict| with the current score
57 // fields. Returns true if |score_dict| changed, otherwise return false.
58 bool UpdateScoreDict(base::DictionaryValue* score_dict);
60 private:
61 friend class SiteEngagementScoreTest;
63 // This version of the constructor is used in unit tests.
64 explicit SiteEngagementScore(base::Clock* clock);
66 // Determine the score, accounting for any decay.
67 double DecayedScore() const;
69 // The clock used to vend times. Enables time travelling in tests. Owned by
70 // the SiteEngagementService.
71 base::Clock* clock_;
73 // |raw_score_| is the score before any decay is applied.
74 double raw_score_;
76 // The points added 'today' are tracked to avoid adding more than
77 // kMaxPointsPerDay on any one day. 'Today' is defined in local time.
78 double points_added_today_;
80 // The last time the score was updated for engagement. Used in conjunction
81 // with |points_added_today_| to avoid adding more than kMaxPointsPerDay on
82 // any one day.
83 base::Time last_engagement_time_;
85 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore);
88 class SiteEngagementScoreProvider {
89 public:
90 // Returns a non-negative integer representing the engagement score of the
91 // origin for this URL.
92 virtual int GetScore(const GURL& url) = 0;
94 // Returns the sum of engagement points awarded to all sites.
95 virtual int GetTotalEngagementPoints() = 0;
98 // Stores and retrieves the engagement score of an origin.
100 // An engagement score is a positive integer that represents how much a user has
101 // engaged with an origin - the higher it is, the more engagement the user has
102 // had with this site recently.
104 // Positive user activity, such as visiting the origin often and adding it to
105 // the homescreen, will increase the site engagement score. Negative activity,
106 // such as rejecting permission prompts or not responding to notifications, will
107 // decrease the site engagement score.
108 class SiteEngagementService : public KeyedService,
109 public SiteEngagementScoreProvider {
110 public:
111 static SiteEngagementService* Get(Profile* profile);
113 // Returns whether or not the SiteEngagementService is enabled.
114 static bool IsEnabled();
116 explicit SiteEngagementService(Profile* profile);
117 ~SiteEngagementService() override;
119 // Returns a map of all stored origins and their engagement scores.
120 std::map<GURL, int> GetScoreMap();
122 // Update the karma score of the origin matching |url| for user navigation.
123 void HandleNavigation(const GURL& url);
125 // Overridden from SiteEngagementScoreProvider:
126 int GetScore(const GURL& url) override;
127 int GetTotalEngagementPoints() override;
129 private:
130 Profile* profile_;
132 // The clock used to vend times.
133 base::DefaultClock clock_;
135 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService);
138 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_