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 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include "base/basictypes.h" |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/task.h" |
| 12 #include "base/tracked_objects.h" |
| 13 #include "content/common/content_export.h" |
| 14 |
| 15 // TODO(joi): Remove these in a follow-up change and IWYU in files |
| 16 // that were getting them directly or indirectly from here. |
| 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/message_loop.h" |
| 20 #include "base/message_loop_proxy.h" |
10 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
11 #include "base/task.h" | |
12 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
13 #include "content/common/content_export.h" | |
14 | 23 |
15 #if defined(UNIT_TEST) | 24 #if defined(UNIT_TEST) |
16 #include "base/logging.h" | 25 #include "base/logging.h" |
17 #endif // UNIT_TEST | 26 #endif // UNIT_TEST |
18 | 27 |
19 namespace base { | 28 namespace base { |
20 class MessageLoopProxy; | 29 class MessageLoopProxy; |
| 30 class Thread; |
21 } | 31 } |
22 | 32 |
23 namespace content { | 33 namespace content { |
24 | 34 |
25 class BrowserThreadImpl; | 35 class BrowserThreadImpl; |
26 class DeprecatedBrowserThread; | |
27 | 36 |
28 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
29 // BrowserThread | 38 // BrowserThread |
30 // | 39 // |
31 // Utility functions for threads that are known by a browser-wide | 40 // Utility functions for threads that are known by a browser-wide |
32 // name. For example, there is one IO thread for the entire browser | 41 // name. For example, there is one IO thread for the entire browser |
33 // process, and various pieces of code find it useful to retrieve a | 42 // process, and various pieces of code find it useful to retrieve a |
34 // pointer to the IO thread's message loop. | 43 // pointer to the IO thread's message loop. |
35 // | 44 // |
36 // Invoke a task by thread ID: | 45 // Invoke a task by thread ID: |
37 // | 46 // |
38 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | 47 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); |
39 // | 48 // |
40 // The return value is false if the task couldn't be posted because the target | 49 // The return value is false if the task couldn't be posted because the target |
41 // thread doesn't exist. If this could lead to data loss, you need to check the | 50 // thread doesn't exist. If this could lead to data loss, you need to check the |
42 // result and restructure the code to ensure it doesn't occur. | 51 // result and restructure the code to ensure it doesn't occur. |
43 // | 52 // |
44 // This class automatically handles the lifetime of different threads. | 53 // This class automatically handles the lifetime of different threads. |
45 // It's always safe to call PostTask on any thread. If it's not yet created, | 54 // It's always safe to call PostTask on any thread. If it's not yet created, |
46 // the task is deleted. There are no race conditions. If the thread that the | 55 // the task is deleted. There are no race conditions. If the thread that the |
47 // task is posted to is guaranteed to outlive the current thread, then no locks | 56 // task is posted to is guaranteed to outlive the current thread, then no locks |
48 // are used. You should never need to cache pointers to MessageLoops, since | 57 // are used. You should never need to cache pointers to MessageLoops, since |
49 // they're not thread safe. | 58 // they're not thread safe. |
50 class CONTENT_EXPORT BrowserThread : public base::Thread { | 59 class CONTENT_EXPORT BrowserThread { |
51 public: | 60 public: |
52 // An enumeration of the well-known threads. | 61 // An enumeration of the well-known threads. |
53 // NOTE: threads must be listed in the order of their life-time, with each | 62 // NOTE: threads must be listed in the order of their life-time, with each |
54 // thread outliving every other thread below it. | 63 // thread outliving every other thread below it. |
55 enum ID { | 64 enum ID { |
56 // The main thread in the browser. | 65 // The main thread in the browser. |
57 UI, | 66 UI, |
58 | 67 |
59 // This is the thread that interacts with the database. | 68 // This is the thread that interacts with the database. |
60 DB, | 69 DB, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 172 |
164 // If the current message loop is one of the known threads, returns true and | 173 // If the current message loop is one of the known threads, returns true and |
165 // sets identifier to its ID. Otherwise returns false. | 174 // sets identifier to its ID. Otherwise returns false. |
166 static bool GetCurrentThreadIdentifier(ID* identifier); | 175 static bool GetCurrentThreadIdentifier(ID* identifier); |
167 | 176 |
168 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime | 177 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime |
169 // of the thread. | 178 // of the thread. |
170 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( | 179 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( |
171 ID identifier); | 180 ID identifier); |
172 | 181 |
| 182 // Gets the Thread object for the specified thread, or NULL if the |
| 183 // thread has not been created (or has been destroyed during |
| 184 // shutdown). |
| 185 // |
| 186 // Before calling this, you must have called content::ContentMain |
| 187 // with a command-line that would specify a browser process (e.g. an |
| 188 // empty command line). |
| 189 // |
| 190 // This is unsafe as your pointer may become invalid close to |
| 191 // shutdown. |
| 192 // |
| 193 // TODO(joi): Remove this once clients such as BrowserProcessImpl |
| 194 // (and classes that call things like |
| 195 // g_browser_process->file_thread()) are switched to using |
| 196 // MessageLoopProxy. |
| 197 static base::Thread* UnsafeGetBrowserThread(ID identifier); |
| 198 |
173 // Use these templates in conjuction with RefCountedThreadSafe when you want | 199 // Use these templates in conjuction with RefCountedThreadSafe when you want |
174 // to ensure that an object is deleted on a specific thread. This is needed | 200 // to ensure that an object is deleted on a specific thread. This is needed |
175 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread | 201 // when an object can hop between threads (i.e. IO -> FILE -> IO), and thread |
176 // switching delays can mean that the final IO tasks executes before the FILE | 202 // switching delays can mean that the final IO tasks executes before the FILE |
177 // task's stack unwinds. This would lead to the object destructing on the | 203 // task's stack unwinds. This would lead to the object destructing on the |
178 // FILE thread, which often is not what you want (i.e. to unregister from | 204 // FILE thread, which often is not what you want (i.e. to unregister from |
179 // NotificationService, to notify other objects on the creating thread etc). | 205 // NotificationService, to notify other objects on the creating thread etc). |
180 template<ID thread> | 206 template<ID thread> |
181 struct DeleteOnThread { | 207 struct DeleteOnThread { |
182 template<typename T> | 208 template<typename T> |
(...skipping 23 matching lines...) Expand all Loading... |
206 // friend class DeleteTask<Foo>; | 232 // friend class DeleteTask<Foo>; |
207 // | 233 // |
208 // ~Foo(); | 234 // ~Foo(); |
209 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; | 235 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
210 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; | 236 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
211 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; | 237 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
212 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; | 238 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
213 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; | 239 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; |
214 | 240 |
215 private: | 241 private: |
216 // Construct a BrowserThread with the supplied identifier. It is an error | 242 friend class BrowserThreadImpl; |
217 // to construct a BrowserThread that already exists. | |
218 explicit BrowserThread(ID identifier); | |
219 | 243 |
220 // Special constructor for the main (UI) thread and unittests. We use a dummy | 244 BrowserThread() {} |
221 // thread here since the main thread already exists. | 245 DISALLOW_COPY_AND_ASSIGN(BrowserThread); |
222 BrowserThread(ID identifier, MessageLoop* message_loop); | |
223 | |
224 virtual ~BrowserThread(); | |
225 | |
226 // Common initialization code for the constructors. | |
227 void Initialize(); | |
228 | |
229 // Constructors are only available through this subclass. | |
230 friend class content::BrowserThreadImpl; | |
231 | |
232 // TODO(joi): Remove. | |
233 friend class DeprecatedBrowserThread; | |
234 | |
235 // The identifier of this thread. Only one thread can exist with a given | |
236 // identifier at a given time. | |
237 // TODO(joi): Move to BrowserThreadImpl, and make constructors here | |
238 // do-nothing. | |
239 ID identifier_; | |
240 }; | |
241 | |
242 // Temporary escape hatch for chrome/ to construct BrowserThread, | |
243 // until we make content/ construct its own threads. | |
244 class CONTENT_EXPORT DeprecatedBrowserThread : public BrowserThread { | |
245 public: | |
246 explicit DeprecatedBrowserThread(BrowserThread::ID identifier); | |
247 DeprecatedBrowserThread(BrowserThread::ID identifier, | |
248 MessageLoop* message_loop); | |
249 virtual ~DeprecatedBrowserThread(); | |
250 }; | 246 }; |
251 | 247 |
252 } // namespace content | 248 } // namespace content |
253 | 249 |
254 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 250 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
OLD | NEW |