[Modern Media Controls] Add a MediaControlsHost API to retrieve the shadow root CSS
[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 "RenderTheme.h"
40 #include "TextTrack.h"
41 #include "TextTrackList.h"
42 #include "UUID.h"
43 #include <runtime/JSCJSValueInlines.h>
44
45 namespace WebCore {
46
47 const AtomicString& MediaControlsHost::automaticKeyword()
48 {
49     static NeverDestroyed<const AtomicString> automatic("automatic", AtomicString::ConstructFromLiteral);
50     return automatic;
51 }
52
53 const AtomicString& MediaControlsHost::forcedOnlyKeyword()
54 {
55     static NeverDestroyed<const AtomicString> forcedOn("forced-only", AtomicString::ConstructFromLiteral);
56     return forcedOn;
57 }
58
59 const AtomicString& MediaControlsHost::alwaysOnKeyword()
60 {
61     static NeverDestroyed<const AtomicString> alwaysOn("always-on", AtomicString::ConstructFromLiteral);
62     return alwaysOn;
63 }
64
65 const AtomicString& MediaControlsHost::manualKeyword()
66 {
67     static NeverDestroyed<const AtomicString> alwaysOn("manual", AtomicString::ConstructFromLiteral);
68     return alwaysOn;
69 }
70
71
72 Ref<MediaControlsHost> MediaControlsHost::create(HTMLMediaElement* mediaElement)
73 {
74     return adoptRef(*new MediaControlsHost(mediaElement));
75 }
76
77 MediaControlsHost::MediaControlsHost(HTMLMediaElement* mediaElement)
78     : m_mediaElement(mediaElement)
79 {
80     ASSERT(mediaElement);
81 }
82
83 MediaControlsHost::~MediaControlsHost()
84 {
85 }
86
87 Vector<RefPtr<TextTrack>> MediaControlsHost::sortedTrackListForMenu(TextTrackList* trackList)
88 {
89     if (!trackList)
90         return Vector<RefPtr<TextTrack>>();
91
92     Page* page = m_mediaElement->document().page();
93     if (!page)
94         return Vector<RefPtr<TextTrack>>();
95
96     return page->group().captionPreferences().sortedTrackListForMenu(trackList);
97 }
98
99 Vector<RefPtr<AudioTrack>> MediaControlsHost::sortedTrackListForMenu(AudioTrackList* trackList)
100 {
101     if (!trackList)
102         return Vector<RefPtr<AudioTrack>>();
103
104     Page* page = m_mediaElement->document().page();
105     if (!page)
106         return Vector<RefPtr<AudioTrack>>();
107
108     return page->group().captionPreferences().sortedTrackListForMenu(trackList);
109 }
110
111 String MediaControlsHost::displayNameForTrack(TextTrack* track)
112 {
113     if (!track)
114         return emptyString();
115
116     Page* page = m_mediaElement->document().page();
117     if (!page)
118         return emptyString();
119
120     return page->group().captionPreferences().displayNameForTrack(track);
121 }
122
123 String MediaControlsHost::displayNameForTrack(AudioTrack* track)
124 {
125     if (!track)
126         return emptyString();
127
128     Page* page = m_mediaElement->document().page();
129     if (!page)
130         return emptyString();
131
132     return page->group().captionPreferences().displayNameForTrack(track);
133 }
134
135 TextTrack* MediaControlsHost::captionMenuOffItem()
136 {
137     return TextTrack::captionMenuOffItem();
138 }
139
140 TextTrack* MediaControlsHost::captionMenuAutomaticItem()
141 {
142     return TextTrack::captionMenuAutomaticItem();
143 }
144
145 AtomicString MediaControlsHost::captionDisplayMode()
146 {
147     Page* page = m_mediaElement->document().page();
148     if (!page)
149         return emptyAtom;
150
151     switch (page->group().captionPreferences().captionDisplayMode()) {
152     case CaptionUserPreferences::Automatic:
153         return automaticKeyword();
154     case CaptionUserPreferences::ForcedOnly:
155         return forcedOnlyKeyword();
156     case CaptionUserPreferences::AlwaysOn:
157         return alwaysOnKeyword();
158     case CaptionUserPreferences::Manual:
159         return manualKeyword();
160     default:
161         ASSERT_NOT_REACHED();
162         return emptyAtom;
163     }
164 }
165
166 void MediaControlsHost::setSelectedTextTrack(TextTrack* track)
167 {
168     m_mediaElement->setSelectedTextTrack(track);
169 }
170
171 Element* MediaControlsHost::textTrackContainer()
172 {
173     if (!m_textTrackContainer) {
174         m_textTrackContainer = MediaControlTextTrackContainerElement::create(m_mediaElement->document());
175         m_textTrackContainer->setMediaController(m_mediaElement);
176     }
177     return m_textTrackContainer.get();
178 }
179
180 void MediaControlsHost::updateTextTrackContainer()
181 {
182     if (m_textTrackContainer)
183         m_textTrackContainer->updateDisplay();
184 }
185
186 void MediaControlsHost::enteredFullscreen()
187 {
188     if (m_textTrackContainer)
189         m_textTrackContainer->enteredFullscreen();
190 }
191
192 void MediaControlsHost::exitedFullscreen()
193 {
194     if (m_textTrackContainer)
195         m_textTrackContainer->exitedFullscreen();
196 }
197
198 void MediaControlsHost::updateCaptionDisplaySizes()
199 {
200     if (m_textTrackContainer)
201         m_textTrackContainer->updateSizes(true);
202 }
203     
204 bool MediaControlsHost::allowsInlineMediaPlayback() const
205 {
206     return !m_mediaElement->mediaSession().requiresFullscreenForVideoPlayback(*m_mediaElement);
207 }
208
209 bool MediaControlsHost::supportsFullscreen()
210 {
211     return m_mediaElement->supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenModeStandard);
212 }
213
214 bool MediaControlsHost::isVideoLayerInline()
215 {
216     return m_mediaElement->isVideoLayerInline();
217 }
218
219 void MediaControlsHost::setPreparedToReturnVideoLayerToInline(bool value)
220 {
221     m_mediaElement->setPreparedToReturnVideoLayerToInline(value);
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(WIRELESS_PLAYBACK_TARGET)
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 auto MediaControlsHost::externalDeviceType() const -> DeviceType
248 {
249 #if !ENABLE(WIRELESS_PLAYBACK_TARGET)
250     return DeviceType::None;
251 #else
252     MediaPlayer* player = m_mediaElement->player();
253     if (!player) {
254         LOG(Media, "MediaControlsHost::externalDeviceType - returning \"none\" because player is NULL");
255         return DeviceType::None;
256     }
257     
258     switch (player->wirelessPlaybackTargetType()) {
259     case MediaPlayer::TargetTypeNone:
260         return DeviceType::None;
261     case MediaPlayer::TargetTypeAirPlay:
262         return DeviceType::Airplay;
263     case MediaPlayer::TargetTypeTVOut:
264         return DeviceType::Tvout;
265     }
266
267     ASSERT_NOT_REACHED();
268     return DeviceType::None;
269 #endif
270 }
271
272 bool MediaControlsHost::controlsDependOnPageScaleFactor() const
273 {
274     return m_mediaElement->mediaControlsDependOnPageScaleFactor();
275 }
276
277 void MediaControlsHost::setControlsDependOnPageScaleFactor(bool value)
278 {
279     m_mediaElement->setMediaControlsDependOnPageScaleFactor(value);
280 }
281
282 String MediaControlsHost::generateUUID() const
283 {
284     return createCanonicalUUIDString();
285 }
286
287 String MediaControlsHost::shadowRootCSSText() const
288 {
289     Page* page = m_mediaElement->document().page();
290     if (!page)
291         return emptyString();
292     return RenderTheme::themeForPage(page)->mediaControlsStyleSheet();
293 }
294
295 }
296
297 #endif