Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp

Issue 2101613002: Record the offscreen playing duration of autoplaying muted videos (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix layout test expectation Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "core/html/HTMLMediaElement.h" 11 #include "core/html/HTMLMediaElement.h"
12 #include "platform/Histogram.h" 12 #include "platform/Histogram.h"
13 #include "wtf/CurrentTime.h"
13 14
14 namespace blink { 15 namespace blink {
15 16
16 namespace { 17 namespace {
17 18
18 void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible) 19 const int32_t maxOffscreenDurationUmaMS = 60 * 60 * 1000;
19 { 20 const int32_t offscreenDurationUmaBucketCount = 50;
20 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
21 histogram.count(visible);
22 }
23 21
24 } // namespace 22 } // namespace
25 23
26 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element) 24 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
27 { 25 {
28 return new AutoplayUmaHelper(element); 26 return new AutoplayUmaHelper(element);
29 } 27 }
30 28
31 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) 29 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
32 : EventListener(CPPEventListenerType) 30 : EventListener(CPPEventListenerType)
33 , m_source(AutoplaySource::NumberOfSources) 31 , m_source(AutoplaySource::NumberOfSources)
34 , m_element(element) 32 , m_element(element)
35 , m_videoMutedPlayMethodVisibilityObserver(nullptr) { } 33 , m_mutedVideoPlayMethodVisibilityObserver(nullptr)
34 , m_mutedVideoAutoplayOffscreenStartTimeMS(0)
35 , m_mutedVideoAutoplayOffscreenDurationMS(0)
36 , m_isVisible(false)
37 , m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) { }
36 38
37 AutoplayUmaHelper::~AutoplayUmaHelper() = default; 39 AutoplayUmaHelper::~AutoplayUmaHelper() = default;
38 40
39 bool AutoplayUmaHelper::operator==(const EventListener& other) const 41 bool AutoplayUmaHelper::operator==(const EventListener& other) const
40 { 42 {
41 return this == &other; 43 return this == &other;
42 } 44 }
43 45
44 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source) 46 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
45 { 47 {
46 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 48 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
47 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources))); 49 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources)));
48 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 50 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
49 51
52 // Autoplay already initiated
53 // TODO(zqzhang): how about having autoplay attribute and calling `play()` i n the script?
54 if (m_source != AutoplaySource::NumberOfSources)
55 return;
56
50 m_source = source; 57 m_source = source;
51 58
52 if (m_element->isHTMLVideoElement()) { 59 if (m_element->isHTMLVideoElement()) {
53 videoHistogram.count(static_cast<int>(m_source)); 60 videoHistogram.count(static_cast<int>(m_source));
54 if (m_element->muted()) 61 if (m_element->muted())
55 mutedVideoHistogram.count(static_cast<int>(m_source)); 62 mutedVideoHistogram.count(static_cast<int>(m_source));
56 } else { 63 } else {
57 audioHistogram.count(static_cast<int>(m_source)); 64 audioHistogram.count(static_cast<int>(m_source));
58 } 65 }
59 66
60 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) 67 m_element->addEventListener(EventTypeNames::playing, this, false);
61 m_element->addEventListener(EventTypeNames::playing, this, false);
62 } 68 }
63 69
64 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus) 70 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus)
65 { 71 {
66 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus))); 72 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus)));
67 73
68 autoplayUnmuteHistogram.count(static_cast<int>(status)); 74 autoplayUnmuteHistogram.count(static_cast<int>(status));
69 } 75 }
70 76
71 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) 77 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument)
72 { 78 {
73 if (!m_videoMutedPlayMethodVisibilityObserver) 79 if (!shouldListenToUnloadEvent())
74 return; 80 return;
75 81
76 if (oldDocument.domWindow()) 82 if (oldDocument.domWindow())
77 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, thi s, false); 83 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, thi s, false);
78 if (m_element && m_element->document().domWindow()) 84 if (m_element && m_element->document().domWindow())
79 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false); 85 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false);
80 } 86 }
81 87
82 void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisibl e) 88 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( bool isVisible)
83 { 89 {
84 if (!isVisible || !m_videoMutedPlayMethodVisibilityObserver) 90 if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver)
85 return; 91 return;
86 92
87 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true); 93 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true);
88 m_videoMutedPlayMethodVisibilityObserver->stop(); 94 }
89 m_videoMutedPlayMethodVisibilityObserver = nullptr; 95
90 if (m_element && m_element->document().domWindow()) 96 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoOffscreenDuration(bool i sVisible)
91 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false); 97 {
98 if (isVisible == m_isVisible)
99 return;
100
101 if (isVisible)
102 m_mutedVideoAutoplayOffscreenDurationMS += static_cast<int64_t>(monotoni callyIncreasingTimeMS()) - m_mutedVideoAutoplayOffscreenStartTimeMS;
103 else
104 m_mutedVideoAutoplayOffscreenStartTimeMS = static_cast<int64_t>(monotoni callyIncreasingTimeMS());
105
106 m_isVisible = isVisible;
92 } 107 }
93 108
94 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent) 109 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent)
95 { 110 {
96 if (event->type() == EventTypeNames::playing) 111 if (event->type() == EventTypeNames::playing)
97 handlePlayingEvent(); 112 handlePlayingEvent();
113 else if (event->type() == EventTypeNames::pause)
114 handlePauseEvent();
98 else if (event->type() == EventTypeNames::unload) 115 else if (event->type() == EventTypeNames::unload)
99 handleUnloadEvent(); 116 handleUnloadEvent();
100 else 117 else
101 NOTREACHED(); 118 NOTREACHED();
102 } 119 }
103 120
121 void AutoplayUmaHelper::handlePlayingEvent()
122 {
123 maybeStartRecordingMutedVideoPlayMethodBecomeVisible();
124 maybeStartRecordingMutedVideoOffscreenDuration();
125
126 m_element->removeEventListener(EventTypeNames::playing, this, false);
127 }
128
129 void AutoplayUmaHelper::handlePauseEvent()
130 {
131 maybeStopRecordingMutedVideoOffscreenDuration();
132 }
133
104 void AutoplayUmaHelper::handleUnloadEvent() 134 void AutoplayUmaHelper::handleUnloadEvent()
105 { 135 {
106 if (m_videoMutedPlayMethodVisibilityObserver) { 136 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false);
107 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false); 137 maybeStopRecordingMutedVideoOffscreenDuration();
108 m_videoMutedPlayMethodVisibilityObserver->stop();
109 m_videoMutedPlayMethodVisibilityObserver = nullptr;
110 if (m_element && m_element->document().domWindow())
111 m_element->document().domWindow()->removeEventListener(EventTypeName s::unload, this, false);
112 }
113 } 138 }
114 139
115 void AutoplayUmaHelper::handlePlayingEvent() 140 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible()
116 { 141 {
117 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) { 142 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() | | !m_element->muted())
118 if (!m_videoMutedPlayMethodVisibilityObserver) { 143 return;
119 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapWeakPersistent(this))); 144
120 m_videoMutedPlayMethodVisibilityObserver->start(); 145 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver(m_e lement, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethod BecomeVisible, wrapWeakPersistent(this)));
121 if (m_element && m_element->document().domWindow()) 146 m_mutedVideoPlayMethodVisibilityObserver->start();
122 m_element->document().domWindow()->addEventListener(EventTypeNam es::unload, this, false); 147 if (m_element->document().domWindow())
123 } 148 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false);
149 }
150
151 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible(bool visible)
152 {
153 if (!m_mutedVideoPlayMethodVisibilityObserver)
154 return;
155
156 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
157
158 histogram.count(visible);
159 m_mutedVideoPlayMethodVisibilityObserver->stop();
160 m_mutedVideoPlayMethodVisibilityObserver = nullptr;
161 maybeUnregisterUnloadListener();
162 }
163
164 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration()
165 {
166 if (!m_element->isHTMLVideoElement() || !m_element->muted())
167 return;
168
169 // Start recording muted video playing offscreen duration.
170 m_mutedVideoAutoplayOffscreenStartTimeMS = static_cast<int64_t>(monotonicall yIncreasingTimeMS());
171 m_isVisible = false;
172 m_mutedVideoOffscreenDurationVisibilityObserver = new ElementVisibilityObser ver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoOff screenDuration, wrapWeakPersistent(this)));
173 m_mutedVideoOffscreenDurationVisibilityObserver->start();
174 m_element->addEventListener(EventTypeNames::pause, this, false);
175 if (m_element->document().domWindow())
176 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false);
177 }
178
179 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration()
180 {
181 if (!m_mutedVideoOffscreenDurationVisibilityObserver)
182 return;
183
184 if (!m_isVisible)
185 m_mutedVideoAutoplayOffscreenDurationMS += static_cast<int64_t>(monotoni callyIncreasingTimeMS()) - m_mutedVideoAutoplayOffscreenStartTimeMS;
186
187 // Since histograms uses int32_t, the duration needs to be limited to std::n umeric_limits<int32_t>::max().
188 int32_t boundedTime = static_cast<int32_t>(std::min<int64_t>(m_mutedVideoAut oplayOffscreenDurationMS, std::numeric_limits<int32_t>::max()));
189
190 if (m_source == AutoplaySource::Attribute) {
191 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.Attribute.OffscreenDuration", 1, maxOffscreenDurationUmaMS, of fscreenDurationUmaBucketCount));
192 durationHistogram.count(boundedTime);
193 } else {
194 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.PlayMethod.OffscreenDuration", 1, maxOffscreenDurationUmaMS, o ffscreenDurationUmaBucketCount));
195 durationHistogram.count(boundedTime);
124 } 196 }
125 m_element->removeEventListener(EventTypeNames::playing, this, false); 197 m_mutedVideoOffscreenDurationVisibilityObserver->stop();
198 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr;
199 m_mutedVideoAutoplayOffscreenDurationMS = 0;
200 m_element->removeEventListener(EventTypeNames::pause, this, false);
201 maybeUnregisterUnloadListener();
202 }
203
204 void AutoplayUmaHelper::maybeUnregisterUnloadListener()
205 {
206 if (!shouldListenToUnloadEvent() && m_element && m_element->document().domWi ndow())
207 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
208 }
209
210 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const
211 {
212 return m_mutedVideoPlayMethodVisibilityObserver || m_mutedVideoOffscreenDura tionVisibilityObserver;
126 } 213 }
127 214
128 DEFINE_TRACE(AutoplayUmaHelper) 215 DEFINE_TRACE(AutoplayUmaHelper)
129 { 216 {
130 EventListener::trace(visitor); 217 EventListener::trace(visitor);
131 visitor->trace(m_element); 218 visitor->trace(m_element);
132 visitor->trace(m_videoMutedPlayMethodVisibilityObserver); 219 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver);
220 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver);
133 } 221 }
134 222
135 } // namespace blink 223 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/AutoplayUmaHelper.h ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698