Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/callback_list.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
Avi Drissman | 63e1f99 | 2023-01-13 18:54:43 | [diff] [blame] | 9 | #include "base/functional/callback.h" |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | CallbackListSubscription::CallbackListSubscription() = default; |
| 14 | |
| 15 | CallbackListSubscription::CallbackListSubscription(base::OnceClosure closure) |
| 16 | : closure_(std::move(closure)) {} |
| 17 | |
| 18 | CallbackListSubscription::CallbackListSubscription( |
| 19 | CallbackListSubscription&& subscription) |
| 20 | : closure_(std::move(subscription.closure_)) {} |
| 21 | |
| 22 | CallbackListSubscription& CallbackListSubscription::operator=( |
| 23 | CallbackListSubscription&& subscription) { |
| 24 | // Note: This still works properly for self-assignment. |
| 25 | Run(); |
| 26 | closure_ = std::move(subscription.closure_); |
| 27 | return *this; |
| 28 | } |
| 29 | |
| 30 | CallbackListSubscription::~CallbackListSubscription() { |
| 31 | Run(); |
| 32 | } |
| 33 | |
| 34 | void CallbackListSubscription::Run() { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 35 | if (closure_) { |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 36 | std::move(closure_).Run(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 37 | } |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | } // namespace base |