| 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_export.h" | 9 #include "base/base_export.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 | 14 |
| 15 namespace base { | 15 namespace base { |
| 16 | 16 |
| 17 struct MessageLoopProxyTraits; | 17 struct MessageLoopProxyTraits; |
| 18 | 18 |
| 19 // 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 |
| 20 // 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. |
| 21 // obtain a MessageLoopProxy via Thread::message_loop_proxy() or | 21 // MessageLoopProxy objects are constructed automatically for all MessageLoops. |
| 22 // MessageLoopProxy::CreateForCurrentThread(). | 22 // So, to access them, you can use any of the following: |
| 23 // Thread::message_loop_proxy() |
| 24 // MessageLoop::current()->message_loop_proxy() |
| 25 // MessageLoopProxy::current() |
| 23 class BASE_EXPORT MessageLoopProxy | 26 class BASE_EXPORT MessageLoopProxy |
| 24 : public base::RefCountedThreadSafe<MessageLoopProxy, | 27 : public base::RefCountedThreadSafe<MessageLoopProxy, |
| 25 MessageLoopProxyTraits> { | 28 MessageLoopProxyTraits> { |
| 26 public: | 29 public: |
| 27 // These methods are the same as in message_loop.h, but are guaranteed to | 30 // These methods are the same as in message_loop.h, but are guaranteed to |
| 28 // either post the Task to the MessageLoop (if it's still alive), or to | 31 // either post the Task to the MessageLoop (if it's still alive), or to |
| 29 // delete the Task otherwise. | 32 // delete the Task otherwise. |
| 30 // They return true iff the thread existed and the task was posted. Note that | 33 // They return true iff the thread existed and the task was posted. Note that |
| 31 // even if the task is posted, there's no guarantee that it will run; for | 34 // even if the task is posted, there's no guarantee that it will run; for |
| 32 // example the target loop may already be quitting, or in the case of a | 35 // example the target loop may already be quitting, or in the case of a |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 bool DeleteSoon(const tracked_objects::Location& from_here, | 74 bool DeleteSoon(const tracked_objects::Location& from_here, |
| 72 T* object) { | 75 T* object) { |
| 73 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); | 76 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
| 74 } | 77 } |
| 75 template <class T> | 78 template <class T> |
| 76 bool ReleaseSoon(const tracked_objects::Location& from_here, | 79 bool ReleaseSoon(const tracked_objects::Location& from_here, |
| 77 T* object) { | 80 T* object) { |
| 78 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); | 81 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
| 79 } | 82 } |
| 80 | 83 |
| 81 // Factory method for creating an implementation of MessageLoopProxy | 84 // Gets the MessageLoopProxy for the current message loop, creating one if |
| 82 // for the current thread. | 85 // needed. |
| 83 static scoped_refptr<MessageLoopProxy> CreateForCurrentThread(); | 86 static scoped_refptr<MessageLoopProxy> current(); |
| 84 | 87 |
| 85 protected: | 88 protected: |
| 86 friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; | 89 friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; |
| 87 friend struct MessageLoopProxyTraits; | 90 friend struct MessageLoopProxyTraits; |
| 88 | 91 |
| 89 MessageLoopProxy(); | 92 MessageLoopProxy(); |
| 90 virtual ~MessageLoopProxy(); | 93 virtual ~MessageLoopProxy(); |
| 91 | 94 |
| 92 // Called when the proxy is about to be deleted. Subclasses can override this | 95 // Called when the proxy is about to be deleted. Subclasses can override this |
| 93 // to provide deletion on specific threads. | 96 // to provide deletion on specific threads. |
| 94 virtual void OnDestruct() const; | 97 virtual void OnDestruct() const; |
| 95 }; | 98 }; |
| 96 | 99 |
| 97 struct MessageLoopProxyTraits { | 100 struct MessageLoopProxyTraits { |
| 98 static void Destruct(const MessageLoopProxy* proxy) { | 101 static void Destruct(const MessageLoopProxy* proxy) { |
| 99 proxy->OnDestruct(); | 102 proxy->OnDestruct(); |
| 100 } | 103 } |
| 101 }; | 104 }; |
| 102 | 105 |
| 103 } // namespace base | 106 } // namespace base |
| 104 | 107 |
| 105 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 108 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
| OLD | NEW |