Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: services/resource_coordinator/coordination_unit/coordination_unit_impl.h

Issue 2942403002: [GRC] Coordination Unit Graph Observer (Closed)
Patch Set: Fix unittest and OnCoordinationUnitWillBeDestroyed Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map>
10 #include <memory> 10 #include <memory>
11 #include <set> 11 #include <set>
12 #include <unordered_map> 12 #include <unordered_map>
13 #include <utility> 13 #include <utility>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/observer_list.h"
17 #include "base/optional.h" 18 #include "base/optional.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "mojo/public/cpp/bindings/binding_set.h" 20 #include "mojo/public/cpp/bindings/binding_set.h"
20 #include "mojo/public/cpp/bindings/interface_request.h" 21 #include "mojo/public/cpp/bindings/interface_request.h"
21 #include "mojo/public/cpp/bindings/strong_binding.h" 22 #include "mojo/public/cpp/bindings/strong_binding.h"
22 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" 23 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h"
23 #include "services/resource_coordinator/public/cpp/coordination_unit_types.h" 24 #include "services/resource_coordinator/public/cpp/coordination_unit_types.h"
24 #include "services/resource_coordinator/public/interfaces/coordination_unit.mojo m.h" 25 #include "services/resource_coordinator/public/interfaces/coordination_unit.mojo m.h"
25 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov ider.mojom.h" 26 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov ider.mojom.h"
26 #include "services/service_manager/public/cpp/service_context_ref.h" 27 #include "services/service_manager/public/cpp/service_context_ref.h"
27 28
28 namespace resource_coordinator { 29 namespace resource_coordinator {
29 30
30 class CoordinationUnitGraphObserver; 31 class CoordinationUnitGraphObserver;
31 32
32 // Collection to manage CoordinationUnitEvent listeners.
33 template <typename Filter>
34 class CoordinationUnitGraphObserverRegistry {
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) {
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.
62 if (filter != kNoFilter) {
63 AppendObservers(&observers, kNoFilter);
64 }
65
66 return observers;
67 }
68
69 std::vector<CoordinationUnitGraphObserver*> GetObserversWithoutAFilter() {
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
86 class CoordinationUnitImpl : public mojom::CoordinationUnit { 33 class CoordinationUnitImpl : public mojom::CoordinationUnit {
87 public: 34 public:
88 CoordinationUnitImpl( 35 CoordinationUnitImpl(
89 const CoordinationUnitID& id, 36 const CoordinationUnitID& id,
90 std::unique_ptr<service_manager::ServiceContextRef> service_ref); 37 std::unique_ptr<service_manager::ServiceContextRef> service_ref);
91 ~CoordinationUnitImpl() override; 38 ~CoordinationUnitImpl() override;
92 39
93 // Overridden from mojom::CoordinationUnit: 40 // Overridden from mojom::CoordinationUnit:
94 void SendEvent(mojom::EventPtr event) override; 41 void SendEvent(mojom::EventPtr event) override;
95 void GetID(const GetIDCallback& callback) override; 42 void GetID(const GetIDCallback& callback) override;
(...skipping 16 matching lines...) Expand all
112 static const double kCPUUsageUnmeasuredForTesting; 59 static const double kCPUUsageUnmeasuredForTesting;
113 virtual double GetCPUUsageForTesting(); 60 virtual double GetCPUUsageForTesting();
114 61
115 // Clear property from internal key-value store 62 // Clear property from internal key-value store
116 void ClearProperty(mojom::PropertyType property); 63 void ClearProperty(mojom::PropertyType property);
117 // Retrieve property from internal key-value store 64 // Retrieve property from internal key-value store
118 base::Value GetProperty(mojom::PropertyType property) const; 65 base::Value GetProperty(mojom::PropertyType property) const;
119 // Set property from internal key-value store 66 // Set property from internal key-value store
120 void SetProperty(mojom::PropertyType property, base::Value value); 67 void SetProperty(mojom::PropertyType property, base::Value value);
121 68
122 // Notify an instance it will be destroyed before its destructor is
123 // actually called.
124 void WillBeDestroyed(); 69 void WillBeDestroyed();
125 70 void AddObserver(CoordinationUnitGraphObserver* observer);
126 void ObserveOnChildAddedEvent(CoordinationUnitGraphObserver* observer, 71 void RemoveObserver(CoordinationUnitGraphObserver* observer);
127 CoordinationUnitType child_filter);
128 void ObserveOnChildAddedEvent(CoordinationUnitGraphObserver* observer);
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 72
143 protected: 73 protected:
144 const CoordinationUnitID id_; 74 const CoordinationUnitID id_;
145 std::set<CoordinationUnitImpl*> children_; 75 std::set<CoordinationUnitImpl*> children_;
146 std::set<CoordinationUnitImpl*> parents_; 76 std::set<CoordinationUnitImpl*> parents_;
147 77
148 private: 78 private:
149 bool AddChild(CoordinationUnitImpl* child); 79 bool AddChild(CoordinationUnitImpl* child);
150 bool RemoveChild(CoordinationUnitImpl* child); 80 bool RemoveChild(CoordinationUnitImpl* child);
151 void AddParent(CoordinationUnitImpl* parent); 81 void AddParent(CoordinationUnitImpl* parent);
(...skipping 14 matching lines...) Expand all
166 kNumStateFlags 96 kNumStateFlags
167 }; 97 };
168 bool SelfOrParentHasFlagSet(StateFlags state); 98 bool SelfOrParentHasFlagSet(StateFlags state);
169 99
170 std::unique_ptr<service_manager::ServiceContextRef> service_ref_; 100 std::unique_ptr<service_manager::ServiceContextRef> service_ref_;
171 mojo::BindingSet<mojom::CoordinationUnit> bindings_; 101 mojo::BindingSet<mojom::CoordinationUnit> bindings_;
172 102
173 mojom::CoordinationPolicyCallbackPtr policy_callback_; 103 mojom::CoordinationPolicyCallbackPtr policy_callback_;
174 mojom::CoordinationPolicyPtr current_policy_; 104 mojom::CoordinationPolicyPtr current_policy_;
175 105
106 base::ObserverList<CoordinationUnitGraphObserver> observers_;
107
176 base::Optional<bool> state_flags_[kNumStateFlags]; 108 base::Optional<bool> state_flags_[kNumStateFlags];
177 109
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
193 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl); 110 DISALLOW_COPY_AND_ASSIGN(CoordinationUnitImpl);
194 }; 111 };
195 112
196 } // namespace resource_coordinator 113 } // namespace resource_coordinator
197 114
198 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_ 115 #endif // SERVICES_RESOURCE_COORDINATOR_COORDINATION_UNIT_COORDINATION_UNIT_IMP L_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698