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 | |
robliao
2016/12/08 01:57:17
How does the below set the binding from |identifie
gab
2016/12/08 18:49:41
I meant to say that it clears that very binding, r
| |
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() -> | |
robliao
2016/12/08 01:57:17
Nit: Might be easier to read this as a sequenced l
gab
2016/12/08 18:49:41
Done.
| |
66 // ~MessageLoop() -> ~TestBrowserThread() (~TestBrowserThreadBundle() does | |
67 // this). | |
68 BrowserThreadImpl::ResetGlobalsForTesting(identifier_); | |
52 } | 69 } |
53 | 70 |
54 bool TestBrowserThread::Start() { | 71 bool TestBrowserThread::Start() { |
55 return impl_->Start(); | 72 return impl_->Start(); |
56 } | 73 } |
57 | 74 |
58 bool TestBrowserThread::StartAndWaitForTesting() { | 75 bool TestBrowserThread::StartAndWaitForTesting() { |
59 return impl_->StartAndWaitForTesting(); | 76 return impl_->StartAndWaitForTesting(); |
60 } | 77 } |
61 | 78 |
62 bool TestBrowserThread::StartIOThread() { | 79 bool TestBrowserThread::StartIOThread() { |
63 base::Thread::Options options; | 80 base::Thread::Options options; |
64 options.message_loop_type = base::MessageLoop::TYPE_IO; | 81 options.message_loop_type = base::MessageLoop::TYPE_IO; |
65 return impl_->StartWithOptions(options); | 82 return impl_->StartWithOptions(options); |
66 } | 83 } |
67 | 84 |
68 void TestBrowserThread::Stop() { | 85 void TestBrowserThread::Stop() { |
69 impl_->Stop(); | 86 impl_->Stop(); |
70 } | 87 } |
71 | 88 |
72 bool TestBrowserThread::IsRunning() { | 89 bool TestBrowserThread::IsRunning() { |
73 return impl_->IsRunning(); | 90 return impl_->IsRunning(); |
74 } | 91 } |
75 | 92 |
76 } // namespace content | 93 } // namespace content |
OLD | NEW |