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 STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | 5 #ifndef STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ |
6 #define STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | 6 #define STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 | 14 |
15 namespace storage { | 15 namespace storage { |
16 | 16 |
17 // A wrapper for dispatching method. | 17 // A wrapper for dispatching method. |
18 template <class T, class Method, class Params> | 18 template <class T, class Method, class Params> |
19 void NotifyWrapper(T obj, Method m, const Params& p) { | 19 void NotifyWrapper(T obj, Method m, const Params& p) { |
20 base::DispatchToMethod(base::internal::UnwrapTraits<T>::Unwrap(obj), m, p); | 20 base::DispatchToMethod(obj, m, p); |
21 } | 21 } |
22 | 22 |
23 // An observer list helper to notify on a given task runner. | 23 // An observer list helper to notify on a given task runner. |
24 // Observer pointers (stored as ObserverStoreType) must be kept alive | 24 // Observer pointers (stored as ObserverStoreType) must be kept alive |
25 // until this list dispatches all the notifications. | 25 // until this list dispatches all the notifications. |
26 // | 26 // |
27 // Unlike regular ObserverList or ObserverListThreadSafe internal observer | 27 // Unlike regular ObserverList or ObserverListThreadSafe internal observer |
28 // list is immutable (though not declared const) and cannot be modified after | 28 // list is immutable (though not declared const) and cannot be modified after |
29 // constructed. | 29 // constructed. |
30 // | 30 // |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // Notify on the task runner that is given to AddObserver. | 62 // Notify on the task runner that is given to AddObserver. |
63 // If we're already on the runner this just dispatches the method. | 63 // If we're already on the runner this just dispatches the method. |
64 template <class Method, class Params> | 64 template <class Method, class Params> |
65 void Notify(Method method, const Params& params) const { | 65 void Notify(Method method, const Params& params) const { |
66 static_assert( | 66 static_assert( |
67 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), | 67 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), |
68 "bad unbound method params"); | 68 "bad unbound method params"); |
69 for (typename ObserversListMap::const_iterator it = observers_.begin(); | 69 for (typename ObserversListMap::const_iterator it = observers_.begin(); |
70 it != observers_.end(); ++it) { | 70 it != observers_.end(); ++it) { |
71 if (!it->second.get() || it->second->RunsTasksOnCurrentThread()) { | 71 if (!it->second.get() || it->second->RunsTasksOnCurrentThread()) { |
72 base::DispatchToMethod(UnwrapTraits::Unwrap(it->first), method, params); | 72 base::DispatchToMethod(it->first, method, params); |
73 continue; | 73 continue; |
74 } | 74 } |
75 it->second->PostTask( | 75 it->second->PostTask( |
76 FROM_HERE, | 76 FROM_HERE, |
77 base::Bind(&NotifyWrapper<ObserverStoreType, Method, Params>, | 77 base::Bind(&NotifyWrapper<ObserverStoreType, Method, Params>, |
78 it->first, method, params)); | 78 it->first, method, params)); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 private: | 82 private: |
83 typedef base::internal::UnwrapTraits<ObserverStoreType> UnwrapTraits; | |
84 | |
85 ObserversListMap observers_; | 83 ObserversListMap observers_; |
86 }; | 84 }; |
87 | 85 |
88 class FileAccessObserver; | 86 class FileAccessObserver; |
89 class FileChangeObserver; | 87 class FileChangeObserver; |
90 class FileUpdateObserver; | 88 class FileUpdateObserver; |
91 | 89 |
92 typedef TaskRunnerBoundObserverList<FileAccessObserver> AccessObserverList; | 90 typedef TaskRunnerBoundObserverList<FileAccessObserver> AccessObserverList; |
93 typedef TaskRunnerBoundObserverList<FileChangeObserver> ChangeObserverList; | 91 typedef TaskRunnerBoundObserverList<FileChangeObserver> ChangeObserverList; |
94 typedef TaskRunnerBoundObserverList<FileUpdateObserver> UpdateObserverList; | 92 typedef TaskRunnerBoundObserverList<FileUpdateObserver> UpdateObserverList; |
95 | 93 |
96 } // namespace storage | 94 } // namespace storage |
97 | 95 |
98 #endif // STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | 96 #endif // STORAGE_BROWSER_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ |
OLD | NEW |