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

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

Issue 2108403003: 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: clean up in a unload event listener Created 4 years, 5 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/ElementVisibilityObserver.h"
8 #include "core/events/Event.h"
9 #include "core/html/HTMLMediaElement.h"
10 #include "platform/Histogram.h"
11
12 namespace blink {
13
14 namespace {
15
16 void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible)
17 {
18 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
19 histogram.count(visible);
20 }
21
22 } // namespace
23
24 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
25 {
26 return new AutoplayUmaHelper(element);
27 }
28
29 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
30 : EventListener(CPPEventListenerType)
31 , m_source(AutoplaySource::NumberOfSources)
32 , m_element(element)
33 , m_videoMutedPlayMethodVisibilityObserver(nullptr) { }
34
35 AutoplayUmaHelper::~AutoplayUmaHelper() = default;
36
37 bool AutoplayUmaHelper::operator==(const EventListener& other) const
38 {
39 return this == &other;
40 }
41
42 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
43 {
44 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
45 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources)));
46 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
47
48 m_source = source;
49
50 if (m_element->isHTMLVideoElement()) {
51 videoHistogram.count(static_cast<int>(m_source));
52 if (m_element->muted())
53 mutedVideoHistogram.count(static_cast<int>(m_source));
54 } else {
55 audioHistogram.count(static_cast<int>(m_source));
56 }
57
58 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted())
59 m_element->addEventListener(EventTypeNames::playing, this, false);
60 }
61
62 void AutoplayUmaHelper::handleUnloadEvent()
63 {
64 if (m_videoMutedPlayMethodVisibilityObserver) {
65 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false);
66 m_videoMutedPlayMethodVisibilityObserver->stop();
67 m_videoMutedPlayMethodVisibilityObserver = nullptr;
68 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
69 }
70 }
71
72 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus)
73 {
74 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus)));
75
76 autoplayUnmuteHistogram.count(static_cast<int>(status));
77 }
78
79 void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisibl e)
80 {
81 if (!isVisible)
82 return;
83
84 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true);
85 m_videoMutedPlayMethodVisibilityObserver->stop();
86 m_videoMutedPlayMethodVisibilityObserver = nullptr;
87 m_element->document().domWindow()->removeEventListener(EventTypeNames::unloa d, this, false);
88 }
89
90 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent)
91 {
92 if (event->type() == EventTypeNames::playing)
93 handlePlayingEvent();
94 else if (event->type() == EventTypeNames::unload)
95 handleUnloadEvent();
96 else
97 NOTREACHED();
98 }
99
100 void AutoplayUmaHelper::handlePlayingEvent()
101 {
102 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) {
103 if (!m_videoMutedPlayMethodVisibilityObserver) {
104 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapPersistent(this)));
105 m_videoMutedPlayMethodVisibilityObserver->start();
106 m_element->document().domWindow()->addEventListener(EventTypeNames:: unload, this, false);
107 }
108 }
109 m_element->removeEventListener(EventTypeNames::playing, this, false);
110 }
111
112 DEFINE_TRACE(AutoplayUmaHelper)
113 {
114 EventListener::trace(visitor);
115 visitor->trace(m_element);
116 visitor->trace(m_videoMutedPlayMethodVisibilityObserver);
117 }
118
119 } // 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