| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/html/AutoplayExperimentHelper.h" | 6 #include "core/html/AutoplayExperimentHelper.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/frame/FrameView.h" | 9 #include "core/frame/FrameView.h" |
| 10 #include "core/frame/Settings.h" | 10 #include "core/frame/Settings.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 AutoplayExperimentHelper::~AutoplayExperimentHelper() | 47 AutoplayExperimentHelper::~AutoplayExperimentHelper() |
| 48 { | 48 { |
| 49 unregisterForPositionUpdatesIfNeeded(); | 49 unregisterForPositionUpdatesIfNeeded(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void AutoplayExperimentHelper::becameReadyToPlay() | 52 void AutoplayExperimentHelper::becameReadyToPlay() |
| 53 { | 53 { |
| 54 // Assuming that we're eligible to override the user gesture requirement, | 54 // Assuming that we're eligible to override the user gesture requirement, |
| 55 // either play if we meet the visibility checks, or install a listener | 55 // either play if we meet the visibility checks, or install a listener |
| 56 // to wait for them to pass. | 56 // to wait for them to pass. We do not actually start playback; our |
| 57 // caller must do that. |
| 57 if (isEligible()) { | 58 if (isEligible()) { |
| 58 if (meetsVisibilityRequirements()) | 59 if (meetsVisibilityRequirements()) |
| 59 prepareToPlay(GesturelessPlaybackStartedByAutoplayFlagImmediately); | 60 prepareToAutoplay(GesturelessPlaybackStartedByAutoplayFlagImmediatel
y); |
| 60 else | 61 else |
| 61 registerForPositionUpdatesIfNeeded(); | 62 registerForPositionUpdatesIfNeeded(); |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 | 65 |
| 65 void AutoplayExperimentHelper::playMethodCalled() | 66 void AutoplayExperimentHelper::playMethodCalled() |
| 66 { | 67 { |
| 67 // Set the pending state, even if the play isn't going to be pending. | 68 // Set the pending state, even if the play isn't going to be pending. |
| 68 // Eligibility can change if, for example, the mute status changes. | 69 // Eligibility can change if, for example, the mute status changes. |
| 69 // Having this set is okay. | 70 // Having this set is okay. |
| 70 m_playPending = true; | 71 m_playPending = true; |
| 71 | 72 |
| 72 if (!UserGestureIndicator::processingUserGesture()) { | 73 if (!UserGestureIndicator::processingUserGesture()) { |
| 73 | |
| 74 if (isEligible()) { | 74 if (isEligible()) { |
| 75 // Remember that userGestureRequiredForPlay is required for | 75 // Remember that userGestureRequiredForPlay is required for |
| 76 // us to be eligible for the experiment. | 76 // us to be eligible for the experiment. |
| 77 // If we are able to override the gesture requirement now, then | 77 // If we are able to override the gesture requirement now, then |
| 78 // do so. Otherwise, install an event listener if we need one. | 78 // do so. Otherwise, install an event listener if we need one. |
| 79 if (meetsVisibilityRequirements()) { | 79 if (meetsVisibilityRequirements()) { |
| 80 // Override the gesture and play. | 80 // Override the gesture and play. |
| 81 prepareToPlay(GesturelessPlaybackStartedByPlayMethodImmediately)
; | 81 prepareToAutoplay(GesturelessPlaybackStartedByPlayMethodImmediat
ely); |
| 82 } else { | 82 } else { |
| 83 // Wait for viewport visibility. | 83 // Wait for viewport visibility. |
| 84 registerForPositionUpdatesIfNeeded(); | 84 registerForPositionUpdatesIfNeeded(); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | |
| 88 } else if (element().isUserGestureRequiredForPlay()) { | 87 } else if (element().isUserGestureRequiredForPlay()) { |
| 89 unregisterForPositionUpdatesIfNeeded(); | 88 unregisterForPositionUpdatesIfNeeded(); |
| 90 } | 89 } |
| 91 } | 90 } |
| 92 | 91 |
| 93 void AutoplayExperimentHelper::pauseMethodCalled() | 92 void AutoplayExperimentHelper::pauseMethodCalled() |
| 94 { | 93 { |
| 95 // Don't try to autoplay, if we would have. | 94 // Don't try to autoplay, if we would have. |
| 96 m_playPending = false; | 95 m_playPending = false; |
| 97 unregisterForPositionUpdatesIfNeeded(); | 96 unregisterForPositionUpdatesIfNeeded(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 } | 247 } |
| 249 | 248 |
| 250 bool AutoplayExperimentHelper::maybeStartPlaying() | 249 bool AutoplayExperimentHelper::maybeStartPlaying() |
| 251 { | 250 { |
| 252 // See if we're allowed to autoplay now. | 251 // See if we're allowed to autoplay now. |
| 253 if (!isEligible() || !meetsVisibilityRequirements()) { | 252 if (!isEligible() || !meetsVisibilityRequirements()) { |
| 254 return false; | 253 return false; |
| 255 } | 254 } |
| 256 | 255 |
| 257 // Start playing! | 256 // Start playing! |
| 258 prepareToPlay(element().shouldAutoplay() | 257 prepareToAutoplay(element().shouldAutoplay() |
| 259 ? GesturelessPlaybackStartedByAutoplayFlagAfterScroll | 258 ? GesturelessPlaybackStartedByAutoplayFlagAfterScroll |
| 260 : GesturelessPlaybackStartedByPlayMethodAfterScroll); | 259 : GesturelessPlaybackStartedByPlayMethodAfterScroll); |
| 260 |
| 261 // Record that this played without a user gesture. |
| 262 element().autoplayMediaEncountered(); |
| 263 |
| 261 element().playInternal(); | 264 element().playInternal(); |
| 262 | 265 |
| 263 return true; | 266 return true; |
| 264 } | 267 } |
| 265 | 268 |
| 266 bool AutoplayExperimentHelper::isEligible() const | 269 bool AutoplayExperimentHelper::isEligible() const |
| 267 { | 270 { |
| 268 if (m_mode == Mode::ExperimentOff) | 271 if (m_mode == Mode::ExperimentOff) |
| 269 return false; | 272 return false; |
| 270 | 273 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 { | 309 { |
| 307 if (enabled(PlayMuted)) { | 310 if (enabled(PlayMuted)) { |
| 308 ASSERT(!isEligible()); | 311 ASSERT(!isEligible()); |
| 309 // If we are actually changing the muted state, then this will call | 312 // If we are actually changing the muted state, then this will call |
| 310 // mutedChanged(). If isEligible(), then mutedChanged() will try | 313 // mutedChanged(). If isEligible(), then mutedChanged() will try |
| 311 // to start playback, which we should not do here. | 314 // to start playback, which we should not do here. |
| 312 element().setMuted(true); | 315 element().setMuted(true); |
| 313 } | 316 } |
| 314 } | 317 } |
| 315 | 318 |
| 316 void AutoplayExperimentHelper::prepareToPlay(AutoplayMetrics metric) | 319 void AutoplayExperimentHelper::prepareToAutoplay(AutoplayMetrics metric) |
| 317 { | 320 { |
| 318 element().recordAutoplayMetric(metric); | |
| 319 | |
| 320 // This also causes !isEligible, so that we don't allow autoplay more than | 321 // This also causes !isEligible, so that we don't allow autoplay more than |
| 321 // once. Be sure to do this before muteIfNeeded(). | 322 // once. Be sure to do this before muteIfNeeded(). |
| 322 element().removeUserGestureRequirement(); | 323 element().removeUserGestureRequirement(metric); |
| 324 |
| 325 // Don't bother to call autoplayMediaEncountered, since whoever initiates |
| 326 // playback has do it anyway, in case we don't allow autoplay. |
| 323 | 327 |
| 324 unregisterForPositionUpdatesIfNeeded(); | 328 unregisterForPositionUpdatesIfNeeded(); |
| 325 muteIfNeeded(); | 329 muteIfNeeded(); |
| 326 | 330 |
| 327 // Record that this autoplayed without a user gesture. This is normally | |
| 328 // set when we discover an autoplay attribute, but we include all cases | |
| 329 // where playback started without a user gesture, e.g., play(). | |
| 330 element().setInitialPlayWithoutUserGestures(true); | |
| 331 | |
| 332 // Do not actually start playback here. | 331 // Do not actually start playback here. |
| 333 } | 332 } |
| 334 | 333 |
| 335 Document& AutoplayExperimentHelper::document() const | 334 Document& AutoplayExperimentHelper::document() const |
| 336 { | 335 { |
| 337 return element().document(); | 336 return element().document(); |
| 338 } | 337 } |
| 339 | 338 |
| 340 HTMLMediaElement& AutoplayExperimentHelper::element() const | 339 HTMLMediaElement& AutoplayExperimentHelper::element() const |
| 341 { | 340 { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 358 value |= IfMuted; | 357 value |= IfMuted; |
| 359 if (mode.contains("-ifmobile")) | 358 if (mode.contains("-ifmobile")) |
| 360 value |= IfMobile; | 359 value |= IfMobile; |
| 361 if (mode.contains("-playmuted")) | 360 if (mode.contains("-playmuted")) |
| 362 value |= PlayMuted; | 361 value |= PlayMuted; |
| 363 | 362 |
| 364 return value; | 363 return value; |
| 365 } | 364 } |
| 366 | 365 |
| 367 } | 366 } |
| OLD | NEW |