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

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: addressed more comments Created 4 years, 4 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 = 3600000;
Ilya Sherman 2016/08/19 23:15:33 nit: This constant value is a bit hard to parse --
Zhiqiang Zhang (Slow) 2016/08/22 09:40:24 Done.
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 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
111 }
112 } 138 }
113 139
114 void AutoplayUmaHelper::handlePlayingEvent() 140 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible()
115 { 141 {
116 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) { 142 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() | | !m_element->muted())
117 if (!m_videoMutedPlayMethodVisibilityObserver) { 143 return;
118 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapWeakPersistent(this))); 144
119 m_videoMutedPlayMethodVisibilityObserver->start(); 145 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver(m_e lement, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethod BecomeVisible, wrapWeakPersistent(this)));
120 m_element->document().domWindow()->addEventListener(EventTypeNames:: unload, this, false); 146 m_mutedVideoPlayMethodVisibilityObserver->start();
121 } 147 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
148 }
149
150 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible(bool visible)
151 {
152 if (!m_mutedVideoPlayMethodVisibilityObserver)
153 return;
154
155 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
156
157 histogram.count(visible);
158 m_mutedVideoPlayMethodVisibilityObserver->stop();
159 m_mutedVideoPlayMethodVisibilityObserver = nullptr;
160 maybeUnregisterUnloadListener();
161 }
162
163 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration()
164 {
165 if (!m_element->isHTMLVideoElement() || !m_element->muted())
166 return;
167
168 // Start recording muted video playing offscreen duration.
169 m_mutedVideoAutoplayOffscreenStartTimeMS = static_cast<int64_t>(monotonicall yIncreasingTimeMS());
170 m_isVisible = false;
171 m_mutedVideoOffscreenDurationVisibilityObserver = new ElementVisibilityObser ver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoOff screenDuration, wrapWeakPersistent(this)));
172 m_mutedVideoOffscreenDurationVisibilityObserver->start();
173 m_element->addEventListener(EventTypeNames::pause, this, false);
174 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
175 }
176
177 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration()
178 {
179 if (!m_mutedVideoOffscreenDurationVisibilityObserver)
180 return;
181
182 if (!m_isVisible)
183 m_mutedVideoAutoplayOffscreenDurationMS += static_cast<int64_t>(monotoni callyIncreasingTimeMS()) - m_mutedVideoAutoplayOffscreenStartTimeMS;
184
185 // Since histograms uses int32_t, the duration needs to be limited to std::n umeric_limits<int32_t>::max().
Zhiqiang Zhang (Slow) 2016/08/19 10:50:49 Now the time is stored as int64_t inside AutoplayU
186 int32_t boundedTime = static_cast<int32_t>(std::min<int64_t>(m_mutedVideoAut oplayOffscreenDurationMS, std::numeric_limits<int32_t>::max()));
Ilya Sherman 2016/08/19 23:15:33 Can you use base::saturated_cast?
Zhiqiang Zhang (Slow) 2016/08/22 09:40:24 I guess not. Using base:: is not allowed in WebKit
187
188 if (m_source == AutoplaySource::Attribute) {
189 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.Attribute.OffscreenDuration", 1, maxOffscreenDurationUmaMS, of fscreenDurationUmaBucketCount));
190 durationHistogram.count(boundedTime);
191 } else {
192 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.PlayMethod.OffscreenDuration", 1, maxOffscreenDurationUmaMS, o ffscreenDurationUmaBucketCount));
193 durationHistogram.count(boundedTime);
122 } 194 }
123 m_element->removeEventListener(EventTypeNames::playing, this, false); 195 m_mutedVideoOffscreenDurationVisibilityObserver->stop();
196 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr;
197 m_mutedVideoAutoplayOffscreenDurationMS = 0;
198 m_element->removeEventListener(EventTypeNames::pause, this, false);
199 maybeUnregisterUnloadListener();
200 }
201
202 void AutoplayUmaHelper::maybeUnregisterUnloadListener()
203 {
204 if (!shouldListenToUnloadEvent() && m_element && m_element->document().domWi ndow())
205 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
206 }
207
208 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const
209 {
210 return m_mutedVideoPlayMethodVisibilityObserver || m_mutedVideoOffscreenDura tionVisibilityObserver;
124 } 211 }
125 212
126 DEFINE_TRACE(AutoplayUmaHelper) 213 DEFINE_TRACE(AutoplayUmaHelper)
127 { 214 {
128 EventListener::trace(visitor); 215 EventListener::trace(visitor);
129 visitor->trace(m_element); 216 visitor->trace(m_element);
130 visitor->trace(m_videoMutedPlayMethodVisibilityObserver); 217 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver);
218 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver);
131 } 219 }
132 220
133 } // namespace blink 221 } // 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