blob: a0c20bfa7447378dbdeeea8ea9d41d05b0251613 [file] [log] [blame]
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +00001/*
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
beidson@apple.com03479702011-04-12 00:11:44 +000026#include "config.h"
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000027
commit-queue@webkit.orgaa4244a2011-04-15 07:13:49 +000028#if ENABLE(VIDEO_TRACK)
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000029#include "HTMLTrackElement.h"
30
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +000031#include "ContentSecurityPolicy.h"
eric.carlson@apple.comac33ead2011-10-18 17:34:27 +000032#include "Event.h"
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000033#include "HTMLMediaElement.h"
34#include "HTMLNames.h"
35#include "Logging.h"
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +000036#include "RuntimeEnabledFeatures.h"
annacc@chromium.orgb6ae44b2011-10-31 16:35:56 +000037#include "ScriptEventListener.h"
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000038
39using namespace std;
40
41namespace WebCore {
42
43using namespace HTMLNames;
44
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +000045#if !LOG_DISABLED
46static String urlForLogging(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
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000056inline HTMLTrackElement::HTMLTrackElement(const QualifiedName& tagName, Document* document)
57 : HTMLElement(tagName, document)
eric.carlson@apple.com456a0c42011-12-15 19:18:54 +000058 , m_hasBeenConfigured(false)
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000059{
60 LOG(Media, "HTMLTrackElement::HTMLTrackElement - %p", this);
61 ASSERT(hasTagName(trackTag));
62}
63
hclam@chromium.org6f068242011-06-24 23:06:01 +000064HTMLTrackElement::~HTMLTrackElement()
65{
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +000066 if (m_track)
67 m_track->clearClient();
hclam@chromium.org6f068242011-06-24 23:06:01 +000068}
69
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000070PassRefPtr<HTMLTrackElement> HTMLTrackElement::create(const QualifiedName& tagName, Document* document)
71{
72 return adoptRef(new HTMLTrackElement(tagName, document));
73}
74
darin@apple.com183547b2012-05-23 16:56:33 +000075Node::InsertionNotificationRequest HTMLTrackElement::insertedInto(ContainerNode* insertionPoint)
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000076{
commit-queue@webkit.org9ea00802012-04-17 06:40:55 +000077 HTMLElement::insertedInto(insertionPoint);
78 if (insertionPoint->inDocument()) {
79 if (HTMLMediaElement* parent = mediaElement())
80 parent->didAddTrack(this);
81 }
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +000082
commit-queue@webkit.org9ea00802012-04-17 06:40:55 +000083 return InsertionDone;
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000084}
85
darin@apple.com183547b2012-05-23 16:56:33 +000086void HTMLTrackElement::removedFrom(ContainerNode* insertionPoint)
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +000087{
morrita@google.com1b44b4d2012-05-10 11:37:09 +000088 HTMLMediaElement* parent = mediaElement();
89 if (!parent && WebCore::isMediaElement(insertionPoint))
90 parent = toMediaElement(insertionPoint);
91 if (parent)
eric.carlson@apple.com35424bc2012-03-21 18:28:48 +000092 parent->willRemoveTrack(this);
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +000093
morrita@google.com1b44b4d2012-05-10 11:37:09 +000094 HTMLElement::removedFrom(insertionPoint);
annacc@chromium.org6896b342011-10-28 17:54:32 +000095}
96
kling@webkit.org260fca82012-05-16 00:34:16 +000097void HTMLTrackElement::parseAttribute(const Attribute& attribute)
annacc@chromium.orgb6ae44b2011-10-31 16:35:56 +000098{
kling@webkit.orgcea5fce2012-02-18 19:58:57 +000099 if (RuntimeEnabledFeatures::webkitVideoTrackEnabled()) {
kling@webkit.org260fca82012-05-16 00:34:16 +0000100 if (attribute.name() == srcAttr) {
101 if (!attribute.isEmpty() && mediaElement())
kling@webkit.orgcea5fce2012-02-18 19:58:57 +0000102 scheduleLoad();
103 // 4.8.10.12.3 Sourcing out-of-band text tracks
104 // As the kind, label, and srclang attributes are set, changed, or removed, the text track must update accordingly...
kling@webkit.org260fca82012-05-16 00:34:16 +0000105 } else if (attribute.name() == kindAttr)
106 track()->setKind(attribute.value());
107 else if (attribute.name() == labelAttr)
108 track()->setLabel(attribute.value());
109 else if (attribute.name() == srclangAttr)
110 track()->setLanguage(attribute.value());
kling@webkit.orgcea5fce2012-02-18 19:58:57 +0000111 }
112
kling@webkit.org260fca82012-05-16 00:34:16 +0000113 if (attribute.name() == onloadAttr)
annacc@chromium.orgb6ae44b2011-10-31 16:35:56 +0000114 setAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(this, attribute));
kling@webkit.org260fca82012-05-16 00:34:16 +0000115 else if (attribute.name() == onerrorAttr)
annacc@chromium.orgb6ae44b2011-10-31 16:35:56 +0000116 setAttributeEventListener(eventNames().errorEvent, createAttributeEventListener(this, attribute));
117 else
kling@webkit.org8a3d4af2012-02-06 02:21:57 +0000118 HTMLElement::parseAttribute(attribute);
annacc@chromium.orgb6ae44b2011-10-31 16:35:56 +0000119}
120
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000121KURL HTMLTrackElement::src() const
122{
commit-queue@webkit.org03477c82011-09-02 17:07:51 +0000123 return document()->completeURL(getAttribute(srcAttr));
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000124}
125
126void HTMLTrackElement::setSrc(const String& url)
127{
128 setAttribute(srcAttr, url);
129}
130
eric.carlson@apple.com892b7112011-11-16 16:05:45 +0000131String HTMLTrackElement::kind()
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000132{
eric.carlson@apple.com892b7112011-11-16 16:05:45 +0000133 return track()->kind();
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000134}
135
136void HTMLTrackElement::setKind(const String& kind)
137{
138 setAttribute(kindAttr, kind);
139}
140
141String HTMLTrackElement::srclang() const
142{
commit-queue@webkit.org03477c82011-09-02 17:07:51 +0000143 return getAttribute(srclangAttr);
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000144}
145
146void HTMLTrackElement::setSrclang(const String& srclang)
147{
148 setAttribute(srclangAttr, srclang);
149}
150
151String HTMLTrackElement::label() const
152{
commit-queue@webkit.org03477c82011-09-02 17:07:51 +0000153 return getAttribute(labelAttr);
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000154}
155
156void HTMLTrackElement::setLabel(const String& label)
157{
158 setAttribute(labelAttr, label);
159}
160
161bool HTMLTrackElement::isDefault() const
162{
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000163 return fastHasAttribute(defaultAttr);
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000164}
165
166void HTMLTrackElement::setIsDefault(bool isDefault)
167{
168 setBooleanAttribute(defaultAttr, isDefault);
169}
170
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000171LoadableTextTrack* HTMLTrackElement::ensureTrack()
annacc@chromium.orgef5d1dc2011-10-20 02:53:38 +0000172{
eric.carlson@apple.com892b7112011-11-16 16:05:45 +0000173 if (!m_track) {
174 // The kind attribute is an enumerated attribute, limited only to know values. It defaults to 'subtitles' if missing or invalid.
175 String kind = getAttribute(kindAttr);
176 if (!TextTrack::isValidKindKeyword(kind))
177 kind = TextTrack::subtitlesKeyword();
178 m_track = LoadableTextTrack::create(this, kind, label(), srclang(), isDefault());
179 }
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000180 return m_track.get();
181}
182
183TextTrack* HTMLTrackElement::track()
184{
185 return ensureTrack();
annacc@chromium.orgef5d1dc2011-10-20 02:53:38 +0000186}
187
kling@webkit.orgd8a6d152012-05-08 16:27:04 +0000188bool HTMLTrackElement::isURLAttribute(const Attribute& attribute) const
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000189{
kling@webkit.orgd8a6d152012-05-08 16:27:04 +0000190 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000191}
192
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000193void HTMLTrackElement::scheduleLoad()
hclam@chromium.org6f068242011-06-24 23:06:01 +0000194{
eric.carlson@apple.com7010aef2011-11-26 04:28:39 +0000195 if (!RuntimeEnabledFeatures::webkitVideoTrackEnabled())
196 return;
197
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000198 if (!mediaElement())
199 return;
hclam@chromium.org6f068242011-06-24 23:06:01 +0000200
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000201 if (!fastHasAttribute(srcAttr))
202 return;
203
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000204 // 4.8.10.12.3 Sourcing out-of-band text tracks
205
206 // 1. Set the text track readiness state to loading.
207 setReadyState(HTMLTrackElement::LOADING);
208
209 KURL url = getNonEmptyURLAttribute(srcAttr);
210 if (!canLoadUrl(url)) {
211 didCompleteLoad(ensureTrack(), HTMLTrackElement::Failure);
212 return;
213 }
214
215 ensureTrack()->scheduleLoad(url);
hclam@chromium.org6f068242011-06-24 23:06:01 +0000216}
217
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000218bool HTMLTrackElement::canLoadUrl(const KURL& url)
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000219{
eric.carlson@apple.com7010aef2011-11-26 04:28:39 +0000220 if (!RuntimeEnabledFeatures::webkitVideoTrackEnabled())
221 return false;
222
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000223 HTMLMediaElement* parent = mediaElement();
224 if (!parent)
225 return false;
226
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000227 // 4.8.10.12.3 Sourcing out-of-band text tracks
228
229 // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the
230 // mode being the state of the media element's crossorigin content attribute, the origin being the
231 // origin of the media element's Document, and the default origin behaviour set to fail.
232 if (url.isEmpty())
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000233 return false;
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000234
235 if (!document()->contentSecurityPolicy()->allowMediaFromSource(url)) {
236 DEFINE_STATIC_LOCAL(String, consoleMessage, ("Text track load denied by Content Security Policy."));
237 document()->addConsoleMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, consoleMessage);
238 LOG(Media, "HTMLTrackElement::canLoadUrl(%s) -> rejected by Content Security Policy", urlForLogging(url).utf8().data());
239 return false;
240 }
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000241
242 return dispatchBeforeLoadEvent(url.string());
243}
244
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000245void HTMLTrackElement::didCompleteLoad(LoadableTextTrack*, LoadStatus status)
eric.carlson@apple.comac33ead2011-10-18 17:34:27 +0000246{
247 ExceptionCode ec = 0;
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000248
249 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
250
251 // 4. Download: ...
252 // If the fetching algorithm fails for any reason (network error, the server returns an error
253 // code, a cross-origin check fails, etc), or if URL is the empty string or has the wrong origin
254 // as determined by the condition at the start of this step, or if the fetched resource is not in
255 // a supported format, then queue a task to first change the text track readiness state to failed
256 // to load and then fire a simple event named error at the track element; and then, once that task
257 // is queued, move on to the step below labeled monitoring.
258
259 if (status == Failure) {
260 setReadyState(HTMLTrackElement::TRACK_ERROR);
261 dispatchEvent(Event::create(eventNames().errorEvent, false, false), ec);
262 return;
263 }
264
265 // If the fetching algorithm does not fail, then the final task that is queued by the networking
266 // task source must run the following steps:
267 // 1. Change the text track readiness state to loaded.
268 setReadyState(HTMLTrackElement::LOADED);
269
270 // 2. If the file was successfully processed, fire a simple event named load at the
271 // track element.
272 dispatchEvent(Event::create(eventNames().loadEvent, false, false), ec);
eric.carlson@apple.comac33ead2011-10-18 17:34:27 +0000273}
annacc@chromium.org333ded82011-11-27 23:25:52 +0000274
eric.carlson@apple.com0fa3f582011-12-01 16:42:49 +0000275// NOTE: The values in the TextTrack::ReadinessState enum must stay in sync with those in HTMLTrackElement::ReadyState.
276COMPILE_ASSERT(HTMLTrackElement::NONE == static_cast<HTMLTrackElement::ReadyState>(TextTrack::NotLoaded), TextTrackEnumNotLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumNONE);
277COMPILE_ASSERT(HTMLTrackElement::LOADING == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loading), TextTrackEnumLoadingIsWrong_ShouldBe_HTMLTrackElementEnumLOADING);
278COMPILE_ASSERT(HTMLTrackElement::LOADED == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loaded), TextTrackEnumLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumLOADED);
279COMPILE_ASSERT(HTMLTrackElement::TRACK_ERROR == static_cast<HTMLTrackElement::ReadyState>(TextTrack::FailedToLoad), TextTrackEnumFailedToLoad_Is_Wrong_Should_Be_HTMLTrackElementEnumTRACK_ERROR);
280
annacc@chromium.org333ded82011-11-27 23:25:52 +0000281void HTMLTrackElement::setReadyState(ReadyState state)
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000282{
eric.carlson@apple.com0fa3f582011-12-01 16:42:49 +0000283 ensureTrack()->setReadinessState(static_cast<TextTrack::ReadinessState>(state));
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000284 if (HTMLMediaElement* parent = mediaElement())
annacc@chromium.org333ded82011-11-27 23:25:52 +0000285 return parent->textTrackReadyStateChanged(m_track.get());
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000286}
eric.carlson@apple.com0fa3f582011-12-01 16:42:49 +0000287
288HTMLTrackElement::ReadyState HTMLTrackElement::readyState()
289{
290 return static_cast<ReadyState>(ensureTrack()->readinessState());
291}
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000292
293const AtomicString& HTMLTrackElement::mediaElementCrossOriginAttribute() const
294{
295 if (HTMLMediaElement* parent = mediaElement())
296 return parent->fastGetAttribute(HTMLNames::crossoriginAttr);
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000297
eric.carlson@apple.come3f1e6d2011-12-05 15:38:11 +0000298 return nullAtom;
299}
300
eric.carlson@apple.com892b7112011-11-16 16:05:45 +0000301void HTMLTrackElement::textTrackKindChanged(TextTrack* track)
302{
303 if (HTMLMediaElement* parent = mediaElement())
304 return parent->textTrackKindChanged(track);
305}
306
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000307void HTMLTrackElement::textTrackModeChanged(TextTrack* track)
308{
309 if (HTMLMediaElement* parent = mediaElement())
310 return parent->textTrackModeChanged(track);
311}
eric.carlson@apple.com892b7112011-11-16 16:05:45 +0000312
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000313void HTMLTrackElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
314{
315 if (HTMLMediaElement* parent = mediaElement())
316 return parent->textTrackAddCues(track, cues);
317}
318
319void HTMLTrackElement::textTrackRemoveCues(TextTrack* track, const TextTrackCueList* cues)
320{
321 if (HTMLMediaElement* parent = mediaElement())
annacc@chromium.org729c40e2012-04-12 20:29:50 +0000322 return parent->textTrackRemoveCues(track, cues);
eric.carlson@apple.com5c26c752011-11-11 17:33:51 +0000323}
324
325void HTMLTrackElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
326{
327 if (HTMLMediaElement* parent = mediaElement())
328 return parent->textTrackAddCue(track, cue);
329}
330
331void HTMLTrackElement::textTrackRemoveCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
332{
333 if (HTMLMediaElement* parent = mediaElement())
334 return parent->textTrackRemoveCue(track, cue);
335}
336
337HTMLMediaElement* HTMLTrackElement::mediaElement() const
338{
339 Element* parent = parentElement();
340 if (parent && parent->isMediaElement())
341 return static_cast<HTMLMediaElement*>(parentNode());
342
343 return 0;
344}
rniwa@webkit.orgd10f6702011-10-25 00:13:34 +0000345
346#if ENABLE(MICRODATA)
347String HTMLTrackElement::itemValueText() const
348{
349 return getURLAttribute(srcAttr);
350}
351
commit-queue@webkit.org7feb9532012-01-24 01:36:56 +0000352void HTMLTrackElement::setItemValueText(const String& value, ExceptionCode&)
rniwa@webkit.orgd10f6702011-10-25 00:13:34 +0000353{
commit-queue@webkit.org7feb9532012-01-24 01:36:56 +0000354 setAttribute(srcAttr, value);
rniwa@webkit.orgd10f6702011-10-25 00:13:34 +0000355}
356#endif
357
alexis.menard@openbossa.orgd41eb532011-04-11 23:11:00 +0000358}
359
360#endif