| 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 "build/build_config.h" |
| 18 #include "chrome/browser/engagement/site_engagement_metrics.h" | 18 #include "chrome/browser/engagement/site_engagement_metrics.h" |
| 19 #include "chrome/browser/engagement/site_engagement_observer.h" | 19 #include "chrome/browser/engagement/site_engagement_observer.h" |
| 20 #include "components/history/core/browser/history_service_observer.h" | 20 #include "components/history/core/browser/history_service_observer.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
| 22 #include "ui/base/page_transition_types.h" | 23 #include "ui/base/page_transition_types.h" |
| 23 | 24 |
| 24 namespace base { | 25 namespace base { |
| 25 class Clock; | 26 class Clock; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 class WebContents; | 30 class WebContents; |
| 30 } | 31 } |
| 31 | 32 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // threads using SiteEngagementService::GetScoreFromSettings, but use of this | 69 // threads using SiteEngagementService::GetScoreFromSettings, but use of this |
| 69 // method is discouraged unless it is not possible to use the UI thread. | 70 // method is discouraged unless it is not possible to use the UI thread. |
| 70 class SiteEngagementService : public KeyedService, | 71 class SiteEngagementService : public KeyedService, |
| 71 public history::HistoryServiceObserver, | 72 public history::HistoryServiceObserver, |
| 72 public SiteEngagementScoreProvider { | 73 public SiteEngagementScoreProvider { |
| 73 public: | 74 public: |
| 74 // WebContentsObserver that detects engagement triggering events and notifies | 75 // WebContentsObserver that detects engagement triggering events and notifies |
| 75 // the service of them. | 76 // the service of them. |
| 76 class Helper; | 77 class Helper; |
| 77 | 78 |
| 78 enum EngagementLevel { | |
| 79 ENGAGEMENT_LEVEL_NONE, | |
| 80 ENGAGEMENT_LEVEL_LOW, | |
| 81 ENGAGEMENT_LEVEL_MEDIUM, | |
| 82 ENGAGEMENT_LEVEL_HIGH, | |
| 83 ENGAGEMENT_LEVEL_MAX, | |
| 84 }; | |
| 85 | |
| 86 // The name of the site engagement variation field trial. | 79 // The name of the site engagement variation field trial. |
| 87 static const char kEngagementParams[]; | 80 static const char kEngagementParams[]; |
| 88 | 81 |
| 89 // Returns the site engagement service attached to this profile. The service | 82 // Returns the site engagement service attached to this profile. The service |
| 90 // exists in incognito mode; scores will be initialised using the score from | 83 // exists in incognito mode; scores will be initialised using the score from |
| 91 // the profile that the incognito session was created from, and will increase | 84 // the profile that the incognito session was created from, and will increase |
| 92 // and decrease as usual. Engagement earned or decayed in incognito will not | 85 // and decrease as usual. Engagement earned or decayed in incognito will not |
| 93 // be persisted or reflected in the original profile. | 86 // be persisted or reflected in the original profile. |
| 94 // | 87 // |
| 95 // This method must be called on the UI thread. | 88 // This method must be called on the UI thread. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 106 // | 99 // |
| 107 // Should only be used if you cannot create a SiteEngagementService (i.e. you | 100 // Should only be used if you cannot create a SiteEngagementService (i.e. you |
| 108 // cannot run on the UI thread). | 101 // cannot run on the UI thread). |
| 109 static double GetScoreFromSettings(HostContentSettingsMap* settings, | 102 static double GetScoreFromSettings(HostContentSettingsMap* settings, |
| 110 const GURL& origin); | 103 const GURL& origin); |
| 111 | 104 |
| 112 explicit SiteEngagementService(Profile* profile); | 105 explicit SiteEngagementService(Profile* profile); |
| 113 ~SiteEngagementService() override; | 106 ~SiteEngagementService() override; |
| 114 | 107 |
| 115 // Returns the engagement level of |url|. | 108 // Returns the engagement level of |url|. |
| 116 EngagementLevel GetEngagementLevel(const GURL& url) const; | 109 blink::mojom::EngagementLevel GetEngagementLevel(const GURL& url) const; |
| 117 | 110 |
| 118 // Returns a map of all stored origins and their engagement scores. | 111 // Returns a map of all stored origins and their engagement scores. |
| 119 std::map<GURL, double> GetScoreMap() const; | 112 std::map<GURL, double> GetScoreMap() const; |
| 120 | 113 |
| 121 // Returns whether the engagement service has enough data to make meaningful | 114 // Returns whether the engagement service has enough data to make meaningful |
| 122 // decisions. Clients should avoid using engagement in their heuristic until | 115 // decisions. Clients should avoid using engagement in their heuristic until |
| 123 // this is true. | 116 // this is true. |
| 124 bool IsBootstrapped() const; | 117 bool IsBootstrapped() const; |
| 125 | 118 |
| 126 // Returns whether |url| has at least the given |level| of engagement. | 119 // Returns whether |url| has at least the given |level| of engagement. |
| 127 bool IsEngagementAtLeast(const GURL& url, EngagementLevel level) const; | 120 bool IsEngagementAtLeast(const GURL& url, |
| 121 blink::mojom::EngagementLevel level) const; |
| 128 | 122 |
| 129 // Resets the engagement score |url| to |score|, clearing daily limits. | 123 // Resets the engagement score |url| to |score|, clearing daily limits. |
| 130 void ResetScoreForURL(const GURL& url, double score); | 124 void ResetScoreForURL(const GURL& url, double score); |
| 131 | 125 |
| 132 // Update the last time |url| was opened from an installed shortcut to be | 126 // Update the last time |url| was opened from an installed shortcut to be |
| 133 // clock_->Now(). | 127 // clock_->Now(). |
| 134 void SetLastShortcutLaunchTime(const GURL& url); | 128 void SetLastShortcutLaunchTime(const GURL& url); |
| 135 | 129 |
| 130 void HelperCreated(SiteEngagementService::Helper* helper); |
| 131 void HelperDeleted(SiteEngagementService::Helper* helper); |
| 132 |
| 136 // Overridden from SiteEngagementScoreProvider. | 133 // Overridden from SiteEngagementScoreProvider. |
| 137 double GetScore(const GURL& url) const override; | 134 double GetScore(const GURL& url) const override; |
| 138 double GetTotalEngagementPoints() const override; | 135 double GetTotalEngagementPoints() const override; |
| 139 | 136 |
| 140 private: | 137 private: |
| 141 friend class SiteEngagementObserver; | 138 friend class SiteEngagementObserver; |
| 142 friend class SiteEngagementServiceAndroid; | 139 friend class SiteEngagementServiceAndroid; |
| 143 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); | 140 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); |
| 144 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); | 141 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); |
| 145 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, | 142 FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 // Update the engagement score of the origin loaded in |web_contents| for | 221 // Update the engagement score of the origin loaded in |web_contents| for |
| 225 // navigation. | 222 // navigation. |
| 226 void HandleNavigation(content::WebContents* web_contents, | 223 void HandleNavigation(content::WebContents* web_contents, |
| 227 ui::PageTransition transition); | 224 ui::PageTransition transition); |
| 228 | 225 |
| 229 // Update the engagement score of the origin loaded in |web_contents| for | 226 // Update the engagement score of the origin loaded in |web_contents| for |
| 230 // time-on-site, based on user input. | 227 // time-on-site, based on user input. |
| 231 void HandleUserInput(content::WebContents* web_contents, | 228 void HandleUserInput(content::WebContents* web_contents, |
| 232 SiteEngagementMetrics::EngagementType type); | 229 SiteEngagementMetrics::EngagementType type); |
| 233 | 230 |
| 231 // Called if |url| changes to |level| engagement, and informs every Helper of |
| 232 // the change. |
| 233 void SendLevelChangeToHelpers(const GURL& url, |
| 234 blink::mojom::EngagementLevel level); |
| 235 |
| 234 // Returns true if the last engagement increasing event seen by the site | 236 // Returns true if the last engagement increasing event seen by the site |
| 235 // engagement service was sufficiently long ago that we need to reset all | 237 // engagement service was sufficiently long ago that we need to reset all |
| 236 // scores to be relative to now. This ensures that users who do not use the | 238 // scores to be relative to now. This ensures that users who do not use the |
| 237 // browser for an extended period of time do not have their engagement decay. | 239 // browser for an extended period of time do not have their engagement decay. |
| 238 bool IsLastEngagementStale() const; | 240 bool IsLastEngagementStale() const; |
| 239 | 241 |
| 240 // Overridden from history::HistoryServiceObserver: | 242 // Overridden from history::HistoryServiceObserver: |
| 241 void OnURLsDeleted(history::HistoryService* history_service, | 243 void OnURLsDeleted(history::HistoryService* history_service, |
| 242 bool all_history, | 244 bool all_history, |
| 243 bool expired, | 245 bool expired, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 270 std::unique_ptr<SiteEngagementServiceAndroid> android_service_; | 272 std::unique_ptr<SiteEngagementServiceAndroid> android_service_; |
| 271 #endif | 273 #endif |
| 272 | 274 |
| 273 // Metrics are recorded at non-incognito browser startup, and then | 275 // Metrics are recorded at non-incognito browser startup, and then |
| 274 // approximately once per hour thereafter. Store the local time at which | 276 // approximately once per hour thereafter. Store the local time at which |
| 275 // metrics were previously uploaded: the first event which affects any | 277 // metrics were previously uploaded: the first event which affects any |
| 276 // origin's engagement score after an hour has elapsed triggers the next | 278 // origin's engagement score after an hour has elapsed triggers the next |
| 277 // upload. | 279 // upload. |
| 278 base::Time last_metrics_time_; | 280 base::Time last_metrics_time_; |
| 279 | 281 |
| 282 // All helpers currently attached to a WebContents. |
| 283 std::set<SiteEngagementService::Helper*> helpers_; |
| 284 |
| 280 // A list of observers. When any origin registers an engagement-increasing | 285 // A list of observers. When any origin registers an engagement-increasing |
| 281 // event, each observer's OnEngagementIncreased method will be called. | 286 // event, each observer's OnEngagementIncreased method will be called. |
| 282 base::ObserverList<SiteEngagementObserver> observer_list_; | 287 base::ObserverList<SiteEngagementObserver> observer_list_; |
| 283 | 288 |
| 284 base::WeakPtrFactory<SiteEngagementService> weak_factory_; | 289 base::WeakPtrFactory<SiteEngagementService> weak_factory_; |
| 285 | 290 |
| 286 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); | 291 DISALLOW_COPY_AND_ASSIGN(SiteEngagementService); |
| 287 }; | 292 }; |
| 288 | 293 |
| 289 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ | 294 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_SERVICE_H_ |
| OLD | NEW |