| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "vr_usage_monitor.h" | 5 #include "vr_usage_monitor.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "components/rappor/public/rappor_utils.h" | 9 #include "components/rappor/public/rappor_utils.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } else if (is_webvr_) { | 168 } else if (is_webvr_) { |
| 169 mode = VRMode::WEBVR; | 169 mode = VRMode::WEBVR; |
| 170 } else { | 170 } else { |
| 171 mode = is_fullscreen_ ? VRMode::VR_FULLSCREEN : VRMode::VR_BROWSER; | 171 mode = is_fullscreen_ ? VRMode::VR_FULLSCREEN : VRMode::VR_BROWSER; |
| 172 } | 172 } |
| 173 | 173 |
| 174 if (mode != mode_) | 174 if (mode != mode_) |
| 175 SetVrMode(mode); | 175 SetVrMode(mode); |
| 176 } | 176 } |
| 177 | 177 |
| 178 void VrMetricsHelper::SetWebVREnabledOnMainThread(bool is_webvr_presenting) { | 178 void VrMetricsHelper::SetWebVREnabled(bool is_webvr_presenting) { |
| 179 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 180 is_webvr_ = is_webvr_presenting; | 179 is_webvr_ = is_webvr_presenting; |
| 181 UpdateMode(); | 180 UpdateMode(); |
| 182 } | 181 } |
| 183 | 182 |
| 184 void VrMetricsHelper::SetVRActiveOnMainThread(bool is_vr_enabled) { | 183 void VrMetricsHelper::SetVRActive(bool is_vr_enabled) { |
| 185 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 186 is_vr_enabled_ = is_vr_enabled; | 184 is_vr_enabled_ = is_vr_enabled; |
| 187 UpdateMode(); | 185 UpdateMode(); |
| 188 } | 186 } |
| 189 | 187 |
| 190 void VrMetricsHelper::SetWebVREnabled(bool is_webvr) { | |
| 191 content::BrowserThread::PostTask( | |
| 192 content::BrowserThread::UI, FROM_HERE, | |
| 193 base::Bind(&VrMetricsHelper::SetWebVREnabledOnMainThread, this, | |
| 194 is_webvr)); | |
| 195 } | |
| 196 | |
| 197 void VrMetricsHelper::SetVRActive(bool is_vr_enabled) { | |
| 198 content::BrowserThread::PostTask( | |
| 199 content::BrowserThread::UI, FROM_HERE, | |
| 200 base::Bind(&VrMetricsHelper::SetVRActiveOnMainThread, this, | |
| 201 is_vr_enabled)); | |
| 202 } | |
| 203 | |
| 204 void VrMetricsHelper::SetVrMode(VRMode mode) { | 188 void VrMetricsHelper::SetVrMode(VRMode mode) { |
| 205 DCHECK(mode != mode_); | 189 DCHECK(mode != mode_); |
| 206 | 190 |
| 207 base::Time switchTime = base::Time::Now(); | 191 base::Time switchTime = base::Time::Now(); |
| 208 | 192 |
| 209 // stop the previous modes | 193 // stop the previous modes |
| 210 if (mode_ != VRMode::NO_VR) { | 194 if (mode_ != VRMode::NO_VR) { |
| 211 if (num_videos_playing_ > 0) | 195 if (num_videos_playing_ > 0) |
| 212 mode_video_timer_->StopSession(false, switchTime); | 196 mode_video_timer_->StopSession(false, switchTime); |
| 213 | 197 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 255 |
| 272 if (num_videos_playing_ > 0) { | 256 if (num_videos_playing_ > 0) { |
| 273 session_video_timer_->StartSession(switchTime); | 257 session_video_timer_->StartSession(switchTime); |
| 274 num_session_video_playback_ = num_videos_playing_; | 258 num_session_video_playback_ = num_videos_playing_; |
| 275 } | 259 } |
| 276 } | 260 } |
| 277 | 261 |
| 278 mode_ = mode; | 262 mode_ = mode; |
| 279 } | 263 } |
| 280 | 264 |
| 281 VrMetricsHelper::VrMetricsHelper(content::WebContents* contents) | 265 VrMetricsHelper::VrMetricsHelper(content::WebContents* contents) { |
| 282 : thread_id_(base::PlatformThread::CurrentId()) { | |
| 283 num_videos_playing_ = contents->GetCurrentlyPlayingVideoCount(); | 266 num_videos_playing_ = contents->GetCurrentlyPlayingVideoCount(); |
| 284 is_fullscreen_ = contents->IsFullscreen(); | 267 is_fullscreen_ = contents->IsFullscreen(); |
| 285 origin_ = contents->GetLastCommittedURL(); | 268 origin_ = contents->GetLastCommittedURL(); |
| 286 | 269 |
| 287 Observe(contents); | 270 Observe(contents); |
| 288 session_timer_.reset(new SessionTimerImpl<SESSION_VR>( | 271 session_timer_.reset(new SessionTimerImpl<SESSION_VR>( |
| 289 maximumHeadsetSessionGap, minimumHeadsetSessionDuration)); | 272 maximumHeadsetSessionGap, minimumHeadsetSessionDuration)); |
| 290 session_video_timer_.reset(new SessionTimerImpl<SESSION_VR_WITH_VIDEO>( | 273 session_video_timer_.reset(new SessionTimerImpl<SESSION_VR_WITH_VIDEO>( |
| 291 maximumVideoSessionGap, minimumVideoSessionDuration)); | 274 maximumVideoSessionGap, minimumVideoSessionDuration)); |
| 292 } | 275 } |
| 293 | 276 |
| 294 VrMetricsHelper::~VrMetricsHelper() { | 277 VrMetricsHelper::~VrMetricsHelper() = default; |
| 295 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 296 } | |
| 297 | 278 |
| 298 void VrMetricsHelper::MediaStartedPlaying(const MediaPlayerInfo& media_info, | 279 void VrMetricsHelper::MediaStartedPlaying(const MediaPlayerInfo& media_info, |
| 299 const MediaPlayerId&) { | 280 const MediaPlayerId&) { |
| 300 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 301 if (!media_info.has_video) | 281 if (!media_info.has_video) |
| 302 return; | 282 return; |
| 303 | 283 |
| 304 if (num_videos_playing_ == 0) { | 284 if (num_videos_playing_ == 0) { |
| 305 // started playing video - start sessions | 285 // started playing video - start sessions |
| 306 base::Time start_time = base::Time::Now(); | 286 base::Time start_time = base::Time::Now(); |
| 307 | 287 |
| 308 if (mode_ != VRMode::NO_VR) { | 288 if (mode_ != VRMode::NO_VR) { |
| 309 session_video_timer_->StartSession(start_time); | 289 session_video_timer_->StartSession(start_time); |
| 310 mode_video_timer_->StartSession(start_time); | 290 mode_video_timer_->StartSession(start_time); |
| 311 SendRapporEnteredVideoMode(origin_, mode_); | 291 SendRapporEnteredVideoMode(origin_, mode_); |
| 312 } | 292 } |
| 313 } | 293 } |
| 314 | 294 |
| 315 num_videos_playing_++; | 295 num_videos_playing_++; |
| 316 num_session_video_playback_++; | 296 num_session_video_playback_++; |
| 317 } | 297 } |
| 318 | 298 |
| 319 void VrMetricsHelper::MediaStoppedPlaying(const MediaPlayerInfo& media_info, | 299 void VrMetricsHelper::MediaStoppedPlaying(const MediaPlayerInfo& media_info, |
| 320 const MediaPlayerId&) { | 300 const MediaPlayerId&) { |
| 321 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 322 if (!media_info.has_video) | 301 if (!media_info.has_video) |
| 323 return; | 302 return; |
| 324 | 303 |
| 325 num_videos_playing_--; | 304 num_videos_playing_--; |
| 326 | 305 |
| 327 if (num_videos_playing_ == 0) { | 306 if (num_videos_playing_ == 0) { |
| 328 // stopped playing video - update existing video sessions | 307 // stopped playing video - update existing video sessions |
| 329 base::Time stop_time = base::Time::Now(); | 308 base::Time stop_time = base::Time::Now(); |
| 330 | 309 |
| 331 if (mode_ != VRMode::NO_VR) { | 310 if (mode_ != VRMode::NO_VR) { |
| 332 session_video_timer_->StopSession(true, stop_time); | 311 session_video_timer_->StopSession(true, stop_time); |
| 333 mode_video_timer_->StopSession(true, stop_time); | 312 mode_video_timer_->StopSession(true, stop_time); |
| 334 } | 313 } |
| 335 } | 314 } |
| 336 } | 315 } |
| 337 | 316 |
| 338 void VrMetricsHelper::DidFinishNavigation(content::NavigationHandle* handle) { | 317 void VrMetricsHelper::DidFinishNavigation(content::NavigationHandle* handle) { |
| 339 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 340 if (handle != nullptr && handle->HasCommitted() && handle->IsInMainFrame()) { | 318 if (handle != nullptr && handle->HasCommitted() && handle->IsInMainFrame()) { |
| 341 origin_ = handle->GetURL(); | 319 origin_ = handle->GetURL(); |
| 342 // Counting the number of pages viewed is difficult - some websites load | 320 // Counting the number of pages viewed is difficult - some websites load |
| 343 // new content dynamically without a navigation. Others redirect several | 321 // new content dynamically without a navigation. Others redirect several |
| 344 // times for a single navigation. | 322 // times for a single navigation. |
| 345 // We look at the number of committed navigations in the main frame, which | 323 // We look at the number of committed navigations in the main frame, which |
| 346 // will slightly overestimate pages viewed instead of trying to filter or | 324 // will slightly overestimate pages viewed instead of trying to filter or |
| 347 // look at page loads, since those will underestimate on some pages, and | 325 // look at page loads, since those will underestimate on some pages, and |
| 348 // overestimate on others. | 326 // overestimate on others. |
| 349 num_session_navigation_++; | 327 num_session_navigation_++; |
| 350 } | 328 } |
| 351 } | 329 } |
| 352 | 330 |
| 353 void VrMetricsHelper::DidToggleFullscreenModeForTab(bool entered_fullscreen, | 331 void VrMetricsHelper::DidToggleFullscreenModeForTab(bool entered_fullscreen, |
| 354 bool will_cause_resize) { | 332 bool will_cause_resize) { |
| 355 DCHECK_EQ(thread_id_, base::PlatformThread::CurrentId()); | |
| 356 is_fullscreen_ = entered_fullscreen; | 333 is_fullscreen_ = entered_fullscreen; |
| 357 UpdateMode(); | 334 UpdateMode(); |
| 358 } | 335 } |
| 359 | 336 |
| 360 } // namespace vr_shell | 337 } // namespace vr_shell |
| OLD | NEW |