OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/resource_coordinator/public/cpp/resource_coordinator_interfac
e.h" |
| 6 |
| 7 #include "mojo/public/cpp/bindings/binding.h" |
| 8 #include "services/resource_coordinator/public/interfaces/coordination_unit_prov
ider.mojom.h" |
| 9 #include "services/resource_coordinator/public/interfaces/service_constants.mojo
m.h" |
| 10 #include "services/service_manager/public/cpp/connector.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 void OnConnectionError() { |
| 15 DCHECK(false); |
| 16 } |
| 17 |
| 18 } // namespace |
| 19 |
| 20 namespace resource_coordinator { |
| 21 |
| 22 ResourceCoordinatorInterface::ResourceCoordinatorInterface( |
| 23 service_manager::Connector* connector, |
| 24 const CoordinationUnitType& type) |
| 25 : weak_ptr_factory_(this) { |
| 26 ConnectToService(connector, type, std::string()); |
| 27 } |
| 28 |
| 29 ResourceCoordinatorInterface::ResourceCoordinatorInterface( |
| 30 service_manager::Connector* connector, |
| 31 const CoordinationUnitType& type, |
| 32 const std::string& id) |
| 33 : weak_ptr_factory_(this) { |
| 34 ConnectToService(connector, type, id); |
| 35 } |
| 36 |
| 37 ResourceCoordinatorInterface::~ResourceCoordinatorInterface() = default; |
| 38 |
| 39 void ResourceCoordinatorInterface::ConnectToService( |
| 40 service_manager::Connector* connector, |
| 41 const CoordinationUnitType& type, |
| 42 const std::string& id) { |
| 43 DCHECK(thread_checker_.CalledOnValidThread()); |
| 44 DCHECK(connector); |
| 45 mojom::CoordinationUnitProviderPtr provider; |
| 46 connector->BindInterface(mojom::kServiceName, mojo::MakeRequest(&provider)); |
| 47 |
| 48 CoordinationUnitID new_cu_id(type, id); |
| 49 |
| 50 provider->CreateCoordinationUnit(mojo::MakeRequest(&service_), new_cu_id); |
| 51 |
| 52 service_.set_connection_error_handler(base::Bind(&OnConnectionError)); |
| 53 } |
| 54 |
| 55 void ResourceCoordinatorInterface::SendEvent( |
| 56 const mojom::EventType& event_type) { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 mojom::EventPtr event = mojom::Event::New(); |
| 59 event->type = event_type; |
| 60 |
| 61 service_->SendEvent(std::move(event)); |
| 62 } |
| 63 |
| 64 void ResourceCoordinatorInterface::AddChild( |
| 65 const ResourceCoordinatorInterface& child) { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 DCHECK(service_); |
| 68 // We could keep the ID around ourselves, but this hop ensures that the child |
| 69 // has been created on the service-side. |
| 70 child.service()->GetID(base::Bind(&ResourceCoordinatorInterface::AddChildByID, |
| 71 weak_ptr_factory_.GetWeakPtr())); |
| 72 } |
| 73 |
| 74 void ResourceCoordinatorInterface::AddChildByID( |
| 75 const CoordinationUnitID& child_id) { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 service_->AddChild(child_id); |
| 78 } |
| 79 |
| 80 } // namespace resource_coordinator |
OLD | NEW |