Use NeverDestroyed instead of DEPRECATED_DEFINE_STATIC_LOCAL
[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 "UUID.h"
42 #include <runtime/JSCJSValueInlines.h>
43
44 namespace WebCore {
45
46 const AtomicString& MediaControlsHost::automaticKeyword()
47 {
48     static NeverDestroyed<const AtomicString> automatic("automatic", AtomicString::ConstructFromLiteral);
49     return automatic;
50 }
51
52 const AtomicString& MediaControlsHost::forcedOnlyKeyword()
53 {
54     static NeverDestroyed<const AtomicString> forcedOn("forced-only", AtomicString::ConstructFromLiteral);
55     return forcedOn;
56 }
57
58 const AtomicString& MediaControlsHost::alwaysOnKeyword()
59 {
60     static NeverDestroyed<const AtomicString> alwaysOn("always-on", AtomicString::ConstructFromLiteral);
61     return alwaysOn;
62 }
63
64 Ref<MediaControlsHost> MediaControlsHost::create(HTMLMediaElement* mediaElement)
65 {
66     return adoptRef(*new MediaControlsHost(mediaElement));
67 }
68
69 MediaControlsHost::MediaControlsHost(HTMLMediaElement* mediaElement)
70     : m_mediaElement(mediaElement)
71 {
72     ASSERT(mediaElement);
73 }
74
75 MediaControlsHost::~MediaControlsHost()
76 {
77 }
78
79 Vector<RefPtr<TextTrack>> MediaControlsHost::sortedTrackListForMenu(TextTrackList* trackList)
80 {
81     if (!trackList)
82         return Vector<RefPtr<TextTrack>>();
83
84     Page* page = m_mediaElement->document().page();
85     if (!page)
86         return Vector<RefPtr<TextTrack>>();
87
88     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
89     return captionPreferences->sortedTrackListForMenu(trackList);
90 }
91
92 Vector<RefPtr<AudioTrack>> MediaControlsHost::sortedTrackListForMenu(AudioTrackList* trackList)
93 {
94     if (!trackList)
95         return Vector<RefPtr<AudioTrack>>();
96
97     Page* page = m_mediaElement->document().page();
98     if (!page)
99         return Vector<RefPtr<AudioTrack>>();
100
101     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
102     return captionPreferences->sortedTrackListForMenu(trackList);
103 }
104
105 String MediaControlsHost::displayNameForTrack(TextTrack* track)
106 {
107     if (!track)
108         return emptyString();
109
110     Page* page = m_mediaElement->document().page();
111     if (!page)
112         return emptyString();
113
114     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
115     return captionPreferences->displayNameForTrack(track);
116 }
117
118 String MediaControlsHost::displayNameForTrack(AudioTrack* track)
119 {
120     if (!track)
121         return emptyString();
122
123     Page* page = m_mediaElement->document().page();
124     if (!page)
125         return emptyString();
126
127     CaptionUserPreferences* captionPreferences = page->group().captionPreferences();
128     return captionPreferences->displayNameForTrack(track);
129 }
130
131 TextTrack* MediaControlsHost::captionMenuOffItem()
132 {
133     return TextTrack::captionMenuOffItem();
134 }
135
136 TextTrack* MediaControlsHost::captionMenuAutomaticItem()
137 {
138     return TextTrack::captionMenuAutomaticItem();
139 }
140
141 AtomicString MediaControlsHost::captionDisplayMode()
142 {
143     Page* page = m_mediaElement->document().page();
144     if (!page)
145         return emptyAtom;
146
147     switch (page->group().captionPreferences()->captionDisplayMode()) {
148     case CaptionUserPreferences::Automatic:
149         return automaticKeyword();
150     case CaptionUserPreferences::ForcedOnly:
151         return forcedOnlyKeyword();
152     case CaptionUserPreferences::AlwaysOn:
153         return alwaysOnKeyword();
154     default:
155         ASSERT_NOT_REACHED();
156         return emptyAtom;
157     }
158 }
159
160 void MediaControlsHost::setSelectedTextTrack(TextTrack* track)
161 {
162     m_mediaElement->setSelectedTextTrack(track);
163 }
164
165 Element* MediaControlsHost::textTrackContainer()
166 {
167     if (!m_textTrackContainer) {
168         m_textTrackContainer = MediaControlTextTrackContainerElement::create(m_mediaElement->document());
169         m_textTrackContainer->setMediaController(m_mediaElement);
170     }
171     return m_textTrackContainer.get();
172 }
173
174 void MediaControlsHost::updateTextTrackContainer()
175 {
176     if (m_textTrackContainer)
177         m_textTrackContainer->updateDisplay();
178 }
179
180 void MediaControlsHost::enteredFullscreen()
181 {
182     if (m_textTrackContainer)
183         m_textTrackContainer->enteredFullscreen();
184 }
185
186 void MediaControlsHost::exitedFullscreen()
187 {
188     if (m_textTrackContainer)
189         m_textTrackContainer->exitedFullscreen();
190 }
191
192 void MediaControlsHost::updateCaptionDisplaySizes()
193 {
194     if (m_textTrackContainer)
195         m_textTrackContainer->updateSizes(true);
196 }
197     
198 bool MediaControlsHost::allowsInlineMediaPlayback() const
199 {
200     return !m_mediaElement->mediaSession().requiresFullscreenForVideoPlayback(*m_mediaElement);
201 }
202
203 bool MediaControlsHost::supportsFullscreen()
204 {
205     return m_mediaElement->supportsFullscreen();
206 }
207
208 bool MediaControlsHost::userGestureRequired() const
209 {
210     return !m_mediaElement->mediaSession().playbackPermitted(*m_mediaElement);
211 }
212
213 String MediaControlsHost::externalDeviceDisplayName() const
214 {
215 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
216     MediaPlayer* player = m_mediaElement->player();
217     if (!player) {
218         LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"\" because player is NULL");
219         return emptyString();
220     }
221     
222     String name = player->wirelessPlaybackTargetName();
223     LOG(Media, "MediaControlsHost::externalDeviceDisplayName - returning \"%s\"", name.utf8().data());
224     
225     return name;
226 #else
227     return emptyString();
228 #endif
229 }
230
231 String MediaControlsHost::externalDeviceType() const
232 {
233     static NeverDestroyed<String> none(ASCIILiteral("none"));
234     String type = none;
235     
236 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
237     static NeverDestroyed<String> airplay(ASCIILiteral("airplay"));
238     static NeverDestroyed<String> tvout(ASCIILiteral("tvout"));
239     
240     MediaPlayer* player = m_mediaElement->player();
241     if (!player) {
242         LOG(Media, "MediaControlsHost::externalDeviceType - returning \"none\" because player is NULL");
243         return none;
244     }
245     
246     switch (player->wirelessPlaybackTargetType()) {
247     case MediaPlayer::TargetTypeNone:
248         type = none;
249         break;
250     case MediaPlayer::TargetTypeAirPlay:
251         type = airplay;
252         break;
253     case MediaPlayer::TargetTypeTVOut:
254         type = tvout;
255         break;
256     }
257 #endif
258     
259     LOG(Media, "MediaControlsHost::externalDeviceType - returning \"%s\"", type.utf8().data());
260     
261     return type;
262 }
263
264 bool MediaControlsHost::controlsDependOnPageScaleFactor() const
265 {
266     return m_mediaElement->mediaControlsDependOnPageScaleFactor();
267 }
268
269 void MediaControlsHost::setControlsDependOnPageScaleFactor(bool value)
270 {
271     m_mediaElement->setMediaControlsDependOnPageScaleFactor(value);
272 }
273
274 String MediaControlsHost::generateUUID() const
275 {
276     return createCanonicalUUIDString();
277 }
278
279 }
280
281 #endif