Chromium Code Reviews| 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 // bounded to the thread of |executionContext| where |obj| is created. | |
|
yhirano
2015/07/01 11:27:50
bound to
hiroshige
2015/07/02 08:24:54
Done.
| |
| 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(); | |
|
yhirano
2015/07/01 11:27:50
ASSERT(!m_holder);
hiroshige
2015/07/02 08:24:55
Done.
| |
| 147 } | |
| 148 | |
| 149 // We have to destruct |*m_obj| here because destructing |*m_obj| | |
| 150 // in ~Bridge() might be too late when |executionContext| is | |
| 151 // stopped. | |
| 152 m_obj.clear(); | |
| 153 } | |
| 154 | |
| 155 | |
| 156 OwnPtr<T> m_obj; | |
| 157 // All accesses to |m_holder| must be protected by |m_mutex|. | |
| 158 RefPtr<MutexWrapper> m_mutex; | |
| 159 CrossThreadHolder* m_holder; | |
| 160 }; | |
| 161 | |
| 162 // Must be protected by |m_mutex|. | |
| 163 void clearInternal() | |
| 164 { | |
| 165 if (m_bridge) | |
| 166 m_bridge->clearInternal(); | |
| 167 m_bridge.clear(); | |
| 168 } | |
| 169 | |
| 170 CrossThreadHolder(ExecutionContext* executionContext, PassOwnPtr<T> obj) | |
| 171 : m_mutex(MutexWrapper::create()) | |
| 172 , m_bridge(new Bridge(executionContext, obj, m_mutex, this)) | |
| 173 { | |
| 174 } | |
| 175 | |
| 176 RefPtr<MutexWrapper> m_mutex; | |
| 177 // |m_bridge| is protected by |m_mutex|. | |
| 178 // |m_bridge| is cleared before the thread that allocated |*m_bridge| | |
| 179 // is stopped. | |
| 180 CrossThreadPersistent<Bridge> m_bridge; | |
| 181 }; | |
| 182 | |
| 183 // Object graph: | |
| 184 // +-------------+ +-------------+ | |
| 185 // |HandleWrapper|<-----------------------------------------|ReaderContext| | |
| 186 // | | +-------------+ +-----------+ +---+ | | | |
| 187 // | |<-|LoaderContext|<-|CTH::Bridge|<->|CTH|<-| | | |
| 188 // +-------------+ +-------------+ +-----------+ +---+ +-------------+ | |
| 189 // | | |
| 190 // ThreadableLoader <--+ | |
| 191 // | |
| 192 // When the loader thread is stopped, CrossThreadHolder::Bridge and | |
| 193 // LoaderContext (and thus ThreadableLoader) is destructed: | |
| 194 // +-------------+ +-------------+ | |
| 195 // |HandleWrapper|<-----------------------------------------|ReaderContext| | |
| 196 // | | +---+ | | | |
| 197 // | | |CTH|<-| | | |
| 198 // +-------------+ +---+ +-------------+ | |
| 199 // and the rest will be destructed when ReaderContext is destructed. | |
| 200 // | |
| 201 // When ReaderContext is destructed, CrossThreadHolder is destructed: | |
| 202 // +-------------+ | |
| 203 // |HandleWrapper| | |
| 204 // | | +-------------+ +-----------+ | |
| 205 // | |<-|LoaderContext|<-|CTH::Bridge| | |
| 206 // +-------------+ +-------------+ +-----------+ | |
| 207 // | | |
| 208 // ThreadableLoader <--+ | |
| 209 // and the rest will be shortly destructed when CrossThreadHolder::Bridge | |
| 210 // is garbage collected. | |
| 211 | |
| 212 // LoaderContext is created and destructed on the same thread | |
| 213 // (call this thread loader thread). | |
| 214 // All methods must be called on the loader thread. | |
| 215 class LoaderContext { | |
| 216 public: | |
| 217 virtual ~LoaderContext() { } | |
| 218 virtual void start(ExecutionContext*) = 0; | |
| 219 }; | |
| 220 | |
| 221 class HandleWrapper : public ThreadSafeRefCounted<HandleWrapper> { | |
| 222 public: | |
| 223 static PassRefPtr<HandleWrapper> create() { return adoptRef(new HandleWrappe r()); } | |
| 224 CompositeDataConsumerHandle* handle() { return m_handle.get(); } | |
| 225 private: | |
| 226 HandleWrapper() | |
| 227 : m_handle(CompositeDataConsumerHandle::create(createWaitingDataConsumer Handle())) { } | |
| 228 | |
| 229 OwnPtr<CompositeDataConsumerHandle> m_handle; | |
| 230 }; | |
| 231 | |
| 232 // All methods must be called on the loader thread. | |
| 233 class BlobLoaderContext final | |
| 234 : public LoaderContext | |
| 235 , public ThreadableLoaderClient { | |
| 236 public: | |
| 237 BlobLoaderContext(PassRefPtr<HandleWrapper> handleWrapper, PassRefPtr<BlobDa taHandle> blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFact ory) | |
| 238 : m_handleWrapper(handleWrapper) | |
| 239 , m_blobDataHandle(blobDataHandle) | |
| 240 , m_loaderFactory(loaderFactory) | |
| 241 , m_receivedResponse(false) { } | |
| 242 | |
| 243 ~BlobLoaderContext() override | |
| 244 { | |
| 245 if (m_loader && !m_receivedResponse) | |
| 246 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH andle()); | |
| 247 if (m_loader) { | |
| 248 m_loader->cancel(); | |
| 249 m_loader.clear(); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 void start(ExecutionContext* executionContext) override | |
| 254 { | |
| 255 ASSERT(executionContext->isContextThread()); | |
| 256 ASSERT(!m_loader); | |
| 257 | |
| 258 m_loader = createLoader(executionContext, this); | |
| 259 if (!m_loader) | |
| 260 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH andle()); | |
| 261 } | |
| 262 | |
| 263 private: | |
| 264 PassRefPtr<ThreadableLoader> createLoader(ExecutionContext* executionContext , ThreadableLoaderClient* client) const | |
| 265 { | |
| 266 KURL url = BlobURL::createPublicURL(executionContext->securityOrigin()); | |
| 267 if (url.isEmpty()) { | |
| 268 return nullptr; | |
| 269 } | |
| 270 BlobRegistry::registerPublicBlobURL(executionContext->securityOrigin(), url, m_blobDataHandle); | |
| 271 | |
| 272 ResourceRequest request(url); | |
| 273 request.setRequestContext(WebURLRequest::RequestContextInternal); | |
| 274 request.setUseStreamOnResponse(true); | |
| 275 | |
| 276 ThreadableLoaderOptions options; | |
| 277 options.preflightPolicy = ConsiderPreflight; | |
| 278 options.crossOriginRequestPolicy = DenyCrossOriginRequests; | |
| 279 options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPo licy; | |
| 280 options.initiator = FetchInitiatorTypeNames::internal; | |
| 281 | |
| 282 ResourceLoaderOptions resourceLoaderOptions; | |
| 283 resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; | |
| 284 | |
| 285 return m_loaderFactory->create(*executionContext, client, request, optio ns, resourceLoaderOptions); | |
| 286 } | |
| 287 | |
| 288 // ThreadableLoaderClient | |
| 289 void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<W ebDataConsumerHandle> handle) override | |
| 290 { | |
| 291 ASSERT(!m_receivedResponse); | |
| 292 m_receivedResponse = true; | |
| 293 if (!handle) { | |
| 294 // Here we assume WebURLLoader must return the response body as | |
| 295 // |WebDataConsumerHandle| since we call | |
| 296 // request.setUseStreamOnResponse(). | |
| 297 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH andle()); | |
| 298 return; | |
| 299 } | |
| 300 m_handleWrapper->handle()->update(handle); | |
| 301 } | |
| 302 | |
| 303 void didFinishLoading(unsigned long, double) override | |
| 304 { | |
| 305 m_loader.clear(); | |
| 306 } | |
| 307 | |
| 308 void didFail(const ResourceError&) override | |
| 309 { | |
| 310 if (!m_receivedResponse) | |
| 311 m_handleWrapper->handle()->update(createUnexpectedErrorDataConsumerH andle()); | |
| 312 m_loader.clear(); | |
| 313 } | |
| 314 | |
| 315 void didFailRedirectCheck() override | |
| 316 { | |
| 317 // We don't expect redirects for Blob loading. | |
| 318 ASSERT_NOT_REACHED(); | |
| 319 } | |
| 320 | |
| 321 RefPtr<HandleWrapper> m_handleWrapper; | |
| 322 | |
| 323 RefPtr<BlobDataHandle> m_blobDataHandle; | |
| 324 Persistent<FetchBlobDataConsumerHandle::LoaderFactory> m_loaderFactory; | |
| 325 RefPtr<ThreadableLoader> m_loader; | |
| 326 | |
| 327 bool m_receivedResponse; | |
| 328 }; | |
| 329 | |
| 330 class DefaultLoaderFactory final : public FetchBlobDataConsumerHandle::LoaderFac tory { | |
| 331 public: | |
| 332 PassRefPtr<ThreadableLoader> create( | |
| 333 ExecutionContext& executionContext, | |
| 334 ThreadableLoaderClient* client, | |
| 335 const ResourceRequest& request, | |
| 336 const ThreadableLoaderOptions& options, | |
| 337 const ResourceLoaderOptions& resourceLoaderOptions) override | |
| 338 { | |
| 339 return ThreadableLoader::create(executionContext, client, request, optio ns, resourceLoaderOptions); | |
| 340 } | |
| 341 }; | |
| 342 | |
| 343 } // namespace | |
| 344 | |
| 345 // ReaderContext is referenced from FetchBlobDataConsumerHandle and | |
| 346 // ReaderContext::ReaderImpl. | |
| 347 // All functions/members must be called/accessed only on the reader thread, | |
| 348 // except for constructor, destructor, and obtainReader(). | |
| 349 class FetchBlobDataConsumerHandle::ReaderContext final : public ThreadSafeRefCou nted<ReaderContext> { | |
| 350 public: | |
| 351 class ReaderImpl : public FetchDataConsumerHandle::Reader { | |
| 352 public: | |
| 353 ReaderImpl(Client* client, PassRefPtr<ReaderContext> readerContext, Pass OwnPtr<WebDataConsumerHandle::Reader> reader) | |
| 354 : m_readerContext(readerContext) | |
| 355 , m_reader(reader) | |
| 356 , m_notifier(client) { } | |
| 357 ~ReaderImpl() override { } | |
| 358 | |
| 359 Result read(void* data, size_t size, Flags flags, size_t* readSize) over ride | |
| 360 { | |
| 361 if (m_readerContext->drained()) | |
| 362 return Done; | |
| 363 m_readerContext->ensureStartLoader(); | |
| 364 Result r = m_reader->read(data, size, flags, readSize); | |
| 365 if (r != ShouldWait) { | |
| 366 // We read non-empty data, so we cannot use the blob data | |
| 367 // handle which represents the whole data. | |
| 368 m_readerContext->clearBlobDataHandleForDrain(); | |
| 369 } | |
| 370 return r; | |
| 371 } | |
| 372 | |
| 373 Result beginRead(const void** buffer, Flags flags, size_t* available) ov erride | |
| 374 { | |
| 375 if (m_readerContext->drained()) | |
| 376 return Done; | |
| 377 m_readerContext->ensureStartLoader(); | |
| 378 Result r = m_reader->beginRead(buffer, flags, available); | |
| 379 if (r != ShouldWait) { | |
| 380 // We read non-empty data, so we cannot use the blob data | |
| 381 // handle which represents the whole data. | |
| 382 m_readerContext->clearBlobDataHandleForDrain(); | |
| 383 } | |
| 384 return r; | |
| 385 } | |
| 386 | |
| 387 Result endRead(size_t readSize) override | |
| 388 { | |
| 389 return m_reader->endRead(readSize); | |
| 390 } | |
| 391 | |
| 392 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy blobSize Policy) override | |
| 393 { | |
| 394 if (!m_readerContext->m_blobDataHandleForDrain) | |
| 395 return nullptr; | |
| 396 if (blobSizePolicy == DisallowBlobWithInvalidSize && m_readerContext ->m_blobDataHandleForDrain->size() == kuint64max) | |
| 397 return nullptr; | |
| 398 RefPtr<BlobDataHandle> blobDataHandle = m_readerContext->m_blobDataH andleForDrain; | |
| 399 m_readerContext->setDrained(); | |
| 400 m_readerContext->clearBlobDataHandleForDrain(); | |
| 401 return blobDataHandle.release(); | |
| 402 } | |
| 403 | |
| 404 private: | |
| 405 RefPtr<ReaderContext> m_readerContext; | |
| 406 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | |
| 407 NotifyOnReaderCreationHelper m_notifier; | |
| 408 }; | |
| 409 | |
| 410 ReaderContext(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle, FetchBlobDataConsumerHandle::LoaderFactory* loaderFactory) | |
| 411 : m_handleWrapper(HandleWrapper::create()) | |
| 412 , m_blobDataHandleForDrain(blobDataHandle) | |
| 413 , m_loaderContextHolder(CrossThreadHolder<LoaderContext>::create(executi onContext, adoptPtr(new BlobLoaderContext(m_handleWrapper, m_blobDataHandleForDr ain, loaderFactory)))) | |
| 414 , m_loaderStarted(false) | |
| 415 , m_drained(false) | |
| 416 { | |
| 417 } | |
| 418 | |
| 419 PassOwnPtr<FetchDataConsumerHandle::Reader> obtainReader(WebDataConsumerHand le::Client* client) | |
| 420 { | |
| 421 return adoptPtr(new ReaderImpl(client, this, m_handleWrapper->handle()-> obtainReader(client))); | |
| 422 } | |
| 423 | |
| 424 private: | |
| 425 void ensureStartLoader() | |
| 426 { | |
| 427 if (m_loaderStarted) | |
| 428 return; | |
| 429 m_loaderStarted = true; | |
| 430 m_loaderContextHolder->postTask(threadSafeBind<LoaderContext*, Execution Context*>(&LoaderContext::start)); | |
| 431 } | |
| 432 | |
| 433 void clearBlobDataHandleForDrain() | |
| 434 { | |
| 435 m_blobDataHandleForDrain.clear(); | |
| 436 } | |
| 437 | |
| 438 bool drained() const { return m_drained; } | |
| 439 void setDrained() { m_drained = true; } | |
| 440 | |
| 441 RefPtr<HandleWrapper> m_handleWrapper; | |
| 442 RefPtr<BlobDataHandle> m_blobDataHandleForDrain; | |
| 443 OwnPtr<CrossThreadHolder<LoaderContext>> m_loaderContextHolder; | |
| 444 | |
| 445 bool m_loaderStarted; | |
| 446 bool m_drained; | |
| 447 }; | |
| 448 | |
| 449 FetchBlobDataConsumerHandle::FetchBlobDataConsumerHandle(ExecutionContext* execu tionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFactory* loaderFac tory) | |
| 450 : m_readerContext(adoptRef(new ReaderContext(executionContext, blobDataHandl e, loaderFactory))) | |
| 451 { | |
| 452 } | |
| 453 | |
| 454 FetchBlobDataConsumerHandle::~FetchBlobDataConsumerHandle() | |
| 455 { | |
| 456 } | |
| 457 | |
| 458 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle, LoaderFac tory* loaderFactory) | |
| 459 { | |
| 460 if (!blobDataHandle) | |
| 461 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer Handle()); | |
| 462 | |
| 463 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa ndle, loaderFactory)); | |
| 464 } | |
| 465 | |
| 466 PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(Executio nContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) | |
| 467 { | |
| 468 if (!blobDataHandle) | |
| 469 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer Handle()); | |
| 470 | |
| 471 return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHa ndle, new DefaultLoaderFactory)); | |
| 472 } | |
| 473 | |
| 474 FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderIntern al(Client* client) | |
| 475 { | |
| 476 return m_readerContext->obtainReader(client).leakPtr(); | |
| 477 } | |
| 478 | |
| 479 } // namespace blink | |
| OLD | NEW |