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