| 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 |
#ifndef GRPC_NO_BINDER |
| 18 |
|
| 19 |
#include <grpcpp/create_channel_binder.h> |
| 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 |
|
| 35 |
#include <grpc/support/port_platform.h> |
| 36 |
|
| 37 |
#include "absl/memory/memory.h" |
| 38 |
#include "absl/strings/substitute.h" |
| 39 |
#include "absl/time/clock.h" |
| 40 |
#include "absl/time/time.h" |
| 41 |
|
| 42 |
#include <grpcpp/impl/grpc_library.h> |
| 43 |
|
| 44 |
#include "src/core/ext/filters/client_channel/client_channel.h" |
| 45 |
#include "src/core/ext/transport/binder/client/channel_create_impl.h" |
| 46 |
#include "src/core/ext/transport/binder/client/connection_id_generator.h" |
| 47 |
#include "src/core/ext/transport/binder/client/endpoint_binder_pool.h" |
| 48 |
#include "src/core/ext/transport/binder/client/jni_utils.h" |
| 49 |
#include "src/core/ext/transport/binder/client/security_policy_setting.h" |
| 50 |
#include "src/core/ext/transport/binder/transport/binder_transport.h" |
| 51 |
#include "src/core/ext/transport/binder/wire_format/binder.h" |
| 52 |
#include "src/core/ext/transport/binder/wire_format/binder_android.h" |
| 53 |
#include "src/core/lib/channel/channel_args.h" |
| 54 |
#include "src/core/lib/config/core_configuration.h" |
| 55 |
#include "src/core/lib/surface/channel.h" |
| 56 |
#include "src/core/lib/transport/transport.h" |
| 57 |
#include "src/cpp/client/create_channel_internal.h" |
| 58 |
|
| 59 |
namespace { |
| 60 |
// grpc.io.action.BIND is the standard action name for binding to binder |
| 61 |
// transport server. |
| 62 |
const char* kStandardActionName = "grpc.io.action.BIND"; |
| 63 |
} // namespace |
| 64 |
|
| 65 |
namespace grpc { |
| 66 |
namespace experimental { |
| 67 |
|
| 68 |
std::shared_ptr<grpc::Channel> CreateBinderChannel( |
| 69 |
void* jni_env_void, jobject application, absl::string_view package_name, |
| 70 |
absl::string_view class_name, |
| 71 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy> |
| 72 |
security_policy) { |
| 73 |
return CreateCustomBinderChannel(jni_env_void, application, package_name, |
| 74 |
class_name, security_policy, |
| 75 |
ChannelArguments()); |
| 76 |
} |
| 77 |
|
| 78 |
std::shared_ptr<grpc::Channel> CreateCustomBinderChannel( |
| 79 |
void* jni_env_void, jobject application, absl::string_view package_name, |
| 80 |
absl::string_view class_name, |
| 81 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy, |
| 82 |
const ChannelArguments& args) { |
| 83 |
return CreateCustomBinderChannel( |
| 84 |
jni_env_void, application, |
| 85 |
absl::Substitute("android-app://$0#Intent;action=$1;component=$0/$2;end", |
| 86 |
package_name, kStandardActionName, class_name), |
| 87 |
security_policy, args); |
| 88 |
} |
| 89 |
|
| 90 |
std::shared_ptr<grpc::Channel> CreateBinderChannel( |
| 91 |
void* jni_env_void, jobject application, absl::string_view uri, |
| 92 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy> |
| 93 |
security_policy) { |
| 94 |
return CreateCustomBinderChannel(jni_env_void, application, uri, |
| 95 |
security_policy, ChannelArguments()); |
| 96 |
} |
| 97 |
|
| 98 |
std::shared_ptr<grpc::Channel> CreateCustomBinderChannel( |
| 99 |
void* jni_env_void, jobject application, absl::string_view uri, |
| 100 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy, |
| 101 |
const ChannelArguments& args) { |
| 102 |
grpc::internal::GrpcLibrary init_lib; |
| 103 |
init_lib.init(); |
| 104 |
|
| 105 |
GPR_ASSERT(jni_env_void != nullptr); |
| 106 |
GPR_ASSERT(security_policy != nullptr); |
| 107 |
|
| 108 |
// Generate an unique connection ID that identifies this connection (Useful |
| 109 |
// for mapping connection between Java and C++ code). |
| 110 |
std::string connection_id = |
| 111 |
grpc_binder::GetConnectionIdGenerator()->Generate(uri); |
| 112 |
|
| 113 |
gpr_log(GPR_ERROR, "connection id is %s", connection_id.c_str()); |
| 114 |
|
| 115 |
// After invoking this Java method, Java code will put endpoint binder into |
| 116 |
// `EndpointBinderPool` after the connection succeeds |
| 117 |
// TODO(mingcl): Consider if we want to delay the connection establishment |
| 118 |
// until SubchannelConnector start establishing connection. For now we don't |
| 119 |
// see any benifits doing that. |
| 120 |
grpc_binder::TryEstablishConnectionWithUri(static_cast<JNIEnv*>(jni_env_void), |
| 121 |
application, uri, connection_id); |
| 122 |
|
| 123 |
grpc_channel_args channel_args; |
| 124 |
args.SetChannelArgs(&channel_args); |
| 125 |
|
| 126 |
// Set server URI to a URI that contains connection id. The URI will be used |
| 127 |
// by subchannel connector to obtain correct endpoint binder from |
| 128 |
// `EndpointBinderPool`. |
| 129 |
grpc_channel_args* new_args; |
| 130 |
{ |
| 131 |
string server_uri = "binder:" + connection_id; |
| 132 |
grpc_arg server_uri_arg = |
| 133 |
grpc_channel_arg_string_create(const_cast<char*>(GRPC_ARG_SERVER_URI), |
| 134 |
const_cast<char*>(server_uri.c_str())); |
| 135 |
const char* to_remove[] = {GRPC_ARG_SERVER_URI}; |
| 136 |
new_args = grpc_channel_args_copy_and_add_and_remove( |
| 137 |
&channel_args, to_remove, 1, &server_uri_arg, 1); |
| 138 |
} |
| 139 |
|
| 140 |
grpc_binder::GetSecurityPolicySetting()->Set(connection_id, security_policy); |
| 141 |
|
| 142 |
auto channel = CreateChannelInternal( |
| 143 |
"", grpc::internal::CreateClientBinderChannelImpl(new_args), |
| 144 |
std::vector< |
| 145 |
std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>()); |
| 146 |
|
| 147 |
grpc_channel_args_destroy(new_args); |
| 148 |
|
| 149 |
return channel; |
| 150 |
} |
| 151 |
|
| 152 |
bool InitializeBinderChannelJavaClass(void* jni_env_void) { |
| 153 |
return grpc_binder::FindNativeConnectionHelper( |
| 154 |
static_cast<JNIEnv*>(jni_env_void)) != nullptr; |
| 155 |
} |
| 156 |
|
| 157 |
bool InitializeBinderChannelJavaClass( |
| 158 |
void* jni_env_void, std::function<void*(std::string)> class_finder) { |
| 159 |
return grpc_binder::FindNativeConnectionHelper( |
| 160 |
static_cast<JNIEnv*>(jni_env_void), class_finder) != nullptr; |
| 161 |
} |
| 162 |
|
| 163 |
} // namespace experimental |
| 164 |
} // namespace grpc |
| 165 |
|
| 166 |
#else // !GPR_SUPPORT_BINDER_TRANSPORT |
| 167 |
|
| 168 |
namespace grpc { |
| 169 |
namespace experimental { |
| 170 |
|
| 171 |
std::shared_ptr<grpc::Channel> CreateBinderChannel( |
| 172 |
void*, jobject, absl::string_view, absl::string_view, |
| 173 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy>) { |
| 174 |
gpr_log(GPR_ERROR, |
| 175 |
"This APK is compiled with Android API level = %d, which is not " |
| 176 |
"supported. See port_platform.h for supported versions.", |
| 177 |
__ANDROID_API__); |
| 178 |
GPR_ASSERT(0); |
| 179 |
return {}; |
| 180 |
} |
| 181 |
|
| 182 |
std::shared_ptr<grpc::Channel> CreateCustomBinderChannel( |
| 183 |
void*, jobject, absl::string_view, absl::string_view, |
| 184 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy>, |
| 185 |
const ChannelArguments&) { |
| 186 |
gpr_log(GPR_ERROR, |
| 187 |
"This APK is compiled with Android API level = %d, which is not " |
| 188 |
"supported. See port_platform.h for supported versions.", |
| 189 |
__ANDROID_API__); |
| 190 |
GPR_ASSERT(0); |
| 191 |
return {}; |
| 192 |
} |
| 193 |
|
| 194 |
std::shared_ptr<grpc::Channel> CreateBinderChannel( |
| 195 |
void*, jobject, absl::string_view, |
| 196 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy>) { |
| 197 |
gpr_log(GPR_ERROR, |
| 198 |
"This APK is compiled with Android API level = %d, which is not " |
| 199 |
"supported. See port_platform.h for supported versions.", |
| 200 |
__ANDROID_API__); |
| 201 |
GPR_ASSERT(0); |
| 202 |
return {}; |
| 203 |
} |
| 204 |
|
| 205 |
std::shared_ptr<grpc::Channel> CreateCustomBinderChannel( |
| 206 |
void*, jobject, absl::string_view, |
| 207 |
std::shared_ptr<grpc::experimental::binder::SecurityPolicy>, |
| 208 |
const ChannelArguments&) { |
| 209 |
gpr_log(GPR_ERROR, |
| 210 |
"This APK is compiled with Android API level = %d, which is not " |
| 211 |
"supported. See port_platform.h for supported versions.", |
| 212 |
__ANDROID_API__); |
| 213 |
GPR_ASSERT(0); |
| 214 |
return {}; |
| 215 |
} |
| 216 |
|
| 217 |
bool InitializeBinderChannelJavaClass(void* jni_env_void) { |
| 218 |
gpr_log(GPR_ERROR, |
| 219 |
"This APK is compiled with Android API level = %d, which is not " |
| 220 |
"supported. See port_platform.h for supported versions.", |
| 221 |
__ANDROID_API__); |
| 222 |
GPR_ASSERT(0); |
| 223 |
return {}; |
| 224 |
} |
| 225 |
|
| 226 |
bool InitializeBinderChannelJavaClass( |
| 227 |
void* jni_env_void, std::function<void*(std::string)> class_finder) { |
| 228 |
gpr_log(GPR_ERROR, |
| 229 |
"This APK is compiled with Android API level = %d, which is not " |
| 230 |
"supported. See port_platform.h for supported versions.", |
| 231 |
__ANDROID_API__); |
| 232 |
GPR_ASSERT(0); |
| 233 |
return {}; |
| 234 |
} |
| 235 |
|
| 236 |
} // namespace experimental |
| 237 |
} // namespace grpc |
| 238 |
|
| 239 |
#endif // GPR_SUPPORT_BINDER_TRANSPORT |
| 240 |
|
| 241 |
#endif // GPR_ANDROID |
| 242 |
|
| 243 |
#endif // GRPC_NO_BINDER |