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