blob: 3ce78f4354b5e65897b01ec52163239fb68a0027 [file] [log] [blame]
// 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.
#include <fuchsia/io/cpp/fidl.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <memory>
#include "base/base_export.h"
#include "base/macros.h"
namespace fidl {
template <typename Interface>
class InterfacePtr;
template <typename Interface>
class SynchronousInterfacePtr;
} // namespace fidl
namespace base {
namespace fuchsia {
// Helper for connecting to services from a supplied
class BASE_EXPORT ServiceDirectoryClient {
// Wraps the supplied |directory| to access the services it contains.
explicit ServiceDirectoryClient(
fidl::InterfaceHandle<::fuchsia::io::Directory> directory);
// Returns the default ServiceDirectoryClient for the current process.
// This connects to the "/svc" path in the namespace that was supplied to the
// current process when it was launched.
static const ServiceDirectoryClient* ForCurrentProcess();
// Connects to the service satisfying the specified |request|.
template <typename Interface>
zx_status_t ConnectToService(
fidl::InterfaceRequest<Interface> request) const {
return ConnectToServiceUnsafe(Interface::Name_, request.TakeChannel());
// Convenience functions returning a [Synchronous]InterfacePtr directly.
// Returns an un-bound pointer if the connection attempt returns an error.
template <typename Interface>
fidl::InterfacePtr<Interface> ConnectToService() const {
fidl::InterfacePtr<Interface> result;
if (ConnectToService(result.NewRequest()) != ZX_OK)
return result;
template <typename Interface>
fidl::SynchronousInterfacePtr<Interface> ConnectToServiceSync() const {
fidl::SynchronousInterfacePtr<Interface> result;
if (ConnectToService(result.NewRequest()) != ZX_OK)
return result;
// Connects the |request| channel to the service specified by |name|.
// This is used only when proxying requests for interfaces not known at
// compile-time. Use the type-safe APIs above whenever possible.
zx_status_t ConnectToServiceUnsafe(const char* name,
zx::channel request) const;
// Creates a ServiceDirectoryClient connected to the process' "/svc"
// directory, or a dummy instance if the "/svc" directory is not available.
static std::unique_ptr<ServiceDirectoryClient> CreateForProcess();
// Returns the container holding the ForCurrentProcess() instance. The
// default ServiceDirectoryClient is created the first time this function is
// called.
static std::unique_ptr<ServiceDirectoryClient>* ProcessInstance();
const fidl::InterfaceHandle<::fuchsia::io::Directory> directory_;
} // namespace fuchsia
} // namespace base