| 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 <array> | 8 #include <array> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/engagement/site_engagement_details.mojom.h" |
| 15 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" | 16 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class Clock; | 20 class Clock; |
| 20 } | 21 } |
| 21 | 22 |
| 22 class HostContentSettingsMap; | 23 class HostContentSettingsMap; |
| 23 | 24 |
| 24 class SiteEngagementScore { | 25 class SiteEngagementScore { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 HostContentSettingsMap* settings); | 131 HostContentSettingsMap* settings); |
| 131 SiteEngagementScore(SiteEngagementScore&& other); | 132 SiteEngagementScore(SiteEngagementScore&& other); |
| 132 ~SiteEngagementScore(); | 133 ~SiteEngagementScore(); |
| 133 | 134 |
| 134 SiteEngagementScore& operator=(SiteEngagementScore&& other); | 135 SiteEngagementScore& operator=(SiteEngagementScore&& other); |
| 135 | 136 |
| 136 // Adds |points| to this score, respecting daily limits and the maximum | 137 // Adds |points| to this score, respecting daily limits and the maximum |
| 137 // possible score. Decays the score if it has not been updated recently | 138 // possible score. Decays the score if it has not been updated recently |
| 138 // enough. | 139 // enough. |
| 139 void AddPoints(double points); | 140 void AddPoints(double points); |
| 140 double GetScore() const; | 141 |
| 142 // Returns the total score, taking into account the base, bonus and maximum |
| 143 // values. |
| 144 double GetTotalScore() const; |
| 145 |
| 146 // Returns a structure containing the origin URL and score, and details |
| 147 // of the base and bonus scores. Note that the |score| is limited to |
| 148 // kMaxPoints, while the detailed scores are returned raw. |
| 149 mojom::SiteEngagementDetails GetDetails() const; |
| 141 | 150 |
| 142 // Writes the values in this score into |settings_map_|. | 151 // Writes the values in this score into |settings_map_|. |
| 143 void Commit(); | 152 void Commit(); |
| 144 | 153 |
| 145 // Returns the discrete engagement level for this score. | 154 // Returns the discrete engagement level for this score. |
| 146 blink::mojom::EngagementLevel GetEngagementLevel() const; | 155 blink::mojom::EngagementLevel GetEngagementLevel() const; |
| 147 | 156 |
| 148 // Returns true if the maximum number of points today has been added. | 157 // Returns true if the maximum number of points today has been added. |
| 149 bool MaxPointsPerDayAdded() const; | 158 bool MaxPointsPerDayAdded() const; |
| 150 | 159 |
| 151 // Resets the score to |points| and resets the daily point limit. If | 160 // Resets the score to |points| and resets the daily point limit. If |
| 152 // |updated_time| is non-null, sets the last engagement time to that value. | 161 // |updated_time| is non-null, sets the last engagement time to that value. |
| 153 void Reset(double points, const base::Time updated_time); | 162 void Reset(double points, const base::Time updated_time); |
| 154 | 163 |
| 164 // Returns the raw, un-decayed score, without bonuses applied. |
| 165 double raw_score() { return raw_score_; } |
| 166 |
| 155 // Get/set the last time this origin was launched from an installed shortcut. | 167 // Get/set the last time this origin was launched from an installed shortcut. |
| 156 base::Time last_shortcut_launch_time() const { | 168 base::Time last_shortcut_launch_time() const { |
| 157 return last_shortcut_launch_time_; | 169 return last_shortcut_launch_time_; |
| 158 } | 170 } |
| 159 void set_last_shortcut_launch_time(const base::Time& time) { | 171 void set_last_shortcut_launch_time(const base::Time& time) { |
| 160 last_shortcut_launch_time_ = time; | 172 last_shortcut_launch_time_ = time; |
| 161 } | 173 } |
| 162 | 174 |
| 163 // Get/set the last time this origin recorded an engagement change. | 175 // Get/set the last time this origin recorded an engagement change. |
| 164 base::Time last_engagement_time() const { | 176 base::Time last_engagement_time() const { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 189 static const char* kLastShortcutLaunchTimeKey; | 201 static const char* kLastShortcutLaunchTimeKey; |
| 190 | 202 |
| 191 // This version of the constructor is used in unit tests. | 203 // This version of the constructor is used in unit tests. |
| 192 SiteEngagementScore(base::Clock* clock, | 204 SiteEngagementScore(base::Clock* clock, |
| 193 const GURL& origin, | 205 const GURL& origin, |
| 194 std::unique_ptr<base::DictionaryValue> score_dict); | 206 std::unique_ptr<base::DictionaryValue> score_dict); |
| 195 | 207 |
| 196 // Determine the score, accounting for any decay. | 208 // Determine the score, accounting for any decay. |
| 197 double DecayedScore() const; | 209 double DecayedScore() const; |
| 198 | 210 |
| 199 // Determine any score bonus from having installed shortcuts. | 211 // Determine bonus from being installed, and having been launched recently.. |
| 200 double BonusScore() const; | 212 double BonusIfShortcutLaunched() const; |
| 213 |
| 214 // Determine bonus from having been granted notifications permission. |
| 215 double BonusIfHasNotifications() const; |
| 201 | 216 |
| 202 // Updates the content settings dictionary |score_dict| with the current score | 217 // Updates the content settings dictionary |score_dict| with the current score |
| 203 // fields. Returns true if |score_dict| changed, otherwise return false. | 218 // fields. Returns true if |score_dict| changed, otherwise return false. |
| 204 bool UpdateScoreDict(base::DictionaryValue* score_dict); | 219 bool UpdateScoreDict(base::DictionaryValue* score_dict); |
| 205 | 220 |
| 206 // The clock used to vend times. Enables time travelling in tests. Owned by | 221 // The clock used to vend times. Enables time travelling in tests. Owned by |
| 207 // the SiteEngagementService. | 222 // the SiteEngagementService. |
| 208 base::Clock* clock_; | 223 base::Clock* clock_; |
| 209 | 224 |
| 210 // |raw_score_| is the score before any decay is applied. | 225 // |raw_score_| is the score before any decay is applied. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 229 // The origin this score represents. | 244 // The origin this score represents. |
| 230 GURL origin_; | 245 GURL origin_; |
| 231 | 246 |
| 232 // The settings to write this score to when Commit() is called. | 247 // The settings to write this score to when Commit() is called. |
| 233 HostContentSettingsMap* settings_map_; | 248 HostContentSettingsMap* settings_map_; |
| 234 | 249 |
| 235 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); | 250 DISALLOW_COPY_AND_ASSIGN(SiteEngagementScore); |
| 236 }; | 251 }; |
| 237 | 252 |
| 238 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ | 253 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SCORE_H_ |
| OLD | NEW |