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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 | 188 |
189 // The wrapper closure. | 189 // The wrapper closure. |
190 base::Callback<void(A1)> forwarder_; | 190 base::Callback<void(A1)> forwarder_; |
191 | 191 |
192 // The stored closure that may be cancelled. | 192 // The stored closure that may be cancelled. |
193 base::Callback<void(A1)> callback_; | 193 base::Callback<void(A1)> callback_; |
194 | 194 |
195 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 195 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
196 }; | 196 }; |
197 | 197 |
| 198 template <typename A1, typename A2> |
| 199 class CancelableCallback<void(A1, A2)> { |
| 200 public: |
| 201 CancelableCallback() : weak_factory_(this) {} |
| 202 |
| 203 // |callback| must not be null. |
| 204 explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback) |
| 205 : weak_factory_(this), |
| 206 callback_(callback) { |
| 207 DCHECK(!callback.is_null()); |
| 208 InitializeForwarder(); |
| 209 } |
| 210 |
| 211 ~CancelableCallback() {} |
| 212 |
| 213 // Cancels and drops the reference to the wrapped callback. |
| 214 void Cancel() { |
| 215 weak_factory_.InvalidateWeakPtrs(); |
| 216 forwarder_.Reset(); |
| 217 callback_.Reset(); |
| 218 } |
| 219 |
| 220 // Returns true if the wrapped callback has been cancelled. |
| 221 bool IsCancelled() const { |
| 222 return callback_.is_null(); |
| 223 } |
| 224 |
| 225 // Sets |callback| as the closure that may be cancelled. |callback| may not |
| 226 // be null. Outstanding and any previously wrapped callbacks are cancelled. |
| 227 void Reset(const base::Callback<void(A1, A2)>& callback) { |
| 228 DCHECK(!callback.is_null()); |
| 229 |
| 230 // Outstanding tasks (e.g., posted to a message loop) must not be called. |
| 231 Cancel(); |
| 232 |
| 233 // |forwarder_| is no longer valid after Cancel(), so re-bind. |
| 234 InitializeForwarder(); |
| 235 |
| 236 callback_ = callback; |
| 237 } |
| 238 |
| 239 // Returns a callback that can be disabled by calling Cancel(). |
| 240 const base::Callback<void(A1, A2)>& callback() const { |
| 241 return forwarder_; |
| 242 } |
| 243 |
| 244 private: |
| 245 void Forward(A1 a1, A2 a2) const { |
| 246 callback_.Run(a1, a2); |
| 247 } |
| 248 |
| 249 // Helper method to bind |forwarder_| using a weak pointer from |
| 250 // |weak_factory_|. |
| 251 void InitializeForwarder() { |
| 252 forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward, |
| 253 weak_factory_.GetWeakPtr()); |
| 254 } |
| 255 |
| 256 // Used to ensure Forward() is not run when this object is destroyed. |
| 257 base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_; |
| 258 |
| 259 // The wrapper closure. |
| 260 base::Callback<void(A1, A2)> forwarder_; |
| 261 |
| 262 // The stored closure that may be cancelled. |
| 263 base::Callback<void(A1, A2)> callback_; |
| 264 |
| 265 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
| 266 }; |
| 267 |
198 typedef CancelableCallback<void(void)> CancelableClosure; | 268 typedef CancelableCallback<void(void)> CancelableClosure; |
199 | 269 |
200 } // namespace base | 270 } // namespace base |
201 | 271 |
202 #endif // BASE_CANCELABLE_CALLBACK_H_ | 272 #endif // BASE_CANCELABLE_CALLBACK_H_ |
OLD | NEW |