| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 | 11 |
| 12 namespace base { |
| 13 |
| 14 struct MessageLoopProxyTraits; |
| 15 |
| 12 // This class provides a thread-safe refcounted interface to the Post* methods | 16 // This class provides a thread-safe refcounted interface to the Post* methods |
| 13 // of a message loop. This class can outlive the target message loop. | 17 // of a message loop. This class can outlive the target message loop. |
| 14 class MessageLoopProxy : public base::RefCountedThreadSafe<MessageLoopProxy> { | 18 class MessageLoopProxy |
| 19 : public base::RefCountedThreadSafe<MessageLoopProxy, |
| 20 MessageLoopProxyTraits> { |
| 15 public: | 21 public: |
| 16 // These are the same methods in message_loop.h, but are guaranteed to either | 22 // These are the same methods in message_loop.h, but are guaranteed to either |
| 17 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. | 23 // get posted to the MessageLoop if it's still alive, or be deleted otherwise. |
| 18 // They return true iff the thread existed and the task was posted. Note that | 24 // They return true iff the thread existed and the task was posted. Note that |
| 19 // even if the task is posted, there's no guarantee that it will run, since | 25 // even if the task is posted, there's no guarantee that it will run, since |
| 20 // the target thread may already have a Quit message in its queue. | 26 // the target thread may already have a Quit message in its queue. |
| 21 virtual bool PostTask(const tracked_objects::Location& from_here, | 27 virtual bool PostTask(const tracked_objects::Location& from_here, |
| 22 Task* task) = 0; | 28 Task* task) = 0; |
| 23 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 29 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 24 Task* task, int64 delay_ms) = 0; | 30 Task* task, int64 delay_ms) = 0; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 template <class T> | 41 template <class T> |
| 36 bool DeleteSoon(const tracked_objects::Location& from_here, | 42 bool DeleteSoon(const tracked_objects::Location& from_here, |
| 37 T* object) { | 43 T* object) { |
| 38 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); | 44 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
| 39 } | 45 } |
| 40 template <class T> | 46 template <class T> |
| 41 bool ReleaseSoon(const tracked_objects::Location& from_here, | 47 bool ReleaseSoon(const tracked_objects::Location& from_here, |
| 42 T* object) { | 48 T* object) { |
| 43 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); | 49 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
| 44 } | 50 } |
| 51 |
| 52 // Factory method for creating an implementation of MessageLoopProxy |
| 53 // for the current thread. |
| 54 static scoped_refptr<MessageLoopProxy> CreateForCurrentThread(); |
| 55 |
| 56 protected: |
| 57 friend struct MessageLoopProxyTraits; |
| 58 // Called when the proxy is about to be deleted. Subclasses can override this |
| 59 // to provide deletion on specific threads. |
| 60 virtual void OnDestruct() { |
| 61 delete this; |
| 62 } |
| 45 }; | 63 }; |
| 46 | 64 |
| 65 struct MessageLoopProxyTraits { |
| 66 static void Destruct(MessageLoopProxy* proxy) { |
| 67 proxy->OnDestruct(); |
| 68 } |
| 69 }; |
| 70 |
| 71 } // namespace base |
| 72 |
| 47 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 73 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
| 48 | 74 |
| OLD | NEW |