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
3 // found in the LICENSE file.
5 // CancelableCallback is a wrapper around base::Callback that allows
6 // cancellation of a callback. CancelableCallback takes a reference on the
7 // wrapped callback until this object is destroyed or Reset()/Cancel() are
12 // Calling CancellableCallback::Cancel() brings the object back to its natural,
13 // default-constructed state, i.e., CancellableCallback::callback() will return
18 // CancelableCallback objects must be created on, posted to, cancelled on, and
19 // destroyed on the same thread.
24 // In the following example, the test is verifying that RunIntensiveTest()
25 // Quit()s the message loop within 4 seconds. The cancelable callback is posted
26 // to the message loop, the intensive test runs, the message loop is run,
27 // then the callback is cancelled.
29 // void TimeoutCallback(const std::string& timeout_message) {
30 // FAIL() << timeout_message;
31 // MessageLoop::current()->Quit();
34 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
35 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
36 // 4000) // 4 seconds to run.
37 // RunIntensiveTest();
38 // MessageLoop::current()->Run();
39 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
42 #ifndef BASE_CANCELABLE_CALLBACK_H_
43 #define BASE_CANCELABLE_CALLBACK_H_
46 #include "base/base_export.h"
47 #include "base/bind.h"
48 #include "base/callback.h"
49 #include "base/callback_internal.h"
50 #include "base/compiler_specific.h"
51 #include "base/logging.h"
52 #include "base/memory/weak_ptr.h"
56 template <typename Sig
>
57 class CancelableCallback
;
60 class CancelableCallback
<void(void)> {
62 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
64 // |callback| must not be null.
65 explicit CancelableCallback(const base::Callback
<void(void)>& callback
)
66 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
68 DCHECK(!callback
.is_null());
69 InitializeForwarder();
72 ~CancelableCallback() {}
74 // Cancels and drops the reference to the wrapped callback.
76 weak_factory_
.InvalidateWeakPtrs();
81 // Returns true if the wrapped callback has been cancelled.
82 bool IsCancelled() const {
83 return callback_
.is_null();
86 // Sets |callback| as the closure that may be cancelled. |callback| may not
87 // be null. Outstanding and any previously wrapped callbacks are cancelled.
88 void Reset(const base::Callback
<void(void)>& callback
) {
89 DCHECK(!callback
.is_null());
91 // Outstanding tasks (e.g., posted to a message loop) must not be called.
94 // |forwarder_| is no longer valid after Cancel(), so re-bind.
95 InitializeForwarder();
100 // Returns a callback that can be disabled by calling Cancel().
101 const base::Callback
<void(void)>& callback() const {
110 // Helper method to bind |forwarder_| using a weak pointer from
112 void InitializeForwarder() {
113 forwarder_
= base::Bind(&CancelableCallback
<void(void)>::Forward
,
114 weak_factory_
.GetWeakPtr());
117 // Used to ensure Forward() is not run when this object is destroyed.
118 base::WeakPtrFactory
<CancelableCallback
<void(void)> > weak_factory_
;
120 // The wrapper closure.
121 base::Callback
<void(void)> forwarder_
;
123 // The stored closure that may be cancelled.
124 base::Callback
<void(void)> callback_
;
126 DISALLOW_COPY_AND_ASSIGN(CancelableCallback
);
129 template <typename A1
>
130 class CancelableCallback
<void(A1
)> {
132 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
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();
142 ~CancelableCallback() {}
144 // Cancels and drops the reference to the wrapped callback.
146 weak_factory_
.InvalidateWeakPtrs();
151 // Returns true if the wrapped callback has been cancelled.
152 bool IsCancelled() const {
153 return callback_
.is_null();
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());
161 // Outstanding tasks (e.g., posted to a message loop) must not be called.
164 // |forwarder_| is no longer valid after Cancel(), so re-bind.
165 InitializeForwarder();
167 callback_
= callback
;
170 // Returns a callback that can be disabled by calling Cancel().
171 const base::Callback
<void(A1
)>& callback() const {
176 void Forward(A1 a1
) const {
180 // Helper method to bind |forwarder_| using a weak pointer from
182 void InitializeForwarder() {
183 forwarder_
= base::Bind(&CancelableCallback
<void(A1
)>::Forward
,
184 weak_factory_
.GetWeakPtr());
187 // Used to ensure Forward() is not run when this object is destroyed.
188 base::WeakPtrFactory
<CancelableCallback
<void(A1
)> > weak_factory_
;
190 // The wrapper closure.
191 base::Callback
<void(A1
)> forwarder_
;
193 // The stored closure that may be cancelled.
194 base::Callback
<void(A1
)> callback_
;
196 DISALLOW_COPY_AND_ASSIGN(CancelableCallback
);
199 typedef CancelableCallback
<void(void)> CancelableClosure
;
203 #endif // BASE_CANCELABLE_CALLBACK_H_