Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / service_provider_impl.h
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.
4
5 #ifndef BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_
6 #define BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_
7
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>
13 #include <string>
14
15 #include "base/base_export.h"
16 #include "base/callback.h"
17 #include "base/fuchsia/service_directory_client.h"
18 #include "base/macros.h"
19
20 namespace base {
21 namespace fuchsia {
22
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 {
27  public:
28   explicit ServiceProviderImpl(
29       fidl::InterfaceHandle<::fuchsia::io::Directory> service_directory);
30   ~ServiceProviderImpl() override;
31
32   // Binds a |request| from a new client to be serviced by this ServiceProvider.
33   void AddBinding(
34       fidl::InterfaceRequest<::fuchsia::sys::ServiceProvider> request);
35
36   // Sets a Closure to be invoked when the last client disconnects.
37   void SetOnLastClientDisconnectedClosure(
38       base::OnceClosure on_last_client_disconnected);
39
40   // Returns true if one or more clients are connected.
41   bool has_clients() const { return bindings_.size() != 0; }
42
43  private:
44   // fuchsia::sys::ServiceProvider implementation.
45   void ConnectToService(std::string service_name,
46                         zx::channel client_handle) override;
47
48   void OnBindingSetEmpty();
49
50   const ServiceDirectoryClient directory_;
51   fidl::BindingSet<::fuchsia::sys::ServiceProvider> bindings_;
52   base::OnceClosure on_last_client_disconnected_;
53
54   DISALLOW_COPY_AND_ASSIGN(ServiceProviderImpl);
55 };
56
57 }  // namespace fuchsia
58 }  // namespace base
59
60 #endif  // BASE_FUCHSIA_SERVICE_PROVIDER_IMPL_H_