[WTF] Clean up StringStatics.cpp by using LazyNeverDestroyed<> for Atoms
[WebKit-https.git] / Source / WebCore / html / track / InbandTextTrack.cpp
1 /*
2  * Copyright (C) 2012-2017 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 #include "InbandTextTrack.h"
28
29 #if ENABLE(VIDEO_TRACK)
30
31 #include "HTMLMediaElement.h"
32 #include "InbandDataTextTrack.h"
33 #include "InbandGenericTextTrack.h"
34 #include "InbandTextTrackPrivate.h"
35 #include "InbandWebVTTTextTrack.h"
36
37 namespace WebCore {
38
39 Ref<InbandTextTrack> InbandTextTrack::create(ScriptExecutionContext& context, TextTrackClient& client, InbandTextTrackPrivate& trackPrivate)
40 {
41     switch (trackPrivate.cueFormat()) {
42     case InbandTextTrackPrivate::Data:
43         return InbandDataTextTrack::create(context, client, trackPrivate);
44     case InbandTextTrackPrivate::Generic:
45         return InbandGenericTextTrack::create(context, client, trackPrivate);
46     case InbandTextTrackPrivate::WebVTT:
47         return InbandWebVTTTextTrack::create(context, client, trackPrivate);
48     }
49     ASSERT_NOT_REACHED();
50     return InbandDataTextTrack::create(context, client, trackPrivate);
51 }
52
53 InbandTextTrack::InbandTextTrack(ScriptExecutionContext& context, TextTrackClient& client, InbandTextTrackPrivate& trackPrivate)
54     : TextTrack(&context, &client, emptyAtom(), trackPrivate.id(), trackPrivate.label(), trackPrivate.language(), InBand)
55     , m_private(trackPrivate)
56 {
57     m_private->setClient(this);
58     updateKindFromPrivate();
59 }
60
61 InbandTextTrack::~InbandTextTrack()
62 {
63     m_private->setClient(nullptr);
64 }
65
66 void InbandTextTrack::setPrivate(InbandTextTrackPrivate& trackPrivate)
67 {
68     if (m_private.ptr() == &trackPrivate)
69         return;
70
71     m_private->setClient(nullptr);
72     m_private = trackPrivate;
73     m_private->setClient(this);
74
75     setModeInternal(mode());
76     updateKindFromPrivate();
77 }
78
79 void InbandTextTrack::setMode(Mode mode)
80 {
81     TextTrack::setMode(mode);
82     setModeInternal(mode);
83 }
84
85 static inline InbandTextTrackPrivate::Mode toPrivate(TextTrack::Mode mode)
86 {
87     switch (mode) {
88     case TextTrack::Mode::Disabled:
89         return InbandTextTrackPrivate::Disabled;
90     case TextTrack::Mode::Hidden:
91         return InbandTextTrackPrivate::Hidden;
92     case TextTrack::Mode::Showing:
93         return InbandTextTrackPrivate::Showing;
94     }
95     ASSERT_NOT_REACHED();
96     return InbandTextTrackPrivate::Disabled;
97 }
98
99 void InbandTextTrack::setModeInternal(Mode mode)
100 {
101     m_private->setMode(toPrivate(mode));
102 }
103
104 bool InbandTextTrack::isClosedCaptions() const
105 {
106     return m_private->isClosedCaptions();
107 }
108
109 bool InbandTextTrack::isSDH() const
110 {
111     return m_private->isSDH();
112 }
113
114 bool InbandTextTrack::containsOnlyForcedSubtitles() const
115 {
116     return m_private->containsOnlyForcedSubtitles();
117 }
118
119 bool InbandTextTrack::isMainProgramContent() const
120 {
121     return m_private->isMainProgramContent();
122 }
123
124 bool InbandTextTrack::isEasyToRead() const
125 {
126     return m_private->isEasyToRead();
127 }
128     
129 size_t InbandTextTrack::inbandTrackIndex()
130 {
131     return m_private->trackIndex();
132 }
133
134 AtomicString InbandTextTrack::inBandMetadataTrackDispatchType() const
135 {
136     return m_private->inBandMetadataTrackDispatchType();
137 }
138
139 void InbandTextTrack::idChanged(const AtomicString& id)
140 {
141     setId(id);
142 }
143
144 void InbandTextTrack::labelChanged(const AtomicString& label)
145 {
146     setLabel(label);
147 }
148
149 void InbandTextTrack::languageChanged(const AtomicString& language)
150 {
151     setLanguage(language);
152 }
153
154 void InbandTextTrack::willRemove()
155 {
156     auto* element = mediaElement();
157     if (!element)
158         return;
159     element->removeTextTrack(*this);
160 }
161
162 void InbandTextTrack::updateKindFromPrivate()
163 {
164     switch (m_private->kind()) {
165     case InbandTextTrackPrivate::Subtitles:
166         setKind(Kind::Subtitles);
167         return;
168     case InbandTextTrackPrivate::Captions:
169         setKind(Kind::Captions);
170         return;
171     case InbandTextTrackPrivate::Descriptions:
172         setKind(Kind::Descriptions);
173         return;
174     case InbandTextTrackPrivate::Chapters:
175         setKind(Kind::Chapters);
176         return;
177     case InbandTextTrackPrivate::Metadata:
178         setKind(Kind::Metadata);
179         return;
180     case InbandTextTrackPrivate::Forced:
181         setKind(Kind::Forced);
182         return;
183     case InbandTextTrackPrivate::None:
184         break;
185     }
186     ASSERT_NOT_REACHED();
187 }
188
189 MediaTime InbandTextTrack::startTimeVariance() const
190 {
191     return m_private->startTimeVariance();
192 }
193
194 } // namespace WebCore
195
196 #endif