[iOS] allow host application to opt-out of alternate fullscreen
[WebKit-https.git] / Source / WebCore / Modules / mediacontrols / MediaControlsHost.cpp
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
29
30 #include "MediaControlsHost.h"
31
32 #include "CaptionUserPreferences.h"
33 #include "Element.h"
34 #include "HTMLMediaElement.h"
35 #include "Logging.h"
36 #include "MediaControlElements.h"
37 #include "Page.h"
38 #include "PageGroup.h"
39 #include "TextTrack.h"
40 #include "TextTrackList.h"
41 #include <runtime/JSCJSValueInlines.h>
42
43 namespace WebCore {
44
45 const AtomicString& MediaControlsHost::automaticKeyword()
46 {
47     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, automatic, ("automatic", AtomicString::ConstructFromLiteral));
48     return automatic;
49 }
50
51 const AtomicString& MediaControlsHost::forcedOnlyKeyword()
52 {
53     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, forcedOn, ("forced-only", AtomicString::ConstructFromLiteral));
54     return forcedOn;
55 }
56
57 const AtomicString& MediaControlsHost::alwaysOnKeyword()
58 {
59     DEPRECATED_DEFINE_STATIC_LOCAL(const AtomicString, alwaysOn, ("always-on", AtomicString::ConstructFromLiteral));
60     return alwaysOn;
61 }
62
63 PassRefPtr<MediaControlsHost> MediaControlsHost::create(HTMLMediaElement* mediaElement)
64 {
65     return adoptRef(new MediaControlsHost(mediaElement));
66 }
67
68 MediaControlsHost::MediaControlsHost(HTMLMediaElement* mediaElement)
69     : m_mediaElement(mediaElement)
70 {
71     ASSERT(mediaElement);
72 }
73
74 MediaControlsHost::~MediaControlsHost()
75 {
76 }
77
78 Vector<RefPtr<TextTrack>> MediaControlsHost::sortedTrackListForMenu(TextTrackList* trackList)
79 {
80     if (!trackList)
81         return Vector<RefPtr<TextTrack>>();
82
83     Page* page = m_mediaElement->document().page();
84     if (!page)
85         return Vector<RefPtr<TextTrack>>();
86
87     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
88     return captionPreferences->sortedTrackListForMenu(trackList);
89 }
90
91 Vector<RefPtr<AudioTrack>> MediaControlsHost::sortedTrackListForMenu(AudioTrackList* trackList)
92 {
93     if (!trackList)
94         return Vector<RefPtr<AudioTrack>>();
95
96     Page* page = m_mediaElement->document().page();
97     if (!page)
98         return Vector<RefPtr<AudioTrack>>();
99
100     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
101     return captionPreferences->sortedTrackListForMenu(trackList);
102 }
103
104 String MediaControlsHost::displayNameForTrack(TextTrack* track)
105 {
106     if (!track)
107         return emptyString();
108
109     Page* page = m_mediaElement->document().page();
110     if (!page)
111         return emptyString();
112
113     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
114     return captionPreferences->displayNameForTrack(track);
115 }
116
117 String MediaControlsHost::displayNameForTrack(AudioTrack* track)
118 {
119     if (!track)
120         return emptyString();
121
122     Page* page = m_mediaElement->document().page();
123     if (!page)
124         return emptyString();
125
126     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
127     return captionPreferences->displayNameForTrack(track);
128 }
129
130 TextTrack* MediaControlsHost::captionMenuOffItem()
131 {
132     return TextTrack::captionMenuOffItem();
133 }
134
135 TextTrack* MediaControlsHost::captionMenuAutomaticItem()
136 {
137     return TextTrack::captionMenuAutomaticItem();
138 }
139
140 AtomicString MediaControlsHost::captionDisplayMode()
141 {
142     Page* page = m_mediaElement->document().page();
143     if (!page)
144         return emptyAtom;
145
146     switch (page->group().captionPreferences()->captionDisplayMode()) {
147     case CaptionUserPreferences::Automatic:
148         return automaticKeyword();
149     case CaptionUserPreferences::ForcedOnly:
150         return forcedOnlyKeyword();
151     case CaptionUserPreferences::AlwaysOn:
152         return alwaysOnKeyword();
153     default:
154         ASSERT_NOT_REACHED();
155         return emptyAtom;
156     }
157 }
158
159 void MediaControlsHost::setSelectedTextTrack(TextTrack* track)
160 {
161     m_mediaElement->setSelectedTextTrack(track);
162 }
163
164 Element* MediaControlsHost::textTrackContainer()
165 {
166     if (!m_textTrackContainer) {
167         m_textTrackContainer = MediaControlTextTrackContainerElement::create(m_mediaElement->document());
168         m_textTrackContainer->setMediaController(m_mediaElement);
169     }
170     return m_textTrackContainer.get();
171 }
172
173 void MediaControlsHost::updateTextTrackContainer()
174 {
175     if (m_textTrackContainer)
176         m_textTrackContainer->updateDisplay();
177 }
178
179 void MediaControlsHost::enteredFullscreen()
180 {
181     if (m_textTrackContainer)
182         m_textTrackContainer->enteredFullscreen();
183 }
184
185 void MediaControlsHost::exitedFullscreen()
186 {
187     if (m_textTrackContainer)
188         m_textTrackContainer->exitedFullscreen();
189 }
190     
191 void MediaControlsHost::enterFullscreenOptimized()
192 {
193 #if PLATFORM(IOS)
194     m_mediaElement->enterFullscreenOptimized();
195 #endif
196 }
197     
198 bool MediaControlsHost::optimizedFullscreenSupported()
199 {
200 #if PLATFORM(IOS)
201     if (!wkIsOptimizedFullscreenSupported())
202         return false;
203 #endif
204
205     return m_mediaElement->mediaSession().allowsAlternateFullscreen(*m_mediaElement);
206 }
207
208 void MediaControlsHost::updateCaptionDisplaySizes()
209 {
210     if (m_textTrackContainer)
211         m_textTrackContainer->updateSizes(true);
212 }
213     
214 bool MediaControlsHost::mediaPlaybackAllowsInline() const
215 {
216     return !m_mediaElement->mediaSession().requiresFullscreenForVideoPlayback(*m_mediaElement);
217 }
218
219 bool MediaControlsHost::supportsFullscreen()
220 {
221     return m_mediaElement->supportsFullscreen();
222 }
223
224 bool MediaControlsHost::userGestureRequired() const
225 {
226     return !m_mediaElement->mediaSession().playbackPermitted(*m_mediaElement);
227 }
228
229 String MediaControlsHost::externalDeviceDisplayName() const
230 {
231 #if ENABLE(IOS_AIRPLAY)
232     MediaPlayer* player = m_mediaElement->player();
233     if (!player) {
234         LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"\" because player is NULL");
235         return emptyString();
236     }
237     
238     String name = player->wirelessPlaybackTargetName();
239     LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"%s\"", name.utf8().data());
240     
241     return name;
242 #else
243     return emptyString();
244 #endif
245 }
246
247 String MediaControlsHost::externalDeviceType() const
248 {
249     DEPRECATED_DEFINE_STATIC_LOCAL(String, none, (ASCIILiteral("none")));
250     String type = none;
251     
252 #if ENABLE(IOS_AIRPLAY)
253     DEPRECATED_DEFINE_STATIC_LOCAL(String, airplay, (ASCIILiteral("airplay")));
254     DEPRECATED_DEFINE_STATIC_LOCAL(String, tvout, (ASCIILiteral("tvout")));
255     
256     MediaPlayer* player = m_mediaElement->player();
257     if (!player) {
258         LOG(Media, "MediaControlsHost::externalDeviceType - returning \"none\" because player is NULL");
259         return none;
260     }
261     
262     switch (player->wirelessPlaybackTargetType()) {
263     case MediaPlayer::TargetTypeNone:
264         type = none;
265         break;
266     case MediaPlayer::TargetTypeAirPlay:
267         type = airplay;
268         break;
269     case MediaPlayer::TargetTypeTVOut:
270         type = tvout;
271         break;
272     }
273 #endif
274     
275     LOG(Media, "MediaControlsHost::externalDeviceType - returning \"%s\"", type.utf8().data());
276     
277     return type;
278 }
279
280 bool MediaControlsHost::controlsDependOnPageScaleFactor() const
281 {
282     return m_mediaElement->mediaControlsDependOnPageScaleFactor();
283 }
284
285 void MediaControlsHost::setControlsDependOnPageScaleFactor(bool value)
286 {
287     m_mediaElement->setMediaControlsDependOnPageScaleFactor(value);
288 }
289
290 String MediaControlsHost::fullscreenMode() const
291 {
292     DEPRECATED_DEFINE_STATIC_LOCAL(String, none, (ASCIILiteral("none")));
293     DEPRECATED_DEFINE_STATIC_LOCAL(String, standard, (ASCIILiteral("standard")));
294     DEPRECATED_DEFINE_STATIC_LOCAL(String, optimized, (ASCIILiteral("optimized")));
295     String mode = none;
296
297     enum VideoFullscreenMode { VideoFullscreenModeNone, VideoFullscreenModeStandard, VideoFullscreenModeOptimized };
298
299     switch (m_mediaElement->fullscreenMode()) {
300     case HTMLMediaElement::VideoFullscreenModeNone:
301         mode = none;
302         break;
303     case HTMLMediaElement::VideoFullscreenModeStandard:
304         mode = standard;
305         break;
306     case HTMLMediaElement::VideoFullscreenModeOptimized:
307         mode = optimized;
308         break;
309     }
310     
311     LOG(Media, "MediaControlsHost::fullscreenMode - returning \"%s\"", mode.utf8().data());
312
313     return mode;
314 }
315
316 String MediaControlsHost::mediaUIImageData(const String& partID) const
317 {
318 #if PLATFORM(IOS)
319     if (partID == "optimized-fullscreen-button")
320         return wkGetMediaUIImageData(wkMediaUIPartOptimizedFullscreenButton);
321
322     if (partID == "optimized-fullscreen-placeholder")
323         return wkGetMediaUIImageData(wkMediaUIPartOptimizedFullscreenPlaceholder);
324 #else
325     UNUSED_PARAM(partID);
326 #endif
327
328     return emptyString();
329 }
330
331 }
332
333 #endif