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