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 #include "chrome/browser/engagement/site_engagement_helper.h" | 5 #include "chrome/browser/engagement/site_engagement_helper.h" |
6 | 6 |
7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
9 #include "chrome/browser/engagement/site_engagement_service.h" | 9 #include "chrome/browser/engagement/site_engagement_service.h" |
10 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 10 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
11 #include "chrome/browser/prerender/prerender_contents.h" | 11 #include "chrome/browser/prerender/prerender_contents.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 int g_seconds_between_user_input_check = 10; | 18 int g_seconds_between_user_input_check = 10; |
19 int g_seconds_tracking_delay_after_navigation = 10; | 19 int g_seconds_tracking_delay_after_navigation = 10; |
20 int g_seconds_tracking_delay_after_show = 5; | 20 int g_seconds_tracking_delay_after_show = 5; |
21 | 21 |
22 } // anonymous namespace | 22 } // anonymous namespace |
23 | 23 |
24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); | 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); |
25 | 25 |
26 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) | 26 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) |
27 : helper_(helper), | 27 : helper_(helper), |
28 pause_timer_(new base::Timer(true, false)), | 28 pause_timer_(new base::Timer(true, false)), |
29 host_(nullptr), | 29 host_(nullptr), |
30 is_active_(false), | |
31 is_tracking_(false) { | 30 is_tracking_(false) { |
32 key_press_event_callback_ = | 31 key_press_event_callback_ = |
33 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent, | 32 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent, |
34 base::Unretained(this)); | 33 base::Unretained(this)); |
35 mouse_event_callback_ = | 34 mouse_event_callback_ = |
36 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent, | 35 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent, |
37 base::Unretained(this)); | 36 base::Unretained(this)); |
38 } | 37 } |
39 | 38 |
40 SiteEngagementHelper::InputTracker::~InputTracker() {} | 39 SiteEngagementHelper::InputTracker::~InputTracker() {} |
(...skipping 23 matching lines...) Expand all Loading... |
64 event.type == blink::WebInputEvent::MouseDown) || | 63 event.type == blink::WebInputEvent::MouseDown) || |
65 event.type == blink::WebInputEvent::MouseWheel) { | 64 event.type == blink::WebInputEvent::MouseWheel) { |
66 Pause(); | 65 Pause(); |
67 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_MOUSE); | 66 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_MOUSE); |
68 } | 67 } |
69 return false; | 68 return false; |
70 } | 69 } |
71 | 70 |
72 void SiteEngagementHelper::InputTracker::Start(content::RenderViewHost* host, | 71 void SiteEngagementHelper::InputTracker::Start(content::RenderViewHost* host, |
73 base::TimeDelta initial_delay) { | 72 base::TimeDelta initial_delay) { |
74 DCHECK(!is_active_); | 73 DCHECK(!host_); |
75 DCHECK(host); | 74 DCHECK(host); |
76 host_ = host; | 75 host_ = host; |
77 StartTimer(initial_delay); | 76 StartTimer(initial_delay); |
78 is_active_ = true; | |
79 } | 77 } |
80 | 78 |
81 void SiteEngagementHelper::InputTracker::Pause() { | 79 void SiteEngagementHelper::InputTracker::Pause() { |
82 RemoveCallbacks(); | 80 RemoveCallbacks(); |
83 StartTimer(base::TimeDelta::FromSeconds(g_seconds_between_user_input_check)); | 81 StartTimer(base::TimeDelta::FromSeconds(g_seconds_between_user_input_check)); |
84 } | 82 } |
85 | 83 |
86 void SiteEngagementHelper::InputTracker::SwitchRenderViewHost( | 84 void SiteEngagementHelper::InputTracker::SwitchRenderViewHost( |
87 content::RenderViewHost* old_host, | 85 content::RenderViewHost* old_host, |
88 content::RenderViewHost* new_host) { | 86 content::RenderViewHost* new_host) { |
89 DCHECK(is_tracking_); | 87 DCHECK(host_); |
90 DCHECK(new_host); | 88 DCHECK(new_host); |
91 | 89 |
92 bool was_tracking = is_tracking_; | 90 bool was_tracking = is_tracking_; |
93 if (old_host) { | 91 if (old_host) { |
94 DCHECK_EQ(host_, old_host); | 92 DCHECK_EQ(host_, old_host); |
95 RemoveCallbacks(); | 93 RemoveCallbacks(); |
96 } | 94 } |
97 | 95 |
98 host_ = new_host; | 96 host_ = new_host; |
99 | 97 |
100 if (was_tracking) | 98 if (was_tracking) |
101 AddCallbacks(); | 99 AddCallbacks(); |
102 } | 100 } |
103 | 101 |
104 void SiteEngagementHelper::InputTracker::Stop() { | 102 void SiteEngagementHelper::InputTracker::Stop() { |
105 pause_timer_->Stop(); | 103 pause_timer_->Stop(); |
106 RemoveCallbacks(); | 104 RemoveCallbacks(); |
107 host_ = nullptr; | 105 host_ = nullptr; |
108 is_active_ = false; | 106 } |
| 107 |
| 108 bool SiteEngagementHelper::InputTracker::IsActive() const { |
| 109 return host_ != nullptr; |
109 } | 110 } |
110 | 111 |
111 void SiteEngagementHelper::InputTracker::SetPauseTimerForTesting( | 112 void SiteEngagementHelper::InputTracker::SetPauseTimerForTesting( |
112 scoped_ptr<base::Timer> timer) { | 113 scoped_ptr<base::Timer> timer) { |
113 pause_timer_ = timer.Pass(); | 114 pause_timer_ = timer.Pass(); |
114 } | 115 } |
115 | 116 |
116 void SiteEngagementHelper::InputTracker::StartTimer(base::TimeDelta delay) { | 117 void SiteEngagementHelper::InputTracker::StartTimer(base::TimeDelta delay) { |
117 pause_timer_->Start( | 118 pause_timer_->Start( |
118 FROM_HERE, delay, | 119 FROM_HERE, delay, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 input_tracker_.Start( | 188 input_tracker_.Start( |
188 web_contents()->GetRenderViewHost(), | 189 web_contents()->GetRenderViewHost(), |
189 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_navigation)); | 190 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_navigation)); |
190 } | 191 } |
191 | 192 |
192 void SiteEngagementHelper::RenderViewHostChanged( | 193 void SiteEngagementHelper::RenderViewHostChanged( |
193 content::RenderViewHost* old_host, | 194 content::RenderViewHost* old_host, |
194 content::RenderViewHost* new_host) { | 195 content::RenderViewHost* new_host) { |
195 // On changing the render view host, we need to re-register the callbacks | 196 // On changing the render view host, we need to re-register the callbacks |
196 // listening for user input. | 197 // listening for user input. |
197 if (input_tracker_.is_tracking()) { | 198 if (input_tracker_.IsActive()) { |
198 input_tracker_.SwitchRenderViewHost(old_host, new_host); | 199 input_tracker_.SwitchRenderViewHost(old_host, new_host); |
199 } | 200 } |
200 } | 201 } |
201 | 202 |
202 void SiteEngagementHelper::WasShown() { | 203 void SiteEngagementHelper::WasShown() { |
203 // Ensure that the input callbacks are registered when we come into view. | 204 // Ensure that the input callbacks are registered when we come into view. |
204 if (record_engagement_) { | 205 if (record_engagement_) { |
205 input_tracker_.Start( | 206 input_tracker_.Start( |
206 web_contents()->GetRenderViewHost(), | 207 web_contents()->GetRenderViewHost(), |
207 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_show)); | 208 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_show)); |
(...skipping 12 matching lines...) Expand all Loading... |
220 | 221 |
221 // static | 222 // static |
222 void SiteEngagementHelper::SetSecondsTrackingDelayAfterNavigation(int seconds) { | 223 void SiteEngagementHelper::SetSecondsTrackingDelayAfterNavigation(int seconds) { |
223 g_seconds_tracking_delay_after_navigation = seconds; | 224 g_seconds_tracking_delay_after_navigation = seconds; |
224 } | 225 } |
225 | 226 |
226 // static | 227 // static |
227 void SiteEngagementHelper::SetSecondsTrackingDelayAfterShow(int seconds) { | 228 void SiteEngagementHelper::SetSecondsTrackingDelayAfterShow(int seconds) { |
228 g_seconds_tracking_delay_after_show = seconds; | 229 g_seconds_tracking_delay_after_show = seconds; |
229 } | 230 } |
OLD | NEW |