OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| 7 |
| 8 #include "core/dom/ActiveDOMObject.h" |
| 9 #include "core/dom/CrossThreadTask.h" |
| 10 #include "core/dom/ExecutionContext.h" |
| 11 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 12 #include "core/loader/ThreadableLoaderClient.h" |
| 13 #include "modules/fetch/CompositeDataConsumerHandle.h" |
| 14 #include "modules/fetch/DataConsumerHandleUtil.h" |
| 15 #include "platform/Task.h" |
| 16 #include "platform/blob/BlobRegistry.h" |
| 17 #include "platform/blob/BlobURL.h" |
| 18 #include "platform/network/ResourceRequest.h" |
| 19 #include "public/platform/Platform.h" |
| 20 #include "public/platform/WebTraceLocation.h" |
| 21 #include "wtf/Locker.h" |
| 22 #include "wtf/ThreadingPrimitives.h" |
| 23 |
| 24 namespace blink { |
| 25 |
| 26 using Result = FetchBlobDataConsumerHandle::Result; |
| 27 |
| 28 namespace { |
| 29 |
| 30 // CrossThreadHolder<T> provides cross-thread access to |obj| of class T |
| 31 // bound to the thread of |executionContext| where |obj| is created. |
| 32 // - CrossThreadHolder<T> can be passed across threads. |
| 33 // - |obj|'s methods are called on the thread of |executionContext| |
| 34 // via CrossThreadHolder<T>::postTask(). |
| 35 // - |obj| is destructed on the thread of |executionContext| |
| 36 // when |executionContext| is stopped or |
| 37 // CrossThreadHolder is destructed (earlier of them). |
| 38 // Note: |obj|'s destruction can be slightly after CrossThreadHolder. |
| 39 template<typename T> |
| 40 class CrossThreadHolder { |
| 41 public: |
| 42 // Must be called on the thread where |obj| is created |
| 43 // (== the thread of |executionContext|). |
| 44 // The current thread must be attached to Oilpan. |
| 45 static PassOwnPtr<CrossThreadHolder<T>> create(ExecutionContext* executionCo
ntext, PassOwnPtr<T> obj) |
| 46 { |
| 47 ASSERT(executionContext->isContextThread()); |
| 48 return adoptPtr(new CrossThreadHolder(executionContext, obj)); |
| 49 } |
| 50 |
| 51 // Can be called from any thread. |
| 52 // Executes |task| with |obj| and |executionContext| on the thread of |
| 53 // |executionContext|. |
| 54 // NOTE: |task| might be silently ignored (i.e. not executed) and |
| 55 // destructed (possibly on the calling thread or on the thread of |
| 56 // |executionContext|) when |executionContext| is stopped or |
| 57 // CrossThreadHolder is destructed. |
| 58 void postTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task) |
| 59 { |
| 60 MutexLocker locker(m_mutex->mutex()); |
| 61 if (!m_bridge) { |
| 62 // The bridge has already disappeared. |
| 63 return; |
| 64 } |
| 65 m_bridge->executionContext()->postTask(FROM_HERE, createCrossThreadTask(
&Bridge::runTask, m_bridge.get(), task)); |
| 66 } |
| 67 |
| 68 ~CrossThreadHolder() |
| 69 { |
| 70 MutexLocker locker(m_mutex->mutex()); |
| 71 clearInternal(); |
| 72 } |
| 73 |
| 74 private: |
| 75 // Object graph: |
| 76 // +------+ +-----------------+ |
| 77 // T <-OwnPtr- |Bridge| ---------RawPtr--------> |CrossThreadHolder| |
| 78 // | | <-CrossThreadPersistent- | | |
| 79 // +------+ +-----------------+ |
| 80 // | | |
| 81 // +--RefPtr--> MutexWrapper <--RefPtr--+ |
| 82 // The CrossThreadPersistent/RawPtr between CrossThreadHolder and Bridge |
| 83 // are protected by MutexWrapper |
| 84 // and cleared when CrossThreadHolder::clearInternal() is called, i.e.: |
| 85 // [1] when |executionContext| is stopped, or |
| 86 // [2] when CrossThreadHolder is destructed. |
| 87 // Then Bridge is shortly garbage collected and T is destructed. |
| 88 |
| 89 class MutexWrapper : public ThreadSafeRefCounted<MutexWrapper> { |
| 90 public: |
| 91 static PassRefPtr<MutexWrapper> create() { return adoptRef(new MutexWrap
per()); } |
| 92 Mutex& mutex() { return m_mutex; } |
| 93 private: |
| 94 MutexWrapper() = default; |
| 95 Mutex m_mutex; |
| 96 }; |
| 97 |
| 98 // All methods except for clearInternal() |
| 99 // must be called on |executionContext|'s thread. |
| 100 class Bridge |
| 101 : public GarbageCollectedFinalized<Bridge> |
| 102 , public ActiveDOMObject { |
| 103 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(Bridge); |
| 104 public: |
| 105 Bridge(ExecutionContext* executionContext, PassOwnPtr<T> obj, PassRefPtr
<MutexWrapper> mutex, CrossThreadHolder* holder) |
| 106 : ActiveDOMObject(executionContext) |
| 107 , m_obj(obj) |
| 108 , m_mutex(mutex) |
| 109 , m_holder(holder) |
| 110 { |
| 111 suspendIfNeeded(); |
| 112 } |
| 113 |
| 114 DEFINE_INLINE_TRACE() |
| 115 { |
| 116 ActiveDOMObject::trace(visitor); |
| 117 } |
| 118 |
| 119 T* getObject() const { return m_obj.get(); } |
| 120 |
| 121 // Must be protected by |m_mutex|. |
| 122 // Is called from CrossThreadHolder::clearInternal() and |
| 123 // can be called on any thread. |
| 124 void clearInternal() |
| 125 { |
| 126 // We don't clear |m_obj| here to destruct |m_obj| on the thread |
| 127 // of |executionContext|. |
| 128 m_holder = nullptr; |
| 129 } |
| 130 |
| 131 void runTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task
) |
| 132 { |
| 133 ASSERT(executionContext()->isContextThread()); |
| 134 if (m_obj) |
| 135 (*task)(m_obj.get(), executionContext()); |
| 136 } |
| 137 |
| 138 private: |
| 139 // ActiveDOMObject |
| 140 void stop() override |
| 141 { |
| 142 ASSERT(executionContext()->isContextThread()); |
| 143 { |
| 144 MutexLocker locker(m_mutex->mutex()); |
| 145 if (m_holder) |
| 146 m_holder->clearInternal(); |
| 147 ASSERT(!m_holder); |
| 148 } |
| 149 |
| 150 // We have to destruct |*m_obj| here because destructing |*m_obj| |
| 151 // in ~Bridge() might be too late when |executionContext| is |
| 152 // stopped. |
| 153 m_obj.clear(); |
| 154 } |
| 155 |
| 156 |
| 157 OwnPtr<T> m_obj; |
| 158 // All accesses to |m_holder| must be protected by |m_mutex|. |
| 159 RefPtr<MutexWrapper> m_mutex; |
| 160 CrossThreadHolder* m_holder; |
| 161 }; |
| 162 |
| 163 // Must be protected by |m_mutex|. |
| 164 void clearInternal() |
| 165 { |
| 166 if (m_bridge) |
| 167 m_bridge->clearInternal(); |
| 168 m_bridge.clear(); |
| 169 } |
| 170 |
| 171 CrossThreadHolder(ExecutionContext* executionContext, PassOwnPtr<T> obj) |
| 172 : m_mutex(MutexWrapper::create()) |
| 173 , m_bridge(new Bridge(executionContext, obj, m_mutex, this)) |
| 174 { |
| 175 } |
| 176 |
| 177 RefPtr<MutexWrapper> m_mutex; |
| 178 // |m_bridge| is protected by |m_mutex|. |
| 179 // |m_bridge| is cleared before the thread that allocated |*m_bridge| |
| 180 // is stopped. |
| 181 CrossThreadPersistent<Bridge> m_bridge; |
| 182 }; |
| 183 |
| 184 // Object graph: |
| 185 // +-------------+ +-------------+ |
| 186 // |HandleWrapper|<-----------------------------------------|ReaderContext| |
| 187 // | | +-------------+ +-----------+ +---+ | | |
| 188 // | |<-|LoaderContext|<-|CTH::Bridge|<->|CTH|<-| | |
| 189 // +-------------+ +-------------+ +-----------+ +---+ +-------------+ |
| 190 // | |
| 191 // ThreadableLoader <--+ |
| 192 // |
| 193 // When the loader thread is stopped, CrossThreadHolder::Bridge and |
| 194 // LoaderContext (and thus ThreadableLoader) is destructed: |
| 195 // +-------------+ +-------------+ |
| 196 // |HandleWrapper|<-----------------------------------------|ReaderContext| |
| 197 // | | +---+ | | |
| 198 // | | |CTH|<-| | |
| 199 // +-------------+ +---+ +-------------+ |
| 200 // and the rest will be destructed when ReaderContext is destructed. |
| 201 // |
| 202 // When ReaderContext is destructed, CrossThreadHolder is destructed: |
| 203 // +-------------+ |
| 204 // |HandleWrapper| |
| 205 // | | +-------------+ +-----------+ |
| 206 // | |<-|LoaderContext|<-|CTH::Bridge| |
| 207 // +-------------+ +-------------+ +-----------+ |
| 208 // | |
| 209 // ThreadableLoader <--+ |
| 210 // and the rest will be shortly destructed when CrossThreadHolder::Bridge |
| 211 // is garbage collected. |
| 212 |
| 213 // LoaderContext is created and destructed on the same thread |
| 214 // (call this thread loader thread). |
| 215 // All methods must be called on the loader thread. |
| 216 class LoaderContext { |
| 217 public: |
| 218 virtual ~LoaderContext() { } |
| 219 virtual void start(ExecutionContext*) = 0; |
| 220 }; |
| 221 |
| 222 class HandleWrapper : public ThreadSafeRefCounted<HandleWrapper> { |
| 223 public: |
| 224 static PassRefPtr<HandleWrapper> create() { return adoptRef(new HandleWrappe
r()); } |
| 225 CompositeDataConsumerHandle* handle() { return m_handle.get(); } |
| 226 private: |
| 227 HandleWrapper() |
| 228 : m_handle(CompositeDataConsumerHandle::create(createWaitingDataConsumer
Handle())) { } |
| 229 |
| 230 OwnPtr<CompositeDataConsumerHandle> m_handle; |
| 231 }; |
| 232 |
| 233 // All methods must be called on the loader thread. |
| 234 class BlobLoaderContext final |
| 235 : public LoaderContext |
| 236 , public ThreadableLoaderClient { |
| 237 public: |
| 238 BlobLoaderContext(PassRefPtr<HandleWrapper> handleWrapper, PassRefPtr<BlobDa
taHandle> blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFact
ory) |
| 239 : m_handleWrapper(handleWrapper) |
| 240 , m_blobDataHandle(blobDataHandle) |
| 241 , m_loaderFactory(loaderFactory) |
| 242 , m_receivedResponse(false) { } |
| 243 |
| 244 ~BlobLoaderContext() override |
| 245 { |
| 246 if (m_loader && !m_receivedResponse) |
| 247 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH
andle()); |
| 248 if (m_loader) { |
| 249 m_loader->cancel(); |
| 250 m_loader.clear(); |
| 251 } |
| 252 } |
| 253 |
| 254 void start(ExecutionContext* executionContext) override |
| 255 { |
| 256 ASSERT(executionContext->isContextThread()); |
| 257 ASSERT(!m_loader); |
| 258 |
| 259 m_loader = createLoader(executionContext, this); |
| 260 if (!m_loader) |
| 261 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH
andle()); |
| 262 } |
| 263 |
| 264 private: |
| 265 PassRefPtr<ThreadableLoader> createLoader(ExecutionContext* executionContext
, ThreadableLoaderClient* client) const |
| 266 { |
| 267 KURL url = BlobURL::createPublicURL(executionContext->securityOrigin()); |
| 268 if (url.isEmpty()) { |
| 269 return nullptr; |
| 270 } |
| 271 BlobRegistry::registerPublicBlobURL(executionContext->securityOrigin(),
url, m_blobDataHandle); |
| 272 |
| 273 ResourceRequest request(url); |
| 274 request.setRequestContext(WebURLRequest::RequestContextInternal); |
| 275 request.setUseStreamOnResponse(true); |
| 276 |
| 277 ThreadableLoaderOptions options; |
| 278 options.preflightPolicy = ConsiderPreflight; |
| 279 options.crossOriginRequestPolicy = DenyCrossOriginRequests; |
| 280 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPo
licy; |
| 281 options.initiator = FetchInitiatorTypeNames::internal; |
| 282 |
| 283 ResourceLoaderOptions resourceLoaderOptions; |
| 284 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; |
| 285 |
| 286 return m_loaderFactory->create(*executionContext, client, request, optio
ns, resourceLoaderOptions); |
| 287 } |
| 288 |
| 289 // ThreadableLoaderClient |
| 290 void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<W
ebDataConsumerHandle> handle) override |
| 291 { |
| 292 ASSERT(!m_receivedResponse); |
| 293 m_receivedResponse = true; |
| 294 if (!handle) { |
| 295 // Here we assume WebURLLoader must return the response body as |
| 296 // |WebDataConsumerHandle| since we call |
| 297 // request.setUseStreamOnResponse(). |
| 298 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH
andle()); |
| 299 return; |
| 300 } |
| 301 m_handleWrapper->handle()->update(handle); |
| 302 } |
| 303 |
| 304 void didFinishLoading(unsigned long, double) override |
| 305 { |
| 306 m_loader.clear(); |
| 307 } |
| 308 |
| 309 void didFail(const ResourceError&) override |
| 310 { |
| 311 if (!m_receivedResponse) |
| 312 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH
andle()); |
| 313 m_loader.clear(); |
| 314 } |
| 315 |
| 316 void didFailRedirectCheck() override |
| 317 { |
| 318 // We don't expect redirects for Blob loading. |
| 319 ASSERT_NOT_REACHED(); |
| 320 } |
| 321 |
| 322 RefPtr<HandleWrapper> m_handleWrapper; |
| 323 |
| 324 RefPtr<BlobDataHandle> m_blobDataHandle; |
| 325 Persistent<FetchBlobDataConsumerHandle::LoaderFactory> m_loaderFactory; |
| 326 RefPtr<ThreadableLoader> m_loader; |
| 327 |
| 328 bool m_receivedResponse; |
| 329 }; |
| 330 |
| 331 class DefaultLoaderFactory final : public FetchBlobDataConsumerHandle::LoaderFac
tory { |
| 332 public: |
| 333 PassRefPtr<ThreadableLoader> create( |
| 334 ExecutionContext& executionContext, |
| 335 ThreadableLoaderClient* client, |
| 336 const ResourceRequest& request, |
| 337 const ThreadableLoaderOptions& options, |
| 338 const ResourceLoaderOptions& resourceLoaderOptions) override |
| 339 { |
| 340 return ThreadableLoader::create(executionContext, client, request, optio
ns, resourceLoaderOptions); |
| 341 } |
| 342 }; |
| 343 |
| 344 } // namespace |
| 345 |
| 346 // ReaderContext is referenced from FetchBlobDataConsumerHandle and |
| 347 // ReaderContext::ReaderImpl. |
| 348 // All functions/members must be called/accessed only on the reader thread, |
| 349 // except for constructor, destructor, and obtainReader(). |
| 350 class FetchBlobDataConsumerHandle::ReaderContext final : public ThreadSafeRefCou
nted<ReaderContext> { |
| 351 public: |
| 352 class ReaderImpl : public FetchDataConsumerHandle::Reader { |
| 353 public: |
| 354 ReaderImpl(Client* client, PassRefPtr<ReaderContext> readerContext, Pass
OwnPtr<WebDataConsumerHandle::Reader> reader) |
| 355 : m_readerContext(readerContext) |
| 356 , m_reader(reader) |
| 357 , m_notifier(client) { } |
| 358 ~ReaderImpl() override { } |
| 359 |
| 360 Result read(void* data, size_t size, Flags flags, size_t* readSize) over
ride |
| 361 { |
| 362 if (m_readerContext->drained()) |
| 363 return Done; |
| 364 m_readerContext->ensureStartLoader(); |
| 365 Result r = m_reader->read(data, size, flags, readSize); |
| 366 if (r != ShouldWait && !(r == Ok && *readSize == 0)) { |
| 367 // We read non-empty data, so we cannot use the blob data |
| 368 // handle which represents the whole data. |
| 369 m_readerContext->clearBlobDataHandleForDrain(); |
| 370 } |
| 371 return r; |
| 372 } |
| 373 |
| 374 Result beginRead(const void** buffer, Flags flags, size_t* available) ov
erride |
| 375 { |
| 376 if (m_readerContext->drained()) |
| 377 return Done; |
| 378 m_readerContext->ensureStartLoader(); |
| 379 Result r = m_reader->beginRead(buffer, flags, available); |
| 380 if (r != ShouldWait && !(r == Ok && *available == 0)) { |
| 381 // We read non-empty data, so we cannot use the blob data |
| 382 // handle which represents the whole data. |
| 383 m_readerContext->clearBlobDataHandleForDrain(); |
| 384 } |
| 385 return r; |
| 386 } |
| 387 |
| 388 Result endRead(size_t readSize) override |
| 389 { |
| 390 return m_reader->endRead(readSize); |
| 391 } |
| 392 |
| 393 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy blobSize
Policy) override |
| 394 { |
| 395 if (!m_readerContext->m_blobDataHandleForDrain) |
| 396 return nullptr; |
| 397 if (blobSizePolicy == DisallowBlobWithInvalidSize && m_readerContext
->m_blobDataHandleForDrain->size() == kuint64max) |
| 398 return nullptr; |
| 399 RefPtr<BlobDataHandle> blobDataHandle = m_readerContext->m_blobDataH
andleForDrain; |
| 400 m_readerContext->setDrained(); |
| 401 m_readerContext->clearBlobDataHandleForDrain(); |
| 402 return blobDataHandle.release(); |
| 403 } |
| 404 |
| 405 private: |
| 406 RefPtr<ReaderContext> m_readerContext; |
| 407 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| 408 NotifyOnReaderCreationHelper m_notifier; |
| 409 }; |
| 410 |
| 411 ReaderContext(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle>
blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) |
| 412 : m_handleWrapper(HandleWrapper::create()) |
| 413 , m_blobDataHandleForDrain(blobDataHandle) |
| 414 , m_loaderContextHolder(CrossThreadHolder<LoaderContext>::create(executi
onContext, adoptPtr(new BlobLoaderContext(m_handleWrapper, m_blobDataHandleForDr
ain, loaderFactory)))) |
| 415 , m_loaderStarted(false) |
| 416 , m_drained(false) |
| 417 { |
| 418 } |
| 419 |
| 420 PassOwnPtr<FetchDataConsumerHandle::Reader> obtainReader(WebDataConsumerHand
le::Client* client) |
| 421 { |
| 422 return adoptPtr(new ReaderImpl(client, this, m_handleWrapper->handle()->
obtainReader(client))); |
| 423 } |
| 424 |
| 425 private: |
| 426 void ensureStartLoader() |
| 427 { |
| 428 if (m_loaderStarted) |
| 429 return; |
| 430 m_loaderStarted = true; |
| 431 m_loaderContextHolder->postTask(threadSafeBind<LoaderContext*, Execution
Context*>(&LoaderContext::start)); |
| 432 } |
| 433 |
| 434 void clearBlobDataHandleForDrain() |
| 435 { |
| 436 m_blobDataHandleForDrain.clear(); |
| 437 } |
| 438 |
| 439 bool drained() const { return m_drained; } |
| 440 void setDrained() { m_drained = true; } |
| 441 |
| 442 RefPtr<HandleWrapper> m_handleWrapper; |
| 443 RefPtr<BlobDataHandle> m_blobDataHandleForDrain; |
| 444 OwnPtr<CrossThreadHolder<LoaderContext>> m_loaderContextHolder; |
| 445 |
| 446 bool m_loaderStarted; |
| 447 bool m_drained; |
| 448 }; |
| 449 |
| 450 FetchBlobDataConsumerHandle::FetchBlobDataConsumerHandle(ExecutionContext* execu
tionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFactory* loaderFac
tory) |
| 451 : m_readerContext(adoptRef(new ReaderContext(executionContext, blobDataHandl
e, loaderFactory))) |
| 452 { |
| 453 } |
| 454 |
| 455 FetchBlobDataConsumerHandle::~FetchBlobDataConsumerHandle() |
| 456 { |
| 457 } |
| 458 |
| 459 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio
nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFac
tory* loaderFactory) |
| 460 { |
| 461 if (!blobDataHandle) |
| 462 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); |
| 463 |
| 464 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, loaderFactory)); |
| 465 } |
| 466 |
| 467 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio
nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
| 468 { |
| 469 if (!blobDataHandle) |
| 470 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); |
| 471 |
| 472 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa
ndle, new DefaultLoaderFactory)); |
| 473 } |
| 474 |
| 475 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern
al(Client* client) |
| 476 { |
| 477 return m_readerContext->obtainReader(client).leakPtr(); |
| 478 } |
| 479 |
| 480 } // namespace blink |
OLD | NEW |