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" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const base::Closure& task) = 0; | 63 const base::Closure& task) = 0; |
64 virtual bool PostNonNestableDelayedTask( | 64 virtual bool PostNonNestableDelayedTask( |
65 const tracked_objects::Location& from_here, | 65 const tracked_objects::Location& from_here, |
66 const base::Closure& task, | 66 const base::Closure& task, |
67 int64 delay_ms) = 0; | 67 int64 delay_ms) = 0; |
68 | 68 |
69 // A method which checks if the caller is currently running in the thread that | 69 // A method which checks if the caller is currently running in the thread that |
70 // this proxy represents. | 70 // this proxy represents. |
71 virtual bool BelongsToCurrentThread() = 0; | 71 virtual bool BelongsToCurrentThread() = 0; |
72 | 72 |
| 73 // Executes |task| on the given MessageLoopProxy. On completion, |reply| |
| 74 // is passed back to the MessageLoopProxy for the thread that called |
| 75 // PostTaskAndReply(). Both |task| and |reply| are guaranteed to be deleted |
| 76 // on the thread from which PostTaskAndReply() is invoked. This allows |
| 77 // objects that must be deleted on the originating thread to be bound into the |
| 78 // |task| and |reply| Closures. In particular, it can be useful to use |
| 79 // WeakPtr<> in the |reply| Closure so that the reply operation can be |
| 80 // canceled. See the following pseudo-code: |
| 81 // |
| 82 // class DataBuffer : public RefCountedThreadSafe<DataBuffer> { |
| 83 // public: |
| 84 // // Called to add data into a buffer. |
| 85 // void AddData(void* buf, size_t length); |
| 86 // ... |
| 87 // }; |
| 88 // |
| 89 // |
| 90 // class DataLoader : public SupportsWeakPtr<ReadToBuffer> { |
| 91 // public: |
| 92 // void GetData() { |
| 93 // scoped_refptr<DataBuffer> buffer = new DataBuffer(); |
| 94 // target_thread_.message_loop_proxy()->PostTaskAndReply( |
| 95 // FROM_HERE, |
| 96 // base::Bind(&DataBuffer::AddData, buffer), |
| 97 // base::Bind(&DataLoader::OnDataReceived, AsWeakPtr(), buffer)); |
| 98 // } |
| 99 // |
| 100 // private: |
| 101 // void OnDataReceived(scoped_refptr<DataBuffer> buffer) { |
| 102 // // Do something with buffer. |
| 103 // } |
| 104 // }; |
| 105 // |
| 106 // |
| 107 // Things to notice: |
| 108 // * Results of |task| are shared with |reply| by binding a shared argument |
| 109 // (a DataBuffer instance). |
| 110 // * The DataLoader object has no special thread safety. |
| 111 // * The DataLoader object can be deleted while |task| is still running, |
| 112 // and the reply will cancel itself safely because it is bound to a |
| 113 // WeakPtr<>. |
| 114 bool PostTaskAndReply(const tracked_objects::Location& from_here, |
| 115 const Closure& task, |
| 116 const Closure& reply); |
| 117 |
73 template <class T> | 118 template <class T> |
74 bool DeleteSoon(const tracked_objects::Location& from_here, | 119 bool DeleteSoon(const tracked_objects::Location& from_here, |
75 T* object) { | 120 T* object) { |
76 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); | 121 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
77 } | 122 } |
78 template <class T> | 123 template <class T> |
79 bool ReleaseSoon(const tracked_objects::Location& from_here, | 124 bool ReleaseSoon(const tracked_objects::Location& from_here, |
80 T* object) { | 125 T* object) { |
81 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); | 126 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
82 } | 127 } |
(...skipping 16 matching lines...) Expand all Loading... |
99 | 144 |
100 struct MessageLoopProxyTraits { | 145 struct MessageLoopProxyTraits { |
101 static void Destruct(const MessageLoopProxy* proxy) { | 146 static void Destruct(const MessageLoopProxy* proxy) { |
102 proxy->OnDestruct(); | 147 proxy->OnDestruct(); |
103 } | 148 } |
104 }; | 149 }; |
105 | 150 |
106 } // namespace base | 151 } // namespace base |
107 | 152 |
108 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 153 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
OLD | NEW |