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 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl
.h" | 5 #include "services/resource_coordinator/coordination_unit/coordination_unit_impl
.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <unordered_map> | 8 #include <unordered_map> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (AddChild(child)) { | 147 if (AddChild(child)) { |
148 child->AddParent(this); | 148 child->AddParent(this); |
149 } | 149 } |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { | 153 bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { |
154 // We don't recalculate the policy here as policies are only dependent | 154 // We don't recalculate the policy here as policies are only dependent |
155 // on the current CU or its parents, not its children. In other words, | 155 // on the current CU or its parents, not its children. In other words, |
156 // policies only bubble down. | 156 // policies only bubble down. |
157 return children_.count(child) ? false : children_.insert(child).second; | 157 bool success = |
| 158 children_.count(child) ? false : children_.insert(child).second; |
| 159 |
| 160 if (success) { |
| 161 for (auto listener : |
| 162 on_add_child_event_listeners_.GetListeners(child->id().type)) { |
| 163 listener.Run(this, child); |
| 164 } |
| 165 } |
| 166 |
| 167 return success; |
158 } | 168 } |
159 | 169 |
160 void CoordinationUnitImpl::RemoveChild(const CoordinationUnitID& child_id) { | 170 void CoordinationUnitImpl::RemoveChild(const CoordinationUnitID& child_id) { |
161 auto child_iter = g_cu_map().find(child_id); | 171 auto child_iter = g_cu_map().find(child_id); |
162 if (child_iter == g_cu_map().end()) { | 172 if (child_iter == g_cu_map().end()) { |
163 return; | 173 return; |
164 } | 174 } |
165 | 175 |
166 CoordinationUnitImpl* child = child_iter->second; | 176 CoordinationUnitImpl* child = child_iter->second; |
167 if (!HasChild(child)) { | 177 if (!HasChild(child)) { |
168 return; | 178 return; |
169 } | 179 } |
170 | 180 |
171 DCHECK(child->id_ == child_id); | 181 DCHECK(child->id_ == child_id); |
172 DCHECK(child != this); | 182 DCHECK(child != this); |
173 | 183 |
174 if (RemoveChild(child)) { | 184 if (RemoveChild(child)) { |
175 child->RemoveParent(this); | 185 child->RemoveParent(this); |
176 } | 186 } |
177 } | 187 } |
178 | 188 |
179 bool CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { | 189 bool CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { |
180 size_t children_removed = children_.erase(child); | 190 size_t children_removed = children_.erase(child); |
181 return children_removed > 0; | 191 bool success = children_removed > 0; |
| 192 |
| 193 if (success) { |
| 194 for (auto listener : |
| 195 on_remove_child_event_listeners_.GetListeners(child->id().type)) { |
| 196 listener.Run(this, child); |
| 197 } |
| 198 } |
| 199 |
| 200 return success; |
182 } | 201 } |
183 | 202 |
184 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { | 203 void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { |
185 DCHECK_EQ(0u, parents_.count(parent)); | 204 DCHECK_EQ(0u, parents_.count(parent)); |
186 parents_.insert(parent); | 205 parents_.insert(parent); |
187 | 206 |
| 207 for (auto listener : |
| 208 on_add_parent_event_listeners_.GetListeners(parent->id().type)) { |
| 209 listener.Run(this, parent); |
| 210 } |
| 211 |
188 RecalcCoordinationPolicy(); | 212 RecalcCoordinationPolicy(); |
189 } | 213 } |
190 | 214 |
191 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { | 215 void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { |
192 size_t parents_removed = parents_.erase(parent); | 216 size_t parents_removed = parents_.erase(parent); |
193 DCHECK_EQ(1u, parents_removed); | 217 DCHECK_EQ(1u, parents_removed); |
194 | 218 |
| 219 // TODO(matthalp, oysteine) should this go before or |
| 220 // after RecalcCoordinationPolicy? |
| 221 for (auto listener : |
| 222 on_remove_parent_event_listeners_.GetListeners(parent->id().type)) { |
| 223 listener.Run(this, parent); |
| 224 } |
| 225 |
195 RecalcCoordinationPolicy(); | 226 RecalcCoordinationPolicy(); |
196 } | 227 } |
197 | 228 |
198 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { | 229 bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { |
199 for (CoordinationUnitImpl* parent : parents_) { | 230 for (CoordinationUnitImpl* parent : parents_) { |
200 if (parent == unit || parent->HasParent(unit)) { | 231 if (parent == unit || parent->HasParent(unit)) { |
201 return true; | 232 return true; |
202 } | 233 } |
203 } | 234 } |
204 | 235 |
(...skipping 23 matching lines...) Expand all Loading... |
228 | 259 |
229 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { | 260 void CoordinationUnitImpl::UnregisterCoordinationPolicyCallback() { |
230 policy_callback_.reset(); | 261 policy_callback_.reset(); |
231 current_policy_.reset(); | 262 current_policy_.reset(); |
232 } | 263 } |
233 | 264 |
234 double CoordinationUnitImpl::GetCPUUsageForTesting() { | 265 double CoordinationUnitImpl::GetCPUUsageForTesting() { |
235 return kCPUUsageUnmeasuredForTesting; | 266 return kCPUUsageUnmeasuredForTesting; |
236 } | 267 } |
237 | 268 |
238 base::Value CoordinationUnitImpl::GetProperty(mojom::PropertyType property) { | 269 base::Value CoordinationUnitImpl::GetProperty( |
| 270 mojom::PropertyType property) const { |
239 auto value_it = property_store_.find(property); | 271 auto value_it = property_store_.find(property); |
240 | 272 |
241 return value_it != property_store_.end() ? value_it->second : base::Value(); | 273 return value_it != property_store_.end() ? value_it->second : base::Value(); |
242 } | 274 } |
243 | 275 |
244 void CoordinationUnitImpl::ClearProperty(mojom::PropertyType property) { | 276 void CoordinationUnitImpl::ClearProperty(mojom::PropertyType property) { |
245 property_store_.erase(property); | 277 property_store_.erase(property); |
246 } | 278 } |
247 | 279 |
248 void CoordinationUnitImpl::SetProperty(mojom::PropertyPtr property) { | 280 void CoordinationUnitImpl::SetProperty(mojom::PropertyPtr property) { |
249 SetProperty(property->property, *property->value); | 281 SetProperty(property->property, *property->value); |
250 } | 282 } |
251 | 283 |
252 void CoordinationUnitImpl::SetProperty(mojom::PropertyType property, | 284 void CoordinationUnitImpl::SetProperty(mojom::PropertyType property, |
253 base::Value value) { | 285 base::Value value) { |
254 // setting a property with an empty value is effectively clearing the | 286 // setting a property with an empty value is effectively clearing the |
255 // value from storage | 287 // value from storage |
256 if (value.IsType(base::Value::Type::NONE)) { | 288 if (value.IsType(base::Value::Type::NONE)) { |
257 ClearProperty(property); | 289 ClearProperty(property); |
258 return; | 290 return; |
259 } | 291 } |
260 | 292 |
261 property_store_[property] = value; | 293 property_store_[property] = value; |
| 294 |
| 295 for (auto listener : |
| 296 on_property_changed_event_listeners_.GetListeners(property)) { |
| 297 listener.Run(this, property); |
| 298 } |
| 299 } |
| 300 |
| 301 void CoordinationUnitImpl::WillBeDestroyed() { |
| 302 for (auto listener : on_will_be_destroyed_event_listeners_.GetListeners()) { |
| 303 listener.Run(this); |
| 304 } |
262 } | 305 } |
263 | 306 |
264 } // namespace resource_coordinator | 307 } // namespace resource_coordinator |
OLD | NEW |