| 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 #ifndef AutoplayExperimentHelper_h | 5 #ifndef AutoplayExperimentHelper_h |
| 6 #define AutoplayExperimentHelper_h | 6 #define AutoplayExperimentHelper_h |
| 7 | 7 |
| 8 #include "core/html/AutoplayExperimentConfig.h" | |
| 9 #include "core/page/Page.h" | 8 #include "core/page/Page.h" |
| 10 #include "platform/Timer.h" | 9 #include "platform/Timer.h" |
| 11 #include "platform/geometry/IntRect.h" | 10 #include "platform/geometry/IntRect.h" |
| 12 | 11 |
| 13 namespace blink { | 12 namespace blink { |
| 14 class Document; | 13 class Document; |
| 15 class HTMLMediaElement; | 14 class HTMLMediaElement; |
| 16 class EventListener; | 15 class EventListener; |
| 17 | 16 |
| 18 // These values are used for a histogram. Do not reorder. | 17 // These values are used for a histogram. Do not reorder. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 69 |
| 71 class AutoplayExperimentHelper { | 70 class AutoplayExperimentHelper { |
| 72 public: | 71 public: |
| 73 AutoplayExperimentHelper(HTMLMediaElement&); | 72 AutoplayExperimentHelper(HTMLMediaElement&); |
| 74 ~AutoplayExperimentHelper(); | 73 ~AutoplayExperimentHelper(); |
| 75 | 74 |
| 76 void becameReadyToPlay(); | 75 void becameReadyToPlay(); |
| 77 void playMethodCalled(); | 76 void playMethodCalled(); |
| 78 void pauseMethodCalled(); | 77 void pauseMethodCalled(); |
| 79 void mutedChanged(); | 78 void mutedChanged(); |
| 80 void positionChanged(); | 79 void positionChanged(const IntRect&); |
| 80 void updatePositionNotificationRegistration(); |
| 81 |
| 82 void triggerAutoplayViewportCheckForTesting(); |
| 83 |
| 84 enum Mode { |
| 85 // Do not enable the autoplay experiment. |
| 86 ExperimentOff = 0, |
| 87 // Enable gestureless autoplay for video elements. |
| 88 ForVideo = 1 << 0, |
| 89 // Enable gestureless autoplay for audio elements. |
| 90 ForAudio = 1 << 1, |
| 91 // Restrict gestureless autoplay to media that is in a visible page. |
| 92 IfPageVisible = 1 << 2, |
| 93 // Restrict gestureless autoplay to media that is visible in |
| 94 // the viewport. |
| 95 IfViewport = 1 << 3, |
| 96 // Restrict gestureless autoplay to audio-less or muted media. |
| 97 IfMuted = 1 << 4, |
| 98 // Restrict gestureless autoplay to sites which contain the |
| 99 // viewport tag. |
| 100 IfMobile = 1 << 5, |
| 101 // If gestureless autoplay is allowed, then mute the media before |
| 102 // starting to play. |
| 103 PlayMuted = 1 << 6, |
| 104 }; |
| 81 | 105 |
| 82 private: | 106 private: |
| 107 // Register to receive position updates, if we haven't already. If we |
| 108 // have, then this does nothing. |
| 109 void registerForPositionUpdatesIfNeeded(); |
| 110 |
| 111 // Un-register for position updates, if we are currently registered. |
| 112 void unregisterForPositionUpdatesIfNeeded(); |
| 113 |
| 83 // Return true if any only if this player meets (most) of the eligibility | 114 // Return true if any only if this player meets (most) of the eligibility |
| 84 // requirements for the experiment to override the need for a user | 115 // requirements for the experiment to override the need for a user |
| 85 // gesture. This includes everything except the visibility test. | 116 // gesture. This includes everything except the visibility test. |
| 86 bool isEligible() const; | 117 bool isEligible() const; |
| 87 | 118 |
| 119 // Return false if and only if m_element is not visible, and we care |
| 120 // that it must be visible. |
| 121 bool meetsVisibilityRequirements() const; |
| 122 |
| 88 // Set the muted flag on the media if we're in an experiment mode that | 123 // Set the muted flag on the media if we're in an experiment mode that |
| 89 // requires it, else do nothing. | 124 // requires it, else do nothing. |
| 90 void muteIfNeeded(); | 125 void muteIfNeeded(); |
| 91 | 126 |
| 92 // Maybe override the requirement for a user gesture, and start playing | 127 // Maybe override the requirement for a user gesture, and start playing |
| 93 // autoplay media. Returns true if only if it starts playback. | 128 // autoplay media. Returns true if only if it starts playback. |
| 94 bool maybeStartPlaying(); | 129 bool maybeStartPlaying(); |
| 95 | 130 |
| 96 // Configure internal state to record that the autoplay experiment is | 131 // Configure internal state to record that the autoplay experiment is |
| 97 // going to start playback. This doesn't actually start playback, since | 132 // going to start playback. This doesn't actually start playback, since |
| 98 // there are several different cases. | 133 // there are several different cases. |
| 99 void prepareToPlay(AutoplayMetrics); | 134 void prepareToPlay(AutoplayMetrics); |
| 100 | 135 |
| 136 // Process a timer for checking visibility. |
| 137 void viewportTimerFired(Timer<AutoplayExperimentHelper>*); |
| 138 |
| 101 // Return our media element's document. | 139 // Return our media element's document. |
| 102 Document& document() const; | 140 Document& document() const; |
| 103 | 141 |
| 104 inline bool enabled(AutoplayExperimentConfig::Mode mode) const | 142 inline bool enabled(Mode mode) const |
| 105 { | 143 { |
| 106 return ((int)m_mode) & ((int)mode); | 144 return ((int)m_mode) & ((int)mode); |
| 107 } | 145 } |
| 108 | 146 |
| 147 Mode fromString(const String& mode); |
| 148 |
| 109 private: | 149 private: |
| 110 HTMLMediaElement& m_element; | 150 HTMLMediaElement& m_element; |
| 111 | 151 |
| 112 AutoplayExperimentConfig::Mode m_mode; | 152 Mode m_mode; |
| 113 | 153 |
| 114 // Autoplay experiment state. | 154 // Autoplay experiment state. |
| 115 // True if we've received a play() without a pause(). | 155 // True if we've received a play() without a pause(). |
| 116 bool m_playPending : 1; | 156 bool m_playPending : 1; |
| 117 | 157 |
| 118 friend class Internals; | 158 // Are we registered with the view for position updates? |
| 159 bool m_registeredWithLayoutObject : 1; |
| 160 |
| 161 // According to our last position update, are we in the viewport? |
| 162 bool m_wasInViewport : 1; |
| 163 |
| 164 // According to our last position update, where was our element? |
| 165 IntRect m_lastLocation; |
| 166 IntRect m_lastVisibleRect; |
| 167 |
| 168 // When was m_lastLocation set? |
| 169 double m_lastLocationUpdateTime; |
| 170 |
| 171 Timer<AutoplayExperimentHelper> m_viewportTimer; |
| 119 }; | 172 }; |
| 120 | 173 |
| 174 inline AutoplayExperimentHelper::Mode& operator|=(AutoplayExperimentHelper::Mode
& a, |
| 175 const AutoplayExperimentHelper::Mode& b) |
| 176 { |
| 177 a = static_cast<AutoplayExperimentHelper::Mode>(static_cast<int>(a) | static
_cast<int>(b)); |
| 178 return a; |
| 179 } |
| 180 |
| 181 |
| 121 } // namespace blink | 182 } // namespace blink |
| 122 | 183 |
| 123 #endif // AutoplayExperimentHelper_h | 184 #endif // AutoplayExperimentHelper_h |
| OLD | NEW |