Chromium Code Reviews| 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/CrossThreadFunctional.h" | 7 #include "platform/CrossThreadFunctional.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" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 private: | 30 private: |
| 31 RefPtr<Context> m_context; | 31 RefPtr<Context> m_context; |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 class CompositeDataConsumerHandle::Context final : public ThreadSafeRefCounted<C ontext> { | 34 class CompositeDataConsumerHandle::Context final : public ThreadSafeRefCounted<C ontext> { |
| 35 public: | 35 public: |
| 36 using Token = unsigned; | 36 using Token = unsigned; |
| 37 static PassRefPtr<Context> create(std::unique_ptr<WebDataConsumerHandle> han dle) { 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))); } |
| 38 ~Context() | 38 ~Context() |
| 39 { | 39 { |
| 40 ASSERT(!m_readerThread); | 40 #if DCHECK_IS_ON() |
|
yhirano
2016/07/26 09:28:46
I'm not sure what this #if directive is doing. Can
tzik
2016/07/26 09:36:06
Oops, no it's not needed. Removed.
| |
| 41 ASSERT(!m_reader); | 41 DCHECK(!m_readerThread); |
| 42 ASSERT(!m_client); | 42 #endif |
| 43 DCHECK(!m_reader); | |
| 44 DCHECK(!m_client); | |
| 43 } | 45 } |
| 44 std::unique_ptr<ReaderImpl> obtainReader(Client* client) | 46 std::unique_ptr<ReaderImpl> obtainReader(Client* client) |
| 45 { | 47 { |
| 46 MutexLocker locker(m_mutex); | 48 MutexLocker locker(m_mutex); |
| 47 ASSERT(!m_readerThread); | 49 #if DCHECK_IS_ON() |
| 48 ASSERT(!m_reader); | 50 DCHECK(!m_readerThread); |
| 49 ASSERT(!m_client); | 51 #endif |
| 52 DCHECK(!m_reader); | |
| 53 DCHECK(!m_client); | |
| 50 ++m_token; | 54 ++m_token; |
| 51 m_client = client; | 55 m_client = client; |
| 52 m_readerThread = Platform::current()->currentThread(); | 56 m_readerThread = Platform::current()->currentThread(); |
| 53 m_reader = m_handle->obtainReader(m_client); | 57 m_reader = m_handle->obtainReader(m_client); |
| 54 return wrapUnique(new ReaderImpl(this)); | 58 return wrapUnique(new ReaderImpl(this)); |
| 55 } | 59 } |
| 56 void detachReader() | 60 void detachReader() |
| 57 { | 61 { |
| 58 MutexLocker locker(m_mutex); | 62 MutexLocker locker(m_mutex); |
| 59 ASSERT(m_readerThread); | 63 #if DCHECK_IS_ON() |
| 60 ASSERT(m_readerThread->isCurrentThread()); | 64 DCHECK(m_readerThread); |
| 61 ASSERT(m_reader); | 65 DCHECK(m_readerThread->isCurrentThread()); |
| 62 ASSERT(!m_isInTwoPhaseRead); | 66 #endif |
| 63 ASSERT(!m_isUpdateWaitingForEndRead); | 67 DCHECK(m_reader); |
| 68 DCHECK(!m_isInTwoPhaseRead); | |
| 69 DCHECK(!m_isUpdateWaitingForEndRead); | |
| 64 ++m_token; | 70 ++m_token; |
| 65 m_reader = nullptr; | 71 m_reader = nullptr; |
| 66 m_readerThread = nullptr; | 72 m_readerThread = nullptr; |
| 67 m_client = nullptr; | 73 m_client = nullptr; |
| 68 } | 74 } |
| 69 void update(std::unique_ptr<WebDataConsumerHandle> handle) | 75 void update(std::unique_ptr<WebDataConsumerHandle> handle) |
| 70 { | 76 { |
| 71 MutexLocker locker(m_mutex); | 77 MutexLocker locker(m_mutex); |
| 72 m_handle = std::move(handle); | 78 m_handle = std::move(handle); |
| 73 if (!m_readerThread) { | 79 if (!m_readerThread) { |
| 74 // There is no reader. | 80 // There is no reader. |
| 75 return; | 81 return; |
| 76 } | 82 } |
| 77 ++m_token; | 83 ++m_token; |
| 78 updateReaderNoLock(m_token); | 84 updateReaderNoLock(m_token); |
| 79 } | 85 } |
| 80 | 86 |
| 81 Result read(void* data, size_t size, Flags flags, size_t* readSize) | 87 Result read(void* data, size_t size, Flags flags, size_t* readSize) |
| 82 { | 88 { |
| 83 ASSERT(m_readerThread && m_readerThread->isCurrentThread()); | 89 DCHECK(m_readerThread && m_readerThread->isCurrentThread()); |
| 84 return m_reader->read(data, size, flags, readSize); | 90 return m_reader->read(data, size, flags, readSize); |
| 85 } | 91 } |
| 86 Result beginRead(const void** buffer, Flags flags, size_t* available) | 92 Result beginRead(const void** buffer, Flags flags, size_t* available) |
| 87 { | 93 { |
| 88 ASSERT(m_readerThread && m_readerThread->isCurrentThread()); | 94 DCHECK(m_readerThread && m_readerThread->isCurrentThread()); |
| 89 ASSERT(!m_isInTwoPhaseRead); | 95 DCHECK(!m_isInTwoPhaseRead); |
| 90 Result r = m_reader->beginRead(buffer, flags, available); | 96 Result r = m_reader->beginRead(buffer, flags, available); |
| 91 m_isInTwoPhaseRead = (r == Ok); | 97 m_isInTwoPhaseRead = (r == Ok); |
| 92 return r; | 98 return r; |
| 93 } | 99 } |
| 94 Result endRead(size_t readSize) | 100 Result endRead(size_t readSize) |
| 95 { | 101 { |
| 96 ASSERT(m_readerThread && m_readerThread->isCurrentThread()); | 102 DCHECK(m_readerThread && m_readerThread->isCurrentThread()); |
| 97 ASSERT(m_isInTwoPhaseRead); | 103 DCHECK(m_isInTwoPhaseRead); |
| 98 Result r = m_reader->endRead(readSize); | 104 Result r = m_reader->endRead(readSize); |
| 99 m_isInTwoPhaseRead = false; | 105 m_isInTwoPhaseRead = false; |
| 100 if (m_isUpdateWaitingForEndRead) { | 106 if (m_isUpdateWaitingForEndRead) { |
| 101 // We need this lock to access |m_handle|. | 107 // We need this lock to access |m_handle|. |
| 102 MutexLocker locker(m_mutex); | 108 MutexLocker locker(m_mutex); |
| 103 m_reader = nullptr; | 109 m_reader = nullptr; |
| 104 m_reader = m_handle->obtainReader(m_client); | 110 m_reader = m_handle->obtainReader(m_client); |
| 105 m_isUpdateWaitingForEndRead = false; | 111 m_isUpdateWaitingForEndRead = false; |
| 106 } | 112 } |
| 107 return r; | 113 return r; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 121 { | 127 { |
| 122 MutexLocker locker(m_mutex); | 128 MutexLocker locker(m_mutex); |
| 123 updateReaderNoLock(token); | 129 updateReaderNoLock(token); |
| 124 } | 130 } |
| 125 void updateReaderNoLock(Token token) | 131 void updateReaderNoLock(Token token) |
| 126 { | 132 { |
| 127 if (token != m_token) { | 133 if (token != m_token) { |
| 128 // This request is not fresh. Ignore it. | 134 // This request is not fresh. Ignore it. |
| 129 return; | 135 return; |
| 130 } | 136 } |
| 131 ASSERT(m_readerThread); | 137 #if DCHECK_IS_ON() |
| 132 ASSERT(m_reader); | 138 DCHECK(m_readerThread); |
| 139 #endif | |
| 140 DCHECK(m_reader); | |
| 133 if (m_readerThread->isCurrentThread()) { | 141 if (m_readerThread->isCurrentThread()) { |
| 134 if (m_isInTwoPhaseRead) { | 142 if (m_isInTwoPhaseRead) { |
| 135 // We are waiting for the two-phase read completion. | 143 // We are waiting for the two-phase read completion. |
| 136 m_isUpdateWaitingForEndRead = true; | 144 m_isUpdateWaitingForEndRead = true; |
| 137 return; | 145 return; |
| 138 } | 146 } |
| 139 // Unregister the old one, then register the new one. | 147 // Unregister the old one, then register the new one. |
| 140 m_reader = nullptr; | 148 m_reader = nullptr; |
| 141 m_reader = m_handle->obtainReader(m_client); | 149 m_reader = m_handle->obtainReader(m_client); |
| 142 return; | 150 return; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 return m_context->beginRead(buffer, flags, available); | 187 return m_context->beginRead(buffer, flags, available); |
| 180 } | 188 } |
| 181 | 189 |
| 182 Result CompositeDataConsumerHandle::ReaderImpl::endRead(size_t readSize) | 190 Result CompositeDataConsumerHandle::ReaderImpl::endRead(size_t readSize) |
| 183 { | 191 { |
| 184 return m_context->endRead(readSize); | 192 return m_context->endRead(readSize); |
| 185 } | 193 } |
| 186 | 194 |
| 187 CompositeDataConsumerHandle::Updater::Updater(PassRefPtr<Context> context) | 195 CompositeDataConsumerHandle::Updater::Updater(PassRefPtr<Context> context) |
| 188 : m_context(context) | 196 : m_context(context) |
| 189 #if ENABLE(ASSERT) | 197 #if DCHECK_IS_ON() |
| 190 , m_thread(Platform::current()->currentThread()) | 198 , m_thread(Platform::current()->currentThread()) |
| 191 #endif | 199 #endif |
| 192 { | 200 { |
| 193 } | 201 } |
| 194 | 202 |
| 195 CompositeDataConsumerHandle::Updater::~Updater() {} | 203 CompositeDataConsumerHandle::Updater::~Updater() {} |
| 196 | 204 |
| 197 void CompositeDataConsumerHandle::Updater::update(std::unique_ptr<WebDataConsume rHandle> handle) | 205 void CompositeDataConsumerHandle::Updater::update(std::unique_ptr<WebDataConsume rHandle> handle) |
| 198 { | 206 { |
| 199 ASSERT(handle); | 207 DCHECK(handle); |
| 200 ASSERT(m_thread->isCurrentThread()); | 208 #if DCHECK_IS_ON() |
|
yhirano
2016/07/26 09:28:46
I'm not sure what this #if directive is doing. Can
tzik
2016/07/26 09:36:06
DCHECK requires the statement be well-formed, whil
| |
| 209 DCHECK(m_thread->isCurrentThread()); | |
| 210 #endif | |
| 201 m_context->update(std::move(handle)); | 211 m_context->update(std::move(handle)); |
| 202 } | 212 } |
| 203 | 213 |
| 204 CompositeDataConsumerHandle::CompositeDataConsumerHandle(std::unique_ptr<WebData ConsumerHandle> handle, Updater** updater) | 214 CompositeDataConsumerHandle::CompositeDataConsumerHandle(std::unique_ptr<WebData ConsumerHandle> handle, Updater** updater) |
| 205 : m_context(Context::create(std::move(handle))) | 215 : m_context(Context::create(std::move(handle))) |
| 206 { | 216 { |
| 207 *updater = new Updater(m_context); | 217 *updater = new Updater(m_context); |
| 208 } | 218 } |
| 209 | 219 |
| 210 CompositeDataConsumerHandle::~CompositeDataConsumerHandle() { } | 220 CompositeDataConsumerHandle::~CompositeDataConsumerHandle() { } |
| 211 | 221 |
| 212 std::unique_ptr<WebDataConsumerHandle::Reader> CompositeDataConsumerHandle::obta inReader(Client* client) | 222 std::unique_ptr<WebDataConsumerHandle::Reader> CompositeDataConsumerHandle::obta inReader(Client* client) |
| 213 { | 223 { |
| 214 return m_context->obtainReader(client); | 224 return m_context->obtainReader(client); |
| 215 } | 225 } |
| 216 | 226 |
| 217 } // namespace blink | 227 } // namespace blink |
| OLD | NEW |