| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 class ObserverListThreadSafe | 53 class ObserverListThreadSafe |
| 54 : public base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> > { | 54 : public base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> > { |
| 55 public: | 55 public: |
| 56 typedef typename ObserverList<ObserverType>::NotificationType | 56 typedef typename ObserverList<ObserverType>::NotificationType |
| 57 NotificationType; | 57 NotificationType; |
| 58 | 58 |
| 59 ObserverListThreadSafe() | 59 ObserverListThreadSafe() |
| 60 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} | 60 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} |
| 61 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} | 61 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
| 62 | 62 |
| 63 ~ObserverListThreadSafe() { | |
| 64 typename ObserversListMap::const_iterator it; | |
| 65 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) | |
| 66 delete (*it).second; | |
| 67 observer_lists_.clear(); | |
| 68 } | |
| 69 | |
| 70 // Add an observer to the list. | 63 // Add an observer to the list. |
| 71 void AddObserver(ObserverType* obs) { | 64 void AddObserver(ObserverType* obs) { |
| 72 ObserverList<ObserverType>* list = NULL; | 65 ObserverList<ObserverType>* list = NULL; |
| 73 MessageLoop* loop = MessageLoop::current(); | 66 MessageLoop* loop = MessageLoop::current(); |
| 74 // TODO(mbelshe): Get rid of this check. Its needed right now because | 67 // TODO(mbelshe): Get rid of this check. Its needed right now because |
| 75 // Time currently triggers usage of the ObserverList. | 68 // Time currently triggers usage of the ObserverList. |
| 76 // And unittests use time without a MessageLoop. | 69 // And unittests use time without a MessageLoop. |
| 77 if (!loop) | 70 if (!loop) |
| 78 return; // Some unittests may access this without a message loop. | 71 return; // Some unittests may access this without a message loop. |
| 79 { | 72 { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 123 |
| 131 template <class Method, class A> | 124 template <class Method, class A> |
| 132 void Notify(Method m, const A &a) { | 125 void Notify(Method m, const A &a) { |
| 133 UnboundMethod<ObserverType, Method, Tuple1<A> > method(m, MakeTuple(a)); | 126 UnboundMethod<ObserverType, Method, Tuple1<A> > method(m, MakeTuple(a)); |
| 134 Notify<Method, Tuple1<A> >(method); | 127 Notify<Method, Tuple1<A> >(method); |
| 135 } | 128 } |
| 136 | 129 |
| 137 // TODO(mbelshe): Add more wrappers for Notify() with more arguments. | 130 // TODO(mbelshe): Add more wrappers for Notify() with more arguments. |
| 138 | 131 |
| 139 private: | 132 private: |
| 133 friend class |
| 134 base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> >; |
| 135 ~ObserverListThreadSafe() { |
| 136 typename ObserversListMap::const_iterator it; |
| 137 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) |
| 138 delete (*it).second; |
| 139 observer_lists_.clear(); |
| 140 } |
| 141 |
| 140 template <class Method, class Params> | 142 template <class Method, class Params> |
| 141 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { | 143 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { |
| 142 AutoLock lock(list_lock_); | 144 AutoLock lock(list_lock_); |
| 143 typename ObserversListMap::iterator it; | 145 typename ObserversListMap::iterator it; |
| 144 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { | 146 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { |
| 145 MessageLoop* loop = (*it).first; | 147 MessageLoop* loop = (*it).first; |
| 146 ObserverList<ObserverType>* list = (*it).second; | 148 ObserverList<ObserverType>* list = (*it).second; |
| 147 loop->PostTask( | 149 loop->PostTask( |
| 148 FROM_HERE, | 150 FROM_HERE, |
| 149 NewRunnableMethod(this, | 151 NewRunnableMethod(this, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 202 |
| 201 // These are marked mutable to facilitate having NotifyAll be const. | 203 // These are marked mutable to facilitate having NotifyAll be const. |
| 202 Lock list_lock_; // Protects the observer_lists_. | 204 Lock list_lock_; // Protects the observer_lists_. |
| 203 ObserversListMap observer_lists_; | 205 ObserversListMap observer_lists_; |
| 204 const NotificationType type_; | 206 const NotificationType type_; |
| 205 | 207 |
| 206 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 208 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
| 207 }; | 209 }; |
| 208 | 210 |
| 209 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 211 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |