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

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

Issue 2266253002: Revert "Record the offscreen playing duration of autoplaying muted videos" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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"
14 13
15 namespace blink { 14 namespace blink {
16 15
17 namespace { 16 namespace {
18 17
19 const int32_t maxOffscreenDurationUmaMS = 60 * 60 * 1000; 18 void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible)
20 const int32_t offscreenDurationUmaBucketCount = 50; 19 {
20 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
21 histogram.count(visible);
22 }
21 23
22 } // namespace 24 } // namespace
23 25
24 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element) 26 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
25 { 27 {
26 return new AutoplayUmaHelper(element); 28 return new AutoplayUmaHelper(element);
27 } 29 }
28 30
29 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) 31 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
30 : EventListener(CPPEventListenerType) 32 : EventListener(CPPEventListenerType)
31 , m_source(AutoplaySource::NumberOfSources) 33 , m_source(AutoplaySource::NumberOfSources)
32 , m_element(element) 34 , m_element(element)
33 , m_mutedVideoPlayMethodVisibilityObserver(nullptr) 35 , m_videoMutedPlayMethodVisibilityObserver(nullptr) { }
34 , m_mutedVideoAutoplayOffscreenStartTimeMS(0)
35 , m_mutedVideoAutoplayOffscreenDurationMS(0)
36 , 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 { 40 {
43 return this == &other; 41 return this == &other;
44 } 42 }
45 43
46 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source) 44 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
47 { 45 {
48 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 46 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
49 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources))); 47 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources)));
50 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 48 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
51 49
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
57 m_source = source; 50 m_source = source;
58 51
59 if (m_element->isHTMLVideoElement()) { 52 if (m_element->isHTMLVideoElement()) {
60 videoHistogram.count(static_cast<int>(m_source)); 53 videoHistogram.count(static_cast<int>(m_source));
61 if (m_element->muted()) 54 if (m_element->muted())
62 mutedVideoHistogram.count(static_cast<int>(m_source)); 55 mutedVideoHistogram.count(static_cast<int>(m_source));
63 } else { 56 } else {
64 audioHistogram.count(static_cast<int>(m_source)); 57 audioHistogram.count(static_cast<int>(m_source));
65 } 58 }
66 59
67 m_element->addEventListener(EventTypeNames::playing, this, false); 60 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted())
61 m_element->addEventListener(EventTypeNames::playing, this, false);
68 } 62 }
69 63
70 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus) 64 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus)
71 { 65 {
72 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus))); 66 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus)));
73 67
74 autoplayUnmuteHistogram.count(static_cast<int>(status)); 68 autoplayUnmuteHistogram.count(static_cast<int>(status));
75 } 69 }
76 70
77 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) 71 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument)
78 { 72 {
79 if (!shouldListenToUnloadEvent()) 73 if (!m_videoMutedPlayMethodVisibilityObserver)
80 return; 74 return;
81 75
82 if (oldDocument.domWindow()) 76 if (oldDocument.domWindow())
83 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, thi s, false); 77 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, thi s, false);
84 if (m_element && m_element->document().domWindow()) 78 if (m_element && m_element->document().domWindow())
85 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false); 79 m_element->document().domWindow()->addEventListener(EventTypeNames::unlo ad, this, false);
86 } 80 }
87 81
88 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( bool isVisible) 82 void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisibl e)
89 { 83 {
90 if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver) 84 if (!isVisible || !m_videoMutedPlayMethodVisibilityObserver)
91 return; 85 return;
92 86
93 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); 87 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true);
94 } 88 m_videoMutedPlayMethodVisibilityObserver->stop();
95 89 m_videoMutedPlayMethodVisibilityObserver = nullptr;
96 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoOffscreenDuration(bool i sVisible) 90 if (m_element && m_element->document().domWindow())
97 { 91 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
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;
107 } 92 }
108 93
109 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent) 94 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent)
110 { 95 {
111 if (event->type() == EventTypeNames::playing) 96 if (event->type() == EventTypeNames::playing)
112 handlePlayingEvent(); 97 handlePlayingEvent();
113 else if (event->type() == EventTypeNames::pause)
114 handlePauseEvent();
115 else if (event->type() == EventTypeNames::unload) 98 else if (event->type() == EventTypeNames::unload)
116 handleUnloadEvent(); 99 handleUnloadEvent();
117 else 100 else
118 NOTREACHED(); 101 NOTREACHED();
119 } 102 }
120 103
104 void AutoplayUmaHelper::handleUnloadEvent()
105 {
106 if (m_videoMutedPlayMethodVisibilityObserver) {
107 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false);
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 }
114
121 void AutoplayUmaHelper::handlePlayingEvent() 115 void AutoplayUmaHelper::handlePlayingEvent()
122 { 116 {
123 maybeStartRecordingMutedVideoPlayMethodBecomeVisible(); 117 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) {
124 maybeStartRecordingMutedVideoOffscreenDuration(); 118 if (!m_videoMutedPlayMethodVisibilityObserver) {
125 119 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapWeakPersistent(this)));
120 m_videoMutedPlayMethodVisibilityObserver->start();
121 if (m_element && m_element->document().domWindow())
122 m_element->document().domWindow()->addEventListener(EventTypeNam es::unload, this, false);
123 }
124 }
126 m_element->removeEventListener(EventTypeNames::playing, this, false); 125 m_element->removeEventListener(EventTypeNames::playing, this, false);
127 } 126 }
128 127
129 void AutoplayUmaHelper::handlePauseEvent()
130 {
131 maybeStopRecordingMutedVideoOffscreenDuration();
132 }
133
134 void AutoplayUmaHelper::handleUnloadEvent()
135 {
136 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false);
137 maybeStopRecordingMutedVideoOffscreenDuration();
138 }
139
140 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible()
141 {
142 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() | | !m_element->muted())
143 return;
144
145 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver(m_e lement, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethod BecomeVisible, wrapWeakPersistent(this)));
146 m_mutedVideoPlayMethodVisibilityObserver->start();
147 if (m_element->document().domWindow())
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);
196 }
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;
213 }
214
215 DEFINE_TRACE(AutoplayUmaHelper) 128 DEFINE_TRACE(AutoplayUmaHelper)
216 { 129 {
217 EventListener::trace(visitor); 130 EventListener::trace(visitor);
218 visitor->trace(m_element); 131 visitor->trace(m_element);
219 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver); 132 visitor->trace(m_videoMutedPlayMethodVisibilityObserver);
220 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver);
221 } 133 }
222 134
223 } // namespace blink 135 } // 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