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