Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 503631c | 2008-10-22 23:09:21 | [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 | #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_ |
| 6 | #define BASE_OBSERVER_LIST_THREADSAFE_H_ |
| 7 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 8 | #include <unordered_map> |
Lei Zhang | 52637ed | 2019-02-20 01:38:37 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 10 | |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 11 | #include "base/auto_reset.h" |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 12 | #include "base/base_export.h" |
David Sanders | 8cfb63a | 2022-04-14 19:36:30 | [diff] [blame] | 13 | #include "base/check.h" |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 14 | #include "base/check_op.h" |
Lei Zhang | c0f9fc5 | 2021-05-22 08:00:53 | [diff] [blame] | 15 | #include "base/containers/contains.h" |
David Sanders | fc1f17fa | 2022-04-15 00:15:49 | [diff] [blame] | 16 | #include "base/dcheck_is_on.h" |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 17 | #include "base/debug/stack_trace.h" |
Ari Chivukula | 621212d0 | 2023-01-11 11:53:10 | [diff] [blame] | 18 | #include "base/functional/bind.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 19 | #include "base/location.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 20 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 21 | #include "base/memory/ref_counted.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 22 | #include "base/observer_list.h" |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 23 | #include "base/strings/strcat.h" |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 24 | #include "base/synchronization/lock.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 25 | #include "base/task/sequenced_task_runner.h" |
Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 26 | #include "base/task/single_thread_task_runner.h" |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 27 | #include "build/build_config.h" |
| 28 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // |
| 31 | // OVERVIEW: |
| 32 | // |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 33 | // A thread-safe container for a list of observers. This is similar to the |
| 34 | // observer_list (see observer_list.h), but it is more robust for multi- |
| 35 | // threaded situations. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 36 | // |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 37 | // The following use cases are supported: |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 38 | // * Observers can register for notifications from any sequence. They are |
| 39 | // always notified on the sequence from which they were registered. |
| 40 | // * Any sequence may trigger a notification via Notify(). |
| 41 | // * Observers can remove themselves from the observer list inside of a |
| 42 | // callback. |
| 43 | // * If one sequence is notifying observers concurrently with an observer |
| 44 | // removing itself from the observer list, the notifications will be |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 45 | // silently dropped. However if the observer is currently inside a |
| 46 | // notification callback, the callback will finish running. |
| 47 | // |
| 48 | // By default, observers can be removed from any sequence. However this can be |
| 49 | // error-prone since an observer may be running a callback when it's removed, |
| 50 | // in which case it isn't safe to delete until the callback is finished. |
| 51 | // Consider using the RemoveObserverPolicy::kAddingSequenceOnly template |
| 52 | // parameter, which will CHECK that observers are only removed from the |
| 53 | // sequence where they were added (which is also the sequence that runs |
| 54 | // callbacks). |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 55 | // |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 56 | // The drawback of the threadsafe observer list is that notifications are not |
| 57 | // as real-time as the non-threadsafe version of this class. Notifications |
| 58 | // will always be done via PostTask() to another sequence, whereas with the |
Francois Doray | 4ffc9e1 | 2021-05-07 14:34:43 | [diff] [blame] | 59 | // non-thread-safe ObserverList, notifications happen synchronously. |
| 60 | // |
| 61 | // Note: this class previously supported synchronous notifications for |
| 62 | // same-sequence observers, but it was error-prone and removed in |
| 63 | // crbug.com/1193750, think twice before re-considering this paradigm. |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 64 | // |
| 65 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | bf68712 | 2011-01-11 21:19:54 | [diff] [blame] | 66 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 67 | namespace base { |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 68 | namespace internal { |
| 69 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 70 | class BASE_EXPORT ObserverListThreadSafeBase |
| 71 | : public RefCountedThreadSafe<ObserverListThreadSafeBase> { |
| 72 | public: |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 73 | struct NotificationDataBase { |
| 74 | NotificationDataBase(void* observer_list_in, const Location& from_here_in) |
| 75 | : observer_list(observer_list_in), from_here(from_here_in) {} |
| 76 | |
| 77 | raw_ptr<void> observer_list; |
| 78 | Location from_here; |
| 79 | }; |
| 80 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 81 | ObserverListThreadSafeBase() = default; |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 82 | ObserverListThreadSafeBase(const ObserverListThreadSafeBase&) = delete; |
| 83 | ObserverListThreadSafeBase& operator=(const ObserverListThreadSafeBase&) = |
| 84 | delete; |
tzik | b7990cc | 2016-08-25 02:19:36 | [diff] [blame] | 85 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 86 | protected: |
| 87 | template <typename ObserverType, typename Method> |
| 88 | struct Dispatcher; |
| 89 | |
| 90 | template <typename ObserverType, typename ReceiverType, typename... Params> |
| 91 | struct Dispatcher<ObserverType, void (ReceiverType::*)(Params...)> { |
| 92 | static void Run(void (ReceiverType::*m)(Params...), |
| 93 | Params... params, |
| 94 | ObserverType* obj) { |
| 95 | (obj->*m)(std::forward<Params>(params)...); |
| 96 | } |
| 97 | }; |
| 98 | |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 99 | static const NotificationDataBase*& GetCurrentNotification(); |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 100 | |
| 101 | virtual ~ObserverListThreadSafeBase() = default; |
| 102 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 103 | private: |
| 104 | friend class RefCountedThreadSafe<ObserverListThreadSafeBase>; |
[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 105 | }; |
| 106 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 107 | } // namespace internal |
| 108 | |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 109 | enum class RemoveObserverPolicy { |
| 110 | // Observers can be removed from any sequence. |
| 111 | kAnySequence, |
| 112 | // Observers can only be removed from the sequence that added them. |
| 113 | kAddingSequenceOnly, |
| 114 | }; |
| 115 | |
| 116 | template <class ObserverType, |
| 117 | RemoveObserverPolicy RemovePolicy = |
| 118 | RemoveObserverPolicy::kAnySequence> |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 119 | class ObserverListThreadSafe : public internal::ObserverListThreadSafeBase { |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 120 | using Self = ObserverListThreadSafe<ObserverType, RemovePolicy>; |
| 121 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 122 | public: |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 123 | enum class AddObserverResult { |
| 124 | kBecameNonEmpty, |
| 125 | kWasAlreadyNonEmpty, |
| 126 | }; |
| 127 | enum class RemoveObserverResult { |
| 128 | kWasOrBecameEmpty, |
| 129 | kRemainsNonEmpty, |
| 130 | }; |
| 131 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 132 | ObserverListThreadSafe() = default; |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 133 | explicit ObserverListThreadSafe(ObserverListPolicy policy) |
| 134 | : policy_(policy) {} |
David Bienvenu | 5f4d4f0 | 2020-09-27 16:55:03 | [diff] [blame] | 135 | ObserverListThreadSafe(const ObserverListThreadSafe&) = delete; |
| 136 | ObserverListThreadSafe& operator=(const ObserverListThreadSafe&) = delete; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 137 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 138 | // Adds |observer| to the list. |observer| must not already be in the list. |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 139 | AddObserverResult AddObserver(ObserverType* observer) { |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 140 | DCHECK(SequencedTaskRunner::HasCurrentDefault()) |
Sean Maher | 70f294293 | 2023-01-04 22:15:06 | [diff] [blame] | 141 | << "An observer can only be registered when " |
| 142 | "SequencedTaskRunner::HasCurrentDefault. If this is in a unit test, " |
| 143 | "you're likely merely missing a " |
Francois Doray | c91b0c3 | 2020-06-30 15:53:55 | [diff] [blame] | 144 | "base::test::(SingleThread)TaskEnvironment in your fixture. " |
| 145 | "Otherwise, try running this code on a named thread (main/UI/IO) or " |
| 146 | "from a task posted to a base::SequencedTaskRunner or " |
| 147 | "base::SingleThreadTaskRunner."; |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 148 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 149 | AutoLock auto_lock(lock_); |
| 150 | |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 151 | bool was_empty = observers_.empty(); |
| 152 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 153 | // Add |observer| to the list of observers. |
Jan Wilken Dörrie | f61e74c | 2019-06-07 08:20:02 | [diff] [blame] | 154 | DCHECK(!Contains(observers_, observer)); |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 155 | const scoped_refptr<SequencedTaskRunner> task_runner = |
Sean Maher | 7d0e805 | 2022-12-09 01:46:32 | [diff] [blame] | 156 | SequencedTaskRunner::GetCurrentDefault(); |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 157 | // Each observer gets a unique identifier. These unique identifiers are used |
| 158 | // to avoid execution of pending posted-tasks over removed or released |
| 159 | // observers. |
| 160 | const size_t observer_id = ++observer_id_counter_; |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 161 | #if DCHECK_IS_ON() |
| 162 | ObserverTaskRunnerInfo task_info = {task_runner, base::debug::StackTrace(), |
| 163 | observer_id}; |
| 164 | #else |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 165 | ObserverTaskRunnerInfo task_info = {task_runner, observer_id}; |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 166 | #endif |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 167 | observers_[observer] = std::move(task_info); |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 168 | |
| 169 | // If this is called while a notification is being dispatched on this thread |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 170 | // and |policy_| is ALL, |observer| must be notified (if a notification is |
| 171 | // being dispatched on another thread in parallel, the notification may or |
| 172 | // may not make it to |observer| depending on the outcome of the race to |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 173 | // |lock_|). |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 174 | if (policy_ == ObserverListPolicy::ALL) { |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 175 | if (const NotificationDataBase* const current_notification = |
| 176 | GetCurrentNotification(); |
| 177 | current_notification && current_notification->observer_list == this) { |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 178 | const NotificationData* notification_data = |
| 179 | static_cast<const NotificationData*>(current_notification); |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 180 | task_runner->PostTask( |
| 181 | current_notification->from_here, |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 182 | BindOnce(&Self::NotifyWrapper, this, |
Ari Chivukula | 621212d0 | 2023-01-11 11:53:10 | [diff] [blame] | 183 | // While `observer` may be dangling, we pass it and |
| 184 | // check it wasn't deallocated in NotifyWrapper() which can |
| 185 | // check `observers_` to verify presence (the owner of the |
| 186 | // observer is responsible for removing it from that list |
| 187 | // before deallocation). |
| 188 | UnsafeDangling(observer), |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 189 | NotificationData(this, observer_id, |
| 190 | current_notification->from_here, |
| 191 | notification_data->method))); |
avi | 816e3cf | 2016-10-27 04:10:51 | [diff] [blame] | 192 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 193 | } |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 194 | |
| 195 | return was_empty ? AddObserverResult::kBecameNonEmpty |
| 196 | : AddObserverResult::kWasAlreadyNonEmpty; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 199 | // Remove an observer from the list if it is in the list. |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 200 | // |
| 201 | // If a notification was sent to the observer but hasn't started to run yet, |
| 202 | // it will be aborted. If a notification has started to run, removing the |
| 203 | // observer won't stop it. |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 204 | RemoveObserverResult RemoveObserver(ObserverType* observer) { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 205 | AutoLock auto_lock(lock_); |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 206 | if constexpr (RemovePolicy == RemoveObserverPolicy::kAddingSequenceOnly) { |
| 207 | const auto it = observers_.find(observer); |
| 208 | CHECK(it == observers_.end() || |
| 209 | it->second.task_runner->RunsTasksInCurrentSequence()); |
| 210 | } |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 211 | observers_.erase(observer); |
Eric Seckler | 3676f3c | 2021-04-27 14:32:38 | [diff] [blame] | 212 | return observers_.empty() ? RemoveObserverResult::kWasOrBecameEmpty |
| 213 | : RemoveObserverResult::kRemainsNonEmpty; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | f6969fe | 2012-02-08 00:22:11 | [diff] [blame] | 216 | // Verifies that the list is currently empty (i.e. there are no observers). |
| 217 | void AssertEmpty() const { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 218 | #if DCHECK_IS_ON() |
| 219 | AutoLock auto_lock(lock_); |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 220 | bool observers_is_empty = observers_.empty(); |
| 221 | DUMP_WILL_BE_CHECK(observers_is_empty) |
| 222 | << "\n" |
| 223 | << GetObserversCreationStackStringLocked(); |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 224 | #endif |
[email protected] | f6969fe | 2012-02-08 00:22:11 | [diff] [blame] | 225 | } |
| 226 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 227 | // Asynchronously invokes a callback on all observers, on their registration |
| 228 | // sequence. You cannot assume that at the completion of the Notify call that |
| 229 | // all Observers have been Notified. The notification may still be pending |
| 230 | // delivery. |
tzik | b7990cc | 2016-08-25 02:19:36 | [diff] [blame] | 231 | template <typename Method, typename... Params> |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 232 | void Notify(const Location& from_here, Method m, Params&&... params) { |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 233 | RepeatingCallback<void(ObserverType*)> method = |
| 234 | BindRepeating(&Dispatcher<ObserverType, Method>::Run, m, |
| 235 | std::forward<Params>(params)...); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 236 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 237 | AutoLock lock(lock_); |
| 238 | for (const auto& observer : observers_) { |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 239 | observer.second.task_runner->PostTask( |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 240 | from_here, |
Joe Mason | 7eda6612 | 2023-08-17 19:10:57 | [diff] [blame] | 241 | BindOnce(&Self::NotifyWrapper, this, |
Ari Chivukula | 621212d0 | 2023-01-11 11:53:10 | [diff] [blame] | 242 | // While `observer.first` may be dangling, we pass it and |
| 243 | // check it wasn't deallocated in NotifyWrapper() which can |
| 244 | // check `observers_` to verify presence (the owner of the |
| 245 | // observer is responsible for removing it from that list |
| 246 | // before deallocation). |
| 247 | UnsafeDangling(observer.first), |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 248 | NotificationData(this, observer.second.observer_id, |
| 249 | from_here, method))); |
thakis | 662b3e4 | 2014-12-23 02:02:57 | [diff] [blame] | 250 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 253 | private: |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 254 | friend class RefCountedThreadSafe<ObserverListThreadSafeBase>; |
[email protected] | bf68712 | 2011-01-11 21:19:54 | [diff] [blame] | 255 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 256 | struct NotificationData : public NotificationDataBase { |
| 257 | NotificationData(ObserverListThreadSafe* observer_list_in, |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 258 | size_t observer_id_in, |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 259 | const Location& from_here_in, |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 260 | const RepeatingCallback<void(ObserverType*)>& method_in) |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 261 | : NotificationDataBase(observer_list_in, from_here_in), |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 262 | method(method_in), |
| 263 | observer_id(observer_id_in) {} |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 264 | |
kylechar | b2695fc | 2019-04-24 14:51:20 | [diff] [blame] | 265 | RepeatingCallback<void(ObserverType*)> method; |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 266 | size_t observer_id; |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 267 | }; |
| 268 | |
Takuto Ikuta | 88317a4c | 2018-04-27 18:19:58 | [diff] [blame] | 269 | ~ObserverListThreadSafe() override = default; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 270 | |
Ari Chivukula | 621212d0 | 2023-01-11 11:53:10 | [diff] [blame] | 271 | void NotifyWrapper(MayBeDangling<ObserverType> observer, |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 272 | const NotificationData& notification) { |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 273 | { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 274 | AutoLock auto_lock(lock_); |
avi | 816e3cf | 2016-10-27 04:10:51 | [diff] [blame] | 275 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 276 | // Check whether the observer still needs a notification. |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 277 | DCHECK_EQ(notification.observer_list, this); |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 278 | auto it = observers_.find(observer); |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 279 | if (it == observers_.end() || |
| 280 | it->second.observer_id != notification.observer_id) { |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 281 | return; |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 282 | } |
| 283 | DCHECK(it->second.task_runner->RunsTasksInCurrentSequence()); |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 284 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 285 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 286 | // Keep track of the notification being dispatched on the current thread. |
| 287 | // This will be used if the callback below calls AddObserver(). |
| 288 | // |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 289 | // Note: GetCurrentNotification() may not return null if this runs in a |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 290 | // nested loop started by a notification callback. In that case, it is |
| 291 | // important to save the previous value to restore it later. |
Peter Kasting | 960e2d3 | 2023-03-14 17:18:41 | [diff] [blame] | 292 | const AutoReset<const NotificationDataBase*> resetter_( |
| 293 | &GetCurrentNotification(), ¬ification); |
fdoray | 5062447 | 2017-01-31 20:26:28 | [diff] [blame] | 294 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 295 | // Invoke the callback. |
| 296 | notification.method.Run(observer); |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 297 | } |
| 298 | |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 299 | std::string GetObserversCreationStackStringLocked() const |
| 300 | EXCLUSIVE_LOCKS_REQUIRED(lock_) { |
| 301 | std::string result; |
| 302 | #if DCHECK_IS_ON() |
| 303 | for (const auto& observer : observers_) { |
| 304 | StrAppend(&result, |
| 305 | {observer.second.add_observer_stack_.ToString(), "\n"}); |
| 306 | } |
| 307 | #endif |
| 308 | return result; |
| 309 | } |
| 310 | |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 311 | const ObserverListPolicy policy_ = ObserverListPolicy::ALL; |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 312 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 313 | mutable Lock lock_; |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 314 | |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 315 | size_t observer_id_counter_ GUARDED_BY(lock_) = 0; |
| 316 | |
| 317 | struct ObserverTaskRunnerInfo { |
| 318 | scoped_refptr<SequencedTaskRunner> task_runner; |
Daniel White | 32856a679 | 2023-05-26 14:31:15 | [diff] [blame] | 319 | #if DCHECK_IS_ON() |
| 320 | base::debug::StackTrace add_observer_stack_; |
| 321 | #endif |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 322 | size_t observer_id = 0; |
| 323 | }; |
| 324 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 325 | // Keys are observers. Values are the SequencedTaskRunners on which they must |
| 326 | // be notified. |
Etienne Bergeron | 3f80c2cf | 2021-03-22 17:32:14 | [diff] [blame] | 327 | std::unordered_map<ObserverType*, ObserverTaskRunnerInfo> observers_ |
| 328 | GUARDED_BY(lock_); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 329 | }; |
| 330 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 331 | } // namespace base |
| 332 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 333 | #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |