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