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 #ifndef DataConsumerHandleTestUtil_h | 5 #ifndef DataConsumerHandleTestUtil_h |
6 #define DataConsumerHandleTestUtil_h | 6 #define DataConsumerHandleTestUtil_h |
7 | 7 |
8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
9 #include "core/testing/NullExecutionContext.h" | 9 #include "core/testing/NullExecutionContext.h" |
10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
11 #include "modules/fetch/DataConsumerHandleUtil.h" | 11 #include "modules/fetch/DataConsumerHandleUtil.h" |
12 #include "modules/fetch/FetchDataConsumerHandle.h" | 12 #include "modules/fetch/FetchDataConsumerHandle.h" |
13 #include "modules/fetch/FetchDataLoader.h" | 13 #include "modules/fetch/FetchDataLoader.h" |
14 #include "platform/ThreadSafeFunctional.h" | 14 #include "platform/ThreadSafeFunctional.h" |
15 #include "platform/WaitableEvent.h" | 15 #include "platform/WaitableEvent.h" |
16 #include "platform/WebThreadSupportingGC.h" | 16 #include "platform/WebThreadSupportingGC.h" |
17 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
18 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
19 #include "public/platform/WebDataConsumerHandle.h" | 19 #include "public/platform/WebDataConsumerHandle.h" |
20 #include "public/platform/WebTraceLocation.h" | 20 #include "public/platform/WebTraceLocation.h" |
21 #include "wtf/Deque.h" | 21 #include "wtf/Deque.h" |
22 #include "wtf/Locker.h" | 22 #include "wtf/Locker.h" |
23 #include "wtf/OwnPtr.h" | 23 #include "wtf/PtrUtil.h" |
24 #include "wtf/PassOwnPtr.h" | |
25 #include "wtf/ThreadSafeRefCounted.h" | 24 #include "wtf/ThreadSafeRefCounted.h" |
26 #include "wtf/ThreadingPrimitives.h" | 25 #include "wtf/ThreadingPrimitives.h" |
27 #include "wtf/Vector.h" | 26 #include "wtf/Vector.h" |
28 | |
29 #include <gmock/gmock.h> | 27 #include <gmock/gmock.h> |
30 #include <gtest/gtest.h> | 28 #include <gtest/gtest.h> |
| 29 #include <memory> |
31 #include <v8.h> | 30 #include <v8.h> |
32 | 31 |
33 namespace blink { | 32 namespace blink { |
34 | 33 |
35 class DataConsumerHandleTestUtil { | 34 class DataConsumerHandleTestUtil { |
36 STATIC_ONLY(DataConsumerHandleTestUtil); | 35 STATIC_ONLY(DataConsumerHandleTestUtil); |
37 public: | 36 public: |
38 class NoopClient final : public WebDataConsumerHandle::Client { | 37 class NoopClient final : public WebDataConsumerHandle::Client { |
39 DISALLOW_NEW(); | 38 DISALLOW_NEW(); |
40 public: | 39 public: |
(...skipping 21 matching lines...) Expand all Loading... |
62 | 61 |
63 WebThreadSupportingGC* thread() { return m_thread.get(); } | 62 WebThreadSupportingGC* thread() { return m_thread.get(); } |
64 ExecutionContext* getExecutionContext() { return m_executionContext.get(
); } | 63 ExecutionContext* getExecutionContext() { return m_executionContext.get(
); } |
65 ScriptState* getScriptState() { return m_scriptState.get(); } | 64 ScriptState* getScriptState() { return m_scriptState.get(); } |
66 v8::Isolate* isolate() { return m_isolateHolder->isolate(); } | 65 v8::Isolate* isolate() { return m_isolateHolder->isolate(); } |
67 | 66 |
68 private: | 67 private: |
69 void initialize(); | 68 void initialize(); |
70 void shutdown(); | 69 void shutdown(); |
71 | 70 |
72 OwnPtr<WebThreadSupportingGC> m_thread; | 71 std::unique_ptr<WebThreadSupportingGC> m_thread; |
73 const InitializationPolicy m_initializationPolicy; | 72 const InitializationPolicy m_initializationPolicy; |
74 OwnPtr<WaitableEvent> m_waitableEvent; | 73 std::unique_ptr<WaitableEvent> m_waitableEvent; |
75 Persistent<NullExecutionContext> m_executionContext; | 74 Persistent<NullExecutionContext> m_executionContext; |
76 OwnPtr<gin::IsolateHolder> m_isolateHolder; | 75 std::unique_ptr<gin::IsolateHolder> m_isolateHolder; |
77 RefPtr<ScriptState> m_scriptState; | 76 RefPtr<ScriptState> m_scriptState; |
78 }; | 77 }; |
79 | 78 |
80 class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> { | 79 class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> { |
81 public: | 80 public: |
82 virtual ~ThreadingTestBase() { } | 81 virtual ~ThreadingTestBase() { } |
83 | 82 |
84 class ThreadHolder; | 83 class ThreadHolder; |
85 | 84 |
86 class Context : public ThreadSafeRefCounted<Context> { | 85 class Context : public ThreadSafeRefCounted<Context> { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 // here is safe. | 152 // here is safe. |
154 ThreadHolder* m_holder; | 153 ThreadHolder* m_holder; |
155 }; | 154 }; |
156 | 155 |
157 // The reading/updating threads are alive while ThreadHolder is alive. | 156 // The reading/updating threads are alive while ThreadHolder is alive. |
158 class ThreadHolder { | 157 class ThreadHolder { |
159 DISALLOW_NEW(); | 158 DISALLOW_NEW(); |
160 public: | 159 public: |
161 ThreadHolder(ThreadingTestBase* test) | 160 ThreadHolder(ThreadingTestBase* test) |
162 : m_context(test->m_context) | 161 : m_context(test->m_context) |
163 , m_readingThread(adoptPtr(new Thread("reading thread"))) | 162 , m_readingThread(wrapUnique(new Thread("reading thread"))) |
164 , m_updatingThread(adoptPtr(new Thread("updating thread"))) | 163 , m_updatingThread(wrapUnique(new Thread("updating thread"))) |
165 { | 164 { |
166 m_context->registerThreadHolder(this); | 165 m_context->registerThreadHolder(this); |
167 } | 166 } |
168 ~ThreadHolder() | 167 ~ThreadHolder() |
169 { | 168 { |
170 m_context->unregisterThreadHolder(); | 169 m_context->unregisterThreadHolder(); |
171 } | 170 } |
172 | 171 |
173 WebThreadSupportingGC* readingThread() { return m_readingThread->thr
ead(); } | 172 WebThreadSupportingGC* readingThread() { return m_readingThread->thr
ead(); } |
174 WebThreadSupportingGC* updatingThread() { return m_updatingThread->t
hread(); } | 173 WebThreadSupportingGC* updatingThread() { return m_updatingThread->t
hread(); } |
175 | 174 |
176 private: | 175 private: |
177 RefPtr<Context> m_context; | 176 RefPtr<Context> m_context; |
178 OwnPtr<Thread> m_readingThread; | 177 std::unique_ptr<Thread> m_readingThread; |
179 OwnPtr<Thread> m_updatingThread; | 178 std::unique_ptr<Thread> m_updatingThread; |
180 }; | 179 }; |
181 | 180 |
182 class ReaderImpl final : public WebDataConsumerHandle::Reader { | 181 class ReaderImpl final : public WebDataConsumerHandle::Reader { |
183 USING_FAST_MALLOC(ReaderImpl); | 182 USING_FAST_MALLOC(ReaderImpl); |
184 public: | 183 public: |
185 ReaderImpl(const String& name, PassRefPtr<Context> context) : m_name
(name.isolatedCopy()), m_context(context) | 184 ReaderImpl(const String& name, PassRefPtr<Context> context) : m_name
(name.isolatedCopy()), m_context(context) |
186 { | 185 { |
187 m_context->recordAttach(m_name.isolatedCopy()); | 186 m_context->recordAttach(m_name.isolatedCopy()); |
188 } | 187 } |
189 ~ReaderImpl() override { m_context->recordDetach(m_name.isolatedCopy
()); } | 188 ~ReaderImpl() override { m_context->recordDetach(m_name.isolatedCopy
()); } |
190 | 189 |
191 using Result = WebDataConsumerHandle::Result; | 190 using Result = WebDataConsumerHandle::Result; |
192 using Flags = WebDataConsumerHandle::Flags; | 191 using Flags = WebDataConsumerHandle::Flags; |
193 Result beginRead(const void**, Flags, size_t*) override { return Web
DataConsumerHandle::ShouldWait; } | 192 Result beginRead(const void**, Flags, size_t*) override { return Web
DataConsumerHandle::ShouldWait; } |
194 Result endRead(size_t) override { return WebDataConsumerHandle::Unex
pectedError; } | 193 Result endRead(size_t) override { return WebDataConsumerHandle::Unex
pectedError; } |
195 | 194 |
196 private: | 195 private: |
197 const String m_name; | 196 const String m_name; |
198 RefPtr<Context> m_context; | 197 RefPtr<Context> m_context; |
199 }; | 198 }; |
200 class DataConsumerHandle final : public WebDataConsumerHandle { | 199 class DataConsumerHandle final : public WebDataConsumerHandle { |
201 USING_FAST_MALLOC(DataConsumerHandle); | 200 USING_FAST_MALLOC(DataConsumerHandle); |
202 public: | 201 public: |
203 static PassOwnPtr<WebDataConsumerHandle> create(const String& name,
PassRefPtr<Context> context) | 202 static std::unique_ptr<WebDataConsumerHandle> create(const String& n
ame, PassRefPtr<Context> context) |
204 { | 203 { |
205 return adoptPtr(new DataConsumerHandle(name, context)); | 204 return wrapUnique(new DataConsumerHandle(name, context)); |
206 } | 205 } |
207 | 206 |
208 private: | 207 private: |
209 DataConsumerHandle(const String& name, PassRefPtr<Context> context)
: m_name(name.isolatedCopy()), m_context(context) { } | 208 DataConsumerHandle(const String& name, PassRefPtr<Context> context)
: m_name(name.isolatedCopy()), m_context(context) { } |
210 | 209 |
211 Reader* obtainReaderInternal(Client*) { return new ReaderImpl(m_name
, m_context); } | 210 Reader* obtainReaderInternal(Client*) { return new ReaderImpl(m_name
, m_context); } |
212 const char* debugName() const override { return "ThreadingTestBase::
DataConsumerHandle"; } | 211 const char* debugName() const override { return "ThreadingTestBase::
DataConsumerHandle"; } |
213 | 212 |
214 const String m_name; | 213 const String m_name; |
215 RefPtr<Context> m_context; | 214 RefPtr<Context> m_context; |
(...skipping 18 matching lines...) Expand all Loading... |
234 void postTaskToUpdatingThreadAndWait(const WebTraceLocation& location, s
td::unique_ptr<CrossThreadClosure> task) | 233 void postTaskToUpdatingThreadAndWait(const WebTraceLocation& location, s
td::unique_ptr<CrossThreadClosure> task) |
235 { | 234 { |
236 postTaskToUpdatingThread(location, std::move(task)); | 235 postTaskToUpdatingThread(location, std::move(task)); |
237 m_waitableEvent->wait(); | 236 m_waitableEvent->wait(); |
238 } | 237 } |
239 protected: | 238 protected: |
240 ThreadingTestBase() | 239 ThreadingTestBase() |
241 : m_context(Context::create()) { } | 240 : m_context(Context::create()) { } |
242 | 241 |
243 RefPtr<Context> m_context; | 242 RefPtr<Context> m_context; |
244 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | 243 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; |
245 OwnPtr<WaitableEvent> m_waitableEvent; | 244 std::unique_ptr<WaitableEvent> m_waitableEvent; |
246 NoopClient m_client; | 245 NoopClient m_client; |
247 }; | 246 }; |
248 | 247 |
249 class ThreadingHandleNotificationTest : public ThreadingTestBase, public Web
DataConsumerHandle::Client { | 248 class ThreadingHandleNotificationTest : public ThreadingTestBase, public Web
DataConsumerHandle::Client { |
250 public: | 249 public: |
251 using Self = ThreadingHandleNotificationTest; | 250 using Self = ThreadingHandleNotificationTest; |
252 static PassRefPtr<Self> create() { return adoptRef(new Self); } | 251 static PassRefPtr<Self> create() { return adoptRef(new Self); } |
253 | 252 |
254 void run(PassOwnPtr<WebDataConsumerHandle> handle) | 253 void run(std::unique_ptr<WebDataConsumerHandle> handle) |
255 { | 254 { |
256 ThreadHolder holder(this); | 255 ThreadHolder holder(this); |
257 m_waitableEvent = adoptPtr(new WaitableEvent()); | 256 m_waitableEvent = wrapUnique(new WaitableEvent()); |
258 m_handle = std::move(handle); | 257 m_handle = std::move(handle); |
259 | 258 |
260 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, threadSafeBind(&Self
::obtainReader, this)); | 259 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, threadSafeBind(&Self
::obtainReader, this)); |
261 } | 260 } |
262 | 261 |
263 private: | 262 private: |
264 ThreadingHandleNotificationTest() = default; | 263 ThreadingHandleNotificationTest() = default; |
265 void obtainReader() | 264 void obtainReader() |
266 { | 265 { |
267 m_reader = m_handle->obtainReader(this); | 266 m_reader = m_handle->obtainReader(this); |
268 } | 267 } |
269 void didGetReadable() override | 268 void didGetReadable() override |
270 { | 269 { |
271 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::reset
Reader, this)); | 270 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::reset
Reader, this)); |
272 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::signa
lDone, this)); | 271 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::signa
lDone, this)); |
273 } | 272 } |
274 | 273 |
275 OwnPtr<WebDataConsumerHandle> m_handle; | 274 std::unique_ptr<WebDataConsumerHandle> m_handle; |
276 }; | 275 }; |
277 | 276 |
278 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W
ebDataConsumerHandle::Client { | 277 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W
ebDataConsumerHandle::Client { |
279 public: | 278 public: |
280 using Self = ThreadingHandleNoNotificationTest; | 279 using Self = ThreadingHandleNoNotificationTest; |
281 static PassRefPtr<Self> create() { return adoptRef(new Self); } | 280 static PassRefPtr<Self> create() { return adoptRef(new Self); } |
282 | 281 |
283 void run(PassOwnPtr<WebDataConsumerHandle> handle) | 282 void run(std::unique_ptr<WebDataConsumerHandle> handle) |
284 { | 283 { |
285 ThreadHolder holder(this); | 284 ThreadHolder holder(this); |
286 m_waitableEvent = adoptPtr(new WaitableEvent()); | 285 m_waitableEvent = wrapUnique(new WaitableEvent()); |
287 m_handle = std::move(handle); | 286 m_handle = std::move(handle); |
288 | 287 |
289 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, threadSafeBind(&Self
::obtainReader, this)); | 288 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, threadSafeBind(&Self
::obtainReader, this)); |
290 } | 289 } |
291 | 290 |
292 private: | 291 private: |
293 ThreadingHandleNoNotificationTest() = default; | 292 ThreadingHandleNoNotificationTest() = default; |
294 void obtainReader() | 293 void obtainReader() |
295 { | 294 { |
296 m_reader = m_handle->obtainReader(this); | 295 m_reader = m_handle->obtainReader(this); |
297 m_reader = nullptr; | 296 m_reader = nullptr; |
298 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::signa
lDone, this)); | 297 postTaskToReadingThread(BLINK_FROM_HERE, threadSafeBind(&Self::signa
lDone, this)); |
299 } | 298 } |
300 void didGetReadable() override | 299 void didGetReadable() override |
301 { | 300 { |
302 ASSERT_NOT_REACHED(); | 301 ASSERT_NOT_REACHED(); |
303 } | 302 } |
304 | 303 |
305 OwnPtr<WebDataConsumerHandle> m_handle; | 304 std::unique_ptr<WebDataConsumerHandle> m_handle; |
306 }; | 305 }; |
307 | 306 |
308 class MockFetchDataConsumerHandle : public FetchDataConsumerHandle { | 307 class MockFetchDataConsumerHandle : public FetchDataConsumerHandle { |
309 public: | 308 public: |
310 static PassOwnPtr<::testing::StrictMock<MockFetchDataConsumerHandle>> cr
eate() { return adoptPtr(new ::testing::StrictMock<MockFetchDataConsumerHandle>)
; } | 309 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerHandle
>> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerH
andle>); } |
311 MOCK_METHOD1(obtainReaderInternal, Reader*(Client*)); | 310 MOCK_METHOD1(obtainReaderInternal, Reader*(Client*)); |
312 | 311 |
313 private: | 312 private: |
314 const char* debugName() const override { return "MockFetchDataConsumerHa
ndle"; } | 313 const char* debugName() const override { return "MockFetchDataConsumerHa
ndle"; } |
315 }; | 314 }; |
316 | 315 |
317 class MockFetchDataConsumerReader : public FetchDataConsumerHandle::Reader { | 316 class MockFetchDataConsumerReader : public FetchDataConsumerHandle::Reader { |
318 public: | 317 public: |
319 static PassOwnPtr<::testing::StrictMock<MockFetchDataConsumerReader>> cr
eate() { return adoptPtr(new ::testing::StrictMock<MockFetchDataConsumerReader>)
; } | 318 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerReader
>> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerR
eader>); } |
320 | 319 |
321 using Result = WebDataConsumerHandle::Result; | 320 using Result = WebDataConsumerHandle::Result; |
322 using Flags = WebDataConsumerHandle::Flags; | 321 using Flags = WebDataConsumerHandle::Flags; |
323 MOCK_METHOD4(read, Result(void*, size_t, Flags, size_t*)); | 322 MOCK_METHOD4(read, Result(void*, size_t, Flags, size_t*)); |
324 MOCK_METHOD3(beginRead, Result(const void**, Flags, size_t*)); | 323 MOCK_METHOD3(beginRead, Result(const void**, Flags, size_t*)); |
325 MOCK_METHOD1(endRead, Result(size_t)); | 324 MOCK_METHOD1(endRead, Result(size_t)); |
326 MOCK_METHOD1(drainAsBlobDataHandle, PassRefPtr<BlobDataHandle>(BlobSizeP
olicy)); | 325 MOCK_METHOD1(drainAsBlobDataHandle, PassRefPtr<BlobDataHandle>(BlobSizeP
olicy)); |
327 | 326 |
328 ~MockFetchDataConsumerReader() override | 327 ~MockFetchDataConsumerReader() override |
329 { | 328 { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 380 |
382 private: | 381 private: |
383 const Name m_name; | 382 const Name m_name; |
384 Vector<char> m_body; | 383 Vector<char> m_body; |
385 }; | 384 }; |
386 | 385 |
387 // ReplayingHandle stores commands via |add| and replays the stored commends
when read. | 386 // ReplayingHandle stores commands via |add| and replays the stored commends
when read. |
388 class ReplayingHandle final : public WebDataConsumerHandle { | 387 class ReplayingHandle final : public WebDataConsumerHandle { |
389 USING_FAST_MALLOC(ReplayingHandle); | 388 USING_FAST_MALLOC(ReplayingHandle); |
390 public: | 389 public: |
391 static PassOwnPtr<ReplayingHandle> create() { return adoptPtr(new Replay
ingHandle()); } | 390 static std::unique_ptr<ReplayingHandle> create() { return wrapUnique(new
ReplayingHandle()); } |
392 ~ReplayingHandle(); | 391 ~ReplayingHandle(); |
393 | 392 |
394 // Add a command to this handle. This function must be called on the | 393 // Add a command to this handle. This function must be called on the |
395 // creator thread. This function must be called BEFORE any reader is | 394 // creator thread. This function must be called BEFORE any reader is |
396 // obtained. | 395 // obtained. |
397 void add(const Command&); | 396 void add(const Command&); |
398 | 397 |
399 class Context final : public ThreadSafeRefCounted<Context> { | 398 class Context final : public ThreadSafeRefCounted<Context> { |
400 public: | 399 public: |
401 static PassRefPtr<Context> create() { return adoptRef(new Context);
} | 400 static PassRefPtr<Context> create() { return adoptRef(new Context);
} |
(...skipping 16 matching lines...) Expand all Loading... |
418 void notify(); | 417 void notify(); |
419 void notifyInternal(); | 418 void notifyInternal(); |
420 | 419 |
421 Deque<Command> m_commands; | 420 Deque<Command> m_commands; |
422 size_t m_offset; | 421 size_t m_offset; |
423 WebThread* m_readerThread; | 422 WebThread* m_readerThread; |
424 Client* m_client; | 423 Client* m_client; |
425 Result m_result; | 424 Result m_result; |
426 bool m_isHandleAttached; | 425 bool m_isHandleAttached; |
427 Mutex m_mutex; | 426 Mutex m_mutex; |
428 OwnPtr<WaitableEvent> m_detached; | 427 std::unique_ptr<WaitableEvent> m_detached; |
429 }; | 428 }; |
430 | 429 |
431 Context* getContext() { return m_context.get(); } | 430 Context* getContext() { return m_context.get(); } |
432 | 431 |
433 private: | 432 private: |
434 class ReaderImpl; | 433 class ReaderImpl; |
435 | 434 |
436 ReplayingHandle(); | 435 ReplayingHandle(); |
437 Reader* obtainReaderInternal(Client*) override; | 436 Reader* obtainReaderInternal(Client*) override; |
438 const char* debugName() const override { return "ReplayingHandle"; } | 437 const char* debugName() const override { return "ReplayingHandle"; } |
(...skipping 12 matching lines...) Expand all Loading... |
451 const WebDataConsumerHandle::Result m_result; | 450 const WebDataConsumerHandle::Result m_result; |
452 const Vector<char> m_data; | 451 const Vector<char> m_data; |
453 }; | 452 }; |
454 | 453 |
455 // HandleReader reads all data from the given WebDataConsumerHandle using | 454 // HandleReader reads all data from the given WebDataConsumerHandle using |
456 // Reader::read on the thread on which it is created. When reading is done | 455 // Reader::read on the thread on which it is created. When reading is done |
457 // or failed, it calls the given callback with the result. | 456 // or failed, it calls the given callback with the result. |
458 class HandleReader final : public WebDataConsumerHandle::Client { | 457 class HandleReader final : public WebDataConsumerHandle::Client { |
459 USING_FAST_MALLOC(HandleReader); | 458 USING_FAST_MALLOC(HandleReader); |
460 public: | 459 public: |
461 using OnFinishedReading = WTF::Function<void(PassOwnPtr<HandleReadResult
>)>; | 460 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR
esult>)>; |
462 | 461 |
463 HandleReader(PassOwnPtr<WebDataConsumerHandle>, std::unique_ptr<OnFinish
edReading>); | 462 HandleReader(std::unique_ptr<WebDataConsumerHandle>, std::unique_ptr<OnF
inishedReading>); |
464 void didGetReadable() override; | 463 void didGetReadable() override; |
465 | 464 |
466 private: | 465 private: |
467 void runOnFinishedReading(PassOwnPtr<HandleReadResult>); | 466 void runOnFinishedReading(std::unique_ptr<HandleReadResult>); |
468 | 467 |
469 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | 468 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; |
470 std::unique_ptr<OnFinishedReading> m_onFinishedReading; | 469 std::unique_ptr<OnFinishedReading> m_onFinishedReading; |
471 Vector<char> m_data; | 470 Vector<char> m_data; |
472 }; | 471 }; |
473 | 472 |
474 // HandleTwoPhaseReader does the same as HandleReader, but it uses | 473 // HandleTwoPhaseReader does the same as HandleReader, but it uses |
475 // |beginRead| / |endRead| instead of |read|. | 474 // |beginRead| / |endRead| instead of |read|. |
476 class HandleTwoPhaseReader final : public WebDataConsumerHandle::Client { | 475 class HandleTwoPhaseReader final : public WebDataConsumerHandle::Client { |
477 USING_FAST_MALLOC(HandleTwoPhaseReader); | 476 USING_FAST_MALLOC(HandleTwoPhaseReader); |
478 public: | 477 public: |
479 using OnFinishedReading = WTF::Function<void(PassOwnPtr<HandleReadResult
>)>; | 478 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR
esult>)>; |
480 | 479 |
481 HandleTwoPhaseReader(PassOwnPtr<WebDataConsumerHandle>, std::unique_ptr<
OnFinishedReading>); | 480 HandleTwoPhaseReader(std::unique_ptr<WebDataConsumerHandle>, std::unique
_ptr<OnFinishedReading>); |
482 void didGetReadable() override; | 481 void didGetReadable() override; |
483 | 482 |
484 private: | 483 private: |
485 void runOnFinishedReading(PassOwnPtr<HandleReadResult>); | 484 void runOnFinishedReading(std::unique_ptr<HandleReadResult>); |
486 | 485 |
487 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | 486 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; |
488 std::unique_ptr<OnFinishedReading> m_onFinishedReading; | 487 std::unique_ptr<OnFinishedReading> m_onFinishedReading; |
489 Vector<char> m_data; | 488 Vector<char> m_data; |
490 }; | 489 }; |
491 | 490 |
492 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread | 491 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread |
493 // where T is one of HandleReader and HandleTwophaseReader. | 492 // where T is one of HandleReader and HandleTwophaseReader. |
494 template <typename T> | 493 template <typename T> |
495 class HandleReaderRunner final { | 494 class HandleReaderRunner final { |
496 STACK_ALLOCATED(); | 495 STACK_ALLOCATED(); |
497 public: | 496 public: |
498 explicit HandleReaderRunner(PassOwnPtr<WebDataConsumerHandle> handle) | 497 explicit HandleReaderRunner(std::unique_ptr<WebDataConsumerHandle> handl
e) |
499 : m_thread(adoptPtr(new Thread("reading thread"))) | 498 : m_thread(wrapUnique(new Thread("reading thread"))) |
500 , m_event(adoptPtr(new WaitableEvent())) | 499 , m_event(wrapUnique(new WaitableEvent())) |
501 , m_isDone(false) | 500 , m_isDone(false) |
502 { | 501 { |
503 m_thread->thread()->postTask(BLINK_FROM_HERE, threadSafeBind(&Handle
ReaderRunner::start, AllowCrossThreadAccess(this), passed(std::move(handle)))); | 502 m_thread->thread()->postTask(BLINK_FROM_HERE, threadSafeBind(&Handle
ReaderRunner::start, AllowCrossThreadAccess(this), passed(std::move(handle)))); |
504 } | 503 } |
505 ~HandleReaderRunner() | 504 ~HandleReaderRunner() |
506 { | 505 { |
507 wait(); | 506 wait(); |
508 } | 507 } |
509 | 508 |
510 PassOwnPtr<HandleReadResult> wait() | 509 std::unique_ptr<HandleReadResult> wait() |
511 { | 510 { |
512 if (m_isDone) | 511 if (m_isDone) |
513 return nullptr; | 512 return nullptr; |
514 m_event->wait(); | 513 m_event->wait(); |
515 m_isDone = true; | 514 m_isDone = true; |
516 return std::move(m_result); | 515 return std::move(m_result); |
517 } | 516 } |
518 | 517 |
519 private: | 518 private: |
520 void start(PassOwnPtr<WebDataConsumerHandle> handle) | 519 void start(std::unique_ptr<WebDataConsumerHandle> handle) |
521 { | 520 { |
522 m_handleReader = adoptPtr(new T(std::move(handle), bind<PassOwnPtr<H
andleReadResult>>(&HandleReaderRunner::onFinished, this))); | 521 m_handleReader = wrapUnique(new T(std::move(handle), WTF::bind<std::
unique_ptr<HandleReadResult>>(&HandleReaderRunner::onFinished, this))); |
523 } | 522 } |
524 | 523 |
525 void onFinished(PassOwnPtr<HandleReadResult> result) | 524 void onFinished(std::unique_ptr<HandleReadResult> result) |
526 { | 525 { |
527 m_handleReader = nullptr; | 526 m_handleReader = nullptr; |
528 m_result = std::move(result); | 527 m_result = std::move(result); |
529 m_event->signal(); | 528 m_event->signal(); |
530 } | 529 } |
531 | 530 |
532 OwnPtr<Thread> m_thread; | 531 std::unique_ptr<Thread> m_thread; |
533 OwnPtr<WaitableEvent> m_event; | 532 std::unique_ptr<WaitableEvent> m_event; |
534 OwnPtr<HandleReadResult> m_result; | 533 std::unique_ptr<HandleReadResult> m_result; |
535 bool m_isDone; | 534 bool m_isDone; |
536 | 535 |
537 OwnPtr<T> m_handleReader; | 536 std::unique_ptr<T> m_handleReader; |
538 }; | 537 }; |
539 }; | 538 }; |
540 | 539 |
541 } // namespace blink | 540 } // namespace blink |
542 | 541 |
543 #endif // DataConsumerHandleTestUtil_h | 542 #endif // DataConsumerHandleTestUtil_h |
OLD | NEW |