cancelable_callback.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. //
  10. // NOTE:
  11. //
  12. // Calling CancelableCallback::Cancel() brings the object back to its natural,
  13. // default-constructed state, i.e., CancelableCallback::callback() will return
  14. // a null callback.
  15. //
  16. // THREAD-SAFETY:
  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. //
  29. // RunLoop run_loop;
  30. //
  31. // void TimeoutCallback(const std::string& timeout_message) {
  32. // FAIL() << timeout_message;
  33. // run_loop.QuitWhenIdle();
  34. // }
  35. //
  36. // CancelableOnceClosure timeout(
  37. // base::BindOnce(&TimeoutCallback, "Test timed out."));
  38. // ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, timeout.callback(),
  39. // TimeDelta::FromSeconds(4));
  40. // RunIntensiveTest();
  41. // run_loop.Run();
  42. // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
  43. //
  44. #ifndef BASE_CANCELABLE_CALLBACK_H_
  45. #define BASE_CANCELABLE_CALLBACK_H_
  46. #include <utility>
  47. #include "base/base_export.h"
  48. #include "base/bind.h"
  49. #include "base/callback.h"
  50. #include "base/callback_internal.h"
  51. #include "base/check.h"
  52. #include "base/compiler_specific.h"
  53. #include "base/memory/weak_ptr.h"
  54. namespace base {
  55. namespace internal {
  56. template <typename CallbackType>
  57. class CancelableCallbackImpl {
  58. public:
  59. CancelableCallbackImpl() = default;
  60. CancelableCallbackImpl(const CancelableCallbackImpl&) = delete;
  61. CancelableCallbackImpl& operator=(const CancelableCallbackImpl&) = delete;
  62. // |callback| must not be null.
  63. explicit CancelableCallbackImpl(CallbackType callback)
  64. : callback_(std::move(callback)) {
  65. DCHECK(callback_);
  66. }
  67. ~CancelableCallbackImpl() = default;
  68. // Cancels and drops the reference to the wrapped callback.
  69. void Cancel() {
  70. weak_ptr_factory_.InvalidateWeakPtrs();
  71. callback_.Reset();
  72. }
  73. // Returns true if the wrapped callback has been cancelled.
  74. bool IsCancelled() const {
  75. return callback_.is_null();
  76. }
  77. // Sets |callback| as the closure that may be cancelled. |callback| may not
  78. // be null. Outstanding and any previously wrapped callbacks are cancelled.
  79. void Reset(CallbackType callback) {
  80. DCHECK(callback);
  81. // Outstanding tasks (e.g., posted to a message loop) must not be called.
  82. Cancel();
  83. callback_ = std::move(callback);
  84. }
  85. // Returns a callback that can be disabled by calling Cancel().
  86. CallbackType callback() const {
  87. if (!callback_)
  88. return CallbackType();
  89. CallbackType forwarder;
  90. MakeForwarder(&forwarder);
  91. return forwarder;
  92. }
  93. private:
  94. template <typename... Args>
  95. void MakeForwarder(RepeatingCallback<void(Args...)>* out) const {
  96. using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
  97. ForwarderType forwarder = &CancelableCallbackImpl::ForwardRepeating;
  98. *out = BindRepeating(forwarder, weak_ptr_factory_.GetWeakPtr());
  99. }
  100. template <typename... Args>
  101. void MakeForwarder(OnceCallback<void(Args...)>* out) const {
  102. using ForwarderType = void (CancelableCallbackImpl::*)(Args...);
  103. ForwarderType forwarder = &CancelableCallbackImpl::ForwardOnce;
  104. *out = BindOnce(forwarder, weak_ptr_factory_.GetWeakPtr());
  105. }
  106. template <typename... Args>
  107. void ForwardRepeating(Args... args) {
  108. callback_.Run(std::forward<Args>(args)...);
  109. }
  110. template <typename... Args>
  111. void ForwardOnce(Args... args) {
  112. weak_ptr_factory_.InvalidateWeakPtrs();
  113. std::move(callback_).Run(std::forward<Args>(args)...);
  114. }
  115. // The stored closure that may be cancelled.
  116. CallbackType callback_;
  117. mutable base::WeakPtrFactory<CancelableCallbackImpl> weak_ptr_factory_{this};
  118. };
  119. } // namespace internal
  120. // Consider using base::WeakPtr directly instead of base::CancelableCallback for
  121. // the task cancellation.
  122. template <typename Signature>
  123. using CancelableOnceCallback =
  124. internal::CancelableCallbackImpl<OnceCallback<Signature>>;
  125. using CancelableOnceClosure = CancelableOnceCallback<void()>;
  126. template <typename Signature>
  127. using CancelableRepeatingCallback =
  128. internal::CancelableCallbackImpl<RepeatingCallback<Signature>>;
  129. using CancelableRepeatingClosure = CancelableRepeatingCallback<void()>;
  130. template <typename Signature>
  131. using CancelableCallback = CancelableRepeatingCallback<Signature>;
  132. using CancelableClosure = CancelableCallback<void()>;
  133. } // namespace base
  134. #endif // BASE_CANCELABLE_CALLBACK_H_