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
|
// Copyright 2018 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.
#ifndef BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
#define BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/zx/channel.h>
#include "base/base_export.h"
#include "base/callback.h"
namespace sys {
class OutgoingDirectory;
} // namespace sys
namespace vfs {
class PseudoDir;
} // namespace vfs
namespace base {
namespace fuchsia {
namespace internal {
class BASE_EXPORT ScopedServiceBindingBase {
public:
explicit ScopedServiceBindingBase(sys::OutgoingDirectory* outgoing_directory);
explicit ScopedServiceBindingBase(vfs::PseudoDir* pseudo_dir);
~ScopedServiceBindingBase();
protected:
// Same type as vfs::Service::Connector, so the value can be passed directly
// to vfs::Service.
using Connector =
fit::function<void(zx::channel channel, async_dispatcher_t* dispatcher)>;
void RegisterService(const char* service_name, Connector connector);
void UnregisterService(const char* service_name);
private:
vfs::PseudoDir* const pseudo_dir_ = nullptr;
};
} // namespace internal
template <typename Interface>
class ScopedServiceBinding : public internal::ScopedServiceBindingBase {
public:
// Published a public service in the specified |outgoing_directory|.
// |outgoing_directory| and |impl| must outlive the binding.
ScopedServiceBinding(sys::OutgoingDirectory* outgoing_directory,
Interface* impl)
: ScopedServiceBindingBase(outgoing_directory), impl_(impl) {
RegisterService(Interface::Name_,
fit::bind_member(this, &ScopedServiceBinding::BindClient));
}
// Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl|
// must outlive the binding.
ScopedServiceBinding(vfs::PseudoDir* pseudo_dir, Interface* impl)
: ScopedServiceBindingBase(pseudo_dir), impl_(impl) {
RegisterService(Interface::Name_,
fit::bind_member(this, &ScopedServiceBinding::BindClient));
}
~ScopedServiceBinding() { UnregisterService(Interface::Name_); }
void SetOnLastClientCallback(base::OnceClosure on_last_client_callback) {
on_last_client_callback_ = std::move(on_last_client_callback);
bindings_.set_empty_set_handler(
fit::bind_member(this, &ScopedServiceBinding::OnBindingSetEmpty));
}
bool has_clients() const { return bindings_.size() != 0; }
private:
void BindClient(zx::channel channel, async_dispatcher_t* dispatcher) {
bindings_.AddBinding(impl_,
fidl::InterfaceRequest<Interface>(std::move(channel)),
dispatcher);
}
void OnBindingSetEmpty() {
bindings_.set_empty_set_handler(nullptr);
std::move(on_last_client_callback_).Run();
}
sys::OutgoingDirectory* const directory_ = nullptr;
vfs::PseudoDir* const pseudo_dir_ = nullptr;
Interface* const impl_;
fidl::BindingSet<Interface> bindings_;
base::OnceClosure on_last_client_callback_;
DISALLOW_COPY_AND_ASSIGN(ScopedServiceBinding);
};
// 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 };
template <typename Interface,
ScopedServiceBindingPolicy Policy =
ScopedServiceBindingPolicy::kPreferNew>
class ScopedSingleClientServiceBinding
: public internal::ScopedServiceBindingBase {
public:
// |outgoing_directory| and |impl| must outlive the binding.
ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory,
Interface* impl)
: ScopedServiceBindingBase(outgoing_directory), binding_(impl) {
RegisterService(
Interface::Name_,
fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient));
}
~ScopedSingleClientServiceBinding() { UnregisterService(Interface::Name_); }
typename Interface::EventSender_& events() { return binding_.events(); }
void SetOnLastClientCallback(base::OnceClosure on_last_client_callback) {
on_last_client_callback_ = std::move(on_last_client_callback);
binding_.set_error_handler(fit::bind_member(
this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
}
bool has_clients() const { return binding_.is_bound(); }
private:
void BindClient(zx::channel channel, async_dispatcher_t* dispatcher) {
if (Policy == ScopedServiceBindingPolicy::kPreferExisting &&
binding_.is_bound()) {
return;
}
binding_.Bind(fidl::InterfaceRequest<Interface>(std::move(channel)),
dispatcher);
}
void OnBindingEmpty(zx_status_t status) {
binding_.set_error_handler(nullptr);
std::move(on_last_client_callback_).Run();
}
fidl::Binding<Interface> binding_;
base::OnceClosure on_last_client_callback_;
DISALLOW_COPY_AND_ASSIGN(ScopedSingleClientServiceBinding);
};
} // namespace fuchsia
} // namespace base
#endif // BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
|