| 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 |
| 73 // Create the UI thread. In production, this work is done in |
| 74 // BrowserMainLoop::MainMessageLoopStart(). |
| 71 if (options_ & IO_MAINLOOP) { | 75 if (options_ & IO_MAINLOOP) { |
| 72 message_loop_.reset(new base::MessageLoopForIO()); | 76 message_loop_.reset(new base::MessageLoopForIO()); |
| 73 } else { | 77 } else { |
| 74 message_loop_.reset(new base::MessageLoopForUI()); | 78 message_loop_.reset(new base::MessageLoopForUI()); |
| 75 } | 79 } |
| 76 | 80 |
| 81 ui_thread_.reset( |
| 82 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); |
| 83 |
| 84 if (!(options_ & DONT_CREATE_THREADS)) |
| 85 CreateThreads(); |
| 86 } |
| 87 |
| 88 // This method mimics the work done in BrowserMainLoop::CreateThreads(). |
| 89 void TestBrowserThreadBundle::CreateThreads() { |
| 90 DCHECK(!threads_created_); |
| 91 |
| 77 task_scheduler_.reset( | 92 task_scheduler_.reset( |
| 78 new base::test::ScopedTaskScheduler(message_loop_.get())); | 93 new base::test::ScopedTaskScheduler(message_loop_.get())); |
| 79 | 94 |
| 80 ui_thread_.reset( | |
| 81 new TestBrowserThread(BrowserThread::UI, message_loop_.get())); | |
| 82 | |
| 83 if (options_ & REAL_DB_THREAD) { | 95 if (options_ & REAL_DB_THREAD) { |
| 84 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); | 96 db_thread_.reset(new TestBrowserThread(BrowserThread::DB)); |
| 97 db_thread_->Start(); |
| 85 } else { | 98 } else { |
| 86 db_thread_.reset( | 99 db_thread_.reset( |
| 87 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); | 100 new TestBrowserThread(BrowserThread::DB, message_loop_.get())); |
| 88 } | 101 } |
| 89 | 102 |
| 90 if (options_ & REAL_FILE_THREAD) { | 103 if (options_ & REAL_FILE_THREAD) { |
| 91 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); | 104 file_thread_.reset(new TestBrowserThread(BrowserThread::FILE)); |
| 105 file_thread_->Start(); |
| 92 } else { | 106 } else { |
| 93 file_thread_.reset( | 107 file_thread_.reset( |
| 94 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); | 108 new TestBrowserThread(BrowserThread::FILE, message_loop_.get())); |
| 95 } | 109 } |
| 96 | 110 |
| 97 file_user_blocking_thread_.reset(new TestBrowserThread( | 111 file_user_blocking_thread_.reset(new TestBrowserThread( |
| 98 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); | 112 BrowserThread::FILE_USER_BLOCKING, message_loop_.get())); |
| 99 process_launcher_thread_.reset(new TestBrowserThread( | 113 process_launcher_thread_.reset(new TestBrowserThread( |
| 100 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); | 114 BrowserThread::PROCESS_LAUNCHER, message_loop_.get())); |
| 101 cache_thread_.reset( | 115 cache_thread_.reset( |
| 102 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); | 116 new TestBrowserThread(BrowserThread::CACHE, message_loop_.get())); |
| 103 | 117 |
| 104 if (options_ & REAL_IO_THREAD) { | 118 if (options_ & REAL_IO_THREAD) { |
| 105 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); | 119 io_thread_.reset(new TestBrowserThread(BrowserThread::IO)); |
| 120 io_thread_->StartIOThread(); |
| 106 } else { | 121 } else { |
| 107 io_thread_.reset( | 122 io_thread_.reset( |
| 108 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); | 123 new TestBrowserThread(BrowserThread::IO, message_loop_.get())); |
| 109 } | 124 } |
| 110 | 125 |
| 111 if (!(options_ & DONT_START_THREADS)) | 126 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 } | 127 } |
| 129 | 128 |
| 130 } // namespace content | 129 } // namespace content |
| OLD | NEW |