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 CancelableCallback::Cancel() brings the object back to its natural,
13 // default-constructed state, i.e., CancelableCallback::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.
31 // void TimeoutCallback(const std::string& timeout_message) {
32 // FAIL() << timeout_message;
33 // run_loop.QuitWhenIdle();
36 // CancelableOnceClosure timeout(
37 // base::BindOnce(&TimeoutCallback, "Test timed out."));
38 // ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(),
39 // TimeDelta::FromSeconds(4));
40 // RunIntensiveTest();
42 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
45 #ifndef BASE_CANCELABLE_CALLBACK_H_
46 #define BASE_CANCELABLE_CALLBACK_H_
50 #include "base/base_export.h"
51 #include "base/bind.h"
52 #include "base/callback.h"
53 #include "base/callback_internal.h"
54 #include "base/check.h"
55 #include "base/compiler_specific.h"
56 #include "base/macros.h"
57 #include "base/memory/weak_ptr.h"
62 template <typename CallbackType>
63 class CancelableCallbackImpl {
65 CancelableCallbackImpl() {}
67 // |callback| must not be null.
68 explicit CancelableCallbackImpl(CallbackType callback)
69 : callback_(std::move(callback)) {
73 ~CancelableCallbackImpl() = default;
75 // Cancels and drops the reference to the wrapped callback.
77 weak_ptr_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(CallbackType callback) {
90 // Outstanding tasks (e.g., posted to a message loop) must not be called.
92 callback_ = std::move(callback);
95 // Returns a callback that can be disabled by calling Cancel().
96 CallbackType callback() const {
98 return CallbackType();
99 CallbackType forwarder;
100 MakeForwarder(&forwarder);
105 template <typename... Args>
106 void MakeForwarder(RepeatingCallback<void(Args...)>* out) const {
107 using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
108 ForwarderType forwarder = &CancelableCallbackImpl::ForwardRepeating;
109 *out = BindRepeating(forwarder, weak_ptr_factory_.GetWeakPtr());
112 template <typename... Args>
113 void MakeForwarder(OnceCallback<void(Args...)>* out) const {
114 using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
115 ForwarderType forwarder = &CancelableCallbackImpl::ForwardOnce;
116 *out = BindOnce(forwarder, weak_ptr_factory_.GetWeakPtr());
119 template <typename... Args>
120 void ForwardRepeating(Args... args) {
121 callback_.Run(std::forward<Args>(args)...);
124 template <typename... Args>
125 void ForwardOnce(Args... args) {
126 weak_ptr_factory_.InvalidateWeakPtrs();
127 std::move(callback_).Run(std::forward<Args>(args)...);
130 // The stored closure that may be cancelled.
131 CallbackType callback_;
132 mutable base::WeakPtrFactory<CancelableCallbackImpl> weak_ptr_factory_{this};
134 DISALLOW_COPY_AND_ASSIGN(CancelableCallbackImpl);
137 } // namespace internal
139 // Consider using base::WeakPtr directly instead of base::CancelableCallback for
140 // the task cancellation.
141 template <typename Signature>
142 using CancelableOnceCallback =
143 internal::CancelableCallbackImpl<OnceCallback<Signature>>;
144 using CancelableOnceClosure = CancelableOnceCallback<void()>;
146 template <typename Signature>
147 using CancelableRepeatingCallback =
148 internal::CancelableCallbackImpl<RepeatingCallback<Signature>>;
149 using CancelableRepeatingClosure = CancelableRepeatingCallback<void()>;
151 template <typename Signature>
152 using CancelableCallback = CancelableRepeatingCallback<Signature>;
153 using CancelableClosure = CancelableCallback<void()>;
157 #endif // BASE_CANCELABLE_CALLBACK_H_