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> |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); | 123 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); |
124 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); | 124 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); |
125 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); | 125 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); |
126 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); | 126 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); |
127 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 127 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
128 CleanupOriginsOnHistoryDeletion); | 128 CleanupOriginsOnHistoryDeletion); |
129 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); | 129 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); |
130 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); | 130 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); |
131 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, Observers); | 131 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, Observers); |
132 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); | 132 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); |
| 133 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastEngagementTime); |
133 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); | 134 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); |
134 | 135 |
135 // Only used in tests. | 136 // Only used in tests. |
136 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); | 137 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); |
137 | 138 |
138 // Adds the specified number of points to the given origin, respecting the | 139 // Adds the specified number of points to the given origin, respecting the |
139 // maximum limits for the day and overall. | 140 // maximum limits for the day and overall. |
140 void AddPoints(const GURL& url, double points); | 141 void AddPoints(const GURL& url, double points); |
141 | 142 |
142 // Retrieves the SiteEngagementScore object for |origin|. | 143 // Retrieves the SiteEngagementScore object for |origin|. |
143 SiteEngagementScore CreateEngagementScore(const GURL& origin); | 144 SiteEngagementScore CreateEngagementScore(const GURL& origin) const; |
144 const SiteEngagementScore CreateEngagementScore(const GURL& origin) const; | |
145 | 145 |
146 // Post startup tasks: cleaning up origins which have decayed to 0, and | 146 // Runs site engagement maintenance tasks. |
147 // logging UMA statistics. | |
148 void AfterStartupTask(); | 147 void AfterStartupTask(); |
149 void CleanupEngagementScores(); | 148 |
| 149 // Removes any origins which have decayed to 0 engagement. If |
| 150 // |update_last_engagement_time| is true, the last engagement time of all |
| 151 // origins is reset by calculating the delta between the last engagement event |
| 152 // recorded by the site engagement service and the origin. The origin's last |
| 153 // engagement time is then set to clock_->Now() - delta. |
| 154 // |
| 155 // If a user does not use the browser at all for some period of time, |
| 156 // engagement is not decayed, and the state is restored equivalent to how they |
| 157 // left it once they return. |
| 158 void CleanupEngagementScores(bool update_last_engagement_time) const; |
| 159 |
| 160 // Records UMA metrics. |
150 void RecordMetrics(); | 161 void RecordMetrics(); |
151 | 162 |
| 163 // Get and set the last engagement time from prefs. |
| 164 base::Time GetLastEngagementTime() const; |
| 165 void SetLastEngagementTime(base::Time last_engagement_time) const; |
| 166 |
| 167 // Get the maximum decay period and the stale period for last engagement |
| 168 // times. |
| 169 base::TimeDelta GetMaxDecayPeriod() const; |
| 170 base::TimeDelta GetStalePeriod() const; |
| 171 |
152 // Returns the median engagement score of all recorded origins. | 172 // Returns the median engagement score of all recorded origins. |
153 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; | 173 double GetMedianEngagement(const std::map<GURL, double>& score_map) const; |
154 | 174 |
155 // Update the engagement score of the origin loaded in |web_contents| for | 175 // Update the engagement score of the origin loaded in |web_contents| for |
156 // media playing. The points awarded are discounted if the media is being | 176 // media playing. The points awarded are discounted if the media is being |
157 // played in a non-visible tab. | 177 // played in a non-visible tab. |
158 void HandleMediaPlaying(content::WebContents* web_contents, bool is_hidden); | 178 void HandleMediaPlaying(content::WebContents* web_contents, bool is_hidden); |
159 | 179 |
160 // Update the engagement score of the origin loaded in |web_contents| for | 180 // Update the engagement score of the origin loaded in |web_contents| for |
161 // navigation. | 181 // navigation. |
162 void HandleNavigation(content::WebContents* web_contents, | 182 void HandleNavigation(content::WebContents* web_contents, |
163 ui::PageTransition transition); | 183 ui::PageTransition transition); |
164 | 184 |
165 // Update the engagement score of the origin loaded in |web_contents| for | 185 // Update the engagement score of the origin loaded in |web_contents| for |
166 // time-on-site, based on user input. | 186 // time-on-site, based on user input. |
167 void HandleUserInput(content::WebContents* web_contents, | 187 void HandleUserInput(content::WebContents* web_contents, |
168 SiteEngagementMetrics::EngagementType type); | 188 SiteEngagementMetrics::EngagementType type); |
169 | 189 |
| 190 // Returns true if the last engagement increasing event seen by the site |
| 191 // engagement service was sufficiently long ago that we need to reset all |
| 192 // scores to be relative to now. This ensures that users who do not use the |
| 193 // browser for an extended period of time do not have their engagement decay. |
| 194 bool IsLastEngagementStale() const; |
| 195 |
170 // Overridden from history::HistoryServiceObserver: | 196 // Overridden from history::HistoryServiceObserver: |
171 void OnURLsDeleted(history::HistoryService* history_service, | 197 void OnURLsDeleted(history::HistoryService* history_service, |
172 bool all_history, | 198 bool all_history, |
173 bool expired, | 199 bool expired, |
174 const history::URLRows& deleted_rows, | 200 const history::URLRows& deleted_rows, |
175 const std::set<GURL>& favicon_urls) override; | 201 const std::set<GURL>& favicon_urls) override; |
176 | 202 |
177 // Returns the number of origins with maximum daily and total engagement | 203 // Returns the number of origins with maximum daily and total engagement |
178 // respectively. | 204 // respectively. |
179 int OriginsWithMaxDailyEngagement() const; | 205 int OriginsWithMaxDailyEngagement() const; |
(...skipping 26 matching lines...) Expand all Loading... |
206 // A list of observers. When any origin registers an engagement-increasing | 232 // A list of observers. When any origin registers an engagement-increasing |
207 // event, each observer's OnEngagementIncreased method will be called. | 233 // event, each observer's OnEngagementIncreased method will be called. |
208 base::ObserverList<SiteEngagementObserver> observer_list_; | 234 base::ObserverList<SiteEngagementObserver> observer_list_; |
209 | 235 |
210 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 236 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
211 | 237 |
212 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 238 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
213 }; | 239 }; |
214 | 240 |
215 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 241 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
OLD | NEW |