• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/TextTrackCue.h"
34 
35 #include "bindings/v8/ExceptionMessages.h"
36 #include "bindings/v8/ExceptionStatePlaceholder.h"
37 #include "core/events/Event.h"
38 #include "core/html/track/TextTrack.h"
39 #include "core/html/track/TextTrackCueList.h"
40 
41 namespace WebCore {
42 
43 static const int invalidCueIndex = -1;
44 
isInfiniteOrNonNumber(double value,ExceptionState & exceptionState)45 bool TextTrackCue::isInfiniteOrNonNumber(double value, ExceptionState& exceptionState)
46 {
47     if (!std::isfinite(value)) {
48         exceptionState.throwTypeError(ExceptionMessages::notAFiniteNumber(value));
49         return true;
50     }
51     return false;
52 }
53 
TextTrackCue(double start,double end)54 TextTrackCue::TextTrackCue(double start, double end)
55     : m_startTime(start)
56     , m_endTime(end)
57     , m_cueIndex(invalidCueIndex)
58     , m_track(0)
59     , m_isActive(false)
60     , m_pauseOnExit(false)
61 {
62 }
63 
cueWillChange()64 void TextTrackCue::cueWillChange()
65 {
66     if (m_track)
67         m_track->cueWillChange(this);
68 }
69 
cueDidChange()70 void TextTrackCue::cueDidChange()
71 {
72     if (m_track)
73         m_track->cueDidChange(this);
74 }
75 
track() const76 TextTrack* TextTrackCue::track() const
77 {
78     return m_track;
79 }
80 
setTrack(TextTrack * track)81 void TextTrackCue::setTrack(TextTrack* track)
82 {
83     m_track = track;
84 }
85 
setId(const String & id)86 void TextTrackCue::setId(const String& id)
87 {
88     if (m_id == id)
89         return;
90 
91     cueWillChange();
92     m_id = id;
93     cueDidChange();
94 }
95 
setStartTime(double value,ExceptionState & exceptionState)96 void TextTrackCue::setStartTime(double value, ExceptionState& exceptionState)
97 {
98     // NaN, Infinity and -Infinity values should trigger a TypeError.
99     if (isInfiniteOrNonNumber(value, exceptionState))
100         return;
101 
102     // TODO(93143): Add spec-compliant behavior for negative time values.
103     if (m_startTime == value || value < 0)
104         return;
105 
106     cueWillChange();
107     m_startTime = value;
108     cueDidChange();
109 }
110 
setEndTime(double value,ExceptionState & exceptionState)111 void TextTrackCue::setEndTime(double value, ExceptionState& exceptionState)
112 {
113     // NaN, Infinity and -Infinity values should trigger a TypeError.
114     if (isInfiniteOrNonNumber(value, exceptionState))
115         return;
116 
117     // TODO(93143): Add spec-compliant behavior for negative time values.
118     if (m_endTime == value || value < 0)
119         return;
120 
121     cueWillChange();
122     m_endTime = value;
123     cueDidChange();
124 }
125 
setPauseOnExit(bool value)126 void TextTrackCue::setPauseOnExit(bool value)
127 {
128     if (m_pauseOnExit == value)
129         return;
130 
131     cueWillChange();
132     m_pauseOnExit = value;
133     cueDidChange();
134 }
135 
cueIndex()136 int TextTrackCue::cueIndex()
137 {
138     if (m_cueIndex == invalidCueIndex)
139         m_cueIndex = track()->cues()->getCueIndex(this);
140 
141     return m_cueIndex;
142 }
143 
invalidateCueIndex()144 void TextTrackCue::invalidateCueIndex()
145 {
146     m_cueIndex = invalidCueIndex;
147 }
148 
dispatchEvent(PassRefPtr<Event> event)149 bool TextTrackCue::dispatchEvent(PassRefPtr<Event> event)
150 {
151     // When a TextTrack's mode is disabled: no cues are active, no events fired.
152     if (!track() || track()->mode() == TextTrack::disabledKeyword())
153         return false;
154 
155     return EventTarget::dispatchEvent(event);
156 }
157 
isActive()158 bool TextTrackCue::isActive()
159 {
160     return m_isActive && track() && track()->mode() != TextTrack::disabledKeyword();
161 }
162 
setIsActive(bool active)163 void TextTrackCue::setIsActive(bool active)
164 {
165     m_isActive = active;
166 
167     // Remove the display tree as soon as the cue becomes inactive.
168     if (!active)
169         removeDisplayTree();
170 }
171 
interfaceName() const172 const AtomicString& TextTrackCue::interfaceName() const
173 {
174     return EventTargetNames::TextTrackCue;
175 }
176 
177 } // namespace WebCore
178