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