| 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_BROWSER_BROWSER_THREAD_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| 6 #define CONTENT_BROWSER_BROWSER_THREAD_H_ | 6 #define CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/synchronization/lock.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/threading/thread.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 } | 21 } |
| 22 | 22 |
| 23 namespace content { |
| 24 class BrowserThreadImpl; |
| 25 } |
| 26 |
| 27 class DeprecatedBrowserThread; |
| 28 |
| 23 /////////////////////////////////////////////////////////////////////////////// | 29 /////////////////////////////////////////////////////////////////////////////// |
| 24 // BrowserThread | 30 // BrowserThread |
| 25 // | 31 // |
| 26 // This class represents a thread that is known by a browser-wide name. For | 32 // Utility functions for threads that are known by a browser-wide |
| 27 // example, there is one IO thread for the entire browser process, and various | 33 // name. For example, there is one IO thread for the entire browser |
| 28 // pieces of code find it useful to retrieve a pointer to the IO thread's | 34 // process, and various pieces of code find it useful to retrieve a |
| 35 // pointer to the IO thread's message loop. |
| 36 // |
| 29 // Invoke a task by thread ID: | 37 // Invoke a task by thread ID: |
| 30 // | 38 // |
| 31 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); | 39 // BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task); |
| 32 // | 40 // |
| 33 // The return value is false if the task couldn't be posted because the target | 41 // The return value is false if the task couldn't be posted because the target |
| 34 // thread doesn't exist. If this could lead to data loss, you need to check the | 42 // thread doesn't exist. If this could lead to data loss, you need to check the |
| 35 // result and restructure the code to ensure it doesn't occur. | 43 // result and restructure the code to ensure it doesn't occur. |
| 36 // | 44 // |
| 37 // This class automatically handles the lifetime of different threads. | 45 // This class automatically handles the lifetime of different threads. |
| 38 // It's always safe to call PostTask on any thread. If it's not yet created, | 46 // It's always safe to call PostTask on any thread. If it's not yet created, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 // as hooks into websocket layer. | 82 // as hooks into websocket layer. |
| 75 WEB_SOCKET_PROXY, | 83 WEB_SOCKET_PROXY, |
| 76 #endif | 84 #endif |
| 77 | 85 |
| 78 // This identifier does not represent a thread. Instead it counts the | 86 // This identifier does not represent a thread. Instead it counts the |
| 79 // number of well-known threads. Insert new well-known threads before this | 87 // number of well-known threads. Insert new well-known threads before this |
| 80 // identifier. | 88 // identifier. |
| 81 ID_COUNT | 89 ID_COUNT |
| 82 }; | 90 }; |
| 83 | 91 |
| 84 // Construct a BrowserThread with the supplied identifier. It is an error | |
| 85 // to construct a BrowserThread that already exists. | |
| 86 explicit BrowserThread(ID identifier); | |
| 87 | |
| 88 // Special constructor for the main (UI) thread and unittests. We use a dummy | |
| 89 // thread here since the main thread already exists. | |
| 90 BrowserThread(ID identifier, MessageLoop* message_loop); | |
| 91 | |
| 92 virtual ~BrowserThread(); | |
| 93 | |
| 94 // These are the same methods in message_loop.h, but are guaranteed to either | 92 // These are the same methods in message_loop.h, but are guaranteed to either |
| 95 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. | 93 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. |
| 96 // They return true iff the thread existed and the task was posted. Note that | 94 // They return true iff the thread existed and the task was posted. Note that |
| 97 // even if the task is posted, there's no guarantee that it will run, since | 95 // even if the task is posted, there's no guarantee that it will run, since |
| 98 // the target thread may already have a Quit message in its queue. | 96 // the target thread may already have a Quit message in its queue. |
| 99 static bool PostTask(ID identifier, | 97 static bool PostTask(ID identifier, |
| 100 const tracked_objects::Location& from_here, | 98 const tracked_objects::Location& from_here, |
| 101 const base::Closure& task); | 99 const base::Closure& task); |
| 102 static bool PostDelayedTask(ID identifier, | 100 static bool PostDelayedTask(ID identifier, |
| 103 const tracked_objects::Location& from_here, | 101 const tracked_objects::Location& from_here, |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 // friend class DeleteTask<Foo>; | 207 // friend class DeleteTask<Foo>; |
| 210 // | 208 // |
| 211 // ~Foo(); | 209 // ~Foo(); |
| 212 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; | 210 struct DeleteOnUIThread : public DeleteOnThread<UI> { }; |
| 213 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; | 211 struct DeleteOnIOThread : public DeleteOnThread<IO> { }; |
| 214 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; | 212 struct DeleteOnFileThread : public DeleteOnThread<FILE> { }; |
| 215 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; | 213 struct DeleteOnDBThread : public DeleteOnThread<DB> { }; |
| 216 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; | 214 struct DeleteOnWebKitThread : public DeleteOnThread<WEBKIT> { }; |
| 217 | 215 |
| 218 private: | 216 private: |
| 217 // Construct a BrowserThread with the supplied identifier. It is an error |
| 218 // to construct a BrowserThread that already exists. |
| 219 explicit BrowserThread(ID identifier); |
| 220 |
| 221 // Special constructor for the main (UI) thread and unittests. We use a dummy |
| 222 // thread here since the main thread already exists. |
| 223 BrowserThread(ID identifier, MessageLoop* message_loop); |
| 224 |
| 225 virtual ~BrowserThread(); |
| 226 |
| 219 // Common initialization code for the constructors. | 227 // Common initialization code for the constructors. |
| 220 void Initialize(); | 228 void Initialize(); |
| 221 | 229 |
| 222 // TODO(brettw) remove this variant when Task->Closure migration is complete. | 230 // Constructors are only available through this subclass. |
| 223 static bool PostTaskHelper( | 231 friend class content::BrowserThreadImpl; |
| 224 ID identifier, | 232 |
| 225 const tracked_objects::Location& from_here, | 233 // TODO(joi): Remove. |
| 226 Task* task, | 234 friend class DeprecatedBrowserThread; |
| 227 int64 delay_ms, | |
| 228 bool nestable); | |
| 229 static bool PostTaskHelper( | |
| 230 ID identifier, | |
| 231 const tracked_objects::Location& from_here, | |
| 232 const base::Closure& task, | |
| 233 int64 delay_ms, | |
| 234 bool nestable); | |
| 235 | 235 |
| 236 // The identifier of this thread. Only one thread can exist with a given | 236 // The identifier of this thread. Only one thread can exist with a given |
| 237 // identifier at a given time. | 237 // identifier at a given time. |
| 238 // TODO(joi): Move to BrowserThreadImpl, and make constructors here |
| 239 // do-nothing. |
| 238 ID identifier_; | 240 ID identifier_; |
| 239 | |
| 240 // This lock protects |browser_threads_|. Do not read or modify that array | |
| 241 // without holding this lock. Do not block while holding this lock. | |
| 242 static base::Lock lock_; | |
| 243 | |
| 244 // An array of the BrowserThread objects. This array is protected by |lock_|. | |
| 245 // The threads are not owned by this array. Typically, the threads are owned | |
| 246 // on the UI thread by the g_browser_process object. BrowserThreads remove | |
| 247 // themselves from this array upon destruction. | |
| 248 static BrowserThread* browser_threads_[ID_COUNT]; | |
| 249 }; | 241 }; |
| 250 | 242 |
| 251 #endif // CONTENT_BROWSER_BROWSER_THREAD_H_ | 243 // Temporary escape hatch for chrome/ to construct BrowserThread, |
| 244 // until we make content/ construct its own threads. |
| 245 class DeprecatedBrowserThread : public BrowserThread { |
| 246 public: |
| 247 explicit DeprecatedBrowserThread(BrowserThread::ID identifier); |
| 248 DeprecatedBrowserThread(BrowserThread::ID identifier, |
| 249 MessageLoop* message_loop); |
| 250 virtual ~DeprecatedBrowserThread(); |
| 251 }; |
| 252 |
| 253 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_THREAD_H_ |
| OLD | NEW |