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/browser/browser_thread_impl.h" | 5 #include "content/browser/browser_thread_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY | 24 "Chrome_WebSocketproxyThread", // WEB_SOCKET_PROXY |
25 #endif | 25 #endif |
26 }; | 26 }; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 namespace content { | 30 namespace content { |
31 | 31 |
32 base::Lock BrowserThreadImpl::lock_; | 32 base::Lock BrowserThreadImpl::lock_; |
33 | 33 |
34 BrowserThread* BrowserThreadImpl::browser_threads_[ID_COUNT]; | 34 BrowserThreadImpl* BrowserThreadImpl::browser_threads_[ID_COUNT]; |
35 | 35 |
36 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier) | 36 BrowserThreadImpl::BrowserThreadImpl(ID identifier) |
37 : BrowserThread(identifier) { | 37 : Thread(browser_thread_names[identifier]), |
| 38 identifier_(identifier) { |
| 39 Initialize(); |
38 } | 40 } |
39 | 41 |
40 BrowserThreadImpl::BrowserThreadImpl(BrowserThread::ID identifier, | 42 BrowserThreadImpl::BrowserThreadImpl(ID identifier, |
41 MessageLoop* message_loop) | 43 MessageLoop* message_loop) |
42 : BrowserThread(identifier, message_loop) { | 44 : Thread(message_loop->thread_name().c_str()), |
| 45 identifier_(identifier) { |
| 46 set_message_loop(message_loop); |
| 47 Initialize(); |
| 48 } |
| 49 |
| 50 void BrowserThreadImpl::Initialize() { |
| 51 base::AutoLock lock(lock_); |
| 52 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); |
| 53 DCHECK(browser_threads_[identifier_] == NULL); |
| 54 browser_threads_[identifier_] = this; |
43 } | 55 } |
44 | 56 |
45 BrowserThreadImpl::~BrowserThreadImpl() { | 57 BrowserThreadImpl::~BrowserThreadImpl() { |
| 58 // All Thread subclasses must call Stop() in the destructor. This is |
| 59 // doubly important here as various bits of code check they are on |
| 60 // the right BrowserThread. |
46 Stop(); | 61 Stop(); |
| 62 |
| 63 base::AutoLock lock(lock_); |
| 64 browser_threads_[identifier_] = NULL; |
| 65 #ifndef NDEBUG |
| 66 // Double check that the threads are ordered correctly in the enumeration. |
| 67 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { |
| 68 DCHECK(!browser_threads_[i]) << |
| 69 "Threads must be listed in the reverse order that they die"; |
| 70 } |
| 71 #endif |
47 } | 72 } |
48 | 73 |
49 // static | 74 // static |
50 bool BrowserThreadImpl::PostTaskHelper( | 75 bool BrowserThreadImpl::PostTaskHelper( |
51 BrowserThread::ID identifier, | 76 BrowserThread::ID identifier, |
52 const tracked_objects::Location& from_here, | 77 const tracked_objects::Location& from_here, |
53 Task* task, | 78 Task* task, |
54 int64 delay_ms, | 79 int64 delay_ms, |
55 bool nestable) { | 80 bool nestable) { |
56 DCHECK(identifier >= 0 && identifier < ID_COUNT); | 81 DCHECK(identifier >= 0 && identifier < ID_COUNT); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); | 143 message_loop->PostNonNestableDelayedTask(from_here, task, delay_ms); |
119 } | 144 } |
120 } | 145 } |
121 | 146 |
122 if (!guaranteed_to_outlive_target_thread) | 147 if (!guaranteed_to_outlive_target_thread) |
123 lock_.Release(); | 148 lock_.Release(); |
124 | 149 |
125 return !!message_loop; | 150 return !!message_loop; |
126 } | 151 } |
127 | 152 |
128 // TODO(joi): Remove | |
129 DeprecatedBrowserThread::DeprecatedBrowserThread(BrowserThread::ID identifier) | |
130 : BrowserThread(identifier) { | |
131 } | |
132 DeprecatedBrowserThread::DeprecatedBrowserThread(BrowserThread::ID identifier, | |
133 MessageLoop* message_loop) | |
134 : BrowserThread(identifier, message_loop) { | |
135 } | |
136 DeprecatedBrowserThread::~DeprecatedBrowserThread() { | |
137 Stop(); | |
138 } | |
139 | |
140 // An implementation of MessageLoopProxy to be used in conjunction | 153 // An implementation of MessageLoopProxy to be used in conjunction |
141 // with BrowserThread. | 154 // with BrowserThread. |
142 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { | 155 class BrowserThreadMessageLoopProxy : public base::MessageLoopProxy { |
143 public: | 156 public: |
144 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) | 157 explicit BrowserThreadMessageLoopProxy(BrowserThread::ID identifier) |
145 : id_(identifier) { | 158 : id_(identifier) { |
146 } | 159 } |
147 | 160 |
148 // MessageLoopProxy implementation. | 161 // MessageLoopProxy implementation. |
149 virtual bool PostTask(const tracked_objects::Location& from_here, | 162 virtual bool PostTask(const tracked_objects::Location& from_here, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 207 |
195 virtual bool BelongsToCurrentThread() { | 208 virtual bool BelongsToCurrentThread() { |
196 return BrowserThread::CurrentlyOn(id_); | 209 return BrowserThread::CurrentlyOn(id_); |
197 } | 210 } |
198 | 211 |
199 private: | 212 private: |
200 BrowserThread::ID id_; | 213 BrowserThread::ID id_; |
201 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); | 214 DISALLOW_COPY_AND_ASSIGN(BrowserThreadMessageLoopProxy); |
202 }; | 215 }; |
203 | 216 |
204 BrowserThread::BrowserThread(ID identifier) | |
205 : Thread(browser_thread_names[identifier]), | |
206 identifier_(identifier) { | |
207 Initialize(); | |
208 } | |
209 | |
210 BrowserThread::BrowserThread(ID identifier, | |
211 MessageLoop* message_loop) | |
212 : Thread(message_loop->thread_name().c_str()), | |
213 identifier_(identifier) { | |
214 set_message_loop(message_loop); | |
215 Initialize(); | |
216 } | |
217 | |
218 void BrowserThread::Initialize() { | |
219 base::AutoLock lock(BrowserThreadImpl::lock_); | |
220 DCHECK(identifier_ >= 0 && identifier_ < ID_COUNT); | |
221 DCHECK(BrowserThreadImpl::browser_threads_[identifier_] == NULL); | |
222 BrowserThreadImpl::browser_threads_[identifier_] = this; | |
223 } | |
224 | |
225 BrowserThread::~BrowserThread() { | |
226 // Stop the thread here, instead of the parent's class destructor. This is so | |
227 // that if there are pending tasks that run, code that checks that it's on the | |
228 // correct BrowserThread succeeds. | |
229 Stop(); | |
230 | |
231 base::AutoLock lock(BrowserThreadImpl::lock_); | |
232 BrowserThreadImpl::browser_threads_[identifier_] = NULL; | |
233 #ifndef NDEBUG | |
234 // Double check that the threads are ordered correctly in the enumeration. | |
235 for (int i = identifier_ + 1; i < ID_COUNT; ++i) { | |
236 DCHECK(!BrowserThreadImpl::browser_threads_[i]) << | |
237 "Threads must be listed in the reverse order that they die"; | |
238 } | |
239 #endif | |
240 } | |
241 | |
242 // static | 217 // static |
243 bool BrowserThread::IsWellKnownThread(ID identifier) { | 218 bool BrowserThread::IsWellKnownThread(ID identifier) { |
244 base::AutoLock lock(BrowserThreadImpl::lock_); | 219 base::AutoLock lock(BrowserThreadImpl::lock_); |
245 return (identifier >= 0 && identifier < ID_COUNT && | 220 return (identifier >= 0 && identifier < ID_COUNT && |
246 BrowserThreadImpl::browser_threads_[identifier]); | 221 BrowserThreadImpl::browser_threads_[identifier]); |
247 } | 222 } |
248 | 223 |
249 // static | 224 // static |
250 bool BrowserThread::CurrentlyOn(ID identifier) { | 225 bool BrowserThread::CurrentlyOn(ID identifier) { |
251 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 226 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 | 348 |
374 // static | 349 // static |
375 scoped_refptr<base::MessageLoopProxy> | 350 scoped_refptr<base::MessageLoopProxy> |
376 BrowserThread::GetMessageLoopProxyForThread( | 351 BrowserThread::GetMessageLoopProxyForThread( |
377 ID identifier) { | 352 ID identifier) { |
378 scoped_refptr<base::MessageLoopProxy> proxy( | 353 scoped_refptr<base::MessageLoopProxy> proxy( |
379 new BrowserThreadMessageLoopProxy(identifier)); | 354 new BrowserThreadMessageLoopProxy(identifier)); |
380 return proxy; | 355 return proxy; |
381 } | 356 } |
382 | 357 |
| 358 base::Thread* BrowserThread::UnsafeGetBrowserThread(ID identifier) { |
| 359 base::AutoLock lock(BrowserThreadImpl::lock_); |
| 360 base::Thread* thread = BrowserThreadImpl::browser_threads_[identifier]; |
| 361 DCHECK(thread); |
| 362 return thread; |
| 363 } |
| 364 |
383 } // namespace content | 365 } // namespace content |
OLD | NEW |