| 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 "content/public/browser/render_view_host.h" | |
| 12 #include "content/public/browser/render_widget_host.h" | |
| 13 #include "content/public/browser/web_contents_observer.h" | 11 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/browser/web_contents_user_data.h" | 12 #include "content/public/browser/web_contents_user_data.h" |
| 15 | 13 |
| 16 namespace content { | 14 namespace content { |
| 17 class WebContents; | 15 class WebContents; |
| 18 } | 16 } |
| 19 | 17 |
| 20 class GURL; | 18 class GURL; |
| 21 | 19 |
| 22 // Per-WebContents class to handle updating the site engagement scores for | 20 // Per-WebContents class to handle updating the site engagement scores for |
| 23 // origins. | 21 // origins. |
| 24 class SiteEngagementHelper | 22 class SiteEngagementHelper |
| 25 : public content::WebContentsObserver, | 23 : public content::WebContentsObserver, |
| 26 public content::WebContentsUserData<SiteEngagementHelper> { | 24 public content::WebContentsUserData<SiteEngagementHelper> { |
| 27 public: | 25 public: |
| 28 ~SiteEngagementHelper() override; | 26 ~SiteEngagementHelper() override; |
| 29 | 27 |
| 30 static void SetSecondsBetweenUserInputCheck(int seconds); | 28 static void SetSecondsBetweenUserInputCheck(int seconds); |
| 31 static void SetSecondsTrackingDelayAfterNavigation(int seconds); | 29 static void SetSecondsTrackingDelayAfterNavigation(int seconds); |
| 32 static void SetSecondsTrackingDelayAfterShow(int seconds); | 30 static void SetSecondsTrackingDelayAfterShow(int seconds); |
| 33 | 31 |
| 34 // content::WebContentsObserver overrides. | 32 // content::WebContentsObserver overrides. |
| 35 void DidNavigateMainFrame( | 33 void DidNavigateMainFrame( |
| 36 const content::LoadCommittedDetails& details, | 34 const content::LoadCommittedDetails& details, |
| 37 const content::FrameNavigateParams& params) override; | 35 const content::FrameNavigateParams& params) override; |
| 38 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 39 content::RenderViewHost* new_host) override; | |
| 40 void WasShown() override; | 36 void WasShown() override; |
| 41 void WasHidden() override; | 37 void WasHidden() override; |
| 42 | 38 |
| 43 private: | 39 private: |
| 44 // Class to encapsulate the user input listening. | 40 // Class to encapsulate the user input listening. |
| 45 // | 41 // |
| 46 // Time on site is recorded by detecting any user input (mouse or keypress) | 42 // Time on site is recorded by detecting any user input (mouse click, |
| 47 // per some discrete time unit. If there is user input, then record a positive | 43 // keypress, or touch gesture tap) per some discrete time unit. If there is |
| 48 // site engagement. | 44 // user input, then record a positive site engagement. |
| 49 // | 45 // |
| 50 // When input is detected, SiteEngagementHelper::RecordUserInput is called, | 46 // When input is detected, SiteEngagementHelper::RecordUserInput is called, |
| 51 // and the input detection callbacks are paused for | 47 // and the input detection callbacks are paused for |
| 52 // g_seconds_between_user_input_check. This ensures that there is minimal | 48 // g_seconds_between_user_input_check. This ensures that there is minimal |
| 53 // overhead in input listening, and that input over an extended length of time | 49 // overhead in input listening, and that input over an extended length of time |
| 54 // is required to continually increase the engagement score. | 50 // is required to continually increase the engagement score. |
| 55 class InputTracker { | 51 class InputTracker : public content::WebContentsObserver { |
| 56 public: | 52 public: |
| 57 explicit InputTracker(SiteEngagementHelper* helper); | 53 explicit InputTracker(content::WebContents* web_contents, |
| 58 ~InputTracker(); | 54 SiteEngagementHelper* helper); |
| 59 | 55 ~InputTracker() override; |
| 60 // Callback to handle key press events from the RenderViewHost. | |
| 61 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event); | |
| 62 | |
| 63 // Callback to handle mouse events from the RenderViewHost. | |
| 64 bool HandleMouseEvent(const blink::WebMouseEvent& event); | |
| 65 | 56 |
| 66 // Begin tracking input after |initial_delay|. | 57 // Begin tracking input after |initial_delay|. |
| 67 void Start(content::RenderViewHost* host, base::TimeDelta initial_delay); | 58 void Start(base::TimeDelta initial_delay); |
| 68 | 59 |
| 69 // Pause listening for user input, restarting listening after a delay. | 60 // Pause listening for user input, restarting listening after a delay. |
| 70 void Pause(); | 61 void Pause(); |
| 71 | 62 |
| 72 // Switches the InputTracker to another RenderViewHost, respecting the pause | |
| 73 // timer state. | |
| 74 void SwitchRenderViewHost(content::RenderViewHost* old_host, | |
| 75 content::RenderViewHost* new_host); | |
| 76 | |
| 77 // Stop listening for user input. | 63 // Stop listening for user input. |
| 78 void Stop(); | 64 void Stop(); |
| 79 | 65 |
| 80 // Returns whether the InputTracker has been started for a RenderViewHost. | 66 // Returns whether the tracker will respond to user input via |
| 81 bool IsActive() const; | 67 // DidGetUserInteraction. |
| 82 | |
| 83 // Returns whether input tracking callbacks have been added to | |
| 84 // RenderViewHost. | |
| 85 bool is_tracking() const { return is_tracking_; } | 68 bool is_tracking() const { return is_tracking_; } |
| 86 | 69 |
| 87 content::RenderViewHost* host() const { return host_; } | |
| 88 | |
| 89 // Set the timer object for testing purposes. | 70 // Set the timer object for testing purposes. |
| 90 void SetPauseTimerForTesting(scoped_ptr<base::Timer> timer); | 71 void SetPauseTimerForTesting(scoped_ptr<base::Timer> timer); |
| 91 | 72 |
| 92 private: | 73 private: |
| 93 // Starts the timer for adding callbacks to the RenderViewHost. | 74 friend class SiteEngagementHelperTest; |
| 75 |
| 76 // Starts the timer for detecting user interaction. |
| 94 void StartTimer(base::TimeDelta delay); | 77 void StartTimer(base::TimeDelta delay); |
| 95 | 78 |
| 96 // Adds/removes tracking callbacks to the RenderViewHost. | 79 // Callback for StartTimer that activates the user input tracking. |
| 97 void AddCallbacks(); | 80 void StartTracking(); |
| 98 void RemoveCallbacks(); | 81 |
| 82 // content::WebContentsObserver overrides. |
| 83 void DidGetUserInteraction(const blink::WebInputEvent::Type type) override; |
| 99 | 84 |
| 100 SiteEngagementHelper* helper_; | 85 SiteEngagementHelper* helper_; |
| 101 scoped_ptr<base::Timer> pause_timer_; | 86 scoped_ptr<base::Timer> pause_timer_; |
| 102 content::RenderViewHost* host_; | |
| 103 content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_; | |
| 104 content::RenderWidgetHost::MouseEventCallback mouse_event_callback_; | |
| 105 bool is_tracking_; | 87 bool is_tracking_; |
| 106 }; | 88 }; |
| 107 | 89 |
| 108 explicit SiteEngagementHelper(content::WebContents* web_contents); | 90 explicit SiteEngagementHelper(content::WebContents* web_contents); |
| 109 friend class content::WebContentsUserData<SiteEngagementHelper>; | 91 friend class content::WebContentsUserData<SiteEngagementHelper>; |
| 110 friend class SiteEngagementServiceBrowserTest; | 92 friend class SiteEngagementHelperTest; |
| 111 | 93 |
| 112 // Ask the SiteEngagementService to record engagement via user input at the | 94 // Ask the SiteEngagementService to record engagement via user input at the |
| 113 // current contents location. | 95 // current contents location. |
| 114 void RecordUserInput(SiteEngagementMetrics::EngagementType type); | 96 void RecordUserInput(SiteEngagementMetrics::EngagementType type); |
| 115 | 97 |
| 116 InputTracker input_tracker_; | 98 InputTracker input_tracker_; |
| 117 bool record_engagement_; | 99 bool record_engagement_; |
| 118 | 100 |
| 119 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); | 101 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); |
| 120 }; | 102 }; |
| 121 | 103 |
| 122 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 104 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
| OLD | NEW |