Chromium Code Reviews| 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_; | |
| 71 | |
| 72 void AppendListeners(std::vector<Listener>* listeners, Filter filter) { | |
| 73 for (auto& listener : registry_[filter]) { | |
| 74 listeners->push_back(listener); | |
| 75 } | |
|
Zhen Wang
2017/06/15 20:07:53
I think you can just use std::vector::insert to ap
matthalp
2017/06/16 00:01:27
Thank you for pointing this out -- done.
| |
| 76 } | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitEventListenerRegistry); | |
| 79 }; | |
| 80 | |
| 26 class CoordinationUnitImpl : public mojom::CoordinationUnit { | 81 class CoordinationUnitImpl : public mojom::CoordinationUnit { |
| 82 typedef base::Callback<void( | |
| 83 const CoordinationUnitImpl* coordination_unit, | |
| 84 const CoordinationUnitImpl* child_coordination_unit)> | |
| 85 OnAddChildEventListener; | |
| 86 typedef base::Callback<void( | |
| 87 const CoordinationUnitImpl* coordination_unit, | |
| 88 const CoordinationUnitImpl* parent_coordination_unit)> | |
| 89 OnAddParentEventListener; | |
| 90 typedef base::Callback<void( | |
| 91 const CoordinationUnitImpl* coordination_unit, | |
| 92 const CoordinationUnitImpl* removed_child_coordination_unit)> | |
| 93 OnRemoveChildEventListener; | |
| 94 typedef base::Callback<void( | |
| 95 const CoordinationUnitImpl* coordination_unit, | |
| 96 const CoordinationUnitImpl* removed_parent_coordination_unit)> | |
| 97 OnRemoveParentEventListener; | |
| 98 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit, | |
| 99 mojom::PropertyType property)> | |
| 100 OnPropertyChangedEventListener; | |
| 101 typedef base::Callback<void(const CoordinationUnitImpl* coordination_unit)> | |
| 102 OnWillBeDestroyedEventListener; | |
| 103 | |
| 27 public: | 104 public: |
| 28 CoordinationUnitImpl( | 105 CoordinationUnitImpl( |
| 29 const CoordinationUnitID& id, | 106 const CoordinationUnitID& id, |
| 30 std::unique_ptr<service_manager::ServiceContextRef> service_ref); | 107 std::unique_ptr<service_manager::ServiceContextRef> service_ref); |
| 31 ~CoordinationUnitImpl() override; | 108 ~CoordinationUnitImpl() override; |
| 32 | 109 |
| 33 // Overridden from mojom::CoordinationUnit: | 110 // Overridden from mojom::CoordinationUnit: |
| 34 void SendEvent(mojom::EventPtr event) override; | 111 void SendEvent(mojom::EventPtr event) override; |
| 35 void GetID(const GetIDCallback& callback) override; | 112 void GetID(const GetIDCallback& callback) override; |
| 36 void AddBinding(mojom::CoordinationUnitRequest request) override; | 113 void AddBinding(mojom::CoordinationUnitRequest request) override; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 48 return property_store_; | 125 return property_store_; |
| 49 } | 126 } |
| 50 | 127 |
| 51 static const double kCPUUsageMinimumForTesting; | 128 static const double kCPUUsageMinimumForTesting; |
| 52 static const double kCPUUsageUnmeasuredForTesting; | 129 static const double kCPUUsageUnmeasuredForTesting; |
| 53 virtual double GetCPUUsageForTesting(); | 130 virtual double GetCPUUsageForTesting(); |
| 54 | 131 |
| 55 // Clear property from internal key-value store | 132 // Clear property from internal key-value store |
| 56 void ClearProperty(mojom::PropertyType property); | 133 void ClearProperty(mojom::PropertyType property); |
| 57 // Retrieve property from internal key-value store | 134 // Retrieve property from internal key-value store |
| 58 base::Value GetProperty(mojom::PropertyType property); | 135 base::Value GetProperty(mojom::PropertyType property) const; |
| 59 // Set property from internal key-value store | 136 // Set property from internal key-value store |
| 60 void SetProperty(mojom::PropertyType property, base::Value value); | 137 void SetProperty(mojom::PropertyType property, base::Value value); |
| 61 | 138 |
| 139 // Notify an instance it will be destroyed before its destructor is | |
| 140 // actually called. | |
| 141 void WillBeDestroyed(); | |
| 142 | |
| 143 CoordinationUnitEventListenerRegistry<OnAddChildEventListener, | |
| 144 CoordinationUnitType>& | |
| 145 on_add_child_event_listeners() { | |
| 146 return on_add_child_event_listeners_; | |
| 147 } | |
| 148 CoordinationUnitEventListenerRegistry<OnAddParentEventListener, | |
| 149 CoordinationUnitType>& | |
| 150 on_add_parent_event_listeners() { | |
| 151 return on_add_parent_event_listeners_; | |
| 152 } | |
| 153 CoordinationUnitEventListenerRegistry<OnRemoveChildEventListener, | |
| 154 CoordinationUnitType>& | |
| 155 on_remove_child_event_listeners() { | |
| 156 return on_remove_child_event_listeners_; | |
| 157 } | |
| 158 CoordinationUnitEventListenerRegistry<OnRemoveParentEventListener, | |
| 159 CoordinationUnitType>& | |
| 160 on_remove_parent_event_listeners() { | |
| 161 return on_remove_parent_event_listeners_; | |
| 162 } | |
| 163 CoordinationUnitEventListenerRegistry<OnPropertyChangedEventListener, | |
| 164 mojom::PropertyType>& | |
| 165 on_property_changed_event_listeners() { | |
| 166 return on_property_changed_event_listeners_; | |
| 167 } | |
| 168 CoordinationUnitEventListenerRegistry<OnWillBeDestroyedEventListener, | |
| 169 CoordinationUnitType>& | |
| 170 on_will_be_destroyed_event_listeners() { | |
| 171 return on_will_be_destroyed_event_listeners_; | |
| 172 } | |
| 173 | |
| 62 protected: | 174 protected: |
| 63 const CoordinationUnitID id_; | 175 const CoordinationUnitID id_; |
| 64 std::set<CoordinationUnitImpl*> children_; | 176 std::set<CoordinationUnitImpl*> children_; |
| 65 std::set<CoordinationUnitImpl*> parents_; | 177 std::set<CoordinationUnitImpl*> parents_; |
| 66 | 178 |
| 67 private: | 179 private: |
| 68 bool AddChild(CoordinationUnitImpl* child); | 180 bool AddChild(CoordinationUnitImpl* child); |
| 69 bool RemoveChild(CoordinationUnitImpl* child); | 181 bool RemoveChild(CoordinationUnitImpl* child); |
| 70 void AddParent(CoordinationUnitImpl* parent); | 182 void AddParent(CoordinationUnitImpl* parent); |
| 71 void RemoveParent(CoordinationUnitImpl* parent); | 183 void RemoveParent(CoordinationUnitImpl* parent); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 87 bool SelfOrParentHasFlagSet(StateFlags state); | 199 bool SelfOrParentHasFlagSet(StateFlags state); |
| 88 | 200 |
| 89 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; | 201 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; |
| 90 mojo::BindingSet<mojom::CoordinationUnit> bindings_; | 202 mojo::BindingSet<mojom::CoordinationUnit> bindings_; |
| 91 | 203 |
| 92 mojom::CoordinationPolicyCallbackPtr policy_callback_; | 204 mojom::CoordinationPolicyCallbackPtr policy_callback_; |
| 93 mojom::CoordinationPolicyPtr current_policy_; | 205 mojom::CoordinationPolicyPtr current_policy_; |
| 94 | 206 |
| 95 base::Optional<bool> state_flags_[kNumStateFlags]; | 207 base::Optional<bool> state_flags_[kNumStateFlags]; |
| 96 | 208 |
| 209 CoordinationUnitEventListenerRegistry<OnAddChildEventListener, | |
| 210 CoordinationUnitType> | |
| 211 on_add_child_event_listeners_; | |
| 212 CoordinationUnitEventListenerRegistry<OnAddParentEventListener, | |
| 213 CoordinationUnitType> | |
| 214 on_add_parent_event_listeners_; | |
| 215 CoordinationUnitEventListenerRegistry<OnRemoveChildEventListener, | |
| 216 CoordinationUnitType> | |
| 217 on_remove_child_event_listeners_; | |
| 218 CoordinationUnitEventListenerRegistry<OnRemoveParentEventListener, | |
| 219 CoordinationUnitType> | |
| 220 on_remove_parent_event_listeners_; | |
| 221 CoordinationUnitEventListenerRegistry<OnPropertyChangedEventListener, | |
| 222 mojom::PropertyType> | |
| 223 on_property_changed_event_listeners_; | |
| 224 // There is nothing to filter WillBeDestroyedEventCallbacks on so the | |
| 225 // CoordinationUnitType is used as a filter placeholder | |
| 226 CoordinationUnitEventListenerRegistry<OnWillBeDestroyedEventListener, | |
| 227 CoordinationUnitType> | |
| 228 on_will_be_destroyed_event_listeners_; | |
| 229 | |
| 97 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); | 230 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); |
| 98 }; | 231 }; |
| 99 | 232 |
| 100 } // namespace resource_coordinator | 233 } // namespace resource_coordinator |
| 101 | 234 |
| 102 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ | 235 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ |
| OLD | NEW |