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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 } | 107 } |
108 | 108 |
109 // Helper method to bind |forwarder_| using a weak pointer from | 109 // Helper method to bind |forwarder_| using a weak pointer from |
110 // |weak_factory_|. | 110 // |weak_factory_|. |
111 void InitializeForwarder() { | 111 void InitializeForwarder() { |
112 forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward, | 112 forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward, |
113 weak_factory_.GetWeakPtr()); | 113 weak_factory_.GetWeakPtr()); |
114 } | 114 } |
115 | 115 |
116 // Used to ensure Forward() is not run when this object is destroyed. | 116 // Used to ensure Forward() is not run when this object is destroyed. |
117 base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_; | 117 base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_; |
Nico
2014/11/06 22:59:49
Heh, changing that down there and not up here look
| |
118 | 118 |
119 // The wrapper closure. | 119 // The wrapper closure. |
120 base::Callback<void(void)> forwarder_; | 120 base::Callback<void(void)> forwarder_; |
121 | 121 |
122 // The stored closure that may be cancelled. | 122 // The stored closure that may be cancelled. |
123 base::Callback<void(void)> callback_; | 123 base::Callback<void(void)> callback_; |
124 | 124 |
125 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 125 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
126 }; | 126 }; |
127 | 127 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 195 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
196 }; | 196 }; |
197 | 197 |
198 template <typename A1, typename A2> | 198 template <typename A1, typename A2> |
199 class CancelableCallback<void(A1, A2)> { | 199 class CancelableCallback<void(A1, A2)> { |
200 public: | 200 public: |
201 CancelableCallback() : weak_factory_(this) {} | 201 CancelableCallback() : weak_factory_(this) {} |
202 | 202 |
203 // |callback| must not be null. | 203 // |callback| must not be null. |
204 explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback) | 204 explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback) |
205 : weak_factory_(this), | 205 : weak_factory_(this), |
Charlie
2014/11/06 22:57:06
I'm getting an error in the constructor now that t
| |
206 callback_(callback) { | 206 callback_(callback) { |
207 DCHECK(!callback.is_null()); | 207 DCHECK(!callback.is_null()); |
208 InitializeForwarder(); | 208 InitializeForwarder(); |
209 } | 209 } |
210 | 210 |
211 ~CancelableCallback() {} | 211 ~CancelableCallback() {} |
212 | 212 |
213 // Cancels and drops the reference to the wrapped callback. | 213 // Cancels and drops the reference to the wrapped callback. |
214 void Cancel() { | 214 void Cancel() { |
215 weak_factory_.InvalidateWeakPtrs(); | 215 weak_factory_.InvalidateWeakPtrs(); |
(...skipping 30 matching lines...) Expand all Loading... | |
246 callback_.Run(a1, a2); | 246 callback_.Run(a1, a2); |
247 } | 247 } |
248 | 248 |
249 // Helper method to bind |forwarder_| using a weak pointer from | 249 // Helper method to bind |forwarder_| using a weak pointer from |
250 // |weak_factory_|. | 250 // |weak_factory_|. |
251 void InitializeForwarder() { | 251 void InitializeForwarder() { |
252 forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward, | 252 forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward, |
253 weak_factory_.GetWeakPtr()); | 253 weak_factory_.GetWeakPtr()); |
254 } | 254 } |
255 | 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. | 256 // The wrapper closure. |
260 base::Callback<void(A1, A2)> forwarder_; | 257 base::Callback<void(A1, A2)> forwarder_; |
261 | 258 |
262 // The stored closure that may be cancelled. | 259 // The stored closure that may be cancelled. |
263 base::Callback<void(A1, A2)> callback_; | 260 base::Callback<void(A1, A2)> callback_; |
264 | 261 |
262 // Used to ensure Forward() is not run when this object is destroyed. | |
263 base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_; | |
265 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 264 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
266 }; | 265 }; |
267 | 266 |
268 typedef CancelableCallback<void(void)> CancelableClosure; | 267 typedef CancelableCallback<void(void)> CancelableClosure; |
269 | 268 |
270 } // namespace base | 269 } // namespace base |
271 | 270 |
272 #endif // BASE_CANCELABLE_CALLBACK_H_ | 271 #endif // BASE_CANCELABLE_CALLBACK_H_ |
OLD | NEW |