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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} | 104 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
105 | 105 |
106 // Add an observer to the list. An observer should not be added to | 106 // Add an observer to the list. An observer should not be added to |
107 // the same list more than once. | 107 // the same list more than once. |
108 void AddObserver(ObserverType* obs) { | 108 void AddObserver(ObserverType* obs) { |
109 // If there is not a current MessageLoop, it is impossible to notify on it, | 109 // If there is not a current MessageLoop, it is impossible to notify on it, |
110 // so do not add the observer. | 110 // so do not add the observer. |
111 if (!base::MessageLoop::current()) | 111 if (!base::MessageLoop::current()) |
112 return; | 112 return; |
113 | 113 |
114 ObserverList<ObserverType>* list = NULL; | 114 ObserverList<ObserverType>* list = nullptr; |
115 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); | 115 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
116 { | 116 { |
117 base::AutoLock lock(list_lock_); | 117 base::AutoLock lock(list_lock_); |
118 if (observer_lists_.find(thread_id) == observer_lists_.end()) | 118 if (observer_lists_.find(thread_id) == observer_lists_.end()) |
119 observer_lists_[thread_id] = new ObserverListContext(type_); | 119 observer_lists_[thread_id] = new ObserverListContext(type_); |
120 list = &(observer_lists_[thread_id]->list); | 120 list = &(observer_lists_[thread_id]->list); |
121 } | 121 } |
122 list->AddObserver(obs); | 122 list->AddObserver(obs); |
123 } | 123 } |
124 | 124 |
125 // Remove an observer from the list if it is in the list. | 125 // Remove an observer from the list if it is in the list. |
126 // If there are pending notifications in-transit to the observer, they will | 126 // If there are pending notifications in-transit to the observer, they will |
127 // be aborted. | 127 // be aborted. |
128 // If the observer to be removed is in the list, RemoveObserver MUST | 128 // If the observer to be removed is in the list, RemoveObserver MUST |
129 // be called from the same thread which called AddObserver. | 129 // be called from the same thread which called AddObserver. |
130 void RemoveObserver(ObserverType* obs) { | 130 void RemoveObserver(ObserverType* obs) { |
131 ObserverListContext* context = NULL; | 131 ObserverListContext* context = nullptr; |
132 ObserverList<ObserverType>* list = NULL; | 132 ObserverList<ObserverType>* list = nullptr; |
133 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); | 133 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
134 { | 134 { |
135 base::AutoLock lock(list_lock_); | 135 base::AutoLock lock(list_lock_); |
136 typename ObserversListMap::iterator it = observer_lists_.find(thread_id); | 136 typename ObserversListMap::iterator it = observer_lists_.find(thread_id); |
137 if (it == observer_lists_.end()) { | 137 if (it == observer_lists_.end()) { |
138 // This will happen if we try to remove an observer on a thread | 138 // This will happen if we try to remove an observer on a thread |
139 // we never added an observer for. | 139 // we never added an observer for. |
140 return; | 140 return; |
141 } | 141 } |
142 context = it->second; | 142 context = it->second; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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()) != nullptr) |
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. |
242 // This can happen if multiple observers got removed in a notification. | 242 // This can happen if multiple observers got removed in a notification. |
243 // See http://crbug.com/55725. | 243 // See http://crbug.com/55725. |
(...skipping 13 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 |