OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/fetch/FetchBlobDataConsumerHandle.h" | 5 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
6 | 6 |
7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/ExecutionContext.h" |
8 #include "core/fetch/FetchInitiatorTypeNames.h" | 8 #include "core/fetch/FetchInitiatorTypeNames.h" |
9 #include "core/loader/ThreadableLoaderClient.h" | 9 #include "core/loader/ThreadableLoaderClient.h" |
10 #include "modules/fetch/CompositeDataConsumerHandle.h" | 10 #include "modules/fetch/CompositeDataConsumerHandle.h" |
11 #include "modules/fetch/CrossThreadHolder.h" | 11 #include "modules/fetch/CrossThreadHolder.h" |
12 #include "modules/fetch/DataConsumerHandleUtil.h" | 12 #include "modules/fetch/DataConsumerHandleUtil.h" |
13 #include "platform/blob/BlobRegistry.h" | 13 #include "platform/blob/BlobRegistry.h" |
14 #include "platform/blob/BlobURL.h" | 14 #include "platform/blob/BlobURL.h" |
15 #include "platform/network/ResourceRequest.h" | 15 #include "platform/network/ResourceRequest.h" |
16 #include "wtf/PtrUtil.h" | 16 #include "wtf/OwnPtr.h" |
17 #include <memory> | |
18 | 17 |
19 namespace blink { | 18 namespace blink { |
20 | 19 |
21 using Result = FetchBlobDataConsumerHandle::Result; | 20 using Result = FetchBlobDataConsumerHandle::Result; |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 // Object graph: | 24 // Object graph: |
26 // +-------------+ | 25 // +-------------+ |
27 // |ReaderContext| | 26 // |ReaderContext| |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 ASSERT(m_loader); | 95 ASSERT(m_loader); |
97 | 96 |
98 ResourceRequest request(url); | 97 ResourceRequest request(url); |
99 request.setRequestContext(WebURLRequest::RequestContextInternal); | 98 request.setRequestContext(WebURLRequest::RequestContextInternal); |
100 request.setUseStreamOnResponse(true); | 99 request.setUseStreamOnResponse(true); |
101 // We intentionally skip 'setExternalRequestStateFromRequestorAddressSpa
ce', as 'data:' can never be external. | 100 // We intentionally skip 'setExternalRequestStateFromRequestorAddressSpa
ce', as 'data:' can never be external. |
102 m_loader->start(request); | 101 m_loader->start(request); |
103 } | 102 } |
104 | 103 |
105 private: | 104 private: |
106 std::unique_ptr<ThreadableLoader> createLoader(ExecutionContext* executionCo
ntext, ThreadableLoaderClient* client) const | 105 PassOwnPtr<ThreadableLoader> createLoader(ExecutionContext* executionContext
, ThreadableLoaderClient* client) const |
107 { | 106 { |
108 ThreadableLoaderOptions options; | 107 ThreadableLoaderOptions options; |
109 options.preflightPolicy = ConsiderPreflight; | 108 options.preflightPolicy = ConsiderPreflight; |
110 options.crossOriginRequestPolicy = DenyCrossOriginRequests; | 109 options.crossOriginRequestPolicy = DenyCrossOriginRequests; |
111 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPo
licy; | 110 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPo
licy; |
112 options.initiator = FetchInitiatorTypeNames::internal; | 111 options.initiator = FetchInitiatorTypeNames::internal; |
113 | 112 |
114 ResourceLoaderOptions resourceLoaderOptions; | 113 ResourceLoaderOptions resourceLoaderOptions; |
115 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; | 114 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; |
116 | 115 |
117 return m_loaderFactory->create(*executionContext, client, options, resou
rceLoaderOptions); | 116 return m_loaderFactory->create(*executionContext, client, options, resou
rceLoaderOptions); |
118 } | 117 } |
119 | 118 |
120 // ThreadableLoaderClient | 119 // ThreadableLoaderClient |
121 void didReceiveResponse(unsigned long, const ResourceResponse&, std::unique_
ptr<WebDataConsumerHandle> handle) override | 120 void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<W
ebDataConsumerHandle> handle) override |
122 { | 121 { |
123 ASSERT(!m_receivedResponse); | 122 ASSERT(!m_receivedResponse); |
124 m_receivedResponse = true; | 123 m_receivedResponse = true; |
125 if (!handle) { | 124 if (!handle) { |
126 // Here we assume WebURLLoader must return the response body as | 125 // Here we assume WebURLLoader must return the response body as |
127 // |WebDataConsumerHandle| since we call | 126 // |WebDataConsumerHandle| since we call |
128 // request.setUseStreamOnResponse(). | 127 // request.setUseStreamOnResponse(). |
129 m_updater->update(createUnexpectedErrorDataConsumerHandle()); | 128 m_updater->update(createUnexpectedErrorDataConsumerHandle()); |
130 return; | 129 return; |
131 } | 130 } |
(...skipping 15 matching lines...) Expand all Loading... |
147 void didFailRedirectCheck() override | 146 void didFailRedirectCheck() override |
148 { | 147 { |
149 // We don't expect redirects for Blob loading. | 148 // We don't expect redirects for Blob loading. |
150 ASSERT_NOT_REACHED(); | 149 ASSERT_NOT_REACHED(); |
151 } | 150 } |
152 | 151 |
153 Persistent<CompositeDataConsumerHandle::Updater> m_updater; | 152 Persistent<CompositeDataConsumerHandle::Updater> m_updater; |
154 | 153 |
155 RefPtr<BlobDataHandle> m_blobDataHandle; | 154 RefPtr<BlobDataHandle> m_blobDataHandle; |
156 Persistent<FetchBlobDataConsumerHandle::LoaderFactory> m_loaderFactory; | 155 Persistent<FetchBlobDataConsumerHandle::LoaderFactory> m_loaderFactory; |
157 std::unique_ptr<ThreadableLoader> m_loader; | 156 OwnPtr<ThreadableLoader> m_loader; |
158 | 157 |
159 bool m_receivedResponse; | 158 bool m_receivedResponse; |
160 }; | 159 }; |
161 | 160 |
162 class DefaultLoaderFactory final : public FetchBlobDataConsumerHandle::LoaderFac
tory { | 161 class DefaultLoaderFactory final : public FetchBlobDataConsumerHandle::LoaderFac
tory { |
163 public: | 162 public: |
164 std::unique_ptr<ThreadableLoader> create( | 163 PassOwnPtr<ThreadableLoader> create( |
165 ExecutionContext& executionContext, | 164 ExecutionContext& executionContext, |
166 ThreadableLoaderClient* client, | 165 ThreadableLoaderClient* client, |
167 const ThreadableLoaderOptions& options, | 166 const ThreadableLoaderOptions& options, |
168 const ResourceLoaderOptions& resourceLoaderOptions) override | 167 const ResourceLoaderOptions& resourceLoaderOptions) override |
169 { | 168 { |
170 return ThreadableLoader::create(executionContext, client, options, resou
rceLoaderOptions); | 169 return ThreadableLoader::create(executionContext, client, options, resou
rceLoaderOptions); |
171 } | 170 } |
172 }; | 171 }; |
173 | 172 |
174 } // namespace | 173 } // namespace |
175 | 174 |
176 // ReaderContext is referenced from FetchBlobDataConsumerHandle and | 175 // ReaderContext is referenced from FetchBlobDataConsumerHandle and |
177 // ReaderContext::ReaderImpl. | 176 // ReaderContext::ReaderImpl. |
178 // All functions/members must be called/accessed only on the reader thread, | 177 // All functions/members must be called/accessed only on the reader thread, |
179 // except for constructor, destructor, and obtainReader(). | 178 // except for constructor, destructor, and obtainReader(). |
180 class FetchBlobDataConsumerHandle::ReaderContext final : public ThreadSafeRefCou
nted<ReaderContext> { | 179 class FetchBlobDataConsumerHandle::ReaderContext final : public ThreadSafeRefCou
nted<ReaderContext> { |
181 public: | 180 public: |
182 class ReaderImpl : public FetchDataConsumerHandle::Reader { | 181 class ReaderImpl : public FetchDataConsumerHandle::Reader { |
183 public: | 182 public: |
184 ReaderImpl(Client* client, PassRefPtr<ReaderContext> readerContext, std:
:unique_ptr<WebDataConsumerHandle::Reader> reader) | 183 ReaderImpl(Client* client, PassRefPtr<ReaderContext> readerContext, Pass
OwnPtr<WebDataConsumerHandle::Reader> reader) |
185 : m_readerContext(readerContext) | 184 : m_readerContext(readerContext) |
186 , m_reader(std::move(reader)) | 185 , m_reader(std::move(reader)) |
187 , m_notifier(client) { } | 186 , m_notifier(client) { } |
188 ~ReaderImpl() override { } | 187 ~ReaderImpl() override { } |
189 | 188 |
190 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride | 189 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride |
191 { | 190 { |
192 if (m_readerContext->drained()) | 191 if (m_readerContext->drained()) |
193 return Done; | 192 return Done; |
194 m_readerContext->ensureStartLoader(); | 193 m_readerContext->ensureStartLoader(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 RefPtr<BlobDataHandle> handle = drainAsBlobDataHandle(AllowBlobWithI
nvalidSize); | 231 RefPtr<BlobDataHandle> handle = drainAsBlobDataHandle(AllowBlobWithI
nvalidSize); |
233 if (!handle) | 232 if (!handle) |
234 return nullptr; | 233 return nullptr; |
235 RefPtr<EncodedFormData> formData = EncodedFormData::create(); | 234 RefPtr<EncodedFormData> formData = EncodedFormData::create(); |
236 formData->appendBlob(handle->uuid(), handle); | 235 formData->appendBlob(handle->uuid(), handle); |
237 return formData.release(); | 236 return formData.release(); |
238 } | 237 } |
239 | 238 |
240 private: | 239 private: |
241 RefPtr<ReaderContext> m_readerContext; | 240 RefPtr<ReaderContext> m_readerContext; |
242 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; | 241 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
243 NotifyOnReaderCreationHelper m_notifier; | 242 NotifyOnReaderCreationHelper m_notifier; |
244 }; | 243 }; |
245 | 244 |
246 ReaderContext(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle>
blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) | 245 ReaderContext(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle>
blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) |
247 : m_blobDataHandleForDrain(blobDataHandle) | 246 : m_blobDataHandleForDrain(blobDataHandle) |
248 , m_loaderStarted(false) | 247 , m_loaderStarted(false) |
249 , m_drained(false) | 248 , m_drained(false) |
250 { | 249 { |
251 CompositeDataConsumerHandle::Updater* updater = nullptr; | 250 CompositeDataConsumerHandle::Updater* updater = nullptr; |
252 m_handle = CompositeDataConsumerHandle::create(createWaitingDataConsumer
Handle(), &updater); | 251 m_handle = CompositeDataConsumerHandle::create(createWaitingDataConsumer
Handle(), &updater); |
253 m_loaderContextHolder = CrossThreadHolder<LoaderContext>::create(executi
onContext, wrapUnique(new BlobLoaderContext(updater, m_blobDataHandleForDrain, l
oaderFactory))); | 252 m_loaderContextHolder = CrossThreadHolder<LoaderContext>::create(executi
onContext, adoptPtr(new BlobLoaderContext(updater, m_blobDataHandleForDrain, loa
derFactory))); |
254 } | 253 } |
255 | 254 |
256 std::unique_ptr<FetchDataConsumerHandle::Reader> obtainReader(WebDataConsume
rHandle::Client* client) | 255 PassOwnPtr<FetchDataConsumerHandle::Reader> obtainReader(WebDataConsumerHand
le::Client* client) |
257 { | 256 { |
258 return wrapUnique(new ReaderImpl(client, this, m_handle->obtainReader(cl
ient))); | 257 return adoptPtr(new ReaderImpl(client, this, m_handle->obtainReader(clie
nt))); |
259 } | 258 } |
260 | 259 |
261 private: | 260 private: |
262 void ensureStartLoader() | 261 void ensureStartLoader() |
263 { | 262 { |
264 if (m_loaderStarted) | 263 if (m_loaderStarted) |
265 return; | 264 return; |
266 m_loaderStarted = true; | 265 m_loaderStarted = true; |
267 m_loaderContextHolder->postTask(threadSafeBind<LoaderContext*, Execution
Context*>(&LoaderContext::start)); | 266 m_loaderContextHolder->postTask(threadSafeBind<LoaderContext*, Execution
Context*>(&LoaderContext::start)); |
268 } | 267 } |
269 | 268 |
270 void clearBlobDataHandleForDrain() | 269 void clearBlobDataHandleForDrain() |
271 { | 270 { |
272 m_blobDataHandleForDrain.clear(); | 271 m_blobDataHandleForDrain.clear(); |
273 } | 272 } |
274 | 273 |
275 bool drained() const { return m_drained; } | 274 bool drained() const { return m_drained; } |
276 void setDrained() { m_drained = true; } | 275 void setDrained() { m_drained = true; } |
277 | 276 |
278 std::unique_ptr<WebDataConsumerHandle> m_handle; | 277 OwnPtr<WebDataConsumerHandle> m_handle; |
279 RefPtr<BlobDataHandle> m_blobDataHandleForDrain; | 278 RefPtr<BlobDataHandle> m_blobDataHandleForDrain; |
280 std::unique_ptr<CrossThreadHolder<LoaderContext>> m_loaderContextHolder; | 279 OwnPtr<CrossThreadHolder<LoaderContext>> m_loaderContextHolder; |
281 | 280 |
282 bool m_loaderStarted; | 281 bool m_loaderStarted; |
283 bool m_drained; | 282 bool m_drained; |
284 }; | 283 }; |
285 | 284 |
286 FetchBlobDataConsumerHandle::FetchBlobDataConsumerHandle(ExecutionContext* execu
tionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFactory* loaderFac
tory) | 285 FetchBlobDataConsumerHandle::FetchBlobDataConsumerHandle(ExecutionContext* execu
tionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFactory* loaderFac
tory) |
287 : m_readerContext(adoptRef(new ReaderContext(executionContext, blobDataHandl
e, loaderFactory))) | 286 : m_readerContext(adoptRef(new ReaderContext(executionContext, blobDataHandl
e, loaderFactory))) |
288 { | 287 { |
289 } | 288 } |
290 | 289 |
291 FetchBlobDataConsumerHandle::~FetchBlobDataConsumerHandle() | 290 FetchBlobDataConsumerHandle::~FetchBlobDataConsumerHandle() |
292 { | 291 { |
293 } | 292 } |
294 | 293 |
295 std::unique_ptr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Exe
cutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle, Load
erFactory* loaderFactory) | 294 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio
nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFac
tory* loaderFactory) |
296 { | 295 { |
297 if (!blobDataHandle) | 296 if (!blobDataHandle) |
298 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); | 297 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); |
299 | 298 |
300 return wrapUnique(new FetchBlobDataConsumerHandle(executionContext, blobData
Handle, loaderFactory)); | 299 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, loaderFactory)); |
301 } | 300 } |
302 | 301 |
303 std::unique_ptr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Exe
cutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) | 302 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio
nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
304 { | 303 { |
305 if (!blobDataHandle) | 304 if (!blobDataHandle) |
306 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); | 305 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); |
307 | 306 |
308 return wrapUnique(new FetchBlobDataConsumerHandle(executionContext, blobData
Handle, new DefaultLoaderFactory)); | 307 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, new DefaultLoaderFactory)); |
309 } | 308 } |
310 | 309 |
311 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern
al(Client* client) | 310 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern
al(Client* client) |
312 { | 311 { |
313 return m_readerContext->obtainReader(client).release(); | 312 return m_readerContext->obtainReader(client).leakPtr(); |
314 } | 313 } |
315 | 314 |
316 } // namespace blink | 315 } // namespace blink |
OLD | NEW |