| 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 "LoadableTextTrack.h" |
| 29 | |
| 30 | #if ENABLE(VIDEO_TRACK) |
| 31 | |
| 32 | #include "HTMLTrackElement.h" |
| 33 | #include "TextTrackCueList.h" |
| 34 | #include "VTTCue.h" |
| 35 | #include "VTTRegionList.h" |
| 36 | #include <wtf/IsoMallocInlines.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | WTF_MAKE_ISO_ALLOCATED_IMPL(LoadableTextTrack); |
| 41 | |
| 42 | LoadableTextTrack::LoadableTextTrack(HTMLTrackElement& track, const String& kind, const String& label, const String& language) |
| 43 | : TextTrack(&track.document(), &track, kind, emptyString(), label, language, TrackElement) |
| 44 | , m_trackElement(&track) |
| 45 | , m_loadTimer(*this, &LoadableTextTrack::loadTimerFired) |
| 46 | , m_isDefault(false) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void LoadableTextTrack::scheduleLoad(const URL& url) |
| 51 | { |
| 52 | if (url == m_url) |
| 53 | return; |
| 54 | |
| 55 | // When src attribute is changed we need to flush all collected track data |
| 56 | removeAllCues(); |
| 57 | |
| 58 | // 4.8.10.12.3 Sourcing out-of-band text tracks (continued) |
| 59 | |
| 60 | // 2. Let URL be the track URL of the track element. |
| 61 | m_url = url; |
| 62 | |
| 63 | // 3. Asynchronously run the remaining steps, while continuing with whatever task |
| 64 | // was responsible for creating the text track or changing the text track mode. |
| 65 | if (!m_loadTimer.isActive()) |
| 66 | m_loadTimer.startOneShot(0_s); |
| 67 | } |
| 68 | |
| 69 | Element* LoadableTextTrack::element() |
| 70 | { |
| 71 | return m_trackElement; |
| 72 | } |
| 73 | |
| 74 | void LoadableTextTrack::loadTimerFired() |
| 75 | { |
| 76 | if (m_loader) |
| 77 | m_loader->cancelLoad(); |
| 78 | |
| 79 | if (!m_trackElement) |
| 80 | return; |
| 81 | |
| 82 | // 4.8.10.12.3 Sourcing out-of-band text tracks (continued) |
| 83 | |
| 84 | // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the |
| 85 | // mode being the state of the media element's crossorigin content attribute, the origin being the |
| 86 | // origin of the media element's Document, and the default origin behaviour set to fail. |
| 87 | m_loader = std::make_unique<TextTrackLoader>(static_cast<TextTrackLoaderClient&>(*this), static_cast<ScriptExecutionContext*>(&m_trackElement->document())); |
| 88 | if (!m_loader->load(m_url, *m_trackElement)) |
| 89 | m_trackElement->didCompleteLoad(HTMLTrackElement::Failure); |
| 90 | } |
| 91 | |
| 92 | void LoadableTextTrack::newCuesAvailable(TextTrackLoader& loader) |
| 93 | { |
| 94 | ASSERT_UNUSED(loader, m_loader.get() == &loader); |
| 95 | |
| 96 | Vector<RefPtr<TextTrackCue>> newCues; |
| 97 | m_loader->getNewCues(newCues); |
| 98 | |
| 99 | if (!m_cues) |
| 100 | m_cues = TextTrackCueList::create(); |
| 101 | |
| 102 | for (auto& newCue : newCues) { |
| 103 | newCue->setTrack(this); |
| 104 | INFO_LOG(LOGIDENTIFIER, *toVTTCue(newCue.get())); |
| 105 | m_cues->add(newCue.releaseNonNull()); |
| 106 | } |
| 107 | |
| 108 | if (client()) |
| 109 | client()->textTrackAddCues(*this, *m_cues); |
| 110 | } |
| 111 | |
| 112 | void LoadableTextTrack::cueLoadingCompleted(TextTrackLoader& loader, bool loadingFailed) |
| 113 | { |
| 114 | ASSERT_UNUSED(loader, m_loader.get() == &loader); |
| 115 | |
| 116 | if (!m_trackElement) |
| 117 | return; |
| 118 | |
| 119 | INFO_LOG(LOGIDENTIFIER); |
| 120 | |
| 121 | m_trackElement->didCompleteLoad(loadingFailed ? HTMLTrackElement::Failure : HTMLTrackElement::Success); |
| 122 | } |
| 123 | |
| 124 | void LoadableTextTrack::newRegionsAvailable(TextTrackLoader& loader) |
| 125 | { |
| 126 | ASSERT_UNUSED(loader, m_loader.get() == &loader); |
| 127 | |
| 128 | Vector<RefPtr<VTTRegion>> newRegions; |
| 129 | m_loader->getNewRegions(newRegions); |
| 130 | |
| 131 | for (auto& newRegion : newRegions) { |
| 132 | newRegion->setTrack(this); |
| 133 | regions()->add(newRegion.releaseNonNull()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void LoadableTextTrack::newStyleSheetsAvailable(TextTrackLoader& loader) |
| 138 | { |
| 139 | ASSERT_UNUSED(loader, m_loader.get() == &loader); |
| 140 | m_styleSheets = m_loader->getNewStyleSheets(); |
| 141 | } |
| 142 | |
| 143 | AtomicString LoadableTextTrack::id() const |
| 144 | { |
| 145 | if (!m_trackElement) |
| 146 | return emptyAtom(); |
| 147 | return m_trackElement->attributeWithoutSynchronization(idAttr); |
| 148 | } |
| 149 | |
| 150 | size_t LoadableTextTrack::trackElementIndex() |
| 151 | { |
| 152 | ASSERT(m_trackElement); |
| 153 | ASSERT(m_trackElement->parentNode()); |
| 154 | |
| 155 | size_t index = 0; |
| 156 | for (RefPtr<Node> node = m_trackElement->parentNode()->firstChild(); node; node = node->nextSibling()) { |
| 157 | if (!node->hasTagName(trackTag) || !node->parentNode()) |
| 158 | continue; |
| 159 | if (node == m_trackElement) |
| 160 | return index; |
| 161 | ++index; |
| 162 | } |
| 163 | ASSERT_NOT_REACHED(); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | } // namespace WebCore |
| 169 | |
| 170 | #endif |
| 171 | |