| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "content/public/test/test_browser_thread.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 BrowserThreadImpl::CleanUp(); | 33 BrowserThreadImpl::CleanUp(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 std::unique_ptr<NotificationService> notification_service_; | 37 std::unique_ptr<NotificationService> notification_service_; |
| 38 | 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadImpl); | 39 DISALLOW_COPY_AND_ASSIGN(TestBrowserThreadImpl); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier) | 42 TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier) |
| 43 : impl_(new TestBrowserThreadImpl(identifier)) { | 43 : impl_(new TestBrowserThreadImpl(identifier)), identifier_(identifier) {} |
| 44 } | |
| 45 | 44 |
| 46 TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier, | 45 TestBrowserThread::TestBrowserThread(BrowserThread::ID identifier, |
| 47 base::MessageLoop* message_loop) | 46 base::MessageLoop* message_loop) |
| 48 : impl_(new TestBrowserThreadImpl(identifier, message_loop)) {} | 47 : impl_(new TestBrowserThreadImpl(identifier, message_loop)), |
| 48 identifier_(identifier) {} |
| 49 | 49 |
| 50 TestBrowserThread::~TestBrowserThread() { | 50 TestBrowserThread::~TestBrowserThread() { |
| 51 Stop(); | 51 // The upcoming BrowserThreadImpl::ResetGlobalsForTesting() call requires that |
| 52 // |impl_| have triggered the shutdown phase for its BrowserThread::ID. This |
| 53 // either happens when the thread is stopped (if real) or destroyed (when fake |
| 54 // -- i.e. using an externally provided MessageLoop). |
| 55 impl_.reset(); |
| 56 |
| 57 // Resets BrowserThreadImpl globals binding |identifier_| to |impl_|. This is |
| 58 // fine since the underlying MessageLoop has already been flushed and deleted |
| 59 // in Stop(). In the case of an externally provided MessageLoop however, this |
| 60 // means that TaskRunners obtained through |
| 61 // |BrowserThreadImpl::GetTaskRunnerForThread(identifier_)| will no longer |
| 62 // recognize their BrowserThreadImpl for RunsTasksOnCurrentThread(). This |
| 63 // happens most often when such verifications are made from |
| 64 // MessageLoop::DestructionObservers. Callers that care to work around that |
| 65 // should instead use this shutdown sequence: TestBrowserThread::Stop() -> |
| 66 // ~MessageLoop() -> ~TestBrowserThread(). |
| 67 BrowserThreadImpl::ResetGlobalsForTesting(identifier_); |
| 52 } | 68 } |
| 53 | 69 |
| 54 bool TestBrowserThread::Start() { | 70 bool TestBrowserThread::Start() { |
| 55 return impl_->Start(); | 71 return impl_->Start(); |
| 56 } | 72 } |
| 57 | 73 |
| 58 bool TestBrowserThread::StartAndWaitForTesting() { | 74 bool TestBrowserThread::StartAndWaitForTesting() { |
| 59 return impl_->StartAndWaitForTesting(); | 75 return impl_->StartAndWaitForTesting(); |
| 60 } | 76 } |
| 61 | 77 |
| 62 bool TestBrowserThread::StartIOThread() { | 78 bool TestBrowserThread::StartIOThread() { |
| 63 base::Thread::Options options; | 79 base::Thread::Options options; |
| 64 options.message_loop_type = base::MessageLoop::TYPE_IO; | 80 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 65 return impl_->StartWithOptions(options); | 81 return impl_->StartWithOptions(options); |
| 66 } | 82 } |
| 67 | 83 |
| 68 void TestBrowserThread::Stop() { | 84 void TestBrowserThread::Stop() { |
| 69 impl_->Stop(); | 85 impl_->Stop(); |
| 70 } | 86 } |
| 71 | 87 |
| 72 bool TestBrowserThread::IsRunning() { | 88 bool TestBrowserThread::IsRunning() { |
| 73 return impl_->IsRunning(); | 89 return impl_->IsRunning(); |
| 74 } | 90 } |
| 75 | 91 |
| 76 } // namespace content | 92 } // namespace content |
| OLD | NEW |