Chromium Code Reviews| 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 BASE_MESSAGE_LOOP_PROXY_H_ | 5 #ifndef BASE_MESSAGE_LOOP_PROXY_H_ |
| 6 #define BASE_MESSAGE_LOOP_PROXY_H_ | 6 #define BASE_MESSAGE_LOOP_PROXY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/base_api.h" | 9 #include "base/base_api.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | |
|
akalin
2011/07/20 07:52:03
Can you remove this and instead forward-declare Ca
awong
2011/07/20 21:17:41
Talked with Fred offline. We realized that the ma
| |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/task.h" | 13 #include "base/task.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 struct MessageLoopProxyTraits; | 17 struct MessageLoopProxyTraits; |
| 17 | 18 |
| 18 // This class provides a thread-safe refcounted interface to the Post* methods | 19 // This class provides a thread-safe refcounted interface to the Post* methods |
| 19 // of a message loop. This class can outlive the target message loop. You can | 20 // of a message loop. This class can outlive the target message loop. You can |
| 20 // obtain a MessageLoopProxy via Thread::message_loop_proxy() or | 21 // obtain a MessageLoopProxy via Thread::message_loop_proxy() or |
| 21 // MessageLoopProxy::CreateForCurrentThread(). | 22 // MessageLoopProxy::CreateForCurrentThread(). |
| 22 class BASE_API MessageLoopProxy | 23 class BASE_API MessageLoopProxy |
| 23 : public base::RefCountedThreadSafe<MessageLoopProxy, | 24 : public base::RefCountedThreadSafe<MessageLoopProxy, |
| 24 MessageLoopProxyTraits> { | 25 MessageLoopProxyTraits> { |
| 25 public: | 26 public: |
| 26 // These methods are the same as in message_loop.h, but are guaranteed to | 27 // These methods are the same as in message_loop.h, but are guaranteed to |
| 27 // either post the Task to the MessageLoop (if it's still alive), or to | 28 // either post the Task to the MessageLoop (if it's still alive), or to |
| 28 // delete the Task otherwise. | 29 // delete the Task otherwise. |
| 29 // They return true iff the thread existed and the task was posted. Note that | 30 // They return true iff the thread existed and the task was posted. Note that |
| 30 // even if the task is posted, there's no guarantee that it will run; for | 31 // even if the task is posted, there's no guarantee that it will run; for |
| 31 // example the target loop may already be quitting, or in the case of a | 32 // example the target loop may already be quitting, or in the case of a |
| 32 // delayed task a Quit message may preempt it in the message loop queue. | 33 // delayed task a Quit message may preempt it in the message loop queue. |
| 33 // Conversely, a return value of false is a guarantee the task will not run. | 34 // Conversely, a return value of false is a guarantee the task will not run. |
| 34 virtual bool PostTask(const tracked_objects::Location& from_here, | 35 virtual bool PostTask(const tracked_objects::Location& from_here, |
| 35 Task* task) = 0; | 36 Task* task) = 0; |
| 36 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 37 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 37 Task* task, int64 delay_ms) = 0; | 38 Task* task, |
| 39 int64 delay_ms) = 0; | |
| 38 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | 40 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, |
| 39 Task* task) = 0; | 41 Task* task) = 0; |
| 40 virtual bool PostNonNestableDelayedTask( | 42 virtual bool PostNonNestableDelayedTask( |
| 41 const tracked_objects::Location& from_here, | 43 const tracked_objects::Location& from_here, |
| 42 Task* task, | 44 Task* task, |
| 43 int64 delay_ms) = 0; | 45 int64 delay_ms) = 0; |
| 46 | |
| 47 // TODO(ajwong): Remove the functions above once the Task -> Closure migration | |
| 48 // is complete. | |
| 49 // | |
| 50 // There are 2 sets of Post*Task functions, one which takes the older Task* | |
| 51 // function object representation, and one that takes the newer base::Closure. | |
| 52 // We have this overload to allow a staged transition between the two systems. | |
| 53 // Once the transition is done, the functions above should be deleted. | |
| 54 virtual bool PostTask(const tracked_objects::Location& from_here, | |
| 55 const base::Closure& task) = 0; | |
| 56 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | |
| 57 const base::Closure& task, | |
| 58 int64 delay_ms) = 0; | |
| 59 virtual bool PostNonNestableTask(const tracked_objects::Location& from_here, | |
| 60 const base::Closure& task) = 0; | |
| 61 virtual bool PostNonNestableDelayedTask( | |
| 62 const tracked_objects::Location& from_here, | |
| 63 const base::Closure& task, | |
| 64 int64 delay_ms) = 0; | |
| 65 | |
| 44 // A method which checks if the caller is currently running in the thread that | 66 // A method which checks if the caller is currently running in the thread that |
| 45 // this proxy represents. | 67 // this proxy represents. |
| 46 virtual bool BelongsToCurrentThread() = 0; | 68 virtual bool BelongsToCurrentThread() = 0; |
| 47 | 69 |
| 48 template <class T> | 70 template <class T> |
| 49 bool DeleteSoon(const tracked_objects::Location& from_here, | 71 bool DeleteSoon(const tracked_objects::Location& from_here, |
| 50 T* object) { | 72 T* object) { |
| 51 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); | 73 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
| 52 } | 74 } |
| 53 template <class T> | 75 template <class T> |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 74 | 96 |
| 75 struct MessageLoopProxyTraits { | 97 struct MessageLoopProxyTraits { |
| 76 static void Destruct(const MessageLoopProxy* proxy) { | 98 static void Destruct(const MessageLoopProxy* proxy) { |
| 77 proxy->OnDestruct(); | 99 proxy->OnDestruct(); |
| 78 } | 100 } |
| 79 }; | 101 }; |
| 80 | 102 |
| 81 } // namespace base | 103 } // namespace base |
| 82 | 104 |
| 83 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 105 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
| OLD | NEW |