Chromium Code Reviews| Index: Source/modules/fetch/FetchBlobDataConsumerHandle.cpp |
| diff --git a/Source/modules/fetch/FetchBlobDataConsumerHandle.cpp b/Source/modules/fetch/FetchBlobDataConsumerHandle.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a8bd22117c62610ecd87976f1de5993147263b76 |
| --- /dev/null |
| +++ b/Source/modules/fetch/FetchBlobDataConsumerHandle.cpp |
| @@ -0,0 +1,458 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "modules/fetch/FetchBlobDataConsumerHandle.h" |
| + |
| +#include "core/dom/ActiveDOMObject.h" |
| +#include "core/dom/CrossThreadTask.h" |
| +#include "core/dom/ExecutionContext.h" |
| +#include "core/fetch/FetchInitiatorTypeNames.h" |
| +#include "core/loader/ThreadableLoaderClient.h" |
| +#include "modules/fetch/CompositeDataConsumerHandle.h" |
| +#include "modules/fetch/DataConsumerHandleUtil.h" |
| +#include "platform/Task.h" |
| +#include "platform/blob/BlobRegistry.h" |
| +#include "platform/blob/BlobURL.h" |
| +#include "platform/network/ResourceRequest.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebTraceLocation.h" |
| +#include "wtf/Locker.h" |
| +#include "wtf/ThreadingPrimitives.h" |
| + |
| +namespace blink { |
| + |
| +using Result = FetchBlobDataConsumerHandle::Result; |
| + |
| +namespace { |
| + |
| +// CrossThreadHolder<T> provides cross-thread access to |obj| of class T. |
| +// CrossThreadHolder<T> can be passed across threads while |obj|'s methods |
| +// are called on the fixed thread of |executionContext| via |
| +// CrossThreadHolder<T>::postTask(). |
| +// |obj| is destructed when |executionContext| is stopped or |
| +// CrossThreadHolder is destructed (earlier of them). |
| +template<typename T> |
| +class CrossThreadHolder { |
| +public: |
| + static PassOwnPtr<CrossThreadHolder<T>> create(ExecutionContext* executionContext, PassOwnPtr<T> obj) |
| + { |
| + return adoptPtr(new CrossThreadHolder(executionContext, obj)); |
| + } |
| + |
| + // Can be called from any thread. |
| + // Executes |task| with |obj| and |executionContext| on the thread of |
| + // |executionContext|. |
| + // NOTE: |task| might be silently ignored (i.e. not executed) when |
| + // |executionContext| is stopped. |
|
yhirano
2015/06/23 07:32:05
It would be worth noting that |task| will be destr
hiroshige
2015/06/23 08:34:22
Done.
|
| + void postTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task) |
| + { |
| + m_peer->postTask(task); |
| + } |
| + |
| + ~CrossThreadHolder() |
| + { |
| + m_peer->clear(); |
| + } |
| + |
| +private: |
| + CrossThreadHolder(ExecutionContext* executionContext, PassOwnPtr<T> obj) |
| + : m_peer(adoptRef(new Peer())) |
| + { |
| + m_peer->initialize(new Bridge(executionContext, m_peer, obj)); |
| + } |
| + |
| + // Bridge and Peer have a reference cycle: |
| + // T <-OwnPtr- |Bridge| ---------RefPtr--------> |Peer| <-(RefPtr)- CTH |
| + // | | <-CrossThreadPersistent- | | |
| + // CTH: CrossThreadHolder<T> |
| + // The reference from Peer to Bridge is protected by |Peer::m_mutex| and |
| + // cleared when Peer::clear() is called, i.e.: |
| + // [1] when |executionContext| is stopped: |
| + // T <-OwnPtr- |Bridge| ---------RefPtr--------> |Peer| <-(RefPtr)- CTH |
| + // [2] or when CrossThreadHolder is destructed: |
| + // T <-OwnPtr- |Bridge| ---------RefPtr--------> |Peer| |
| + // in either case, Bridge is shortly garbage collected. |
| + |
| + class Peer; |
| + |
| + // All methods must be called on |executionContext|'s thread. |
| + class Bridge |
| + : public GarbageCollectedFinalized<Bridge> |
| + , public ActiveDOMObject { |
| + WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(Bridge); |
| + public: |
| + Bridge(ExecutionContext* executionContext, PassRefPtr<Peer> peer, PassOwnPtr<T> obj) |
| + : ActiveDOMObject(executionContext) |
| + , m_peer(peer) |
| + , m_obj(obj) |
| + { |
| + suspendIfNeeded(); |
| + } |
| + |
| + DEFINE_INLINE_TRACE() |
| + { |
| + visitor->trace(m_obj); |
| + ActiveDOMObject::trace(visitor); |
| + } |
| + |
| + T* getObject() const { return m_obj.get(); } |
| + |
| + private: |
| + // ActiveDOMObject |
| + void stop() override |
| + { |
| + ASSERT(executionContext()->isContextThread()); |
| + m_obj.clear(); |
| + m_peer->clear(); |
| + } |
| + |
| + RefPtr<Peer> m_peer; |
| + OwnPtr<T> m_obj; |
| + }; |
| + |
| + class Peer : public ThreadSafeRefCounted<Peer> { |
| + public: |
| + // initialize() must be called only just after construction to create a |
| + // reference cycle. |
| + void initialize(Bridge* bridge) |
| + { |
| + m_bridge = bridge; |
| + } |
| + |
| + void clear() |
| + { |
| + MutexLocker locker(m_mutex); |
| + m_bridge.clear(); |
| + } |
| + |
| + void postTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task) |
| + { |
| + MutexLocker locker(m_mutex); |
| + if (!m_bridge) { |
| + // The bridge has already disappeared. |
| + return; |
| + } |
| + m_bridge->executionContext()->postTask(FROM_HERE, createCrossThreadTask(&Peer::runTask, this, task)); |
| + } |
| + |
| + private: |
| + // Must be called on the loader thread. |
| + void runTask(PassOwnPtr<WTF::Function<void(T*, ExecutionContext*)>> task) |
| + { |
| + Bridge* bridge; |
| + { |
| + MutexLocker locker(m_mutex); |
| + bridge = m_bridge.get(); |
| + if (!bridge) { |
| + // LoaderContext has already disappeared. |
| + return; |
| + } |
| + ASSERT(bridge->executionContext()->isContextThread()); |
| + } |
| + |
| + // |bridge| can be set to nullptr by another thread after the |
| + // mutex is unlocked, but we can still use |loaderContext| here because |
|
yhirano
2015/06/23 07:32:05
Please wrap comments in 80 columns. Ditto for belo
hiroshige
2015/06/23 08:34:22
Done.
|
| + // the LoaderContext belongs to the current thread (== the loader thread). |
| + (*task)(bridge->getObject(), bridge->executionContext()); |
| + } |
| + |
| + // |m_brige| is protected by |m_mutex|. |
| + CrossThreadPersistent<Bridge> m_bridge; |
| + Mutex m_mutex; |
| + }; |
| + |
| + RefPtr<Peer> m_peer; |
| +}; |
| + |
| +// LoaderContext and associated ThreadableLoader is created and |
| +// destructed on the same thread (call this thread loader thread). |
| +// All methods must be called on the loader thread. |
| +// CommonContext and LoaderContext have a reference cycle: |
| +// CTH <-----(OwnPtr)----+ |
| +// | | |
| +// (CrossThreadPersistent) | |
| +// v | |
| +// LC ----(RefPtr)----> CC <-- RC |
| +// LC: LoaderContext |
| +// CC: CommonContext |
| +// RC: ReaderContext |
| +// CTH: CrossThreadHolder<LoaderContext> |
| +// This cycle is broken when CommonContext::clearLoaderContext() is called |
| +// (which clears the reference to CrossThreadHolder), |
| +// or ExecutionContext of the loader thread is stopped |
| +// (CrossThreadHolder clears the reference to LoaderContext). |
| + |
| +class LoaderContext { |
|
hiroshige
2015/06/23 06:17:08
I used OwnPtr<LoaderContext> in this Patch Set.
A
yhirano
2015/06/23 07:32:06
I don't have a strong preference at this moment, t
hiroshige
2015/06/23 08:34:22
Fine with me.
|
| +public: |
| + virtual ~LoaderContext() { } |
| + virtual void start(ExecutionContext*) = 0; |
| +}; |
| + |
| +class CommonContext |
| + : public ThreadSafeRefCounted<CommonContext> { |
| +public: |
| + CommonContext() |
| + : m_handle(CompositeDataConsumerHandle::create(createWaitingDataConsumerHandle())) { } |
| + |
| + // initialize() must be called only just after construction to create a |
| + // reference cycle. |
| + void initialize(ExecutionContext* executionContext, PassOwnPtr<LoaderContext> loaderContext) |
| + { |
| + m_loaderContextHolder = CrossThreadHolder<LoaderContext>::create(executionContext, loaderContext); |
| + } |
| + |
| + CompositeDataConsumerHandle* handle() { return m_handle.get(); } |
| + |
| + void startLoader() |
| + { |
| + MutexLocker locker(m_mutex); |
| + if (m_loaderContextHolder) |
| + m_loaderContextHolder->postTask(threadSafeBind<LoaderContext*, ExecutionContext*>(&LoaderContext::start)); |
| + } |
| + |
| + void clearLoaderContext() |
| + { |
| + MutexLocker locker(m_mutex); |
| + m_loaderContextHolder.clear(); |
| + } |
| + |
| + // Must be called on the loader thread. |
| + void error() |
| + { |
| + clearLoaderContext(); |
| + handle()->update(createUnexpectedErrorDataConsumerHandle()); |
| + } |
| + |
| +private: |
| + OwnPtr<CompositeDataConsumerHandle> m_handle; |
| + |
| + // |m_loaderContextHolder| is protected by |m_mutex|. |
| + OwnPtr<CrossThreadHolder<LoaderContext>> m_loaderContextHolder; |
| + Mutex m_mutex; |
| +}; |
| + |
| +// All methods must be called on the loader thread. |
| +// This class is not final for testing. |
| +class BlobLoaderContext |
| + : public LoaderContext |
| + , public ThreadableLoaderClient { |
| + WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED(LoaderContext); |
| +public: |
| + BlobLoaderContext(PassRefPtr<CommonContext> commonContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
| + : m_commonContext(commonContext) |
| + , m_blobDataHandle(blobDataHandle) |
| + , m_receivedResponse(false) { } |
| + |
| + ~BlobLoaderContext() override |
| + { |
| + if (m_loader && !m_receivedResponse) |
| + m_commonContext->handle()->update(createUnexpectedErrorDataConsumerHandle()); |
| + if (m_loader) { |
| + m_loader->cancel(); |
| + m_loader.clear(); |
| + } |
| + } |
| + |
| + void start(ExecutionContext* executionContext) override |
| + { |
| + ASSERT(executionContext->isContextThread()); |
| + ASSERT(!m_loader); |
| + |
| + m_loader = createLoader(executionContext, this); |
| + if (!m_loader) |
| + m_commonContext->error(); |
| + } |
| + |
| +private: |
| + virtual PassRefPtr<ThreadableLoader> createLoader(ExecutionContext* executionContext, ThreadableLoaderClient* client) const |
| + { |
| + KURL url = BlobURL::createPublicURL(executionContext->securityOrigin()); |
| + if (url.isEmpty()) { |
| + return nullptr; |
| + } |
| + BlobRegistry::registerPublicBlobURL(executionContext->securityOrigin(), url, m_blobDataHandle); |
| + |
| + ResourceRequest request(url); |
| + request.setRequestContext(WebURLRequest::RequestContextInternal); |
| + request.setUseStreamOnResponse(true); |
| + |
| + ThreadableLoaderOptions options; |
| + options.preflightPolicy = ConsiderPreflight; |
| + options.crossOriginRequestPolicy = DenyCrossOriginRequests; |
| + options.contentSecurityPolicyEnforcement = DoNotEnforceContentSecurityPolicy; |
| + options.initiator = FetchInitiatorTypeNames::internal; |
| + |
| + ResourceLoaderOptions resourceLoaderOptions; |
| + resourceLoaderOptions.dataBufferingPolicy = DoNotBufferData; |
| + |
| + return ThreadableLoader::create(*executionContext, client, request, options, resourceLoaderOptions); |
| + } |
| + |
| + // ThreadableLoaderClient |
| + void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<WebDataConsumerHandle> handle) override |
| + { |
| + ASSERT(!m_receivedResponse); |
| + m_receivedResponse = true; |
| + if (!handle) { |
| + // Here we assume WebURLLoader must return the response body as |
| + // |WebDataConsumerHandle| since we call |
| + // request.setUseStreamOnResponse(). |
| + m_commonContext->handle()->update(createUnexpectedErrorDataConsumerHandle()); |
| + return; |
| + } |
| + m_commonContext->handle()->update(handle); |
| + } |
| + void didFinishLoading(unsigned long, double) override |
| + { |
| + m_commonContext->clearLoaderContext(); |
| + } |
| + void didFail(const ResourceError&) override |
| + { |
| + if (m_receivedResponse) |
| + m_commonContext->clearLoaderContext(); |
| + else |
| + m_commonContext->error(); |
| + } |
| + void didFailRedirectCheck() override |
| + { |
| + if (m_receivedResponse) |
|
yhirano
2015/06/23 07:32:06
Is just ASSERT(!m_receivedResponse) fine?
Or just
hiroshige
2015/06/23 08:34:22
Done.
|
| + m_commonContext->clearLoaderContext(); |
| + else |
| + m_commonContext->error(); |
| + } |
| + |
| + RefPtr<CommonContext> m_commonContext; |
| + RefPtr<BlobDataHandle> m_blobDataHandle; |
| + RefPtr<ThreadableLoader> m_loader; |
| + |
| + bool m_receivedResponse; |
| +}; |
| + |
| +} // namespace |
| + |
| +// ReaderContext is referenced from FetchBlobDataConsumerHandle and |
| +// FetchDataConsumerHandle::Reader. |
| +class FetchBlobDataConsumerHandle::ReaderContext : public ThreadSafeRefCounted<ReaderContext> { |
|
yhirano
2015/06/23 07:32:06
+final
hiroshige
2015/06/23 08:34:22
Done.
|
| +public: |
| + class ReaderImpl : public FetchDataConsumerHandle::Reader { |
| + public: |
| + ReaderImpl(Client* client, PassRefPtr<ReaderContext> readerContext, PassOwnPtr<WebDataConsumerHandle::Reader> reader) |
| + : m_readerContext(readerContext) |
| + , m_reader(reader) |
| + , m_notifier(client) { } |
| + ~ReaderImpl() override { } |
| + |
| + Result read(void* data, size_t size, Flags flags, size_t* readSize) override |
| + { |
| + if (m_readerContext->drained()) |
| + return Done; |
| + m_readerContext->ensureStartLoader(); |
| + Result r = m_reader->read(data, size, flags, readSize); |
| + if (r != ShouldWait) { |
| + // We read non-empty data, so we cannot use the blob data handle which |
| + // represents the whole data. |
| + m_readerContext->clearBlobDataHandleForDrain(); |
| + } |
| + return r; |
| + } |
| + |
| + Result beginRead(const void** buffer, Flags flags, size_t* available) override |
| + { |
| + if (m_readerContext->drained()) |
| + return Done; |
| + m_readerContext->ensureStartLoader(); |
| + Result r = m_reader->beginRead(buffer, flags, available); |
| + if (r != ShouldWait) { |
| + // We read non-empty data, so we cannot use the blob data handle which |
| + // represents the whole data. |
| + m_readerContext->clearBlobDataHandleForDrain(); |
| + } |
| + return r; |
| + } |
| + |
| + Result endRead(size_t readSize) override |
| + { |
| + return m_reader->endRead(readSize); |
| + } |
| + |
| + PassRefPtr<BlobDataHandle> drainAsBlobDataHandle() override |
| + { |
| + if (!m_readerContext->m_blobDataHandleForDrain || m_readerContext->m_blobDataHandleForDrain->size() == kuint64max) |
| + return nullptr; |
| + RefPtr<BlobDataHandle> blobDataHandle = m_readerContext->m_blobDataHandleForDrain; |
| + m_readerContext->setDrained(); |
| + m_readerContext->clearBlobDataHandleForDrain(); |
| + return blobDataHandle.release(); |
| + } |
| + |
| + private: |
| + RefPtr<ReaderContext> m_readerContext; |
| + OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| + NotifyOnReaderCreationHelper m_notifier; |
|
yhirano
2015/06/23 07:32:05
Is this member used?
hiroshige
2015/06/23 08:34:22
Yes, just having as a member and its initializatio
|
| + }; |
| + |
| + ReaderContext(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
| + : m_commonContext(adoptRef(new CommonContext())) |
| + , m_loaderStarted(false) |
| + , m_drained(false) |
| + , m_blobDataHandleForDrain(blobDataHandle) |
| + { |
| + m_commonContext->initialize(executionContext, adoptPtr(new BlobLoaderContext(m_commonContext, m_blobDataHandleForDrain))); |
| + } |
| + |
| + ~ReaderContext() |
| + { |
| + m_commonContext->clearLoaderContext(); |
| + } |
| + |
| + PassOwnPtr<FetchDataConsumerHandle::Reader> obtainReader(WebDataConsumerHandle::Client* client) |
| + { |
| + return adoptPtr(new ReaderImpl(client, this, m_commonContext->handle()->obtainReader(client))); |
| + } |
| + |
| + void ensureStartLoader() |
| + { |
| + if (m_loaderStarted) |
| + return; |
| + m_loaderStarted = true; |
| + m_commonContext->startLoader(); |
| + } |
| + |
| + void clearBlobDataHandleForDrain() |
| + { |
| + m_blobDataHandleForDrain.clear(); |
| + } |
| + |
| +private: |
| + bool drained() const { return m_drained; } |
| + void setDrained() { m_drained = true; } |
| + |
| + RefPtr<CommonContext> m_commonContext; |
| + |
| + // Following members must be accessed only on the reader thread. |
| + bool m_loaderStarted; |
| + bool m_drained; |
| + RefPtr<BlobDataHandle> m_blobDataHandleForDrain; |
| +}; |
| + |
| +FetchBlobDataConsumerHandle::FetchBlobDataConsumerHandle(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
| + : m_readerContext(adoptRef(new ReaderContext(executionContext, blobDataHandle))) |
| +{ |
| +} |
| + |
| +PassOwnPtr<FetchDataConsumerHandle> FetchBlobDataConsumerHandle::create(ExecutionContext* executionContext, PassRefPtr<BlobDataHandle> blobDataHandle) |
| +{ |
| + if (!blobDataHandle) |
| + return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumerHandle()); |
| + |
| + return adoptPtr(new FetchBlobDataConsumerHandle(executionContext, blobDataHandle)); |
| +} |
| + |
| +FetchDataConsumerHandle::Reader* FetchBlobDataConsumerHandle::obtainReaderInternal(Client* client) |
| +{ |
| + return m_readerContext->obtainReader(client).leakPtr(); |
| +} |
| + |
| +} // namespace blink |