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

[fuchsia] Allow name to be specified to ScopedServiceBinding.

ScopedServicePublisher and ScopedSingleClientServiceBinding were
updated to allow the service name to be specified explicitly, but
ScopedServiceBinding was missed.  Add the missing functionality
to the ScopedServiceBinding template.

Bug: b/171256428
Test: locally tested and pass unit tests
Change-Id: I0623256895da020eaf3a423e28f72936cf843994
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2499625Reviewed-by: default avatarWez <wez@chromium.org>
Commit-Queue: Hai Bi <bihai@google.com>
Cr-Commit-Position: refs/heads/master@{#821381}
parent a1a1f50b
...@@ -32,13 +32,14 @@ class BASE_EXPORT ScopedServiceBinding { ...@@ -32,13 +32,14 @@ class BASE_EXPORT ScopedServiceBinding {
// Published a public service in the specified |outgoing_directory|. // Published a public service in the specified |outgoing_directory|.
// |outgoing_directory| and |impl| must outlive the binding. // |outgoing_directory| and |impl| must outlive the binding.
ScopedServiceBinding(sys::OutgoingDirectory* outgoing_directory, ScopedServiceBinding(sys::OutgoingDirectory* outgoing_directory,
Interface* impl) Interface* impl, base::StringPiece name = Interface::Name_)
: publisher_(outgoing_directory, bindings_.GetHandler(impl)) {} : publisher_(outgoing_directory, bindings_.GetHandler(impl), name) {}
// Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl| // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and |impl|
// must outlive the binding. // must outlive the binding.
ScopedServiceBinding(vfs::PseudoDir* pseudo_dir, Interface* impl) ScopedServiceBinding(vfs::PseudoDir* pseudo_dir, Interface* impl,
: publisher_(pseudo_dir, bindings_.GetHandler(impl)) {} base::StringPiece name = Interface::Name_)
: publisher_(pseudo_dir, bindings_.GetHandler(impl), name) {}
~ScopedServiceBinding() = default; ~ScopedServiceBinding() = default;
...@@ -74,12 +75,12 @@ class BASE_EXPORT ScopedSingleClientServiceBinding { ...@@ -74,12 +75,12 @@ class BASE_EXPORT ScopedSingleClientServiceBinding {
// |outgoing_directory| and |impl| must outlive the binding. // |outgoing_directory| and |impl| must outlive the binding.
ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory, ScopedSingleClientServiceBinding(sys::OutgoingDirectory* outgoing_directory,
Interface* impl, Interface* impl,
std::string name = Interface::Name_) base::StringPiece name = Interface::Name_)
: binding_(impl) { : binding_(impl) {
publisher_.emplace( publisher_.emplace(
outgoing_directory, outgoing_directory,
fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient), fit::bind_member(this, &ScopedSingleClientServiceBinding::BindClient),
std::move(name)); name);
binding_.set_error_handler(fit::bind_member( binding_.set_error_handler(fit::bind_member(
this, &ScopedSingleClientServiceBinding::OnBindingEmpty)); this, &ScopedSingleClientServiceBinding::OnBindingEmpty));
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "base/fuchsia/service_directory_test_base.h" #include "base/fuchsia/service_directory_test_base.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string_piece.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace base { namespace base {
...@@ -21,15 +22,31 @@ TEST_F(ScopedServiceBindingTest, ConnectTwice) { ...@@ -21,15 +22,31 @@ TEST_F(ScopedServiceBindingTest, ConnectTwice) {
VerifyTestInterface(&stub2, ZX_OK); VerifyTestInterface(&stub2, ZX_OK);
} }
// Verifies that ScopedServiceBinding allows connection more than once.
TEST_F(ScopedServiceBindingTest, ConnectTwiceNewName) {
const char kInterfaceName[] = "fuchsia.TestInterface2";
ScopedServiceBinding<testfidl::TestInterface> new_service_binding(
outgoing_directory_.get(), &test_service_, kInterfaceName);
testfidl::TestInterfacePtr stub, stub2;
public_service_directory_->Connect(
kInterfaceName, stub.NewRequest().TakeChannel());
public_service_directory_->Connect(
kInterfaceName, stub2.NewRequest().TakeChannel());
VerifyTestInterface(&stub, ZX_OK);
VerifyTestInterface(&stub2, ZX_OK);
}
// Verifies that ScopedSingleClientServiceBinding allows a different name. // Verifies that ScopedSingleClientServiceBinding allows a different name.
TEST_F(ScopedServiceBindingTest, SingleClientConnectNewName) { TEST_F(ScopedServiceBindingTest, SingleClientConnectNewName) {
const std::string interface_name = "fuchsia.TestInterface2"; const char kInterfaceName[] = "fuchsia.TestInterface2";
auto service_binding_new_name_ = std::make_unique< auto service_binding_new_name_ = std::make_unique<
ScopedSingleClientServiceBinding<testfidl::TestInterface>>( ScopedSingleClientServiceBinding<testfidl::TestInterface>>(
outgoing_directory_.get(), &test_service_, interface_name); outgoing_directory_.get(), &test_service_, kInterfaceName);
testfidl::TestInterfacePtr stub; testfidl::TestInterfacePtr stub;
public_service_directory_->Connect(interface_name, public_service_directory_->Connect(kInterfaceName,
stub.NewRequest().TakeChannel()); stub.NewRequest().TakeChannel());
VerifyTestInterface(&stub, ZX_OK); VerifyTestInterface(&stub, ZX_OK);
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/base_export.h" #include "base/base_export.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_piece.h"
namespace base { namespace base {
namespace fuchsia { namespace fuchsia {
...@@ -25,16 +26,16 @@ class BASE_EXPORT ScopedServicePublisher { ...@@ -25,16 +26,16 @@ class BASE_EXPORT ScopedServicePublisher {
// |outgoing_directory| and |handler| must outlive the binding. // |outgoing_directory| and |handler| must outlive the binding.
ScopedServicePublisher(sys::OutgoingDirectory* outgoing_directory, ScopedServicePublisher(sys::OutgoingDirectory* outgoing_directory,
fidl::InterfaceRequestHandler<Interface> handler, fidl::InterfaceRequestHandler<Interface> handler,
std::string name = Interface::Name_) base::StringPiece name = Interface::Name_)
: ScopedServicePublisher(outgoing_directory->GetOrCreateDirectory("svc"), : ScopedServicePublisher(outgoing_directory->GetOrCreateDirectory("svc"),
std::move(handler), std::move(name)) {} std::move(handler), name) {}
// Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and // Publishes a service in the specified |pseudo_dir|. |pseudo_dir| and
// |handler| must outlive the binding. // |handler| must outlive the binding.
ScopedServicePublisher(vfs::PseudoDir* pseudo_dir, ScopedServicePublisher(vfs::PseudoDir* pseudo_dir,
fidl::InterfaceRequestHandler<Interface> handler, fidl::InterfaceRequestHandler<Interface> handler,
std::string name = Interface::Name_) base::StringPiece name = Interface::Name_)
: pseudo_dir_(pseudo_dir), name_(std::move(name)) { : pseudo_dir_(pseudo_dir), name_(name.as_string()) {
pseudo_dir_->AddEntry(name_, pseudo_dir_->AddEntry(name_,
std::make_unique<vfs::Service>(std::move(handler))); std::make_unique<vfs::Service>(std::move(handler)));
} }
......
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