OLD | NEW |
---|---|
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 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ | 5 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ |
6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ | 6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ |
7 | 7 |
dominickn
2016/05/25 06:52:43
#include <memory> for std::unique_ptr
calamity
2016/05/26 08:26:20
Done.
| |
8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "url/gurl.h" | |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 class Clock; | 15 class Clock; |
15 } | 16 } |
16 | 17 |
18 class HostContentSettingsMap; | |
19 | |
17 class SiteEngagementScore { | 20 class SiteEngagementScore { |
18 public: | 21 public: |
19 // The parameters which can be varied via field trial. All "points" values | 22 // The parameters which can be varied via field trial. All "points" values |
20 // should be appended to the end of the enum prior to MAX_VARIATION. | 23 // should be appended to the end of the enum prior to MAX_VARIATION. |
21 enum Variation { | 24 enum Variation { |
22 // The maximum number of points that can be accrued in one day. | 25 // The maximum number of points that can be accrued in one day. |
23 MAX_POINTS_PER_DAY = 0, | 26 MAX_POINTS_PER_DAY = 0, |
24 | 27 |
25 // The period over which site engagement decays. | 28 // The period over which site engagement decays. |
26 DECAY_PERIOD_IN_DAYS, | 29 DECAY_PERIOD_IN_DAYS, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 static double GetMediumEngagementBoundary(); | 80 static double GetMediumEngagementBoundary(); |
78 static double GetHighEngagementBoundary(); | 81 static double GetHighEngagementBoundary(); |
79 | 82 |
80 // Update the default engagement settings via variations. | 83 // Update the default engagement settings via variations. |
81 static void UpdateFromVariations(const char* param_name); | 84 static void UpdateFromVariations(const char* param_name); |
82 | 85 |
83 // The SiteEngagementScore does not take ownership of |clock|. It is the | 86 // The SiteEngagementScore does not take ownership of |clock|. It is the |
84 // responsibility of the caller to make sure |clock| outlives this | 87 // responsibility of the caller to make sure |clock| outlives this |
85 // SiteEngagementScore. | 88 // SiteEngagementScore. |
86 SiteEngagementScore(base::Clock* clock, | 89 SiteEngagementScore(base::Clock* clock, |
87 const base::DictionaryValue& score_dict); | 90 const GURL& origin, |
91 HostContentSettingsMap* settings_map); | |
92 SiteEngagementScore(SiteEngagementScore&& other); | |
88 ~SiteEngagementScore(); | 93 ~SiteEngagementScore(); |
89 | 94 |
95 SiteEngagementScore& operator=(SiteEngagementScore&& other); | |
96 | |
90 // Adds |points| to this score, respecting daily limits and the maximum | 97 // Adds |points| to this score, respecting daily limits and the maximum |
91 // possible score. Decays the score if it has not been updated recently | 98 // possible score. Decays the score if it has not been updated recently |
92 // enough. | 99 // enough. |
93 void AddPoints(double points); | 100 void AddPoints(double points); |
94 double GetScore() const; | 101 double GetScore() const; |
95 | 102 |
103 // Writes the values in this score into |settings_map_|. | |
104 void Commit(); | |
105 | |
96 // Returns true if the maximum number of points today has been added. | 106 // Returns true if the maximum number of points today has been added. |
97 bool MaxPointsPerDayAdded() const; | 107 bool MaxPointsPerDayAdded() const; |
98 | 108 |
99 // Resets the score to |points| and resets the daily point limit. If | 109 // Resets the score to |points| and resets the daily point limit. If |
100 // |updated_time| is non-null, sets the last engagement time and last | 110 // |updated_time| is non-null, sets the last engagement time and last |
101 // shortcut launch time (if it is non-null) to |updated_time|. Otherwise, last | 111 // shortcut launch time (if it is non-null) to |updated_time|. Otherwise, last |
102 // engagement time is set to the current time and last shortcut launch time is | 112 // engagement time is set to the current time and last shortcut launch time is |
103 // left unchanged. | 113 // left unchanged. |
104 // TODO(calamity): Ideally, all SiteEngagementScore methods should take a | 114 void Reset(double points, const base::Time updated_time); |
105 // base::Time argument like this one does rather than each SiteEngagementScore | |
106 // hold a pointer to a base::Clock. Then SiteEngagementScore doesn't need to | |
107 // worry about clock vending. See crbug.com/604305. | |
108 void Reset(double points, const base::Time* updated_time); | |
109 | |
110 // Updates the content settings dictionary |score_dict| with the current score | |
111 // fields. Returns true if |score_dict| changed, otherwise return false. | |
112 bool UpdateScoreDict(base::DictionaryValue* score_dict); | |
113 | 115 |
114 // Get/set the last time this origin was launched from an installed shortcut. | 116 // Get/set the last time this origin was launched from an installed shortcut. |
115 base::Time last_shortcut_launch_time() const { | 117 base::Time last_shortcut_launch_time() const { |
116 return last_shortcut_launch_time_; | 118 return last_shortcut_launch_time_; |
117 } | 119 } |
118 void set_last_shortcut_launch_time(const base::Time& time) { | 120 void set_last_shortcut_launch_time(const base::Time& time) { |
119 last_shortcut_launch_time_ = time; | 121 last_shortcut_launch_time_ = time; |
120 } | 122 } |
121 | 123 |
122 private: | 124 private: |
123 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PartiallyEmptyDictionary); | 125 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PartiallyEmptyDictionary); |
124 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PopulatedDictionary); | 126 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, PopulatedDictionary); |
125 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, Reset); | 127 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, Reset); |
126 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, FirstDailyEngagementBonus); | 128 FRIEND_TEST_ALL_PREFIXES(SiteEngagementScoreTest, FirstDailyEngagementBonus); |
127 friend class ImportantSitesUtilTest; | 129 friend class ImportantSitesUtilTest; |
128 friend class SiteEngagementHelperTest; | 130 friend class SiteEngagementHelperTest; |
129 friend class SiteEngagementScoreTest; | 131 friend class SiteEngagementScoreTest; |
130 friend class SiteEngagementServiceTest; | 132 friend class SiteEngagementServiceTest; |
131 | 133 |
132 // Array holding the values corresponding to each item in Variation array. | 134 // Array holding the values corresponding to each item in Variation array. |
133 static double param_values[]; | 135 static double param_values[]; |
134 | 136 |
135 // Keys used in the content settings dictionary. | 137 // Keys used in the content settings dictionary. |
136 static const char* kRawScoreKey; | 138 static const char* kRawScoreKey; |
137 static const char* kPointsAddedTodayKey; | 139 static const char* kPointsAddedTodayKey; |
138 static const char* kLastEngagementTimeKey; | 140 static const char* kLastEngagementTimeKey; |
139 static const char* kLastShortcutLaunchTimeKey; | 141 static const char* kLastShortcutLaunchTimeKey; |
140 | 142 |
141 // This version of the constructor is used in unit tests. | 143 // This version of the constructor is used in unit tests. |
142 explicit SiteEngagementScore(base::Clock* clock); | 144 explicit SiteEngagementScore( |
dominickn
2016/05/25 06:52:43
Nit: remove explicit and the unit tests comment
calamity
2016/05/26 08:26:20
The comment is still true.
dominickn
2016/05/26 09:33:20
Acknowledged.
| |
145 base::Clock* clock, | |
146 std::unique_ptr<base::DictionaryValue> score_dict); | |
143 | 147 |
144 // Determine the score, accounting for any decay. | 148 // Determine the score, accounting for any decay. |
145 double DecayedScore() const; | 149 double DecayedScore() const; |
146 | 150 |
147 // Determine any score bonus from having installed shortcuts. | 151 // Determine any score bonus from having installed shortcuts. |
148 double BonusScore() const; | 152 double BonusScore() const; |
149 | 153 |
150 // Sets fixed parameter values for testing site engagement. Ensure that any | 154 // Sets fixed parameter values for testing site engagement. Ensure that any |
151 // newly added parameters receive a fixed value here. | 155 // newly added parameters receive a fixed value here. |
152 static void SetParamValuesForTesting(); | 156 static void SetParamValuesForTesting(); |
153 | 157 |
158 // Updates the content settings dictionary |score_dict| with the current score | |
159 // fields. Returns true if |score_dict| changed, otherwise return false. | |
160 bool UpdateScoreDict(base::DictionaryValue* score_dict); | |
161 | |
154 // The clock used to vend times. Enables time travelling in tests. Owned by | 162 // The clock used to vend times. Enables time travelling in tests. Owned by |
155 // the SiteEngagementService. | 163 // the SiteEngagementService. |
156 base::Clock* clock_; | 164 base::Clock* clock_; |
157 | 165 |
158 // |raw_score_| is the score before any decay is applied. | 166 // |raw_score_| is the score before any decay is applied. |
159 double raw_score_; | 167 double raw_score_; |
160 | 168 |
161 // The points added 'today' are tracked to avoid adding more than | 169 // The points added 'today' are tracked to avoid adding more than |
162 // kMaxPointsPerDay on any one day. 'Today' is defined in local time. | 170 // kMaxPointsPerDay on any one day. 'Today' is defined in local time. |
163 double points_added_today_; | 171 double points_added_today_; |
164 | 172 |
165 // The last time the score was updated for engagement. Used in conjunction | 173 // The last time the score was updated for engagement. Used in conjunction |
166 // with |points_added_today_| to avoid adding more than kMaxPointsPerDay on | 174 // with |points_added_today_| to avoid adding more than kMaxPointsPerDay on |
167 // any one day. | 175 // any one day. |
168 base::Time last_engagement_time_; | 176 base::Time last_engagement_time_; |
169 | 177 |
170 // The last time the site with this score was launched from an installed | 178 // The last time the site with this score was launched from an installed |
171 // shortcut. | 179 // shortcut. |
172 base::Time last_shortcut_launch_time_; | 180 base::Time last_shortcut_launch_time_; |
173 | 181 |
182 // The dictionary that represents this engagement score. | |
183 std::unique_ptr<base::DictionaryValue> score_dict_; | |
184 | |
185 // The origin this score represents. | |
186 GURL origin_; | |
187 | |
188 // The settings map to write this score to when Commit() is called. | |
189 HostContentSettingsMap* settings_map_; | |
190 | |
174 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); | 191 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); |
175 }; | 192 }; |
176 | 193 |
177 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ | 194 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ |
OLD | NEW |