Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: Source/core/html/AutoplayExperimentHelper.h

Issue 1329853004: Include viewport visibility checks for autoplay experiment. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@autoplay_step1
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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. Also supports
87 // "not valid", in case some of the information isn't available.
88 class LocationState {
89 public:
90 LocationState() : m_valid(false) {}
91 LocationState(Element&);
92
93 bool valid() const { return m_valid; }
94 PageVisibilityState visibilityState() const { return m_visibilityState; }
95 const IntRect& element() const { return m_element; }
96 const IntRect& screen() const { return m_screen; }
97
98 bool operator==(const LocationState&) const;
99 bool operator!=(const LocationState&) const;
100
101 // Return true if and only if the player is visible.
102 bool isInViewport();
103
104 private:
105 bool m_valid;
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 player is not visible, and we care
124 // that it must be visible.
125 bool isInViewportIfNeeded();
126
88 // Set the muted flag on the media if we're in an experiment mode that 127 // Set the muted flag on the media if we're in an experiment mode that
89 // requires it, else do nothing. 128 // requires it, else do nothing.
90 void muteIfNeeded(); 129 void muteIfNeeded();
91 130
92 // Maybe override the requirement for a user gesture, and start playing 131 // Maybe override the requirement for a user gesture, and start playing
93 // autoplay media. Returns true if only if it starts playback. 132 // autoplay media. Returns true if only if it starts playback.
94 bool maybeStartPlaying(); 133 bool maybeStartPlaying();
95 134
96 // Configure internal state to record that the autoplay experiment is 135 // Configure internal state to record that the autoplay experiment is
97 // going to start playback. This doesn't actually start playback, since 136 // going to start playback. This doesn't actually start playback, since
98 // there are several different cases. 137 // there are several different cases.
99 void prepareToPlay(AutoplayMetrics); 138 void prepareToPlay(AutoplayMetrics);
100 139
140 // Process a timer for checking visibility.
141 void viewportTimerFired(Timer<AutoplayExperimentHelper>*);
142
101 // Return our media element's document. 143 // Return our media element's document.
102 Document& document() const; 144 Document& document() const;
103 145
104 inline bool enabled(AutoplayExperimentConfig::Mode mode) const 146 inline bool enabled(AutoplayExperimentConfig::Mode mode) const
105 { 147 {
106 return ((int)m_mode) & ((int)mode); 148 return ((int)m_mode) & ((int)mode);
107 } 149 }
108 150
109 private: 151 private:
110 HTMLMediaElement& m_element; 152 HTMLMediaElement& m_element;
111 153
112 AutoplayExperimentConfig::Mode m_mode; 154 AutoplayExperimentConfig::Mode m_mode;
113 155
114 // Autoplay experiment state. 156 // Autoplay experiment state.
115 // True if we've received a play() without a pause(). 157 // True if we've received a play() without a pause().
116 bool m_playPending : 1; 158 bool m_playPending : 1;
117 159
160 // Are we registered with the view for position updates?
161 bool m_registeredWithView : 1;
162
163 // According to our last position update, are we in the viewport?
164 bool m_wasInViewport : 1;
165
166 Timer<AutoplayExperimentHelper> m_viewportTimer;
167
118 friend class Internals; 168 friend class Internals;
119 }; 169 };
120 170
121 } // namespace blink 171 } // namespace blink
122 172
123 #endif // AutoplayExperimentHelper_h 173 #endif // AutoplayExperimentHelper_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698