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