OLD | NEW |
1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback_old.h" | |
15 #include "base/location.h" | 14 #include "base/location.h" |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
18 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
19 #include "base/message_loop_proxy.h" | 18 #include "base/message_loop_proxy.h" |
20 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
21 #include "base/task.h" | 20 #include "base/task.h" |
22 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
23 | 22 |
24 /////////////////////////////////////////////////////////////////////////////// | 23 /////////////////////////////////////////////////////////////////////////////// |
(...skipping 26 matching lines...) Expand all Loading... |
51 // which uses the ThreadSafeObserver. When Notifying the observers, | 50 // which uses the ThreadSafeObserver. When Notifying the observers, |
52 // we simply call PostTask to each registered thread, and then each thread | 51 // we simply call PostTask to each registered thread, and then each thread |
53 // will notify its regular ObserverList. | 52 // will notify its regular ObserverList. |
54 // | 53 // |
55 /////////////////////////////////////////////////////////////////////////////// | 54 /////////////////////////////////////////////////////////////////////////////// |
56 | 55 |
57 // Forward declaration for ObserverListThreadSafeTraits. | 56 // Forward declaration for ObserverListThreadSafeTraits. |
58 template <class ObserverType> | 57 template <class ObserverType> |
59 class ObserverListThreadSafe; | 58 class ObserverListThreadSafe; |
60 | 59 |
| 60 // An UnboundMethod is a wrapper for a method where the actual object is |
| 61 // provided at Run dispatch time. |
| 62 template <class T, class Method, class Params> |
| 63 class UnboundMethod { |
| 64 public: |
| 65 UnboundMethod(Method m, const Params& p) : m_(m), p_(p) { |
| 66 COMPILE_ASSERT( |
| 67 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), |
| 68 badunboundmethodparams); |
| 69 } |
| 70 void Run(T* obj) const { |
| 71 DispatchToMethod(obj, m_, p_); |
| 72 } |
| 73 private: |
| 74 Method m_; |
| 75 Params p_; |
| 76 }; |
| 77 |
61 // This class is used to work around VS2005 not accepting: | 78 // This class is used to work around VS2005 not accepting: |
62 // | 79 // |
63 // friend class | 80 // friend class |
64 // base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> >; | 81 // base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> >; |
65 // | 82 // |
66 // Instead of friending the class, we could friend the actual function | 83 // Instead of friending the class, we could friend the actual function |
67 // which calls delete. However, this ends up being | 84 // which calls delete. However, this ends up being |
68 // RefCountedThreadSafe::DeleteInternal(), which is private. So we | 85 // RefCountedThreadSafe::DeleteInternal(), which is private. So we |
69 // define our own templated traits class so we can friend it. | 86 // define our own templated traits class so we can friend it. |
70 template <class T> | 87 template <class T> |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 ObserversListMap; | 284 ObserversListMap; |
268 | 285 |
269 base::Lock list_lock_; // Protects the observer_lists_. | 286 base::Lock list_lock_; // Protects the observer_lists_. |
270 ObserversListMap observer_lists_; | 287 ObserversListMap observer_lists_; |
271 const NotificationType type_; | 288 const NotificationType type_; |
272 | 289 |
273 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 290 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
274 }; | 291 }; |
275 | 292 |
276 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 293 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |