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

Unified Diff: services/resource_coordinator/coordination_unit/coordination_unit_impl.cc

Issue 2798713002: Global Resource Coordinator: Basic service internals (Closed)
Patch Set: Wrap EventType directly in resource_coordinator namespace when using helper Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
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..3612012addbc599ac989fd491c37c5b2b23f00d6
--- /dev/null
+++ b/services/resource_coordinator/coordination_unit/coordination_unit_impl.cc
@@ -0,0 +1,203 @@
+// 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 "base/logging.h"
+#include "base/process/process_handle.h"
+#include "base/strings/string_number_conversions.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() {
+ static CUIDMap* instance = new CUIDMap();
+ return instance;
dcheng 2017/04/18 05:40:46 Consider returning a ref, since this is never null
oystein (OOO til 10th of July) 2017/04/18 20:55:33 Done.
+}
+
+} // namespace
+
+CoordinationUnitImpl::CoordinationUnitImpl(
+ const CoordinationUnitID& id,
+ std::unique_ptr<service_manager::ServiceContextRef> service_ref) {
+ if (!id.id) {
+ static int next_id = 0;
+ id_ = CoordinationUnitID(id.type,
+ base::IntToString(base::GetCurrentProcId()) + "." +
dcheng 2017/04/18 05:40:46 I don't entirely understand the purpose of buildin
oystein (OOO til 10th of July) 2017/04/18 20:55:33 UnguessableToken makes more sense; this was more a
+ base::IntToString(++next_id));
+ } 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);
dcheng 2017/04/18 05:40:46 Nit: #include <utility>
oystein (OOO til 10th of July) 2017/04/18 20:55:33 Done.
+}
+
+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

Powered by Google App Engine
This is Rietveld 408576698