| 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_HELPER_H_ | 5 #ifndef CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
| 6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 6 #define CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/timer/timer.h" | 9 #include "base/timer/timer.h" |
| 10 #include "chrome/browser/engagement/site_engagement_metrics.h" | 10 #include "chrome/browser/engagement/site_engagement_metrics.h" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" |
| 11 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "content/public/browser/web_contents_user_data.h" | 13 #include "content/public/browser/web_contents_user_data.h" |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 class NavigationHandle; | 16 class NavigationHandle; |
| 16 class WebContents; | 17 class WebContents; |
| 17 } | 18 } |
| 18 | 19 |
| 19 class GURL; | 20 class GURL; |
| 20 | 21 |
| 21 // Per-WebContents class to handle updating the site engagement scores for | 22 // Per-WebContents class to handle updating the site engagement scores for |
| 22 // origins. | 23 // origins. |
| 23 class SiteEngagementHelper | 24 class SiteEngagementService::Helper |
| 24 : public content::WebContentsObserver, | 25 : public content::WebContentsObserver, |
| 25 public content::WebContentsUserData<SiteEngagementHelper> { | 26 public content::WebContentsUserData<SiteEngagementService::Helper> { |
| 26 public: | 27 public: |
| 27 ~SiteEngagementHelper() override; | 28 ~Helper() override; |
| 28 | 29 |
| 29 static void SetSecondsBetweenUserInputCheck(int seconds); | 30 static void SetSecondsBetweenUserInputCheck(int seconds); |
| 30 static void SetSecondsTrackingDelayAfterNavigation(int seconds); | 31 static void SetSecondsTrackingDelayAfterNavigation(int seconds); |
| 31 static void SetSecondsTrackingDelayAfterShow(int seconds); | 32 static void SetSecondsTrackingDelayAfterShow(int seconds); |
| 32 | 33 |
| 33 private: | 34 private: |
| 34 // Class to encapsulate the periodic detection of site engagement. | 35 // Class to encapsulate the periodic detection of site engagement. |
| 35 // | 36 // |
| 36 // Engagement detection begins at some constant time delta following | 37 // Engagement detection begins at some constant time delta following |
| 37 // navigation, tab activation, or media starting to play. Once engagement is | 38 // navigation, tab activation, or media starting to play. Once engagement is |
| 38 // recorded, detection is suspended for another constant time delta. For sites | 39 // recorded, detection is suspended for another constant time delta. For sites |
| 39 // to continually record engagement, this overall design requires: | 40 // to continually record engagement, this overall design requires: |
| 40 // | 41 // |
| 41 // 1. engagement at a non-trivial time after a site loads | 42 // 1. engagement at a non-trivial time after a site loads |
| 42 // 2. continual engagement over a non-trivial duration of time | 43 // 2. continual engagement over a non-trivial duration of time |
| 43 class PeriodicTracker { | 44 class PeriodicTracker { |
| 44 public: | 45 public: |
| 45 explicit PeriodicTracker(SiteEngagementHelper* helper); | 46 explicit PeriodicTracker(SiteEngagementService::Helper* helper); |
| 46 virtual ~PeriodicTracker(); | 47 virtual ~PeriodicTracker(); |
| 47 | 48 |
| 48 // Begin tracking after |initial_delay|. | 49 // Begin tracking after |initial_delay|. |
| 49 void Start(base::TimeDelta initial_delay); | 50 void Start(base::TimeDelta initial_delay); |
| 50 | 51 |
| 51 // Pause tracking and restart after a delay. | 52 // Pause tracking and restart after a delay. |
| 52 void Pause(); | 53 void Pause(); |
| 53 | 54 |
| 54 // Stop tracking. | 55 // Stop tracking. |
| 55 void Stop(); | 56 void Stop(); |
| 56 | 57 |
| 57 // Returns true if the timer is currently running. | 58 // Returns true if the timer is currently running. |
| 58 bool IsTimerRunning(); | 59 bool IsTimerRunning(); |
| 59 | 60 |
| 60 // Set the timer object for testing. | 61 // Set the timer object for testing. |
| 61 void SetPauseTimerForTesting(std::unique_ptr<base::Timer> timer); | 62 void SetPauseTimerForTesting(std::unique_ptr<base::Timer> timer); |
| 62 | 63 |
| 63 SiteEngagementHelper* helper() { return helper_; } | 64 SiteEngagementService::Helper* helper() { return helper_; } |
| 64 | 65 |
| 65 protected: | 66 protected: |
| 66 friend class SiteEngagementHelperTest; | 67 friend class SiteEngagementHelperTest; |
| 67 | 68 |
| 68 // Called when tracking is to be paused by |delay|. Used when tracking first | 69 // Called when tracking is to be paused by |delay|. Used when tracking first |
| 69 // starts or is paused. | 70 // starts or is paused. |
| 70 void StartTimer(base::TimeDelta delay); | 71 void StartTimer(base::TimeDelta delay); |
| 71 | 72 |
| 72 // Called when the timer expires and engagement tracking is activated. | 73 // Called when the timer expires and engagement tracking is activated. |
| 73 virtual void TrackingStarted() {} | 74 virtual void TrackingStarted() {} |
| 74 | 75 |
| 75 // Called when engagement tracking is paused or stopped. | 76 // Called when engagement tracking is paused or stopped. |
| 76 virtual void TrackingStopped() {} | 77 virtual void TrackingStopped() {} |
| 77 | 78 |
| 78 private: | 79 private: |
| 79 SiteEngagementHelper* helper_; | 80 SiteEngagementService::Helper* helper_; |
| 80 std::unique_ptr<base::Timer> pause_timer_; | 81 std::unique_ptr<base::Timer> pause_timer_; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 // Class to encapsulate time-on-site engagement detection. Time-on-site is | 84 // Class to encapsulate time-on-site engagement detection. Time-on-site is |
| 84 // recorded by detecting user input on a focused WebContents (mouse click, | 85 // recorded by detecting user input on a focused WebContents (mouse click, |
| 85 // mouse wheel, keypress, or touch gesture tap) over time. | 86 // mouse wheel, keypress, or touch gesture tap) over time. |
| 86 // | 87 // |
| 87 // After an initial delay, the input tracker begins listening to | 88 // After an initial delay, the input tracker begins listening to |
| 88 // DidGetUserInteraction. When user input is signaled, site engagement is | 89 // DidGetUserInteraction. When user input is signaled, site engagement is |
| 89 // recorded, and the tracker sleeps for a delay period. | 90 // recorded, and the tracker sleeps for a delay period. |
| 90 class InputTracker : public PeriodicTracker, | 91 class InputTracker : public PeriodicTracker, |
| 91 public content::WebContentsObserver { | 92 public content::WebContentsObserver { |
| 92 public: | 93 public: |
| 93 InputTracker(SiteEngagementHelper* helper, | 94 InputTracker(SiteEngagementService::Helper* helper, |
| 94 content::WebContents* web_contents); | 95 content::WebContents* web_contents); |
| 95 | 96 |
| 96 bool is_tracking() const { return is_tracking_; } | 97 bool is_tracking() const { return is_tracking_; } |
| 97 | 98 |
| 98 private: | 99 private: |
| 99 friend class SiteEngagementHelperTest; | 100 friend class SiteEngagementHelperTest; |
| 100 | 101 |
| 101 void TrackingStarted() override; | 102 void TrackingStarted() override; |
| 102 void TrackingStopped() override; | 103 void TrackingStopped() override; |
| 103 | 104 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 115 // WebContents. Media which has been muted will also accumulate engagement | 116 // WebContents. Media which has been muted will also accumulate engagement |
| 116 // more slowly. | 117 // more slowly. |
| 117 // | 118 // |
| 118 // When media begins playing in the main frame of a tab, the tracker is | 119 // When media begins playing in the main frame of a tab, the tracker is |
| 119 // triggered with an initial delay. It then wakes up every | 120 // triggered with an initial delay. It then wakes up every |
| 120 // |g_seconds_to_pause_engagement_detection| and notes the visible/hidden | 121 // |g_seconds_to_pause_engagement_detection| and notes the visible/hidden |
| 121 // state of the tab, as well as whether media is still playing. | 122 // state of the tab, as well as whether media is still playing. |
| 122 class MediaTracker : public PeriodicTracker, | 123 class MediaTracker : public PeriodicTracker, |
| 123 public content::WebContentsObserver { | 124 public content::WebContentsObserver { |
| 124 public: | 125 public: |
| 125 MediaTracker(SiteEngagementHelper* helper, | 126 MediaTracker(SiteEngagementService::Helper* helper, |
| 126 content::WebContents* web_contents); | 127 content::WebContents* web_contents); |
| 127 ~MediaTracker() override; | 128 ~MediaTracker() override; |
| 128 | 129 |
| 129 private: | 130 private: |
| 130 friend class SiteEngagementHelperTest; | 131 friend class SiteEngagementHelperTest; |
| 131 | 132 |
| 132 void TrackingStarted() override; | 133 void TrackingStarted() override; |
| 133 | 134 |
| 134 // content::WebContentsObserver overrides. | 135 // content::WebContentsObserver overrides. |
| 135 void MediaStartedPlaying(const MediaPlayerId& id) override; | 136 void MediaStartedPlaying(const MediaPlayerId& id) override; |
| 136 void MediaStoppedPlaying(const MediaPlayerId& id) override; | 137 void MediaStoppedPlaying(const MediaPlayerId& id) override; |
| 137 void WasShown() override; | 138 void WasShown() override; |
| 138 void WasHidden() override; | 139 void WasHidden() override; |
| 139 | 140 |
| 140 bool is_hidden_; | 141 bool is_hidden_; |
| 141 std::vector<MediaPlayerId> active_media_players_; | 142 std::vector<MediaPlayerId> active_media_players_; |
| 142 }; | 143 }; |
| 143 | 144 |
| 144 explicit SiteEngagementHelper(content::WebContents* web_contents); | 145 explicit Helper(content::WebContents* web_contents); |
| 145 friend class content::WebContentsUserData<SiteEngagementHelper>; | 146 friend class content::WebContentsUserData<SiteEngagementService::Helper>; |
| 146 friend class SiteEngagementHelperTest; | 147 friend class SiteEngagementHelperTest; |
| 147 | 148 |
| 148 // Ask the SiteEngagementService to record engagement via user input at the | 149 // Ask the SiteEngagementService to record engagement via user input at the |
| 149 // current WebContents URL. | 150 // current WebContents URL. |
| 150 void RecordUserInput(SiteEngagementMetrics::EngagementType type); | 151 void RecordUserInput(SiteEngagementMetrics::EngagementType type); |
| 151 | 152 |
| 152 // Ask the SiteEngagementService to record engagement via media playing at the | 153 // Ask the SiteEngagementService to record engagement via media playing at the |
| 153 // current WebContents URL. | 154 // current WebContents URL. |
| 154 void RecordMediaPlaying(bool is_hidden); | 155 void RecordMediaPlaying(bool is_hidden); |
| 155 | 156 |
| 156 // content::WebContentsObserver overrides. | 157 // content::WebContentsObserver overrides. |
| 157 void DidFinishNavigation(content::NavigationHandle* handle) override; | 158 void DidFinishNavigation(content::NavigationHandle* handle) override; |
| 158 void WasShown() override; | 159 void WasShown() override; |
| 159 void WasHidden() override; | 160 void WasHidden() override; |
| 160 | 161 |
| 161 InputTracker input_tracker_; | 162 InputTracker input_tracker_; |
| 162 MediaTracker media_tracker_; | 163 MediaTracker media_tracker_; |
| 163 bool record_engagement_; | 164 bool record_engagement_; |
| 164 | 165 |
| 165 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); | 166 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 166 }; | 167 }; |
| 167 | 168 |
| 168 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 169 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
| OLD | NEW |