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