| 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/CompositeDataConsumerHandle.h" | 5 #include "modules/fetch/CompositeDataConsumerHandle.h" |
| 6 | 6 |
| 7 #include "platform/ThreadSafeFunctional.h" | 7 #include "platform/ThreadSafeFunctional.h" |
| 8 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
| 9 #include "public/platform/WebTaskRunner.h" | 9 #include "public/platform/WebTaskRunner.h" |
| 10 #include "public/platform/WebThread.h" | 10 #include "public/platform/WebThread.h" |
| 11 #include "public/platform/WebTraceLocation.h" | 11 #include "public/platform/WebTraceLocation.h" |
| 12 #include "wtf/Locker.h" | 12 #include "wtf/Locker.h" |
| 13 #include "wtf/PtrUtil.h" |
| 13 #include "wtf/ThreadSafeRefCounted.h" | 14 #include "wtf/ThreadSafeRefCounted.h" |
| 14 #include "wtf/ThreadingPrimitives.h" | 15 #include "wtf/ThreadingPrimitives.h" |
| 16 #include <memory> |
| 15 | 17 |
| 16 namespace blink { | 18 namespace blink { |
| 17 | 19 |
| 18 using Result = WebDataConsumerHandle::Result; | 20 using Result = WebDataConsumerHandle::Result; |
| 19 | 21 |
| 20 class CompositeDataConsumerHandle::ReaderImpl final : public WebDataConsumerHand
le::Reader { | 22 class CompositeDataConsumerHandle::ReaderImpl final : public WebDataConsumerHand
le::Reader { |
| 21 public: | 23 public: |
| 22 explicit ReaderImpl(PassRefPtr<Context>); | 24 explicit ReaderImpl(PassRefPtr<Context>); |
| 23 ~ReaderImpl() override; | 25 ~ReaderImpl() override; |
| 24 Result read(void* data, size_t /* size */, Flags, size_t* readSize) override
; | 26 Result read(void* data, size_t /* size */, Flags, size_t* readSize) override
; |
| 25 Result beginRead(const void** buffer, Flags, size_t* available) override; | 27 Result beginRead(const void** buffer, Flags, size_t* available) override; |
| 26 Result endRead(size_t readSize) override; | 28 Result endRead(size_t readSize) override; |
| 27 | 29 |
| 28 private: | 30 private: |
| 29 RefPtr<Context> m_context; | 31 RefPtr<Context> m_context; |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 class CompositeDataConsumerHandle::Context final : public ThreadSafeRefCounted<C
ontext> { | 34 class CompositeDataConsumerHandle::Context final : public ThreadSafeRefCounted<C
ontext> { |
| 33 public: | 35 public: |
| 34 using Token = unsigned; | 36 using Token = unsigned; |
| 35 static PassRefPtr<Context> create(PassOwnPtr<WebDataConsumerHandle> handle)
{ return adoptRef(new Context(std::move(handle))); } | 37 static PassRefPtr<Context> create(std::unique_ptr<WebDataConsumerHandle> han
dle) { return adoptRef(new Context(std::move(handle))); } |
| 36 ~Context() | 38 ~Context() |
| 37 { | 39 { |
| 38 ASSERT(!m_readerThread); | 40 ASSERT(!m_readerThread); |
| 39 ASSERT(!m_reader); | 41 ASSERT(!m_reader); |
| 40 ASSERT(!m_client); | 42 ASSERT(!m_client); |
| 41 } | 43 } |
| 42 PassOwnPtr<ReaderImpl> obtainReader(Client* client) | 44 std::unique_ptr<ReaderImpl> obtainReader(Client* client) |
| 43 { | 45 { |
| 44 MutexLocker locker(m_mutex); | 46 MutexLocker locker(m_mutex); |
| 45 ASSERT(!m_readerThread); | 47 ASSERT(!m_readerThread); |
| 46 ASSERT(!m_reader); | 48 ASSERT(!m_reader); |
| 47 ASSERT(!m_client); | 49 ASSERT(!m_client); |
| 48 ++m_token; | 50 ++m_token; |
| 49 m_client = client; | 51 m_client = client; |
| 50 m_readerThread = Platform::current()->currentThread(); | 52 m_readerThread = Platform::current()->currentThread(); |
| 51 m_reader = m_handle->obtainReader(m_client); | 53 m_reader = m_handle->obtainReader(m_client); |
| 52 return adoptPtr(new ReaderImpl(this)); | 54 return wrapUnique(new ReaderImpl(this)); |
| 53 } | 55 } |
| 54 void detachReader() | 56 void detachReader() |
| 55 { | 57 { |
| 56 MutexLocker locker(m_mutex); | 58 MutexLocker locker(m_mutex); |
| 57 ASSERT(m_readerThread); | 59 ASSERT(m_readerThread); |
| 58 ASSERT(m_readerThread->isCurrentThread()); | 60 ASSERT(m_readerThread->isCurrentThread()); |
| 59 ASSERT(m_reader); | 61 ASSERT(m_reader); |
| 60 ASSERT(!m_isInTwoPhaseRead); | 62 ASSERT(!m_isInTwoPhaseRead); |
| 61 ASSERT(!m_isUpdateWaitingForEndRead); | 63 ASSERT(!m_isUpdateWaitingForEndRead); |
| 62 ++m_token; | 64 ++m_token; |
| 63 m_reader = nullptr; | 65 m_reader = nullptr; |
| 64 m_readerThread = nullptr; | 66 m_readerThread = nullptr; |
| 65 m_client = nullptr; | 67 m_client = nullptr; |
| 66 } | 68 } |
| 67 void update(PassOwnPtr<WebDataConsumerHandle> handle) | 69 void update(std::unique_ptr<WebDataConsumerHandle> handle) |
| 68 { | 70 { |
| 69 MutexLocker locker(m_mutex); | 71 MutexLocker locker(m_mutex); |
| 70 m_handle = std::move(handle); | 72 m_handle = std::move(handle); |
| 71 if (!m_readerThread) { | 73 if (!m_readerThread) { |
| 72 // There is no reader. | 74 // There is no reader. |
| 73 return; | 75 return; |
| 74 } | 76 } |
| 75 ++m_token; | 77 ++m_token; |
| 76 updateReaderNoLock(m_token); | 78 updateReaderNoLock(m_token); |
| 77 } | 79 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 99 // We need this lock to access |m_handle|. | 101 // We need this lock to access |m_handle|. |
| 100 MutexLocker locker(m_mutex); | 102 MutexLocker locker(m_mutex); |
| 101 m_reader = nullptr; | 103 m_reader = nullptr; |
| 102 m_reader = m_handle->obtainReader(m_client); | 104 m_reader = m_handle->obtainReader(m_client); |
| 103 m_isUpdateWaitingForEndRead = false; | 105 m_isUpdateWaitingForEndRead = false; |
| 104 } | 106 } |
| 105 return r; | 107 return r; |
| 106 } | 108 } |
| 107 | 109 |
| 108 private: | 110 private: |
| 109 explicit Context(PassOwnPtr<WebDataConsumerHandle> handle) | 111 explicit Context(std::unique_ptr<WebDataConsumerHandle> handle) |
| 110 : m_handle(std::move(handle)) | 112 : m_handle(std::move(handle)) |
| 111 , m_readerThread(nullptr) | 113 , m_readerThread(nullptr) |
| 112 , m_client(nullptr) | 114 , m_client(nullptr) |
| 113 , m_token(0) | 115 , m_token(0) |
| 114 , m_isUpdateWaitingForEndRead(false) | 116 , m_isUpdateWaitingForEndRead(false) |
| 115 , m_isInTwoPhaseRead(false) | 117 , m_isInTwoPhaseRead(false) |
| 116 { | 118 { |
| 117 } | 119 } |
| 118 void updateReader(Token token) | 120 void updateReader(Token token) |
| 119 { | 121 { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 136 } | 138 } |
| 137 // Unregister the old one, then register the new one. | 139 // Unregister the old one, then register the new one. |
| 138 m_reader = nullptr; | 140 m_reader = nullptr; |
| 139 m_reader = m_handle->obtainReader(m_client); | 141 m_reader = m_handle->obtainReader(m_client); |
| 140 return; | 142 return; |
| 141 } | 143 } |
| 142 ++m_token; | 144 ++m_token; |
| 143 m_readerThread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafe
Bind(&Context::updateReader, this, m_token)); | 145 m_readerThread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafe
Bind(&Context::updateReader, this, m_token)); |
| 144 } | 146 } |
| 145 | 147 |
| 146 OwnPtr<Reader> m_reader; | 148 std::unique_ptr<Reader> m_reader; |
| 147 OwnPtr<WebDataConsumerHandle> m_handle; | 149 std::unique_ptr<WebDataConsumerHandle> m_handle; |
| 148 // Note: Holding a WebThread raw pointer is not generally safe, but we can | 150 // Note: Holding a WebThread raw pointer is not generally safe, but we can |
| 149 // do that in this case because: | 151 // do that in this case because: |
| 150 // 1. Destructing a ReaderImpl when the bound thread ends is a user's | 152 // 1. Destructing a ReaderImpl when the bound thread ends is a user's |
| 151 // responsibility. | 153 // responsibility. |
| 152 // 2. |m_readerThread| will never be used after the associated reader is | 154 // 2. |m_readerThread| will never be used after the associated reader is |
| 153 // detached. | 155 // detached. |
| 154 WebThread* m_readerThread; | 156 WebThread* m_readerThread; |
| 155 Client* m_client; | 157 Client* m_client; |
| 156 Token m_token; | 158 Token m_token; |
| 157 // These boolean values are bound to the reader thread. | 159 // These boolean values are bound to the reader thread. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 185 CompositeDataConsumerHandle::Updater::Updater(PassRefPtr<Context> context) | 187 CompositeDataConsumerHandle::Updater::Updater(PassRefPtr<Context> context) |
| 186 : m_context(context) | 188 : m_context(context) |
| 187 #if ENABLE(ASSERT) | 189 #if ENABLE(ASSERT) |
| 188 , m_thread(Platform::current()->currentThread()) | 190 , m_thread(Platform::current()->currentThread()) |
| 189 #endif | 191 #endif |
| 190 { | 192 { |
| 191 } | 193 } |
| 192 | 194 |
| 193 CompositeDataConsumerHandle::Updater::~Updater() {} | 195 CompositeDataConsumerHandle::Updater::~Updater() {} |
| 194 | 196 |
| 195 void CompositeDataConsumerHandle::Updater::update(PassOwnPtr<WebDataConsumerHand
le> handle) | 197 void CompositeDataConsumerHandle::Updater::update(std::unique_ptr<WebDataConsume
rHandle> handle) |
| 196 { | 198 { |
| 197 ASSERT(handle); | 199 ASSERT(handle); |
| 198 ASSERT(m_thread->isCurrentThread()); | 200 ASSERT(m_thread->isCurrentThread()); |
| 199 m_context->update(std::move(handle)); | 201 m_context->update(std::move(handle)); |
| 200 } | 202 } |
| 201 | 203 |
| 202 CompositeDataConsumerHandle::CompositeDataConsumerHandle(PassOwnPtr<WebDataConsu
merHandle> handle, Updater** updater) | 204 CompositeDataConsumerHandle::CompositeDataConsumerHandle(std::unique_ptr<WebData
ConsumerHandle> handle, Updater** updater) |
| 203 : m_context(Context::create(std::move(handle))) | 205 : m_context(Context::create(std::move(handle))) |
| 204 { | 206 { |
| 205 *updater = new Updater(m_context); | 207 *updater = new Updater(m_context); |
| 206 } | 208 } |
| 207 | 209 |
| 208 CompositeDataConsumerHandle::~CompositeDataConsumerHandle() { } | 210 CompositeDataConsumerHandle::~CompositeDataConsumerHandle() { } |
| 209 | 211 |
| 210 WebDataConsumerHandle::Reader* CompositeDataConsumerHandle::obtainReaderInternal
(Client* client) | 212 WebDataConsumerHandle::Reader* CompositeDataConsumerHandle::obtainReaderInternal
(Client* client) |
| 211 { | 213 { |
| 212 return m_context->obtainReader(client).leakPtr(); | 214 return m_context->obtainReader(client).release(); |
| 213 } | 215 } |
| 214 | 216 |
| 215 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |