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