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. |
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 | |
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. | |
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 gSecondsBetweenUserInputCheck. This | |
53 // ensures that there is minimal overhead in input listening, and that input | |
54 // over an extended length of time is required to continually increase the | |
55 // 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 // gSecondsBetweenUserInputCheck seconds. | |
calamity
2015/09/24 03:50:53
nit: update var
dominickn
2015/09/24 06:52:07
Done.
| |
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 private: | |
84 SiteEngagementHelper* helper_; | |
85 scoped_ptr<base::Timer> timer_; | |
86 content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_; | |
87 content::RenderWidgetHost::MouseEventCallback mouse_event_callback_; | |
88 bool callbacks_added_; | |
89 }; | |
90 | |
27 explicit SiteEngagementHelper(content::WebContents* web_contents); | 91 explicit SiteEngagementHelper(content::WebContents* web_contents); |
28 friend class content::WebContentsUserData<SiteEngagementHelper>; | 92 friend class content::WebContentsUserData<SiteEngagementHelper>; |
93 friend class SiteEngagementServiceBrowserTest; | |
94 | |
95 // Ask the SiteEngagementService to record engagement via user input at the | |
96 // current contents location. | |
97 void RecordUserInput(); | |
98 | |
99 void SetTimerForTesting(scoped_ptr<base::Timer> timer); | |
100 | |
101 static void EnableCallbackRegistrationForTesting(); | |
102 static void DisableCallbackRegistrationForTesting(); | |
29 | 103 |
30 // content::WebContentsObserver overrides. | 104 // content::WebContentsObserver overrides. |
31 void DidStartNavigationToPendingEntry( | 105 void DidNavigateMainFrame( |
32 const GURL& url, | 106 const content::LoadCommittedDetails& details, |
33 content::NavigationController::ReloadType reload_type) override; | 107 const content::FrameNavigateParams& params) override; |
108 | |
109 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
110 content::RenderViewHost* new_host) override; | |
111 | |
112 void WasShown() override; | |
113 void WasHidden() override; | |
114 | |
115 scoped_ptr<InputTracker> tracker_; | |
116 base::ObserverList<Observer> observer_list_; | |
117 bool record_engagement_; | |
34 | 118 |
35 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); | 119 DISALLOW_COPY_AND_ASSIGN(SiteEngagementHelper); |
36 }; | 120 }; |
37 | 121 |
38 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ | 122 #endif // CHROME_BROWSER_ENGAGEMENT_SITE_ENGAGEMENT_HELPER_H_ |
OLD | NEW |