Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / track / TextTrack.cpp
1 /*
2  * Copyright (C) 2011 Google Inc.  All rights reserved.
3  * Copyright (C) 2011, 2012, 2013 Apple Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "core/html/track/TextTrack.h"
34
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/html/HTMLMediaElement.h"
39 #include "core/html/track/TextTrackCueList.h"
40 #include "core/html/track/TextTrackList.h"
41 #include "core/html/track/vtt/VTTRegion.h"
42 #include "core/html/track/vtt/VTTRegionList.h"
43 #include "platform/RuntimeEnabledFeatures.h"
44
45 namespace blink {
46
47 static const int invalidTrackIndex = -1;
48
49 const AtomicString& TextTrack::subtitlesKeyword()
50 {
51     DEFINE_STATIC_LOCAL(const AtomicString, subtitles, ("subtitles", AtomicString::ConstructFromLiteral));
52     return subtitles;
53 }
54
55 const AtomicString& TextTrack::captionsKeyword()
56 {
57     DEFINE_STATIC_LOCAL(const AtomicString, captions, ("captions", AtomicString::ConstructFromLiteral));
58     return captions;
59 }
60
61 const AtomicString& TextTrack::descriptionsKeyword()
62 {
63     DEFINE_STATIC_LOCAL(const AtomicString, descriptions, ("descriptions", AtomicString::ConstructFromLiteral));
64     return descriptions;
65 }
66
67 const AtomicString& TextTrack::chaptersKeyword()
68 {
69     DEFINE_STATIC_LOCAL(const AtomicString, chapters, ("chapters", AtomicString::ConstructFromLiteral));
70     return chapters;
71 }
72
73 const AtomicString& TextTrack::metadataKeyword()
74 {
75     DEFINE_STATIC_LOCAL(const AtomicString, metadata, ("metadata", AtomicString::ConstructFromLiteral));
76     return metadata;
77 }
78
79 const AtomicString& TextTrack::disabledKeyword()
80 {
81     DEFINE_STATIC_LOCAL(const AtomicString, open, ("disabled", AtomicString::ConstructFromLiteral));
82     return open;
83 }
84
85 const AtomicString& TextTrack::hiddenKeyword()
86 {
87     DEFINE_STATIC_LOCAL(const AtomicString, closed, ("hidden", AtomicString::ConstructFromLiteral));
88     return closed;
89 }
90
91 const AtomicString& TextTrack::showingKeyword()
92 {
93     DEFINE_STATIC_LOCAL(const AtomicString, ended, ("showing", AtomicString::ConstructFromLiteral));
94     return ended;
95 }
96
97 TextTrack::TextTrack(const AtomicString& kind, const AtomicString& label, const AtomicString& language, const AtomicString& id, TextTrackType type)
98     : TrackBase(TrackBase::TextTrack, label, language, id)
99     , m_cues(nullptr)
100     , m_regions(nullptr)
101     , m_trackList(nullptr)
102     , m_mode(disabledKeyword())
103     , m_trackType(type)
104     , m_readinessState(NotLoaded)
105     , m_trackIndex(invalidTrackIndex)
106     , m_renderedTrackIndex(invalidTrackIndex)
107     , m_hasBeenConfigured(false)
108 {
109     ScriptWrappable::init(this);
110     setKind(kind);
111 }
112
113 TextTrack::~TextTrack()
114 {
115 #if !ENABLE(OILPAN)
116     ASSERT(!m_trackList);
117
118     if (m_cues) {
119         for (size_t i = 0; i < m_cues->length(); ++i)
120             m_cues->item(i)->setTrack(0);
121     }
122     if (m_regions) {
123         for (size_t i = 0; i < m_regions->length(); ++i)
124             m_regions->item(i)->setTrack(0);
125     }
126 #endif
127 }
128
129 bool TextTrack::isValidKindKeyword(const AtomicString& value)
130 {
131     if (value == subtitlesKeyword())
132         return true;
133     if (value == captionsKeyword())
134         return true;
135     if (value == descriptionsKeyword())
136         return true;
137     if (value == chaptersKeyword())
138         return true;
139     if (value == metadataKeyword())
140         return true;
141
142     return false;
143 }
144
145 void TextTrack::setTrackList(TextTrackList* trackList)
146 {
147     if (!trackList && mediaElement() && m_cues)
148         mediaElement()->textTrackRemoveCues(this, m_cues.get());
149
150     m_trackList = trackList;
151     invalidateTrackIndex();
152 }
153
154 void TextTrack::setKind(const AtomicString& newKind)
155 {
156     AtomicString oldKind = kind();
157     TrackBase::setKind(newKind);
158
159     if (mediaElement() && oldKind != kind())
160         mediaElement()->textTrackKindChanged(this);
161 }
162
163 void TextTrack::setMode(const AtomicString& mode)
164 {
165     ASSERT(mode == disabledKeyword() || mode == hiddenKeyword() || mode == showingKeyword());
166
167     // On setting, if the new value isn't equal to what the attribute would currently
168     // return, the new value must be processed as follows ...
169     if (m_mode == mode)
170         return;
171
172     // If mode changes to disabled, remove this track's cues from the client
173     // because they will no longer be accessible from the cues() function.
174     if (mode == disabledKeyword() && mediaElement() && m_cues)
175         mediaElement()->textTrackRemoveCues(this, m_cues.get());
176
177     if (mode != showingKeyword() && m_cues)
178         for (size_t i = 0; i < m_cues->length(); ++i)
179             m_cues->item(i)->removeDisplayTree();
180
181     m_mode = mode;
182
183     if (mediaElement())
184         mediaElement()->textTrackModeChanged(this);
185 }
186
187 TextTrackCueList* TextTrack::cues()
188 {
189     // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
190     // then the cues attribute must return a live TextTrackCueList object ...
191     // Otherwise, it must return null. When an object is returned, the
192     // same object must be returned each time.
193     // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-cues
194     if (m_mode != disabledKeyword())
195         return ensureTextTrackCueList();
196     return 0;
197 }
198
199 void TextTrack::removeAllCues()
200 {
201     if (!m_cues)
202         return;
203
204     if (mediaElement())
205         mediaElement()->textTrackRemoveCues(this, m_cues.get());
206
207     for (size_t i = 0; i < m_cues->length(); ++i)
208         m_cues->item(i)->setTrack(0);
209
210     m_cues = nullptr;
211 }
212
213 TextTrackCueList* TextTrack::activeCues() const
214 {
215     // 4.8.10.12.5 If the text track mode ... is not the text track disabled mode,
216     // then the activeCues attribute must return a live TextTrackCueList object ...
217     // ... whose active flag was set when the script started, in text track cue
218     // order. Otherwise, it must return null. When an object is returned, the
219     // same object must be returned each time.
220     // http://www.whatwg.org/specs/web-apps/current-work/#dom-texttrack-activecues
221     if (m_cues && m_mode != disabledKeyword())
222         return m_cues->activeCues();
223     return 0;
224 }
225
226 void TextTrack::addCue(PassRefPtrWillBeRawPtr<TextTrackCue> prpCue)
227 {
228     if (!prpCue)
229         return;
230
231     RefPtrWillBeRawPtr<TextTrackCue> cue = prpCue;
232
233     // TODO(93143): Add spec-compliant behavior for negative time values.
234     if (std::isnan(cue->startTime()) || std::isnan(cue->endTime()) || cue->startTime() < 0 || cue->endTime() < 0)
235         return;
236
237     // 4.8.10.12.5 Text track API
238
239     // The addCue(cue) method of TextTrack objects, when invoked, must run the following steps:
240
241     // 1. If the given cue is in a text track list of cues, then remove cue from that text track
242     // list of cues.
243     TextTrack* cueTrack = cue->track();
244     if (cueTrack && cueTrack != this)
245         cueTrack->removeCue(cue.get(), ASSERT_NO_EXCEPTION);
246
247     // 2. Add cue to the method's TextTrack object's text track's text track list of cues.
248     cue->setTrack(this);
249     ensureTextTrackCueList()->add(cue);
250
251     if (mediaElement())
252         mediaElement()->textTrackAddCue(this, cue.get());
253 }
254
255 void TextTrack::removeCue(TextTrackCue* cue, ExceptionState& exceptionState)
256 {
257     if (!cue)
258         return;
259
260     // 4.8.10.12.5 Text track API
261
262     // The removeCue(cue) method of TextTrack objects, when invoked, must run the following steps:
263
264     // 1. If the given cue is not currently listed in the method's TextTrack
265     // object's text track's text track list of cues, then throw a NotFoundError exception.
266     if (cue->track() != this) {
267         exceptionState.throwDOMException(NotFoundError, "The specified cue is not listed in the TextTrack's list of cues.");
268         return;
269     }
270
271     // 2. Remove cue from the method's TextTrack object's text track's text track list of cues.
272     if (!m_cues || !m_cues->remove(cue)) {
273         exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified cue.");
274         return;
275     }
276
277     cue->setTrack(0);
278     if (mediaElement())
279         mediaElement()->textTrackRemoveCue(this, cue);
280 }
281
282 VTTRegionList* TextTrack::ensureVTTRegionList()
283 {
284     if (!m_regions)
285         m_regions = VTTRegionList::create();
286
287     return m_regions.get();
288 }
289
290 VTTRegionList* TextTrack::regions()
291 {
292     // If the text track mode of the text track that the TextTrack object
293     // represents is not the text track disabled mode, then the regions
294     // attribute must return a live VTTRegionList object that represents
295     // the text track list of regions of the text track. Otherwise, it must
296     // return null. When an object is returned, the same object must be returned
297     // each time.
298     if (RuntimeEnabledFeatures::webVTTRegionsEnabled() && m_mode != disabledKeyword())
299         return ensureVTTRegionList();
300     return 0;
301 }
302
303 void TextTrack::addRegion(PassRefPtrWillBeRawPtr<VTTRegion> prpRegion)
304 {
305     if (!prpRegion)
306         return;
307
308     RefPtrWillBeRawPtr<VTTRegion> region = prpRegion;
309     VTTRegionList* regionList = ensureVTTRegionList();
310
311     // 1. If the given region is in a text track list of regions, then remove
312     // region from that text track list of regions.
313     TextTrack* regionTrack = region->track();
314     if (regionTrack && regionTrack != this)
315         regionTrack->removeRegion(region.get(), ASSERT_NO_EXCEPTION);
316
317     // 2. If the method's TextTrack object's text track list of regions contains
318     // a region with the same identifier as region replace the values of that
319     // region's width, height, anchor point, viewport anchor point and scroll
320     // attributes with those of region.
321     VTTRegion* existingRegion = regionList->getRegionById(region->id());
322     if (existingRegion) {
323         existingRegion->updateParametersFromRegion(region.get());
324         return;
325     }
326
327     // Otherwise: add region to the method's TextTrack object's text track
328     // list of regions.
329     region->setTrack(this);
330     regionList->add(region);
331 }
332
333 void TextTrack::removeRegion(VTTRegion* region, ExceptionState &exceptionState)
334 {
335     if (!region)
336         return;
337
338     // 1. If the given region is not currently listed in the method's TextTrack
339     // object's text track list of regions, then throw a NotFoundError exception.
340     if (region->track() != this) {
341         exceptionState.throwDOMException(NotFoundError, "The specified region is not listed in the TextTrack's list of regions.");
342         return;
343     }
344
345     if (!m_regions || !m_regions->remove(region)) {
346         exceptionState.throwDOMException(InvalidStateError, "Failed to remove the specified region.");
347         return;
348     }
349
350     region->setTrack(0);
351 }
352
353 void TextTrack::cueWillChange(TextTrackCue* cue)
354 {
355     if (!mediaElement())
356         return;
357
358     // The cue may need to be repositioned in the media element's interval tree, may need to
359     // be re-rendered, etc, so remove it before the modification...
360     mediaElement()->textTrackRemoveCue(this, cue);
361 }
362
363 void TextTrack::cueDidChange(TextTrackCue* cue)
364 {
365     if (!mediaElement())
366         return;
367
368     // Make sure the TextTrackCueList order is up-to-date.
369     ensureTextTrackCueList()->updateCueIndex(cue);
370
371     // ... and add it back again.
372     mediaElement()->textTrackAddCue(this, cue);
373 }
374
375 int TextTrack::trackIndex()
376 {
377     ASSERT(m_trackList);
378
379     if (m_trackIndex == invalidTrackIndex)
380         m_trackIndex = m_trackList->getTrackIndex(this);
381
382     return m_trackIndex;
383 }
384
385 void TextTrack::invalidateTrackIndex()
386 {
387     m_trackIndex = invalidTrackIndex;
388     m_renderedTrackIndex = invalidTrackIndex;
389 }
390
391 bool TextTrack::isRendered()
392 {
393     if (kind() != captionsKeyword() && kind() != subtitlesKeyword())
394         return false;
395
396     if (m_mode != showingKeyword())
397         return false;
398
399     return true;
400 }
401
402 TextTrackCueList* TextTrack::ensureTextTrackCueList()
403 {
404     if (!m_cues)
405         m_cues = TextTrackCueList::create();
406
407     return m_cues.get();
408 }
409
410 int TextTrack::trackIndexRelativeToRenderedTracks()
411 {
412     ASSERT(m_trackList);
413
414     if (m_renderedTrackIndex == invalidTrackIndex)
415         m_renderedTrackIndex = m_trackList->getTrackIndexRelativeToRenderedTracks(this);
416
417     return m_renderedTrackIndex;
418 }
419
420 const AtomicString& TextTrack::interfaceName() const
421 {
422     return EventTargetNames::TextTrack;
423 }
424
425 ExecutionContext* TextTrack::executionContext() const
426 {
427     HTMLMediaElement* owner = mediaElement();
428     return owner ? owner->executionContext() : 0;
429 }
430
431 HTMLMediaElement* TextTrack::mediaElement() const
432 {
433     return m_trackList ? m_trackList->owner() : 0;
434 }
435
436 Node* TextTrack::owner() const
437 {
438     return mediaElement();
439 }
440
441 void TextTrack::trace(Visitor* visitor)
442 {
443     visitor->trace(m_cues);
444     visitor->trace(m_regions);
445     visitor->trace(m_trackList);
446     TrackBase::trace(visitor);
447     EventTargetWithInlineData::trace(visitor);
448 }
449
450 } // namespace blink