Chromium Code Reviews| 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 // Thread-safety notes: | 10 // NOTE: |
| 11 // | |
| 12 // Calling Cancel() brings the object back to its natural, default-constructed | |
|
michaeln
2011/11/30 20:04:22
ah... i see
| |
| 13 // state, i.e., callback() will return a null callback. | |
|
awong
2011/11/30 20:28:17
callback() -> CancellableCallback::callback()
James Hawkins
2011/11/30 20:31:05
Done.
| |
| 14 // | |
| 15 // THREAD-SAFETY: | |
| 11 // | 16 // |
| 12 // CancelableCallback objects must be created on, posted to, cancelled on, and | 17 // CancelableCallback objects must be created on, posted to, cancelled on, and |
| 13 // destroyed on the same thread. | 18 // destroyed on the same thread. |
| 14 // | 19 // |
| 15 // | 20 // |
| 16 // EXAMPLE USAGE: | 21 // EXAMPLE USAGE: |
| 17 // | 22 // |
| 18 // In the following example, the test is verifying that RunIntensiveTest() | 23 // In the following example, the test is verifying that RunIntensiveTest() |
| 19 // Quit()s the message loop within 4 seconds. The cancelable callback is posted | 24 // Quit()s the message loop within 4 seconds. The cancelable callback is posted |
| 20 // to the message loop, the intensive test runs, the message loop is run, | 25 // to the message loop, the intensive test runs, the message loop is run, |
| 21 // then the callback is cancelled. | 26 // then the callback is cancelled. |
| 22 // | 27 // |
| 23 // void TimeoutCallback(const std::string& timeout_message) { | 28 // void TimeoutCallback(const std::string& timeout_message) { |
| 24 // FAIL() << timeout_message; | 29 // FAIL() << timeout_message; |
| 25 // MessageLoop::current()->Quit(); | 30 // MessageLoop::current()->Quit(); |
| 26 // } | 31 // } |
| 27 // | 32 // |
| 28 // CancelableCallback timeout(base::Bind(&TimeoutCallback, "Test timed out.")); | 33 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out.")); |
| 29 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(), | 34 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(), |
| 30 // 4000) // 4 seconds to run. | 35 // 4000) // 4 seconds to run. |
| 31 // RunIntensiveTest(); | 36 // RunIntensiveTest(); |
| 32 // MessageLoop::current()->Run(); | 37 // MessageLoop::current()->Run(); |
| 33 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs. | 38 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs. |
| 34 // | 39 // |
| 35 | 40 |
| 36 #ifndef BASE_CANCELABLE_CALLBACK_H_ | 41 #ifndef BASE_CANCELABLE_CALLBACK_H_ |
| 37 #define BASE_CANCELABLE_CALLBACK_H_ | 42 #define BASE_CANCELABLE_CALLBACK_H_ |
| 38 #pragma once | 43 #pragma once |
| 39 | 44 |
| 45 #include "base/base_export.h" | |
| 46 #include "base/bind.h" | |
| 40 #include "base/callback.h" | 47 #include "base/callback.h" |
| 41 #include "base/base_export.h" | 48 #include "base/callback_internal.h" |
| 49 #include "base/compiler_specific.h" | |
| 50 #include "base/logging.h" | |
| 42 #include "base/memory/weak_ptr.h" | 51 #include "base/memory/weak_ptr.h" |
| 43 | 52 |
| 44 namespace base { | 53 namespace base { |
| 45 | 54 |
| 46 class BASE_EXPORT CancelableCallback { | 55 template <typename Sig> |
| 56 class CancelableCallback; | |
| 57 | |
| 58 template <> | |
| 59 class CancelableCallback<void(void)> { | |
| 47 public: | 60 public: |
| 48 CancelableCallback(); | 61 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
| 49 | 62 |
| 50 // |callback| must not be null. | 63 // |callback| must not be null. |
| 51 explicit CancelableCallback(const base::Closure& callback); | 64 explicit CancelableCallback(const base::Callback<void(void)>& callback) |
| 65 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 66 callback_(callback) { | |
| 67 DCHECK(!callback.is_null()); | |
| 68 InitializeForwarder(); | |
| 69 } | |
| 52 | 70 |
| 53 ~CancelableCallback(); | 71 ~CancelableCallback() {} |
| 54 | 72 |
| 55 // Cancels and drops the reference to the wrapped callback. | 73 // Cancels and drops the reference to the wrapped callback. |
| 56 void Cancel(); | 74 void Cancel() { |
| 75 weak_factory_.InvalidateWeakPtrs(); | |
| 76 forwarder_.Reset(); | |
| 77 callback_.Reset(); | |
| 78 } | |
| 57 | 79 |
| 58 // Returns true if the wrapped callback has been cancelled. | 80 // Returns true if the wrapped callback has been cancelled. |
| 59 bool IsCancelled() const; | 81 bool IsCancelled() const { |
| 82 return callback_.is_null(); | |
| 83 } | |
| 60 | 84 |
| 61 // Sets |callback| as the closure that may be cancelled. |callback| may not | 85 // Sets |callback| as the closure that may be cancelled. |callback| may not |
| 62 // be null. Outstanding and any previously wrapped callbacks are cancelled. | 86 // be null. Outstanding and any previously wrapped callbacks are cancelled. |
| 63 void Reset(const base::Closure& callback); | 87 void Reset(const base::Callback<void(void)>& callback) { |
| 88 DCHECK(!callback.is_null()); | |
| 89 | |
| 90 // Outstanding tasks (e.g., posted to a message loop) must not be called. | |
| 91 Cancel(); | |
| 92 | |
| 93 // |forwarder_| is no longer valid after Cancel(), so re-bind. | |
| 94 InitializeForwarder(); | |
| 95 | |
| 96 callback_ = callback; | |
| 97 } | |
| 64 | 98 |
| 65 // Returns a callback that can be disabled by calling Cancel(). | 99 // Returns a callback that can be disabled by calling Cancel(). |
| 66 const base::Closure& callback() const; | 100 const base::Callback<void(void)>& callback() const { |
| 101 return forwarder_; | |
| 102 } | |
| 67 | 103 |
| 68 private: | 104 private: |
| 69 void RunCallback(); | 105 void Forward() { |
| 106 callback_.Run(); | |
| 107 } | |
| 70 | 108 |
| 71 // Helper method to bind |forwarder_| using a weak pointer from | 109 // Helper method to bind |forwarder_| using a weak pointer from |
| 72 // |weak_factory_|. | 110 // |weak_factory_|. |
| 73 void InitializeForwarder(); | 111 void InitializeForwarder() { |
| 112 forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward, | |
| 113 weak_factory_.GetWeakPtr()); | |
| 114 } | |
| 74 | 115 |
| 75 // Used to ensure RunCallback() is not run when this object is destroyed. | 116 // Used to ensure Forward() is not run when this object is destroyed. |
| 76 base::WeakPtrFactory<CancelableCallback> weak_factory_; | 117 base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_; |
| 77 | 118 |
| 78 // The wrapper closure. | 119 // The wrapper closure. |
| 79 base::Closure forwarder_; | 120 base::Callback<void(void)> forwarder_; |
| 80 | 121 |
| 81 // The stored closure that may be cancelled. | 122 // The stored closure that may be cancelled. |
| 82 base::Closure callback_; | 123 base::Callback<void(void)> callback_; |
| 83 | 124 |
| 84 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | 125 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
| 85 }; | 126 }; |
| 86 | 127 |
| 128 template <typename A1> | |
| 129 class CancelableCallback<void(A1)> { | |
| 130 public: | |
| 131 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | |
| 132 | |
| 133 // |callback| must not be null. | |
| 134 explicit CancelableCallback(const base::Callback<void(A1)>& callback) | |
| 135 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 136 callback_(callback) { | |
| 137 DCHECK(!callback.is_null()); | |
| 138 InitializeForwarder(); | |
| 139 } | |
| 140 | |
| 141 ~CancelableCallback() {} | |
| 142 | |
| 143 // Cancels and drops the reference to the wrapped callback. | |
| 144 void Cancel() { | |
| 145 weak_factory_.InvalidateWeakPtrs(); | |
| 146 forwarder_.Reset(); | |
| 147 callback_.Reset(); | |
| 148 } | |
| 149 | |
| 150 // Returns true if the wrapped callback has been cancelled. | |
| 151 bool IsCancelled() const { | |
| 152 return callback_.is_null(); | |
| 153 } | |
| 154 | |
| 155 // Sets |callback| as the closure that may be cancelled. |callback| may not | |
| 156 // be null. Outstanding and any previously wrapped callbacks are cancelled. | |
| 157 void Reset(const base::Callback<void(A1)>& callback) { | |
| 158 DCHECK(!callback.is_null()); | |
| 159 | |
| 160 // Outstanding tasks (e.g., posted to a message loop) must not be called. | |
| 161 Cancel(); | |
| 162 | |
| 163 // |forwarder_| is no longer valid after Cancel(), so re-bind. | |
| 164 InitializeForwarder(); | |
| 165 | |
| 166 callback_ = callback; | |
| 167 } | |
| 168 | |
| 169 // Returns a callback that can be disabled by calling Cancel(). | |
| 170 const base::Callback<void(A1)>& callback() const { | |
| 171 return forwarder_; | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 void Forward( | |
| 176 typename internal::CallbackParamTraits<A1>::ForwardType a1) const { | |
| 177 callback_.Run(a1); | |
| 178 } | |
| 179 | |
| 180 // Helper method to bind |forwarder_| using a weak pointer from | |
| 181 // |weak_factory_|. | |
| 182 void InitializeForwarder() { | |
| 183 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, | |
| 184 weak_factory_.GetWeakPtr()); | |
| 185 } | |
| 186 | |
| 187 // Used to ensure Forward() is not run when this object is destroyed. | |
| 188 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; | |
| 189 | |
| 190 // The wrapper closure. | |
| 191 base::Callback<void(A1)> forwarder_; | |
| 192 | |
| 193 // The stored closure that may be cancelled. | |
| 194 base::Callback<void(A1)> callback_; | |
| 195 | |
| 196 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | |
| 197 }; | |
| 198 | |
| 199 typedef CancelableCallback<void(void)> CancelableClosure; | |
| 200 | |
| 87 } // namespace base | 201 } // namespace base |
| 88 | 202 |
| 89 #endif // BASE_CANCELABLE_CALLBACK_H_ | 203 #endif // BASE_CANCELABLE_CALLBACK_H_ |
| OLD | NEW |