Chromium Code Reviews| 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 double 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; | |
| 20 int g_seconds_tracking_delay_after_show = 5; | |
| 19 | 21 |
| 20 } // anonymous namespace | 22 } // anonymous namespace |
| 21 | 23 |
| 22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); | 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SiteEngagementHelper); |
| 23 | 25 |
| 24 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) | 26 SiteEngagementHelper::InputTracker::InputTracker(SiteEngagementHelper* helper) |
| 25 : helper_(helper), | 27 : helper_(helper), |
| 26 pause_timer_(new base::Timer(true, false)), | 28 pause_timer_(new base::Timer(true, false)), |
| 27 callbacks_added_(false) { | 29 host_(nullptr), |
| 30 is_active_(false), | |
| 31 is_tracking_(false) { | |
| 28 key_press_event_callback_ = | 32 key_press_event_callback_ = |
| 29 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent, | 33 base::Bind(&SiteEngagementHelper::InputTracker::HandleKeyPressEvent, |
| 30 base::Unretained(this)); | 34 base::Unretained(this)); |
| 31 mouse_event_callback_ = | 35 mouse_event_callback_ = |
| 32 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent, | 36 base::Bind(&SiteEngagementHelper::InputTracker::HandleMouseEvent, |
| 33 base::Unretained(this)); | 37 base::Unretained(this)); |
| 34 } | 38 } |
| 35 | 39 |
| 36 SiteEngagementHelper::InputTracker::~InputTracker() { } | 40 SiteEngagementHelper::InputTracker::~InputTracker() {} |
| 37 | 41 |
| 38 // Record that there was some user input, and defer handling of the input event. | 42 // Record that there was some user input, and defer handling of the input event. |
| 39 // web_contents() will return nullptr if the observed contents have been | 43 // web_contents() will return nullptr if the observed contents have been |
| 40 // deleted; if the contents exist, record engagement for the site. Once the | 44 // deleted; if the contents exist, record engagement for the site. Once the |
| 41 // timer finishes running, the callbacks detecting user input will be registered | 45 // timer finishes running, the callbacks detecting user input will be registered |
| 42 // again. | 46 // again. |
| 43 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent( | 47 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent( |
| 44 const content::NativeWebKeyboardEvent& event) { | 48 const content::NativeWebKeyboardEvent& event) { |
| 45 // Only respond to raw key down to avoid multiple triggering on a single input | 49 // Only respond to raw key down to avoid multiple triggering on a single input |
| 46 // (e.g. keypress is a key down then key up). | 50 // (e.g. keypress is a key down then key up). |
| 47 if (event.type == blink::WebInputEvent::RawKeyDown) { | 51 if (event.type == blink::WebInputEvent::RawKeyDown) { |
| 48 PauseTracking(helper_->web_contents()->GetRenderViewHost()); | 52 Pause(); |
| 49 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_KEYPRESS); | 53 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_KEYPRESS); |
| 50 } | 54 } |
| 51 return false; | 55 return false; |
| 52 } | 56 } |
| 53 | 57 |
| 54 bool SiteEngagementHelper::InputTracker::HandleMouseEvent( | 58 bool SiteEngagementHelper::InputTracker::HandleMouseEvent( |
| 55 const blink::WebMouseEvent& event) { | 59 const blink::WebMouseEvent& event) { |
| 56 // Only respond to mouse down with a button or mouse move events (e.g. a click | 60 // Only respond to mouse down with a button or mouse move events (e.g. a click |
| 57 // is a mouse down and mouse up) to avoid cases where multiple events come in | 61 // is a mouse down and mouse up) to avoid cases where multiple events come in |
| 58 // before we can pause tracking. | 62 // before we can pause tracking. |
| 59 if ((event.button != blink::WebMouseEvent::ButtonNone && | 63 if ((event.button != blink::WebMouseEvent::ButtonNone && |
| 60 event.type == blink::WebInputEvent::MouseDown) || | 64 event.type == blink::WebInputEvent::MouseDown) || |
| 61 event.type == blink::WebInputEvent::MouseWheel) { | 65 event.type == blink::WebInputEvent::MouseWheel) { |
| 62 PauseTracking(helper_->web_contents()->GetRenderViewHost()); | 66 Pause(); |
| 63 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_MOUSE); | 67 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_MOUSE); |
| 64 } | 68 } |
| 65 return false; | 69 return false; |
| 66 } | 70 } |
| 67 | 71 |
| 68 void SiteEngagementHelper::InputTracker::StartTracking( | 72 void SiteEngagementHelper::InputTracker::Start(content::RenderViewHost* host, |
| 69 content::RenderViewHost* host) { | 73 base::TimeDelta initial_delay) { |
| 70 if (!callbacks_added_) { | 74 DCHECK(!is_active_); |
| 71 host->AddKeyPressEventCallback(key_press_event_callback_); | 75 DCHECK(host); |
| 72 host->AddMouseEventCallback(mouse_event_callback_); | 76 host_ = host; |
| 73 callbacks_added_ = true; | 77 StartTimer(initial_delay); |
| 74 } | 78 is_active_ = true; |
| 75 } | 79 } |
| 76 | 80 |
| 77 void SiteEngagementHelper::InputTracker::PauseTracking( | 81 void SiteEngagementHelper::InputTracker::Pause() { |
| 78 content::RenderViewHost* host) { | 82 RemoveCallbacks(); |
| 79 StopTracking(host); | 83 StartTimer(base::TimeDelta::FromSeconds(g_seconds_between_user_input_check)); |
| 84 } | |
| 85 | |
| 86 void SiteEngagementHelper::InputTracker::SwitchRenderViewHost( | |
| 87 content::RenderViewHost* old_host, | |
| 88 content::RenderViewHost* new_host) { | |
| 89 DCHECK(is_tracking_); | |
| 90 DCHECK(new_host); | |
| 91 | |
| 92 bool was_tracking = is_tracking_; | |
| 93 if (old_host) { | |
| 94 DCHECK_EQ(host_, old_host); | |
| 95 RemoveCallbacks(); | |
| 96 } | |
| 97 | |
| 98 host_ = new_host; | |
| 99 | |
| 100 if (was_tracking) | |
| 101 AddCallbacks(); | |
| 102 } | |
| 103 | |
| 104 void SiteEngagementHelper::InputTracker::Stop() { | |
| 105 pause_timer_->Stop(); | |
| 106 is_active_ = false; | |
|
dominickn
2015/10/07 20:11:03
Nit: assign is_active_ to false last
calamity
2015/10/08 04:26:02
Done.
| |
| 107 RemoveCallbacks(); | |
| 108 host_ = nullptr; | |
| 109 } | |
| 110 | |
| 111 void SiteEngagementHelper::InputTracker::SetPauseTimerForTesting( | |
| 112 scoped_ptr<base::Timer> timer) { | |
| 113 pause_timer_ = timer.Pass(); | |
| 114 } | |
| 115 | |
| 116 void SiteEngagementHelper::InputTracker::StartTimer(base::TimeDelta delay) { | |
| 80 pause_timer_->Start( | 117 pause_timer_->Start( |
| 81 FROM_HERE, | 118 FROM_HERE, delay, |
| 82 base::TimeDelta::FromSeconds(g_seconds_between_user_input_check), | 119 base::Bind(&SiteEngagementHelper::InputTracker::AddCallbacks, |
| 83 base::Bind(&SiteEngagementHelper::InputTracker::ResumeTracking, | |
| 84 base::Unretained(this))); | 120 base::Unretained(this))); |
| 85 } | 121 } |
| 86 | 122 |
| 87 void SiteEngagementHelper::InputTracker::ResumeTracking() { | 123 void SiteEngagementHelper::InputTracker::AddCallbacks() { |
| 88 content::WebContents* contents = helper_->web_contents(); | 124 content::WebContents* contents = helper_->web_contents(); |
| 89 if (contents) | 125 if (!contents) |
| 90 StartTracking(contents->GetRenderViewHost()); | 126 return; |
| 127 | |
| 128 host_->AddKeyPressEventCallback(key_press_event_callback_); | |
| 129 host_->AddMouseEventCallback(mouse_event_callback_); | |
| 130 is_tracking_ = true; | |
| 91 } | 131 } |
| 92 | 132 |
| 93 void SiteEngagementHelper::InputTracker::StopTracking( | 133 void SiteEngagementHelper::InputTracker::RemoveCallbacks() { |
| 94 content::RenderViewHost* host) { | 134 if (is_tracking_) { |
| 95 pause_timer_->Stop(); | 135 host_->RemoveKeyPressEventCallback(key_press_event_callback_); |
| 96 if (callbacks_added_) { | 136 host_->RemoveMouseEventCallback(mouse_event_callback_); |
| 97 host->RemoveKeyPressEventCallback(key_press_event_callback_); | 137 is_tracking_ = false; |
| 98 host->RemoveMouseEventCallback(mouse_event_callback_); | |
| 99 callbacks_added_ = false; | |
| 100 } | 138 } |
| 101 } | 139 } |
| 102 | 140 |
| 103 void SiteEngagementHelper::InputTracker::SetTimerForTesting( | |
| 104 scoped_ptr<base::Timer> timer) { | |
| 105 pause_timer_ = timer.Pass(); | |
| 106 } | |
| 107 | |
| 108 SiteEngagementHelper::~SiteEngagementHelper() { | 141 SiteEngagementHelper::~SiteEngagementHelper() { |
| 109 content::WebContents* contents = web_contents(); | 142 content::WebContents* contents = web_contents(); |
| 110 if (contents) | 143 if (contents) |
| 111 input_tracker_.StopTracking(contents->GetRenderViewHost()); | 144 input_tracker_.Stop(); |
| 112 } | 145 } |
| 113 | 146 |
| 114 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) | 147 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) |
| 115 : content::WebContentsObserver(contents), | 148 : content::WebContentsObserver(contents), |
| 116 input_tracker_(this), | 149 input_tracker_(this), |
| 117 record_engagement_(false) { } | 150 record_engagement_(false) {} |
| 118 | 151 |
| 119 void SiteEngagementHelper::RecordUserInput( | 152 void SiteEngagementHelper::RecordUserInput( |
| 120 SiteEngagementMetrics::EngagementType type) { | 153 SiteEngagementMetrics::EngagementType type) { |
| 121 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); | 154 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); |
| 122 content::WebContents* contents = web_contents(); | 155 content::WebContents* contents = web_contents(); |
| 123 if (contents) { | 156 if (contents) { |
| 124 Profile* profile = | 157 Profile* profile = |
| 125 Profile::FromBrowserContext(contents->GetBrowserContext()); | 158 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 126 SiteEngagementService* service = | 159 SiteEngagementService* service = |
| 127 SiteEngagementServiceFactory::GetForProfile(profile); | 160 SiteEngagementServiceFactory::GetForProfile(profile); |
| 128 | 161 |
| 129 // Service is null in incognito. | 162 // Service is null in incognito. |
| 130 if (service) | 163 if (service) |
| 131 service->HandleUserInput(contents->GetVisibleURL(), type); | 164 service->HandleUserInput(contents->GetVisibleURL(), type); |
| 132 } | 165 } |
| 133 } | 166 } |
| 134 | 167 |
| 135 bool SiteEngagementHelper::ShouldRecordEngagement() { | |
| 136 return record_engagement_; | |
| 137 } | |
| 138 | |
| 139 void SiteEngagementHelper::DidNavigateMainFrame( | 168 void SiteEngagementHelper::DidNavigateMainFrame( |
| 140 const content::LoadCommittedDetails& details, | 169 const content::LoadCommittedDetails& details, |
| 141 const content::FrameNavigateParams& params) { | 170 const content::FrameNavigateParams& params) { |
| 142 content::WebContents* contents = web_contents(); | 171 input_tracker_.Stop(); |
| 143 input_tracker_.StopTracking(contents->GetRenderViewHost()); | 172 |
| 173 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); | |
| 144 | 174 |
| 145 // Ignore all schemes except HTTP and HTTPS. | 175 // Ignore all schemes except HTTP and HTTPS. |
| 146 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); | 176 if (!record_engagement_) |
| 147 | |
| 148 if (!ShouldRecordEngagement()) | |
| 149 return; | 177 return; |
| 150 | 178 |
| 151 Profile* profile = | 179 Profile* profile = |
| 152 Profile::FromBrowserContext(contents->GetBrowserContext()); | 180 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 153 SiteEngagementService* service = | 181 SiteEngagementService* service = |
| 154 SiteEngagementServiceFactory::GetForProfile(profile); | 182 SiteEngagementServiceFactory::GetForProfile(profile); |
| 155 | 183 |
| 156 if (service) | 184 if (service) |
| 157 service->HandleNavigation(params.url, params.transition); | 185 service->HandleNavigation(params.url, params.transition); |
| 158 | 186 |
| 159 input_tracker_.StartTracking(contents->GetRenderViewHost()); | 187 input_tracker_.Start( |
| 188 web_contents()->GetRenderViewHost(), | |
| 189 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_navigation)); | |
| 160 } | 190 } |
| 161 | 191 |
| 162 void SiteEngagementHelper::RenderViewHostChanged( | 192 void SiteEngagementHelper::RenderViewHostChanged( |
| 163 content::RenderViewHost* old_host, | 193 content::RenderViewHost* old_host, |
| 164 content::RenderViewHost* new_host) { | 194 content::RenderViewHost* new_host) { |
| 165 // On changing the render view host, we need to re-register the callbacks | 195 // On changing the render view host, we need to re-register the callbacks |
| 166 // listening for user input. | 196 // listening for user input. |
| 167 if (ShouldRecordEngagement()) { | 197 if (input_tracker_.is_tracking()) { |
| 168 if (old_host) | 198 input_tracker_.SwitchRenderViewHost(old_host, new_host); |
| 169 input_tracker_.StopTracking(old_host); | |
| 170 input_tracker_.StartTracking(new_host); | |
| 171 } | 199 } |
| 172 } | 200 } |
| 173 | 201 |
| 174 void SiteEngagementHelper::WasShown() { | 202 void SiteEngagementHelper::WasShown() { |
| 175 // Ensure that the input callbacks are registered when we come into view. | 203 // Ensure that the input callbacks are registered when we come into view. |
| 176 if (ShouldRecordEngagement()) | 204 if (record_engagement_) { |
| 177 input_tracker_.StartTracking(web_contents()->GetRenderViewHost()); | 205 input_tracker_.Start( |
| 206 web_contents()->GetRenderViewHost(), | |
| 207 base::TimeDelta::FromSeconds(g_seconds_tracking_delay_after_show)); | |
| 208 } | |
| 178 } | 209 } |
| 179 | 210 |
| 180 void SiteEngagementHelper::WasHidden() { | 211 void SiteEngagementHelper::WasHidden() { |
| 181 // Ensure that the input callbacks are not registered when hidden. | 212 // Ensure that the input callbacks are not registered when hidden. |
| 182 if (ShouldRecordEngagement()) { | 213 input_tracker_.Stop(); |
| 183 content::WebContents* contents = web_contents(); | |
| 184 if (contents) | |
| 185 input_tracker_.StopTracking(contents->GetRenderViewHost()); | |
| 186 } | |
| 187 } | 214 } |
| 188 | 215 |
| 189 // static | 216 // static |
| 190 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(double seconds) { | 217 void SiteEngagementHelper::SetSecondsBetweenUserInputCheck(int seconds) { |
| 191 g_seconds_between_user_input_check = seconds; | 218 g_seconds_between_user_input_check = seconds; |
| 192 } | 219 } |
| 220 | |
| 221 // static | |
| 222 void SiteEngagementHelper::SetSecondsTrackingDelayAfterNavigation(int seconds) { | |
| 223 g_seconds_tracking_delay_after_navigation = seconds; | |
| 224 } | |
| 225 | |
| 226 // static | |
| 227 void SiteEngagementHelper::SetSecondsTrackingDelayAfterShow(int seconds) { | |
| 228 g_seconds_tracking_delay_after_show = seconds; | |
| 229 } | |
| OLD | NEW |