1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/ext/transport/binder/client/channel_create_impl.h"
18
19 #ifndef GRPC_NO_BINDER
20
21 #include <memory>
22 #include <utility>
23
24 #include "src/core/ext/transport/binder/client/binder_connector.h"
25 #include "src/core/ext/transport/binder/transport/binder_transport.h"
26 #include "src/core/ext/transport/binder/wire_format/binder.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/config/core_configuration.h"
29 #include "src/core/lib/surface/api_trace.h"
30 #include "src/core/lib/surface/channel.h"
31
32 namespace {
33
34 grpc_core::BinderClientChannelFactory* g_factory;
35 gpr_once g_factory_once = GPR_ONCE_INIT;
36
FactoryInit()37 void FactoryInit() { g_factory = new grpc_core::BinderClientChannelFactory(); }
38 } // namespace
39
40 namespace grpc {
41 namespace internal {
42
CreateDirectBinderChannelImplForTesting(std::unique_ptr<grpc_binder::Binder> endpoint_binder,const grpc_channel_args * args,std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy)43 grpc_channel* CreateDirectBinderChannelImplForTesting(
44 std::unique_ptr<grpc_binder::Binder> endpoint_binder,
45 const grpc_channel_args* args,
46 std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
47 security_policy) {
48 grpc_core::ExecCtx exec_ctx;
49
50 grpc_transport* transport = grpc_create_binder_transport_client(
51 std::move(endpoint_binder), security_policy);
52 GPR_ASSERT(transport != nullptr);
53
54 auto channel_args = grpc_core::CoreConfiguration::Get()
55 .channel_args_preconditioning()
56 .PreconditionChannelArgs(args)
57 .Set(GRPC_ARG_DEFAULT_AUTHORITY, "binder.authority");
58 auto channel =
59 grpc_core::Channel::Create("binder_target_placeholder", channel_args,
60 GRPC_CLIENT_DIRECT_CHANNEL, transport);
61 // TODO(mingcl): Handle error properly
62 GPR_ASSERT(channel.ok());
63 grpc_channel_args_destroy(args);
64 return channel->release()->c_ptr();
65 }
66
CreateClientBinderChannelImpl(const grpc_channel_args * args)67 grpc_channel* CreateClientBinderChannelImpl(const grpc_channel_args* args) {
68 grpc_core::ExecCtx exec_ctx;
69
70 gpr_once_init(&g_factory_once, FactoryInit);
71
72 auto channel_args = grpc_core::CoreConfiguration::Get()
73 .channel_args_preconditioning()
74 .PreconditionChannelArgs(args)
75 .SetObject(g_factory);
76
77 auto channel =
78 grpc_core::Channel::Create("binder_channel_target_placeholder",
79 channel_args, GRPC_CLIENT_CHANNEL, nullptr);
80
81 if (!channel.ok()) {
82 return grpc_lame_client_channel_create(
83 "binder_channel_target_placeholder",
84 static_cast<grpc_status_code>(channel.status().code()),
85 "Failed to create binder channel");
86 }
87
88 return channel->release()->c_ptr();
89 }
90
91 } // namespace internal
92 } // namespace grpc
93 #endif
94