| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef WEBKIT_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | |
| 6 #define WEBKIT_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/bind.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/sequenced_task_runner.h" | |
| 14 #include "base/threading/thread.h" | |
| 15 | |
| 16 namespace fileapi { | |
| 17 | |
| 18 // A wrapper for dispatching method. | |
| 19 template <class T, class Method, class Params> | |
| 20 void NotifyWrapper(T obj, Method m, const Params& p) { | |
| 21 DispatchToMethod(base::internal::UnwrapTraits<T>::Unwrap(obj), m, p); | |
| 22 } | |
| 23 | |
| 24 // An observer list helper to notify on a given task runner. | |
| 25 // Observer pointers (stored as ObserverStoreType) must be kept alive | |
| 26 // until this list dispatches all the notifications. | |
| 27 // | |
| 28 // Unlike regular ObserverList or ObserverListThreadSafe internal observer | |
| 29 // list is immutable (though not declared const) and cannot be modified after | |
| 30 // constructed. | |
| 31 // | |
| 32 // It is ok to specify scoped_refptr<Observer> as ObserverStoreType to | |
| 33 // explicitly keep references if necessary. | |
| 34 template <class Observer, class ObserverStoreType = Observer*> | |
| 35 class TaskRunnerBoundObserverList { | |
| 36 public: | |
| 37 // A constructor parameter class. | |
| 38 class Source { | |
| 39 public: | |
| 40 typedef scoped_refptr<base::SequencedTaskRunner> TaskRunnerPtr; | |
| 41 typedef std::map<ObserverStoreType, TaskRunnerPtr> ObserversListMap; | |
| 42 | |
| 43 // Add |observer| to the list parameter. The |observer| will be notified on | |
| 44 // the |runner_to_notify| runner. It is valid to give NULL as | |
| 45 // |runner_to_notify| (in such case notifications are dispatched on | |
| 46 // the current runner). | |
| 47 void AddObserver(Observer* observer, | |
| 48 base::SequencedTaskRunner* runner_to_notify) { | |
| 49 observers_.insert(std::make_pair(observer, runner_to_notify)); | |
| 50 } | |
| 51 | |
| 52 const ObserversListMap& observers() const { return observers_; } | |
| 53 | |
| 54 private: | |
| 55 ObserversListMap observers_; | |
| 56 }; | |
| 57 | |
| 58 // Creates an empty list. | |
| 59 TaskRunnerBoundObserverList<Observer, ObserverStoreType>() {} | |
| 60 | |
| 61 // Creates a new list with given |observers_param|. | |
| 62 explicit TaskRunnerBoundObserverList<Observer, ObserverStoreType>( | |
| 63 const Source& observers) | |
| 64 : source_(observers) {} | |
| 65 | |
| 66 virtual ~TaskRunnerBoundObserverList<Observer, ObserverStoreType>() {} | |
| 67 | |
| 68 // Notify on the task runner that is given to AddObserver. | |
| 69 // If we're already on the runner this just dispatches the method. | |
| 70 template <class Method, class Params> | |
| 71 void Notify(Method method, const Params& params) const { | |
| 72 COMPILE_ASSERT( | |
| 73 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), | |
| 74 badunboundmethodparams); | |
| 75 for (typename ObserversListMap::const_iterator it = | |
| 76 source_.observers().begin(); | |
| 77 it != source_.observers().end(); ++it) { | |
| 78 if (!it->second.get() || it->second->RunsTasksOnCurrentThread()) { | |
| 79 DispatchToMethod(UnwrapTraits::Unwrap(it->first), method, params); | |
| 80 continue; | |
| 81 } | |
| 82 it->second->PostTask( | |
| 83 FROM_HERE, | |
| 84 base::Bind(&NotifyWrapper<ObserverStoreType, Method, Params>, | |
| 85 it->first, method, params)); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 const Source& source() const { return source_; } | |
| 90 | |
| 91 private: | |
| 92 typedef base::internal::UnwrapTraits<ObserverStoreType> UnwrapTraits; | |
| 93 typedef scoped_refptr<base::SequencedTaskRunner> TaskRunnerPtr; | |
| 94 typedef std::map<ObserverStoreType, TaskRunnerPtr> ObserversListMap; | |
| 95 | |
| 96 Source source_; | |
| 97 }; | |
| 98 | |
| 99 class FileAccessObserver; | |
| 100 class FileChangeObserver; | |
| 101 class FileUpdateObserver; | |
| 102 | |
| 103 typedef TaskRunnerBoundObserverList<FileAccessObserver> AccessObserverList; | |
| 104 typedef TaskRunnerBoundObserverList<FileChangeObserver> ChangeObserverList; | |
| 105 typedef TaskRunnerBoundObserverList<FileUpdateObserver> UpdateObserverList; | |
| 106 | |
| 107 } // namespace fileapi | |
| 108 | |
| 109 #endif // WEBKIT_FILEAPI_TASK_RUNNER_BOUND_OBSERVER_LIST_H_ | |
| OLD | NEW |