| 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" |
| 10 #include "base/synchronization/lock.h" | |
| 11 #include "base/task.h" | 11 #include "base/task.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/tracked_objects.h" |
| 13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 | 14 |
| 15 #if defined(UNIT_TEST) | 15 #if defined(UNIT_TEST) |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #endif // UNIT_TEST | 17 #endif // UNIT_TEST |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class MessageLoopProxy; | 20 class MessageLoopProxy; |
| 21 class Thread; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 25 class BrowserThreadImpl; | 26 class BrowserThreadImpl; |
| 26 class DeprecatedBrowserThread; | |
| 27 | 27 |
| 28 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
| 29 // BrowserThread | 29 // BrowserThread |
| 30 // | 30 // |
| 31 // Utility functions for threads that are known by a browser-wide | 31 // Utility functions for threads that are known by a browser-wide |
| 32 // name. For example, there is one IO thread for the entire browser | 32 // 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 | 33 // process, and various pieces of code find it useful to retrieve a |
| 34 // pointer to the IO thread's message loop. | 34 // pointer to the IO thread's message loop. |
| 35 // | 35 // |
| 36 // Invoke a task by thread ID: | 36 // Invoke a task by thread ID: |
| 37 // | 37 // |
| 38 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | 38 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); |
| 39 // | 39 // |
| 40 // The return value is false if the task couldn't be posted because the target | 40 // 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 | 41 // 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. | 42 // result and restructure the code to ensure it doesn't occur. |
| 43 // | 43 // |
| 44 // This class automatically handles the lifetime of different threads. | 44 // 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, | 45 // 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 | 46 // 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 | 47 // 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 | 48 // are used. You should never need to cache pointers to MessageLoops, since |
| 49 // they're not thread safe. | 49 // they're not thread safe. |
| 50 class CONTENT_EXPORT BrowserThread : public base::Thread { | 50 class CONTENT_EXPORT BrowserThread { |
| 51 public: | 51 public: |
| 52 // An enumeration of the well-known threads. | 52 // An enumeration of the well-known threads. |
| 53 // NOTE: threads must be listed in the order of their life-time, with each | 53 // NOTE: threads must be listed in the order of their life-time, with each |
| 54 // thread outliving every other thread below it. | 54 // thread outliving every other thread below it. |
| 55 enum ID { | 55 enum ID { |
| 56 // The main thread in the browser. | 56 // The main thread in the browser. |
| 57 UI, | 57 UI, |
| 58 | 58 |
| 59 // This is the thread that interacts with the database. | 59 // This is the thread that interacts with the database. |
| 60 DB, | 60 DB, |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 163 |
| 164 // If the current message loop is one of the known threads, returns true and | 164 // If the current message loop is one of the known threads, returns true and |
| 165 // sets identifier to its ID. Otherwise returns false. | 165 // sets identifier to its ID. Otherwise returns false. |
| 166 static bool GetCurrentThreadIdentifier(ID* identifier); | 166 static bool GetCurrentThreadIdentifier(ID* identifier); |
| 167 | 167 |
| 168 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime | 168 // Callers can hold on to a refcounted MessageLoopProxy beyond the lifetime |
| 169 // of the thread. | 169 // of the thread. |
| 170 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( | 170 static scoped_refptr<base::MessageLoopProxy> GetMessageLoopProxyForThread( |
| 171 ID identifier); | 171 ID identifier); |
| 172 | 172 |
| 173 // Gets the Thread object for the specified thread, or NULL if the |
| 174 // thread has not been created (or has been destroyed during |
| 175 // shutdown). |
| 176 // |
| 177 // Before calling this, you must have called content::ContentMain |
| 178 // with a command-line that would specify a browser process (e.g. an |
| 179 // empty command line). |
| 180 // |
| 181 // This is unsafe as your pointer may become invalid close to |
| 182 // shutdown. |
| 183 // |
| 184 // TODO(joi): Remove this once clients such as BrowserProcessImpl |
| 185 // (and classes that call things like |
| 186 // g_browser_process->file_thread()) are switched to using |
| 187 // MessageLoopProxy. |
| 188 static base::Thread* UnsafeGetBrowserThread(ID identifier); |
| 189 |
| 173 // Use these templates in conjuction with RefCountedThreadSafe when you want | 190 // 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 | 191 // 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 | 192 // 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 | 193 // 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 | 194 // 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 | 195 // 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). | 196 // NotificationService, to notify other objects on the creating thread etc). |
| 180 template<ID thread> | 197 template<ID thread> |
| 181 struct DeleteOnThread { | 198 struct DeleteOnThread { |
| 182 template<typename T> | 199 template<typename T> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 206 // friend class DeleteTask<Foo>; | 223 // friend class DeleteTask<Foo>; |
| 207 // | 224 // |
| 208 // ~Foo(); | 225 // ~Foo(); |
| 209 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; | 226 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
| 210 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; | 227 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
| 211 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; | 228 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
| 212 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; | 229 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
| 213 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; | 230 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; |
| 214 | 231 |
| 215 private: | 232 private: |
| 216 // Construct a BrowserThread with the supplied identifier. It is an error | 233 friend class BrowserThreadImpl; |
| 217 // to construct a BrowserThread that already exists. | |
| 218 explicit BrowserThread(ID identifier); | |
| 219 | 234 |
| 220 // Special constructor for the main (UI) thread and unittests. We use a dummy | 235 BrowserThread() {} |
| 221 // thread here since the main thread already exists. | 236 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 }; | 237 }; |
| 251 | 238 |
| 252 } // namespace content | 239 } // namespace content |
| 253 | 240 |
| 254 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ | 241 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| OLD | NEW |