Index: services/resource_coordinator/public/cpp/resource_coordinator_interface.cc |
diff --git a/services/resource_coordinator/public/cpp/resource_coordinator_interface.cc b/services/resource_coordinator/public/cpp/resource_coordinator_interface.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5963348a9bc6e20eea01505aa61e7c7a9149516a |
--- /dev/null |
+++ b/services/resource_coordinator/public/cpp/resource_coordinator_interface.cc |
@@ -0,0 +1,79 @@ |
+// 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/public/cpp/resource_coordinator_interface.h" |
+ |
+#include "base/atomic_sequence_num.h" |
+#include "base/process/process_handle.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "mojo/public/cpp/bindings/binding.h" |
+#include "services/resource_coordinator/public/interfaces/coordination_unit_provider.mojom.h" |
+#include "services/resource_coordinator/public/interfaces/service_constants.mojom.h" |
+#include "services/service_manager/public/cpp/connector.h" |
+ |
+namespace { |
+ |
+void OnConnectionError() { |
+ CHECK(false); |
+} |
+ |
+base::StaticAtomicSequenceNumber g_next_id; |
+ |
+} // namespace |
+ |
+namespace resource_coordinator { |
+ |
+ResourceCoordinatorInterface::ResourceCoordinatorInterface( |
+ service_manager::Connector* connector, |
+ const CoordinationUnitType& type) |
+ : weak_ptr_factory_(this) { |
+ std::string id = base::IntToString(base::GetCurrentProcId()) + "." + |
Primiano Tucci (use gerrit)
2017/04/11 18:04:52
isn't GetCurrentProcId going to be always 1 in the
oystein (OOO til 10th of July)
2017/04/12 19:15:31
Yep that makes perfect sense; moved to the service
|
+ base::IntToString(g_next_id.GetNext()); |
+ ConnectToService(connector, type, id); |
+} |
+ |
+ResourceCoordinatorInterface::~ResourceCoordinatorInterface() = default; |
+ |
+void ResourceCoordinatorInterface::ConnectToService( |
+ service_manager::Connector* connector, |
+ const CoordinationUnitType& type, |
+ const std::string& id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(connector); |
+ mojom::CoordinationUnitProviderPtr provider; |
+ connector->BindInterface(mojom::kServiceName, mojo::MakeRequest(&provider)); |
+ |
+ CoordinationUnitID new_cu_id(type, id); |
+ |
+ provider->CreateCoordinationUnit(mojo::MakeRequest(&service_), new_cu_id); |
+ |
+ service_.set_connection_error_handler(base::Bind(&OnConnectionError)); |
+} |
+ |
+void ResourceCoordinatorInterface::SendEvent( |
+ const resource_coordinator::EventType& event_type) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ mojom::EventPtr event = mojom::Event::New(); |
+ event->type = event_type; |
+ |
+ service_->SendEvent(std::move(event)); |
+} |
+ |
+void ResourceCoordinatorInterface::AddChild( |
+ const ResourceCoordinatorInterface& child) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ DCHECK(service_); |
+ // We could keep the ID around ourselves, but this hop ensures that the child |
+ // has been created on the service-side. |
+ child.service()->GetID(base::Bind(&ResourceCoordinatorInterface::AddChildByID, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void ResourceCoordinatorInterface::AddChildByID( |
+ const CoordinationUnitID& child_id) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ service_->AddChild(child_id); |
+} |
+ |
+} // namespace resource_coordinator |