[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | // This defines helpful methods for dealing with Callbacks. Because Callbacks |
| 6 | // are implemented using templates, with a class per callback signature, adding |
| 7 | // methods to Callback<> itself is unattractive (lots of extra code gets |
| 8 | // generated). Instead, consider adding methods here. |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 9 | |
| 10 | #ifndef BASE_CALLBACK_HELPERS_H_ |
| 11 | #define BASE_CALLBACK_HELPERS_H_ |
| 12 | |
tzik | faa3495 | 2017-04-20 12:19:50 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
| 15 | #include "base/atomicops.h" |
| 16 | #include "base/bind.h" |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 17 | #include "base/callback.h" |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 19 | #include "base/macros.h" |
tzik | faa3495 | 2017-04-20 12:19:50 | [diff] [blame] | 20 | #include "base/memory/ptr_util.h" |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | |
Bence Béky | 1532745 | 2018-05-10 20:59:07 | [diff] [blame] | 24 | // Prefer std::move() over ResetAndReturn(). |
tzik | d4bb5b7d | 2017-08-28 19:08:52 | [diff] [blame] | 25 | template <typename CallbackType> |
| 26 | CallbackType ResetAndReturn(CallbackType* cb) { |
| 27 | CallbackType ret(std::move(*cb)); |
tzik | 6eb0284 | 2017-02-22 09:13:19 | [diff] [blame] | 28 | DCHECK(!*cb); |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 29 | return ret; |
| 30 | } |
| 31 | |
tzik | faa3495 | 2017-04-20 12:19:50 | [diff] [blame] | 32 | namespace internal { |
| 33 | |
| 34 | template <typename... Args> |
| 35 | class AdaptCallbackForRepeatingHelper final { |
| 36 | public: |
| 37 | explicit AdaptCallbackForRepeatingHelper(OnceCallback<void(Args...)> callback) |
| 38 | : callback_(std::move(callback)) { |
| 39 | DCHECK(callback_); |
| 40 | } |
| 41 | |
| 42 | void Run(Args... args) { |
| 43 | if (subtle::NoBarrier_AtomicExchange(&has_run_, 1)) |
| 44 | return; |
| 45 | DCHECK(callback_); |
| 46 | std::move(callback_).Run(std::forward<Args>(args)...); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | volatile subtle::Atomic32 has_run_ = 0; |
| 51 | base::OnceCallback<void(Args...)> callback_; |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(AdaptCallbackForRepeatingHelper); |
| 54 | }; |
| 55 | |
| 56 | } // namespace internal |
| 57 | |
| 58 | // Wraps the given OnceCallback into a RepeatingCallback that relays its |
| 59 | // invocation to the original OnceCallback on the first invocation. The |
| 60 | // following invocations are just ignored. |
Avi Drissman | aed8223 | 2018-03-20 13:36:57 | [diff] [blame] | 61 | // |
| 62 | // Note that this deliberately subverts the Once/Repeating paradigm of Callbacks |
| 63 | // but helps ease the migration from old-style Callbacks. Avoid if possible; use |
| 64 | // if necessary for migration. TODO(tzik): Remove it. https://crbug.com/730593 |
tzik | faa3495 | 2017-04-20 12:19:50 | [diff] [blame] | 65 | template <typename... Args> |
| 66 | RepeatingCallback<void(Args...)> AdaptCallbackForRepeating( |
| 67 | OnceCallback<void(Args...)> callback) { |
| 68 | using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>; |
| 69 | return base::BindRepeating(&Helper::Run, |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 70 | std::make_unique<Helper>(std::move(callback))); |
tzik | faa3495 | 2017-04-20 12:19:50 | [diff] [blame] | 71 | } |
| 72 | |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 73 | // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures |
| 74 | // that the Closure is executed no matter how the current scope exits. |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 75 | class BASE_EXPORT ScopedClosureRunner { |
| 76 | public: |
| 77 | ScopedClosureRunner(); |
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 78 | explicit ScopedClosureRunner(OnceClosure closure); |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 79 | ~ScopedClosureRunner(); |
| 80 | |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 81 | ScopedClosureRunner(ScopedClosureRunner&& other); |
| 82 | |
sergeyu | 668613aa | 2016-07-08 00:34:27 | [diff] [blame] | 83 | // Releases the current closure if it's set and replaces it with the closure |
| 84 | // from |other|. |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 85 | ScopedClosureRunner& operator=(ScopedClosureRunner&& other); |
| 86 | |
| 87 | // Calls the current closure and resets it, so it wont be called again. |
sergeyu | 668613aa | 2016-07-08 00:34:27 | [diff] [blame] | 88 | void RunAndReset(); |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 89 | |
sergeyu | 668613aa | 2016-07-08 00:34:27 | [diff] [blame] | 90 | // Replaces closure with the new one releasing the old one without calling it. |
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 91 | void ReplaceClosure(OnceClosure closure); |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 92 | |
| 93 | // Releases the Closure without calling. |
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 94 | OnceClosure Release() WARN_UNUSED_RESULT; |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 95 | |
| 96 | private: |
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 97 | OnceClosure closure_; |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); |
| 100 | }; |
| 101 | |
[email protected] | 1192339c | 2012-03-24 20:37:27 | [diff] [blame] | 102 | } // namespace base |
| 103 | |
| 104 | #endif // BASE_CALLBACK_HELPERS_H_ |