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

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

Issue 2463113002: Let AutoplayUmaHelper listen to visibilitychange instead of unload (Closed)
Patch Set: nit Created 4 years, 1 month 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"
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),
30 ContextLifecycleObserver(nullptr),
31 m_source(AutoplaySource::NumberOfSources), 31 m_source(AutoplaySource::NumberOfSources),
32 m_element(element), 32 m_element(element),
33 m_mutedVideoPlayMethodVisibilityObserver(nullptr), 33 m_mutedVideoPlayMethodVisibilityObserver(nullptr),
34 m_mutedVideoAutoplayOffscreenStartTimeMS(0), 34 m_mutedVideoAutoplayOffscreenStartTimeMS(0),
35 m_mutedVideoAutoplayOffscreenDurationMS(0), 35 m_mutedVideoAutoplayOffscreenDurationMS(0),
36 m_isVisible(false), 36 m_isVisible(false),
37 m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) {} 37 m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) {}
38 38
39 AutoplayUmaHelper::~AutoplayUmaHelper() = default; 39 AutoplayUmaHelper::~AutoplayUmaHelper() = default;
40 40
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 AutoplayUnmuteActionStatus status) { 98 AutoplayUnmuteActionStatus status) {
99 DEFINE_STATIC_LOCAL( 99 DEFINE_STATIC_LOCAL(
100 EnumerationHistogram, autoplayUnmuteHistogram, 100 EnumerationHistogram, autoplayUnmuteHistogram,
101 ("Media.Video.Autoplay.Muted.UnmuteAction", 101 ("Media.Video.Autoplay.Muted.UnmuteAction",
102 static_cast<int>(AutoplayUnmuteActionStatus::NumberOfStatus))); 102 static_cast<int>(AutoplayUnmuteActionStatus::NumberOfStatus)));
103 103
104 autoplayUnmuteHistogram.count(static_cast<int>(status)); 104 autoplayUnmuteHistogram.count(static_cast<int>(status));
105 } 105 }
106 106
107 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) { 107 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) {
108 if (!shouldListenToUnloadEvent()) 108 if (!shouldListenToContextDestroyed())
109 return; 109 return;
110 110
111 if (oldDocument.domWindow()) 111 setContext(&m_element->document());
112 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this,
113 false);
114 if (m_element->document().domWindow())
115 m_element->document().domWindow()->addEventListener(EventTypeNames::unload,
116 this, false);
117 } 112 }
118 113
119 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( 114 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible(
120 bool isVisible) { 115 bool isVisible) {
121 if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver) 116 if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver)
122 return; 117 return;
123 118
124 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); 119 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true);
125 } 120 }
126 121
(...skipping 12 matching lines...) Expand all
139 134
140 m_isVisible = isVisible; 135 m_isVisible = isVisible;
141 } 136 }
142 137
143 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, 138 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext,
144 Event* event) { 139 Event* event) {
145 if (event->type() == EventTypeNames::playing) 140 if (event->type() == EventTypeNames::playing)
146 handlePlayingEvent(); 141 handlePlayingEvent();
147 else if (event->type() == EventTypeNames::pause) 142 else if (event->type() == EventTypeNames::pause)
148 handlePauseEvent(); 143 handlePauseEvent();
149 else if (event->type() == EventTypeNames::unload)
150 handleUnloadEvent();
151 else 144 else
152 NOTREACHED(); 145 NOTREACHED();
153 } 146 }
154 147
155 void AutoplayUmaHelper::handlePlayingEvent() { 148 void AutoplayUmaHelper::handlePlayingEvent() {
156 maybeStartRecordingMutedVideoPlayMethodBecomeVisible(); 149 maybeStartRecordingMutedVideoPlayMethodBecomeVisible();
157 maybeStartRecordingMutedVideoOffscreenDuration(); 150 maybeStartRecordingMutedVideoOffscreenDuration();
158 151
159 m_element->removeEventListener(EventTypeNames::playing, this, false); 152 m_element->removeEventListener(EventTypeNames::playing, this, false);
160 } 153 }
161 154
162 void AutoplayUmaHelper::handlePauseEvent() { 155 void AutoplayUmaHelper::handlePauseEvent() {
163 maybeStopRecordingMutedVideoOffscreenDuration(); 156 maybeStopRecordingMutedVideoOffscreenDuration();
164 } 157 }
165 158
166 void AutoplayUmaHelper::handleUnloadEvent() { 159 void AutoplayUmaHelper::contextDestroyed() {
160 handleContextDestroyed();
161 }
162
163 void AutoplayUmaHelper::handleContextDestroyed() {
167 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false); 164 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false);
168 maybeStopRecordingMutedVideoOffscreenDuration(); 165 maybeStopRecordingMutedVideoOffscreenDuration();
169 } 166 }
170 167
171 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() { 168 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() {
172 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() || 169 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() ||
173 !m_element->muted()) 170 !m_element->muted())
174 return; 171 return;
175 172
176 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver( 173 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver(
177 m_element, 174 m_element,
178 WTF::bind(&AutoplayUmaHelper:: 175 WTF::bind(&AutoplayUmaHelper::
179 onVisibilityChangedForMutedVideoPlayMethodBecomeVisible, 176 onVisibilityChangedForMutedVideoPlayMethodBecomeVisible,
180 wrapWeakPersistent(this))); 177 wrapWeakPersistent(this)));
181 m_mutedVideoPlayMethodVisibilityObserver->start(); 178 m_mutedVideoPlayMethodVisibilityObserver->start();
182 if (m_element->document().domWindow()) 179 setContext(&m_element->document());
183 m_element->document().domWindow()->addEventListener(EventTypeNames::unload,
184 this, false);
185 } 180 }
186 181
187 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible( 182 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible(
188 bool visible) { 183 bool visible) {
189 if (!m_mutedVideoPlayMethodVisibilityObserver) 184 if (!m_mutedVideoPlayMethodVisibilityObserver)
190 return; 185 return;
191 186
192 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, 187 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram,
193 ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible")); 188 ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible"));
194 189
195 histogram.count(visible); 190 histogram.count(visible);
196 m_mutedVideoPlayMethodVisibilityObserver->stop(); 191 m_mutedVideoPlayMethodVisibilityObserver->stop();
197 m_mutedVideoPlayMethodVisibilityObserver = nullptr; 192 m_mutedVideoPlayMethodVisibilityObserver = nullptr;
198 maybeUnregisterUnloadListener(); 193 maybeUnregisterContextDestroyedObserver();
199 } 194 }
200 195
201 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() { 196 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() {
202 if (!m_element->isHTMLVideoElement() || !m_element->muted()) 197 if (!m_element->isHTMLVideoElement() || !m_element->muted())
203 return; 198 return;
204 199
205 // Start recording muted video playing offscreen duration. 200 // Start recording muted video playing offscreen duration.
206 m_mutedVideoAutoplayOffscreenStartTimeMS = 201 m_mutedVideoAutoplayOffscreenStartTimeMS =
207 static_cast<int64_t>(monotonicallyIncreasingTimeMS()); 202 static_cast<int64_t>(monotonicallyIncreasingTimeMS());
208 m_isVisible = false; 203 m_isVisible = false;
209 m_mutedVideoOffscreenDurationVisibilityObserver = 204 m_mutedVideoOffscreenDurationVisibilityObserver =
210 new ElementVisibilityObserver( 205 new ElementVisibilityObserver(
211 m_element, 206 m_element,
212 WTF::bind(&AutoplayUmaHelper:: 207 WTF::bind(&AutoplayUmaHelper::
213 onVisibilityChangedForMutedVideoOffscreenDuration, 208 onVisibilityChangedForMutedVideoOffscreenDuration,
214 wrapWeakPersistent(this))); 209 wrapWeakPersistent(this)));
215 m_mutedVideoOffscreenDurationVisibilityObserver->start(); 210 m_mutedVideoOffscreenDurationVisibilityObserver->start();
216 m_element->addEventListener(EventTypeNames::pause, this, false); 211 m_element->addEventListener(EventTypeNames::pause, this, false);
217 if (m_element->document().domWindow()) 212 setContext(&m_element->document());
218 m_element->document().domWindow()->addEventListener(EventTypeNames::unload,
219 this, false);
220 } 213 }
221 214
222 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() { 215 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() {
223 if (!m_mutedVideoOffscreenDurationVisibilityObserver) 216 if (!m_mutedVideoOffscreenDurationVisibilityObserver)
224 return; 217 return;
225 218
226 if (!m_isVisible) 219 if (!m_isVisible)
227 m_mutedVideoAutoplayOffscreenDurationMS += 220 m_mutedVideoAutoplayOffscreenDurationMS +=
228 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - 221 static_cast<int64_t>(monotonicallyIncreasingTimeMS()) -
229 m_mutedVideoAutoplayOffscreenStartTimeMS; 222 m_mutedVideoAutoplayOffscreenStartTimeMS;
(...skipping 14 matching lines...) Expand all
244 DEFINE_STATIC_LOCAL( 237 DEFINE_STATIC_LOCAL(
245 CustomCountHistogram, durationHistogram, 238 CustomCountHistogram, durationHistogram,
246 ("Media.Video.Autoplay.Muted.PlayMethod.OffscreenDuration", 1, 239 ("Media.Video.Autoplay.Muted.PlayMethod.OffscreenDuration", 1,
247 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); 240 maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount));
248 durationHistogram.count(boundedTime); 241 durationHistogram.count(boundedTime);
249 } 242 }
250 m_mutedVideoOffscreenDurationVisibilityObserver->stop(); 243 m_mutedVideoOffscreenDurationVisibilityObserver->stop();
251 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr; 244 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr;
252 m_mutedVideoAutoplayOffscreenDurationMS = 0; 245 m_mutedVideoAutoplayOffscreenDurationMS = 0;
253 m_element->removeEventListener(EventTypeNames::pause, this, false); 246 m_element->removeEventListener(EventTypeNames::pause, this, false);
254 maybeUnregisterUnloadListener(); 247 maybeUnregisterContextDestroyedObserver();
255 } 248 }
256 249
257 void AutoplayUmaHelper::maybeUnregisterUnloadListener() { 250 void AutoplayUmaHelper::maybeUnregisterContextDestroyedObserver() {
258 if (!shouldListenToUnloadEvent() && m_element->document().domWindow()) 251 if (!shouldListenToContextDestroyed()) {
259 m_element->document().domWindow()->removeEventListener( 252 setContext(nullptr);
260 EventTypeNames::unload, this, false); 253 }
261 } 254 }
262 255
263 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const { 256 bool AutoplayUmaHelper::shouldListenToContextDestroyed() const {
264 return m_mutedVideoPlayMethodVisibilityObserver || 257 return m_mutedVideoPlayMethodVisibilityObserver ||
265 m_mutedVideoOffscreenDurationVisibilityObserver; 258 m_mutedVideoOffscreenDurationVisibilityObserver;
266 } 259 }
267 260
268 DEFINE_TRACE(AutoplayUmaHelper) { 261 DEFINE_TRACE(AutoplayUmaHelper) {
269 EventListener::trace(visitor); 262 EventListener::trace(visitor);
263 ContextLifecycleObserver::trace(visitor);
270 visitor->trace(m_element); 264 visitor->trace(m_element);
271 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver); 265 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver);
272 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver); 266 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver);
273 } 267 }
274 268
275 } // namespace blink 269 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/AutoplayUmaHelper.h ('k') | third_party/WebKit/Source/core/html/AutoplayUmaHelperTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698