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

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

Powered by Google App Engine
This is Rietveld 408576698