| 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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 // web_contents() will return nullptr if the observed contents have been | 40 // web_contents() will return nullptr if the observed contents have been |
| 41 // deleted; if the contents exist, record engagement for the site. Once the | 41 // deleted; if the contents exist, record engagement for the site. Once the |
| 42 // timer finishes running, the callbacks detecting user input will be registered | 42 // timer finishes running, the callbacks detecting user input will be registered |
| 43 // again. | 43 // again. |
| 44 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent( | 44 bool SiteEngagementHelper::InputTracker::HandleKeyPressEvent( |
| 45 const content::NativeWebKeyboardEvent& event) { | 45 const content::NativeWebKeyboardEvent& event) { |
| 46 // Only respond to raw key down to avoid multiple triggering on a single input | 46 // Only respond to raw key down to avoid multiple triggering on a single input |
| 47 // (e.g. keypress is a key down then key up). | 47 // (e.g. keypress is a key down then key up). |
| 48 if (event.type == blink::WebInputEvent::RawKeyDown) { | 48 if (event.type == blink::WebInputEvent::RawKeyDown) { |
| 49 PauseTracking(helper_->web_contents()->GetRenderViewHost()); | 49 PauseTracking(helper_->web_contents()->GetRenderViewHost()); |
| 50 helper_->RecordUserInput(); | 50 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_KEYPRESS); |
| 51 } | 51 } |
| 52 return false; | 52 return false; |
| 53 } | 53 } |
| 54 | 54 |
| 55 bool SiteEngagementHelper::InputTracker::HandleMouseEvent( | 55 bool SiteEngagementHelper::InputTracker::HandleMouseEvent( |
| 56 const blink::WebMouseEvent& event) { | 56 const blink::WebMouseEvent& event) { |
| 57 // Only respond to mouse down with a button or mouse move events (e.g. a click | 57 // Only respond to mouse down with a button or mouse move events (e.g. a click |
| 58 // is a mouse down and mouse up) to avoid cases where multiple events come in | 58 // is a mouse down and mouse up) to avoid cases where multiple events come in |
| 59 // before we can pause tracking. | 59 // before we can pause tracking. |
| 60 if ((event.button != blink::WebMouseEvent::ButtonNone && | 60 if ((event.button != blink::WebMouseEvent::ButtonNone && |
| 61 event.type == blink::WebInputEvent::MouseDown) || | 61 event.type == blink::WebInputEvent::MouseDown) || |
| 62 event.type == blink::WebInputEvent::MouseWheel) { | 62 event.type == blink::WebInputEvent::MouseWheel) { |
| 63 PauseTracking(helper_->web_contents()->GetRenderViewHost()); | 63 PauseTracking(helper_->web_contents()->GetRenderViewHost()); |
| 64 helper_->RecordUserInput(); | 64 helper_->RecordUserInput(SiteEngagementMetrics::ENGAGEMENT_MOUSE); |
| 65 } | 65 } |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void SiteEngagementHelper::InputTracker::StartTracking( | 69 void SiteEngagementHelper::InputTracker::StartTracking( |
| 70 content::RenderViewHost* host) { | 70 content::RenderViewHost* host) { |
| 71 if (!callbacks_added_ && !g_disable_callback_registration_for_testing) { | 71 if (!callbacks_added_ && !g_disable_callback_registration_for_testing) { |
| 72 host->AddKeyPressEventCallback(key_press_event_callback_); | 72 host->AddKeyPressEventCallback(key_press_event_callback_); |
| 73 host->AddMouseEventCallback(mouse_event_callback_); | 73 host->AddMouseEventCallback(mouse_event_callback_); |
| 74 callbacks_added_ = true; | 74 callbacks_added_ = true; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 118 |
| 119 void SiteEngagementHelper::RemoveObserverForTesting(Observer* observer) { | 119 void SiteEngagementHelper::RemoveObserverForTesting(Observer* observer) { |
| 120 observer_list_.RemoveObserver(observer); | 120 observer_list_.RemoveObserver(observer); |
| 121 } | 121 } |
| 122 | 122 |
| 123 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) | 123 SiteEngagementHelper::SiteEngagementHelper(content::WebContents* contents) |
| 124 : content::WebContentsObserver(contents), | 124 : content::WebContentsObserver(contents), |
| 125 input_tracker_(new InputTracker(this)), | 125 input_tracker_(new InputTracker(this)), |
| 126 record_engagement_(false) { } | 126 record_engagement_(false) { } |
| 127 | 127 |
| 128 void SiteEngagementHelper::RecordUserInput() { | 128 void SiteEngagementHelper::RecordUserInput( |
| 129 SiteEngagementMetrics::EngagementType type) { |
| 129 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); | 130 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); |
| 130 content::WebContents* contents = web_contents(); | 131 content::WebContents* contents = web_contents(); |
| 131 if (contents) { | 132 if (contents) { |
| 132 Profile* profile = | 133 Profile* profile = |
| 133 Profile::FromBrowserContext(contents->GetBrowserContext()); | 134 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 134 SiteEngagementService* service = | 135 SiteEngagementService* service = |
| 135 SiteEngagementServiceFactory::GetForProfile(profile); | 136 SiteEngagementServiceFactory::GetForProfile(profile); |
| 136 | 137 |
| 137 // Service is null in incognito. | 138 // Service is null in incognito. |
| 138 if (service) { | 139 if (service) { |
| 140 SiteEngagementMetrics::RecordEngagement(type); |
| 139 service->HandleUserInput(contents->GetVisibleURL()); | 141 service->HandleUserInput(contents->GetVisibleURL()); |
| 140 FOR_EACH_OBSERVER(Observer, observer_list_, OnInputRecorded(this)); | 142 FOR_EACH_OBSERVER(Observer, observer_list_, OnInputRecorded(this)); |
| 141 } | 143 } |
| 142 } | 144 } |
| 143 } | 145 } |
| 144 | 146 |
| 145 void SiteEngagementHelper::DidNavigateMainFrame( | 147 void SiteEngagementHelper::DidNavigateMainFrame( |
| 146 const content::LoadCommittedDetails& details, | 148 const content::LoadCommittedDetails& details, |
| 147 const content::FrameNavigateParams& params) { | 149 const content::FrameNavigateParams& params) { |
| 148 content::WebContents* contents = web_contents(); | 150 content::WebContents* contents = web_contents(); |
| 149 input_tracker_->StopTracking(contents->GetRenderViewHost()); | 151 input_tracker_->StopTracking(contents->GetRenderViewHost()); |
| 150 | 152 |
| 151 // Ignore all schemes except HTTP and HTTPS. | 153 // Ignore all schemes except HTTP and HTTPS. |
| 152 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); | 154 record_engagement_ = params.url.SchemeIsHTTPOrHTTPS(); |
| 153 | 155 |
| 154 if (!record_engagement_) | 156 if (!record_engagement_) |
| 155 return; | 157 return; |
| 156 | 158 |
| 157 Profile* profile = | 159 Profile* profile = |
| 158 Profile::FromBrowserContext(contents->GetBrowserContext()); | 160 Profile::FromBrowserContext(contents->GetBrowserContext()); |
| 159 SiteEngagementService* service = | 161 SiteEngagementService* service = |
| 160 SiteEngagementServiceFactory::GetForProfile(profile); | 162 SiteEngagementServiceFactory::GetForProfile(profile); |
| 161 | 163 |
| 162 if (service) | 164 if (service) { |
| 165 SiteEngagementMetrics::RecordEngagement( |
| 166 SiteEngagementMetrics::ENGAGEMENT_NAVIGATION); |
| 163 service->HandleNavigation(params.url, params.transition); | 167 service->HandleNavigation(params.url, params.transition); |
| 168 } |
| 164 | 169 |
| 165 input_tracker_->StartTracking(contents->GetRenderViewHost()); | 170 input_tracker_->StartTracking(contents->GetRenderViewHost()); |
| 166 } | 171 } |
| 167 | 172 |
| 168 void SiteEngagementHelper::RenderViewHostChanged( | 173 void SiteEngagementHelper::RenderViewHostChanged( |
| 169 content::RenderViewHost* old_host, | 174 content::RenderViewHost* old_host, |
| 170 content::RenderViewHost* new_host) { | 175 content::RenderViewHost* new_host) { |
| 171 // On changing the render view host, we need to re-register the callbacks | 176 // On changing the render view host, we need to re-register the callbacks |
| 172 // listening for user input. | 177 // listening for user input. |
| 173 if (record_engagement_) { | 178 if (record_engagement_) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 199 | 204 |
| 200 // static | 205 // static |
| 201 void SiteEngagementHelper::EnableCallbackRegistrationForTesting() { | 206 void SiteEngagementHelper::EnableCallbackRegistrationForTesting() { |
| 202 g_disable_callback_registration_for_testing = false; | 207 g_disable_callback_registration_for_testing = false; |
| 203 } | 208 } |
| 204 | 209 |
| 205 // static | 210 // static |
| 206 void SiteEngagementHelper::DisableCallbackRegistrationForTesting() { | 211 void SiteEngagementHelper::DisableCallbackRegistrationForTesting() { |
| 207 g_disable_callback_registration_for_testing = true; | 212 g_disable_callback_registration_for_testing = true; |
| 208 } | 213 } |
| OLD | NEW |