| 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/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "build/build_config.h" |
| 17 #include "chrome/browser/engagement/site_engagement_metrics.h" | 18 #include "chrome/browser/engagement/site_engagement_metrics.h" |
| 18 #include "chrome/browser/engagement/site_engagement_observer.h" | 19 #include "chrome/browser/engagement/site_engagement_observer.h" |
| 19 #include "components/history/core/browser/history_service_observer.h" | 20 #include "components/history/core/browser/history_service_observer.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "ui/base/page_transition_types.h" | 22 #include "ui/base/page_transition_types.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 class Clock; | 25 class Clock; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 class WebContents; | 29 class WebContents; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace history { | 32 namespace history { |
| 32 class HistoryService; | 33 class HistoryService; |
| 33 } | 34 } |
| 34 | 35 |
| 35 class GURL; | 36 class GURL; |
| 36 class HostContentSettingsMap; | 37 class HostContentSettingsMap; |
| 37 class Profile; | 38 class Profile; |
| 38 class SiteEngagementScore; | 39 class SiteEngagementScore; |
| 39 | 40 |
| 41 #if defined(OS_ANDROID) |
| 42 class SiteEngagementServiceAndroid; |
| 43 #endif |
| 44 |
| 40 class SiteEngagementScoreProvider { | 45 class SiteEngagementScoreProvider { |
| 41 public: | 46 public: |
| 42 // Returns a non-negative integer representing the engagement score of the | 47 // Returns a non-negative integer representing the engagement score of the |
| 43 // origin for this URL. | 48 // origin for this URL. |
| 44 virtual double GetScore(const GURL& url) const = 0; | 49 virtual double GetScore(const GURL& url) const = 0; |
| 45 | 50 |
| 46 // Returns the sum of engagement points awarded to all sites. | 51 // Returns the sum of engagement points awarded to all sites. |
| 47 virtual double GetTotalEngagementPoints() const = 0; | 52 virtual double GetTotalEngagementPoints() const = 0; |
| 48 }; | 53 }; |
| 49 | 54 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 // Update the last time |url| was opened from an installed shortcut to be | 132 // Update the last time |url| was opened from an installed shortcut to be |
| 128 // clock_->Now(). | 133 // clock_->Now(). |
| 129 void SetLastShortcutLaunchTime(const GURL& url); | 134 void SetLastShortcutLaunchTime(const GURL& url); |
| 130 | 135 |
| 131 // Overridden from SiteEngagementScoreProvider. | 136 // Overridden from SiteEngagementScoreProvider. |
| 132 double GetScore(const GURL& url) const override; | 137 double GetScore(const GURL& url) const override; |
| 133 double GetTotalEngagementPoints() const override; | 138 double GetTotalEngagementPoints() const override; |
| 134 | 139 |
| 135 private: | 140 private: |
| 136 friend class SiteEngagementObserver; | 141 friend class SiteEngagementObserver; |
| 142 friend class SiteEngagementServiceAndroid; |
| 137 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); | 143 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); |
| 138 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 144 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
| 139 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 140 CleanupMovesScoreBackToNow); | 146 CleanupMovesScoreBackToNow); |
| 141 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 147 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 142 CleanupMovesScoreBackToRebase); | 148 CleanupMovesScoreBackToRebase); |
| 143 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 149 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 144 CleanupEngagementScoresProportional); | 150 CleanupEngagementScoresProportional); |
| 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); | 151 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ClearHistoryForURLs); |
| 146 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); | 152 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetMedianEngagement); |
| 147 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); | 153 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalNavigationPoints); |
| 148 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); | 154 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetTotalUserInputPoints); |
| 149 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, RestrictedToHTTPAndHTTPS); | 155 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, RestrictedToHTTPAndHTTPS); |
| 150 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); | 156 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastShortcutLaunch); |
| 151 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 157 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 152 CleanupOriginsOnHistoryDeletion); | 158 CleanupOriginsOnHistoryDeletion); |
| 153 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); | 159 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, IsBootstrapped); |
| 154 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); | 160 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, EngagementLevel); |
| 155 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, Observers); | 161 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, Observers); |
| 156 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); | 162 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, ScoreDecayHistograms); |
| 157 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastEngagementTime); | 163 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, LastEngagementTime); |
| 158 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 164 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| 159 IncognitoEngagementService); | 165 IncognitoEngagementService); |
| 160 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetScoreFromSettings); | 166 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, GetScoreFromSettings); |
| 161 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); | 167 FRIEND_TEST_ALL_PREFIXES(AppBannerSettingsHelperTest, SiteEngagementTrigger); |
| 162 | 168 |
| 169 #if defined(OS_ANDROID) |
| 170 // Shim class to expose the service to Java. |
| 171 SiteEngagementServiceAndroid* GetAndroidService() const; |
| 172 void SetAndroidService( |
| 173 std::unique_ptr<SiteEngagementServiceAndroid> android_service); |
| 174 #endif |
| 175 |
| 163 // Only used in tests. | 176 // Only used in tests. |
| 164 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); | 177 SiteEngagementService(Profile* profile, std::unique_ptr<base::Clock> clock); |
| 165 | 178 |
| 166 // Adds the specified number of points to the given origin, respecting the | 179 // Adds the specified number of points to the given origin, respecting the |
| 167 // maximum limits for the day and overall. | 180 // maximum limits for the day and overall. |
| 168 void AddPoints(const GURL& url, double points); | 181 void AddPoints(const GURL& url, double points); |
| 169 | 182 |
| 170 // Retrieves the SiteEngagementScore object for |origin|. | 183 // Retrieves the SiteEngagementScore object for |origin|. |
| 171 SiteEngagementScore CreateEngagementScore(const GURL& origin) const; | 184 SiteEngagementScore CreateEngagementScore(const GURL& origin) const; |
| 172 | 185 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 259 |
| 247 // Add and remove observers of this service. | 260 // Add and remove observers of this service. |
| 248 void AddObserver(SiteEngagementObserver* observer); | 261 void AddObserver(SiteEngagementObserver* observer); |
| 249 void RemoveObserver(SiteEngagementObserver* observer); | 262 void RemoveObserver(SiteEngagementObserver* observer); |
| 250 | 263 |
| 251 Profile* profile_; | 264 Profile* profile_; |
| 252 | 265 |
| 253 // The clock used to vend times. | 266 // The clock used to vend times. |
| 254 std::unique_ptr<base::Clock> clock_; | 267 std::unique_ptr<base::Clock> clock_; |
| 255 | 268 |
| 269 #if defined(OS_ANDROID) |
| 270 std::unique_ptr<SiteEngagementServiceAndroid> android_service_; |
| 271 #endif |
| 272 |
| 256 // Metrics are recorded at non-incognito browser startup, and then | 273 // Metrics are recorded at non-incognito browser startup, and then |
| 257 // approximately once per hour thereafter. Store the local time at which | 274 // approximately once per hour thereafter. Store the local time at which |
| 258 // metrics were previously uploaded: the first event which affects any | 275 // metrics were previously uploaded: the first event which affects any |
| 259 // origin's engagement score after an hour has elapsed triggers the next | 276 // origin's engagement score after an hour has elapsed triggers the next |
| 260 // upload. | 277 // upload. |
| 261 base::Time last_metrics_time_; | 278 base::Time last_metrics_time_; |
| 262 | 279 |
| 263 // A list of observers. When any origin registers an engagement-increasing | 280 // A list of observers. When any origin registers an engagement-increasing |
| 264 // event, each observer's OnEngagementIncreased method will be called. | 281 // event, each observer's OnEngagementIncreased method will be called. |
| 265 base::ObserverList<SiteEngagementObserver> observer_list_; | 282 base::ObserverList<SiteEngagementObserver> observer_list_; |
| 266 | 283 |
| 267 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 284 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
| 268 | 285 |
| 269 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 286 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
| 270 }; | 287 }; |
| 271 | 288 |
| 272 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 289 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
| OLD | NEW |