| 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 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 18 #include "base/single_thread_task_runner.h" | |
| 19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 20 #include "base/thread_task_runner_handle.h" | |
| 21 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 22 | 21 |
| 23 /////////////////////////////////////////////////////////////////////////////// | 22 /////////////////////////////////////////////////////////////////////////////// |
| 24 // | 23 // |
| 25 // OVERVIEW: | 24 // OVERVIEW: |
| 26 // | 25 // |
| 27 // A thread-safe container for a list of observers. | 26 // A thread-safe container for a list of observers. |
| 28 // This is similar to the observer_list (see observer_list.h), but it | 27 // This is similar to the observer_list (see observer_list.h), but it |
| 29 // is more robust for multi-threaded situations. | 28 // is more robust for multi-threaded situations. |
| 30 // | 29 // |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 template <class Method, class... Params> | 170 template <class Method, class... Params> |
| 172 void Notify(const tracked_objects::Location& from_here, | 171 void Notify(const tracked_objects::Location& from_here, |
| 173 Method m, | 172 Method m, |
| 174 const Params&... params) { | 173 const Params&... params) { |
| 175 UnboundMethod<ObserverType, Method, Tuple<Params...>> method( | 174 UnboundMethod<ObserverType, Method, Tuple<Params...>> method( |
| 176 m, MakeTuple(params...)); | 175 m, MakeTuple(params...)); |
| 177 | 176 |
| 178 base::AutoLock lock(list_lock_); | 177 base::AutoLock lock(list_lock_); |
| 179 for (const auto& entry : observer_lists_) { | 178 for (const auto& entry : observer_lists_) { |
| 180 ObserverListContext* context = entry.second; | 179 ObserverListContext* context = entry.second; |
| 181 context->task_runner->PostTask( | 180 context->loop->PostTask( |
| 182 from_here, | 181 from_here, |
| 183 base::Bind( | 182 base::Bind( |
| 184 &ObserverListThreadSafe<ObserverType>::template NotifyWrapper< | 183 &ObserverListThreadSafe<ObserverType>::template NotifyWrapper< |
| 185 Method, Tuple<Params...>>, | 184 Method, Tuple<Params...>>, |
| 186 this, context, method)); | 185 this, context, method)); |
| 187 } | 186 } |
| 188 } | 187 } |
| 189 | 188 |
| 190 private: | 189 private: |
| 191 // See comment above ObserverListThreadSafeTraits' definition. | 190 // See comment above ObserverListThreadSafeTraits' definition. |
| 192 friend struct ObserverListThreadSafeTraits<ObserverType>; | 191 friend struct ObserverListThreadSafeTraits<ObserverType>; |
| 193 | 192 |
| 194 struct ObserverListContext { | 193 struct ObserverListContext { |
| 195 explicit ObserverListContext(NotificationType type) | 194 explicit ObserverListContext(NotificationType type) |
| 196 : task_runner(base::ThreadTaskRunnerHandle::Get()), list(type) {} | 195 : loop(base::MessageLoopProxy::current()), |
| 196 list(type) { |
| 197 } |
| 197 | 198 |
| 198 scoped_refptr<base::SingleThreadTaskRunner> task_runner; | 199 scoped_refptr<base::MessageLoopProxy> loop; |
| 199 ObserverList<ObserverType> list; | 200 ObserverList<ObserverType> list; |
| 200 | 201 |
| 201 private: | 202 private: |
| 202 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); | 203 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); |
| 203 }; | 204 }; |
| 204 | 205 |
| 205 ~ObserverListThreadSafe() { | 206 ~ObserverListThreadSafe() { |
| 206 STLDeleteValues(&observer_lists_); | 207 STLDeleteValues(&observer_lists_); |
| 207 } | 208 } |
| 208 | 209 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 ObserversListMap; | 257 ObserversListMap; |
| 257 | 258 |
| 258 mutable base::Lock list_lock_; // Protects the observer_lists_. | 259 mutable base::Lock list_lock_; // Protects the observer_lists_. |
| 259 ObserversListMap observer_lists_; | 260 ObserversListMap observer_lists_; |
| 260 const NotificationType type_; | 261 const NotificationType type_; |
| 261 | 262 |
| 262 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
| 263 }; | 264 }; |
| 264 | 265 |
| 265 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |