1 | /* |
2 | * Copyright (C) 2011 Google Inc. All rights reserved. |
3 | * Copyright (C) 2014-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 | |
29 | #if ENABLE(VIDEO_TRACK) |
30 | |
31 | #include "TextTrackLoader.h" |
32 | |
33 | #include "CachedResourceLoader.h" |
34 | #include "CachedResourceRequest.h" |
35 | #include "CachedTextTrack.h" |
36 | #include "CrossOriginAccessControl.h" |
37 | #include "Document.h" |
38 | #include "HTMLTrackElement.h" |
39 | #include "InspectorInstrumentation.h" |
40 | #include "Logging.h" |
41 | #include "SharedBuffer.h" |
42 | #include "VTTCue.h" |
43 | #include "WebVTTParser.h" |
44 | #include <wtf/NeverDestroyed.h> |
45 | |
46 | namespace WebCore { |
47 | |
48 | TextTrackLoader::TextTrackLoader(TextTrackLoaderClient& client, ScriptExecutionContext* context) |
49 | : m_client(client) |
50 | , m_scriptExecutionContext(context) |
51 | , m_cueLoadTimer(*this, &TextTrackLoader::cueLoadTimerFired) |
52 | , m_state(Idle) |
53 | , m_parseOffset(0) |
54 | , m_newCuesAvailable(false) |
55 | { |
56 | } |
57 | |
58 | TextTrackLoader::~TextTrackLoader() |
59 | { |
60 | if (m_resource) |
61 | m_resource->removeClient(*this); |
62 | } |
63 | |
64 | void TextTrackLoader::cueLoadTimerFired() |
65 | { |
66 | if (m_newCuesAvailable) { |
67 | m_newCuesAvailable = false; |
68 | m_client.newCuesAvailable(*this); |
69 | } |
70 | |
71 | if (m_state >= Finished) |
72 | m_client.cueLoadingCompleted(*this, m_state == Failed); |
73 | } |
74 | |
75 | void TextTrackLoader::cancelLoad() |
76 | { |
77 | if (m_resource) { |
78 | m_resource->removeClient(*this); |
79 | m_resource = nullptr; |
80 | } |
81 | } |
82 | |
83 | void TextTrackLoader::processNewCueData(CachedResource& resource) |
84 | { |
85 | ASSERT_UNUSED(resource, m_resource == &resource); |
86 | |
87 | if (m_state == Failed || !m_resource->resourceBuffer()) |
88 | return; |
89 | |
90 | auto* buffer = m_resource->resourceBuffer(); |
91 | if (m_parseOffset == buffer->size()) |
92 | return; |
93 | |
94 | if (!m_cueParser) |
95 | m_cueParser = std::make_unique<WebVTTParser>(static_cast<WebVTTParserClient*>(this), m_scriptExecutionContext); |
96 | |
97 | while (m_parseOffset < buffer->size()) { |
98 | auto data = buffer->getSomeData(m_parseOffset); |
99 | m_cueParser->parseBytes(data.data(), data.size()); |
100 | m_parseOffset += data.size(); |
101 | } |
102 | } |
103 | |
104 | // FIXME: This is a very unusual pattern, no other CachedResourceClient does this. Refactor to use notifyFinished() instead. |
105 | void TextTrackLoader::deprecatedDidReceiveCachedResource(CachedResource& resource) |
106 | { |
107 | ASSERT_UNUSED(resource, m_resource == &resource); |
108 | |
109 | if (!m_resource->resourceBuffer()) |
110 | return; |
111 | |
112 | processNewCueData(*m_resource); |
113 | } |
114 | |
115 | void TextTrackLoader::corsPolicyPreventedLoad() |
116 | { |
117 | static NeverDestroyed<String> consoleMessage(MAKE_STATIC_STRING_IMPL("Cross-origin text track load denied by Cross-Origin Resource Sharing policy." )); |
118 | Document* document = downcast<Document>(m_scriptExecutionContext); |
119 | document->addConsoleMessage(MessageSource::Security, MessageLevel::Error, consoleMessage); |
120 | m_state = Failed; |
121 | } |
122 | |
123 | void TextTrackLoader::notifyFinished(CachedResource& resource) |
124 | { |
125 | ASSERT_UNUSED(resource, m_resource == &resource); |
126 | |
127 | if (m_resource->resourceError().isAccessControl()) |
128 | corsPolicyPreventedLoad(); |
129 | |
130 | if (m_state != Failed) { |
131 | processNewCueData(*m_resource); |
132 | if (m_cueParser) |
133 | m_cueParser->fileFinished(); |
134 | if (m_state != Failed) |
135 | m_state = m_resource->errorOccurred() ? Failed : Finished; |
136 | } |
137 | |
138 | if (m_state == Finished && m_cueParser) |
139 | m_cueParser->flush(); |
140 | |
141 | if (!m_cueLoadTimer.isActive()) |
142 | m_cueLoadTimer.startOneShot(0_s); |
143 | |
144 | cancelLoad(); |
145 | } |
146 | |
147 | bool TextTrackLoader::load(const URL& url, HTMLTrackElement& element) |
148 | { |
149 | cancelLoad(); |
150 | |
151 | ASSERT(is<Document>(m_scriptExecutionContext)); |
152 | Document& document = downcast<Document>(*m_scriptExecutionContext); |
153 | |
154 | ResourceLoaderOptions options = CachedResourceLoader::defaultCachedResourceOptions(); |
155 | options.contentSecurityPolicyImposition = element.isInUserAgentShadowTree() ? ContentSecurityPolicyImposition::SkipPolicyCheck : ContentSecurityPolicyImposition::DoPolicyCheck; |
156 | |
157 | ResourceRequest resourceRequest(document.completeURL(url)); |
158 | |
159 | if (auto mediaElement = element.mediaElement()) |
160 | resourceRequest.setInspectorInitiatorNodeIdentifier(InspectorInstrumentation::identifierForNode(*mediaElement)); |
161 | |
162 | auto cueRequest = createPotentialAccessControlRequest(WTFMove(resourceRequest), document, element.mediaElementCrossOriginAttribute(), WTFMove(options)); |
163 | m_resource = document.cachedResourceLoader().requestTextTrack(WTFMove(cueRequest)).value_or(nullptr); |
164 | if (!m_resource) |
165 | return false; |
166 | m_resource->addClient(*this); |
167 | return true; |
168 | } |
169 | |
170 | void TextTrackLoader::newCuesParsed() |
171 | { |
172 | if (m_cueLoadTimer.isActive()) |
173 | return; |
174 | |
175 | m_newCuesAvailable = true; |
176 | m_cueLoadTimer.startOneShot(0_s); |
177 | } |
178 | |
179 | void TextTrackLoader::newRegionsParsed() |
180 | { |
181 | m_client.newRegionsAvailable(*this); |
182 | } |
183 | |
184 | void TextTrackLoader::newStyleSheetsParsed() |
185 | { |
186 | m_client.newStyleSheetsAvailable(*this); |
187 | } |
188 | |
189 | void TextTrackLoader::fileFailedToParse() |
190 | { |
191 | LOG(Media, "TextTrackLoader::fileFailedToParse" ); |
192 | |
193 | m_state = Failed; |
194 | |
195 | if (!m_cueLoadTimer.isActive()) |
196 | m_cueLoadTimer.startOneShot(0_s); |
197 | |
198 | cancelLoad(); |
199 | } |
200 | |
201 | void TextTrackLoader::getNewCues(Vector<RefPtr<TextTrackCue>>& outputCues) |
202 | { |
203 | ASSERT(m_cueParser); |
204 | if (m_cueParser) { |
205 | Vector<RefPtr<WebVTTCueData>> newCues; |
206 | m_cueParser->getNewCues(newCues); |
207 | |
208 | for (auto& cueData : newCues) |
209 | outputCues.append(VTTCue::create(*m_scriptExecutionContext, *cueData)); |
210 | } |
211 | } |
212 | |
213 | void TextTrackLoader::getNewRegions(Vector<RefPtr<VTTRegion>>& outputRegions) |
214 | { |
215 | ASSERT(m_cueParser); |
216 | if (m_cueParser) |
217 | m_cueParser->getNewRegions(outputRegions); |
218 | } |
219 | |
220 | Vector<String> TextTrackLoader::getNewStyleSheets() |
221 | { |
222 | ASSERT(m_cueParser); |
223 | if (m_cueParser) |
224 | return m_cueParser->getStyleSheets(); |
225 | return Vector<String>(); |
226 | } |
227 | |
228 | } |
229 | |
230 | #endif |
231 | |