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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 is_hidden_ = true; | 155 is_hidden_ = true; |
| 156 } | 156 } |
| 157 | 157 |
| 158 SiteEngagementService::Helper::~Helper() { | 158 SiteEngagementService::Helper::~Helper() { |
| 159 if (web_contents()) { | 159 if (web_contents()) { |
| 160 input_tracker_.Stop(); | 160 input_tracker_.Stop(); |
| 161 media_tracker_.Stop(); | 161 media_tracker_.Stop(); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 SiteEngagementService::Helper::Helper(content::WebContents* web_contents) | 165 SiteEngagementService::Helper::Helper(content::WebContents* web_contents, |
| 166 SiteEngagementService* service) | |
| 166 : content::WebContentsObserver(web_contents), | 167 : content::WebContentsObserver(web_contents), |
| 167 input_tracker_(this, web_contents), | 168 input_tracker_(this, web_contents), |
| 168 media_tracker_(this, web_contents), | 169 media_tracker_(this, web_contents), |
| 170 service_(service), | |
|
calamity
2016/06/09 01:41:19
Can we use the web_contents->browser_context() to
dominickn
2016/06/09 03:25:47
Done.
| |
| 169 record_engagement_(false) {} | 171 record_engagement_(false) {} |
| 170 | 172 |
| 171 void SiteEngagementService::Helper::RecordUserInput( | 173 void SiteEngagementService::Helper::RecordUserInput( |
| 172 SiteEngagementMetrics::EngagementType type) { | 174 SiteEngagementMetrics::EngagementType type) { |
| 173 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); | 175 TRACE_EVENT0("SiteEngagement", "RecordUserInput"); |
| 174 content::WebContents* contents = web_contents(); | 176 content::WebContents* contents = web_contents(); |
| 175 if (contents) { | 177 // Service is null in incognito. |
| 176 Profile* profile = | 178 if (contents && service_) |
| 177 Profile::FromBrowserContext(contents->GetBrowserContext()); | 179 service_->HandleUserInput(contents, type); |
| 178 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
| 179 | |
| 180 // Service is null in incognito. | |
| 181 if (service) | |
| 182 service->HandleUserInput(contents->GetVisibleURL(), type); | |
| 183 } | |
| 184 } | 180 } |
| 185 | 181 |
| 186 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) { | 182 void SiteEngagementService::Helper::RecordMediaPlaying(bool is_hidden) { |
| 187 content::WebContents* contents = web_contents(); | 183 content::WebContents* contents = web_contents(); |
| 188 if (contents) { | 184 if (contents && service_) |
| 189 Profile* profile = | 185 service_->HandleMediaPlaying(contents, is_hidden); |
| 190 Profile::FromBrowserContext(contents->GetBrowserContext()); | |
| 191 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
| 192 | |
| 193 if (service) | |
| 194 service->HandleMediaPlaying(contents->GetVisibleURL(), is_hidden); | |
| 195 } | |
| 196 } | 186 } |
| 197 | 187 |
| 198 void SiteEngagementService::Helper::DidFinishNavigation( | 188 void SiteEngagementService::Helper::DidFinishNavigation( |
| 199 content::NavigationHandle* handle) { | 189 content::NavigationHandle* handle) { |
| 200 input_tracker_.Stop(); | 190 input_tracker_.Stop(); |
| 201 media_tracker_.Stop(); | 191 media_tracker_.Stop(); |
| 202 | 192 |
| 203 // Ignore all schemes except HTTP and HTTPS, as well as uncommitted, non | 193 // Ignore all schemes except HTTP and HTTPS, as well as uncommitted, non |
| 204 // main-frame, same page, or error page navigations. | 194 // main-frame, same page, or error page navigations. |
| 205 record_engagement_ = handle->GetURL().SchemeIsHTTPOrHTTPS(); | 195 record_engagement_ = handle->GetURL().SchemeIsHTTPOrHTTPS(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 217 // engagement. | 207 // engagement. |
| 218 // - Prerenders initiated by <link rel="prerender"> (e.g. search results) are | 208 // - Prerenders initiated by <link rel="prerender"> (e.g. search results) are |
| 219 // always assigned the LINK transition, which is ignored for navigation | 209 // always assigned the LINK transition, which is ignored for navigation |
| 220 // engagement. | 210 // engagement. |
| 221 // | 211 // |
| 222 // Prerenders trigger WasShown() when they are swapped in, so input engagement | 212 // Prerenders trigger WasShown() when they are swapped in, so input engagement |
| 223 // will activate even if navigation engagement is not scored. | 213 // will activate even if navigation engagement is not scored. |
| 224 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr) | 214 if (prerender::PrerenderContents::FromWebContents(web_contents()) != nullptr) |
| 225 return; | 215 return; |
| 226 | 216 |
| 227 Profile* profile = | 217 if (service_) |
| 228 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 218 service_->HandleNavigation(web_contents(), handle->GetPageTransition()); |
| 229 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
| 230 | |
| 231 if (service) | |
| 232 service->HandleNavigation(handle->GetURL(), handle->GetPageTransition()); | |
| 233 | 219 |
| 234 input_tracker_.Start( | 220 input_tracker_.Start( |
| 235 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); | 221 base::TimeDelta::FromSeconds(g_seconds_delay_after_navigation)); |
| 236 } | 222 } |
| 237 | 223 |
| 238 void SiteEngagementService::Helper::WasShown() { | 224 void SiteEngagementService::Helper::WasShown() { |
| 239 // Ensure that the input callbacks are registered when we come into view. | 225 // Ensure that the input callbacks are registered when we come into view. |
| 240 if (record_engagement_) { | 226 if (record_engagement_) { |
| 241 input_tracker_.Start( | 227 input_tracker_.Start( |
| 242 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); | 228 base::TimeDelta::FromSeconds(g_seconds_delay_after_show)); |
| 243 } | 229 } |
| 244 } | 230 } |
| 245 | 231 |
| 246 void SiteEngagementService::Helper::WasHidden() { | 232 void SiteEngagementService::Helper::WasHidden() { |
| 247 // Ensure that the input callbacks are not registered when hidden. | 233 // Ensure that the input callbacks are not registered when hidden. |
| 248 input_tracker_.Stop(); | 234 input_tracker_.Stop(); |
| 249 } | 235 } |
| 250 | 236 |
| 251 // static | 237 // static |
| 238 void SiteEngagementService::Helper::CreateForWebContents( | |
| 239 content::WebContents* web_contents, | |
| 240 SiteEngagementService* service) { | |
| 241 DCHECK(web_contents); | |
| 242 if (!FromWebContents(web_contents)) { | |
| 243 web_contents->SetUserData(UserDataKey(), new SiteEngagementService::Helper( | |
| 244 web_contents, service)); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 // static | |
| 252 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck( | 249 void SiteEngagementService::Helper::SetSecondsBetweenUserInputCheck( |
| 253 int seconds) { | 250 int seconds) { |
| 254 g_seconds_to_pause_engagement_detection = seconds; | 251 g_seconds_to_pause_engagement_detection = seconds; |
| 255 } | 252 } |
| 256 | 253 |
| 257 // static | 254 // static |
| 258 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation( | 255 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation( |
| 259 int seconds) { | 256 int seconds) { |
| 260 g_seconds_delay_after_navigation = seconds; | 257 g_seconds_delay_after_navigation = seconds; |
| 261 } | 258 } |
| 262 | 259 |
| 263 // static | 260 // static |
| 264 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow( | 261 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow( |
| 265 int seconds) { | 262 int seconds) { |
| 266 g_seconds_delay_after_show = seconds; | 263 g_seconds_delay_after_show = seconds; |
| 267 } | 264 } |
| OLD | NEW |