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