| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 ObserverListThreadSafe<ObserverType>, | 76 ObserverListThreadSafe<ObserverType>, |
| 77 ObserverListThreadSafeTraits<ObserverType> > { | 77 ObserverListThreadSafeTraits<ObserverType> > { |
| 78 public: | 78 public: |
| 79 typedef typename ObserverList<ObserverType>::NotificationType | 79 typedef typename ObserverList<ObserverType>::NotificationType |
| 80 NotificationType; | 80 NotificationType; |
| 81 | 81 |
| 82 ObserverListThreadSafe() | 82 ObserverListThreadSafe() |
| 83 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} | 83 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} |
| 84 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} | 84 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
| 85 | 85 |
| 86 // Add an observer to the list. | 86 // Add an observer to the list. An observer should not be added to |
| 87 // the same list more than once. |
| 87 void AddObserver(ObserverType* obs) { | 88 void AddObserver(ObserverType* obs) { |
| 88 ObserverList<ObserverType>* list = NULL; | 89 ObserverList<ObserverType>* list = NULL; |
| 89 MessageLoop* loop = MessageLoop::current(); | 90 MessageLoop* loop = MessageLoop::current(); |
| 90 // TODO(mbelshe): Get rid of this check. Its needed right now because | 91 // TODO(mbelshe): Get rid of this check. Its needed right now because |
| 91 // Time currently triggers usage of the ObserverList. | 92 // Time currently triggers usage of the ObserverList. |
| 92 // And unittests use time without a MessageLoop. | 93 // And unittests use time without a MessageLoop. |
| 93 if (!loop) | 94 if (!loop) |
| 94 return; // Some unittests may access this without a message loop. | 95 return; // Some unittests may access this without a message loop. |
| 95 { | 96 { |
| 96 base::AutoLock lock(list_lock_); | 97 base::AutoLock lock(list_lock_); |
| 97 if (observer_lists_.find(loop) == observer_lists_.end()) | 98 if (observer_lists_.find(loop) == observer_lists_.end()) |
| 98 observer_lists_[loop] = new ObserverList<ObserverType>(type_); | 99 observer_lists_[loop] = new ObserverList<ObserverType>(type_); |
| 99 list = observer_lists_[loop]; | 100 list = observer_lists_[loop]; |
| 100 } | 101 } |
| 101 list->AddObserver(obs); | 102 list->AddObserver(obs); |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Remove an observer from the list. | 105 // Remove an observer from the list if it is in the list. |
| 105 // If there are pending notifications in-transit to the observer, they will | 106 // If there are pending notifications in-transit to the observer, they will |
| 106 // be aborted. | 107 // be aborted. |
| 107 // RemoveObserver MUST be called from the same thread which called | 108 // If the observer to be removed is in the list, RemoveObserver MUST |
| 108 // AddObserver. | 109 // be called from the same thread which called AddObserver. |
| 109 void RemoveObserver(ObserverType* obs) { | 110 void RemoveObserver(ObserverType* obs) { |
| 110 ObserverList<ObserverType>* list = NULL; | 111 ObserverList<ObserverType>* list = NULL; |
| 111 MessageLoop* loop = MessageLoop::current(); | 112 MessageLoop* loop = MessageLoop::current(); |
| 112 if (!loop) | 113 if (!loop) |
| 113 return; // On shutdown, it is possible that current() is already null. | 114 return; // On shutdown, it is possible that current() is already null. |
| 114 { | 115 { |
| 115 base::AutoLock lock(list_lock_); | 116 base::AutoLock lock(list_lock_); |
| 116 list = observer_lists_[loop]; | 117 typename ObserversListMap::iterator it = observer_lists_.find(loop); |
| 117 if (!list) { | 118 if (it == observer_lists_.end()) { |
| 118 NOTREACHED() << "RemoveObserver called on for unknown thread"; | 119 // This may happen if we try to remove an observer on a thread |
| 120 // we never added an observer for. |
| 119 return; | 121 return; |
| 120 } | 122 } |
| 123 list = it->second; |
| 121 | 124 |
| 122 // If we're about to remove the last observer from the list, | 125 // If we're about to remove the last observer from the list, |
| 123 // then we can remove this observer_list entirely. | 126 // then we can remove this observer_list entirely. |
| 124 if (list->size() == 1) | 127 if (list->HasObserver(obs) && list->size() == 1) |
| 125 observer_lists_.erase(loop); | 128 observer_lists_.erase(it); |
| 126 } | 129 } |
| 127 list->RemoveObserver(obs); | 130 list->RemoveObserver(obs); |
| 128 | 131 |
| 129 // If RemoveObserver is called from a notification, the size will be | 132 // If RemoveObserver is called from a notification, the size will be |
| 130 // nonzero. Instead of deleting here, the NotifyWrapper will delete | 133 // nonzero. Instead of deleting here, the NotifyWrapper will delete |
| 131 // when it finishes iterating. | 134 // when it finishes iterating. |
| 132 if (list->size() == 0) | 135 if (list->size() == 0) |
| 133 delete list; | 136 delete list; |
| 134 } | 137 } |
| 135 | 138 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 229 |
| 227 // These are marked mutable to facilitate having NotifyAll be const. | 230 // These are marked mutable to facilitate having NotifyAll be const. |
| 228 base::Lock list_lock_; // Protects the observer_lists_. | 231 base::Lock list_lock_; // Protects the observer_lists_. |
| 229 ObserversListMap observer_lists_; | 232 ObserversListMap observer_lists_; |
| 230 const NotificationType type_; | 233 const NotificationType type_; |
| 231 | 234 |
| 232 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 235 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
| 233 }; | 236 }; |
| 234 | 237 |
| 235 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 238 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |