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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp
diff --git a/third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp b/third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..1201d9d02a5873673df986a565ff7d418c44df3c
--- /dev/null
+++ b/third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp
@@ -0,0 +1,119 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/html/AutoplayUmaHelper.h"
+
+#include "core/dom/ElementVisibilityObserver.h"
+#include "core/events/Event.h"
+#include "core/html/HTMLMediaElement.h"
+#include "platform/Histogram.h"
+
+namespace blink {
+
+namespace {
+
+void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible)
+{
+ DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible"));
+ histogram.count(visible);
+}
+
+} // namespace
+
+AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
+{
+ return new AutoplayUmaHelper(element);
+}
+
+AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
+ : EventListener(CPPEventListenerType)
+ , m_source(AutoplaySource::NumberOfSources)
+ , m_element(element)
+ , m_videoMutedPlayMethodVisibilityObserver(nullptr) { }
+
+AutoplayUmaHelper::~AutoplayUmaHelper() = default;
+
+bool AutoplayUmaHelper::operator==(const EventListener& other) const
+{
+ return this == &other;
+}
+
+void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
+{
+ DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Autoplay", static_cast<int>(AutoplaySource::NumberOfSources)));
+ DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video.Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources)));
+ DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Autoplay", static_cast<int>(AutoplaySource::NumberOfSources)));
+
+ m_source = source;
+
+ if (m_element->isHTMLVideoElement()) {
+ videoHistogram.count(static_cast<int>(m_source));
+ if (m_element->muted())
+ mutedVideoHistogram.count(static_cast<int>(m_source));
+ } else {
+ audioHistogram.count(static_cast<int>(m_source));
+ }
+
+ if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted())
+ m_element->addEventListener(EventTypeNames::playing, this, false);
+}
+
+void AutoplayUmaHelper::handleUnloadEvent()
+{
+ if (m_videoMutedPlayMethodVisibilityObserver) {
+ recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false);
+ m_videoMutedPlayMethodVisibilityObserver->stop();
+ m_videoMutedPlayMethodVisibilityObserver = nullptr;
+ m_element->document().domWindow()->removeEventListener(EventTypeNames::unload, this, false);
+ }
+}
+
+void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus status)
+{
+ DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.Video.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus::NumberOfStatus)));
+
+ autoplayUnmuteHistogram.count(static_cast<int>(status));
+}
+
+void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisible)
+{
+ if (!isVisible)
+ return;
+
+ recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true);
+ m_videoMutedPlayMethodVisibilityObserver->stop();
+ m_videoMutedPlayMethodVisibilityObserver = nullptr;
+ m_element->document().domWindow()->removeEventListener(EventTypeNames::unload, this, false);
+}
+
+void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* event)
+{
+ if (event->type() == EventTypeNames::playing)
+ handlePlayingEvent();
+ else if (event->type() == EventTypeNames::unload)
+ handleUnloadEvent();
+ else
+ NOTREACHED();
+}
+
+void AutoplayUmaHelper::handlePlayingEvent()
+{
+ if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) {
+ if (!m_videoMutedPlayMethodVisibilityObserver) {
+ m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObserver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod, wrapPersistent(this)));
+ m_videoMutedPlayMethodVisibilityObserver->start();
+ m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
+ }
+ }
+ m_element->removeEventListener(EventTypeNames::playing, this, false);
+}
+
+DEFINE_TRACE(AutoplayUmaHelper)
+{
+ EventListener::trace(visitor);
+ visitor->trace(m_element);
+ visitor->trace(m_videoMutedPlayMethodVisibilityObserver);
+}
+
+} // namespace blink
« 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