| 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 | |
| 21 // Per-WebContents class to handle updating the site engagement scores for | 20 // Per-WebContents class to handle updating the site engagement scores for |
| 22 // origins. | 21 // origins. |
| 23 class SiteEngagementHelper | 22 class SiteEngagementService::Helper |
| 24 : public content::WebContentsObserver, | 23 : public content::WebContentsObserver, |
| 25 public content::WebContentsUserData<SiteEngagementHelper> { | 24 public content::WebContentsUserData<SiteEngagementService::Helper> { |
| 26 public: | 25 public: |
| 27 ~SiteEngagementHelper() override; | 26 ~Helper() override; |
| 28 | 27 |
| 29 static void SetSecondsBetweenUserInputCheck(int seconds); | 28 static void SetSecondsBetweenUserInputCheck(int seconds); |
| 30 static void SetSecondsTrackingDelayAfterNavigation(int seconds); | 29 static void SetSecondsTrackingDelayAfterNavigation(int seconds); |
| 31 static void SetSecondsTrackingDelayAfterShow(int seconds); | 30 static void SetSecondsTrackingDelayAfterShow(int seconds); |
| 32 | 31 |
| 33 private: | 32 private: |
| 34 // Class to encapsulate the periodic detection of site engagement. | 33 // Class to encapsulate the periodic detection of site engagement. |
| 35 // | 34 // |
| 36 // Engagement detection begins at some constant time delta following | 35 // Engagement detection begins at some constant time delta following |
| 37 // navigation, tab activation, or media starting to play. Once engagement is | 36 // navigation, tab activation, or media starting to play. Once engagement is |
| 38 // recorded, detection is suspended for another constant time delta. For sites | 37 // recorded, detection is suspended for another constant time delta. For sites |
| 39 // to continually record engagement, this overall design requires: | 38 // to continually record engagement, this overall design requires: |
| 40 // | 39 // |
| 41 // 1. engagement at a non-trivial time after a site loads | 40 // 1. engagement at a non-trivial time after a site loads |
| 42 // 2. continual engagement over a non-trivial duration of time | 41 // 2. continual engagement over a non-trivial duration of time |
| 43 class PeriodicTracker { | 42 class PeriodicTracker { |
| 44 public: | 43 public: |
| 45 explicit PeriodicTracker(SiteEngagementHelper* helper); | 44 explicit PeriodicTracker(SiteEngagementService::Helper* helper); |
| 46 virtual ~PeriodicTracker(); | 45 virtual ~PeriodicTracker(); |
| 47 | 46 |
| 48 // Begin tracking after |initial_delay|. | 47 // Begin tracking after |initial_delay|. |
| 49 void Start(base::TimeDelta initial_delay); | 48 void Start(base::TimeDelta initial_delay); |
| 50 | 49 |
| 51 // Pause tracking and restart after a delay. | 50 // Pause tracking and restart after a delay. |
| 52 void Pause(); | 51 void Pause(); |
| 53 | 52 |
| 54 // Stop tracking. | 53 // Stop tracking. |
| 55 void Stop(); | 54 void Stop(); |
| 56 | 55 |
| 57 // Returns true if the timer is currently running. | 56 // Returns true if the timer is currently running. |
| 58 bool IsTimerRunning(); | 57 bool IsTimerRunning(); |
| 59 | 58 |
| 60 // Set the timer object for testing. | 59 // Set the timer object for testing. |
| 61 void SetPauseTimerForTesting(std::unique_ptr<base::Timer> timer); | 60 void SetPauseTimerForTesting(std::unique_ptr<base::Timer> timer); |
| 62 | 61 |
| 63 SiteEngagementHelper* helper() { return helper_; } | 62 SiteEngagementService::Helper* helper() { return helper_; } |
| 64 | 63 |
| 65 protected: | 64 protected: |
| 66 friend class SiteEngagementHelperTest; | 65 friend class SiteEngagementHelperTest; |
| 67 | 66 |
| 68 // Called when tracking is to be paused by |delay|. Used when tracking first | 67 // Called when tracking is to be paused by |delay|. Used when tracking first |
| 69 // starts or is paused. | 68 // starts or is paused. |
| 70 void StartTimer(base::TimeDelta delay); | 69 void StartTimer(base::TimeDelta delay); |
| 71 | 70 |
| 72 // Called when the timer expires and engagement tracking is activated. | 71 // Called when the timer expires and engagement tracking is activated. |
| 73 virtual void TrackingStarted() {} | 72 virtual void TrackingStarted() {} |
| 74 | 73 |
| 75 // Called when engagement tracking is paused or stopped. | 74 // Called when engagement tracking is paused or stopped. |
| 76 virtual void TrackingStopped() {} | 75 virtual void TrackingStopped() {} |
| 77 | 76 |
| 78 private: | 77 private: |
| 79 SiteEngagementHelper* helper_; | 78 SiteEngagementService::Helper* helper_; |
| 80 std::unique_ptr<base::Timer> pause_timer_; | 79 std::unique_ptr<base::Timer> pause_timer_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(PeriodicTracker); |
| 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 |
| 104 // Returns whether the tracker will respond to user input via | 105 // Returns whether the tracker will respond to user input via |
| 105 // DidGetUserInteraction. | 106 // DidGetUserInteraction. |
| 106 bool is_tracking_; | 107 bool is_tracking_; |
| 107 | 108 |
| 108 // content::WebContentsObserver overrides. | 109 // content::WebContentsObserver overrides. |
| 109 void DidGetUserInteraction(const blink::WebInputEvent::Type type) override; | 110 void DidGetUserInteraction(const blink::WebInputEvent::Type type) override; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(InputTracker); |
| 110 }; | 113 }; |
| 111 | 114 |
| 112 // Class to encapsulate media detection. Any media playing in a WebContents | 115 // Class to encapsulate media detection. Any media playing in a WebContents |
| 113 // (focused or not) will accumulate engagement points. Media in a hidden | 116 // (focused or not) will accumulate engagement points. Media in a hidden |
| 114 // WebContents will accumulate engagement more slowly than in an active | 117 // WebContents will accumulate engagement more slowly than in an active |
| 115 // WebContents. Media which has been muted will also accumulate engagement | 118 // WebContents. Media which has been muted will also accumulate engagement |
| 116 // more slowly. | 119 // more slowly. |
| 117 // | 120 // |
| 118 // When media begins playing in the main frame of a tab, the tracker is | 121 // 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 | 122 // triggered with an initial delay. It then wakes up every |
| 120 // |g_seconds_to_pause_engagement_detection| and notes the visible/hidden | 123 // |g_seconds_to_pause_engagement_detection| and notes the visible/hidden |
| 121 // state of the tab, as well as whether media is still playing. | 124 // state of the tab, as well as whether media is still playing. |
| 122 class MediaTracker : public PeriodicTracker, | 125 class MediaTracker : public PeriodicTracker, |
| 123 public content::WebContentsObserver { | 126 public content::WebContentsObserver { |
| 124 public: | 127 public: |
| 125 MediaTracker(SiteEngagementHelper* helper, | 128 MediaTracker(SiteEngagementService::Helper* helper, |
| 126 content::WebContents* web_contents); | 129 content::WebContents* web_contents); |
| 127 ~MediaTracker() override; | 130 ~MediaTracker() override; |
| 128 | 131 |
| 129 private: | 132 private: |
| 130 friend class SiteEngagementHelperTest; | 133 friend class SiteEngagementHelperTest; |
| 131 | 134 |
| 132 void TrackingStarted() override; | 135 void TrackingStarted() override; |
| 133 | 136 |
| 134 // content::WebContentsObserver overrides. | 137 // content::WebContentsObserver overrides. |
| 135 void MediaStartedPlaying(const MediaPlayerId& id) override; | 138 void MediaStartedPlaying(const MediaPlayerId& id) override; |
| 136 void MediaStoppedPlaying(const MediaPlayerId& id) override; | 139 void MediaStoppedPlaying(const MediaPlayerId& id) override; |
| 137 void WasShown() override; | 140 void WasShown() override; |
| 138 void WasHidden() override; | 141 void WasHidden() override; |
| 139 | 142 |
| 140 bool is_hidden_; | 143 bool is_hidden_; |
| 141 std::vector<MediaPlayerId> active_media_players_; | 144 std::vector<MediaPlayerId> active_media_players_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(MediaTracker); |
| 142 }; | 147 }; |
| 143 | 148 |
| 144 explicit SiteEngagementHelper(content::WebContents* web_contents); | 149 explicit Helper(content::WebContents* web_contents); |
| 145 friend class content::WebContentsUserData<SiteEngagementHelper>; | 150 friend class content::WebContentsUserData<SiteEngagementService::Helper>; |
| 146 friend class SiteEngagementHelperTest; | 151 friend class SiteEngagementHelperTest; |
| 147 | 152 |
| 148 // Ask the SiteEngagementService to record engagement via user input at the | 153 // Ask the SiteEngagementService to record engagement via user input at the |
| 149 // current WebContents URL. | 154 // current WebContents URL. |
| 150 void RecordUserInput(SiteEngagementMetrics::EngagementType type); | 155 void RecordUserInput(SiteEngagementMetrics::EngagementType type); |
| 151 | 156 |
| 152 // Ask the SiteEngagementService to record engagement via media playing at the | 157 // Ask the SiteEngagementService to record engagement via media playing at the |
| 153 // current WebContents URL. | 158 // current WebContents URL. |
| 154 void RecordMediaPlaying(bool is_hidden); | 159 void RecordMediaPlaying(bool is_hidden); |
| 155 | 160 |
| 156 // content::WebContentsObserver overrides. | 161 // content::WebContentsObserver overrides. |
| 157 void DidFinishNavigation(content::NavigationHandle* handle) override; | 162 void DidFinishNavigation(content::NavigationHandle* handle) override; |
| 158 void WasShown() override; | 163 void WasShown() override; |
| 159 void WasHidden() override; | 164 void WasHidden() override; |
| 160 | 165 |
| 161 InputTracker input_tracker_; | 166 InputTracker input_tracker_; |
| 162 MediaTracker media_tracker_; | 167 MediaTracker media_tracker_; |
| 163 bool record_engagement_; | 168 bool record_engagement_; |
| 164 | 169 |
| 165 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); | 170 DISALLOW_COPY_AND_ASSIGN(Helper); |
| 166 }; | 171 }; |
| 167 | 172 |
| 168 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 173 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
| OLD | NEW |