Skip to content

Commit

Permalink
[fuchsia] Allow published service name to be set explicitly.
Browse files Browse the repository at this point in the history
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: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819405}
  • Loading branch information
Hai Bi authored and Commit Bot committed Oct 21, 2020
1 parent ed6b0c6 commit e6243cc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions base/fuchsia/scoped_service_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
13 changes: 13 additions & 0 deletions base/fuchsia/scoped_service_binding_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions base/fuchsia/scoped_service_publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit e6243cc

Please sign in to comment.