Commit e6243cc1 authored by Hai Bi's avatar Hai Bi Committed by Commit Bot

[fuchsia] Allow published service name to be set explicitly.

The ScopedService* publishing templates infer the service name
from the supplied Interface class. It is sometimes desirable to
publish a service under a manually-specified name, e.g. if the
service isn't actually defined as [Discoverable], or it's needed
to publish it under a different name.

The templates now accept an explicit |name| parameter which
defaults to the name of the Interface, if available.

Bug: b/171256428
Test: locally tested and pass unit tests
Change-Id: If206fea8a7555baea5806b8526be2affc1e0dc74
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2486620
Commit-Queue: Hai Bi <bihai@google.com>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819405}
parent ed6b0c6b
......@@ -73,11 +73,13 @@ class BASE_EXPORT ScopedSingleClientServiceBinding {
public:
// |outgoing_directory| and |impl| must outlive the binding.
ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory,
Interface* impl)
Interface* impl,
std::string name = Interface::Name_)
: binding_(impl) {
publisher_.emplace(
outgoing_directory,
fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient));
fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient),
std::move(name));
binding_.set_error_handler(fit::bind_member(
this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
}
......
......@@ -21,6 +21,19 @@ TEST_F(ScopedServiceBindingTest, ConnectTwice) {
VerifyTestInterface(&stub2, ZX_OK);
}
// Verifies that ScopedSingleClientServiceBinding allows a different name.
TEST_F(ScopedServiceBindingTest, SingleClientConnectNewName) {
const std::string interface_name = "fuchsia.TestInterface2";
auto service_binding_new_name_ = std::make_unique<
ScopedSingleClientServiceBinding<testfidl::TestInterface>>(
outgoing_directory_.get(), &test_service_, interface_name);
testfidl::TestInterfacePtr stub;
public_service_directory_->Connect(interface_name,
stub.NewRequest().TakeChannel());
VerifyTestInterface(&stub, ZX_OK);
}
// Verify that if we connect twice to a prefer-new bound service, the existing
// connection gets closed.
TEST_F(ScopedServiceBindingTest, SingleClientPreferNew) {
......
......@@ -24,24 +24,26 @@ class BASE_EXPORT ScopedServicePublisher {
// Publishes a public service in the specified |outgoing_directory|.
// |outgoing_directory| and |handler| must outlive the binding.
ScopedServicePublisher(sys::OutgoingDirectory* outgoing_directory,
fidl::InterfaceRequestHandler<Interface> handler)
fidl::InterfaceRequestHandler<Interface> handler,
std::string name = Interface::Name_)
: ScopedServicePublisher(outgoing_directory->GetOrCreateDirectory("svc"),
std::move(handler)) {}
std::move(handler), std::move(name)) {}
// Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and
// |handler| must outlive the binding.
ScopedServicePublisher(vfs::PseudoDir* pseudo_dir,
fidl::InterfaceRequestHandler<Interface> handler)
: pseudo_dir_(pseudo_dir) {
pseudo_dir_->AddEntry(Interface::Name_,
fidl::InterfaceRequestHandler<Interface> handler,
std::string name = Interface::Name_)
: pseudo_dir_(pseudo_dir), name_(std::move(name)) {
pseudo_dir_->AddEntry(name_,
std::make_unique<vfs::Service>(std::move(handler)));
}
~ScopedServicePublisher() { pseudo_dir_->RemoveEntry(Interface::Name_); }
~ScopedServicePublisher() { pseudo_dir_->RemoveEntry(name_); }
private:
vfs::PseudoDir* const pseudo_dir_ = nullptr;
std::string name_;
DISALLOW_COPY_AND_ASSIGN(ScopedServicePublisher);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment