OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/time/default_clock.h" | 11 #include "base/time/default_clock.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
| 14 #include "ui/base/page_transition_types.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DictionaryValue; | 17 class DictionaryValue; |
17 } | 18 } |
18 | 19 |
19 class GURL; | 20 class GURL; |
20 class Profile; | 21 class Profile; |
21 | 22 |
22 class SiteEngagementScore { | 23 class SiteEngagementScore { |
23 public: | 24 public: |
24 // Keys used in the content settings dictionary. | 25 // Keys used in the content settings dictionary. |
25 static const char* kRawScoreKey; | 26 static const char* kRawScoreKey; |
26 static const char* kPointsAddedTodayKey; | 27 static const char* kPointsAddedTodayKey; |
27 static const char* kLastEngagementTimeKey; | 28 static const char* kLastEngagementTimeKey; |
28 | 29 |
29 // The maximum number of points that are allowed. | 30 // The maximum number of points that are allowed. |
30 static const double kMaxPoints; | 31 static const double kMaxPoints; |
31 | 32 |
32 // The maximum number of points that can be accrued in one day. | 33 // The maximum number of points that can be accrued in one day. |
33 static const double kMaxPointsPerDay; | 34 static const double kMaxPointsPerDay; |
34 | 35 |
35 // The number of points given for a navigation. | 36 // The number of points given for navigations. |
36 static const double kNavigationPoints; | 37 static const double kNavigationPoints; |
37 | 38 |
| 39 // The number of points given for user input (indicating time-on-site). |
| 40 static const double kUserInputPoints; |
| 41 |
38 // Decaying works by removing a portion of the score periodically. This | 42 // Decaying works by removing a portion of the score periodically. This |
39 // constant determines how often that happens. | 43 // constant determines how often that happens. |
40 static const int kDecayPeriodInDays; | 44 static const int kDecayPeriodInDays; |
41 | 45 |
42 // How much the score decays after every kDecayPeriodInDays. | 46 // How much the score decays after every kDecayPeriodInDays. |
43 static const double kDecayPoints; | 47 static const double kDecayPoints; |
44 | 48 |
45 // The SiteEngagementService does not take ownership of |clock|. It is the | 49 // The SiteEngagementService does not take ownership of |clock|. It is the |
46 // responsibility of the caller to make sure |clock| outlives this | 50 // responsibility of the caller to make sure |clock| outlives this |
47 // SiteEngagementScore. | 51 // SiteEngagementScore. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // any one day. | 85 // any one day. |
82 base::Time last_engagement_time_; | 86 base::Time last_engagement_time_; |
83 | 87 |
84 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); | 88 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); |
85 }; | 89 }; |
86 | 90 |
87 class SiteEngagementScoreProvider { | 91 class SiteEngagementScoreProvider { |
88 public: | 92 public: |
89 // Returns a non-negative integer representing the engagement score of the | 93 // Returns a non-negative integer representing the engagement score of the |
90 // origin for this URL. | 94 // origin for this URL. |
91 virtual int GetScore(const GURL& url) = 0; | 95 virtual double GetScore(const GURL& url) = 0; |
92 | 96 |
93 // Returns the sum of engagement points awarded to all sites. | 97 // Returns the sum of engagement points awarded to all sites. |
94 virtual int GetTotalEngagementPoints() = 0; | 98 virtual double GetTotalEngagementPoints() = 0; |
95 }; | 99 }; |
96 | 100 |
97 // Stores and retrieves the engagement score of an origin. | 101 // Stores and retrieves the engagement score of an origin. |
98 // | 102 // |
99 // An engagement score is a positive integer that represents how much a user has | 103 // An engagement score is a positive integer that represents how much a user has |
100 // engaged with an origin - the higher it is, the more engagement the user has | 104 // engaged with an origin - the higher it is, the more engagement the user has |
101 // had with this site recently. | 105 // had with this site recently. |
102 // | 106 // |
103 // Positive user activity, such as visiting the origin often and adding it to | 107 // Positive user activity, such as visiting the origin often and adding it to |
104 // the homescreen, will increase the site engagement score. Negative activity, | 108 // the homescreen, will increase the site engagement score. Negative activity, |
105 // such as rejecting permission prompts or not responding to notifications, will | 109 // such as rejecting permission prompts or not responding to notifications, will |
106 // decrease the site engagement score. | 110 // decrease the site engagement score. |
107 class SiteEngagementService : public KeyedService, | 111 class SiteEngagementService : public KeyedService, |
108 public SiteEngagementScoreProvider { | 112 public SiteEngagementScoreProvider { |
109 public: | 113 public: |
110 static SiteEngagementService* Get(Profile* profile); | 114 static SiteEngagementService* Get(Profile* profile); |
111 | 115 |
112 // Returns whether or not the SiteEngagementService is enabled. | 116 // Returns whether or not the SiteEngagementService is enabled. |
113 static bool IsEnabled(); | 117 static bool IsEnabled(); |
114 | 118 |
115 explicit SiteEngagementService(Profile* profile); | 119 explicit SiteEngagementService(Profile* profile); |
116 ~SiteEngagementService() override; | 120 ~SiteEngagementService() override; |
117 | 121 |
118 // Returns a map of all stored origins and their engagement scores. | 122 // Returns a map of all stored origins and their engagement scores. |
119 std::map<GURL, int> GetScoreMap(); | 123 std::map<GURL, double> GetScoreMap(); |
120 | 124 |
121 // Update the karma score of the origin matching |url| for user navigation. | 125 // Update the karma score of the origin matching |url| for navigation. |
122 void HandleNavigation(const GURL& url); | 126 void HandleNavigation(const GURL& url, ui::PageTransition transition); |
| 127 |
| 128 // Update the karma score of the origin matching |url| for time-on-site, based |
| 129 // on user input. |
| 130 void HandleUserInput(const GURL& url); |
123 | 131 |
124 // Overridden from SiteEngagementScoreProvider: | 132 // Overridden from SiteEngagementScoreProvider: |
125 int GetScore(const GURL& url) override; | 133 double GetScore(const GURL& url) override; |
126 int GetTotalEngagementPoints() override; | 134 double GetTotalEngagementPoints() override; |
127 | 135 |
128 private: | 136 private: |
| 137 // Adds the specified number of points to the given origin, respecting the |
| 138 // maximum limits for the day and overall. |
| 139 void AddPoints(const GURL& url, double points); |
| 140 |
129 Profile* profile_; | 141 Profile* profile_; |
130 | 142 |
131 // The clock used to vend times. | 143 // The clock used to vend times. |
132 base::DefaultClock clock_; | 144 base::DefaultClock clock_; |
133 | 145 |
134 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 146 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
135 }; | 147 }; |
136 | 148 |
137 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 149 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |