1 // Copyright 2019 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.
5 #ifndef BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_
6 #define BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <fuchsia/sys/cpp/fidl.h>
10 #include <lib/fidl/cpp/binding_set.h>
11 #include <lib/fidl/cpp/interface_handle.h>
12 #include <lib/zx/channel.h>
15 #include "base/base_export.h"
16 #include "base/callback.h"
17 #include "base/fuchsia/service_directory_client.h"
18 #include "base/macros.h"
23 // Implementation of the legacy sys.ServiceProvider interface which delegates
24 // requests to an underlying fuchsia.io.Directory of services.
25 // TODO(https://crbug.com/920920): Remove this when ServiceProvider is gone.
26 class BASE_EXPORT ServiceProviderImpl : public ::fuchsia::sys::ServiceProvider {
28 explicit ServiceProviderImpl(
29 fidl::InterfaceHandle<::fuchsia::io::Directory> service_directory);
30 ~ServiceProviderImpl() override;
32 // Binds a |request| from a new client to be serviced by this ServiceProvider.
34 fidl::InterfaceRequest<::fuchsia::sys::ServiceProvider> request);
36 // Sets a Closure to be invoked when the last client disconnects.
37 void SetOnLastClientDisconnectedClosure(
38 base::OnceClosure on_last_client_disconnected);
40 // Returns true if one or more clients are connected.
41 bool has_clients() const { return bindings_.size() != 0; }
44 // fuchsia::sys::ServiceProvider implementation.
45 void ConnectToService(std::string service_name,
46 zx::channel client_handle) override;
48 void OnBindingSetEmpty();
50 const ServiceDirectoryClient directory_;
51 fidl::BindingSet<::fuchsia::sys::ServiceProvider> bindings_;
52 base::OnceClosure on_last_client_disconnected_;
54 DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
57 } // namespace fuchsia
60 #endif // BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_