OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_ | 5 #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_ |
6 #define BASE_OBSERVER_LIST_THREADSAFE_H_ | 6 #define BASE_OBSERVER_LIST_THREADSAFE_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 } | 149 } |
150 list->RemoveObserver(obs); | 150 list->RemoveObserver(obs); |
151 | 151 |
152 // If RemoveObserver is called from a notification, the size will be | 152 // If RemoveObserver is called from a notification, the size will be |
153 // nonzero. Instead of deleting here, the NotifyWrapper will delete | 153 // nonzero. Instead of deleting here, the NotifyWrapper will delete |
154 // when it finishes iterating. | 154 // when it finishes iterating. |
155 if (list->size() == 0) | 155 if (list->size() == 0) |
156 delete context; | 156 delete context; |
157 } | 157 } |
158 | 158 |
159 // Returns true if the observer is in the list for the current thread. | |
160 // Otherwise returns false. | |
161 bool HasObserver(ObserverType* obs) { | |
162 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); | |
163 base::AutoLock lock(list_lock_); | |
164 typename ObserversListMap::iterator it = observer_lists_.find(thread_id); | |
165 if (it == observer_lists_.end()) | |
166 return false; | |
167 | |
168 ObserverListContext* context = it->second; | |
169 ObserverList<ObserverType>* list = &context->list; | |
flackr
2015/02/25 23:15:16
nit: should be able to return context->list.HasObs
jonross
2015/03/02 15:18:10
Done.
| |
170 | |
171 return list->HasObserver(obs); | |
172 } | |
173 | |
159 // Verifies that the list is currently empty (i.e. there are no observers). | 174 // Verifies that the list is currently empty (i.e. there are no observers). |
160 void AssertEmpty() const { | 175 void AssertEmpty() const { |
161 base::AutoLock lock(list_lock_); | 176 base::AutoLock lock(list_lock_); |
162 DCHECK(observer_lists_.empty()); | 177 DCHECK(observer_lists_.empty()); |
163 } | 178 } |
164 | 179 |
165 // Notify methods. | 180 // Notify methods. |
166 // Make a thread-safe callback to each Observer in the list. | 181 // Make a thread-safe callback to each Observer in the list. |
167 // Note, these calls are effectively asynchronous. You cannot assume | 182 // Note, these calls are effectively asynchronous. You cannot assume |
168 // that at the completion of the Notify call that all Observers have | 183 // that at the completion of the Notify call that all Observers have |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 ObserversListMap; | 272 ObserversListMap; |
258 | 273 |
259 mutable base::Lock list_lock_; // Protects the observer_lists_. | 274 mutable base::Lock list_lock_; // Protects the observer_lists_. |
260 ObserversListMap observer_lists_; | 275 ObserversListMap observer_lists_; |
261 const NotificationType type_; | 276 const NotificationType type_; |
262 | 277 |
263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 278 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
264 }; | 279 }; |
265 | 280 |
266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 281 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |