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