Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / html / shadow / MediaControls.cpp
1 /*
2  * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3  * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28
29 #if ENABLE(VIDEO)
30 #include "MediaControls.h"
31
32 #include "ExceptionCodePlaceholder.h"
33 #include "Settings.h"
34
35 namespace WebCore {
36
37 MediaControls::MediaControls(Document& document)
38     : HTMLDivElement(HTMLNames::divTag, document)
39     , m_mediaController(0)
40     , m_panel(0)
41 #if ENABLE(VIDEO_TRACK)
42     , m_textDisplayContainer(0)
43 #endif
44     , m_playButton(0)
45     , m_currentTimeDisplay(0)
46     , m_timeline(0)
47     , m_panelMuteButton(0)
48     , m_volumeSlider(0)
49     , m_toggleClosedCaptionsButton(0)
50     , m_fullScreenButton(0)
51     , m_hideFullscreenControlsTimer(*this, &MediaControls::hideFullscreenControlsTimerFired)
52     , m_isFullscreen(false)
53     , m_isMouseOverControls(false)
54 {
55     setPseudo(AtomicString("-webkit-media-controls", AtomicString::ConstructFromLiteral));
56 }
57
58 void MediaControls::setMediaController(MediaControllerInterface* controller)
59 {
60     if (m_mediaController == controller)
61         return;
62     m_mediaController = controller;
63
64     if (m_panel)
65         m_panel->setMediaController(controller);
66 #if ENABLE(VIDEO_TRACK)
67     if (m_textDisplayContainer)
68         m_textDisplayContainer->setMediaController(controller);
69 #endif
70     if (m_playButton)
71         m_playButton->setMediaController(controller);
72     if (m_currentTimeDisplay)
73         m_currentTimeDisplay->setMediaController(controller);
74     if (m_timeline)
75         m_timeline->setMediaController(controller);
76     if (m_panelMuteButton)
77         m_panelMuteButton->setMediaController(controller);
78     if (m_volumeSlider)
79         m_volumeSlider->setMediaController(controller);
80     if (m_toggleClosedCaptionsButton)
81         m_toggleClosedCaptionsButton->setMediaController(controller);
82     if (m_fullScreenButton)
83         m_fullScreenButton->setMediaController(controller);
84 }
85
86 void MediaControls::reset()
87 {
88     Page* page = document().page();
89     if (!page)
90         return;
91
92     m_playButton->updateDisplayType();
93
94     updateCurrentTimeDisplay();
95
96     double duration = m_mediaController->duration();
97     if (std::isfinite(duration) || page->theme().hasOwnDisabledStateHandlingFor(MediaSliderPart)) {
98         m_timeline->setDuration(duration);
99         m_timeline->setPosition(m_mediaController->currentTime());
100     }
101
102     if (m_mediaController->hasAudio() || page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart))
103         m_panelMuteButton->show();
104     else
105         m_panelMuteButton->hide();
106
107     if (m_volumeSlider) {
108         if (!m_mediaController->hasAudio())
109             m_volumeSlider->hide();
110         else {
111             m_volumeSlider->show();
112             setSliderVolume();
113         }
114     }
115
116     refreshClosedCaptionsButtonVisibility();
117
118     if (m_fullScreenButton) {
119         if (m_mediaController->supportsFullscreen() && m_mediaController->hasVideo())
120             m_fullScreenButton->show();
121         else
122             m_fullScreenButton->hide();
123     }
124
125     makeOpaque();
126 }
127
128 void MediaControls::reportedError()
129 {
130     Page* page = document().page();
131     if (!page)
132         return;
133
134     if (!page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart)) {
135         m_panelMuteButton->hide();
136         m_volumeSlider->hide();
137     }
138
139     if (m_toggleClosedCaptionsButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaToggleClosedCaptionsButtonPart))
140         m_toggleClosedCaptionsButton->hide();
141
142     if (m_fullScreenButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaEnterFullscreenButtonPart))
143         m_fullScreenButton->hide();
144 }
145
146 void MediaControls::loadedMetadata()
147 {
148     reset();
149 }
150
151 void MediaControls::show()
152 {
153     makeOpaque();
154     m_panel->setIsDisplayed(true);
155     m_panel->show();
156 }
157
158 void MediaControls::hide()
159 {
160     m_panel->setIsDisplayed(false);
161     m_panel->hide();
162 }
163
164 void MediaControls::makeOpaque()
165 {
166     m_panel->makeOpaque();
167 }
168
169 void MediaControls::makeTransparent()
170 {
171     m_panel->makeTransparent();
172 }
173
174 bool MediaControls::shouldHideControls()
175 {
176     return !m_panel->hovered();
177 }
178
179 void MediaControls::bufferingProgressed()
180 {
181     // We only need to update buffering progress when paused, during normal
182     // playback playbackProgressed() will take care of it.
183     if (m_mediaController->paused())
184         m_timeline->setPosition(m_mediaController->currentTime());
185 }
186
187 void MediaControls::playbackStarted()
188 {
189     m_playButton->updateDisplayType();
190     m_timeline->setPosition(m_mediaController->currentTime());
191     updateCurrentTimeDisplay();
192
193     if (m_isFullscreen)
194         startHideFullscreenControlsTimer();
195 }
196
197 void MediaControls::playbackProgressed()
198 {
199     m_timeline->setPosition(m_mediaController->currentTime());
200     updateCurrentTimeDisplay();
201
202     if (!m_isMouseOverControls && m_mediaController->hasVideo())
203         makeTransparent();
204 }
205
206 void MediaControls::playbackStopped()
207 {
208     m_playButton->updateDisplayType();
209     m_timeline->setPosition(m_mediaController->currentTime());
210     updateCurrentTimeDisplay();
211     makeOpaque();
212
213     stopHideFullscreenControlsTimer();
214 }
215
216 void MediaControls::updateCurrentTimeDisplay()
217 {
218     double now = m_mediaController->currentTime();
219
220     Page* page = document().page();
221     if (!page)
222         return;
223
224     m_currentTimeDisplay->setInnerText(page->theme().formatMediaControlsTime(now), IGNORE_EXCEPTION);
225     m_currentTimeDisplay->setCurrentValue(now);
226 }
227
228 void MediaControls::showVolumeSlider()
229 {
230     if (!m_mediaController->hasAudio())
231         return;
232
233     m_volumeSlider->show();
234 }
235
236 void MediaControls::changedMute()
237 {
238     m_panelMuteButton->changedMute();
239 }
240
241 void MediaControls::changedVolume()
242 {
243     if (m_volumeSlider)
244         setSliderVolume();
245     if (m_panelMuteButton && m_panelMuteButton->renderer())
246         m_panelMuteButton->renderer()->repaint();
247 }
248
249 void MediaControls::changedClosedCaptionsVisibility()
250 {
251     if (m_toggleClosedCaptionsButton)
252         m_toggleClosedCaptionsButton->updateDisplayType();
253 }
254
255 void MediaControls::refreshClosedCaptionsButtonVisibility()
256 {
257     if (!m_toggleClosedCaptionsButton)
258         return;
259
260     if (m_mediaController->hasClosedCaptions())
261         m_toggleClosedCaptionsButton->show();
262     else
263         m_toggleClosedCaptionsButton->hide();
264 }
265
266 void MediaControls::closedCaptionTracksChanged()
267 {
268     refreshClosedCaptionsButtonVisibility();
269 }
270
271 void MediaControls::enteredFullscreen()
272 {
273     m_isFullscreen = true;
274     m_fullScreenButton->setIsFullscreen(true);
275
276     if (Page* page = document().page())
277         page->chrome().setCursorHiddenUntilMouseMoves(true);
278
279     startHideFullscreenControlsTimer();
280 #if ENABLE(VIDEO_TRACK)
281     if (m_textDisplayContainer)
282         m_textDisplayContainer->enteredFullscreen();
283 #endif
284 }
285
286 void MediaControls::exitedFullscreen()
287 {
288     m_isFullscreen = false;
289     m_fullScreenButton->setIsFullscreen(false);
290     stopHideFullscreenControlsTimer();
291 #if ENABLE(VIDEO_TRACK)
292     if (m_textDisplayContainer)
293         m_textDisplayContainer->exitedFullscreen();
294 #endif
295 }
296
297 void MediaControls::defaultEventHandler(Event* event)
298 {
299     HTMLDivElement::defaultEventHandler(event);
300
301     if (event->type() == eventNames().mouseoverEvent) {
302         if (!containsRelatedTarget(event)) {
303             m_isMouseOverControls = true;
304             if (!m_mediaController->canPlay()) {
305                 makeOpaque();
306                 if (shouldHideControls())
307                     startHideFullscreenControlsTimer();
308             }
309         }
310         return;
311     }
312
313     if (event->type() == eventNames().mouseoutEvent) {
314         if (!containsRelatedTarget(event)) {
315             m_isMouseOverControls = false;
316             stopHideFullscreenControlsTimer();
317         }
318         return;
319     }
320
321     if (event->type() == eventNames().mousemoveEvent) {
322         if (m_isFullscreen) {
323             // When we get a mouse move in fullscreen mode, show the media controls, and start a timer
324             // that will hide the media controls after a 3 seconds without a mouse move.
325             makeOpaque();
326             if (shouldHideControls())
327                 startHideFullscreenControlsTimer();
328         }
329         return;
330     }
331 }
332
333 void MediaControls::hideFullscreenControlsTimerFired()
334 {
335     if (m_mediaController->paused())
336         return;
337
338     if (!m_isFullscreen)
339         return;
340
341     if (!shouldHideControls())
342         return;
343
344     if (Page* page = document().page())
345         page->chrome().setCursorHiddenUntilMouseMoves(true);
346
347     makeTransparent();
348 }
349
350 void MediaControls::startHideFullscreenControlsTimer()
351 {
352     if (!m_isFullscreen)
353         return;
354
355     Page* page = document().page();
356     if (!page)
357         return;
358
359     m_hideFullscreenControlsTimer.startOneShot(page->settings().timeWithoutMouseMovementBeforeHidingControls());
360 }
361
362 void MediaControls::stopHideFullscreenControlsTimer()
363 {
364     m_hideFullscreenControlsTimer.stop();
365 }
366
367 bool MediaControls::containsRelatedTarget(Event* event)
368 {
369     if (!is<MouseEvent>(*event))
370         return false;
371     EventTarget* relatedTarget = downcast<MouseEvent>(*event).relatedTarget();
372     if (!relatedTarget)
373         return false;
374     return contains(relatedTarget->toNode());
375 }
376
377 #if ENABLE(VIDEO_TRACK)
378 void MediaControls::createTextTrackDisplay()
379 {
380     if (m_textDisplayContainer)
381         return;
382
383     Ref<MediaControlTextTrackContainerElement> textDisplayContainer = MediaControlTextTrackContainerElement::create(document());
384     m_textDisplayContainer = textDisplayContainer.ptr();
385
386     if (m_mediaController)
387         m_textDisplayContainer->setMediaController(m_mediaController);
388
389     // Insert it before the first controller element so it always displays behind the controls.
390     insertBefore(WTFMove(textDisplayContainer), m_panel, IGNORE_EXCEPTION);
391 }
392
393 void MediaControls::showTextTrackDisplay()
394 {
395     if (!m_textDisplayContainer)
396         createTextTrackDisplay();
397     m_textDisplayContainer->show();
398 }
399
400 void MediaControls::hideTextTrackDisplay()
401 {
402     if (!m_textDisplayContainer)
403         createTextTrackDisplay();
404     m_textDisplayContainer->hide();
405 }
406
407 void MediaControls::updateTextTrackDisplay()
408 {
409     if (!m_textDisplayContainer)
410         createTextTrackDisplay();
411
412     m_textDisplayContainer->updateDisplay();
413 }
414     
415 void MediaControls::textTrackPreferencesChanged()
416 {
417     closedCaptionTracksChanged();
418     if (m_textDisplayContainer)
419         m_textDisplayContainer->updateSizes(true);
420 }
421 #endif
422
423 void MediaControls::setSliderVolume()
424 {
425     m_volumeSlider->setVolume(m_mediaController->muted() ? 0.0 : m_mediaController->volume());
426 }
427
428 }
429
430 #endif