Chromium Code Reviews| 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 |
| index 72ab6fe5645646c002559684f5da5634e1bf8242..b1bbf1741e39304b8c485d49b5a2373f57c6d0f8 100644 |
| --- a/third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp |
| +++ b/third_party/WebKit/Source/core/html/AutoplayUmaHelper.cpp |
| @@ -30,11 +30,9 @@ AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) |
| : EventListener(CPPEventListenerType), |
| m_source(AutoplaySource::NumberOfSources), |
| m_element(element), |
| - m_mutedVideoPlayMethodVisibilityObserver(nullptr), |
| m_mutedVideoAutoplayOffscreenStartTimeMS(0), |
| m_mutedVideoAutoplayOffscreenDurationMS(0), |
| - m_isVisible(false), |
| - m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) {} |
| + m_isVisible(false) {} |
| AutoplayUmaHelper::~AutoplayUmaHelper() = default; |
| @@ -116,28 +114,22 @@ void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) { |
| this, false); |
| } |
| -void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( |
| - bool isVisible) { |
| - if (!isVisible || !m_mutedVideoPlayMethodVisibilityObserver) |
| - return; |
| - |
| - maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); |
| -} |
| - |
| -void AutoplayUmaHelper::onVisibilityChangedForMutedVideoOffscreenDuration( |
| - bool isVisible) { |
| - if (isVisible == m_isVisible) |
| - return; |
| - |
| - if (isVisible) |
| - m_mutedVideoAutoplayOffscreenDurationMS += |
| - static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - |
| - m_mutedVideoAutoplayOffscreenStartTimeMS; |
| - else |
| - m_mutedVideoAutoplayOffscreenStartTimeMS = |
| - static_cast<int64_t>(monotonicallyIncreasingTimeMS()); |
| +void AutoplayUmaHelper::visibilityMightChangedforMutedVideo(bool isVisible) { |
| + if (m_isObservingVisibilityOfMutedVideoOffscreenDuration && isVisible) |
|
miu
2016/11/09 22:02:08
Note: This method is going to be called from HTMLM
xjz
2016/11/11 01:07:29
Done.
|
| + maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true); |
| - m_isVisible = isVisible; |
| + if (m_isObservingVisibilityOfMutedVideoOffscreenDuration && |
| + isVisible != m_isVisible) { |
| + if (isVisible) { |
| + m_mutedVideoAutoplayOffscreenDurationMS += |
| + static_cast<int64_t>(monotonicallyIncreasingTimeMS()) - |
| + m_mutedVideoAutoplayOffscreenStartTimeMS; |
| + } else { |
| + m_mutedVideoAutoplayOffscreenStartTimeMS = |
| + static_cast<int64_t>(monotonicallyIncreasingTimeMS()); |
| + } |
| + m_isVisible = isVisible; |
| + } |
| } |
| void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, |
| @@ -173,12 +165,7 @@ void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() { |
| !m_element->muted()) |
| return; |
| - m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver( |
| - m_element, |
| - WTF::bind(&AutoplayUmaHelper:: |
| - onVisibilityChangedForMutedVideoPlayMethodBecomeVisible, |
| - wrapWeakPersistent(this))); |
| - m_mutedVideoPlayMethodVisibilityObserver->start(); |
| + m_isObservingVisibilityOfMutedAutoplayVideo = true; |
| if (m_element->document().domWindow()) |
| m_element->document().domWindow()->addEventListener(EventTypeNames::unload, |
| this, false); |
| @@ -186,15 +173,14 @@ void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible() { |
| void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible( |
| bool visible) { |
| - if (!m_mutedVideoPlayMethodVisibilityObserver) |
| + if (!m_isObservingVisibilityOfMutedAutoplayVideo) |
| return; |
| DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, |
| ("Media.Video.Autoplay.Muted.PlayMethod.BecomesVisible")); |
| histogram.count(visible); |
| - m_mutedVideoPlayMethodVisibilityObserver->stop(); |
| - m_mutedVideoPlayMethodVisibilityObserver = nullptr; |
| + m_isObservingVisibilityOfMutedAutoplayVideo = false; |
| maybeUnregisterUnloadListener(); |
| } |
| @@ -206,13 +192,7 @@ void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() { |
| m_mutedVideoAutoplayOffscreenStartTimeMS = |
| static_cast<int64_t>(monotonicallyIncreasingTimeMS()); |
| m_isVisible = false; |
| - m_mutedVideoOffscreenDurationVisibilityObserver = |
| - new ElementVisibilityObserver( |
| - m_element, |
| - WTF::bind(&AutoplayUmaHelper:: |
| - onVisibilityChangedForMutedVideoOffscreenDuration, |
| - wrapWeakPersistent(this))); |
| - m_mutedVideoOffscreenDurationVisibilityObserver->start(); |
| + m_isObservingVisibilityOfMutedVideoOffscreenDuration = true; |
| m_element->addEventListener(EventTypeNames::pause, this, false); |
| if (m_element->document().domWindow()) |
| m_element->document().domWindow()->addEventListener(EventTypeNames::unload, |
| @@ -220,7 +200,7 @@ void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration() { |
| } |
| void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() { |
| - if (!m_mutedVideoOffscreenDurationVisibilityObserver) |
| + if (!m_isObservingVisibilityOfMutedVideoOffscreenDuration) |
| return; |
| if (!m_isVisible) |
| @@ -247,8 +227,7 @@ void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration() { |
| maxOffscreenDurationUmaMS, offscreenDurationUmaBucketCount)); |
| durationHistogram.count(boundedTime); |
| } |
| - m_mutedVideoOffscreenDurationVisibilityObserver->stop(); |
| - m_mutedVideoOffscreenDurationVisibilityObserver = nullptr; |
| + m_isObservingVisibilityOfMutedVideoOffscreenDuration = false; |
| m_mutedVideoAutoplayOffscreenDurationMS = 0; |
| m_element->removeEventListener(EventTypeNames::pause, this, false); |
| maybeUnregisterUnloadListener(); |
| @@ -261,15 +240,13 @@ void AutoplayUmaHelper::maybeUnregisterUnloadListener() { |
| } |
| bool AutoplayUmaHelper::shouldListenToUnloadEvent() const { |
| - return m_mutedVideoPlayMethodVisibilityObserver || |
| - m_mutedVideoOffscreenDurationVisibilityObserver; |
| + return m_isObservingVisibilityOfMutedAutoplayVideo || |
| + m_isObservingVisibilityOfMutedVideoOffscreenDuration; |
| } |
| DEFINE_TRACE(AutoplayUmaHelper) { |
| EventListener::trace(visitor); |
| visitor->trace(m_element); |
| - visitor->trace(m_mutedVideoPlayMethodVisibilityObserver); |
| - visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver); |
| } |
| } // namespace blink |