channel_create.cc
Go to the documentation of this file.
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 
16 
17 #ifndef GRPC_NO_BINDER
18 
20 
21 // The interface is only defined if GPR_ANDROID is defined, because some
22 // arguments requires JNI.
23 // Furthermore, the interface is non-phony only when
24 // GPR_SUPPORT_BINDER_TRANSPORT is true because actual implementation of binder
25 // transport requires newer version of NDK API
26 
27 #ifdef GPR_ANDROID
28 
29 #include <grpc/grpc.h>
30 #include <grpc/grpc_posix.h>
31 #include <grpc/support/log.h>
32 
33 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
34 
36 
37 #include "absl/memory/memory.h"
38 #include "absl/time/clock.h"
39 #include "absl/time/time.h"
40 
42 
57 
58 namespace {
59 // grpc.io.action.BIND is the standard action name for binding to binder
60 // transport server.
61 const char* kStandardActionName = "grpc.io.action.BIND";
62 
63 // grpc::ChannelArguments key for specifying custom action name.
64 const char* kCustomIntentActionNameArgKey =
65  "grpc.binder.custom_android_intent_action_name";
66 } // namespace
67 
68 namespace grpc {
69 namespace experimental {
70 
71 std::shared_ptr<grpc::Channel> CreateBinderChannel(
72  void* jni_env_void, jobject application, absl::string_view package_name,
74  std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
75  security_policy) {
76  return CreateCustomBinderChannel(jni_env_void, application, package_name,
77  class_name, security_policy,
78  ChannelArguments());
79 }
80 
81 std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
82  void* jni_env_void, jobject application, absl::string_view package_name,
84  std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy,
85  const ChannelArguments& args) {
87  init_lib.init();
88 
89  GPR_ASSERT(jni_env_void != nullptr);
90  GPR_ASSERT(security_policy != nullptr);
91 
92  // Generate an unique connection ID that identifies this connection (Useful
93  // for mapping connection between Java and C++ code).
95  std::string(package_name), std::string(class_name));
96 
97  grpc_channel_args channel_args_with_custom_action;
98  args.SetChannelArgs(&channel_args_with_custom_action);
99 
100  // Check if user set an option to use non-standard action name to bind to
101  // server. At this moment this option is not intend for general production use
102  // and is mainly for stress testing purpose.
103  std::string action_name = kStandardActionName;
104  const grpc_arg* action_name_arg = grpc_channel_args_find(
105  &channel_args_with_custom_action, kCustomIntentActionNameArgKey);
106  if (action_name_arg != nullptr) {
107  // The option is set. Now check if it is a string.
108  char* action_name_arg_string = grpc_channel_arg_get_string(action_name_arg);
109  if (action_name_arg_string != nullptr) {
110  action_name = action_name_arg_string;
111  }
112  }
113  grpc_channel_args* channel_args;
114  {
115  // Passing the key down will cause gRPC internal error for unclear reason.
116  // Remove it here.
117  const char* to_remove[] = {kCustomIntentActionNameArgKey};
118  channel_args = grpc_channel_args_copy_and_remove(
119  &channel_args_with_custom_action, to_remove, 1);
120  }
121 
122  // After invoking this Java method, Java code will put endpoint binder into
123  // `EndpointBinderPool` after the connection succeeds
124  // TODO(mingcl): Consider if we want to delay the connection establishment
125  // until SubchannelConnector start establishing connection. For now we don't
126  // see any benifits doing that.
127  grpc_binder::TryEstablishConnection(static_cast<JNIEnv*>(jni_env_void),
128  application, package_name, class_name,
129  action_name, connection_id);
130 
131  // Set server URI to a URI that contains connection id. The URI will be used
132  // by subchannel connector to obtain correct endpoint binder from
133  // `EndpointBinderPool`.
134  grpc_channel_args* new_args;
135  {
136  grpc_arg server_uri_arg = grpc_channel_arg_string_create(
137  const_cast<char*>(GRPC_ARG_SERVER_URI),
138  const_cast<char*>(("binder:" + connection_id).c_str()));
139  const char* to_remove[] = {GRPC_ARG_SERVER_URI};
141  channel_args, to_remove, 1, &server_uri_arg, 1);
142  }
143 
144  grpc_binder::GetSecurityPolicySetting()->Set(connection_id, security_policy);
145 
148  std::vector<
149  std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
150 
151  grpc_channel_args_destroy(new_args);
152  grpc_channel_args_destroy(channel_args);
153 
154  return channel;
155 }
156 
157 bool InitializeBinderChannelJavaClass(void* jni_env_void) {
158  return grpc_binder::FindNativeConnectionHelper(
159  static_cast<JNIEnv*>(jni_env_void)) != nullptr;
160 }
161 
162 bool InitializeBinderChannelJavaClass(
163  void* jni_env_void, std::function<void*(std::string)> class_finder) {
164  return grpc_binder::FindNativeConnectionHelper(
165  static_cast<JNIEnv*>(jni_env_void), class_finder) != nullptr;
166 }
167 
168 } // namespace experimental
169 } // namespace grpc
170 
171 #else // !GPR_SUPPORT_BINDER_TRANSPORT
172 
173 namespace grpc {
174 namespace experimental {
175 
176 std::shared_ptr<grpc::Channel> CreateBinderChannel(
177  void*, jobject, absl::string_view, absl::string_view,
178  std::shared_ptr<grpc::experimental::binder::SecurityPolicy>) {
180  "This APK is compiled with Android API level = %d, which is not "
181  "supported. See port_platform.h for supported versions.",
182  __ANDROID_API__);
183  GPR_ASSERT(0);
184  return {};
185 }
186 
187 std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
188  void*, jobject, absl::string_view, absl::string_view,
189  std::shared_ptr<grpc::experimental::binder::SecurityPolicy>,
190  const ChannelArguments&) {
192  "This APK is compiled with Android API level = %d, which is not "
193  "supported. See port_platform.h for supported versions.",
194  __ANDROID_API__);
195  GPR_ASSERT(0);
196  return {};
197 }
198 
199 bool InitializeBinderChannelJavaClass(void* jni_env_void) {
201  "This APK is compiled with Android API level = %d, which is not "
202  "supported. See port_platform.h for supported versions.",
203  __ANDROID_API__);
204  GPR_ASSERT(0);
205  return {};
206 }
207 
208 bool InitializeBinderChannelJavaClass(
209  void* jni_env_void, std::function<void*(std::string)> class_finder) {
211  "This APK is compiled with Android API level = %d, which is not "
212  "supported. See port_platform.h for supported versions.",
213  __ANDROID_API__);
214  GPR_ASSERT(0);
215  return {};
216 }
217 
218 } // namespace experimental
219 } // namespace grpc
220 
221 #endif // GPR_SUPPORT_BINDER_TRANSPORT
222 
223 #endif // GPR_ANDROID
224 
225 #endif // GRPC_NO_BINDER
grpc_arg
Definition: grpc_types.h:103
grpc::CreateChannelInternal
std::shared_ptr< Channel > CreateChannelInternal(const std::string &host, grpc_channel *c_channel, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
grpc_channel_arg_get_string
char * grpc_channel_arg_get_string(const grpc_arg *arg)
Definition: channel_args.cc:432
create_channel_internal.h
log.h
core_configuration.h
grpc
Definition: grpcpp/alarm.h:33
grpc::testing::CreateBinderChannel
std::shared_ptr< grpc::Channel > CreateBinderChannel(std::unique_ptr< grpc_binder::Binder > endpoint_binder)
Definition: binder_server_test.cc:70
grpc_channel_args_copy_and_remove
grpc_channel_args * grpc_channel_args_copy_and_remove(const grpc_channel_args *src, const char **to_remove, size_t num_to_remove)
Definition: channel_args.cc:231
security_policy_setting.h
binder.h
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
client_channel.h
grpc_channel_args_copy_and_add_and_remove
grpc_channel_args * grpc_channel_args_copy_and_add_and_remove(const grpc_channel_args *src, const char **to_remove, size_t num_to_remove, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:246
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
grpc::internal::CreateClientBinderChannelImpl
grpc_channel * CreateClientBinderChannelImpl(const grpc_channel_args *args)
Definition: channel_create_impl.cc:67
jni_utils.h
grpc_channel_args
Definition: grpc_types.h:132
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
binder_android.h
grpc_binder::GetConnectionIdGenerator
ConnectionIdGenerator * GetConnectionIdGenerator()
Definition: connection_id_generator.cc:64
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
endpoint_binder_pool.h
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
GRPC_ARG_SERVER_URI
#define GRPC_ARG_SERVER_URI
Definition: client_channel.h:89
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_posix.h
grpc_binder::GetSecurityPolicySetting
SecurityPolicySetting * GetSecurityPolicySetting()
Definition: security_policy_setting.cc:39
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc::internal::GrpcLibrary
Definition: grpcpp/impl/grpc_library.h:32
grpc_binder::ConnectionIdGenerator::Generate
std::string Generate(absl::string_view package_name, absl::string_view class_name)
Definition: connection_id_generator.cc:47
class_name
static const char * class_name(int dnsclass)
Definition: adig.c:901
connection_id_generator.h
grpc_library.h
channel_create_impl.h
transport.h
grpc::internal::GrpcLibrary::init
void init() override
Definition: grpcpp/impl/grpc_library.h:34
channel_args.h
grpc_binder::SecurityPolicySetting::Set
void Set(absl::string_view connection_id, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: security_policy_setting.cc:23
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_channel_args_find
const grpc_arg * grpc_channel_args_find(const grpc_channel_args *args, const char *name)
Definition: channel_args.cc:393
binder_transport.h
create_channel_binder.h
channel.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:43