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