xref: /aosp_15_r20/external/cronet/base/fuchsia/fuchsia_component_connect.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
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_FUCHSIA_COMPONENT_CONNECT_H_
6 #define BASE_FUCHSIA_FUCHSIA_COMPONENT_CONNECT_H_
7 
8 #include <fidl/fuchsia.io/cpp/markers.h>
9 #include <lib/component/incoming/cpp/protocol.h>
10 #include <lib/fidl/cpp/wire/connect_service.h>
11 #include <lib/zx/result.h>
12 
13 #include <string>
14 
15 #include "base/base_export.h"
16 #include "base/fuchsia/process_context.h"
17 
18 // This namespace is designed to be consistent with the `component::Connect`
19 // calls used within Fuchsia. Consistency keeps Fuchsia documentation more
20 // relevant for developers working on Chromium as well as creating less mental
21 // overhead when working in both domains. See go/natural-component-context for
22 // more details (Googlers only).
23 namespace base::fuchsia_component {
24 
25 template <typename Protocol,
26           typename = std::enable_if_t<fidl::IsProtocolV<Protocol>>>
27 BASE_EXPORT zx::result<> Connect(
28     fidl::ServerEnd<Protocol> server_end,
29     std::string name = fidl::DiscoverableProtocolName<Protocol>) {
30   return component::ConnectAt<Protocol>(
31       base::BorrowIncomingServiceDirectoryForProcess(), std::move(server_end),
32       name);
33 }
34 
35 template <typename Protocol,
36           typename = std::enable_if_t<fidl::IsProtocolV<Protocol>>>
37 BASE_EXPORT zx::result<fidl::ClientEnd<Protocol>> Connect(
38     std::string name = fidl::DiscoverableProtocolName<Protocol>) {
39   return component::ConnectAt<Protocol>(
40       base::BorrowIncomingServiceDirectoryForProcess(), name);
41 }
42 
43 template <typename Protocol,
44           typename = std::enable_if_t<fidl::IsProtocolV<Protocol>>>
45 BASE_EXPORT zx::result<fidl::ClientEnd<Protocol>> ConnectAt(
46     fidl::UnownedClientEnd<fuchsia_io::Directory> service_directory,
47     std::string name = fidl::DiscoverableProtocolName<Protocol>) {
48   return component::ConnectAt<Protocol>(service_directory, name);
49 }
50 
51 }  // namespace base::fuchsia_component
52 
53 #endif  // BASE_FUCHSIA_FUCHSIA_COMPONENT_CONNECT_H_
54