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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 template <typename Sig> | 55 template <typename Sig> |
56 class CancelableCallback; | 56 class CancelableCallback; |
57 | 57 |
58 template <typename... A> | 58 template <typename... A> |
59 class CancelableCallback<void(A...)> { | 59 class CancelableCallback<void(A...)> { |
60 public: | 60 public: |
61 CancelableCallback() : weak_factory_(this) {} | 61 CancelableCallback() : weak_factory_(this) {} |
62 | 62 |
63 // |callback| must not be null. | 63 // |callback| must not be null. |
64 explicit CancelableCallback(const base::Callback<void(A...)>& callback) | 64 explicit CancelableCallback(const base::Callback<void(A...)>& callback) |
65 : weak_factory_(this), | 65 : callback_(callback), weak_factory_(this) { |
66 callback_(callback) { | |
67 DCHECK(!callback.is_null()); | 66 DCHECK(!callback.is_null()); |
68 InitializeForwarder(); | 67 InitializeForwarder(); |
69 } | 68 } |
70 | 69 |
71 ~CancelableCallback() {} | 70 ~CancelableCallback() {} |
72 | 71 |
73 // Cancels and drops the reference to the wrapped callback. | 72 // Cancels and drops the reference to the wrapped callback. |
74 void Cancel() { | 73 void Cancel() { |
75 weak_factory_.InvalidateWeakPtrs(); | 74 weak_factory_.InvalidateWeakPtrs(); |
76 forwarder_.Reset(); | 75 forwarder_.Reset(); |
(...skipping 29 matching lines...) Expand all Loading... |
106 callback_.Run(args...); | 105 callback_.Run(args...); |
107 } | 106 } |
108 | 107 |
109 // Helper method to bind |forwarder_| using a weak pointer from | 108 // Helper method to bind |forwarder_| using a weak pointer from |
110 // |weak_factory_|. | 109 // |weak_factory_|. |
111 void InitializeForwarder() { | 110 void InitializeForwarder() { |
112 forwarder_ = base::Bind(&CancelableCallback<void(A...)>::Forward, | 111 forwarder_ = base::Bind(&CancelableCallback<void(A...)>::Forward, |
113 weak_factory_.GetWeakPtr()); | 112 weak_factory_.GetWeakPtr()); |
114 } | 113 } |
115 | 114 |
116 // Used to ensure Forward() is not run when this object is destroyed. | |
117 // TODO(ckehoe): This should be the last class member. | |
118 // Move it there when crbug.com/433583 is fixed. | |
119 base::WeakPtrFactory<CancelableCallback<void(A...)> > weak_factory_; | |
120 | |
121 // The wrapper closure. | 115 // The wrapper closure. |
122 base::Callback<void(A...)> forwarder_; | 116 base::Callback<void(A...)> forwarder_; |
123 | 117 |
124 // The stored closure that may be cancelled. | 118 // The stored closure that may be cancelled. |
125 base::Callback<void(A...)> callback_; | 119 base::Callback<void(A...)> callback_; |
126 | 120 |
| 121 // Used to ensure Forward() is not run when this object is destroyed. |
| 122 base::WeakPtrFactory<CancelableCallback<void(A...)>> weak_factory_; |
| 123 |
127 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 124 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
128 }; | 125 }; |
129 | 126 |
130 typedef CancelableCallback<void(void)> CancelableClosure; | 127 typedef CancelableCallback<void(void)> CancelableClosure; |
131 | 128 |
132 } // namespace base | 129 } // namespace base |
133 | 130 |
134 #endif // BASE_CANCELABLE_CALLBACK_H_ | 131 #endif // BASE_CANCELABLE_CALLBACK_H_ |
OLD | NEW |