1/*
2 * Copyright (C) 2011 Google 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 COMPUTER, 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 COMPUTER, 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#if ENABLE(VIDEO_TRACK)
28#include "HTMLTrackElement.h"
29
30#include "ContentSecurityPolicy.h"
31#include "Event.h"
32#include "HTMLMediaElement.h"
33#include "HTMLNames.h"
34#include "Logging.h"
35#include "RuntimeEnabledFeatures.h"
36#include "ScriptCallStack.h"
37#include "ScriptEventListener.h"
38
39using namespace std;
40
41namespace WebCore {
42
43using namespace HTMLNames;
44
45#if !LOG_DISABLED
46static String urlForLoggingTrack(const KURL& url)
47{
48    static const unsigned maximumURLLengthForLogging = 128;
49
50    if (url.string().length() < maximumURLLengthForLogging)
51        return url.string();
52    return url.string().substring(0, maximumURLLengthForLogging) + "...";
53}
54#endif
55
56inline HTMLTrackElement::HTMLTrackElement(const QualifiedName& tagName, Document* document)
57    : HTMLElement(tagName, document)
58    , m_loadTimer(this, &HTMLTrackElement::loadTimerFired)
59{
60    LOG(Media, "HTMLTrackElement::HTMLTrackElement - %p", this);
61    ASSERT(hasTagName(trackTag));
62}
63
64HTMLTrackElement::~HTMLTrackElement()
65{
66    if (m_track)
67        m_track->clearClient();
68}
69
70PassRefPtr<HTMLTrackElement> HTMLTrackElement::create(const QualifiedName& tagName, Document* document)
71{
72    return adoptRef(new HTMLTrackElement(tagName, document));
73}
74
75Node::InsertionNotificationRequest HTMLTrackElement::insertedInto(ContainerNode* insertionPoint)
76{
77    // Since we've moved to a new parent, we may now be able to load.
78    scheduleLoad();
79
80    HTMLElement::insertedInto(insertionPoint);
81    HTMLMediaElement* parent = mediaElement();
82    if (insertionPoint == parent) {
83        ensureTrack();
84        parent->didAddTextTrack(this);
85    }
86    return InsertionDone;
87}
88
89void HTMLTrackElement::removedFrom(ContainerNode* insertionPoint)
90{
91    if (!parentNode() && WebCore::isMediaElement(insertionPoint))
92        toMediaElement(insertionPoint)->didRemoveTextTrack(this);
93    HTMLElement::removedFrom(insertionPoint);
94}
95
96void HTMLTrackElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
97{
98    if (RuntimeEnabledFeatures::webkitVideoTrackEnabled()) {
99        if (name == srcAttr) {
100            if (!value.isEmpty())
101                scheduleLoad();
102            else if (m_track)
103                m_track->removeAllCues();
104
105        // 4.8.10.12.3 Sourcing out-of-band text tracks
106        // As the kind, label, and srclang attributes are set, changed, or removed, the text track must update accordingly...
107        } else if (name == kindAttr)
108            track()->setKind(value.lower());
109        else if (name == labelAttr)
110            track()->setLabel(value);
111        else if (name == srclangAttr)
112            track()->setLanguage(value);
113        else if (name == defaultAttr)
114            track()->setIsDefault(!value.isNull());
115    }
116
117    HTMLElement::parseAttribute(name, value);
118}
119
120KURL HTMLTrackElement::src() const
121{
122    return document()->completeURL(getAttribute(srcAttr));
123}
124
125void HTMLTrackElement::setSrc(const String& url)
126{
127    setAttribute(srcAttr, url);
128}
129
130String HTMLTrackElement::kind()
131{
132    return track()->kind();
133}
134
135void HTMLTrackElement::setKind(const String& kind)
136{
137    setAttribute(kindAttr, kind);
138}
139
140String HTMLTrackElement::srclang() const
141{
142    return getAttribute(srclangAttr);
143}
144
145void HTMLTrackElement::setSrclang(const String& srclang)
146{
147    setAttribute(srclangAttr, srclang);
148}
149
150String HTMLTrackElement::label() const
151{
152    return getAttribute(labelAttr);
153}
154
155void HTMLTrackElement::setLabel(const String& label)
156{
157    setAttribute(labelAttr, label);
158}
159
160bool HTMLTrackElement::isDefault() const
161{
162    return fastHasAttribute(defaultAttr);
163}
164
165void HTMLTrackElement::setIsDefault(bool isDefault)
166{
167    setBooleanAttribute(defaultAttr, isDefault);
168}
169
170LoadableTextTrack* HTMLTrackElement::ensureTrack()
171{
172    if (!m_track) {
173        // The kind attribute is an enumerated attribute, limited only to know values. It defaults to 'subtitles' if missing or invalid.
174        String kind = getAttribute(kindAttr).lower();
175        if (!TextTrack::isValidKindKeyword(kind))
176            kind = TextTrack::subtitlesKeyword();
177        m_track = LoadableTextTrack::create(this, kind, label(), srclang());
178    } else
179        m_track->setTrackElement(this);
180
181    return m_track.get();
182}
183
184TextTrack* HTMLTrackElement::track()
185{
186    return ensureTrack();
187}
188
189bool HTMLTrackElement::isURLAttribute(const Attribute& attribute) const
190{
191    return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
192}
193
194void HTMLTrackElement::scheduleLoad()
195{
196    // 1. If another occurrence of this algorithm is already running for this text track and its track element,
197    // abort these steps, letting that other algorithm take care of this element.
198    if (m_loadTimer.isActive())
199        return;
200
201    if (!RuntimeEnabledFeatures::webkitVideoTrackEnabled())
202        return;
203
204    // 2. If the text track's text track mode is not set to one of hidden or showing, abort these steps.
205    if (ensureTrack()->mode() != TextTrack::hiddenKeyword() && ensureTrack()->mode() != TextTrack::showingKeyword())
206        return;
207
208    // 3. If the text track's track element does not have a media element as a parent, abort these steps.
209    if (!mediaElement())
210        return;
211
212    // 4. Run the remainder of these steps asynchronously, allowing whatever caused these steps to run to continue.
213    m_loadTimer.startOneShot(0);
214}
215
216void HTMLTrackElement::loadTimerFired(Timer<HTMLTrackElement>*)
217{
218    if (!fastHasAttribute(srcAttr))
219        return;
220
221    // 6. Set the text track readiness state to loading.
222    setReadyState(HTMLTrackElement::LOADING);
223
224    // 7. Let URL be the track URL of the track element.
225    KURL url = getNonEmptyURLAttribute(srcAttr);
226
227    // 8. If the track element's parent is a media element then let CORS mode be the state of the parent media
228    // element's crossorigin content attribute. Otherwise, let CORS mode be No CORS.
229    if (!canLoadUrl(url)) {
230        didCompleteLoad(ensureTrack(), HTMLTrackElement::Failure);
231        return;
232    }
233
234    ensureTrack()->scheduleLoad(url);
235}
236
237bool HTMLTrackElement::canLoadUrl(const KURL& url)
238{
239    if (!RuntimeEnabledFeatures::webkitVideoTrackEnabled())
240        return false;
241
242    HTMLMediaElement* parent = mediaElement();
243    if (!parent)
244        return false;
245
246    // 4.8.10.12.3 Sourcing out-of-band text tracks
247
248    // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the
249    // mode being the state of the media element's crossorigin content attribute, the origin being the
250    // origin of the media element's Document, and the default origin behaviour set to fail.
251    if (url.isEmpty())
252        return false;
253
254    if (!document()->contentSecurityPolicy()->allowMediaFromSource(url)) {
255        LOG(Media, "HTMLTrackElement::canLoadUrl(%s) -> rejected by Content Security Policy", urlForLoggingTrack(url).utf8().data());
256        return false;
257    }
258
259    return dispatchBeforeLoadEvent(url.string());
260}
261
262void HTMLTrackElement::didCompleteLoad(LoadableTextTrack*, LoadStatus status)
263{
264    // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
265
266    // 4. Download: ...
267    // If the fetching algorithm fails for any reason (network error, the server returns an error
268    // code, a cross-origin check fails, etc), or if URL is the empty string or has the wrong origin
269    // as determined by the condition at the start of this step, or if the fetched resource is not in
270    // a supported format, then queue a task to first change the text track readiness state to failed
271    // to load and then fire a simple event named error at the track element; and then, once that task
272    // is queued, move on to the step below labeled monitoring.
273
274    if (status == Failure) {
275        setReadyState(HTMLTrackElement::TRACK_ERROR);
276        dispatchEvent(Event::create(eventNames().errorEvent, false, false), IGNORE_EXCEPTION);
277        return;
278    }
279
280    // If the fetching algorithm does not fail, then the final task that is queued by the networking
281    // task source must run the following steps:
282    //     1. Change the text track readiness state to loaded.
283    setReadyState(HTMLTrackElement::LOADED);
284
285    //     2. If the file was successfully processed, fire a simple event named load at the
286    //        track element.
287    dispatchEvent(Event::create(eventNames().loadEvent, false, false), IGNORE_EXCEPTION);
288}
289
290// NOTE: The values in the TextTrack::ReadinessState enum must stay in sync with those in HTMLTrackElement::ReadyState.
291COMPILE_ASSERT(HTMLTrackElement::NONE == static_cast<HTMLTrackElement::ReadyState>(TextTrack::NotLoaded), TextTrackEnumNotLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumNONE);
292COMPILE_ASSERT(HTMLTrackElement::LOADING == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loading), TextTrackEnumLoadingIsWrong_ShouldBe_HTMLTrackElementEnumLOADING);
293COMPILE_ASSERT(HTMLTrackElement::LOADED == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loaded), TextTrackEnumLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumLOADED);
294COMPILE_ASSERT(HTMLTrackElement::TRACK_ERROR == static_cast<HTMLTrackElement::ReadyState>(TextTrack::FailedToLoad), TextTrackEnumFailedToLoad_Is_Wrong_Should_Be_HTMLTrackElementEnumTRACK_ERROR);
295
296void HTMLTrackElement::setReadyState(ReadyState state)
297{
298    ensureTrack()->setReadinessState(static_cast<TextTrack::ReadinessState>(state));
299    if (HTMLMediaElement* parent = mediaElement())
300        return parent->textTrackReadyStateChanged(m_track.get());
301}
302
303HTMLTrackElement::ReadyState HTMLTrackElement::readyState()
304{
305    return static_cast<ReadyState>(ensureTrack()->readinessState());
306}
307
308const AtomicString& HTMLTrackElement::mediaElementCrossOriginAttribute() const
309{
310    if (HTMLMediaElement* parent = mediaElement())
311        return parent->fastGetAttribute(HTMLNames::crossoriginAttr);
312
313    return nullAtom;
314}
315
316void HTMLTrackElement::textTrackKindChanged(TextTrack* track)
317{
318    if (HTMLMediaElement* parent = mediaElement())
319        return parent->textTrackKindChanged(track);
320}
321
322void HTMLTrackElement::textTrackModeChanged(TextTrack* track)
323{
324    // Since we've moved to a new parent, we may now be able to load.
325    if (readyState() == HTMLTrackElement::NONE)
326        scheduleLoad();
327
328    if (HTMLMediaElement* parent = mediaElement())
329        return parent->textTrackModeChanged(track);
330}
331
332void HTMLTrackElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
333{
334    if (HTMLMediaElement* parent = mediaElement())
335        return parent->textTrackAddCues(track, cues);
336}
337
338void HTMLTrackElement::textTrackRemoveCues(TextTrack* track, const TextTrackCueList* cues)
339{
340    if (HTMLMediaElement* parent = mediaElement())
341        return parent->textTrackRemoveCues(track, cues);
342}
343
344void HTMLTrackElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
345{
346    if (HTMLMediaElement* parent = mediaElement())
347        return parent->textTrackAddCue(track, cue);
348}
349
350void HTMLTrackElement::textTrackRemoveCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
351{
352    if (HTMLMediaElement* parent = mediaElement())
353        return parent->textTrackRemoveCue(track, cue);
354}
355
356HTMLMediaElement* HTMLTrackElement::mediaElement() const
357{
358    Element* parent = parentElement();
359    if (parent && parent->isMediaElement())
360        return static_cast<HTMLMediaElement*>(parentNode());
361
362    return 0;
363}
364
365#if ENABLE(MICRODATA)
366String HTMLTrackElement::itemValueText() const
367{
368    return getURLAttribute(srcAttr);
369}
370
371void HTMLTrackElement::setItemValueText(const String& value, ExceptionCode&)
372{
373    setAttribute(srcAttr, value);
374}
375#endif
376
377}
378
379#endif
380