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( | 176 void Forward(A1 a1) const { |
177 typename internal::CallbackParamTraits<A1>::ForwardType a1) const { | |
178 callback_.Run(a1); | 177 callback_.Run(a1); |
179 } | 178 } |
180 | 179 |
181 // Helper method to bind |forwarder_| using a weak pointer from | 180 // Helper method to bind |forwarder_| using a weak pointer from |
182 // |weak_factory_|. | 181 // |weak_factory_|. |
183 void InitializeForwarder() { | 182 void InitializeForwarder() { |
184 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, | 183 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, |
185 weak_factory_.GetWeakPtr()); | 184 weak_factory_.GetWeakPtr()); |
186 } | 185 } |
187 | 186 |
188 // Used to ensure Forward() is not run when this object is destroyed. | 187 // Used to ensure Forward() is not run when this object is destroyed. |
189 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; | 188 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; |
190 | 189 |
191 // The wrapper closure. | 190 // The wrapper closure. |
192 base::Callback<void(A1)> forwarder_; | 191 base::Callback<void(A1)> forwarder_; |
193 | 192 |
194 // The stored closure that may be cancelled. | 193 // The stored closure that may be cancelled. |
195 base::Callback<void(A1)> callback_; | 194 base::Callback<void(A1)> callback_; |
196 | 195 |
197 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 196 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
198 }; | 197 }; |
199 | 198 |
200 typedef CancelableCallback<void(void)> CancelableClosure; | 199 typedef CancelableCallback<void(void)> CancelableClosure; |
201 | 200 |
202 } // namespace base | 201 } // namespace base |
203 | 202 |
204 #endif // BASE_CANCELABLE_CALLBACK_H_ | 203 #endif // BASE_CANCELABLE_CALLBACK_H_ |
OLD | NEW |