| 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/AutoplayUmaHelper.h" | 5 #include "core/html/AutoplayUmaHelper.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/ElementVisibilityObserver.h" | |
| 9 #include "core/events/Event.h" | 8 #include "core/events/Event.h" |
| 10 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
| 11 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
| 12 #include "core/html/HTMLMediaElement.h" | 11 #include "core/html/HTMLMediaElement.h" |
| 13 #include "platform/Histogram.h" | 12 #include "platform/Histogram.h" |
| 14 #include "wtf/CurrentTime.h" | 13 #include "wtf/CurrentTime.h" |
| 15 | 14 |
| 16 namespace blink { | 15 namespace blink { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 const int32_t maxOffscreenDurationUmaMS = 60 * 60 * 1000; | 19 const int32_t maxOffscreenDurationUmaMS = 60 * 60 * 1000; |
| 21 const int32_t offscreenDurationUmaBucketCount = 50; | 20 const int32_t offscreenDurationUmaBucketCount = 50; |
| 22 | 21 |
| 23 } // namespace | 22 } // namespace |
| 24 | 23 |
| 25 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element) { | 24 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element) { |
| 26 return new AutoplayUmaHelper(element); | 25 return new AutoplayUmaHelper(element); |
| 27 } | 26 } |
| 28 | 27 |
| 29 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) | 28 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) |
| 30 : EventListener(CPPEventListenerType), | 29 : EventListener(CPPEventListenerType), |
| 31 m_source(AutoplaySource::NumberOfSources), | 30 m_source(AutoplaySource::NumberOfSources), |
| 32 m_element(element), | 31 m_element(element), |
| 33 m_mutedVideoPlayMethodVisibilityObserver(nullptr), | |
| 34 m_mutedVideoAutoplayOffscreenStartTimeMS(0), | 32 m_mutedVideoAutoplayOffscreenStartTimeMS(0), |
| 35 m_mutedVideoAutoplayOffscreenDurationMS(0), | 33 m_mutedVideoAutoplayOffscreenDurationMS(0), |
| 36 m_isVisible(false), | 34 m_isVisible(false) {} |
| 37 m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) {} | |
| 38 | 35 |
| 39 AutoplayUmaHelper::~AutoplayUmaHelper() = default; | 36 AutoplayUmaHelper::~AutoplayUmaHelper() = default; |
| 40 | 37 |
| 41 bool AutoplayUmaHelper::operator==(const EventListener& other) const { | 38 bool AutoplayUmaHelper::operator==(const EventListener& other) const { |
| 42 return this == &other; | 39 return this == &other; |
| 43 } | 40 } |
| 44 | 41 |
| 45 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source) { | 42 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source) { |
| 46 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, | 43 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, |
| 47 ("Media.Video.Autoplay", | 44 ("Media.Video.Autoplay", |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 return; | 106 return; |
| 110 | 107 |
| 111 if (oldDocument.domWindow()) | 108 if (oldDocument.domWindow()) |
| 112 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this, | 109 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this, |
| 113 false); | 110 false); |
| 114 if (m_element->document().domWindow()) | 111 if (m_element->document().domWindow()) |
| 115 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, | 112 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, |
| 116 this, false); | 113 this, false); |
| 117 } | 114 } |
| 118 | 115 |
| 119 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( | 116 void AutoplayUmaHelper::visibilityMaybeChangedforMutedVideo(bool isVisible) { |
| 120 bool isVisible) { | |
| 121 if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver) | |
| 122 return; | |
| 123 | |
| 124 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); | |
| 125 } | |
| 126 | |
| 127 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoOffscreenDuration( | |
| 128 bool isVisible) { | |
| 129 if (isVisible == m_isVisible) | 117 if (isVisible == m_isVisible) |
| 130 return; | 118 return; |
| 131 | 119 |
| 132 if (isVisible) | 120 m_isVisible = isVisible; |
| 133 m_mutedVideoAutoplayOffscreenDurationMS += | |
| 134 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - | |
| 135 m_mutedVideoAutoplayOffscreenStartTimeMS; | |
| 136 else | |
| 137 m_mutedVideoAutoplayOffscreenStartTimeMS = | |
| 138 static_cast<int64_t>(monotonicallyIncreasingTimeMS()); | |
| 139 | 121 |
| 140 m_isVisible = isVisible; | 122 if (m_isObservingVisibilityOfMutedVideoOffscreenDuration && isVisible) |
| 123 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); |
| 124 |
| 125 if (m_isObservingVisibilityOfMutedVideoOffscreenDuration) { |
| 126 if (isVisible) { |
| 127 m_mutedVideoAutoplayOffscreenDurationMS += |
| 128 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - |
| 129 m_mutedVideoAutoplayOffscreenStartTimeMS; |
| 130 } else { |
| 131 m_mutedVideoAutoplayOffscreenStartTimeMS = |
| 132 static_cast<int64_t>(monotonicallyIncreasingTimeMS()); |
| 133 } |
| 134 } |
| 141 } | 135 } |
| 142 | 136 |
| 143 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, | 137 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, |
| 144 Event* event) { | 138 Event* event) { |
| 145 if (event->type() == EventTypeNames::playing) | 139 if (event->type() == EventTypeNames::playing) |
| 146 handlePlayingEvent(); | 140 handlePlayingEvent(); |
| 147 else if (event->type() == EventTypeNames::pause) | 141 else if (event->type() == EventTypeNames::pause) |
| 148 handlePauseEvent(); | 142 handlePauseEvent(); |
| 149 else if (event->type() == EventTypeNames::unload) | 143 else if (event->type() == EventTypeNames::unload) |
| 150 handleUnloadEvent(); | 144 handleUnloadEvent(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 166 void AutoplayUmaHelper::handleUnloadEvent() { | 160 void AutoplayUmaHelper::handleUnloadEvent() { |
| 167 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false); | 161 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false); |
| 168 maybeStopRecordingMutedVideoOffscreenDuration(); | 162 maybeStopRecordingMutedVideoOffscreenDuration(); |
| 169 } | 163 } |
| 170 | 164 |
| 171 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() { | 165 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() { |
| 172 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() || | 166 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() || |
| 173 !m_element->muted()) | 167 !m_element->muted()) |
| 174 return; | 168 return; |
| 175 | 169 |
| 176 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver( | 170 m_isObservingMutedVideoPlayMethodBecomeVisible = true; |
| 177 m_element, | |
| 178 WTF::bind(&AutoplayUmaHelper:: | |
| 179 onVisibilityChangedForMutedVideoPlayMethodBecomeVisible, | |
| 180 wrapWeakPersistent(this))); | |
| 181 m_mutedVideoPlayMethodVisibilityObserver->start(); | |
| 182 if (m_element->document().domWindow()) | 171 if (m_element->document().domWindow()) |
| 183 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, | 172 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, |
| 184 this, false); | 173 this, false); |
| 185 } | 174 } |
| 186 | 175 |
| 187 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible( | 176 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible( |
| 188 bool visible) { | 177 bool visible) { |
| 189 if (!m_mutedVideoPlayMethodVisibilityObserver) | 178 if (!m_isObservingMutedVideoPlayMethodBecomeVisible) |
| 190 return; | 179 return; |
| 191 | 180 |
| 192 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, | 181 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, |
| 193 ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible")); | 182 ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible")); |
| 194 | 183 |
| 195 histogram.count(visible); | 184 histogram.count(visible); |
| 196 m_mutedVideoPlayMethodVisibilityObserver->stop(); | 185 m_isObservingMutedVideoPlayMethodBecomeVisible = false; |
| 197 m_mutedVideoPlayMethodVisibilityObserver = nullptr; | |
| 198 maybeUnregisterUnloadListener(); | 186 maybeUnregisterUnloadListener(); |
| 199 } | 187 } |
| 200 | 188 |
| 201 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() { | 189 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() { |
| 202 if (!m_element->isHTMLVideoElement() || !m_element->muted()) | 190 if (!m_element->isHTMLVideoElement() || !m_element->muted()) |
| 203 return; | 191 return; |
| 204 | 192 |
| 205 // Start recording muted video playing offscreen duration. | 193 // Start recording muted video playing offscreen duration. |
| 206 m_mutedVideoAutoplayOffscreenStartTimeMS = | 194 m_mutedVideoAutoplayOffscreenStartTimeMS = |
| 207 static_cast<int64_t>(monotonicallyIncreasingTimeMS()); | 195 static_cast<int64_t>(monotonicallyIncreasingTimeMS()); |
| 208 m_isVisible = false; | 196 m_isVisible = false; |
| 209 m_mutedVideoOffscreenDurationVisibilityObserver = | 197 m_isObservingVisibilityOfMutedVideoOffscreenDuration = true; |
| 210 new ElementVisibilityObserver( | |
| 211 m_element, | |
| 212 WTF::bind(&AutoplayUmaHelper:: | |
| 213 onVisibilityChangedForMutedVideoOffscreenDuration, | |
| 214 wrapWeakPersistent(this))); | |
| 215 m_mutedVideoOffscreenDurationVisibilityObserver->start(); | |
| 216 m_element->addEventListener(EventTypeNames::pause, this, false); | 198 m_element->addEventListener(EventTypeNames::pause, this, false); |
| 217 if (m_element->document().domWindow()) | 199 if (m_element->document().domWindow()) |
| 218 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, | 200 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, |
| 219 this, false); | 201 this, false); |
| 220 } | 202 } |
| 221 | 203 |
| 222 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() { | 204 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() { |
| 223 if (!m_mutedVideoOffscreenDurationVisibilityObserver) | 205 if (!m_isObservingVisibilityOfMutedVideoOffscreenDuration) |
| 224 return; | 206 return; |
| 225 | 207 |
| 226 if (!m_isVisible) | 208 if (!m_isVisible) |
| 227 m_mutedVideoAutoplayOffscreenDurationMS += | 209 m_mutedVideoAutoplayOffscreenDurationMS += |
| 228 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - | 210 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - |
| 229 m_mutedVideoAutoplayOffscreenStartTimeMS; | 211 m_mutedVideoAutoplayOffscreenStartTimeMS; |
| 230 | 212 |
| 231 // Since histograms uses int32_t, the duration needs to be limited to | 213 // Since histograms uses int32_t, the duration needs to be limited to |
| 232 // std::numeric_limits<int32_t>::max(). | 214 // std::numeric_limits<int32_t>::max(). |
| 233 int32_t boundedTime = static_cast<int32_t>( | 215 int32_t boundedTime = static_cast<int32_t>( |
| 234 std::min<int64_t>(m_mutedVideoAutoplayOffscreenDurationMS, | 216 std::min<int64_t>(m_mutedVideoAutoplayOffscreenDurationMS, |
| 235 std::numeric_limits<int32_t>::max())); | 217 std::numeric_limits<int32_t>::max())); |
| 236 | 218 |
| 237 if (m_source == AutoplaySource::Attribute) { | 219 if (m_source == AutoplaySource::Attribute) { |
| 238 DEFINE_STATIC_LOCAL( | 220 DEFINE_STATIC_LOCAL( |
| 239 CustomCountHistogram, durationHistogram, | 221 CustomCountHistogram, durationHistogram, |
| 240 ("Media.Video.Autoplay.Muted.Attribute.OffscreenDuration", 1, | 222 ("Media.Video.Autoplay.Muted.Attribute.OffscreenDuration", 1, |
| 241 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); | 223 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); |
| 242 durationHistogram.count(boundedTime); | 224 durationHistogram.count(boundedTime); |
| 243 } else { | 225 } else { |
| 244 DEFINE_STATIC_LOCAL( | 226 DEFINE_STATIC_LOCAL( |
| 245 CustomCountHistogram, durationHistogram, | 227 CustomCountHistogram, durationHistogram, |
| 246 ("Media.Video.Autoplay.Muted.PlayMethod.OffscreenDuration", 1, | 228 ("Media.Video.Autoplay.Muted.PlayMethod.OffscreenDuration", 1, |
| 247 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); | 229 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); |
| 248 durationHistogram.count(boundedTime); | 230 durationHistogram.count(boundedTime); |
| 249 } | 231 } |
| 250 m_mutedVideoOffscreenDurationVisibilityObserver->stop(); | 232 m_isObservingVisibilityOfMutedVideoOffscreenDuration = false; |
| 251 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr; | |
| 252 m_mutedVideoAutoplayOffscreenDurationMS = 0; | 233 m_mutedVideoAutoplayOffscreenDurationMS = 0; |
| 253 m_element->removeEventListener(EventTypeNames::pause, this, false); | 234 m_element->removeEventListener(EventTypeNames::pause, this, false); |
| 254 maybeUnregisterUnloadListener(); | 235 maybeUnregisterUnloadListener(); |
| 255 } | 236 } |
| 256 | 237 |
| 257 void AutoplayUmaHelper::maybeUnregisterUnloadListener() { | 238 void AutoplayUmaHelper::maybeUnregisterUnloadListener() { |
| 258 if (!shouldListenToUnloadEvent() && m_element->document().domWindow()) | 239 if (!shouldListenToUnloadEvent() && m_element->document().domWindow()) |
| 259 m_element->document().domWindow()->removeEventListener( | 240 m_element->document().domWindow()->removeEventListener( |
| 260 EventTypeNames::unload, this, false); | 241 EventTypeNames::unload, this, false); |
| 261 } | 242 } |
| 262 | 243 |
| 263 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const { | 244 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const { |
| 264 return m_mutedVideoPlayMethodVisibilityObserver || | 245 return m_isObservingMutedVideoPlayMethodBecomeVisible || |
| 265 m_mutedVideoOffscreenDurationVisibilityObserver; | 246 m_isObservingVisibilityOfMutedVideoOffscreenDuration; |
| 266 } | 247 } |
| 267 | 248 |
| 268 DEFINE_TRACE(AutoplayUmaHelper) { | 249 DEFINE_TRACE(AutoplayUmaHelper) { |
| 269 EventListener::trace(visitor); | 250 EventListener::trace(visitor); |
| 270 visitor->trace(m_element); | 251 visitor->trace(m_element); |
| 271 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver); | |
| 272 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver); | |
| 273 } | 252 } |
| 274 | 253 |
| 275 } // namespace blink | 254 } // namespace blink |
| OLD | NEW |