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's globals so that |impl_| is no longer bound to |
| 58 // |identifier_|. This is fine since the underlying MessageLoop has already |
| 59 // been flushed and deleted in Stop(). In the case of an externally provided |
| 60 // MessageLoop however, this 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: |
| 66 // 1) TestBrowserThread::Stop() |
| 67 // 2) ~MessageLoop() |
| 68 // 3) ~TestBrowserThread() |
| 69 // (~TestBrowserThreadBundle() does this). |
| 70 BrowserThreadImpl::ResetGlobalsForTesting(identifier_); |
52 } | 71 } |
53 | 72 |
54 bool TestBrowserThread::Start() { | 73 bool TestBrowserThread::Start() { |
55 return impl_->Start(); | 74 return impl_->Start(); |
56 } | 75 } |
57 | 76 |
58 bool TestBrowserThread::StartAndWaitForTesting() { | 77 bool TestBrowserThread::StartAndWaitForTesting() { |
59 return impl_->StartAndWaitForTesting(); | 78 return impl_->StartAndWaitForTesting(); |
60 } | 79 } |
61 | 80 |
62 bool TestBrowserThread::StartIOThread() { | 81 bool TestBrowserThread::StartIOThread() { |
63 base::Thread::Options options; | 82 base::Thread::Options options; |
64 options.message_loop_type = base::MessageLoop::TYPE_IO; | 83 options.message_loop_type = base::MessageLoop::TYPE_IO; |
65 return impl_->StartWithOptions(options); | 84 return impl_->StartWithOptions(options); |
66 } | 85 } |
67 | 86 |
68 void TestBrowserThread::Stop() { | 87 void TestBrowserThread::Stop() { |
69 impl_->Stop(); | 88 impl_->Stop(); |
70 } | 89 } |
71 | 90 |
72 bool TestBrowserThread::IsRunning() { | 91 bool TestBrowserThread::IsRunning() { |
73 return impl_->IsRunning(); | 92 return impl_->IsRunning(); |
74 } | 93 } |
75 | 94 |
76 } // namespace content | 95 } // namespace content |
OLD | NEW |