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 <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <unordered_map> | 11 #include <unordered_map> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include <vector> | |
15 #include "base/callback.h" | |
16 | |
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 // It is naive prototype with lots of room for improvement. | |
32 template <typename Listener, typename Filter> | |
33 class CoordinationUnitEventListenerList { | |
34 public: | |
35 CoordinationUnitEventListenerList() = default; | |
36 ~CoordinationUnitEventListenerList() = default; | |
37 | |
38 // callbacks paired with this kWildcardFilter will always be invoked | |
39 static const Filter kWildcardFilter = static_cast<Filter>(0); | |
40 | |
41 void AddListener(Listener listener, Filter filter) { | |
42 listeners_.push_back(std::make_pair(filter, listener)); | |
43 } | |
44 | |
45 void AddListener(Listener listener) { | |
46 AddListener(listener, kWildcardFilter); | |
47 } | |
48 | |
49 // Get the listeners who are are either registered to be invoked for a | |
50 // specific filter or were registered with the kWildcardFilter. | |
51 // TODO(matthalp) add iterator support to replace this call and | |
52 // avoid excessive copying that currently occurs | |
53 std::vector<Listener> GetListeners(Filter filter) { | |
54 std::vector<Listener> listeners; | |
55 | |
56 for (auto& listener_info : listeners_) { | |
57 if (listener_info.first == filter || | |
58 listener_info.first == kWildcardFilter) { | |
59 listeners.push_back(listener_info.second); | |
60 } | |
61 } | |
62 | |
63 return listeners; | |
64 } | |
65 | |
66 std::vector<Listener> GetListeners() { return GetListeners(kWildcardFilter); } | |
zhenw
2017/06/13 18:38:41
There can be 2 ways to interpret this:
1. Get the
matthalp
2017/06/13 20:57:11
I see the ambiguity. I don't believe this method i
| |
67 | |
68 private: | |
69 std::vector<std::pair<Filter, Listener>> listeners_; | |
zhenw
2017/06/13 18:38:41
A map of vectors is probably more efficient.
matthalp
2017/06/13 20:57:11
I can do this.
| |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitEventListenerList); | |
72 }; | |
73 | |
26 class CoordinationUnitImpl : public mojom::CoordinationUnit { | 74 class CoordinationUnitImpl : public mojom::CoordinationUnit { |
75 typedef base::Callback<void( | |
76 const CoordinationUnitImpl* coordination_unit, | |
77 const CoordinationUnitImpl* child_coordination_unit)> | |
78 AddChildEventListener; | |
zhenw
2017/06/13 18:38:41
Those should follow On*() style.
matthalp
2017/06/13 20:57:11
I can do this.
| |
79 typedef base::Callback<void( | |
80 const CoordinationUnitImpl* coordination_unit, | |
81 const CoordinationUnitImpl* parent_coordination_unit)> | |
82 AddParentEventListener; | |
83 typedef base::Callback<void( | |
84 const CoordinationUnitImpl* coordination_unit, | |
85 const CoordinationUnitImpl* removed_child_coordination_unit)> | |
86 RemoveChildEventListener; | |
87 typedef base::Callback<void( | |
88 const CoordinationUnitImpl* coordination_unit, | |
89 const CoordinationUnitImpl* removed_parent_coordination_unit)> | |
90 RemoveParentEventListener; | |
91 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit, | |
92 mojom::PropertyType property)> | |
93 PropertyChangedEventListener; | |
94 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit)> | |
95 WillBeDestroyedEventListener; | |
96 | |
27 public: | 97 public: |
28 CoordinationUnitImpl( | 98 CoordinationUnitImpl( |
29 const CoordinationUnitID& id, | 99 const CoordinationUnitID& id, |
30 std::unique_ptr<service_manager::ServiceContextRef> service_ref); | 100 std::unique_ptr<service_manager::ServiceContextRef> service_ref); |
31 ~CoordinationUnitImpl() override; | 101 ~CoordinationUnitImpl() override; |
32 | 102 |
33 // Overridden from mojom::CoordinationUnit: | 103 // Overridden from mojom::CoordinationUnit: |
34 void SendEvent(mojom::EventPtr event) override; | 104 void SendEvent(mojom::EventPtr event) override; |
35 void GetID(const GetIDCallback& callback) override; | 105 void GetID(const GetIDCallback& callback) override; |
36 void AddBinding(mojom::CoordinationUnitRequest request) override; | 106 void AddBinding(mojom::CoordinationUnitRequest request) override; |
(...skipping 11 matching lines...) Expand all Loading... | |
48 return property_store_; | 118 return property_store_; |
49 } | 119 } |
50 | 120 |
51 static const double kCPUUsageMinimumForTesting; | 121 static const double kCPUUsageMinimumForTesting; |
52 static const double kCPUUsageUnmeasuredForTesting; | 122 static const double kCPUUsageUnmeasuredForTesting; |
53 virtual double GetCPUUsageForTesting(); | 123 virtual double GetCPUUsageForTesting(); |
54 | 124 |
55 // Clear property from internal key-value store | 125 // Clear property from internal key-value store |
56 void ClearProperty(mojom::PropertyType property); | 126 void ClearProperty(mojom::PropertyType property); |
57 // Retrieve property from internal key-value store | 127 // Retrieve property from internal key-value store |
58 base::Value GetProperty(mojom::PropertyType property); | 128 base::Value GetProperty(mojom::PropertyType property) const; |
59 // Set property from internal key-value store | 129 // Set property from internal key-value store |
60 void SetProperty(mojom::PropertyType property, base::Value value); | 130 void SetProperty(mojom::PropertyType property, base::Value value); |
61 | 131 |
132 // Notify an instance it will be destroyed before its destructor is | |
133 // actually called. | |
134 void WillBeDestroyed(); | |
135 | |
136 CoordinationUnitEventListenerList<AddChildEventListener, | |
137 CoordinationUnitType>& | |
138 on_add_child_event_listeners() { | |
139 return on_add_child_event_listeners_; | |
140 } | |
141 CoordinationUnitEventListenerList<AddParentEventListener, | |
142 CoordinationUnitType>& | |
143 on_add_parent_event_listeners() { | |
144 return on_add_parent_event_listeners_; | |
145 } | |
146 CoordinationUnitEventListenerList<RemoveChildEventListener, | |
147 CoordinationUnitType>& | |
148 on_remove_child_event_listeners() { | |
149 return on_remove_child_event_listeners_; | |
150 } | |
151 CoordinationUnitEventListenerList<RemoveParentEventListener, | |
152 CoordinationUnitType>& | |
153 on_remove_parent_event_listeners() { | |
154 return on_remove_parent_event_listeners_; | |
155 } | |
156 CoordinationUnitEventListenerList<PropertyChangedEventListener, | |
157 mojom::PropertyType>& | |
158 on_property_changed_event_listeners() { | |
159 return on_property_changed_event_listeners_; | |
160 } | |
161 CoordinationUnitEventListenerList<WillBeDestroyedEventListener, | |
162 CoordinationUnitType>& | |
163 on_will_be_destroyed_event_listeners() { | |
164 return on_will_be_destroyed_event_listeners_; | |
165 } | |
166 | |
62 protected: | 167 protected: |
63 const CoordinationUnitID id_; | 168 const CoordinationUnitID id_; |
64 std::set<CoordinationUnitImpl*> children_; | 169 std::set<CoordinationUnitImpl*> children_; |
65 std::set<CoordinationUnitImpl*> parents_; | 170 std::set<CoordinationUnitImpl*> parents_; |
66 | 171 |
67 private: | 172 private: |
68 bool AddChild(CoordinationUnitImpl* child); | 173 bool AddChild(CoordinationUnitImpl* child); |
69 bool RemoveChild(CoordinationUnitImpl* child); | 174 bool RemoveChild(CoordinationUnitImpl* child); |
70 void AddParent(CoordinationUnitImpl* parent); | 175 void AddParent(CoordinationUnitImpl* parent); |
71 void RemoveParent(CoordinationUnitImpl* parent); | 176 void RemoveParent(CoordinationUnitImpl* parent); |
(...skipping 14 matching lines...) Expand all Loading... | |
86 bool SelfOrParentHasFlagSet(StateFlags state); | 191 bool SelfOrParentHasFlagSet(StateFlags state); |
87 | 192 |
88 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; | 193 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; |
89 mojo::BindingSet<mojom::CoordinationUnit> bindings_; | 194 mojo::BindingSet<mojom::CoordinationUnit> bindings_; |
90 | 195 |
91 mojom::CoordinationPolicyCallbackPtr policy_callback_; | 196 mojom::CoordinationPolicyCallbackPtr policy_callback_; |
92 mojom::CoordinationPolicyPtr current_policy_; | 197 mojom::CoordinationPolicyPtr current_policy_; |
93 | 198 |
94 base::Optional<bool> state_flags_[kNumStateFlags]; | 199 base::Optional<bool> state_flags_[kNumStateFlags]; |
95 | 200 |
201 CoordinationUnitEventListenerList<AddChildEventListener, CoordinationUnitType> | |
202 on_add_child_event_listeners_; | |
203 CoordinationUnitEventListenerList<AddParentEventListener, | |
204 CoordinationUnitType> | |
205 on_add_parent_event_listeners_; | |
206 CoordinationUnitEventListenerList<RemoveChildEventListener, | |
207 CoordinationUnitType> | |
208 on_remove_child_event_listeners_; | |
209 CoordinationUnitEventListenerList<RemoveParentEventListener, | |
210 CoordinationUnitType> | |
211 on_remove_parent_event_listeners_; | |
212 CoordinationUnitEventListenerList<PropertyChangedEventListener, | |
213 mojom::PropertyType> | |
214 on_property_changed_event_listeners_; | |
215 // There is nothing to filter WillBeDestroyedEventCallbacks on so the | |
216 // CoordinationUnitType is used as a filter placeholder | |
217 CoordinationUnitEventListenerList<WillBeDestroyedEventListener, | |
218 CoordinationUnitType> | |
219 on_will_be_destroyed_event_listeners_; | |
220 | |
96 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); | 221 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); |
97 }; | 222 }; |
98 | 223 |
99 } // namespace resource_coordinator | 224 } // namespace resource_coordinator |
100 | 225 |
101 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ | 226 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ |
OLD | NEW |