OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMPL_H _ | 5 #ifndef SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMPL_H _ |
6 #define SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMPL_H _ | 6 #define SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMPL_H _ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | |
9 #include <memory> | 10 #include <memory> |
10 #include <set> | 11 #include <set> |
11 #include <unordered_map> | 12 #include <unordered_map> |
12 #include <utility> | 13 #include <utility> |
14 #include <vector> | |
13 | 15 |
16 #include "base/callback.h" | |
14 #include "base/optional.h" | 17 #include "base/optional.h" |
15 #include "base/values.h" | 18 #include "base/values.h" |
16 #include "mojo/public/cpp/bindings/binding_set.h" | 19 #include "mojo/public/cpp/bindings/binding_set.h" |
17 #include "mojo/public/cpp/bindings/interface_request.h" | 20 #include "mojo/public/cpp/bindings/interface_request.h" |
18 #include "mojo/public/cpp/bindings/strong_binding.h" | 21 #include "mojo/public/cpp/bindings/strong_binding.h" |
19 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" | 22 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" |
23 #include "services/resource_coordinator/public/cpp/coordination_unit_types.h" | |
20 #include "services/resource_coordinator/public/interfaces/coordination_unit.mojo m.h" | 24 #include "services/resource_coordinator/public/interfaces/coordination_unit.mojo m.h" |
21 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov ider.mojom.h" | 25 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov ider.mojom.h" |
22 #include "services/service_manager/public/cpp/service_context_ref.h" | 26 #include "services/service_manager/public/cpp/service_context_ref.h" |
23 | 27 |
24 namespace resource_coordinator { | 28 namespace resource_coordinator { |
25 | 29 |
30 // Collection to manage CoordinationUnitEvent listeners. | |
31 template <typename Listener, typename Filter> | |
32 class CoordinationUnitEventListenerRegistry { | |
33 public: | |
34 CoordinationUnitEventListenerRegistry() = default; | |
35 ~CoordinationUnitEventListenerRegistry() = default; | |
36 | |
37 // callbacks paired with this kNoFilter will always be invoked | |
38 static const Filter kNoFilter = static_cast<Filter>(0); | |
39 | |
40 void AddListener(Listener listener, Filter filter) { | |
41 registry_[filter].push_back(listener); | |
42 } | |
43 | |
44 void AddListener(Listener listener) { AddListener(listener, kNoFilter); } | |
45 | |
46 // Get the listeners who are are either registered to be invoked for a | |
47 // specific filter or were registered with the kNoFilter. | |
48 // TODO(matthalp) add iterator support to replace this call and | |
49 // avoid excessive copying that currently occurs | |
50 std::vector<Listener> GetListeners(Filter filter) { | |
51 std::vector<Listener> listeners; | |
52 | |
53 // Get listeners registered with a listener first. | |
54 AppendListeners(&listeners, filter); | |
55 | |
56 // Get listeners registered without a filter after. | |
57 if (filter != kNoFilter) { | |
58 AppendListeners(&listeners, kNoFilter); | |
59 } | |
60 | |
61 return listeners; | |
62 } | |
63 | |
64 std::vector<Listener> GetListenersWithoutAFilter() { | |
65 return GetListeners(kNoFilter); | |
66 } | |
67 | |
68 private: | |
69 // TODO(matthalp) Consider using a std::unordered_map instead. | |
70 std::map<Filter, std::vector<Listener>> registry_; | |
ducbui
2017/06/19 04:09:15
Is there any reason for not using std::unordered_m
matthalp
2017/06/19 04:18:12
std::unordered_map requires a std::hash implementa
| |
71 | |
72 void AppendListeners(std::vector<Listener>* listeners, Filter filter) { | |
73 listeners->insert(listeners->end(), registry_[filter].begin(), | |
74 registry_[filter].end()); | |
75 } | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitEventListenerRegistry); | |
78 }; | |
79 | |
26 class CoordinationUnitImpl : public mojom::CoordinationUnit { | 80 class CoordinationUnitImpl : public mojom::CoordinationUnit { |
81 typedef base::Callback<void( | |
82 const CoordinationUnitImpl* coordination_unit, | |
83 const CoordinationUnitImpl* child_coordination_unit)> | |
84 OnAddChildEventListener; | |
85 typedef base::Callback<void( | |
86 const CoordinationUnitImpl* coordination_unit, | |
87 const CoordinationUnitImpl* parent_coordination_unit)> | |
88 OnAddParentEventListener; | |
89 typedef base::Callback<void( | |
90 const CoordinationUnitImpl* coordination_unit, | |
91 const CoordinationUnitImpl* removed_child_coordination_unit)> | |
92 OnRemoveChildEventListener; | |
93 typedef base::Callback<void( | |
94 const CoordinationUnitImpl* coordination_unit, | |
95 const CoordinationUnitImpl* removed_parent_coordination_unit)> | |
96 OnRemoveParentEventListener; | |
97 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit, | |
98 mojom::PropertyType property)> | |
99 OnPropertyChangedEventListener; | |
100 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit)> | |
101 OnWillBeDestroyedEventListener; | |
102 | |
27 public: | 103 public: |
28 CoordinationUnitImpl( | 104 CoordinationUnitImpl( |
29 const CoordinationUnitID& id, | 105 const CoordinationUnitID& id, |
30 std::unique_ptr<service_manager::ServiceContextRef> service_ref); | 106 std::unique_ptr<service_manager::ServiceContextRef> service_ref); |
31 ~CoordinationUnitImpl() override; | 107 ~CoordinationUnitImpl() override; |
32 | 108 |
33 // Overridden from mojom::CoordinationUnit: | 109 // Overridden from mojom::CoordinationUnit: |
34 void SendEvent(mojom::EventPtr event) override; | 110 void SendEvent(mojom::EventPtr event) override; |
35 void GetID(const GetIDCallback& callback) override; | 111 void GetID(const GetIDCallback& callback) override; |
36 void AddBinding(mojom::CoordinationUnitRequest request) override; | 112 void AddBinding(mojom::CoordinationUnitRequest request) override; |
(...skipping 11 matching lines...) Expand all Loading... | |
48 return property_store_; | 124 return property_store_; |
49 } | 125 } |
50 | 126 |
51 static const double kCPUUsageMinimumForTesting; | 127 static const double kCPUUsageMinimumForTesting; |
52 static const double kCPUUsageUnmeasuredForTesting; | 128 static const double kCPUUsageUnmeasuredForTesting; |
53 virtual double GetCPUUsageForTesting(); | 129 virtual double GetCPUUsageForTesting(); |
54 | 130 |
55 // Clear property from internal key-value store | 131 // Clear property from internal key-value store |
56 void ClearProperty(mojom::PropertyType property); | 132 void ClearProperty(mojom::PropertyType property); |
57 // Retrieve property from internal key-value store | 133 // Retrieve property from internal key-value store |
58 base::Value GetProperty(mojom::PropertyType property); | 134 base::Value GetProperty(mojom::PropertyType property) const; |
59 // Set property from internal key-value store | 135 // Set property from internal key-value store |
60 void SetProperty(mojom::PropertyType property, base::Value value); | 136 void SetProperty(mojom::PropertyType property, base::Value value); |
61 | 137 |
138 // Notify an instance it will be destroyed before its destructor is | |
139 // actually called. | |
140 void WillBeDestroyed(); | |
141 | |
142 CoordinationUnitEventListenerRegistry<OnAddChildEventListener, | |
143 CoordinationUnitType>& | |
144 on_add_child_event_listeners() { | |
145 return on_add_child_event_listeners_; | |
146 } | |
147 CoordinationUnitEventListenerRegistry<OnAddParentEventListener, | |
148 CoordinationUnitType>& | |
149 on_add_parent_event_listeners() { | |
150 return on_add_parent_event_listeners_; | |
151 } | |
152 CoordinationUnitEventListenerRegistry<OnRemoveChildEventListener, | |
153 CoordinationUnitType>& | |
154 on_remove_child_event_listeners() { | |
155 return on_remove_child_event_listeners_; | |
156 } | |
157 CoordinationUnitEventListenerRegistry<OnRemoveParentEventListener, | |
158 CoordinationUnitType>& | |
159 on_remove_parent_event_listeners() { | |
160 return on_remove_parent_event_listeners_; | |
161 } | |
162 CoordinationUnitEventListenerRegistry<OnPropertyChangedEventListener, | |
163 mojom::PropertyType>& | |
164 on_property_changed_event_listeners() { | |
165 return on_property_changed_event_listeners_; | |
166 } | |
167 CoordinationUnitEventListenerRegistry<OnWillBeDestroyedEventListener, | |
168 CoordinationUnitType>& | |
169 on_will_be_destroyed_event_listeners() { | |
170 return on_will_be_destroyed_event_listeners_; | |
171 } | |
172 | |
62 protected: | 173 protected: |
63 const CoordinationUnitID id_; | 174 const CoordinationUnitID id_; |
64 std::set<CoordinationUnitImpl*> children_; | 175 std::set<CoordinationUnitImpl*> children_; |
65 std::set<CoordinationUnitImpl*> parents_; | 176 std::set<CoordinationUnitImpl*> parents_; |
66 | 177 |
67 private: | 178 private: |
68 bool AddChild(CoordinationUnitImpl* child); | 179 bool AddChild(CoordinationUnitImpl* child); |
69 bool RemoveChild(CoordinationUnitImpl* child); | 180 bool RemoveChild(CoordinationUnitImpl* child); |
70 void AddParent(CoordinationUnitImpl* parent); | 181 void AddParent(CoordinationUnitImpl* parent); |
71 void RemoveParent(CoordinationUnitImpl* parent); | 182 void RemoveParent(CoordinationUnitImpl* parent); |
(...skipping 15 matching lines...) Expand all Loading... | |
87 bool SelfOrParentHasFlagSet(StateFlags state); | 198 bool SelfOrParentHasFlagSet(StateFlags state); |
88 | 199 |
89 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; | 200 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; |
90 mojo::BindingSet<mojom::CoordinationUnit> bindings_; | 201 mojo::BindingSet<mojom::CoordinationUnit> bindings_; |
91 | 202 |
92 mojom::CoordinationPolicyCallbackPtr policy_callback_; | 203 mojom::CoordinationPolicyCallbackPtr policy_callback_; |
93 mojom::CoordinationPolicyPtr current_policy_; | 204 mojom::CoordinationPolicyPtr current_policy_; |
94 | 205 |
95 base::Optional<bool> state_flags_[kNumStateFlags]; | 206 base::Optional<bool> state_flags_[kNumStateFlags]; |
96 | 207 |
208 CoordinationUnitEventListenerRegistry<OnAddChildEventListener, | |
209 CoordinationUnitType> | |
210 on_add_child_event_listeners_; | |
211 CoordinationUnitEventListenerRegistry<OnAddParentEventListener, | |
212 CoordinationUnitType> | |
213 on_add_parent_event_listeners_; | |
214 CoordinationUnitEventListenerRegistry<OnRemoveChildEventListener, | |
215 CoordinationUnitType> | |
216 on_remove_child_event_listeners_; | |
217 CoordinationUnitEventListenerRegistry<OnRemoveParentEventListener, | |
218 CoordinationUnitType> | |
219 on_remove_parent_event_listeners_; | |
220 CoordinationUnitEventListenerRegistry<OnPropertyChangedEventListener, | |
221 mojom::PropertyType> | |
222 on_property_changed_event_listeners_; | |
223 // There is nothing to filter WillBeDestroyedEventCallbacks on so the | |
224 // CoordinationUnitType is used as a filter placeholder | |
225 CoordinationUnitEventListenerRegistry<OnWillBeDestroyedEventListener, | |
226 CoordinationUnitType> | |
227 on_will_be_destroyed_event_listeners_; | |
228 | |
97 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); | 229 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); |
98 }; | 230 }; |
99 | 231 |
100 } // namespace resource_coordinator | 232 } // namespace resource_coordinator |
101 | 233 |
102 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ | 234 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ |
OLD | NEW |