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 public: | 104 public: |
105 typedef typename ObserverList<ObserverType>::NotificationType | 105 typedef typename ObserverList<ObserverType>::NotificationType |
106 NotificationType; | 106 NotificationType; |
107 | 107 |
108 ObserverListThreadSafe() | 108 ObserverListThreadSafe() |
109 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} | 109 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} |
110 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} | 110 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
111 | 111 |
112 // Add an observer to the list. An observer should not be added to | 112 // Add an observer to the list. An observer should not be added to |
113 // the same list more than once. | 113 // the same list more than once. |
114 void AddObserver(ObserverType* obs) { | 114 bool AddObserver(ObserverType* obs) { |
115 // If there is not a current MessageLoop, it is impossible to notify on it, | 115 // If there is not a current MessageLoop, it is impossible to notify on it, |
116 // so do not add the observer. | 116 // so do not add the observer. |
117 if (!MessageLoop::current()) | 117 if (!MessageLoop::current()) |
118 return; | 118 return false; |
119 | 119 |
120 ObserverList<ObserverType>* list = nullptr; | 120 ObserverList<ObserverType>* list = nullptr; |
121 PlatformThreadId thread_id = PlatformThread::CurrentId(); | 121 PlatformThreadId thread_id = PlatformThread::CurrentId(); |
122 { | 122 { |
123 AutoLock lock(list_lock_); | 123 AutoLock lock(list_lock_); |
124 if (observer_lists_.find(thread_id) == observer_lists_.end()) | 124 if (observer_lists_.find(thread_id) == observer_lists_.end()) |
125 observer_lists_[thread_id] = new ObserverListContext(type_); | 125 observer_lists_[thread_id] = new ObserverListContext(type_); |
126 list = &(observer_lists_[thread_id]->list); | 126 list = &(observer_lists_[thread_id]->list); |
127 } | 127 } |
128 list->AddObserver(obs); | 128 list->AddObserver(obs); |
| 129 return true; |
129 } | 130 } |
130 | 131 |
131 // Remove an observer from the list if it is in the list. | 132 // Remove an observer from the list if it is in the list. |
132 // If there are pending notifications in-transit to the observer, they will | 133 // If there are pending notifications in-transit to the observer, they will |
133 // be aborted. | 134 // be aborted. |
134 // If the observer to be removed is in the list, RemoveObserver MUST | 135 // If the observer to be removed is in the list, RemoveObserver MUST |
135 // be called from the same thread which called AddObserver. | 136 // be called from the same thread which called AddObserver. |
136 void RemoveObserver(ObserverType* obs) { | 137 void RemoveObserver(ObserverType* obs) { |
137 ObserverListContext* context = nullptr; | 138 ObserverListContext* context = nullptr; |
138 ObserverList<ObserverType>* list = nullptr; | 139 ObserverList<ObserverType>* list = nullptr; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 mutable Lock list_lock_; // Protects the observer_lists_. | 264 mutable Lock list_lock_; // Protects the observer_lists_. |
264 ObserversListMap observer_lists_; | 265 ObserversListMap observer_lists_; |
265 const NotificationType type_; | 266 const NotificationType type_; |
266 | 267 |
267 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 268 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
268 }; | 269 }; |
269 | 270 |
270 } // namespace base | 271 } // namespace base |
271 | 272 |
272 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 273 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |