Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / base / fuchsia / service_directory_client.h
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.
4
5 #ifndef BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_
6 #define BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_
7
8 #include <fuchsia/io/cpp/fidl.h>
9 #include <lib/fidl/cpp/interface_handle.h>
10 #include <memory>
11
12 #include "base/base_export.h"
13 #include "base/macros.h"
14
15 namespace fidl {
16
17 template <typename Interface>
18 class InterfacePtr;
19
20 template <typename Interface>
21 class SynchronousInterfacePtr;
22
23 }  // namespace fidl
24
25 namespace base {
26 namespace fuchsia {
27
28 class ScopedServiceDirectoryClientForCurrentProcessForTest;
29
30 // Helper for connecting to services from a supplied fuchsia.io.Directory.
31 class BASE_EXPORT ServiceDirectoryClient {
32  public:
33   // Wraps the supplied |directory| to access the services it contains.
34   explicit ServiceDirectoryClient(
35       fidl::InterfaceHandle<::fuchsia::io::Directory> directory);
36   ~ServiceDirectoryClient();
37
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();
42
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());
48   }
49
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)
56       result.Unbind();
57     return result;
58   }
59   template <typename Interface>
60   fidl::SynchronousInterfacePtr<Interface> ConnectToServiceSync() const {
61     fidl::SynchronousInterfacePtr<Interface> result;
62     if (ConnectToService(result.NewRequest()) != ZX_OK)
63       result.Unbind();
64     return result;
65   }
66
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;
72
73  private:
74   friend class ScopedServiceDirectoryClientForCurrentProcessForTest;
75   ServiceDirectoryClient();
76
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();
80
81   // Returns the container holding the ForCurrentProcess() instance. The
82   // default ServiceDirectoryClient is created the first time this function is
83   // called.
84   static std::unique_ptr<ServiceDirectoryClient>* ProcessInstance();
85
86   const fidl::InterfaceHandle<::fuchsia::io::Directory> directory_;
87
88   DISALLOW_COPY_AND_ASSIGN(ServiceDirectoryClient);
89 };
90
91 // Replaces the current process' ServiceDirectoryClient with the supplied
92 // |directory|, and restores it when going out-of-scope.
93 class BASE_EXPORT ScopedServiceDirectoryClientForCurrentProcessForTest {
94  public:
95   explicit ScopedServiceDirectoryClientForCurrentProcessForTest(
96       fidl::InterfaceHandle<::fuchsia::io::Directory> directory);
97   ~ScopedServiceDirectoryClientForCurrentProcessForTest();
98
99  private:
100   ServiceDirectoryClient* client_;
101   std::unique_ptr<ServiceDirectoryClient> old_client_;
102
103   DISALLOW_COPY_AND_ASSIGN(
104       ScopedServiceDirectoryClientForCurrentProcessForTest);
105 };
106
107 }  // namespace fuchsia
108 }  // namespace base
109
110 #endif  // BASE_FUCHSIA_SERVICE_DIRECTORY_CLIENT_H_