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/observer_list.h" | |
10 #include "base/timer/timer.h" | |
11 #include "content/public/browser/render_view_host.h" | |
9 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
10 #include "content/public/browser/web_contents_user_data.h" | 13 #include "content/public/browser/web_contents_user_data.h" |
11 | 14 |
12 namespace content { | 15 namespace content { |
13 class WebContents; | 16 class WebContents; |
14 } | 17 } |
15 | 18 |
16 class GURL; | 19 class GURL; |
17 | 20 |
18 // Per-WebContents class to handle updating the site engagement scores for | 21 // Per-WebContents class to handle updating the site engagement scores for |
19 // origins as the user navigates. | 22 // origins based on the time spent on site. |
benwells
2015/09/29 00:24:12
Nit: this comment is too restrictive.
dominickn
2015/09/29 01:40:36
Done.
| |
23 // | |
24 // Time on site is recorded by detecting any user input (mouse or keypress) per | |
25 // some discrete time unit. If there is user input, then record a positive site | |
benwells
2015/09/29 00:24:12
Nit: move this into the InputTracker class level c
dominickn
2015/09/29 01:40:36
Done.
| |
26 // engagement. | |
27 // | |
28 // TODO(dominickn): account for use cases where user input is not a good proxy | |
29 // for time on site: e.g. watching videos. | |
benwells
2015/09/29 00:24:12
Nit: I'd remove this TODO as we don't have clear i
dominickn
2015/09/29 01:40:36
Done.
| |
20 class SiteEngagementHelper | 30 class SiteEngagementHelper |
21 : public content::WebContentsObserver, | 31 : public content::WebContentsObserver, |
22 public content::WebContentsUserData<SiteEngagementHelper> { | 32 public content::WebContentsUserData<SiteEngagementHelper> { |
23 public: | 33 public: |
34 class Observer { | |
35 public: | |
36 // Called once user input is recorded. | |
37 virtual void OnInputRecorded(SiteEngagementHelper* helper) = 0; | |
38 }; | |
39 | |
24 ~SiteEngagementHelper() override; | 40 ~SiteEngagementHelper() override; |
25 | 41 |
42 void AddObserverForTesting(Observer* observer); | |
43 void RemoveObserverForTesting(Observer* observer); | |
44 | |
45 static void SetSecondsBetweenUserInputCheck(double seconds); | |
46 | |
26 private: | 47 private: |
48 // Class to encapsulate the user input listening. | |
49 // | |
50 // User input is used to record time-on-site. When input is detected, | |
51 // SiteEngagementHelper::RecordUserInput is called, and the input detection | |
52 // callbacks are paused for a duration of g_seconds_between_user_input_check. | |
53 // This ensures that there is minimal overhead in input listening, and that | |
54 // input over an extended length of time is required to continually increase | |
55 // the engagement score. | |
56 class InputTracker { | |
57 public: | |
58 explicit InputTracker(SiteEngagementHelper* helper); | |
59 ~InputTracker(); | |
60 | |
61 // Callback to handle key press events from the RenderViewHost. | |
62 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event); | |
63 | |
64 // Callback to handle mouse events from the RenderViewHost. | |
65 bool HandleMouseEvent(const blink::WebMouseEvent& event); | |
66 | |
67 // Register callbacks to listen for user input. | |
68 void StartTracking(content::RenderViewHost* host); | |
69 | |
70 // Pause listening for user input, restarting listening after | |
71 // g_seconds_between_user_input_check seconds. | |
72 void PauseTracking(content::RenderViewHost* host); | |
73 | |
74 // Restart listening for user input. | |
75 void ResumeTracking(); | |
76 | |
77 // Stop listening for user input. | |
78 void StopTracking(content::RenderViewHost* host); | |
79 | |
80 // Set the timer object for testing purposes. | |
81 void SetTimerForTesting(scoped_ptr<base::Timer> timer); | |
82 | |
83 bool callbacks_added() { return callbacks_added_; } | |
84 | |
85 private: | |
86 SiteEngagementHelper* helper_; | |
87 scoped_ptr<base::Timer> pause_timer_; | |
88 content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_; | |
89 content::RenderWidgetHost::MouseEventCallback mouse_event_callback_; | |
90 bool callbacks_added_; | |
91 }; | |
92 | |
27 explicit SiteEngagementHelper(content::WebContents* web_contents); | 93 explicit SiteEngagementHelper(content::WebContents* web_contents); |
28 friend class content::WebContentsUserData<SiteEngagementHelper>; | 94 friend class content::WebContentsUserData<SiteEngagementHelper>; |
95 friend class SiteEngagementServiceBrowserTest; | |
96 | |
97 // Ask the SiteEngagementService to record engagement via user input at the | |
98 // current contents location. | |
99 void RecordUserInput(); | |
100 | |
101 void SetTimerForTesting(scoped_ptr<base::Timer> timer); | |
102 | |
103 static void EnableCallbackRegistrationForTesting(); | |
104 static void DisableCallbackRegistrationForTesting(); | |
29 | 105 |
30 // content::WebContentsObserver overrides. | 106 // content::WebContentsObserver overrides. |
31 void DidStartNavigationToPendingEntry( | 107 void DidNavigateMainFrame( |
32 const GURL& url, | 108 const content::LoadCommittedDetails& details, |
33 content::NavigationController::ReloadType reload_type) override; | 109 const content::FrameNavigateParams& params) override; |
110 | |
111 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
112 content::RenderViewHost* new_host) override; | |
113 | |
114 void WasShown() override; | |
115 void WasHidden() override; | |
116 | |
117 scoped_ptr<InputTracker> input_tracker_; | |
benwells
2015/09/29 00:24:12
Is there any need for this to be a scoped_ptr, ver
dominickn
2015/09/29 01:40:36
Done.
| |
118 base::ObserverList<Observer> observer_list_; | |
119 bool record_engagement_; | |
34 | 120 |
35 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); | 121 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); |
36 }; | 122 }; |
37 | 123 |
38 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 124 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
OLD | NEW |