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