| 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/Task.h" | 14 #include "platform/Task.h" |
| 15 #include "platform/ThreadSafeFunctional.h" | 15 #include "platform/ThreadSafeFunctional.h" |
| 16 #include "platform/WaitableEvent.h" |
| 16 #include "platform/WebThreadSupportingGC.h" | 17 #include "platform/WebThreadSupportingGC.h" |
| 17 #include "platform/heap/Handle.h" | 18 #include "platform/heap/Handle.h" |
| 18 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
| 19 #include "public/platform/WebDataConsumerHandle.h" | 20 #include "public/platform/WebDataConsumerHandle.h" |
| 20 #include "public/platform/WebTraceLocation.h" | 21 #include "public/platform/WebTraceLocation.h" |
| 21 #include "public/platform/WebWaitableEvent.h" | |
| 22 #include "wtf/Deque.h" | 22 #include "wtf/Deque.h" |
| 23 #include "wtf/Locker.h" | 23 #include "wtf/Locker.h" |
| 24 #include "wtf/OwnPtr.h" | 24 #include "wtf/OwnPtr.h" |
| 25 #include "wtf/PassOwnPtr.h" | 25 #include "wtf/PassOwnPtr.h" |
| 26 #include "wtf/ThreadSafeRefCounted.h" | 26 #include "wtf/ThreadSafeRefCounted.h" |
| 27 #include "wtf/ThreadingPrimitives.h" | 27 #include "wtf/ThreadingPrimitives.h" |
| 28 #include "wtf/Vector.h" | 28 #include "wtf/Vector.h" |
| 29 | 29 |
| 30 #include <gmock/gmock.h> | 30 #include <gmock/gmock.h> |
| 31 #include <gtest/gtest.h> | 31 #include <gtest/gtest.h> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 ExecutionContext* executionContext() { return m_executionContext.get();
} | 65 ExecutionContext* executionContext() { return m_executionContext.get();
} |
| 66 ScriptState* scriptState() { return m_scriptState.get(); } | 66 ScriptState* scriptState() { return m_scriptState.get(); } |
| 67 v8::Isolate* isolate() { return m_isolateHolder->isolate(); } | 67 v8::Isolate* isolate() { return m_isolateHolder->isolate(); } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 void initialize(); | 70 void initialize(); |
| 71 void shutdown(); | 71 void shutdown(); |
| 72 | 72 |
| 73 OwnPtr<WebThreadSupportingGC> m_thread; | 73 OwnPtr<WebThreadSupportingGC> m_thread; |
| 74 const InitializationPolicy m_initializationPolicy; | 74 const InitializationPolicy m_initializationPolicy; |
| 75 OwnPtr<WebWaitableEvent> m_waitableEvent; | 75 OwnPtr<WaitableEvent> m_waitableEvent; |
| 76 RefPtrWillBePersistent<NullExecutionContext> m_executionContext; | 76 RefPtrWillBePersistent<NullExecutionContext> m_executionContext; |
| 77 OwnPtr<gin::IsolateHolder> m_isolateHolder; | 77 OwnPtr<gin::IsolateHolder> m_isolateHolder; |
| 78 RefPtr<ScriptState> m_scriptState; | 78 RefPtr<ScriptState> m_scriptState; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> { | 81 class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> { |
| 82 public: | 82 public: |
| 83 virtual ~ThreadingTestBase() { } | 83 virtual ~ThreadingTestBase() { } |
| 84 | 84 |
| 85 class ThreadHolder; | 85 class ThreadHolder; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 { | 236 { |
| 237 postTaskToUpdatingThread(location, task); | 237 postTaskToUpdatingThread(location, task); |
| 238 m_waitableEvent->wait(); | 238 m_waitableEvent->wait(); |
| 239 } | 239 } |
| 240 protected: | 240 protected: |
| 241 ThreadingTestBase() | 241 ThreadingTestBase() |
| 242 : m_context(Context::create()) { } | 242 : m_context(Context::create()) { } |
| 243 | 243 |
| 244 RefPtr<Context> m_context; | 244 RefPtr<Context> m_context; |
| 245 OwnPtr<WebDataConsumerHandle::Reader> m_reader; | 245 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| 246 OwnPtr<WebWaitableEvent> m_waitableEvent; | 246 OwnPtr<WaitableEvent> m_waitableEvent; |
| 247 NoopClient m_client; | 247 NoopClient m_client; |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 class ThreadingHandleNotificationTest : public ThreadingTestBase, public Web
DataConsumerHandle::Client { | 250 class ThreadingHandleNotificationTest : public ThreadingTestBase, public Web
DataConsumerHandle::Client { |
| 251 public: | 251 public: |
| 252 using Self = ThreadingHandleNotificationTest; | 252 using Self = ThreadingHandleNotificationTest; |
| 253 static PassRefPtr<Self> create() { return adoptRef(new Self); } | 253 static PassRefPtr<Self> create() { return adoptRef(new Self); } |
| 254 | 254 |
| 255 void run(PassOwnPtr<WebDataConsumerHandle> handle) | 255 void run(PassOwnPtr<WebDataConsumerHandle> handle) |
| 256 { | 256 { |
| 257 ThreadHolder holder(this); | 257 ThreadHolder holder(this); |
| 258 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent(
)); | 258 m_waitableEvent = adoptPtr(new WaitableEvent()); |
| 259 m_handle = handle; | 259 m_handle = handle; |
| 260 | 260 |
| 261 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, new Task(threadSafeB
ind(&Self::obtainReader, this))); | 261 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, new Task(threadSafeB
ind(&Self::obtainReader, this))); |
| 262 } | 262 } |
| 263 | 263 |
| 264 private: | 264 private: |
| 265 ThreadingHandleNotificationTest() = default; | 265 ThreadingHandleNotificationTest() = default; |
| 266 void obtainReader() | 266 void obtainReader() |
| 267 { | 267 { |
| 268 m_reader = m_handle->obtainReader(this); | 268 m_reader = m_handle->obtainReader(this); |
| 269 } | 269 } |
| 270 void didGetReadable() override | 270 void didGetReadable() override |
| 271 { | 271 { |
| 272 postTaskToReadingThread(BLINK_FROM_HERE, new Task(threadSafeBind(&Se
lf::resetReader, this))); | 272 postTaskToReadingThread(BLINK_FROM_HERE, new Task(threadSafeBind(&Se
lf::resetReader, this))); |
| 273 postTaskToReadingThread(BLINK_FROM_HERE, new Task(threadSafeBind(&Se
lf::signalDone, this))); | 273 postTaskToReadingThread(BLINK_FROM_HERE, new Task(threadSafeBind(&Se
lf::signalDone, this))); |
| 274 } | 274 } |
| 275 | 275 |
| 276 OwnPtr<WebDataConsumerHandle> m_handle; | 276 OwnPtr<WebDataConsumerHandle> m_handle; |
| 277 }; | 277 }; |
| 278 | 278 |
| 279 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W
ebDataConsumerHandle::Client { | 279 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W
ebDataConsumerHandle::Client { |
| 280 public: | 280 public: |
| 281 using Self = ThreadingHandleNoNotificationTest; | 281 using Self = ThreadingHandleNoNotificationTest; |
| 282 static PassRefPtr<Self> create() { return adoptRef(new Self); } | 282 static PassRefPtr<Self> create() { return adoptRef(new Self); } |
| 283 | 283 |
| 284 void run(PassOwnPtr<WebDataConsumerHandle> handle) | 284 void run(PassOwnPtr<WebDataConsumerHandle> handle) |
| 285 { | 285 { |
| 286 ThreadHolder holder(this); | 286 ThreadHolder holder(this); |
| 287 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent(
)); | 287 m_waitableEvent = adoptPtr(new WaitableEvent()); |
| 288 m_handle = handle; | 288 m_handle = handle; |
| 289 | 289 |
| 290 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, new Task(threadSafeB
ind(&Self::obtainReader, this))); | 290 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, new Task(threadSafeB
ind(&Self::obtainReader, this))); |
| 291 } | 291 } |
| 292 | 292 |
| 293 private: | 293 private: |
| 294 ThreadingHandleNoNotificationTest() = default; | 294 ThreadingHandleNoNotificationTest() = default; |
| 295 void obtainReader() | 295 void obtainReader() |
| 296 { | 296 { |
| 297 m_reader = m_handle->obtainReader(this); | 297 m_reader = m_handle->obtainReader(this); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 public: | 401 public: |
| 402 static PassRefPtr<Context> create() { return adoptRef(new Context);
} | 402 static PassRefPtr<Context> create() { return adoptRef(new Context);
} |
| 403 | 403 |
| 404 // This function cannot be called after creating a tee. | 404 // This function cannot be called after creating a tee. |
| 405 void add(const Command&); | 405 void add(const Command&); |
| 406 void attachReader(WebDataConsumerHandle::Client*); | 406 void attachReader(WebDataConsumerHandle::Client*); |
| 407 void detachReader(); | 407 void detachReader(); |
| 408 void detachHandle(); | 408 void detachHandle(); |
| 409 Result beginRead(const void** buffer, Flags, size_t* available); | 409 Result beginRead(const void** buffer, Flags, size_t* available); |
| 410 Result endRead(size_t readSize); | 410 Result endRead(size_t readSize); |
| 411 WebWaitableEvent* detached() { return m_detached.get(); } | 411 WaitableEvent* detached() { return m_detached.get(); } |
| 412 | 412 |
| 413 private: | 413 private: |
| 414 Context(); | 414 Context(); |
| 415 bool isEmpty() const { return m_commands.isEmpty(); } | 415 bool isEmpty() const { return m_commands.isEmpty(); } |
| 416 const Command& top(); | 416 const Command& top(); |
| 417 void consume(size_t); | 417 void consume(size_t); |
| 418 size_t offset() const { return m_offset; } | 418 size_t offset() const { return m_offset; } |
| 419 void notify(); | 419 void notify(); |
| 420 void notifyInternal(); | 420 void notifyInternal(); |
| 421 | 421 |
| 422 Deque<Command> m_commands; | 422 Deque<Command> m_commands; |
| 423 size_t m_offset; | 423 size_t m_offset; |
| 424 WebThread* m_readerThread; | 424 WebThread* m_readerThread; |
| 425 Client* m_client; | 425 Client* m_client; |
| 426 Result m_result; | 426 Result m_result; |
| 427 bool m_isHandleAttached; | 427 bool m_isHandleAttached; |
| 428 Mutex m_mutex; | 428 Mutex m_mutex; |
| 429 OwnPtr<WebWaitableEvent> m_detached; | 429 OwnPtr<WaitableEvent> m_detached; |
| 430 }; | 430 }; |
| 431 | 431 |
| 432 Context* context() { return m_context.get(); } | 432 Context* context() { return m_context.get(); } |
| 433 | 433 |
| 434 private: | 434 private: |
| 435 class ReaderImpl; | 435 class ReaderImpl; |
| 436 | 436 |
| 437 ReplayingHandle(); | 437 ReplayingHandle(); |
| 438 Reader* obtainReaderInternal(Client*) override; | 438 Reader* obtainReaderInternal(Client*) override; |
| 439 const char* debugName() const override { return "ReplayingHandle"; } | 439 const char* debugName() const override { return "ReplayingHandle"; } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 }; | 491 }; |
| 492 | 492 |
| 493 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread | 493 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread |
| 494 // where T is one of HandleReader and HandleTwophaseReader. | 494 // where T is one of HandleReader and HandleTwophaseReader. |
| 495 template <typename T> | 495 template <typename T> |
| 496 class HandleReaderRunner final { | 496 class HandleReaderRunner final { |
| 497 STACK_ALLOCATED(); | 497 STACK_ALLOCATED(); |
| 498 public: | 498 public: |
| 499 explicit HandleReaderRunner(PassOwnPtr<WebDataConsumerHandle> handle) | 499 explicit HandleReaderRunner(PassOwnPtr<WebDataConsumerHandle> handle) |
| 500 : m_thread(adoptPtr(new Thread("reading thread"))) | 500 : m_thread(adoptPtr(new Thread("reading thread"))) |
| 501 , m_event(adoptPtr(Platform::current()->createWaitableEvent())) | 501 , m_event(adoptPtr(new WaitableEvent())) |
| 502 , m_isDone(false) | 502 , m_isDone(false) |
| 503 { | 503 { |
| 504 m_thread->thread()->postTask(BLINK_FROM_HERE, new Task(threadSafeBin
d(&HandleReaderRunner::start, AllowCrossThreadAccess(this), handle))); | 504 m_thread->thread()->postTask(BLINK_FROM_HERE, new Task(threadSafeBin
d(&HandleReaderRunner::start, AllowCrossThreadAccess(this), handle))); |
| 505 } | 505 } |
| 506 ~HandleReaderRunner() | 506 ~HandleReaderRunner() |
| 507 { | 507 { |
| 508 wait(); | 508 wait(); |
| 509 } | 509 } |
| 510 | 510 |
| 511 PassOwnPtr<HandleReadResult> wait() | 511 PassOwnPtr<HandleReadResult> wait() |
| (...skipping 12 matching lines...) Expand all Loading... |
| 524 } | 524 } |
| 525 | 525 |
| 526 void onFinished(PassOwnPtr<HandleReadResult> result) | 526 void onFinished(PassOwnPtr<HandleReadResult> result) |
| 527 { | 527 { |
| 528 m_handleReader = nullptr; | 528 m_handleReader = nullptr; |
| 529 m_result = result; | 529 m_result = result; |
| 530 m_event->signal(); | 530 m_event->signal(); |
| 531 } | 531 } |
| 532 | 532 |
| 533 OwnPtr<Thread> m_thread; | 533 OwnPtr<Thread> m_thread; |
| 534 OwnPtr<WebWaitableEvent> m_event; | 534 OwnPtr<WaitableEvent> m_event; |
| 535 OwnPtr<HandleReadResult> m_result; | 535 OwnPtr<HandleReadResult> m_result; |
| 536 bool m_isDone; | 536 bool m_isDone; |
| 537 | 537 |
| 538 OwnPtr<T> m_handleReader; | 538 OwnPtr<T> m_handleReader; |
| 539 }; | 539 }; |
| 540 }; | 540 }; |
| 541 | 541 |
| 542 } // namespace blink | 542 } // namespace blink |
| 543 | 543 |
| 544 #endif // DataConsumerHandleTestUtil_h | 544 #endif // DataConsumerHandleTestUtil_h |
| OLD | NEW |