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 class CoordinationUnitGraphObserver; | |
31 | |
32 // Collection to manage CoordinationUnitEvent listeners. | |
Zhen Wang
2017/06/19 22:54:25
s/CoordinationUnitEvent listeners/CoordinatorUnitG
matthalp
2017/06/20 19:02:52
Removed CoordinationGraphObserverRegistry -- no lo
| |
33 template <typename Filter> | |
34 class CoordinationUnitGraphObserverRegistry { | |
Zhen Wang
2017/06/19 22:54:29
Remember to add RemoveObserver API. (This is just
lpy
2017/06/19 23:21:38
Let's leave the filtering to each CUGraphObserver
oystein (OOO til 10th of July)
2017/06/20 18:39:23
Agreed. I think it's better to defer the per-event
matthalp
2017/06/20 19:02:52
Removed CoordinationGraphObserverRegistry -- no lo
| |
35 public: | |
36 CoordinationUnitGraphObserverRegistry() = default; | |
37 ~CoordinationUnitGraphObserverRegistry() = default; | |
38 | |
39 // callbacks paired with this kNoFilter will always be invoked | |
40 static const Filter kNoFilter = static_cast<Filter>(0); | |
41 | |
42 void AddObserver(CoordinationUnitGraphObserver* observer, Filter filter) { | |
43 registry_[filter].push_back(observer); | |
44 } | |
45 | |
46 void AddObserver(CoordinationUnitGraphObserver* observer) { | |
Zhen Wang
2017/06/19 22:54:28
Comments needed for the above two functions.
matthalp
2017/06/20 19:02:52
Removed CoordinationGraphObserverRegistry -- no lo
| |
47 AddObserver(observer, kNoFilter); | |
48 } | |
49 | |
50 // Get the observers who are are either registered to be invoked for | |
51 // a specific filter or were registered with the kNoFilter. | |
52 // TODO(matthalp) add iterator support to replace this call and | |
53 // avoid excessive copying that currently occurs | |
54 std::vector<CoordinationUnitGraphObserver*> GetObserversForFilter( | |
55 Filter filter) { | |
56 std::vector<CoordinationUnitGraphObserver*> observers; | |
57 | |
58 // Get listeners registered with a listener first. | |
59 AppendObservers(&observers, filter); | |
60 | |
61 // Get listeners registered without a filter after. | |
Zhen Wang
2017/06/19 22:54:25
I think it should be s/without/with here? If you g
| |
62 if (filter != kNoFilter) { | |
63 AppendObservers(&observers, kNoFilter); | |
64 } | |
65 | |
66 return observers; | |
67 } | |
68 | |
69 std::vector<CoordinationUnitGraphObserver*> GetObserversWithoutAFilter() { | |
Zhen Wang
2017/06/19 22:54:25
nit: s/GetObserversWithoutAFilter/GetObserversWith
| |
70 return GetObserversForFilter(kNoFilter); | |
71 } | |
72 | |
73 private: | |
74 void AppendObservers(std::vector<CoordinationUnitGraphObserver*>* observers, | |
75 Filter filter) { | |
76 observers->insert(observers->end(), registry_[filter].begin(), | |
77 registry_[filter].end()); | |
78 } | |
79 | |
80 // TODO(matthalp) Consider using a std::unordered_map instead. | |
81 std::map<Filter, std::vector<CoordinationUnitGraphObserver*>> registry_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitGraphObserverRegistry); | |
84 }; | |
85 | |
26 class CoordinationUnitImpl : public mojom::CoordinationUnit { | 86 class CoordinationUnitImpl : public mojom::CoordinationUnit { |
27 public: | 87 public: |
28 CoordinationUnitImpl( | 88 CoordinationUnitImpl( |
29 const CoordinationUnitID& id, | 89 const CoordinationUnitID& id, |
30 std::unique_ptr<service_manager::ServiceContextRef> service_ref); | 90 std::unique_ptr<service_manager::ServiceContextRef> service_ref); |
31 ~CoordinationUnitImpl() override; | 91 ~CoordinationUnitImpl() override; |
32 | 92 |
33 // Overridden from mojom::CoordinationUnit: | 93 // Overridden from mojom::CoordinationUnit: |
34 void SendEvent(mojom::EventPtr event) override; | 94 void SendEvent(mojom::EventPtr event) override; |
35 void GetID(const GetIDCallback& callback) override; | 95 void GetID(const GetIDCallback& callback) override; |
(...skipping 12 matching lines...) Expand all Loading... | |
48 return property_store_; | 108 return property_store_; |
49 } | 109 } |
50 | 110 |
51 static const double kCPUUsageMinimumForTesting; | 111 static const double kCPUUsageMinimumForTesting; |
52 static const double kCPUUsageUnmeasuredForTesting; | 112 static const double kCPUUsageUnmeasuredForTesting; |
53 virtual double GetCPUUsageForTesting(); | 113 virtual double GetCPUUsageForTesting(); |
54 | 114 |
55 // Clear property from internal key-value store | 115 // Clear property from internal key-value store |
56 void ClearProperty(mojom::PropertyType property); | 116 void ClearProperty(mojom::PropertyType property); |
57 // Retrieve property from internal key-value store | 117 // Retrieve property from internal key-value store |
58 base::Value GetProperty(mojom::PropertyType property); | 118 base::Value GetProperty(mojom::PropertyType property) const; |
59 // Set property from internal key-value store | 119 // Set property from internal key-value store |
60 void SetProperty(mojom::PropertyType property, base::Value value); | 120 void SetProperty(mojom::PropertyType property, base::Value value); |
61 | 121 |
122 // Notify an instance it will be destroyed before its destructor is | |
123 // actually called. | |
124 void WillBeDestroyed(); | |
125 | |
126 void ObserveOnChildAddedEvent(CoordinationUnitGraphObserver* observer, | |
Zhen Wang
2017/06/19 22:54:26
Can you also add some comments for those APIs.
matthalp
2017/06/20 19:02:52
Filter has been removed -- no long applicable.
| |
127 CoordinationUnitType child_filter); | |
128 void ObserveOnChildAddedEvent(CoordinationUnitGraphObserver* observer); | |
Zhen Wang
2017/06/19 22:54:25
I forget if using default arguments are allowed in
oystein (OOO til 10th of July)
2017/06/20 18:39:23
AFAIK default arguments are allowed on non-virtual
matthalp
2017/06/20 19:02:52
Filter has been removed -- no long applicable.
| |
129 void ObserveOnParentAddedEvent(CoordinationUnitGraphObserver* observer, | |
130 CoordinationUnitType parent_filter); | |
131 void ObserveOnParentAddedEvent(CoordinationUnitGraphObserver* observer); | |
132 void ObserveOnPropertyChangedEvent(CoordinationUnitGraphObserver* observer, | |
133 mojom::PropertyType property_filter); | |
134 void ObserveOnPropertyChangedEvent(CoordinationUnitGraphObserver* observer); | |
135 void ObserveOnChildRemovedEvent(CoordinationUnitGraphObserver* observer, | |
136 CoordinationUnitType child_filter); | |
137 void ObserveOnChildRemovedEvent(CoordinationUnitGraphObserver* observer); | |
138 void ObserveOnParentRemovedEvent(CoordinationUnitGraphObserver* observer, | |
139 CoordinationUnitType parent_filter); | |
140 void ObserveOnParentRemovedEvent(CoordinationUnitGraphObserver* observer); | |
141 void ObserveOnWillBeDestroyedEvent(CoordinationUnitGraphObserver* observer); | |
142 | |
62 protected: | 143 protected: |
63 const CoordinationUnitID id_; | 144 const CoordinationUnitID id_; |
64 std::set<CoordinationUnitImpl*> children_; | 145 std::set<CoordinationUnitImpl*> children_; |
65 std::set<CoordinationUnitImpl*> parents_; | 146 std::set<CoordinationUnitImpl*> parents_; |
66 | 147 |
67 private: | 148 private: |
68 bool AddChild(CoordinationUnitImpl* child); | 149 bool AddChild(CoordinationUnitImpl* child); |
69 bool RemoveChild(CoordinationUnitImpl* child); | 150 bool RemoveChild(CoordinationUnitImpl* child); |
70 void AddParent(CoordinationUnitImpl* parent); | 151 void AddParent(CoordinationUnitImpl* parent); |
71 void RemoveParent(CoordinationUnitImpl* parent); | 152 void RemoveParent(CoordinationUnitImpl* parent); |
(...skipping 15 matching lines...) Expand all Loading... | |
87 bool SelfOrParentHasFlagSet(StateFlags state); | 168 bool SelfOrParentHasFlagSet(StateFlags state); |
88 | 169 |
89 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; | 170 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; |
90 mojo::BindingSet<mojom::CoordinationUnit> bindings_; | 171 mojo::BindingSet<mojom::CoordinationUnit> bindings_; |
91 | 172 |
92 mojom::CoordinationPolicyCallbackPtr policy_callback_; | 173 mojom::CoordinationPolicyCallbackPtr policy_callback_; |
93 mojom::CoordinationPolicyPtr current_policy_; | 174 mojom::CoordinationPolicyPtr current_policy_; |
94 | 175 |
95 base::Optional<bool> state_flags_[kNumStateFlags]; | 176 base::Optional<bool> state_flags_[kNumStateFlags]; |
96 | 177 |
178 CoordinationUnitGraphObserverRegistry<CoordinationUnitType> | |
179 on_child_added_event_observer_registry_; | |
180 CoordinationUnitGraphObserverRegistry<CoordinationUnitType> | |
181 on_parent_added_event_observer_registry_; | |
182 CoordinationUnitGraphObserverRegistry<CoordinationUnitType> | |
183 on_child_removed_event_observer_registry_; | |
184 CoordinationUnitGraphObserverRegistry<CoordinationUnitType> | |
185 on_parent_removed_event_observer_registry_; | |
186 CoordinationUnitGraphObserverRegistry<mojom::PropertyType> | |
187 on_property_changed_event_observer_registry_; | |
188 // There is nothing to filter WillBeDestroyedEventCallbacks on so the | |
189 // CoordinationUnitType is used as a filter placeholder | |
190 CoordinationUnitGraphObserverRegistry<CoordinationUnitType> | |
191 on_will_be_destroyed_event_observer_registry_; | |
192 | |
97 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); | 193 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); |
98 }; | 194 }; |
99 | 195 |
100 } // namespace resource_coordinator | 196 } // namespace resource_coordinator |
101 | 197 |
102 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ | 198 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ |
OLD | NEW |