| 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/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/engagement/site_engagement_metrics.h" |
| 14 #include "components/keyed_service/core/keyed_service.h" | 15 #include "components/keyed_service/core/keyed_service.h" |
| 15 #include "ui/base/page_transition_types.h" | 16 #include "ui/base/page_transition_types.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 class DictionaryValue; | 19 class DictionaryValue; |
| 19 class Clock; | 20 class Clock; |
| 20 } | 21 } |
| 21 | 22 |
| 22 class GURL; | 23 class GURL; |
| 23 class Profile; | 24 class Profile; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 51 // The SiteEngagementService does not take ownership of |clock|. It is the | 52 // The SiteEngagementService does not take ownership of |clock|. It is the |
| 52 // responsibility of the caller to make sure |clock| outlives this | 53 // responsibility of the caller to make sure |clock| outlives this |
| 53 // SiteEngagementScore. | 54 // SiteEngagementScore. |
| 54 SiteEngagementScore(base::Clock* clock, | 55 SiteEngagementScore(base::Clock* clock, |
| 55 const base::DictionaryValue& score_dict); | 56 const base::DictionaryValue& score_dict); |
| 56 ~SiteEngagementScore(); | 57 ~SiteEngagementScore(); |
| 57 | 58 |
| 58 double Score() const; | 59 double Score() const; |
| 59 void AddPoints(double points); | 60 void AddPoints(double points); |
| 60 | 61 |
| 62 // Returns true if the maximum number of points today has been added. |
| 63 bool MaxPointsPerDayAdded(); |
| 64 |
| 61 // Updates the content settings dictionary |score_dict| with the current score | 65 // Updates the content settings dictionary |score_dict| with the current score |
| 62 // fields. Returns true if |score_dict| changed, otherwise return false. | 66 // fields. Returns true if |score_dict| changed, otherwise return false. |
| 63 bool UpdateScoreDict(base::DictionaryValue* score_dict); | 67 bool UpdateScoreDict(base::DictionaryValue* score_dict); |
| 64 | 68 |
| 65 private: | 69 private: |
| 66 friend class SiteEngagementScoreTest; | 70 friend class SiteEngagementScoreTest; |
| 67 | 71 |
| 68 // This version of the constructor is used in unit tests. | 72 // This version of the constructor is used in unit tests. |
| 69 explicit SiteEngagementScore(base::Clock* clock); | 73 explicit SiteEngagementScore(base::Clock* clock); |
| 70 | 74 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 ~SiteEngagementService() override; | 126 ~SiteEngagementService() override; |
| 123 | 127 |
| 124 // Returns a map of all stored origins and their engagement scores. | 128 // Returns a map of all stored origins and their engagement scores. |
| 125 std::map<GURL, double> GetScoreMap(); | 129 std::map<GURL, double> GetScoreMap(); |
| 126 | 130 |
| 127 // Update the karma score of the origin matching |url| for navigation. | 131 // Update the karma score of the origin matching |url| for navigation. |
| 128 void HandleNavigation(const GURL& url, ui::PageTransition transition); | 132 void HandleNavigation(const GURL& url, ui::PageTransition transition); |
| 129 | 133 |
| 130 // Update the karma score of the origin matching |url| for time-on-site, based | 134 // Update the karma score of the origin matching |url| for time-on-site, based |
| 131 // on user input. | 135 // on user input. |
| 132 void HandleUserInput(const GURL& url); | 136 void HandleUserInput(const GURL& url, |
| 137 SiteEngagementMetrics::EngagementType type); |
| 133 | 138 |
| 134 // Overridden from SiteEngagementScoreProvider: | 139 // Overridden from SiteEngagementScoreProvider: |
| 135 double GetScore(const GURL& url) override; | 140 double GetScore(const GURL& url) override; |
| 136 double GetTotalEngagementPoints() override; | 141 double GetTotalEngagementPoints() override; |
| 137 | 142 |
| 138 private: | 143 private: |
| 144 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); |
| 139 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
| 146 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 147 GetNavigationPointsAndHistograms); |
| 148 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 149 GetUserInputPointsAndHistograms); |
| 140 | 150 |
| 141 // Only used in tests. | 151 // Only used in tests. |
| 142 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); | 152 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); |
| 143 | 153 |
| 144 // Adds the specified number of points to the given origin, respecting the | 154 // Adds the specified number of points to the given origin, respecting the |
| 145 // maximum limits for the day and overall. | 155 // maximum limits for the day and overall. |
| 146 void AddPoints(const GURL& url, double points); | 156 void AddPoints(const GURL& url, double points); |
| 147 | 157 |
| 158 // Post startup tasks: cleaning up origins which have decayed to 0, and |
| 159 // logging UMA statistics. |
| 160 void AfterStartupTask(); |
| 148 void CleanupEngagementScores(); | 161 void CleanupEngagementScores(); |
| 162 void RecordMetrics(); |
| 163 |
| 164 // Returns the median engagement score of all recorded origins. |
| 165 double GetMedianEngagement(std::map<GURL, double>& score_map); |
| 166 |
| 167 // Returns the number of origins with maximum daily and total engagement |
| 168 // respectively. |
| 169 int OriginsWithMaxDailyEngagement(); |
| 170 int OriginsWithMaxEngagement(std::map<GURL, double>& score_map); |
| 149 | 171 |
| 150 Profile* profile_; | 172 Profile* profile_; |
| 151 | 173 |
| 152 // The clock used to vend times. | 174 // The clock used to vend times. |
| 153 scoped_ptr<base::Clock> clock_; | 175 scoped_ptr<base::Clock> clock_; |
| 154 | 176 |
| 177 // Metrics are recorded at non-incognito browser startup, and then |
| 178 // approximately once per hour thereafter. Store the local time at which |
| 179 // metrics were previously uploaded: the first event which affects any |
| 180 // origin's engagement score after an hour has elapsed triggers the next |
| 181 // upload. |
| 182 base::Time last_metrics_time_; |
| 183 |
| 155 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 184 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
| 156 | 185 |
| 157 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 186 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
| 158 }; | 187 }; |
| 159 | 188 |
| 160 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 189 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
| OLD | NEW |