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

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

Issue 2154303002: Reland: Measure whether muted videos that started playing with play() become visible at some point (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "core/html/AutoplayUmaHelper.h"
6
7 #include "core/dom/Document.h"
8 #include "core/dom/ElementVisibilityObserver.h"
9 #include "core/events/Event.h"
10 #include "core/frame/LocalDOMWindow.h"
11 #include "core/html/HTMLMediaElement.h"
12 #include "platform/Histogram.h"
13
14 namespace blink {
15
16 namespace {
17
18 void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible)
19 {
20 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
21 histogram.count(visible);
22 }
23
24 } // namespace
25
26 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
27 {
28 return new AutoplayUmaHelper(element);
29 }
30
31 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
32 : EventListener(CPPEventListenerType)
33 , m_source(AutoplaySource::NumberOfSources)
34 , m_element(element)
35 , m_videoMutedPlayMethodVisibilityObserver(nullptr) { }
36
37 AutoplayUmaHelper::~AutoplayUmaHelper() = default;
38
39 bool AutoplayUmaHelper::operator==(const EventListener& other) const
40 {
41 return this == &other;
42 }
43
44 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
45 {
46 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)));
48 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
49
50 m_source = source;
51
52 if (m_element->isHTMLVideoElement()) {
53 videoHistogram.count(static_cast<int>(m_source));
54 if (m_element->muted())
55 mutedVideoHistogram.count(static_cast<int>(m_source));
56 } else {
57 audioHistogram.count(static_cast<int>(m_source));
58 }
59
60 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted())
61 m_element->addEventListener(EventTypeNames::playing, this, false);
62 }
63
64 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus)
65 {
66 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus)));
67
68 autoplayUnmuteHistogram.count(static_cast<int>(status));
69 }
70
71 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument)
72 {
73 if (!m_videoMutedPlayMethodVisibilityObserver)
74 return;
75
76 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this, f alse);
77 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
78 }
79
80 void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisibl e)
81 {
82 if (!isVisible)
83 return;
84
85 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true);
86 m_videoMutedPlayMethodVisibilityObserver->stop();
87 m_videoMutedPlayMethodVisibilityObserver = nullptr;
88 m_element->document().domWindow()->removeEventListener(EventTypeNames::unloa d, this, false);
89 }
90
91 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent)
92 {
93 if (event->type() == EventTypeNames::playing)
94 handlePlayingEvent();
95 else if (event->type() == EventTypeNames::unload)
96 handleUnloadEvent();
97 else
98 NOTREACHED();
99 }
100
101 void AutoplayUmaHelper::handleUnloadEvent()
102 {
103 if (m_videoMutedPlayMethodVisibilityObserver) {
104 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false);
105 m_videoMutedPlayMethodVisibilityObserver->stop();
106 m_videoMutedPlayMethodVisibilityObserver = nullptr;
107 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
108 }
109 }
110
111 void AutoplayUmaHelper::handlePlayingEvent()
112 {
113 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) {
114 if (!m_videoMutedPlayMethodVisibilityObserver) {
115 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapPersistent(this)));
116 m_videoMutedPlayMethodVisibilityObserver->start();
117 m_element->document().domWindow()->addEventListener(EventTypeNames:: unload, this, false);
118 }
119 }
120 m_element->removeEventListener(EventTypeNames::playing, this, false);
121 }
122
123 DEFINE_TRACE(AutoplayUmaHelper)
124 {
125 EventListener::trace(visitor);
126 visitor->trace(m_element);
127 visitor->trace(m_videoMutedPlayMethodVisibilityObserver);
128 }
129
130 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/AutoplayUmaHelper.h ('k') | third_party/WebKit/Source/core/html/HTMLMediaElement.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698