| 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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 : content::WebContentsObserver(web_contents), | 166 : content::WebContentsObserver(web_contents), |
| 167 input_tracker_(this, web_contents), | 167 input_tracker_(this, web_contents), |
| 168 media_tracker_(this, web_contents), | 168 media_tracker_(this, web_contents), |
| 169 service_(SiteEngagementService::Get( |
| 170 Profile::FromBrowserContext(web_contents->GetBrowserContext()))), |
| 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)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 258 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation( | 244 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterNavigation( |
| 259 int seconds) { | 245 int seconds) { |
| 260 g_seconds_delay_after_navigation = seconds; | 246 g_seconds_delay_after_navigation = seconds; |
| 261 } | 247 } |
| 262 | 248 |
| 263 // static | 249 // static |
| 264 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow( | 250 void SiteEngagementService::Helper::SetSecondsTrackingDelayAfterShow( |
| 265 int seconds) { | 251 int seconds) { |
| 266 g_seconds_delay_after_show = seconds; | 252 g_seconds_delay_after_show = seconds; |
| 267 } | 253 } |
| OLD | NEW |