Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/public/test/test_browser_thread_bundle.h" | 5 #include "content/public/test/test_browser_thread_bundle.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/test/scoped_task_scheduler.h" | 10 #include "base/test/scoped_task_scheduler.h" |
| 11 #include "content/browser/browser_thread_impl.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 12 #include "content/public/test/test_browser_thread.h" | 12 #include "content/public/test/test_browser_thread.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 TestBrowserThreadBundle::TestBrowserThreadBundle() | 16 TestBrowserThreadBundle::TestBrowserThreadBundle() |
| 17 : TestBrowserThreadBundle(DEFAULT) {} | 17 : TestBrowserThreadBundle(DEFAULT) {} |
| 18 | 18 |
| 19 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) | 19 TestBrowserThreadBundle::TestBrowserThreadBundle(int options) |
| 20 : options_(options), threads_started_(false) { | 20 : options_(options), threads_created_(false) { |
| 21 Init(); | 21 Init(); |
| 22 } | 22 } |
| 23 | 23 |
| 24 TestBrowserThreadBundle::~TestBrowserThreadBundle() { | 24 TestBrowserThreadBundle::~TestBrowserThreadBundle() { |
| 25 DCHECK(threads_created_); | |
| 26 | |
| 25 // To avoid memory leaks, we must ensure that any tasks posted to the blocking | 27 // To avoid memory leaks, we must ensure that any tasks posted to the blocking |
| 26 // pool via PostTaskAndReply are able to reply back to the originating thread. | 28 // pool via PostTaskAndReply are able to reply back to the originating thread. |
| 27 // Thus we must flush the blocking pool while the browser threads still exist. | 29 // Thus we must flush the blocking pool while the browser threads still exist. |
| 28 base::RunLoop().RunUntilIdle(); | 30 base::RunLoop().RunUntilIdle(); |
| 29 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); | 31 BrowserThreadImpl::FlushThreadPoolHelperForTesting(); |
| 30 | 32 |
| 31 // To ensure a clean teardown, each thread's message loop must be flushed | 33 // To ensure a clean teardown, each thread's message loop must be flushed |
| 32 // just before the thread is destroyed. But stopping a fake thread does not | 34 // just before the thread is destroyed. But stopping a fake thread does not |
| 33 // automatically flush the message loop, so we have to do it manually. | 35 // automatically flush the message loop, so we have to do it manually. |
| 34 // See http://crbug.com/247525 for discussion. | 36 // See http://crbug.com/247525 for discussion. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 58 // |message_loop_| needs to explicitly go away before fake threads in order | 60 // |message_loop_| needs to explicitly go away before fake threads in order |
| 59 // for DestructionObservers hooked to |message_loop_| to be able to invoke | 61 // for DestructionObservers hooked to |message_loop_| to be able to invoke |
| 60 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). | 62 // BrowserThread::CurrentlyOn() -- ref. ~TestBrowserThread(). |
| 61 CHECK(message_loop_->IsIdleForTesting()); | 63 CHECK(message_loop_->IsIdleForTesting()); |
| 62 message_loop_.reset(); | 64 message_loop_.reset(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 void TestBrowserThreadBundle::Init() { | 67 void TestBrowserThreadBundle::Init() { |
| 66 // Check for conflicting options can't have two IO threads. | 68 // Check for conflicting options can't have two IO threads. |
| 67 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); | 69 CHECK(!(options_ & IO_MAINLOOP) || !(options_ & REAL_IO_THREAD)); |
| 68 // There must be a thread to start to use DONT_START_THREADS | 70 // There must be a thread to start to use DONT_CREATE_THREADS |
| 69 CHECK((options_ & ~IO_MAINLOOP) != DONT_START_THREADS); | 71 CHECK((options_ & ~IO_MAINLOOP) != DONT_CREATE_THREADS); |
| 70 | 72 |
| 71 if (options_ & IO_MAINLOOP) { | 73 if (options_ & IO_MAINLOOP) { |
| 72 message_loop_.reset(new base::MessageLoopForIO()); | 74 message_loop_.reset(new base::MessageLoopForIO()); |
| 73 } else { | 75 } else { |
| 74 message_loop_.reset(new base::MessageLoopForUI()); | 76 message_loop_.reset(new base::MessageLoopForUI()); |
| 75 } | 77 } |
| 76 | 78 |
| 79 ui_thread_.reset( | |
| 80 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); | |
|
gab
2017/01/12 19:47:35
Why is the UI thread unconditionally created?
fdoray
2017/01/12 19:59:10
TestBrowserThreadBundle::CreateThreads() mimics Br
gab
2017/01/12 20:13:27
I see, then add a comment to that effect here and
fdoray
2017/01/12 21:02:56
Done.
| |
| 81 | |
| 82 if (!(options_ & DONT_CREATE_THREADS)) | |
| 83 CreateThreads(); | |
| 84 } | |
| 85 | |
| 86 void TestBrowserThreadBundle::CreateThreads() { | |
| 87 DCHECK(!threads_created_); | |
| 88 | |
| 77 task_scheduler_.reset( | 89 task_scheduler_.reset( |
| 78 new base::test::ScopedTaskScheduler(message_loop_.get())); | 90 new base::test::ScopedTaskScheduler(message_loop_.get())); |
| 79 | 91 |
| 80 ui_thread_.reset( | |
| 81 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); | |
| 82 | |
| 83 if (options_ & REAL_DB_THREAD) { | 92 if (options_ & REAL_DB_THREAD) { |
| 84 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); | 93 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); |
| 94 db_thread_->Start(); | |
| 85 } else { | 95 } else { |
| 86 db_thread_.reset( | 96 db_thread_.reset( |
| 87 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); | 97 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); |
| 88 } | 98 } |
| 89 | 99 |
| 90 if (options_ & REAL_FILE_THREAD) { | 100 if (options_ & REAL_FILE_THREAD) { |
| 91 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); | 101 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); |
| 102 file_thread_->Start(); | |
| 92 } else { | 103 } else { |
| 93 file_thread_.reset( | 104 file_thread_.reset( |
| 94 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); | 105 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); |
| 95 } | 106 } |
| 96 | 107 |
| 97 file_user_blocking_thread_.reset(new TestBrowserThread( | 108 file_user_blocking_thread_.reset(new TestBrowserThread( |
| 98 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); | 109 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); |
| 99 process_launcher_thread_.reset(new TestBrowserThread( | 110 process_launcher_thread_.reset(new TestBrowserThread( |
| 100 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); | 111 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); |
| 101 cache_thread_.reset( | 112 cache_thread_.reset( |
| 102 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); | 113 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); |
| 103 | 114 |
| 104 if (options_ & REAL_IO_THREAD) { | 115 if (options_ & REAL_IO_THREAD) { |
| 105 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); | 116 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); |
| 117 io_thread_->StartIOThread(); | |
| 106 } else { | 118 } else { |
| 107 io_thread_.reset( | 119 io_thread_.reset( |
| 108 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); | 120 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); |
| 109 } | 121 } |
| 110 | 122 |
| 111 if (!(options_ & DONT_START_THREADS)) | 123 threads_created_ = true; |
| 112 Start(); | |
| 113 } | |
| 114 | |
| 115 void TestBrowserThreadBundle::Start() { | |
| 116 DCHECK(!threads_started_); | |
| 117 | |
| 118 if (options_ & REAL_DB_THREAD) | |
| 119 db_thread_->Start(); | |
| 120 | |
| 121 if (options_ & REAL_FILE_THREAD) | |
| 122 file_thread_->Start(); | |
| 123 | |
| 124 if (options_ & REAL_IO_THREAD) | |
| 125 io_thread_->StartIOThread(); | |
| 126 | |
| 127 threads_started_ = true; | |
| 128 } | 124 } |
| 129 | 125 |
| 130 } // namespace content | 126 } // namespace content |
| OLD | NEW |