| 1 | /* |
| 2 | * Copyright (C) 2011, 2013 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2011-2017 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 |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include "HTMLTrackElement.h" |
| 29 | |
| 30 | #if ENABLE(VIDEO_TRACK) |
| 31 | |
| 32 | #include "ContentSecurityPolicy.h" |
| 33 | #include "Event.h" |
| 34 | #include "EventNames.h" |
| 35 | #include "HTMLMediaElement.h" |
| 36 | #include "HTMLNames.h" |
| 37 | #include "Logging.h" |
| 38 | #include "RuntimeEnabledFeatures.h" |
| 39 | #include <wtf/IsoMallocInlines.h> |
| 40 | #include <wtf/text/CString.h> |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLTrackElement); |
| 45 | |
| 46 | using namespace HTMLNames; |
| 47 | |
| 48 | #if !LOG_DISABLED |
| 49 | |
| 50 | static String urlForLoggingTrack(const URL& url) |
| 51 | { |
| 52 | static const unsigned maximumURLLengthForLogging = 128; |
| 53 | |
| 54 | if (url.string().length() < maximumURLLengthForLogging) |
| 55 | return url.string(); |
| 56 | return url.string().substring(0, maximumURLLengthForLogging) + "..." ; |
| 57 | } |
| 58 | |
| 59 | #endif |
| 60 | |
| 61 | inline HTMLTrackElement::HTMLTrackElement(const QualifiedName& tagName, Document& document) |
| 62 | : HTMLElement(tagName, document) |
| 63 | , m_loadTimer(*this, &HTMLTrackElement::loadTimerFired) |
| 64 | { |
| 65 | LOG(Media, "HTMLTrackElement::HTMLTrackElement - %p" , this); |
| 66 | ASSERT(hasTagName(trackTag)); |
| 67 | } |
| 68 | |
| 69 | HTMLTrackElement::~HTMLTrackElement() |
| 70 | { |
| 71 | if (m_track) { |
| 72 | m_track->clearElement(); |
| 73 | m_track->clearClient(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | Ref<HTMLTrackElement> HTMLTrackElement::create(const QualifiedName& tagName, Document& document) |
| 78 | { |
| 79 | return adoptRef(*new HTMLTrackElement(tagName, document)); |
| 80 | } |
| 81 | |
| 82 | Node::InsertedIntoAncestorResult HTMLTrackElement::insertedIntoAncestor(InsertionType insertionType, ContainerNode& parentOfInsertedTree) |
| 83 | { |
| 84 | HTMLElement::insertedIntoAncestor(insertionType, parentOfInsertedTree); |
| 85 | |
| 86 | if (parentNode() == &parentOfInsertedTree && is<HTMLMediaElement>(parentOfInsertedTree)) { |
| 87 | downcast<HTMLMediaElement>(parentOfInsertedTree).didAddTextTrack(*this); |
| 88 | scheduleLoad(); |
| 89 | } |
| 90 | |
| 91 | return InsertedIntoAncestorResult::Done; |
| 92 | } |
| 93 | |
| 94 | void HTMLTrackElement::removedFromAncestor(RemovalType removalType, ContainerNode& oldParentOfRemovedTree) |
| 95 | { |
| 96 | HTMLElement::removedFromAncestor(removalType, oldParentOfRemovedTree); |
| 97 | |
| 98 | if (!parentNode() && is<HTMLMediaElement>(oldParentOfRemovedTree)) |
| 99 | downcast<HTMLMediaElement>(oldParentOfRemovedTree).didRemoveTextTrack(*this); |
| 100 | } |
| 101 | |
| 102 | void HTMLTrackElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
| 103 | { |
| 104 | if (name == srcAttr) { |
| 105 | scheduleLoad(); |
| 106 | |
| 107 | // 4.8.10.12.3 Sourcing out-of-band text tracks |
| 108 | // As the kind, label, and srclang attributes are set, changed, or removed, the text track must update accordingly... |
| 109 | } else if (name == kindAttr) |
| 110 | track().setKindKeywordIgnoringASCIICase(value.string()); |
| 111 | else if (name == labelAttr) |
| 112 | track().setLabel(value); |
| 113 | else if (name == srclangAttr) |
| 114 | track().setLanguage(value); |
| 115 | else if (name == defaultAttr) |
| 116 | track().setIsDefault(!value.isNull()); |
| 117 | |
| 118 | HTMLElement::parseAttribute(name, value); |
| 119 | } |
| 120 | |
| 121 | const AtomicString& HTMLTrackElement::kind() |
| 122 | { |
| 123 | return track().kindKeyword(); |
| 124 | } |
| 125 | |
| 126 | void HTMLTrackElement::setKind(const AtomicString& kind) |
| 127 | { |
| 128 | setAttributeWithoutSynchronization(kindAttr, kind); |
| 129 | } |
| 130 | |
| 131 | const AtomicString& HTMLTrackElement::srclang() const |
| 132 | { |
| 133 | return attributeWithoutSynchronization(srclangAttr); |
| 134 | } |
| 135 | |
| 136 | const AtomicString& HTMLTrackElement::label() const |
| 137 | { |
| 138 | return attributeWithoutSynchronization(labelAttr); |
| 139 | } |
| 140 | |
| 141 | bool HTMLTrackElement::isDefault() const |
| 142 | { |
| 143 | return hasAttributeWithoutSynchronization(defaultAttr); |
| 144 | } |
| 145 | |
| 146 | LoadableTextTrack& HTMLTrackElement::track() |
| 147 | { |
| 148 | // FIXME: There is no practical value in lazily initializing this. |
| 149 | // Instead this should be created in the constructor. |
| 150 | if (!m_track) { |
| 151 | // The kind attribute is an enumerated attribute, limited only to known values. It defaults to 'subtitles' if missing or invalid. |
| 152 | String kind = attributeWithoutSynchronization(kindAttr).convertToASCIILowercase(); |
| 153 | if (!TextTrack::isValidKindKeyword(kind)) |
| 154 | kind = TextTrack::subtitlesKeyword(); |
| 155 | m_track = LoadableTextTrack::create(*this, kind, label(), srclang()); |
| 156 | } |
| 157 | ASSERT(m_track->trackElement() == this); |
| 158 | |
| 159 | return *m_track; |
| 160 | } |
| 161 | |
| 162 | bool HTMLTrackElement::isURLAttribute(const Attribute& attribute) const |
| 163 | { |
| 164 | return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute); |
| 165 | } |
| 166 | |
| 167 | void HTMLTrackElement::scheduleLoad() |
| 168 | { |
| 169 | // 1. If another occurrence of this algorithm is already running for this text track and its track element, |
| 170 | // abort these steps, letting that other algorithm take care of this element. |
| 171 | if (m_loadTimer.isActive()) |
| 172 | return; |
| 173 | |
| 174 | // 2. If the text track's text track mode is not set to one of hidden or showing, abort these steps. |
| 175 | if (track().mode() != TextTrack::Mode::Hidden && track().mode() != TextTrack::Mode::Showing) |
| 176 | return; |
| 177 | |
| 178 | // 3. If the text track's track element does not have a media element as a parent, abort these steps. |
| 179 | if (!mediaElement()) |
| 180 | return; |
| 181 | |
| 182 | // 4. Run the remainder of these steps asynchronously, allowing whatever caused these steps to run to continue. |
| 183 | m_loadTimer.startOneShot(0_s); |
| 184 | } |
| 185 | |
| 186 | void HTMLTrackElement::loadTimerFired() |
| 187 | { |
| 188 | if (!hasAttributeWithoutSynchronization(srcAttr)) { |
| 189 | track().removeAllCues(); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | // 6. Set the text track readiness state to loading. |
| 194 | setReadyState(HTMLTrackElement::LOADING); |
| 195 | |
| 196 | // 7. Let URL be the track URL of the track element. |
| 197 | URL url = getNonEmptyURLAttribute(srcAttr); |
| 198 | |
| 199 | // ... if URL is the empty string, then queue a task to first change the text track readiness state |
| 200 | // to failed to load and then fire an event named error at the track element. |
| 201 | // 8. If the track element's parent is a media element then let CORS mode be the state of the parent media |
| 202 | // element's crossorigin content attribute. Otherwise, let CORS mode be No CORS. |
| 203 | if (url.isEmpty() || !canLoadURL(url)) { |
| 204 | track().removeAllCues(); |
| 205 | didCompleteLoad(HTMLTrackElement::Failure); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | track().scheduleLoad(url); |
| 210 | } |
| 211 | |
| 212 | bool HTMLTrackElement::canLoadURL(const URL& url) |
| 213 | { |
| 214 | auto parent = mediaElement(); |
| 215 | if (!parent) |
| 216 | return false; |
| 217 | |
| 218 | // 4.8.10.12.3 Sourcing out-of-band text tracks |
| 219 | |
| 220 | // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the |
| 221 | // mode being the state of the media element's crossorigin content attribute, the origin being the |
| 222 | // origin of the media element's Document, and the default origin behaviour set to fail. |
| 223 | if (url.isEmpty()) |
| 224 | return false; |
| 225 | |
| 226 | ASSERT(document().contentSecurityPolicy()); |
| 227 | // Elements in user agent show tree should load whatever the embedding document policy is. |
| 228 | if (!isInUserAgentShadowTree() && !document().contentSecurityPolicy()->allowMediaFromSource(url)) { |
| 229 | LOG(Media, "HTMLTrackElement::canLoadURL(%s) -> rejected by Content Security Policy" , urlForLoggingTrack(url).utf8().data()); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return dispatchBeforeLoadEvent(url.string()); |
| 234 | } |
| 235 | |
| 236 | void HTMLTrackElement::didCompleteLoad(LoadStatus status) |
| 237 | { |
| 238 | // 4.8.10.12.3 Sourcing out-of-band text tracks (continued) |
| 239 | |
| 240 | // 4. Download: ... |
| 241 | // If the fetching algorithm fails for any reason (network error, the server returns an error |
| 242 | // code, a cross-origin check fails, etc), or if URL is the empty string or has the wrong origin |
| 243 | // as determined by the condition at the start of this step, or if the fetched resource is not in |
| 244 | // a supported format, then queue a task to first change the text track readiness state to failed |
| 245 | // to load and then fire a simple event named error at the track element; and then, once that task |
| 246 | // is queued, move on to the step below labeled monitoring. |
| 247 | |
| 248 | if (status == Failure) { |
| 249 | setReadyState(HTMLTrackElement::TRACK_ERROR); |
| 250 | dispatchEvent(Event::create(eventNames().errorEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // If the fetching algorithm does not fail, then the final task that is queued by the networking |
| 255 | // task source must run the following steps: |
| 256 | // 1. Change the text track readiness state to loaded. |
| 257 | setReadyState(HTMLTrackElement::LOADED); |
| 258 | |
| 259 | // 2. If the file was successfully processed, fire a simple event named load at the |
| 260 | // track element. |
| 261 | dispatchEvent(Event::create(eventNames().loadEvent, Event::CanBubble::No, Event::IsCancelable::No)); |
| 262 | } |
| 263 | |
| 264 | // NOTE: The values in the TextTrack::ReadinessState enum must stay in sync with those in HTMLTrackElement::ReadyState. |
| 265 | COMPILE_ASSERT(HTMLTrackElement::NONE == static_cast<HTMLTrackElement::ReadyState>(TextTrack::NotLoaded), TextTrackEnumNotLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumNONE); |
| 266 | COMPILE_ASSERT(HTMLTrackElement::LOADING == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loading), TextTrackEnumLoadingIsWrong_ShouldBe_HTMLTrackElementEnumLOADING); |
| 267 | COMPILE_ASSERT(HTMLTrackElement::LOADED == static_cast<HTMLTrackElement::ReadyState>(TextTrack::Loaded), TextTrackEnumLoaded_Is_Wrong_Should_Be_HTMLTrackElementEnumLOADED); |
| 268 | COMPILE_ASSERT(HTMLTrackElement::TRACK_ERROR == static_cast<HTMLTrackElement::ReadyState>(TextTrack::FailedToLoad), TextTrackEnumFailedToLoad_Is_Wrong_Should_Be_HTMLTrackElementEnumTRACK_ERROR); |
| 269 | |
| 270 | void HTMLTrackElement::setReadyState(ReadyState state) |
| 271 | { |
| 272 | track().setReadinessState(static_cast<TextTrack::ReadinessState>(state)); |
| 273 | if (auto parent = mediaElement()) |
| 274 | parent->textTrackReadyStateChanged(m_track.get()); |
| 275 | } |
| 276 | |
| 277 | HTMLTrackElement::ReadyState HTMLTrackElement::readyState() |
| 278 | { |
| 279 | return static_cast<ReadyState>(track().readinessState()); |
| 280 | } |
| 281 | |
| 282 | const AtomicString& HTMLTrackElement::mediaElementCrossOriginAttribute() const |
| 283 | { |
| 284 | if (auto parent = mediaElement()) |
| 285 | return parent->attributeWithoutSynchronization(HTMLNames::crossoriginAttr); |
| 286 | return nullAtom(); |
| 287 | } |
| 288 | |
| 289 | void HTMLTrackElement::textTrackKindChanged(TextTrack& track) |
| 290 | { |
| 291 | if (auto parent = mediaElement()) |
| 292 | parent->textTrackKindChanged(track); |
| 293 | } |
| 294 | |
| 295 | void HTMLTrackElement::textTrackModeChanged(TextTrack& track) |
| 296 | { |
| 297 | // Since we've moved to a new parent, we may now be able to load. |
| 298 | if (readyState() == HTMLTrackElement::NONE) |
| 299 | scheduleLoad(); |
| 300 | |
| 301 | if (auto parent = mediaElement()) |
| 302 | parent->textTrackModeChanged(track); |
| 303 | } |
| 304 | |
| 305 | void HTMLTrackElement::textTrackAddCues(TextTrack& track, const TextTrackCueList& cues) |
| 306 | { |
| 307 | if (auto parent = mediaElement()) |
| 308 | parent->textTrackAddCues(track, cues); |
| 309 | } |
| 310 | |
| 311 | void HTMLTrackElement::textTrackRemoveCues(TextTrack& track, const TextTrackCueList& cues) |
| 312 | { |
| 313 | if (auto parent = mediaElement()) |
| 314 | parent->textTrackRemoveCues(track, cues); |
| 315 | } |
| 316 | |
| 317 | void HTMLTrackElement::textTrackAddCue(TextTrack& track, TextTrackCue& cue) |
| 318 | { |
| 319 | if (auto parent = mediaElement()) |
| 320 | parent->textTrackAddCue(track, cue); |
| 321 | } |
| 322 | |
| 323 | void HTMLTrackElement::textTrackRemoveCue(TextTrack& track, TextTrackCue& cue) |
| 324 | { |
| 325 | if (auto parent = mediaElement()) |
| 326 | parent->textTrackRemoveCue(track, cue); |
| 327 | } |
| 328 | |
| 329 | RefPtr<HTMLMediaElement> HTMLTrackElement::mediaElement() const |
| 330 | { |
| 331 | auto parent = makeRefPtr(parentElement()); |
| 332 | if (!is<HTMLMediaElement>(parent)) |
| 333 | return nullptr; |
| 334 | return downcast<HTMLMediaElement>(parent.get()); |
| 335 | } |
| 336 | |
| 337 | } |
| 338 | |
| 339 | #endif |
| 340 | |