Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/DataConsumerHandleTestUtil.h

Issue 2177243002: Use per-frame TaskRunner instead of thread's default in DataConsumerHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@data_consumer_handle_unique_ptr
Patch Set: update Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // Creating an isolate in addition to GarbageCollection. 53 // Creating an isolate in addition to GarbageCollection.
54 ScriptExecution, 54 ScriptExecution,
55 // Creating an execution context in addition to ScriptExecution. 55 // Creating an execution context in addition to ScriptExecution.
56 WithExecutionContext, 56 WithExecutionContext,
57 }; 57 };
58 58
59 Thread(const char* name, InitializationPolicy = GarbageCollection); 59 Thread(const char* name, InitializationPolicy = GarbageCollection);
60 ~Thread(); 60 ~Thread();
61 61
62 WebThreadSupportingGC* thread() { return m_thread.get(); } 62 WebThreadSupportingGC* thread() { return m_thread.get(); }
63 WebTaskRunner* taskRunner() { return m_thread->platformThread().getWebTa skRunner(); }
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 std::unique_ptr<WebThreadSupportingGC> m_thread;
72 const InitializationPolicy m_initializationPolicy; 73 const InitializationPolicy m_initializationPolicy;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 ASSERT(m_holder); 121 ASSERT(m_holder);
121 m_holder->readingThread()->postTask(location, std::move(task)); 122 m_holder->readingThread()->postTask(location, std::move(task));
122 } 123 }
123 void postTaskToUpdatingThread(const WebTraceLocation& location, std: :unique_ptr<CrossThreadClosure> task) 124 void postTaskToUpdatingThread(const WebTraceLocation& location, std: :unique_ptr<CrossThreadClosure> task)
124 { 125 {
125 MutexLocker locker(m_holderMutex); 126 MutexLocker locker(m_holderMutex);
126 ASSERT(m_holder); 127 ASSERT(m_holder);
127 m_holder->updatingThread()->postTask(location, std::move(task)); 128 m_holder->updatingThread()->postTask(location, std::move(task));
128 } 129 }
129 130
131 WebTaskRunner* getReadingThreadTaskRunner()
132 {
133 return m_holder->readingThread()->platformThread().getWebTaskRun ner();
134 }
135
130 private: 136 private:
131 Context() 137 Context()
132 : m_holder(nullptr) { } 138 : m_holder(nullptr) { }
133 String currentThreadName() 139 String currentThreadName()
134 { 140 {
135 MutexLocker locker(m_holderMutex); 141 MutexLocker locker(m_holderMutex);
136 if (m_holder) { 142 if (m_holder) {
137 if (m_holder->readingThread()->isCurrentThread()) 143 if (m_holder->readingThread()->isCurrentThread())
138 return "the reading thread"; 144 return "the reading thread";
139 if (m_holder->updatingThread()->isCurrentThread()) 145 if (m_holder->updatingThread()->isCurrentThread())
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 USING_FAST_MALLOC(DataConsumerHandle); 206 USING_FAST_MALLOC(DataConsumerHandle);
201 public: 207 public:
202 static std::unique_ptr<WebDataConsumerHandle> create(const String& n ame, PassRefPtr<Context> context) 208 static std::unique_ptr<WebDataConsumerHandle> create(const String& n ame, PassRefPtr<Context> context)
203 { 209 {
204 return wrapUnique(new DataConsumerHandle(name, context)); 210 return wrapUnique(new DataConsumerHandle(name, context));
205 } 211 }
206 212
207 private: 213 private:
208 DataConsumerHandle(const String& name, PassRefPtr<Context> context) : m_name(name.isolatedCopy()), m_context(context) { } 214 DataConsumerHandle(const String& name, PassRefPtr<Context> context) : m_name(name.isolatedCopy()), m_context(context) { }
209 215
210 std::unique_ptr<Reader> obtainReader(Client*) { return WTF::wrapUniq ue(new ReaderImpl(m_name, m_context)); } 216 std::unique_ptr<Reader> obtainReader(Client*, std::unique_ptr<WebTas kRunner>)
217 {
218 return WTF::wrapUnique(new ReaderImpl(m_name, m_context));
219 }
211 const char* debugName() const override { return "ThreadingTestBase:: DataConsumerHandle"; } 220 const char* debugName() const override { return "ThreadingTestBase:: DataConsumerHandle"; }
212 221
213 const String m_name; 222 const String m_name;
214 RefPtr<Context> m_context; 223 RefPtr<Context> m_context;
215 }; 224 };
216 225
217 void resetReader() { m_reader = nullptr; } 226 void resetReader() { m_reader = nullptr; }
218 void signalDone() { m_waitableEvent->signal(); } 227 void signalDone() { m_waitableEvent->signal(); }
219 const String& result() { return m_context->result(); } 228 const String& result() { return m_context->result(); }
220 void postTaskToReadingThread(const WebTraceLocation& location, std::uniq ue_ptr<CrossThreadClosure> task) 229 void postTaskToReadingThread(const WebTraceLocation& location, std::uniq ue_ptr<CrossThreadClosure> task)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 m_waitableEvent = wrapUnique(new WaitableEvent()); 265 m_waitableEvent = wrapUnique(new WaitableEvent());
257 m_handle = std::move(handle); 266 m_handle = std::move(handle);
258 267
259 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, crossThreadBind(&Sel f::obtainReader, wrapPassRefPtr(this))); 268 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, crossThreadBind(&Sel f::obtainReader, wrapPassRefPtr(this)));
260 } 269 }
261 270
262 private: 271 private:
263 ThreadingHandleNotificationTest() = default; 272 ThreadingHandleNotificationTest() = default;
264 void obtainReader() 273 void obtainReader()
265 { 274 {
266 m_reader = m_handle->obtainReader(this); 275 m_reader = m_handle->obtainReader(this, m_context->getReadingThreadT askRunner()->clone());
267 } 276 }
268 void didGetReadable() override 277 void didGetReadable() override
269 { 278 {
270 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::rese tReader, wrapPassRefPtr(this))); 279 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::rese tReader, wrapPassRefPtr(this)));
271 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::sign alDone, wrapPassRefPtr(this))); 280 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::sign alDone, wrapPassRefPtr(this)));
272 } 281 }
273 282
274 std::unique_ptr<WebDataConsumerHandle> m_handle; 283 std::unique_ptr<WebDataConsumerHandle> m_handle;
275 }; 284 };
276 285
277 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W ebDataConsumerHandle::Client { 286 class ThreadingHandleNoNotificationTest : public ThreadingTestBase, public W ebDataConsumerHandle::Client {
278 public: 287 public:
279 using Self = ThreadingHandleNoNotificationTest; 288 using Self = ThreadingHandleNoNotificationTest;
280 static PassRefPtr<Self> create() { return adoptRef(new Self); } 289 static PassRefPtr<Self> create() { return adoptRef(new Self); }
281 290
282 void run(std::unique_ptr<WebDataConsumerHandle> handle) 291 void run(std::unique_ptr<WebDataConsumerHandle> handle)
283 { 292 {
284 ThreadHolder holder(this); 293 ThreadHolder holder(this);
285 m_waitableEvent = wrapUnique(new WaitableEvent()); 294 m_waitableEvent = wrapUnique(new WaitableEvent());
286 m_handle = std::move(handle); 295 m_handle = std::move(handle);
287 296
288 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, crossThreadBind(&Sel f::obtainReader, wrapPassRefPtr(this))); 297 postTaskToReadingThreadAndWait(BLINK_FROM_HERE, crossThreadBind(&Sel f::obtainReader, wrapPassRefPtr(this)));
289 } 298 }
290 299
291 private: 300 private:
292 ThreadingHandleNoNotificationTest() = default; 301 ThreadingHandleNoNotificationTest() = default;
293 void obtainReader() 302 void obtainReader()
294 { 303 {
295 m_reader = m_handle->obtainReader(this); 304 m_reader = m_handle->obtainReader(this, m_context->getReadingThreadT askRunner()->clone());
296 m_reader = nullptr; 305 m_reader = nullptr;
297 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::sign alDone, wrapPassRefPtr(this))); 306 postTaskToReadingThread(BLINK_FROM_HERE, crossThreadBind(&Self::sign alDone, wrapPassRefPtr(this)));
298 } 307 }
299 void didGetReadable() override 308 void didGetReadable() override
300 { 309 {
301 ASSERT_NOT_REACHED(); 310 ASSERT_NOT_REACHED();
302 } 311 }
303 312
304 std::unique_ptr<WebDataConsumerHandle> m_handle; 313 std::unique_ptr<WebDataConsumerHandle> m_handle;
305 }; 314 };
306 315
307 class MockFetchDataConsumerHandle : public FetchDataConsumerHandle { 316 class MockFetchDataConsumerHandle : public FetchDataConsumerHandle {
308 public: 317 public:
309 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerHandle >> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerH andle>); } 318 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerHandle >> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerH andle>); }
310 MOCK_METHOD1(obtainFetchDataReader, std::unique_ptr<Reader>(Client*)); 319 MOCK_METHOD1(obtainFetchDataReaderMock, std::unique_ptr<Reader>(Client*) );
320
321 std::unique_ptr<Reader> obtainFetchDataReader(Client* client, std::uniqu e_ptr<WebTaskRunner>)
322 {
323 return obtainFetchDataReaderMock(client);
324 }
311 325
312 private: 326 private:
313 const char* debugName() const override { return "MockFetchDataConsumerHa ndle"; } 327 const char* debugName() const override { return "MockFetchDataConsumerHa ndle"; }
314 }; 328 };
315 329
316 class MockFetchDataConsumerReader : public FetchDataConsumerHandle::Reader { 330 class MockFetchDataConsumerReader : public FetchDataConsumerHandle::Reader {
317 public: 331 public:
318 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerReader >> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerR eader>); } 332 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerReader >> create() { return wrapUnique(new ::testing::StrictMock<MockFetchDataConsumerR eader>); }
319 333
320 using Result = WebDataConsumerHandle::Result; 334 using Result = WebDataConsumerHandle::Result;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 // creator thread. This function must be called BEFORE any reader is 408 // creator thread. This function must be called BEFORE any reader is
395 // obtained. 409 // obtained.
396 void add(const Command&); 410 void add(const Command&);
397 411
398 class Context final : public ThreadSafeRefCounted<Context> { 412 class Context final : public ThreadSafeRefCounted<Context> {
399 public: 413 public:
400 static PassRefPtr<Context> create() { return adoptRef(new Context); } 414 static PassRefPtr<Context> create() { return adoptRef(new Context); }
401 415
402 // This function cannot be called after creating a tee. 416 // This function cannot be called after creating a tee.
403 void add(const Command&); 417 void add(const Command&);
404 void attachReader(WebDataConsumerHandle::Client*); 418 void attachReader(WebDataConsumerHandle::Client*, std::unique_ptr<We bTaskRunner>);
405 void detachReader(); 419 void detachReader();
406 void detachHandle(); 420 void detachHandle();
407 Result beginRead(const void** buffer, Flags, size_t* available); 421 Result beginRead(const void** buffer, Flags, size_t* available);
408 Result endRead(size_t readSize); 422 Result endRead(size_t readSize);
409 WaitableEvent* detached() { return m_detached.get(); } 423 WaitableEvent* detached() { return m_detached.get(); }
410 424
411 private: 425 private:
412 Context(); 426 Context();
413 bool isEmpty() const { return m_commands.isEmpty(); } 427 bool isEmpty() const { return m_commands.isEmpty(); }
414 const Command& top(); 428 const Command& top();
415 void consume(size_t); 429 void consume(size_t);
416 size_t offset() const { return m_offset; } 430 size_t offset() const { return m_offset; }
417 void notify(); 431 void notify();
418 void notifyInternal(); 432 void notifyInternal();
419 433
420 Deque<Command> m_commands; 434 Deque<Command> m_commands;
421 size_t m_offset; 435 size_t m_offset;
422 WebThread* m_readerThread; 436 std::unique_ptr<WebTaskRunner> m_readerTaskRunner;
423 Client* m_client; 437 Client* m_client;
424 Result m_result; 438 Result m_result;
425 bool m_isHandleAttached; 439 bool m_isHandleAttached;
426 Mutex m_mutex; 440 Mutex m_mutex;
427 std::unique_ptr<WaitableEvent> m_detached; 441 std::unique_ptr<WaitableEvent> m_detached;
428 }; 442 };
429 443
430 Context* getContext() { return m_context.get(); } 444 Context* getContext() { return m_context.get(); }
431 std::unique_ptr<Reader> obtainReader(Client*) override; 445 std::unique_ptr<Reader> obtainReader(Client*, std::unique_ptr<WebTaskRun ner>) override;
432 446
433 private: 447 private:
434 class ReaderImpl; 448 class ReaderImpl;
435 449
436 ReplayingHandle(); 450 ReplayingHandle();
437 const char* debugName() const override { return "ReplayingHandle"; } 451 const char* debugName() const override { return "ReplayingHandle"; }
438 452
439 RefPtr<Context> m_context; 453 RefPtr<Context> m_context;
440 }; 454 };
441 455
(...skipping 10 matching lines...) Expand all
452 }; 466 };
453 467
454 // HandleReader reads all data from the given WebDataConsumerHandle using 468 // HandleReader reads all data from the given WebDataConsumerHandle using
455 // Reader::read on the thread on which it is created. When reading is done 469 // 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. 470 // or failed, it calls the given callback with the result.
457 class HandleReader final : public WebDataConsumerHandle::Client { 471 class HandleReader final : public WebDataConsumerHandle::Client {
458 USING_FAST_MALLOC(HandleReader); 472 USING_FAST_MALLOC(HandleReader);
459 public: 473 public:
460 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR esult>)>; 474 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR esult>)>;
461 475
462 HandleReader(std::unique_ptr<WebDataConsumerHandle>, std::unique_ptr<OnF inishedReading>); 476 HandleReader(std::unique_ptr<WebDataConsumerHandle>, std::unique_ptr<OnF inishedReading>, std::unique_ptr<WebTaskRunner> readerTaskRunner);
463 void didGetReadable() override; 477 void didGetReadable() override;
464 478
465 private: 479 private:
466 void runOnFinishedReading(std::unique_ptr<HandleReadResult>); 480 void runOnFinishedReading(std::unique_ptr<HandleReadResult>);
467 481
482 std::unique_ptr<WebTaskRunner> m_readerTaskRunner;
468 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; 483 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader;
469 std::unique_ptr<OnFinishedReading> m_onFinishedReading; 484 std::unique_ptr<OnFinishedReading> m_onFinishedReading;
470 Vector<char> m_data; 485 Vector<char> m_data;
471 }; 486 };
472 487
473 // HandleTwoPhaseReader does the same as HandleReader, but it uses 488 // HandleTwoPhaseReader does the same as HandleReader, but it uses
474 // |beginRead| / |endRead| instead of |read|. 489 // |beginRead| / |endRead| instead of |read|.
475 class HandleTwoPhaseReader final : public WebDataConsumerHandle::Client { 490 class HandleTwoPhaseReader final : public WebDataConsumerHandle::Client {
476 USING_FAST_MALLOC(HandleTwoPhaseReader); 491 USING_FAST_MALLOC(HandleTwoPhaseReader);
477 public: 492 public:
478 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR esult>)>; 493 using OnFinishedReading = WTF::Function<void(std::unique_ptr<HandleReadR esult>)>;
479 494
480 HandleTwoPhaseReader(std::unique_ptr<WebDataConsumerHandle>, std::unique _ptr<OnFinishedReading>); 495 HandleTwoPhaseReader(std::unique_ptr<WebDataConsumerHandle>, std::unique _ptr<OnFinishedReading>, std::unique_ptr<WebTaskRunner>);
481 void didGetReadable() override; 496 void didGetReadable() override;
482 497
483 private: 498 private:
484 void runOnFinishedReading(std::unique_ptr<HandleReadResult>); 499 void runOnFinishedReading(std::unique_ptr<HandleReadResult>);
485 500
501 std::unique_ptr<WebTaskRunner> m_readerTaskRunner;
486 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader; 502 std::unique_ptr<WebDataConsumerHandle::Reader> m_reader;
487 std::unique_ptr<OnFinishedReading> m_onFinishedReading; 503 std::unique_ptr<OnFinishedReading> m_onFinishedReading;
488 Vector<char> m_data; 504 Vector<char> m_data;
489 }; 505 };
490 506
491 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread 507 // HandleReaderRunner<T> creates a dedicated thread and run T on the thread
492 // where T is one of HandleReader and HandleTwophaseReader. 508 // where T is one of HandleReader and HandleTwophaseReader.
493 template <typename T> 509 template <typename T>
494 class HandleReaderRunner final { 510 class HandleReaderRunner final {
495 STACK_ALLOCATED(); 511 STACK_ALLOCATED();
496 public: 512 public:
497 explicit HandleReaderRunner(std::unique_ptr<WebDataConsumerHandle> handl e) 513 explicit HandleReaderRunner(std::unique_ptr<WebDataConsumerHandle> handl e)
498 : m_thread(wrapUnique(new Thread("reading thread"))) 514 : m_thread(wrapUnique(new Thread("reading thread")))
499 , m_event(wrapUnique(new WaitableEvent())) 515 , m_event(wrapUnique(new WaitableEvent()))
500 , m_isDone(false) 516 , m_isDone(false)
501 { 517 {
502 m_thread->thread()->postTask(BLINK_FROM_HERE, crossThreadBind(&Handl eReaderRunner::start, crossThreadUnretained(this), passed(std::move(handle)))); 518 m_thread->thread()->postTask(BLINK_FROM_HERE, crossThreadBind(&Handl eReaderRunner::start, crossThreadUnretained(this), passed(std::move(handle)), pa ssed(m_thread->taskRunner()->clone())));
503 } 519 }
504 ~HandleReaderRunner() 520 ~HandleReaderRunner()
505 { 521 {
506 wait(); 522 wait();
507 } 523 }
508 524
509 std::unique_ptr<HandleReadResult> wait() 525 std::unique_ptr<HandleReadResult> wait()
510 { 526 {
511 if (m_isDone) 527 if (m_isDone)
512 return nullptr; 528 return nullptr;
513 m_event->wait(); 529 m_event->wait();
514 m_isDone = true; 530 m_isDone = true;
515 return std::move(m_result); 531 return std::move(m_result);
516 } 532 }
517 533
518 private: 534 private:
519 void start(std::unique_ptr<WebDataConsumerHandle> handle) 535 void start(std::unique_ptr<WebDataConsumerHandle> handle, std::unique_pt r<WebTaskRunner> readerTaskRunner)
520 { 536 {
521 m_handleReader = wrapUnique(new T(std::move(handle), WTF::bind(&Hand leReaderRunner::onFinished, WTF::unretained(this)))); 537 m_handleReader = wrapUnique(new T(std::move(handle), WTF::bind(&Hand leReaderRunner::onFinished, WTF::unretained(this)), std::move(readerTaskRunner)) );
522 } 538 }
523 539
524 void onFinished(std::unique_ptr<HandleReadResult> result) 540 void onFinished(std::unique_ptr<HandleReadResult> result)
525 { 541 {
526 m_handleReader = nullptr; 542 m_handleReader = nullptr;
527 m_result = std::move(result); 543 m_result = std::move(result);
528 m_event->signal(); 544 m_event->signal();
529 } 545 }
530 546
531 std::unique_ptr<Thread> m_thread; 547 std::unique_ptr<Thread> m_thread;
532 std::unique_ptr<WaitableEvent> m_event; 548 std::unique_ptr<WaitableEvent> m_event;
533 std::unique_ptr<HandleReadResult> m_result; 549 std::unique_ptr<HandleReadResult> m_result;
534 bool m_isDone; 550 bool m_isDone;
535 551
536 std::unique_ptr<T> m_handleReader; 552 std::unique_ptr<T> m_handleReader;
537 }; 553 };
538 }; 554 };
539 555
556 std::unique_ptr<WebTaskRunner> getCurrentTaskRunner();
557
540 } // namespace blink 558 } // namespace blink
541 559
542 #endif // DataConsumerHandleTestUtil_h 560 #endif // DataConsumerHandleTestUtil_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698