[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 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. |
| 4 | // |
| 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 |
| 8 | // called. |
| 9 | // |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 10 | // NOTE: |
| 11 | // |
[email protected] | 33db319 | 2013-10-18 13:07:00 | [diff] [blame] | 12 | // Calling CancelableCallback::Cancel() brings the object back to its natural, |
| 13 | // default-constructed state, i.e., CancelableCallback::callback() will return |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 14 | // a null callback. |
| 15 | // |
| 16 | // THREAD-SAFETY: |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 17 | // |
| 18 | // CancelableCallback objects must be created on, posted to, cancelled on, and |
| 19 | // destroyed on the same thread. |
| 20 | // |
| 21 | // |
| 22 | // EXAMPLE USAGE: |
| 23 | // |
| 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. |
| 28 | // |
fdoray | c5e45f6 | 2016-09-22 15:06:07 | [diff] [blame] | 29 | // RunLoop run_loop; |
| 30 | // |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 31 | // void TimeoutCallback(const std::string& timeout_message) { |
| 32 | // FAIL() << timeout_message; |
fdoray | c5e45f6 | 2016-09-22 15:06:07 | [diff] [blame] | 33 | // run_loop.QuitWhenIdle(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 34 | // } |
| 35 | // |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 36 | // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out.")); |
fdoray | c5e45f6 | 2016-09-22 15:06:07 | [diff] [blame] | 37 | // ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(), |
| 38 | // TimeDelta::FromSeconds(4)); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 39 | // RunIntensiveTest(); |
fdoray | c5e45f6 | 2016-09-22 15:06:07 | [diff] [blame] | 40 | // run_loop.Run(); |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 41 | // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs. |
| 42 | // |
| 43 | |
| 44 | #ifndef BASE_CANCELABLE_CALLBACK_H_ |
| 45 | #define BASE_CANCELABLE_CALLBACK_H_ |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 46 | |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 47 | #include <utility> |
| 48 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 49 | #include "base/base_export.h" |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 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" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 55 | #include "base/macros.h" |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 56 | #include "base/memory/weak_ptr.h" |
| 57 | |
| 58 | namespace base { |
| 59 | |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 60 | template <typename Sig> |
| 61 | class CancelableCallback; |
| 62 | |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 63 | template <typename... A> |
| 64 | class CancelableCallback<void(A...)> { |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 65 | public: |
[email protected] | c9f977b | 2013-04-25 12:17:15 | [diff] [blame] | 66 | CancelableCallback() : weak_factory_(this) {} |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 67 | |
| 68 | // |callback| must not be null. |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 69 | explicit CancelableCallback(const base::Callback<void(A...)>& callback) |
dmichael | 57c885a | 2015-01-14 01:38:08 | [diff] [blame] | 70 | : callback_(callback), weak_factory_(this) { |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 71 | DCHECK(!callback.is_null()); |
| 72 | InitializeForwarder(); |
| 73 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 74 | |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 75 | ~CancelableCallback() {} |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 76 | |
| 77 | // Cancels and drops the reference to the wrapped callback. |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 78 | void Cancel() { |
| 79 | weak_factory_.InvalidateWeakPtrs(); |
| 80 | forwarder_.Reset(); |
| 81 | callback_.Reset(); |
| 82 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 83 | |
| 84 | // Returns true if the wrapped callback has been cancelled. |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 85 | bool IsCancelled() const { |
| 86 | return callback_.is_null(); |
| 87 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 88 | |
| 89 | // Sets |callback| as the closure that may be cancelled. |callback| may not |
| 90 | // be null. Outstanding and any previously wrapped callbacks are cancelled. |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 91 | void Reset(const base::Callback<void(A...)>& callback) { |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 92 | DCHECK(!callback.is_null()); |
| 93 | |
| 94 | // Outstanding tasks (e.g., posted to a message loop) must not be called. |
| 95 | Cancel(); |
| 96 | |
| 97 | // |forwarder_| is no longer valid after Cancel(), so re-bind. |
| 98 | InitializeForwarder(); |
| 99 | |
| 100 | callback_ = callback; |
| 101 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 102 | |
| 103 | // Returns a callback that can be disabled by calling Cancel(). |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 104 | const base::Callback<void(A...)>& callback() const { |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 105 | return forwarder_; |
| 106 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 107 | |
| 108 | private: |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 109 | void Forward(A... args) const { |
kuznetsovs | b020bcd8 | 2016-04-13 13:01:47 | [diff] [blame] | 110 | callback_.Run(std::forward<A>(args)...); |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 111 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 112 | |
| 113 | // Helper method to bind |forwarder_| using a weak pointer from |
| 114 | // |weak_factory_|. |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 115 | void InitializeForwarder() { |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 116 | forwarder_ = base::Bind(&CancelableCallback<void(A...)>::Forward, |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 117 | weak_factory_.GetWeakPtr()); |
| 118 | } |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 119 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 120 | // The wrapper closure. |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 121 | base::Callback<void(A...)> forwarder_; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 122 | |
| 123 | // The stored closure that may be cancelled. |
ckehoe | 79411ab | 2014-11-15 06:00:52 | [diff] [blame] | 124 | base::Callback<void(A...)> callback_; |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 125 | |
dmichael | 57c885a | 2015-01-14 01:38:08 | [diff] [blame] | 126 | // Used to ensure Forward() is not run when this object is destroyed. |
| 127 | base::WeakPtrFactory<CancelableCallback<void(A...)>> weak_factory_; |
| 128 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(CancelableCallback); |
| 130 | }; |
| 131 | |
[email protected] | d4d57df | 2011-11-30 20:33:52 | [diff] [blame] | 132 | typedef CancelableCallback<void(void)> CancelableClosure; |
| 133 | |
[email protected] | b77576f5 | 2011-11-24 04:12:04 | [diff] [blame] | 134 | } // namespace base |
| 135 | |
| 136 | #endif // BASE_CANCELABLE_CALLBACK_H_ |