Chromium Code Reviews| Index: third_party/WebKit/Source/core/html/HTMLMediaElement.cpp |
| diff --git a/third_party/WebKit/Source/core/html/HTMLMediaElement.cpp b/third_party/WebKit/Source/core/html/HTMLMediaElement.cpp |
| index 0599ce2187e9d5ee7a4d68778485fd4a11fc5540..9d87a58f85f943cdbce1a3b1f3a6eba0653b99bf 100644 |
| --- a/third_party/WebKit/Source/core/html/HTMLMediaElement.cpp |
| +++ b/third_party/WebKit/Source/core/html/HTMLMediaElement.cpp |
| @@ -37,6 +37,7 @@ |
| #include "core/dom/Fullscreen.h" |
| #include "core/dom/shadow/ShadowRoot.h" |
| #include "core/events/Event.h" |
| +#include "core/frame/FrameView.h" |
| #include "core/frame/LocalFrame.h" |
| #include "core/frame/Settings.h" |
| #include "core/frame/UseCounter.h" |
| @@ -329,14 +330,13 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum |
| , m_remoteRoutesAvailable(false) |
| , m_playingRemotely(false) |
| , m_isFinalizing(false) |
| - , m_initialPlayWithoutUserGesture(false) |
| - , m_autoplayMediaCounted(false) |
| , m_inOverlayFullscreenVideo(false) |
| , m_audioTracks(AudioTrackList::create(*this)) |
| , m_videoTracks(VideoTrackList::create(*this)) |
| , m_textTracks(nullptr) |
| , m_audioSourceNode(nullptr) |
| - , m_autoplayHelper(*this) |
| + , m_autoplayHelperClient(adoptPtr(new AutoplayHelperClientImpl(this))) |
|
sof
2016/02/16 08:40:06
Could you add a static create() factory method & c
liberato (no reviews please)
2016/02/24 18:44:05
Done.
|
| + , m_autoplayHelper(adoptPtr(new AutoplayExperimentHelper(*m_autoplayHelperClient))) |
|
sof
2016/02/16 08:40:06
Could you add a static create() factory method & c
liberato (no reviews please)
2016/02/24 18:44:05
Done.
|
| { |
| #if ENABLE(OILPAN) |
| ThreadState::current()->registerPreFinalizer(this); |
| @@ -358,6 +358,12 @@ HTMLMediaElement::~HTMLMediaElement() |
| { |
| WTF_LOG(Media, "HTMLMediaElement::~HTMLMediaElement(%p)", this); |
| + if (m_autoplayHelper) |
|
sof
2016/02/16 08:40:06
I haven't looked at the details, but is this expli
liberato (no reviews please)
2016/02/24 18:44:05
yes. destroying the helper calls into the client,
|
| + m_autoplayHelper.clear(); |
| + |
| + if (m_autoplayHelperClient) |
| + m_autoplayHelperClient.clear(); |
| + |
| #if !ENABLE(OILPAN) |
| // HTMLMediaElement and m_asyncEventQueue always become unreachable |
| // together. So HTMLMediaElement and m_asyncEventQueue are destructed in |
| @@ -658,47 +664,11 @@ String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySy |
| return canPlay; |
| } |
| -void HTMLMediaElement::recordMetricsIfPausing() |
| -{ |
| - // If not playing, then nothing to record. |
| - // TODO(liberato): test metrics. this was m_paused. |
| - if (m_paused) |
| - return; |
| - |
| - const bool bailout = isBailout(); |
| - |
| - // Record that play was paused. We don't care if it was autoplay, |
| - // play(), or the user manually started it. |
| - recordAutoplayMetric(AnyPlaybackPaused); |
| - if (bailout) |
| - recordAutoplayMetric(AnyPlaybackBailout); |
| - |
| - // If this was a gestureless play, then record that separately. |
| - // These cover attr and play() gestureless starts. |
| - if (m_initialPlayWithoutUserGesture) { |
| - m_initialPlayWithoutUserGesture = false; |
| - |
| - recordAutoplayMetric(AutoplayPaused); |
| - |
| - if (bailout) |
| - recordAutoplayMetric(AutoplayBailout); |
| - } |
| -} |
| - |
| void HTMLMediaElement::load() |
| { |
| WTF_LOG(Media, "HTMLMediaElement::load(%p)", this); |
| - recordMetricsIfPausing(); |
| - |
| - if (UserGestureIndicator::processingUserGesture() && m_userGestureRequiredForPlay) { |
| - recordAutoplayMetric(AutoplayEnabledThroughLoad); |
| - m_userGestureRequiredForPlay = false; |
| - // While usergesture-initiated load()s technically count as autoplayed, |
| - // they don't feel like such to the users and hence we don't want to |
| - // count them for the purposes of metrics. |
| - m_autoplayMediaCounted = true; |
| - } |
| + m_autoplayHelper->loadMethodCalled(); |
| prepareForLoad(); |
| loadInternal(); |
| @@ -921,6 +891,8 @@ void HTMLMediaElement::loadResource(const KURL& url, ContentType& contentType, c |
| // The resource fetch algorithm |
| setNetworkState(NETWORK_LOADING); |
| + m_autoplayHelper->loadingStarted(); |
| + |
| // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app |
| // cache is an internal detail not exposed through the media element API. |
| m_currentSrc = url; |
| @@ -947,7 +919,7 @@ void HTMLMediaElement::loadResource(const KURL& url, ContentType& contentType, c |
| if (url.protocolIs(mediaSourceBlobProtocol)) { |
| if (isMediaStreamURL(url.string())) { |
| - m_userGestureRequiredForPlay = false; |
| + m_autoplayHelper->removeUserGestureRequirement(GesturelessPlaybackEnabledByStream); |
| } else { |
| m_mediaSource = HTMLMediaSource::lookup(url.string()); |
| @@ -1530,10 +1502,10 @@ void HTMLMediaElement::setReadyState(ReadyState state) |
| } |
| // Check for autoplay, and record metrics about it if needed. |
| - if (shouldAutoplay(RecordMetricsBehavior::DoRecord)) { |
| + if (shouldAutoplay(RecordMetricsBehavior::RecordOnSandboxFailure)) { |
| // If the autoplay experiment says that it's okay to play now, |
| // then don't require a user gesture. |
| - m_autoplayHelper.becameReadyToPlay(); |
| + m_autoplayHelper->becameReadyToPlay(); |
| if (!m_userGestureRequiredForPlay) { |
| m_paused = false; |
| @@ -1672,8 +1644,6 @@ void HTMLMediaElement::seek(double time) |
| // 11 - Set the current playback position to the given new playback position. |
| webMediaPlayer()->seek(time); |
| - m_initialPlayWithoutUserGesture = false; |
| - |
| // 14-17 are handled, if necessary, when the engine signals a readystate change or otherwise |
| // satisfies seek completion and signals a time change. |
| } |
| @@ -1855,12 +1825,9 @@ bool HTMLMediaElement::autoplay() const |
| bool HTMLMediaElement::shouldAutoplay(const RecordMetricsBehavior recordMetrics) |
| { |
| if (m_autoplaying && m_paused && autoplay()) { |
| - if (recordMetrics == RecordMetricsBehavior::DoRecord) |
| - autoplayMediaEncountered(); |
| - |
| if (document().isSandboxed(SandboxAutomaticFeatures)) { |
| - if (recordMetrics == RecordMetricsBehavior::DoRecord) |
| - recordAutoplayMetric(AutoplayDisabledBySandbox); |
| + if (recordMetrics == RecordMetricsBehavior::RecordOnSandboxFailure) |
| + m_autoplayHelper->recordSandboxFailure(); |
| return false; |
| } |
| @@ -1935,21 +1902,19 @@ void HTMLMediaElement::play() |
| { |
| WTF_LOG(Media, "HTMLMediaElement::play(%p)", this); |
| - m_autoplayHelper.playMethodCalled(); |
| + m_autoplayHelper->playMethodCalled(); |
| if (!UserGestureIndicator::processingUserGesture()) { |
| - autoplayMediaEncountered(); |
| - |
| if (m_userGestureRequiredForPlay) { |
| recordAutoplayMetric(PlayMethodFailed); |
| String message = ExceptionMessages::failedToExecute("play", "HTMLMediaElement", "API can only be initiated by a user gesture."); |
| document().addConsoleMessage(ConsoleMessage::create(JSMessageSource, WarningMessageLevel, message)); |
| return; |
| } |
| - } else if (m_userGestureRequiredForPlay) { |
| - if (m_autoplayMediaCounted) |
| - recordAutoplayMetric(AutoplayManualStart); |
| - m_userGestureRequiredForPlay = false; |
| + } else { |
| + // We ask the helper to remove the gesture requirement for us, so that |
| + // it can record the reason. |
| + m_autoplayHelper->removeUserGestureRequirement(GesturelessPlaybackEnabledByPlayMethod); |
| } |
| playInternal(); |
| @@ -1990,26 +1955,6 @@ void HTMLMediaElement::playInternal() |
| updatePlayState(); |
| } |
| -void HTMLMediaElement::autoplayMediaEncountered() |
| -{ |
| - if (!m_autoplayMediaCounted) { |
| - m_autoplayMediaCounted = true; |
| - recordAutoplayMetric(AutoplayMediaFound); |
| - |
| - if (!m_userGestureRequiredForPlay) |
| - m_initialPlayWithoutUserGesture = true; |
| - } |
| -} |
| - |
| -bool HTMLMediaElement::isBailout() const |
| -{ |
| - // We count the user as having bailed-out on the video if they watched |
| - // less than one minute and less than 50% of it. |
| - const double playedTime = currentTime(); |
| - const double progress = playedTime / duration(); |
| - return (playedTime < 60) && (progress < 0.5); |
| -} |
| - |
| void HTMLMediaElement::pause() |
| { |
| WTF_LOG(Media, "HTMLMediaElement::pause(%p)", this); |
| @@ -2029,13 +1974,11 @@ void HTMLMediaElement::pauseInternal() |
| if (m_networkState == NETWORK_EMPTY) |
| scheduleDelayedAction(LoadMediaResource); |
| - m_autoplayHelper.pauseMethodCalled(); |
| + m_autoplayHelper->pauseMethodCalled(); |
| m_autoplaying = false; |
| if (!m_paused) { |
| - recordMetricsIfPausing(); |
| - |
| m_paused = true; |
| scheduleTimeupdateEvent(false); |
| scheduleEvent(EventTypeNames::pause); |
| @@ -2127,7 +2070,7 @@ void HTMLMediaElement::setMuted(bool muted) |
| m_muted = muted; |
| - m_autoplayHelper.mutedChanged(); |
| + m_autoplayHelper->mutedChanged(); |
| updateVolume(); |
| @@ -2727,6 +2670,7 @@ void HTMLMediaElement::timeChanged() |
| // forwards, and paused is false, |
| if (!m_paused) { |
| // changes paused to true and fires a simple event named pause at the media element. |
| + m_autoplayHelper->playbackEnded(); |
| m_paused = true; |
| scheduleEvent(EventTypeNames::pause); |
| } |
| @@ -2735,7 +2679,6 @@ void HTMLMediaElement::timeChanged() |
| m_sentEndEvent = true; |
| scheduleEvent(EventTypeNames::ended); |
| } |
| - recordMetricsIfPausing(); |
| } |
| } else { |
| m_sentEndEvent = false; |
| @@ -2942,17 +2885,18 @@ void HTMLMediaElement::updatePlayState() |
| webMediaPlayer()->setRate(playbackRate()); |
| updateVolume(); |
| webMediaPlayer()->play(); |
| + m_autoplayHelper->playbackStarted(); |
| } |
| if (mediaControls()) |
| mediaControls()->playbackStarted(); |
| startPlaybackProgressTimer(); |
| m_playing = true; |
| - recordAutoplayMetric(AnyPlaybackStarted); |
| } else { // Should not be playing right now |
| if (isPlaying) |
| webMediaPlayer()->pause(); |
| + |
| refreshCachedTime(); |
| m_playbackProgressTimer.stop(); |
| @@ -3020,8 +2964,6 @@ void HTMLMediaElement::stop() |
| { |
| WTF_LOG(Media, "HTMLMediaElement::stop(%p)", this); |
| - recordMetricsIfPausing(); |
| - |
| // Close the async event queue so that no events are enqueued. |
| cancelPendingEventsAndCallbacks(); |
| m_asyncEventQueue->close(); |
| @@ -3490,7 +3432,6 @@ DEFINE_TRACE(HTMLMediaElement) |
| visitor->trace(m_textTracksWhenResourceSelectionBegan); |
| visitor->trace(m_audioSourceProvider); |
| visitor->template registerWeakMembers<HTMLMediaElement, &HTMLMediaElement::clearWeakMembers>(this); |
| - visitor->trace(m_autoplayHelper); |
| HeapSupplementable<HTMLMediaElement>::trace(visitor); |
| #endif |
| HTMLElement::trace(visitor); |
| @@ -3535,11 +3476,6 @@ void HTMLMediaElement::removeUserGestureRequirement() |
| m_userGestureRequiredForPlay = false; |
| } |
| -void HTMLMediaElement::setInitialPlayWithoutUserGestures(bool value) |
| -{ |
| - m_initialPlayWithoutUserGesture = value; |
| -} |
| - |
| void HTMLMediaElement::setNetworkState(NetworkState state) |
| { |
| if (m_networkState != state) { |
| @@ -3551,18 +3487,20 @@ void HTMLMediaElement::setNetworkState(NetworkState state) |
| void HTMLMediaElement::notifyPositionMayHaveChanged(const IntRect& visibleRect) |
| { |
| - m_autoplayHelper.positionChanged(visibleRect); |
| + m_autoplayHelper->positionChanged(visibleRect); |
| } |
| void HTMLMediaElement::updatePositionNotificationRegistration() |
| { |
| - m_autoplayHelper.updatePositionNotificationRegistration(); |
| + m_autoplayHelper->updatePositionNotificationRegistration(); |
| } |
| // TODO(liberato): remove once autoplay gesture override experiment concludes. |
| void HTMLMediaElement::triggerAutoplayViewportCheckForTesting() |
| { |
| - m_autoplayHelper.triggerAutoplayViewportCheckForTesting(); |
| + if (FrameView* view = document().view()) |
| + m_autoplayHelper->positionChanged(view->rootFrameToContents(view->computeVisibleArea())); |
| + m_autoplayHelper->triggerAutoplayViewportCheckForTesting(); |
| } |
| void HTMLMediaElement::clearWeakMembers(Visitor* visitor) |
| @@ -3631,4 +3569,43 @@ DEFINE_TRACE(HTMLMediaElement::AudioSourceProviderImpl) |
| visitor->trace(m_client); |
| } |
| +HTMLMediaElement::AutoplayHelperClientImpl::~AutoplayHelperClientImpl() |
| +{ |
| +} |
| + |
| +bool HTMLMediaElement::AutoplayHelperClientImpl::isLegacyViewportType() |
| +{ |
| + return m_element->document().viewportDescription().isLegacyViewportType(); |
| +} |
| + |
| +PageVisibilityState HTMLMediaElement::AutoplayHelperClientImpl::pageVisibilityState() const |
| +{ |
| + return m_element->document().pageVisibilityState(); |
| +} |
| + |
| +String HTMLMediaElement::AutoplayHelperClientImpl::autoplayExperimentMode() const |
| +{ |
| + String mode; |
| + if (m_element->document().settings()) |
| + mode = m_element->document().settings()->autoplayExperimentMode(); |
| + |
| + return mode; |
| +} |
| + |
| +void HTMLMediaElement::AutoplayHelperClientImpl::setRequestPositionUpdates(bool request) |
| +{ |
| + if (LayoutObject* layoutObject = m_element->layoutObject()) { |
| + LayoutMedia* layoutMedia = toLayoutMedia(layoutObject); |
| + layoutMedia->setRequestPositionUpdates(request); |
| + } |
| +} |
| + |
| +IntRect HTMLMediaElement::AutoplayHelperClientImpl::absoluteBoundingBoxRect() const |
| +{ |
| + IntRect result; |
| + if (LayoutObject* object = m_element->layoutObject()) |
| + result = object->absoluteBoundingBoxRect(); |
| + return result; |
| +} |
| + |
| } // namespace blink |