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. Resets | |
63 // the number of points added today if the day has changed. | |
benwells
2015/10/02 01:33:25
Nit: you can delete the last sentence now.
dominickn
2015/10/02 04:54:50
Done.
| |
64 bool MaxPointsPerDayAdded(); | |
65 | |
61 // Updates the content settings dictionary |score_dict| with the current score | 66 // Updates the content settings dictionary |score_dict| with the current score |
62 // fields. Returns true if |score_dict| changed, otherwise return false. | 67 // fields. Returns true if |score_dict| changed, otherwise return false. |
63 bool UpdateScoreDict(base::DictionaryValue* score_dict); | 68 bool UpdateScoreDict(base::DictionaryValue* score_dict); |
64 | 69 |
65 private: | 70 private: |
66 friend class SiteEngagementScoreTest; | 71 friend class SiteEngagementScoreTest; |
67 | 72 |
68 // This version of the constructor is used in unit tests. | 73 // This version of the constructor is used in unit tests. |
69 explicit SiteEngagementScore(base::Clock* clock); | 74 explicit SiteEngagementScore(base::Clock* clock); |
70 | 75 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 ~SiteEngagementService() override; | 127 ~SiteEngagementService() override; |
123 | 128 |
124 // Returns a map of all stored origins and their engagement scores. | 129 // Returns a map of all stored origins and their engagement scores. |
125 std::map<GURL, double> GetScoreMap(); | 130 std::map<GURL, double> GetScoreMap(); |
126 | 131 |
127 // Update the karma score of the origin matching |url| for navigation. | 132 // Update the karma score of the origin matching |url| for navigation. |
128 void HandleNavigation(const GURL& url, ui::PageTransition transition); | 133 void HandleNavigation(const GURL& url, ui::PageTransition transition); |
129 | 134 |
130 // Update the karma score of the origin matching |url| for time-on-site, based | 135 // Update the karma score of the origin matching |url| for time-on-site, based |
131 // on user input. | 136 // on user input. |
132 void HandleUserInput(const GURL& url); | 137 void HandleUserInput(const GURL& url, |
138 SiteEngagementMetrics::EngagementType type); | |
133 | 139 |
134 // Overridden from SiteEngagementScoreProvider: | 140 // Overridden from SiteEngagementScoreProvider: |
135 double GetScore(const GURL& url) override; | 141 double GetScore(const GURL& url) override; |
136 double GetTotalEngagementPoints() override; | 142 double GetTotalEngagementPoints() override; |
137 | 143 |
138 private: | 144 private: |
139 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
146 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); | |
147 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); | |
140 | 148 |
141 // Only used in tests. | 149 // Only used in tests. |
142 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); | 150 SiteEngagementService(Profile* profile, scoped_ptr<base::Clock> clock); |
143 | 151 |
144 // Adds the specified number of points to the given origin, respecting the | 152 // Adds the specified number of points to the given origin, respecting the |
145 // maximum limits for the day and overall. | 153 // maximum limits for the day and overall. |
146 void AddPoints(const GURL& url, double points); | 154 void AddPoints(const GURL& url, double points); |
147 | 155 |
156 // Post startup tasks: cleaning up origins which have decayed to 0, and | |
157 // logging UMA statistics. | |
158 void AfterStartupTask(); | |
148 void CleanupEngagementScores(); | 159 void CleanupEngagementScores(); |
160 void RecordMetrics(); | |
161 | |
162 // Returns the number of origins with maximum daily and total engagement | |
163 // respectively. | |
164 int OriginsWithMaxDailyEngagement(); | |
165 int OriginsWithMaxEngagement(std::map<GURL, double>& score_map); | |
166 | |
benwells
2015/10/02 01:33:25
Nit: only one blank line.
dominickn
2015/10/02 04:54:50
Done.
| |
149 | 167 |
150 Profile* profile_; | 168 Profile* profile_; |
151 | 169 |
152 // The clock used to vend times. | 170 // The clock used to vend times. |
153 scoped_ptr<base::Clock> clock_; | 171 scoped_ptr<base::Clock> clock_; |
154 | 172 |
173 // Metrics are recorded at non-incognito browser startup, and then | |
174 // approximately once per hour thereafter. Store the local time at which | |
175 // metrics were previously uploaded: the first event which affects any | |
176 // origin's engagement score after an hour has elapsed triggers the next | |
177 // upload. | |
178 base::Time last_metrics_time_; | |
179 | |
155 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 180 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
156 | 181 |
157 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 182 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
158 }; | 183 }; |
159 | 184 |
160 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 185 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |