summaryrefslogtreecommitdiff
path: root/chromium/base/fuchsia/service_provider_impl.cc
blob: d015557a7d43be26a48f319dc4eec8a8f751a9d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2019 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 "base/fuchsia/service_provider_impl.h"

#include <lib/sys/cpp/outgoing_directory.h>
#include <utility>

namespace base {
namespace fuchsia {

// static
std::unique_ptr<ServiceProviderImpl>
ServiceProviderImpl::CreateForOutgoingDirectory(
    sys::OutgoingDirectory* outgoing_directory) {
  fidl::InterfaceHandle<::fuchsia::io::Directory> service_directory;
  outgoing_directory->GetOrCreateDirectory("svc")->Serve(
      ::fuchsia::io::OPEN_RIGHT_READABLE | ::fuchsia::io::OPEN_RIGHT_WRITABLE,
      service_directory.NewRequest().TakeChannel());
  return std::make_unique<ServiceProviderImpl>(std::move(service_directory));
}

ServiceProviderImpl::ServiceProviderImpl(
    fidl::InterfaceHandle<::fuchsia::io::Directory> service_directory)
    : directory_(std::move(service_directory)) {}

ServiceProviderImpl::~ServiceProviderImpl() = default;

void ServiceProviderImpl::AddBinding(
    fidl::InterfaceRequest<::fuchsia::sys::ServiceProvider> request) {
  bindings_.AddBinding(this, std::move(request));
}

void ServiceProviderImpl::ConnectToService(std::string service_name,
                                           zx::channel client_handle) {
  directory_.Connect(service_name.c_str(), std::move(client_handle));
}

void ServiceProviderImpl::SetOnLastClientDisconnectedClosure(
    base::OnceClosure on_last_client_disconnected) {
  on_last_client_disconnected_ = std::move(on_last_client_disconnected);
  bindings_.set_empty_set_handler(
      fit::bind_member(this, &ServiceProviderImpl::OnBindingSetEmpty));
}

void ServiceProviderImpl::OnBindingSetEmpty() {
  bindings_.set_empty_set_handler(nullptr);
  std::move(on_last_client_disconnected_).Run();
}

}  // namespace fuchsia
}  // namespace base