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