Commit 089bc8ad authored by Kevin Marshall's avatar Kevin Marshall Committed by Commit Bot

Fuchsia: Migrate ServicesDirectory from ScopedZxHandle to zx::channel.

Part of a larger effort to use Fuchsia native zx::channel objects
for smoother integration with FIDL types and API calls.


Bug: 852541
Change-Id: I095e17b98a334964e72cbb97dc7ea4dbded67ec8
Reviewed-on: https://chromium-review.googlesource.com/1101413
Commit-Queue: Kevin Marshall <kmarshall@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567405}
parent 4066ee9d
...@@ -29,9 +29,9 @@ class ScopedServiceBinding { ...@@ -29,9 +29,9 @@ class ScopedServiceBinding {
~ScopedServiceBinding() { directory_->RemoveService(Interface::Name_); } ~ScopedServiceBinding() { directory_->RemoveService(Interface::Name_); }
private: private:
void BindClient(ScopedZxHandle channel) { void BindClient(zx::channel channel) {
binding_.Bind(typename fidl::InterfaceRequest<Interface>( binding_.Bind(
zx::channel(channel.release()))); typename fidl::InterfaceRequest<Interface>(std::move(channel)));
} }
ServicesDirectory* directory_; ServicesDirectory* directory_;
...@@ -43,4 +43,4 @@ class ScopedServiceBinding { ...@@ -43,4 +43,4 @@ class ScopedServiceBinding {
} // namespace fuchsia } // namespace fuchsia
} // namespace base } // namespace base
#endif // BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_ #endif // BASE_FUCHSIA_SCOPED_SERVICE_BINDING_H_
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <lib/async/default.h> #include <lib/async/default.h>
#include <lib/svc/dir.h> #include <lib/svc/dir.h>
#include <lib/zx/channel.h>
#include <zircon/process.h> #include <zircon/process.h>
#include <zircon/processargs.h> #include <zircon/processargs.h>
...@@ -16,7 +17,7 @@ ...@@ -16,7 +17,7 @@
namespace base { namespace base {
namespace fuchsia { namespace fuchsia {
ServicesDirectory::ServicesDirectory(ScopedZxHandle directory_request) { ServicesDirectory::ServicesDirectory(zx::channel directory_request) {
zx_status_t status = svc_dir_create(async_get_default(), zx_status_t status = svc_dir_create(async_get_default(),
directory_request.release(), &svc_dir_); directory_request.release(), &svc_dir_);
ZX_CHECK(status == ZX_OK, status); ZX_CHECK(status == ZX_OK, status);
...@@ -33,7 +34,7 @@ ServicesDirectory::~ServicesDirectory() { ...@@ -33,7 +34,7 @@ ServicesDirectory::~ServicesDirectory() {
// static // static
ServicesDirectory* ServicesDirectory::GetDefault() { ServicesDirectory* ServicesDirectory::GetDefault() {
static base::NoDestructor<ServicesDirectory> directory( static base::NoDestructor<ServicesDirectory> directory(
ScopedZxHandle(zx_get_startup_handle(PA_DIRECTORY_REQUEST))); zx::channel(zx_get_startup_handle(PA_DIRECTORY_REQUEST)));
return directory.get(); return directory.get();
} }
...@@ -77,7 +78,7 @@ void ServicesDirectory::HandleConnectRequest(void* context, ...@@ -77,7 +78,7 @@ void ServicesDirectory::HandleConnectRequest(void* context,
// services. // services.
DCHECK(it != directory->services_.end()); DCHECK(it != directory->services_.end());
it->second.Run(ScopedZxHandle(service_request)); it->second.Run(zx::channel(service_request));
} }
} // namespace fuchsia } // namespace fuchsia
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
#ifndef BASE_FUCHSIA_SERVICES_DIRECTORY_H_ #ifndef BASE_FUCHSIA_SERVICES_DIRECTORY_H_
#define BASE_FUCHSIA_SERVICES_DIRECTORY_H_ #define BASE_FUCHSIA_SERVICES_DIRECTORY_H_
#include <lib/zx/channel.h>
#include "base/base_export.h" #include "base/base_export.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/flat_map.h" #include "base/containers/flat_map.h"
#include "base/fuchsia/scoped_zx_handle.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
...@@ -32,11 +33,11 @@ class BASE_EXPORT ServicesDirectory { ...@@ -32,11 +33,11 @@ class BASE_EXPORT ServicesDirectory {
public: public:
// Callback called to connect incoming requests. // Callback called to connect incoming requests.
using ConnectServiceCallback = using ConnectServiceCallback =
base::RepeatingCallback<void(ScopedZxHandle channel)>; base::RepeatingCallback<void(zx::channel channel)>;
// Creates services directory that will be served over the // Creates services directory that will be served over the
// |directory_channel|. // |directory_channel|.
explicit ServicesDirectory(ScopedZxHandle directory_channel); explicit ServicesDirectory(zx::channel directory_channel);
~ServicesDirectory(); ~ServicesDirectory();
...@@ -64,4 +65,4 @@ class BASE_EXPORT ServicesDirectory { ...@@ -64,4 +65,4 @@ class BASE_EXPORT ServicesDirectory {
} // namespace fuchsia } // namespace fuchsia
} // namespace base } // namespace base
#endif // BASE_FUCHSIA_SERVICES_DIRECTORY_H_ #endif // BASE_FUCHSIA_SERVICES_DIRECTORY_H_
\ No newline at end of file
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/fuchsia/services_directory.h" #include "base/fuchsia/services_directory.h"
#include <lib/fdio/util.h> #include <lib/fdio/util.h>
#include <lib/zx/channel.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/fuchsia/component_context.h" #include "base/fuchsia/component_context.h"
...@@ -28,30 +29,30 @@ class TestInterfaceImpl : public test_fidl::TestInterface { ...@@ -28,30 +29,30 @@ class TestInterfaceImpl : public test_fidl::TestInterface {
TEST(ServicesDirectoryTest, Connect) { TEST(ServicesDirectoryTest, Connect) {
MessageLoopForIO message_loop_; MessageLoopForIO message_loop_;
ScopedZxHandle dir_service_handle; zx::channel dir_service_channel;
ScopedZxHandle dir_client_handle; zx::channel dir_client_channel;
ASSERT_EQ(zx_channel_create(0, dir_service_handle.receive(), ASSERT_EQ(zx::channel::create(0, &dir_service_channel, &dir_client_channel),
dir_client_handle.receive()),
ZX_OK); ZX_OK);
// Mount service dir and publish the service. // Mount service dir and publish the service.
ServicesDirectory service_dir(std::move(dir_service_handle)); ServicesDirectory service_dir(std::move(dir_service_channel));
TestInterfaceImpl test_service; TestInterfaceImpl test_service;
ScopedServiceBinding<test_fidl::TestInterface> service_binding(&service_dir, ScopedServiceBinding<test_fidl::TestInterface> service_binding(&service_dir,
&test_service); &test_service);
// Open public directory from the service directory. // Open public directory from the service directory.
ScopedZxHandle public_dir_service_handle; zx::channel public_dir_service_channel;
ScopedZxHandle public_dir_client_handle; zx::channel public_dir_client_channel;
ASSERT_EQ(zx_channel_create(0, public_dir_service_handle.receive(), ASSERT_EQ(zx::channel::create(0, &public_dir_service_channel,
public_dir_client_handle.receive()), &public_dir_client_channel),
ZX_OK); ZX_OK);
ASSERT_EQ(fdio_open_at(dir_client_handle.get(), "public", 0, ASSERT_EQ(fdio_open_at(dir_client_channel.get(), "public", 0,
public_dir_service_handle.release()), public_dir_service_channel.release()),
ZX_OK); ZX_OK);
// Create ComponentContext and connect to the test service. // Create ComponentContext and connect to the test service.
ComponentContext client_context(std::move(public_dir_client_handle)); ComponentContext client_context(
base::ScopedZxHandle(public_dir_client_channel.release()));
auto stub = client_context.ConnectToService<test_fidl::TestInterface>(); auto stub = client_context.ConnectToService<test_fidl::TestInterface>();
// Call the service and wait for response. // Call the service and wait for response.
......
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