| 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" | 8 #include "core/html/AutoplayExperimentConfig.h" |
| 9 #include "core/page/Page.h" | 9 #include "core/page/Page.h" |
| 10 #include "platform/Timer.h" | 10 #include "platform/Timer.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 public: | 72 public: |
| 73 AutoplayExperimentHelper(HTMLMediaElement&); | 73 AutoplayExperimentHelper(HTMLMediaElement&); |
| 74 ~AutoplayExperimentHelper(); | 74 ~AutoplayExperimentHelper(); |
| 75 | 75 |
| 76 void becameReadyToPlay(); | 76 void becameReadyToPlay(); |
| 77 void playMethodCalled(); | 77 void playMethodCalled(); |
| 78 void pauseMethodCalled(); | 78 void pauseMethodCalled(); |
| 79 void mutedChanged(); | 79 void mutedChanged(); |
| 80 void positionChanged(); | 80 void positionChanged(); |
| 81 | 81 |
| 82 // For testing. |
| 83 void triggerAutoplayViewportCheck(); |
| 84 |
| 82 private: | 85 private: |
| 86 // The location and size of our element, and the viewport. |
| 87 class LocationState { |
| 88 public: |
| 89 LocationState() {} |
| 90 LocationState(Element&); |
| 91 |
| 92 PageVisibilityState visibilityState() const { return m_visibilityState;
} |
| 93 const IntRect& element() const { return m_element; } |
| 94 const IntRect& screen() const { return m_screen; } |
| 95 |
| 96 bool operator==(const LocationState&) const; |
| 97 bool operator!=(const LocationState&) const; |
| 98 |
| 99 // Return true if and only if the element is visible. |
| 100 bool isElementVisible() const; |
| 101 |
| 102 // Return true if and only if the element's page is visible. |
| 103 bool isPageVisible() const; |
| 104 |
| 105 private: |
| 106 PageVisibilityState m_visibilityState; |
| 107 IntRect m_element; |
| 108 IntRect m_screen; |
| 109 }; |
| 110 |
| 111 // Register to receive position updates, if we haven't already. If we |
| 112 // have, then this does nothing. |
| 113 void registerForPositionUpdatesIfNeeded(); |
| 114 |
| 115 // Un-register for position updates, if we are currently registered. |
| 116 void unregisterForPositionUpdatesIfNeeded(); |
| 117 |
| 83 // Return true if any only if this player meets (most) of the eligibility | 118 // 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 | 119 // requirements for the experiment to override the need for a user |
| 85 // gesture. This includes everything except the visibility test. | 120 // gesture. This includes everything except the visibility test. |
| 86 bool isEligible() const; | 121 bool isEligible() const; |
| 87 | 122 |
| 123 // Return false if and only if the element described by LocationState is |
| 124 // not visible, and we care that it must be visible. |
| 125 bool meetsVisibilityRequirements(const LocationState&) const; |
| 126 |
| 127 // Return false if and only if m_element is not visible, and we care |
| 128 // that it must be visible. This is equivalent to calling |
| 129 // meetsVisibilityRequirements(LocationState(m_element)) |
| 130 bool meetsVisibilityRequirements() const; |
| 131 |
| 88 // Set the muted flag on the media if we're in an experiment mode that | 132 // Set the muted flag on the media if we're in an experiment mode that |
| 89 // requires it, else do nothing. | 133 // requires it, else do nothing. |
| 90 void muteIfNeeded(); | 134 void muteIfNeeded(); |
| 91 | 135 |
| 92 // Maybe override the requirement for a user gesture, and start playing | 136 // Maybe override the requirement for a user gesture, and start playing |
| 93 // autoplay media. Returns true if only if it starts playback. | 137 // autoplay media. Returns true if only if it starts playback. |
| 94 bool maybeStartPlaying(); | 138 bool maybeStartPlaying(); |
| 95 | 139 |
| 96 // Configure internal state to record that the autoplay experiment is | 140 // Configure internal state to record that the autoplay experiment is |
| 97 // going to start playback. This doesn't actually start playback, since | 141 // going to start playback. This doesn't actually start playback, since |
| 98 // there are several different cases. | 142 // there are several different cases. |
| 99 void prepareToPlay(AutoplayMetrics); | 143 void prepareToPlay(AutoplayMetrics); |
| 100 | 144 |
| 145 // Process a timer for checking visibility. |
| 146 void viewportTimerFired(Timer<AutoplayExperimentHelper>*); |
| 147 |
| 101 // Return our media element's document. | 148 // Return our media element's document. |
| 102 Document& document() const; | 149 Document& document() const; |
| 103 | 150 |
| 104 inline bool enabled(AutoplayExperimentConfig::Mode mode) const | 151 inline bool enabled(AutoplayExperimentConfig::Mode mode) const |
| 105 { | 152 { |
| 106 return ((int)m_mode) & ((int)mode); | 153 return ((int)m_mode) & ((int)mode); |
| 107 } | 154 } |
| 108 | 155 |
| 109 private: | 156 private: |
| 110 HTMLMediaElement& m_element; | 157 HTMLMediaElement& m_element; |
| 111 | 158 |
| 112 AutoplayExperimentConfig::Mode m_mode; | 159 AutoplayExperimentConfig::Mode m_mode; |
| 113 | 160 |
| 114 // Autoplay experiment state. | 161 // Autoplay experiment state. |
| 115 // True if we've received a play() without a pause(). | 162 // True if we've received a play() without a pause(). |
| 116 bool m_playPending : 1; | 163 bool m_playPending : 1; |
| 117 | 164 |
| 165 // Are we registered with the view for position updates? |
| 166 bool m_registeredWithLayoutObject : 1; |
| 167 |
| 168 // According to our last position update, are we in the viewport? |
| 169 bool m_wasInViewport : 1; |
| 170 |
| 171 // According to our last position update, where was our element? |
| 172 LocationState m_lastLocation; |
| 173 |
| 174 // When was m_lastLocation set? |
| 175 double m_lastLocationUpdateTime; |
| 176 |
| 177 Timer<AutoplayExperimentHelper> m_viewportTimer; |
| 178 |
| 118 friend class Internals; | 179 friend class Internals; |
| 119 }; | 180 }; |
| 120 | 181 |
| 121 } // namespace blink | 182 } // namespace blink |
| 122 | 183 |
| 123 #endif // AutoplayExperimentHelper_h | 184 #endif // AutoplayExperimentHelper_h |
| OLD | NEW |