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 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
37 // ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(),
38 // TimeDelta::FromSeconds(4));
39 // RunIntensiveTest();
41 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
44 #ifndef BASE_CANCELABLE_CALLBACK_H_
45 #define BASE_CANCELABLE_CALLBACK_H_
49 #include "base/base_export.h"
50 #include "base/bind.h"
51 #include "base/callback.h"
52 #include "base/callback_internal.h"
53 #include "base/compiler_specific.h"
54 #include "base/logging.h"
55 #include "base/macros.h"
56 #include "base/memory/weak_ptr.h"
61 template <typename CallbackType>
62 class CancelableCallbackImpl {
64 CancelableCallbackImpl() : weak_ptr_factory_(this) {}
66 // |callback| must not be null.
67 explicit CancelableCallbackImpl(CallbackType callback)
68 : callback_(std::move(callback)), weak_ptr_factory_(this) {
72 ~CancelableCallbackImpl() = default;
74 // Cancels and drops the reference to the wrapped callback.
76 weak_ptr_factory_.InvalidateWeakPtrs();
80 // Returns true if the wrapped callback has been cancelled.
81 bool IsCancelled() const {
82 return callback_.is_null();
85 // Sets |callback| as the closure that may be cancelled. |callback| may not
86 // be null. Outstanding and any previously wrapped callbacks are cancelled.
87 void Reset(CallbackType callback) {
89 // Outstanding tasks (e.g., posted to a message loop) must not be called.
91 callback_ = std::move(callback);
94 // Returns a callback that can be disabled by calling Cancel().
95 CallbackType callback() const {
97 return CallbackType();
98 CallbackType forwarder;
99 MakeForwarder(&forwarder);
104 template <typename... Args>
105 void MakeForwarder(RepeatingCallback<void(Args...)>* out) const {
106 using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
107 ForwarderType forwarder = &CancelableCallbackImpl::ForwardRepeating;
108 *out = BindRepeating(forwarder, weak_ptr_factory_.GetWeakPtr());
111 template <typename... Args>
112 void MakeForwarder(OnceCallback<void(Args...)>* out) const {
113 using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
114 ForwarderType forwarder = &CancelableCallbackImpl::ForwardOnce;
115 *out = BindOnce(forwarder, weak_ptr_factory_.GetWeakPtr());
118 template <typename... Args>
119 void ForwardRepeating(Args... args) {
120 callback_.Run(std::forward<Args>(args)...);
123 template <typename... Args>
124 void ForwardOnce(Args... args) {
125 weak_ptr_factory_.InvalidateWeakPtrs();
126 std::move(callback_).Run(std::forward<Args>(args)...);
129 // The stored closure that may be cancelled.
130 CallbackType callback_;
131 mutable base::WeakPtrFactory<CancelableCallbackImpl> weak_ptr_factory_;
133 DISALLOW_COPY_AND_ASSIGN(CancelableCallbackImpl);
136 } // namespace internal
138 // Consider using base::WeakPtr directly instead of base::CancelableCallback for
139 // the task cancellation.
140 template <typename Signature>
141 using CancelableOnceCallback =
142 internal::CancelableCallbackImpl<OnceCallback<Signature>>;
143 using CancelableOnceClosure = CancelableOnceCallback<void()>;
145 template <typename Signature>
146 using CancelableRepeatingCallback =
147 internal::CancelableCallbackImpl<RepeatingCallback<Signature>>;
148 using CancelableRepeatingClosure = CancelableOnceCallback<void()>;
150 template <typename Signature>
151 using CancelableCallback = CancelableRepeatingCallback<Signature>;
152 using CancelableClosure = CancelableCallback<void()>;
156 #endif // BASE_CANCELABLE_CALLBACK_H_