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