| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <vector> | 8 #include <vector> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 typename ObserversListMap::const_iterator it; | 57 typename ObserversListMap::const_iterator it; |
| 58 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) | 58 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) |
| 59 delete (*it).second; | 59 delete (*it).second; |
| 60 observer_lists_.clear(); | 60 observer_lists_.clear(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Add an observer to the list. | 63 // Add an observer to the list. |
| 64 void AddObserver(ObserverType* obs) { | 64 void AddObserver(ObserverType* obs) { |
| 65 ObserverList<ObserverType>* list = NULL; | 65 ObserverList<ObserverType>* list = NULL; |
| 66 MessageLoop* loop = MessageLoop::current(); | 66 MessageLoop* loop = MessageLoop::current(); |
| 67 // TODO(mbelshe): Get rid of this check. Its needed right now because |
| 68 // Time currently triggers usage of the ObserverList. |
| 69 // And unittests use time without a MessageLoop. |
| 67 if (!loop) | 70 if (!loop) |
| 68 return; // Some unittests may access this without a message loop. | 71 return; // Some unittests may access this without a message loop. |
| 69 { | 72 { |
| 70 AutoLock lock(list_lock_); | 73 AutoLock lock(list_lock_); |
| 71 if (observer_lists_.find(loop) == observer_lists_.end()) | 74 if (observer_lists_.find(loop) == observer_lists_.end()) |
| 72 observer_lists_[loop] = new ObserverList<ObserverType>(); | 75 observer_lists_[loop] = new ObserverList<ObserverType>(); |
| 73 list = observer_lists_[loop]; | 76 list = observer_lists_[loop]; |
| 74 } | 77 } |
| 75 list->AddObserver(obs); | 78 list->AddObserver(obs); |
| 76 } | 79 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; | 190 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; |
| 188 | 191 |
| 189 // These are marked mutable to facilitate having NotifyAll be const. | 192 // These are marked mutable to facilitate having NotifyAll be const. |
| 190 Lock list_lock_; // Protects the observer_lists_. | 193 Lock list_lock_; // Protects the observer_lists_. |
| 191 ObserversListMap observer_lists_; | 194 ObserversListMap observer_lists_; |
| 192 | 195 |
| 193 DISALLOW_EVIL_CONSTRUCTORS(ObserverListThreadSafe); | 196 DISALLOW_EVIL_CONSTRUCTORS(ObserverListThreadSafe); |
| 194 }; | 197 }; |
| 195 | 198 |
| 196 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 199 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |