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 // CancelableCallback is a wrapper around base::Callback that allows | 5 // CancelableCallback is a wrapper around base::Callback that allows |
6 // cancellation of a callback. CancelableCallback takes a reference on the | 6 // cancellation of a callback. CancelableCallback takes a reference on the |
7 // wrapped callback until this object is destroyed or Reset()/Cancel() are | 7 // wrapped callback until this object is destroyed or Reset()/Cancel() are |
8 // called. | 8 // called. |
9 // | 9 // |
10 // NOTE: | 10 // NOTE: |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 166 |
167 callback_ = callback; | 167 callback_ = callback; |
168 } | 168 } |
169 | 169 |
170 // Returns a callback that can be disabled by calling Cancel(). | 170 // Returns a callback that can be disabled by calling Cancel(). |
171 const base::Callback<void(A1)>& callback() const { | 171 const base::Callback<void(A1)>& callback() const { |
172 return forwarder_; | 172 return forwarder_; |
173 } | 173 } |
174 | 174 |
175 private: | 175 private: |
176 void Forward(A1 a1) const { | 176 void Forward( |
| 177 typename internal::CallbackParamTraits<A1>::ForwardType a1) const { |
177 callback_.Run(a1); | 178 callback_.Run(a1); |
178 } | 179 } |
179 | 180 |
180 // Helper method to bind |forwarder_| using a weak pointer from | 181 // Helper method to bind |forwarder_| using a weak pointer from |
181 // |weak_factory_|. | 182 // |weak_factory_|. |
182 void InitializeForwarder() { | 183 void InitializeForwarder() { |
183 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, | 184 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, |
184 weak_factory_.GetWeakPtr()); | 185 weak_factory_.GetWeakPtr()); |
185 } | 186 } |
186 | 187 |
187 // Used to ensure Forward() is not run when this object is destroyed. | 188 // Used to ensure Forward() is not run when this object is destroyed. |
188 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; | 189 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; |
189 | 190 |
190 // The wrapper closure. | 191 // The wrapper closure. |
191 base::Callback<void(A1)> forwarder_; | 192 base::Callback<void(A1)> forwarder_; |
192 | 193 |
193 // The stored closure that may be cancelled. | 194 // The stored closure that may be cancelled. |
194 base::Callback<void(A1)> callback_; | 195 base::Callback<void(A1)> callback_; |
195 | 196 |
196 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 197 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
197 }; | 198 }; |
198 | 199 |
199 typedef CancelableCallback<void(void)> CancelableClosure; | 200 typedef CancelableCallback<void(void)> CancelableClosure; |
200 | 201 |
201 } // namespace base | 202 } // namespace base |
202 | 203 |
203 #endif // BASE_CANCELABLE_CALLBACK_H_ | 204 #endif // BASE_CANCELABLE_CALLBACK_H_ |
OLD | NEW |