| 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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 192 |
| 193 struct ObserverListContext { | 193 struct ObserverListContext { |
| 194 explicit ObserverListContext(NotificationType type) | 194 explicit ObserverListContext(NotificationType type) |
| 195 : loop(base::MessageLoopProxy::current()), | 195 : loop(base::MessageLoopProxy::current()), |
| 196 list(type) { | 196 list(type) { |
| 197 } | 197 } |
| 198 | 198 |
| 199 scoped_refptr<base::MessageLoopProxy> loop; | 199 scoped_refptr<base::MessageLoopProxy> loop; |
| 200 ObserverList<ObserverType> list; | 200 ObserverList<ObserverType> list; |
| 201 | 201 |
| 202 private: |
| 202 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); | 203 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); |
| 203 }; | 204 }; |
| 204 | 205 |
| 205 ~ObserverListThreadSafe() { | 206 ~ObserverListThreadSafe() { |
| 206 STLDeleteValues(&observer_lists_); | 207 STLDeleteValues(&observer_lists_); |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Wrapper which is called to fire the notifications for each thread's | 210 // Wrapper which is called to fire the notifications for each thread's |
| 210 // ObserverList. This function MUST be called on the thread which owns | 211 // ObserverList. This function MUST be called on the thread which owns |
| 211 // the unsafe ObserverList. | 212 // the unsafe ObserverList. |
| 212 template <class Method, class Params> | 213 template <class Method, class Params> |
| 213 void NotifyWrapper(ObserverListContext* context, | 214 void NotifyWrapper(ObserverListContext* context, |
| 214 const UnboundMethod<ObserverType, Method, Params>& method) { | 215 const UnboundMethod<ObserverType, Method, Params>& method) { |
| 215 | |
| 216 // Check that this list still needs notifications. | 216 // Check that this list still needs notifications. |
| 217 { | 217 { |
| 218 base::AutoLock lock(list_lock_); | 218 base::AutoLock lock(list_lock_); |
| 219 typename ObserversListMap::iterator it = | 219 typename ObserversListMap::iterator it = |
| 220 observer_lists_.find(base::PlatformThread::CurrentId()); | 220 observer_lists_.find(base::PlatformThread::CurrentId()); |
| 221 | 221 |
| 222 // The ObserverList could have been removed already. In fact, it could | 222 // The ObserverList could have been removed already. In fact, it could |
| 223 // have been removed and then re-added! If the master list's loop | 223 // have been removed and then re-added! If the master list's loop |
| 224 // does not match this one, then we do not need to finish this | 224 // does not match this one, then we do not need to finish this |
| 225 // notification. | 225 // notification. |
| 226 if (it == observer_lists_.end() || it->second != context) | 226 if (it == observer_lists_.end() || it->second != context) |
| 227 return; | 227 return; |
| 228 } | 228 } |
| 229 | 229 |
| 230 { | 230 { |
| 231 typename ObserverList<ObserverType>::Iterator it(context->list); | 231 typename ObserverList<ObserverType>::Iterator it(&context->list); |
| 232 ObserverType* obs; | 232 ObserverType* obs; |
| 233 while ((obs = it.GetNext()) != NULL) | 233 while ((obs = it.GetNext()) != NULL) |
| 234 method.Run(obs); | 234 method.Run(obs); |
| 235 } | 235 } |
| 236 | 236 |
| 237 // If there are no more observers on the list, we can now delete it. | 237 // If there are no more observers on the list, we can now delete it. |
| 238 if (context->list.size() == 0) { | 238 if (context->list.size() == 0) { |
| 239 { | 239 { |
| 240 base::AutoLock lock(list_lock_); | 240 base::AutoLock lock(list_lock_); |
| 241 // Remove |list| if it's not already removed. | 241 // Remove |list| if it's not already removed. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 257 ObserversListMap; | 257 ObserversListMap; |
| 258 | 258 |
| 259 mutable base::Lock list_lock_; // Protects the observer_lists_. | 259 mutable base::Lock list_lock_; // Protects the observer_lists_. |
| 260 ObserversListMap observer_lists_; | 260 ObserversListMap observer_lists_; |
| 261 const NotificationType type_; | 261 const NotificationType type_; |
| 262 | 262 |
| 263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
| 264 }; | 264 }; |
| 265 | 265 |
| 266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |