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 if (!loop) |
| 68 return; // Some unittests may access this without a message loop. |
67 { | 69 { |
68 AutoLock lock(list_lock_); | 70 AutoLock lock(list_lock_); |
69 if (observer_lists_.find(loop) == observer_lists_.end()) | 71 if (observer_lists_.find(loop) == observer_lists_.end()) |
70 observer_lists_[loop] = new ObserverList<ObserverType>(); | 72 observer_lists_[loop] = new ObserverList<ObserverType>(); |
71 list = observer_lists_[loop]; | 73 list = observer_lists_[loop]; |
72 } | 74 } |
73 list->AddObserver(obs); | 75 list->AddObserver(obs); |
74 } | 76 } |
75 | 77 |
76 // Remove an observer from the list. | 78 // Remove an observer from the list. |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; | 187 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; |
186 | 188 |
187 // These are marked mutable to facilitate having NotifyAll be const. | 189 // These are marked mutable to facilitate having NotifyAll be const. |
188 Lock list_lock_; // Protects the observer_lists_. | 190 Lock list_lock_; // Protects the observer_lists_. |
189 ObserversListMap observer_lists_; | 191 ObserversListMap observer_lists_; |
190 | 192 |
191 DISALLOW_EVIL_CONSTRUCTORS(ObserverListThreadSafe); | 193 DISALLOW_EVIL_CONSTRUCTORS(ObserverListThreadSafe); |
192 }; | 194 }; |
193 | 195 |
194 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 196 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |