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