1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_
6 #define BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <lib/fidl/cpp/interface_handle.h>
12 #include "base/base_export.h"
13 #include "base/macros.h"
17 template <typename Interface>
20 template <typename Interface>
21 class SynchronousInterfacePtr;
28 class ScopedServiceDirectoryClientForCurrentProcessForTest;
30 // Helper for connecting to services from a supplied fuchsia.io.Directory.
31 class BASE_EXPORT ServiceDirectoryClient {
33 // Wraps the supplied |directory| to access the services it contains.
34 explicit ServiceDirectoryClient(
35 fidl::InterfaceHandle<::fuchsia::io::Directory> directory);
36 ~ServiceDirectoryClient();
38 // Returns the default ServiceDirectoryClient for the current process.
39 // This connects to the "/svc" path in the namespace that was supplied to the
40 // current process when it was launched.
41 static const ServiceDirectoryClient* ForCurrentProcess();
43 // Connects to the service satisfying the specified |request|.
44 template <typename Interface>
45 zx_status_t ConnectToService(
46 fidl::InterfaceRequest<Interface> request) const {
47 return ConnectToServiceUnsafe(Interface::Name_, request.TakeChannel());
50 // Convenience functions returning a [Synchronous]InterfacePtr directly.
51 // Returns an un-bound pointer if the connection attempt returns an error.
52 template <typename Interface>
53 fidl::InterfacePtr<Interface> ConnectToService() const {
54 fidl::InterfacePtr<Interface> result;
55 if (ConnectToService(result.NewRequest()) != ZX_OK)
59 template <typename Interface>
60 fidl::SynchronousInterfacePtr<Interface> ConnectToServiceSync() const {
61 fidl::SynchronousInterfacePtr<Interface> result;
62 if (ConnectToService(result.NewRequest()) != ZX_OK)
67 // Connects the |request| channel to the service specified by |name|.
68 // This is used only when proxying requests for interfaces not known at
69 // compile-time. Use the type-safe APIs above whenever possible.
70 zx_status_t ConnectToServiceUnsafe(const char* name,
71 zx::channel request) const;
74 friend class ScopedServiceDirectoryClientForCurrentProcessForTest;
75 ServiceDirectoryClient();
77 // Creates a ServiceDirectoryClient connected to the process' "/svc"
78 // directory, or a dummy instance if the "/svc" directory is not available.
79 static std::unique_ptr<ServiceDirectoryClient> CreateForProcess();
81 // Returns the container holding the ForCurrentProcess() instance. The
82 // default ServiceDirectoryClient is created the first time this function is
84 static std::unique_ptr<ServiceDirectoryClient>* ProcessInstance();
86 const fidl::InterfaceHandle<::fuchsia::io::Directory> directory_;
88 DISALLOW_COPY_AND_ASSIGN(ServiceDirectoryClient);
91 // Replaces the current process' ServiceDirectoryClient with the supplied
92 // |directory|, and restores it when going out-of-scope.
93 class BASE_EXPORT ScopedServiceDirectoryClientForCurrentProcessForTest {
95 explicit ScopedServiceDirectoryClientForCurrentProcessForTest(
96 fidl::InterfaceHandle<::fuchsia::io::Directory> directory);
97 ~ScopedServiceDirectoryClientForCurrentProcessForTest();
100 ServiceDirectoryClient* client_;
101 std::unique_ptr<ServiceDirectoryClient> old_client_;
103 DISALLOW_COPY_AND_ASSIGN(
104 ScopedServiceDirectoryClientForCurrentProcessForTest);
107 } // namespace fuchsia
110 #endif // BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_