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 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/callback_forward.h" | |
12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
16 #include "chrome/browser/engagement/site_engagement_metrics.h" | 17 #include "chrome/browser/engagement/site_engagement_metrics.h" |
17 #include "components/history/core/browser/history_service_observer.h" | 18 #include "components/history/core/browser/history_service_observer.h" |
18 #include "components/keyed_service/core/keyed_service.h" | 19 #include "components/keyed_service/core/keyed_service.h" |
19 #include "ui/base/page_transition_types.h" | 20 #include "ui/base/page_transition_types.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
(...skipping 29 matching lines...) Expand all Loading... | |
51 // such as rejecting permission prompts or not responding to notifications, will | 52 // such as rejecting permission prompts or not responding to notifications, will |
52 // decrease the site engagement score. | 53 // decrease the site engagement score. |
53 class SiteEngagementService : public KeyedService, | 54 class SiteEngagementService : public KeyedService, |
54 public history::HistoryServiceObserver, | 55 public history::HistoryServiceObserver, |
55 public SiteEngagementScoreProvider { | 56 public SiteEngagementScoreProvider { |
56 public: | 57 public: |
57 // WebContentsObserver that detects engagement triggering events and notifies | 58 // WebContentsObserver that detects engagement triggering events and notifies |
58 // the service of them. | 59 // the service of them. |
59 class Helper; | 60 class Helper; |
60 | 61 |
62 // Callback used by the site engagement service to signal that a given origin | |
63 // has reached a given engagement score. When invoked, the callback will be | |
64 // passed the score for |url|'s origin at the time of invocation, and the URL | |
65 // which led to the engagement reaching that score (or the origin if the score | |
66 // was already at the requested level). | |
67 using EngagementCallback = base::Callback<void(const GURL&, double)>; | |
68 using EngagementCallbacks = std::vector<EngagementCallback>; | |
69 | |
61 enum EngagementLevel { | 70 enum EngagementLevel { |
62 ENGAGEMENT_LEVEL_NONE, | 71 ENGAGEMENT_LEVEL_NONE, |
63 ENGAGEMENT_LEVEL_LOW, | 72 ENGAGEMENT_LEVEL_LOW, |
64 ENGAGEMENT_LEVEL_MEDIUM, | 73 ENGAGEMENT_LEVEL_MEDIUM, |
65 ENGAGEMENT_LEVEL_HIGH, | 74 ENGAGEMENT_LEVEL_HIGH, |
66 ENGAGEMENT_LEVEL_MAX, | 75 ENGAGEMENT_LEVEL_MAX, |
67 }; | 76 }; |
68 | 77 |
69 // The name of the site engagement variation field trial. | 78 // The name of the site engagement variation field trial. |
70 static const char kEngagementParams[]; | 79 static const char kEngagementParams[]; |
(...skipping 19 matching lines...) Expand all Loading... | |
90 std::map<GURL, double> GetScoreMap() const; | 99 std::map<GURL, double> GetScoreMap() const; |
91 | 100 |
92 // Returns whether the engagement service has enough data to make meaningful | 101 // Returns whether the engagement service has enough data to make meaningful |
93 // decisions. Clients should avoid using engagement in their heuristic until | 102 // decisions. Clients should avoid using engagement in their heuristic until |
94 // this is true. | 103 // this is true. |
95 bool IsBootstrapped(); | 104 bool IsBootstrapped(); |
96 | 105 |
97 // Returns whether |url| has at least the given |level| of engagement. | 106 // Returns whether |url| has at least the given |level| of engagement. |
98 bool IsEngagementAtLeast(const GURL& url, EngagementLevel level) const; | 107 bool IsEngagementAtLeast(const GURL& url, EngagementLevel level) const; |
99 | 108 |
109 // Register |callback| to be run on the UI thread when the engagement for | |
110 // |url|'s origin reaches |level| or |score|. Callbacks for origins which have | |
calamity
2016/05/19 03:28:22
It's not obvious that this only happens when engag
dominickn
2016/05/19 04:36:15
I've made this comment more explicit - it's equal
| |
111 // already reached the requested score will be dispatched as soon as they are | |
112 // registered. Callers are responsible for ensuring that |callback| is safe to | |
113 // run at any time. | |
114 void RegisterCallback(const EngagementCallback& callback, | |
115 const GURL& url, | |
116 EngagementLevel level); | |
117 void RegisterCallback(const EngagementCallback& callback, | |
118 const GURL& url, | |
119 double score); | |
120 | |
100 // Resets the engagement score |url| to |score|, clearing daily limits. | 121 // Resets the engagement score |url| to |score|, clearing daily limits. |
101 void ResetScoreForURL(const GURL& url, double score); | 122 void ResetScoreForURL(const GURL& url, double score); |
102 | 123 |
103 // Update the last time |url| was opened from an installed shortcut to be | 124 // Update the last time |url| was opened from an installed shortcut to be |
104 // clock_->Now(). | 125 // clock_->Now(). |
105 void SetLastShortcutLaunchTime(const GURL& url); | 126 void SetLastShortcutLaunchTime(const GURL& url); |
106 | 127 |
107 // Overridden from SiteEngagementScoreProvider. | 128 // Overridden from SiteEngagementScoreProvider. |
108 double GetScore(const GURL& url) const override; | 129 double GetScore(const GURL& url) const override; |
109 double GetTotalEngagementPoints() const override; | 130 double GetTotalEngagementPoints() const override; |
110 | 131 |
111 private: | 132 private: |
133 // Wrapping struct for callbacks. | |
134 struct EngagementCallbackWrapper { | |
135 EngagementCallback* callback; | |
136 GURL origin; | |
137 double score; | |
138 }; | |
139 | |
112 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); | 140 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); |
113 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 141 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
114 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); | 142 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); |
115 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); | 143 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); |
116 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); | 144 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); |
117 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); | 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); |
118 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); | 146 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); |
119 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 147 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
120 CleanupOriginsOnHistoryDeletion); | 148 CleanupOriginsOnHistoryDeletion); |
121 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); | 149 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); |
122 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); | 150 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); |
151 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, RegisterCallbacks); | |
123 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); | 152 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); |
124 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); | 153 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); |
125 | 154 |
126 // Only used in tests. | 155 // Only used in tests. |
127 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); | 156 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); |
128 | 157 |
129 // Adds the specified number of points to the given origin, respecting the | 158 // Adds the specified number of points to the given origin, respecting the |
130 // maximum limits for the day and overall. | 159 // maximum limits for the day and overall. |
131 void AddPoints(const GURL& url, double points); | 160 void AddPoints(const GURL& url, double points); |
132 | 161 |
133 // Post startup tasks: cleaning up origins which have decayed to 0, and | 162 // Post startup tasks: cleaning up origins which have decayed to 0, and |
134 // logging UMA statistics. | 163 // logging UMA statistics. |
135 void AfterStartupTask(); | 164 void AfterStartupTask(); |
136 void CleanupEngagementScores(); | 165 void CleanupEngagementScores(); |
137 void RecordMetrics(); | 166 void RecordMetrics(); |
138 | 167 |
139 // Returns the median engagement score of all recorded origins. | 168 // Returns the median engagement score of all recorded origins. |
140 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; | 169 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; |
141 | 170 |
171 // Returns the minimum engagement score corresponding to |level|. | |
172 double GetScoreForEngagementLevel(EngagementLevel level) const; | |
173 | |
142 // Update the engagement score of the origin matching |url| for media playing. | 174 // Update the engagement score of the origin matching |url| for media playing. |
143 // The points awarded are discounted if the media is being played in a non- | 175 // The points awarded are discounted if the media is being played in a non- |
144 // visible tab. | 176 // visible tab. |
145 void HandleMediaPlaying(const GURL& url, bool is_hidden); | 177 void HandleMediaPlaying(const GURL& url, bool is_hidden); |
146 | 178 |
147 // Update the engagement score of the origin matching |url| for navigation. | 179 // Update the engagement score of the origin matching |url| for navigation. |
148 void HandleNavigation(const GURL& url, ui::PageTransition transition); | 180 void HandleNavigation(const GURL& url, ui::PageTransition transition); |
149 | 181 |
150 // Update the engagement score of the origin matching |url| for time-on-site, | 182 // Update the engagement score of the origin matching |url| for time-on-site, |
151 // based on user input. | 183 // based on user input. |
(...skipping 13 matching lines...) Expand all Loading... | |
165 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const; | 197 int OriginsWithMaxEngagement(const std::map<GURL, double>& score_map) const; |
166 | 198 |
167 // Callback for the history service when it is asked for a map of origins to | 199 // Callback for the history service when it is asked for a map of origins to |
168 // how many URLs corresponding to that origin remain in history. | 200 // how many URLs corresponding to that origin remain in history. |
169 void GetCountsAndLastVisitForOriginsComplete( | 201 void GetCountsAndLastVisitForOriginsComplete( |
170 history::HistoryService* history_service, | 202 history::HistoryService* history_service, |
171 const std::multiset<GURL>& deleted_url_origins, | 203 const std::multiset<GURL>& deleted_url_origins, |
172 bool expired, | 204 bool expired, |
173 const history::OriginCountAndLastVisitMap& remaining_origin_counts); | 205 const history::OriginCountAndLastVisitMap& remaining_origin_counts); |
174 | 206 |
207 // Runs any callbacks in |engagement_callbacks_| which have been registered to | |
208 // listen to |url|'s origin reaching at least |score| engagement. | |
209 void ProcessCallbacks(const GURL& url, double score); | |
210 | |
211 // Dispatches |callback| to be run on the UI thread, and deletes it once it | |
212 // has finished running. | |
213 void PostCallback(const EngagementCallback* callback, | |
214 const GURL& url, | |
215 double score); | |
216 | |
175 // Resets the engagement score for |url| to |score|, and sets the last | 217 // Resets the engagement score for |url| to |score|, and sets the last |
176 // engagement time and last shortcut launch time (if it is non-null) to | 218 // engagement time and last shortcut launch time (if it is non-null) to |
177 // |updated_time|. Clears daily limits. | 219 // |updated_time|. Clears daily limits. |
178 void ResetScoreAndAccessTimesForURL(const GURL& url, | 220 void ResetScoreAndAccessTimesForURL(const GURL& url, |
179 double score, | 221 double score, |
180 const base::Time* updated_time); | 222 const base::Time* updated_time); |
181 | 223 |
224 // Runs |callback| and deletes it once it has finished execution. | |
225 void RunAndDeleteCallback(const EngagementCallback* callback, | |
226 const GURL& origin, | |
227 double score) const; | |
228 | |
182 Profile* profile_; | 229 Profile* profile_; |
183 | 230 |
184 // The clock used to vend times. | 231 // The clock used to vend times. |
185 std::unique_ptr<base::Clock> clock_; | 232 std::unique_ptr<base::Clock> clock_; |
186 | 233 |
187 // Metrics are recorded at non-incognito browser startup, and then | 234 // Metrics are recorded at non-incognito browser startup, and then |
188 // approximately once per hour thereafter. Store the local time at which | 235 // approximately once per hour thereafter. Store the local time at which |
189 // metrics were previously uploaded: the first event which affects any | 236 // metrics were previously uploaded: the first event which affects any |
190 // origin's engagement score after an hour has elapsed triggers the next | 237 // origin's engagement score after an hour has elapsed triggers the next |
191 // upload. | 238 // upload. |
192 base::Time last_metrics_time_; | 239 base::Time last_metrics_time_; |
193 | 240 |
241 // A list of callbacks, registered with an origin and an engagement score. | |
242 // Once a callback is dispatched, its wrapper will be removed from the list. | |
243 std::vector<EngagementCallbackWrapper> engagement_callbacks_; | |
244 | |
194 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 245 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
195 | 246 |
196 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 247 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
197 }; | 248 }; |
198 | 249 |
199 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 250 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |