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, CheckHistograms); |
139 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
| 146 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); |
| 147 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); |
| 148 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); |
140 | 149 |
141 // Only used in tests. | 150 // Only used in tests. |
142 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); | 151 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); |
143 | 152 |
144 // Adds the specified number of points to the given origin, respecting the | 153 // Adds the specified number of points to the given origin, respecting the |
145 // maximum limits for the day and overall. | 154 // maximum limits for the day and overall. |
146 void AddPoints(const GURL& url, double points); | 155 void AddPoints(const GURL& url, double points); |
147 | 156 |
| 157 // Post startup tasks: cleaning up origins which have decayed to 0, and |
| 158 // logging UMA statistics. |
| 159 void AfterStartupTask(); |
148 void CleanupEngagementScores(); | 160 void CleanupEngagementScores(); |
| 161 void RecordMetrics(); |
| 162 |
| 163 // Returns the median engagement score of all recorded origins. |
| 164 double GetMedianEngagement(std::map<GURL, double>& score_map); |
| 165 |
| 166 // Returns the number of origins with maximum daily and total engagement |
| 167 // respectively. |
| 168 int OriginsWithMaxDailyEngagement(); |
| 169 int OriginsWithMaxEngagement(std::map<GURL, double>& score_map); |
149 | 170 |
150 Profile* profile_; | 171 Profile* profile_; |
151 | 172 |
152 // The clock used to vend times. | 173 // The clock used to vend times. |
153 scoped_ptr<base::Clock> clock_; | 174 scoped_ptr<base::Clock> clock_; |
154 | 175 |
| 176 // Metrics are recorded at non-incognito browser startup, and then |
| 177 // approximately once per hour thereafter. Store the local time at which |
| 178 // metrics were previously uploaded: the first event which affects any |
| 179 // origin's engagement score after an hour has elapsed triggers the next |
| 180 // upload. |
| 181 base::Time last_metrics_time_; |
| 182 |
155 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 183 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
156 | 184 |
157 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 185 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
158 }; | 186 }; |
159 | 187 |
160 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 188 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |