File: scoped_service_binding.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (209 lines) | stat: -rw-r--r-- 7,578 bytes parent folder | download | duplicates (9)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
#define BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_

// TODO(crbug.com/42050587): Remove this include once the explicit
// async_get_default_dispatcher() is no longer needed.
#include <lib/async/default.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/fidl/cpp/interface_request.h>
#include <lib/fidl/cpp/wire/connect_service.h>
#include <lib/zx/channel.h>

#include <optional>
#include <string_view>
#include <utility>

#include "base/base_export.h"
#include "base/fuchsia/scoped_service_publisher.h"
#include "base/functional/callback.h"

namespace sys {
class OutgoingDirectory;
}  // namespace sys

namespace vfs {
class PseudoDir;
}  // namespace vfs

namespace base {

template <typename Interface>
class BASE_EXPORT ScopedServiceBinding {
 public:
  // Publishes a public service in the specified |outgoing_directory|.
  // |outgoing_directory| and |impl| must outlive the binding. The service is
  // unpublished on destruction.
  ScopedServiceBinding(sys::OutgoingDirectory* outgoing_directory,
                       Interface* impl,
                       std::string_view name = Interface::Name_)
      : publisher_(outgoing_directory, bindings_.GetHandler(impl), name) {}

  // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl|
  // must outlive the binding. The service is unpublished on destruction.
  ScopedServiceBinding(vfs::PseudoDir* pseudo_dir,
                       Interface* impl,
                       std::string_view name = Interface::Name_)
      : publisher_(pseudo_dir, bindings_.GetHandler(impl), name) {}

  ScopedServiceBinding(const ScopedServiceBinding&) = delete;
  ScopedServiceBinding& operator=(const ScopedServiceBinding&) = delete;

  ~ScopedServiceBinding() = default;

  // |on_last_client_callback| will be called every time the number of connected
  // clients drops to 0.
  void SetOnLastClientCallback(base::RepeatingClosure on_last_client_callback) {
    bindings_.set_empty_set_handler(
        [callback = std::move(on_last_client_callback)] { callback.Run(); });
  }

  bool has_clients() const { return bindings_.size() != 0; }

 private:
  fidl::BindingSet<Interface> bindings_;
  ScopedServicePublisher<Interface> publisher_;
};

template <typename Protocol>
class BASE_EXPORT ScopedNaturalServiceBinding {
 public:
  // Publishes a public service in the specified |outgoing_directory|.
  // |outgoing_directory| and |impl| must outlive the binding. The service is
  // unpublished on destruction.
  ScopedNaturalServiceBinding(
      sys::OutgoingDirectory* outgoing_directory,
      fidl::Server<Protocol>* impl,
      std::string_view name = fidl::DiscoverableProtocolName<Protocol>)
      : publisher_(
            outgoing_directory,
            bindings_.CreateHandler(
                impl,
                // TODO(crbug.com/42050587): Remove this param once there's an
                // overload of `CreateHandler` that doesn't require it.
                async_get_default_dispatcher(),
                [](fidl::UnbindInfo info) {}),
            name) {}

  // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl|
  // must outlive the binding. The service is unpublished on destruction.
  ScopedNaturalServiceBinding(
      vfs::PseudoDir* pseudo_dir,
      fidl::Server<Protocol>* impl,
      std::string_view name = fidl::DiscoverableProtocolName<Protocol>)
      : publisher_(
            pseudo_dir,
            bindings_.CreateHandler(
                impl,
                // TODO(crbug.com/42050587): Remove this param once there's an
                // overload of `CreateHandler` that doesn't require it.
                async_get_default_dispatcher(),
                [](fidl::UnbindInfo info) {}),
            name) {}

  ScopedNaturalServiceBinding(const ScopedNaturalServiceBinding&) = delete;
  ScopedNaturalServiceBinding& operator=(const ScopedNaturalServiceBinding&) =
      delete;

  ~ScopedNaturalServiceBinding() = default;

  // Registers `on_last_client_callback` to be called every time the number of
  // connected clients drops to 0.
  void SetOnLastClientCallback(base::RepeatingClosure on_last_client_callback) {
    bindings_.set_empty_set_handler(
        [callback = std::move(on_last_client_callback)] { callback.Run(); });
  }

  bool has_clients() const { return bindings_.size() != 0; }

 private:
  fidl::ServerBindingGroup<Protocol> bindings_;
  ScopedNaturalServicePublisher<Protocol> publisher_;
};

// Scoped service binding which allows only a single client to be connected
// at any time. By default a new connection will disconnect an existing client.
enum class ScopedServiceBindingPolicy {
  kPreferNew,
  kPreferExisting,
  kConnectOnce
};

template <typename Interface,
          ScopedServiceBindingPolicy Policy =
              ScopedServiceBindingPolicy::kPreferNew>
class BASE_EXPORT ScopedSingleClientServiceBinding {
 public:
  // |outgoing_directory| and |impl| must outlive the binding.
  ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory,
                                   Interface* impl,
                                   std::string_view name = Interface::Name_)
      : binding_(impl) {
    publisher_.emplace(
        outgoing_directory,
        fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient),
        name);
    binding_.set_error_handler(fit::bind_member(
        this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
  }

  ScopedSingleClientServiceBinding(vfs::PseudoDir* publish_to,
                                   Interface* impl,
                                   std::string_view name = Interface::Name_)
      : binding_(impl) {
    publisher_.emplace(
        publish_to,
        fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient),
        name);
    binding_.set_error_handler(fit::bind_member(
        this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
  }

  ScopedSingleClientServiceBinding(const ScopedSingleClientServiceBinding&) =
      delete;
  ScopedSingleClientServiceBinding& operator=(
      const ScopedSingleClientServiceBinding&) = delete;

  ~ScopedSingleClientServiceBinding() = default;

  typename Interface::EventSender_& events() { return binding_.events(); }

  // |on_last_client_callback| will be called the first time a client
  // disconnects. It is still  possible for a client to connect after that point
  // if Policy is kPreferNew of kPreferExisting.
  void SetOnLastClientCallback(base::OnceClosure on_last_client_callback) {
    on_last_client_callback_ = std::move(on_last_client_callback);
  }

  bool has_clients() const { return binding_.is_bound(); }

 private:
  void BindClient(fidl::InterfaceRequest<Interface> request) {
    if (Policy == ScopedServiceBindingPolicy::kPreferExisting &&
        binding_.is_bound()) {
      return;
    }
    binding_.Bind(std::move(request));
    if (Policy == ScopedServiceBindingPolicy::kConnectOnce) {
      publisher_.reset();
    }
  }

  void OnBindingEmpty(zx_status_t status) {
    if (on_last_client_callback_) {
      std::move(on_last_client_callback_).Run();
    }
  }

  fidl::Binding<Interface> binding_;
  std::optional<ScopedServicePublisher<Interface>> publisher_;
  base::OnceClosure on_last_client_callback_;
};

}  // namespace base

#endif  // BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_