Chromium Code Reviews| Index: services/resource_coordinator/coordination_unit/coordination_unit_impl.cc |
| diff --git a/services/resource_coordinator/coordination_unit/coordination_unit_impl.cc b/services/resource_coordinator/coordination_unit/coordination_unit_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e1a7282bca5dfe8d76fe85b310beaa06d87c8f1c |
| --- /dev/null |
| +++ b/services/resource_coordinator/coordination_unit/coordination_unit_impl.cc |
| @@ -0,0 +1,205 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "services/resource_coordinator/coordination_unit/coordination_unit_impl.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "base/process/process_handle.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/unguessable_token.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "services/resource_coordinator/public/cpp/coordination_unit_id.h" |
| + |
| +namespace resource_coordinator { |
| + |
| +namespace { |
| + |
| +using CUIDMap = std::unordered_map<CoordinationUnitID, CoordinationUnitImpl*>; |
| + |
| +CUIDMap& g_cu_map() { |
|
nasko
2017/04/21 00:17:32
Can't we just use base::LazyInstance<CUIDMap>?
dcheng
2017/04/21 00:23:16
I'm technically not here, but the jury is out on t
oystein (OOO til 10th of July)
2017/04/21 17:57:55
Yeah I was originally using LazyInstance, but ther
|
| + static CUIDMap* instance = new CUIDMap(); |
| + return *instance; |
| +} |
| + |
| +} // namespace |
| + |
| +CoordinationUnitImpl::CoordinationUnitImpl( |
| + const CoordinationUnitID& id, |
| + std::unique_ptr<service_manager::ServiceContextRef> service_ref) { |
| + if (!id.id) { |
| + id_ = CoordinationUnitID(id.type, |
| + base::UnguessableToken().Create().ToString()); |
| + } else { |
| + id_ = id; |
| + } |
| + |
| + auto it = g_cu_map().insert(std::make_pair(id_, this)); |
| + DCHECK(it.second); // Inserted successfully |
| + |
| + service_ref_ = std::move(service_ref); |
| +} |
| + |
| +CoordinationUnitImpl::~CoordinationUnitImpl() { |
| + g_cu_map().erase(id_); |
| + |
| + for (CoordinationUnitImpl* child : children_) { |
| + child->RemoveParent(this); |
| + } |
| + |
| + for (CoordinationUnitImpl* parent : parents_) { |
| + parent->RemoveChild(this); |
| + } |
| +} |
| + |
| +bool CoordinationUnitImpl::SelfOrParentHasFlagSet(StateFlags state) { |
| + const base::Optional<bool>& state_flag = state_flags_[state]; |
| + if (state_flag && *state_flag) |
| + return true; |
| + |
| + for (CoordinationUnitImpl* parent : parents_) { |
| + if (parent->SelfOrParentHasFlagSet(state)) |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void CoordinationUnitImpl::RecalcPolicy() { |
| + for (CoordinationUnitImpl* child : children_) { |
| + child->RecalcPolicy(); |
| + } |
| + |
| + if (!policy_callback_) { |
| + return; |
| + } |
| + |
| + bool background_priority = !SelfOrParentHasFlagSet(TAB_VISIBLE) && |
| + !SelfOrParentHasFlagSet(AUDIO_PLAYING); |
| + |
| + // Send the priority to the client if it's new or changed. |
| + if (!current_policy_) |
| + current_policy_ = mojom::Policy::New(); |
| + else if ((current_policy_->use_background_priority == background_priority) && |
| + !SelfOrParentHasFlagSet(StateFlags::TEST_STATE)) { |
| + return; |
| + } |
| + |
| + mojom::PolicyPtr policy = mojom::Policy::New(); |
| + policy->use_background_priority = background_priority; |
| + current_policy_->use_background_priority = background_priority; |
| + |
| + policy_callback_->SetPolicy(std::move(policy)); |
| +} |
| + |
| +void CoordinationUnitImpl::SendEvent(mojom::EventPtr event) { |
| + switch (event->type) { |
| + case mojom::EventType::ON_WEBCONTENTS_SHOWN: |
| + state_flags_[TAB_VISIBLE] = true; |
| + break; |
| + case mojom::EventType::ON_WEBCONTENTS_HIDDEN: |
| + state_flags_[TAB_VISIBLE] = false; |
| + break; |
| + case mojom::EventType::ON_PROCESS_AUDIO_STARTED: |
| + state_flags_[AUDIO_PLAYING] = true; |
| + break; |
| + case mojom::EventType::ON_PROCESS_AUDIO_STOPPED: |
| + state_flags_[AUDIO_PLAYING] = false; |
| + break; |
| + case mojom::EventType::TEST_EVENT: |
| + state_flags_[TEST_STATE] = true; |
| + break; |
| + default: |
| + return; |
| + } |
| + |
| + RecalcPolicy(); |
| +} |
| + |
| +void CoordinationUnitImpl::GetID(const GetIDCallback& callback) { |
| + callback.Run(id_); |
| +} |
| + |
| +void CoordinationUnitImpl::Duplicate(mojom::CoordinationUnitRequest request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +void CoordinationUnitImpl::AddChild(const CoordinationUnitID& child_id) { |
| + if (child_id == id_) |
| + return; |
| + |
| + auto child_iter = g_cu_map().find(child_id); |
| + if (child_iter != g_cu_map().end()) { |
| + CoordinationUnitImpl* child = child_iter->second; |
| + if (HasParent(child) || HasChild(child)) |
| + return; |
| + |
| + DCHECK(child->id_ == child_id); |
| + DCHECK(child != this); |
| + |
| + if (AddChild(child)) { |
| + child->AddParent(this); |
| + } |
| + } |
| +} |
| + |
| +bool CoordinationUnitImpl::AddChild(CoordinationUnitImpl* child) { |
| + return children_.count(child) ? false : children_.insert(child).second; |
| +} |
| + |
| +void CoordinationUnitImpl::RemoveChild(CoordinationUnitImpl* child) { |
| + size_t children_removed = children_.erase(child); |
| + DCHECK_EQ(1u, children_removed); |
| +} |
| + |
| +void CoordinationUnitImpl::AddParent(CoordinationUnitImpl* parent) { |
| + DCHECK_EQ(0u, parents_.count(parent)); |
| + parents_.insert(parent); |
| + |
| + RecalcPolicy(); |
| +} |
| + |
| +void CoordinationUnitImpl::RemoveParent(CoordinationUnitImpl* parent) { |
| + size_t parents_removed = parents_.erase(parent); |
| + DCHECK_EQ(1u, parents_removed); |
| + |
| + RecalcPolicy(); |
| +} |
| + |
| +bool CoordinationUnitImpl::HasParent(CoordinationUnitImpl* unit) { |
| + for (CoordinationUnitImpl* parent : parents_) { |
| + if (parent == unit || parent->HasParent(unit)) |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool CoordinationUnitImpl::HasChild(CoordinationUnitImpl* unit) { |
| + for (CoordinationUnitImpl* child : children_) { |
| + if (child == unit || child->HasChild(unit)) |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void CoordinationUnitImpl::SetPolicyCallback( |
| + mojom::PolicyCallbackPtr callback) { |
| + callback.set_connection_error_handler(base::Bind( |
| + &CoordinationUnitImpl::UnregisterPolicyCallback, base::Unretained(this))); |
| + |
| + policy_callback_ = std::move(callback); |
| + |
| + RecalcPolicy(); |
| +} |
| + |
| +void CoordinationUnitImpl::UnregisterPolicyCallback() { |
| + policy_callback_.reset(); |
| + current_policy_.reset(); |
| +} |
| + |
| +} // namespace resource_coordinator |