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 return context->list.HasObserver(obs); |
| 170 } |
| 171 |
159 // Verifies that the list is currently empty (i.e. there are no observers). | 172 // Verifies that the list is currently empty (i.e. there are no observers). |
160 void AssertEmpty() const { | 173 void AssertEmpty() const { |
161 base::AutoLock lock(list_lock_); | 174 base::AutoLock lock(list_lock_); |
162 DCHECK(observer_lists_.empty()); | 175 DCHECK(observer_lists_.empty()); |
163 } | 176 } |
164 | 177 |
165 // Notify methods. | 178 // Notify methods. |
166 // Make a thread-safe callback to each Observer in the list. | 179 // Make a thread-safe callback to each Observer in the list. |
167 // Note, these calls are effectively asynchronous. You cannot assume | 180 // Note, these calls are effectively asynchronous. You cannot assume |
168 // that at the completion of the Notify call that all Observers have | 181 // 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; | 270 ObserversListMap; |
258 | 271 |
259 mutable base::Lock list_lock_; // Protects the observer_lists_. | 272 mutable base::Lock list_lock_; // Protects the observer_lists_. |
260 ObserversListMap observer_lists_; | 273 ObserversListMap observer_lists_; |
261 const NotificationType type_; | 274 const NotificationType type_; |
262 | 275 |
263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 276 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
264 }; | 277 }; |
265 | 278 |
266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 279 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |