Chromium Code Reviews| 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..31e7c4453edda0884b0fffd12ec4a8234905f827 |
| --- /dev/null |
| +++ b/services/resource_coordinator/public/cpp/resource_coordinator_interface.cc |
| @@ -0,0 +1,77 @@ |
| +// 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/process/process_handle.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "mojo/public/cpp/bindings/binding.h" |
| +#include "services/resource_coordinator/public/interfaces/constants.mojom.h" |
| +#include "services/resource_coordinator/public/interfaces/coordination_unit_provider.mojom.h" |
| +#include "services/service_manager/public/cpp/connector.h" |
| + |
| +namespace { |
| + |
| +void OnConnectionError() { |
| + CHECK(false); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace resource_coordinator { |
| + |
| +ResourceCoordinatorInterface::ResourceCoordinatorInterface( |
| + service_manager::Connector* connector, |
| + const mojom::IDType& type) |
| + : weak_ptr_factory_(this) { |
| + static int next_id = 0; |
|
Sami
2017/03/22 13:51:43
Do we need some locking here? I assume we'd eventu
|
| + std::string id = base::IntToString(base::GetCurrentProcId()) + "." + |
| + base::IntToString(++next_id); |
| + ConnectToService(connector, type, id); |
| +} |
| + |
| +ResourceCoordinatorInterface::~ResourceCoordinatorInterface() = default; |
| + |
| +void ResourceCoordinatorInterface::ConnectToService( |
| + service_manager::Connector* connector, |
| + const mojom::IDType& type, |
| + const std::string& id) { |
| + DCHECK(connector); |
| + mojom::CoordinationUnitProviderPtr provider; |
| + connector->BindInterface(mojom::kServiceName, mojo::MakeRequest(&provider)); |
| + |
| + mojom::CoordinationUnitIDPtr options = mojom::CoordinationUnitID::New(); |
| + options->type = type; |
| + options->id = id; |
| + |
| + provider->CreateCoordinationUnit(mojo::MakeRequest(&service_), |
| + std::move(options)); |
| + |
| + service_.set_connection_error_handler(base::Bind(&OnConnectionError)); |
| +} |
| + |
| +void ResourceCoordinatorInterface::SendEvent( |
| + const resource_coordinator::mojom::EventType& event_type) { |
| + DCHECK(service_); |
| + mojom::EventPtr event = mojom::Event::New(); |
| + event->type = event_type; |
| + |
| + service_->SendEvent(std::move(event)); |
| +} |
| + |
| +void ResourceCoordinatorInterface::AddChild( |
| + const ResourceCoordinatorInterface& child) { |
| + 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( |
| + resource_coordinator::mojom::CoordinationUnitIDPtr child_id) { |
| + service_->AddChild(std::move(child_id)); |
| +} |
| + |
| +} // namespace resource_coordinator |