[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 5 | #ifndef BASE_CALLBACK_LIST_H_ |
| 6 | #define BASE_CALLBACK_LIST_H_ |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 7 | |
| 8 | #include <list> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 10 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 11 | #include "base/callback.h" |
| 12 | #include "base/compiler_specific.h" |
| 13 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 15 | |
| 16 | // OVERVIEW: |
| 17 | // |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 18 | // A container for a list of (repeating) callbacks. Unlike a normal vector or |
| 19 | // list, this container can be modified during iteration without invalidating |
| 20 | // the iterator. It safely handles the case of a callback removing itself or |
| 21 | // another callback from the list while callbacks are being run. |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 22 | // |
| 23 | // TYPICAL USAGE: |
| 24 | // |
| 25 | // class MyWidget { |
| 26 | // public: |
| 27 | // ... |
| 28 | // |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 29 | // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 30 | // RegisterCallback(const base::RepeatingCallback<void(const Foo&)>& cb) { |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 31 | // return callback_list_.Add(cb); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 32 | // } |
| 33 | // |
| 34 | // private: |
| 35 | // void NotifyFoo(const Foo& foo) { |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 36 | // callback_list_.Notify(foo); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 37 | // } |
| 38 | // |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 39 | // base::CallbackList<void(const Foo&)> callback_list_; |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 40 | // |
| 41 | // DISALLOW_COPY_AND_ASSIGN(MyWidget); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 42 | // }; |
| 43 | // |
| 44 | // |
| 45 | // class MyWidgetListener { |
| 46 | // public: |
| 47 | // MyWidgetListener::MyWidgetListener() { |
| 48 | // foo_subscription_ = MyWidget::GetCurrent()->RegisterCallback( |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 49 | // base::BindRepeating(&MyWidgetListener::OnFoo, this))); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 50 | // } |
| 51 | // |
| 52 | // MyWidgetListener::~MyWidgetListener() { |
| 53 | // // Subscription gets deleted automatically and will deregister |
| 54 | // // the callback in the process. |
| 55 | // } |
| 56 | // |
| 57 | // private: |
| 58 | // void OnFoo(const Foo& foo) { |
| 59 | // // Do something. |
| 60 | // } |
| 61 | // |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 62 | // std::unique_ptr<base::CallbackList<void(const Foo&)>::Subscription> |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 63 | // foo_subscription_; |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 64 | // |
| 65 | // DISALLOW_COPY_AND_ASSIGN(MyWidgetListener); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 66 | // }; |
| 67 | |
| 68 | namespace base { |
| 69 | |
| 70 | namespace internal { |
| 71 | |
| 72 | template <typename CallbackType> |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 73 | class CallbackListBase { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 74 | public: |
| 75 | class Subscription { |
| 76 | public: |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 77 | Subscription(CallbackListBase<CallbackType>* list, |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 78 | typename std::list<CallbackType>::iterator iter) |
| 79 | : list_(list), |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 80 | iter_(iter) { |
| 81 | } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 82 | |
| 83 | ~Subscription() { |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 84 | if (list_->active_iterator_count_) { |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 85 | iter_->Reset(); |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 86 | } else { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 87 | list_->callbacks_.erase(iter_); |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 88 | if (!list_->removal_callback_.is_null()) |
| 89 | list_->removal_callback_.Run(); |
| 90 | } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 94 | CallbackListBase<CallbackType>* list_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 95 | typename std::list<CallbackType>::iterator iter_; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(Subscription); |
| 98 | }; |
| 99 | |
| 100 | // Add a callback to the list. The callback will remain registered until the |
| 101 | // returned Subscription is destroyed, which must occur before the |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 102 | // CallbackList is destroyed. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 103 | std::unique_ptr<Subscription> Add(const CallbackType& cb) WARN_UNUSED_RESULT { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 104 | DCHECK(!cb.is_null()); |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 105 | return std::make_unique<Subscription>( |
| 106 | this, callbacks_.insert(callbacks_.end(), cb)); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 109 | // Sets a callback which will be run when a subscription list is changed. |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 110 | void set_removal_callback(const RepeatingClosure& callback) { |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 111 | removal_callback_ = callback; |
| 112 | } |
| 113 | |
| 114 | // Returns true if there are no subscriptions. This is only valid to call when |
| 115 | // not looping through the list. |
| 116 | bool empty() { |
| 117 | DCHECK_EQ(0, active_iterator_count_); |
| 118 | return callbacks_.empty(); |
| 119 | } |
| 120 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 121 | protected: |
| 122 | // An iterator class that can be used to access the list of callbacks. |
| 123 | class Iterator { |
| 124 | public: |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 125 | explicit Iterator(CallbackListBase<CallbackType>* list) |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 126 | : list_(list), |
| 127 | list_iter_(list_->callbacks_.begin()) { |
| 128 | ++list_->active_iterator_count_; |
| 129 | } |
| 130 | |
| 131 | Iterator(const Iterator& iter) |
| 132 | : list_(iter.list_), |
| 133 | list_iter_(iter.list_iter_) { |
| 134 | ++list_->active_iterator_count_; |
| 135 | } |
| 136 | |
| 137 | ~Iterator() { |
| 138 | if (list_ && --list_->active_iterator_count_ == 0) { |
| 139 | list_->Compact(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | CallbackType* GetNext() { |
| 144 | while ((list_iter_ != list_->callbacks_.end()) && list_iter_->is_null()) |
| 145 | ++list_iter_; |
| 146 | |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 147 | CallbackType* cb = nullptr; |
[email protected] | da04fbf | 2013-09-11 16:44:56 | [diff] [blame] | 148 | if (list_iter_ != list_->callbacks_.end()) { |
| 149 | cb = &(*list_iter_); |
| 150 | ++list_iter_; |
| 151 | } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 152 | return cb; |
| 153 | } |
| 154 | |
| 155 | private: |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 156 | CallbackListBase<CallbackType>* list_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 157 | typename std::list<CallbackType>::iterator list_iter_; |
| 158 | }; |
| 159 | |
[email protected] | abdd66e | 2013-10-09 23:28:21 | [diff] [blame] | 160 | CallbackListBase() : active_iterator_count_(0) {} |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 161 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 162 | ~CallbackListBase() { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 163 | DCHECK_EQ(0, active_iterator_count_); |
| 164 | DCHECK_EQ(0U, callbacks_.size()); |
| 165 | } |
| 166 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 167 | // Returns an instance of a CallbackListBase::Iterator which can be used |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 168 | // to run callbacks. |
| 169 | Iterator GetIterator() { |
| 170 | return Iterator(this); |
| 171 | } |
| 172 | |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 173 | // Compact the list: remove any entries which were nulled out during |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 174 | // iteration. |
| 175 | void Compact() { |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 176 | auto it = callbacks_.begin(); |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 177 | bool updated = false; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 178 | while (it != callbacks_.end()) { |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 179 | if ((*it).is_null()) { |
| 180 | updated = true; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 181 | it = callbacks_.erase(it); |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 182 | } else { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 183 | ++it; |
[email protected] | f28ef9a3 | 2014-05-12 16:36:10 | [diff] [blame] | 184 | } |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 185 | } |
davidben | f126cb2 | 2015-10-30 20:42:07 | [diff] [blame] | 186 | |
| 187 | if (updated && !removal_callback_.is_null()) |
| 188 | removal_callback_.Run(); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | private: |
| 192 | std::list<CallbackType> callbacks_; |
| 193 | int active_iterator_count_; |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 194 | RepeatingClosure removal_callback_; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 195 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 196 | DISALLOW_COPY_AND_ASSIGN(CallbackListBase); |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace internal |
| 200 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 201 | template <typename Sig> class CallbackList; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 202 | |
thakis | 6117a47 | 2014-11-20 01:15:14 | [diff] [blame] | 203 | template <typename... Args> |
| 204 | class CallbackList<void(Args...)> |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 205 | : public internal::CallbackListBase<RepeatingCallback<void(Args...)>> { |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 206 | public: |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 207 | using CallbackType = RepeatingCallback<void(Args...)>; |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 208 | |
Chris Watkins | 091d629 | 2017-12-13 04:25:58 | [diff] [blame] | 209 | CallbackList() = default; |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 210 | |
tzik | d2046ce3 | 2016-04-14 21:44:39 | [diff] [blame] | 211 | template <typename... RunArgs> |
| 212 | void Notify(RunArgs&&... args) { |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 213 | auto it = this->GetIterator(); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 214 | CallbackType* cb; |
Avi Drissman | 0939b549 | 2018-03-29 01:13:22 | [diff] [blame] | 215 | while ((cb = it.GetNext()) != nullptr) { |
thakis | 6117a47 | 2014-11-20 01:15:14 | [diff] [blame] | 216 | cb->Run(args...); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | private: |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 221 | DISALLOW_COPY_AND_ASSIGN(CallbackList); |
[email protected] | 243576f | 2013-09-25 13:56:23 | [diff] [blame] | 222 | }; |
| 223 | |
[email protected] | 893c816 | 2013-09-11 15:16:33 | [diff] [blame] | 224 | } // namespace base |
| 225 | |
[email protected] | 2a7cac0 | 2013-09-26 19:20:18 | [diff] [blame] | 226 | #endif // BASE_CALLBACK_LIST_H_ |