| 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 "core/html/media/AutoplayUmaHelper.h" | 5 #include "core/html/media/AutoplayUmaHelper.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/ElementVisibilityObserver.h" | 8 #include "core/dom/ElementVisibilityObserver.h" |
| 9 #include "core/events/Event.h" | 9 #include "core/events/Event.h" |
| 10 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
| 11 #include "core/frame/UseCounter.h" | 11 #include "core/frame/UseCounter.h" |
| 12 #include "core/html/HTMLMediaElement.h" | 12 #include "core/html/HTMLMediaElement.h" |
| 13 #include "core/html/media/AutoplayPolicy.h" | 13 #include "core/html/media/AutoplayPolicy.h" |
| 14 #include "platform/Histogram.h" | 14 #include "platform/Histogram.h" |
| 15 #include "platform/wtf/CurrentTime.h" | 15 #include "platform/wtf/CurrentTime.h" |
| 16 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const int32_t kMaxOffscreenDurationUmaMS = 60 * 60 * 1000; | 22 const int32_t kMaxOffscreenDurationUmaMS = 60 * 60 * 1000; |
| 23 const int32_t kOffscreenDurationUmaBucketCount = 50; | 23 const int32_t kOffscreenDurationUmaBucketCount = 50; |
| 24 const int32_t kMaxWaitTimeUmaMS = 30 * 1000; |
| 25 const int32_t kWaitTimeBucketCount = 50; |
| 24 | 26 |
| 25 } // namespace | 27 } // namespace |
| 26 | 28 |
| 27 AutoplayUmaHelper* AutoplayUmaHelper::Create(HTMLMediaElement* element) { | 29 AutoplayUmaHelper* AutoplayUmaHelper::Create(HTMLMediaElement* element) { |
| 28 return new AutoplayUmaHelper(element); | 30 return new AutoplayUmaHelper(element); |
| 29 } | 31 } |
| 30 | 32 |
| 31 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) | 33 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) |
| 32 : EventListener(kCPPEventListenerType), | 34 : EventListener(kCPPEventListenerType), |
| 33 ContextLifecycleObserver(nullptr), | 35 ContextLifecycleObserver(nullptr), |
| 34 element_(element), | 36 element_(element), |
| 35 muted_video_play_method_visibility_observer_(nullptr), | 37 muted_video_play_method_visibility_observer_(nullptr), |
| 36 muted_video_autoplay_offscreen_start_time_ms_(0), | 38 muted_video_autoplay_offscreen_start_time_ms_(0), |
| 37 muted_video_autoplay_offscreen_duration_ms_(0), | 39 muted_video_autoplay_offscreen_duration_ms_(0), |
| 38 is_visible_(false), | 40 is_visible_(false), |
| 39 muted_video_offscreen_duration_visibility_observer_(nullptr) {} | 41 muted_video_offscreen_duration_visibility_observer_(nullptr), |
| 42 load_start_time_ms_(0.0) { |
| 43 element->addEventListener(EventTypeNames::loadstart, this, false); |
| 44 } |
| 40 | 45 |
| 41 AutoplayUmaHelper::~AutoplayUmaHelper() = default; | 46 AutoplayUmaHelper::~AutoplayUmaHelper() = default; |
| 42 | 47 |
| 43 bool AutoplayUmaHelper::operator==(const EventListener& other) const { | 48 bool AutoplayUmaHelper::operator==(const EventListener& other) const { |
| 44 return this == &other; | 49 return this == &other; |
| 45 } | 50 } |
| 46 | 51 |
| 52 void AutoplayUmaHelper::OnLoadStarted() { |
| 53 if (element_->GetLoadType() == WebMediaPlayer::kLoadTypeURL) |
| 54 load_start_time_ms_ = MonotonicallyIncreasingTimeMS(); |
| 55 } |
| 56 |
| 47 void AutoplayUmaHelper::OnAutoplayInitiated(AutoplaySource source) { | 57 void AutoplayUmaHelper::OnAutoplayInitiated(AutoplaySource source) { |
| 58 int32_t autoplay_wait_time_ms = -1; |
| 59 if (load_start_time_ms_ != 0.0) { |
| 60 autoplay_wait_time_ms = static_cast<int32_t>( |
| 61 std::min<int64_t>(MonotonicallyIncreasingTimeMS() - load_start_time_ms_, |
| 62 std::numeric_limits<int32_t>::max())); |
| 63 } |
| 48 DEFINE_STATIC_LOCAL(EnumerationHistogram, video_histogram, | 64 DEFINE_STATIC_LOCAL(EnumerationHistogram, video_histogram, |
| 49 ("Media.Video.Autoplay", | 65 ("Media.Video.Autoplay", |
| 50 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); | 66 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); |
| 51 DEFINE_STATIC_LOCAL(EnumerationHistogram, muted_video_histogram, | 67 DEFINE_STATIC_LOCAL(EnumerationHistogram, muted_video_histogram, |
| 52 ("Media.Video.Autoplay.Muted", | 68 ("Media.Video.Autoplay.Muted", |
| 53 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); | 69 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); |
| 54 DEFINE_STATIC_LOCAL(EnumerationHistogram, audio_histogram, | 70 DEFINE_STATIC_LOCAL(EnumerationHistogram, audio_histogram, |
| 55 ("Media.Audio.Autoplay", | 71 ("Media.Audio.Autoplay", |
| 56 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); | 72 static_cast<int>(AutoplaySource::kNumberOfUmaSources))); |
| 57 DEFINE_STATIC_LOCAL( | 73 DEFINE_STATIC_LOCAL( |
| 58 EnumerationHistogram, blocked_muted_video_histogram, | 74 EnumerationHistogram, blocked_muted_video_histogram, |
| 59 ("Media.Video.Autoplay.Muted.Blocked", kAutoplayBlockedReasonMax)); | 75 ("Media.Video.Autoplay.Muted.Blocked", kAutoplayBlockedReasonMax)); |
| 60 | 76 |
| 77 DEFINE_STATIC_LOCAL(CustomCountHistogram, wait_time_video_attrib_histogram, |
| 78 ("Media.Video.Autoplay.Attribute.WaitTime", 1, |
| 79 kMaxWaitTimeUmaMS, kWaitTimeBucketCount)); |
| 80 DEFINE_STATIC_LOCAL(CustomCountHistogram, wait_time_audio_attrib_histogram, |
| 81 ("Media.Audio.Autoplay.Attribute.WaitTime", 1, |
| 82 kMaxWaitTimeUmaMS, kWaitTimeBucketCount)); |
| 83 DEFINE_STATIC_LOCAL(CustomCountHistogram, wait_time_video_play_histogram, |
| 84 ("Media.Video.Autoplay.PlayMethod.WaitTime", 1, |
| 85 kMaxWaitTimeUmaMS, kWaitTimeBucketCount)); |
| 86 DEFINE_STATIC_LOCAL(CustomCountHistogram, wait_time_audio_play_histogram, |
| 87 ("Media.Audio.Autoplay.PlayMethod.WaitTime", 1, |
| 88 kMaxWaitTimeUmaMS, kWaitTimeBucketCount)); |
| 89 |
| 61 // Autoplay already initiated | 90 // Autoplay already initiated |
| 62 if (sources_.count(source)) | 91 if (sources_.count(source)) |
| 63 return; | 92 return; |
| 64 | 93 |
| 65 sources_.insert(source); | 94 sources_.insert(source); |
| 66 | 95 |
| 67 // Record the source. | 96 // Record the source. |
| 68 if (element_->IsHTMLVideoElement()) { | 97 if (element_->IsHTMLVideoElement()) { |
| 69 video_histogram.Count(static_cast<int>(source)); | 98 video_histogram.Count(static_cast<int>(source)); |
| 70 if (element_->muted()) | 99 if (element_->muted()) |
| 71 muted_video_histogram.Count(static_cast<int>(source)); | 100 muted_video_histogram.Count(static_cast<int>(source)); |
| 101 if (autoplay_wait_time_ms >= 0) { |
| 102 if (source == AutoplaySource::kAttribute) { |
| 103 wait_time_video_attrib_histogram.Count(autoplay_wait_time_ms); |
| 104 } else if (source == AutoplaySource::kMethod) { |
| 105 wait_time_video_play_histogram.Count(autoplay_wait_time_ms); |
| 106 } |
| 107 } |
| 72 } else { | 108 } else { |
| 73 audio_histogram.Count(static_cast<int>(source)); | 109 audio_histogram.Count(static_cast<int>(source)); |
| 110 if (autoplay_wait_time_ms >= 0) { |
| 111 if (source == AutoplaySource::kAttribute) { |
| 112 wait_time_audio_attrib_histogram.Count(autoplay_wait_time_ms); |
| 113 } else if (source == AutoplaySource::kMethod) { |
| 114 wait_time_audio_play_histogram.Count(autoplay_wait_time_ms); |
| 115 } |
| 116 } |
| 74 } | 117 } |
| 75 | 118 |
| 76 // Record dual source. | 119 // Record dual source. |
| 77 if (sources_.size() == | 120 if (sources_.size() == |
| 78 static_cast<size_t>(AutoplaySource::kNumberOfSources)) { | 121 static_cast<size_t>(AutoplaySource::kNumberOfSources)) { |
| 79 if (element_->IsHTMLVideoElement()) { | 122 if (element_->IsHTMLVideoElement()) { |
| 80 video_histogram.Count(static_cast<int>(AutoplaySource::kDualSource)); | 123 video_histogram.Count(static_cast<int>(AutoplaySource::kDualSource)); |
| 81 if (element_->muted()) | 124 if (element_->muted()) |
| 82 muted_video_histogram.Count( | 125 muted_video_histogram.Count( |
| 83 static_cast<int>(AutoplaySource::kDualSource)); | 126 static_cast<int>(AutoplaySource::kDualSource)); |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } else { | 293 } else { |
| 251 muted_video_autoplay_offscreen_start_time_ms_ = | 294 muted_video_autoplay_offscreen_start_time_ms_ = |
| 252 static_cast<int64_t>(MonotonicallyIncreasingTimeMS()); | 295 static_cast<int64_t>(MonotonicallyIncreasingTimeMS()); |
| 253 } | 296 } |
| 254 | 297 |
| 255 is_visible_ = is_visible; | 298 is_visible_ = is_visible; |
| 256 } | 299 } |
| 257 | 300 |
| 258 void AutoplayUmaHelper::handleEvent(ExecutionContext* execution_context, | 301 void AutoplayUmaHelper::handleEvent(ExecutionContext* execution_context, |
| 259 Event* event) { | 302 Event* event) { |
| 260 if (event->type() == EventTypeNames::playing) | 303 if (event->type() == EventTypeNames::loadstart) |
| 304 OnLoadStarted(); |
| 305 else if (event->type() == EventTypeNames::playing) |
| 261 HandlePlayingEvent(); | 306 HandlePlayingEvent(); |
| 262 else if (event->type() == EventTypeNames::pause) | 307 else if (event->type() == EventTypeNames::pause) |
| 263 HandlePauseEvent(); | 308 HandlePauseEvent(); |
| 264 else | 309 else |
| 265 NOTREACHED(); | 310 NOTREACHED(); |
| 266 } | 311 } |
| 267 | 312 |
| 268 void AutoplayUmaHelper::HandlePlayingEvent() { | 313 void AutoplayUmaHelper::HandlePlayingEvent() { |
| 269 MaybeStartRecordingMutedVideoPlayMethodBecomeVisible(); | 314 MaybeStartRecordingMutedVideoPlayMethodBecomeVisible(); |
| 270 MaybeStartRecordingMutedVideoOffscreenDuration(); | 315 MaybeStartRecordingMutedVideoOffscreenDuration(); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 | 444 |
| 400 DEFINE_TRACE(AutoplayUmaHelper) { | 445 DEFINE_TRACE(AutoplayUmaHelper) { |
| 401 EventListener::Trace(visitor); | 446 EventListener::Trace(visitor); |
| 402 ContextLifecycleObserver::Trace(visitor); | 447 ContextLifecycleObserver::Trace(visitor); |
| 403 visitor->Trace(element_); | 448 visitor->Trace(element_); |
| 404 visitor->Trace(muted_video_play_method_visibility_observer_); | 449 visitor->Trace(muted_video_play_method_visibility_observer_); |
| 405 visitor->Trace(muted_video_offscreen_duration_visibility_observer_); | 450 visitor->Trace(muted_video_offscreen_duration_visibility_observer_); |
| 406 } | 451 } |
| 407 | 452 |
| 408 } // namespace blink | 453 } // namespace blink |
| OLD | NEW |