1 // Copyright 2021 gRPC authors.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <grpc/impl/codegen/port_platform.h>
17 #include "src/core/ext/transport/binder/client/channel_create.h"
19 // The interface is only defined if GPR_ANDROID is defined, because some
20 // arguments requires JNI.
21 // Furthermore, the interface is non-phony only when
22 // GPR_SUPPORT_BINDER_TRANSPORT is true because actual implementation of binder
23 // transport requires newer version of NDK API
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_posix.h>
30 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
32 #include <grpc/support/port_platform.h>
34 #include <android/binder_auto_utils.h>
35 #include <android/binder_ibinder.h>
36 #include <android/binder_ibinder_jni.h>
37 #include <android/binder_interface_utils.h>
39 #include "absl/memory/memory.h"
40 #include "absl/time/clock.h"
41 #include "absl/time/time.h"
43 #include <grpc/support/log.h>
44 #include <grpcpp/impl/grpc_library.h>
46 #include "src/core/ext/transport/binder/client/channel_create_impl.h"
47 #include "src/core/ext/transport/binder/client/jni_utils.h"
48 #include "src/core/ext/transport/binder/transport/binder_transport.h"
49 #include "src/core/ext/transport/binder/wire_format/binder.h"
50 #include "src/core/ext/transport/binder/wire_format/binder_android.h"
51 #include "src/core/lib/surface/channel.h"
52 #include "src/core/lib/transport/transport.h"
53 #include "src/cpp/client/create_channel_internal.h"
56 namespace experimental {
58 // This should be called before calling CreateBinderChannel
59 // TODO(mingcl): Invoke a callback and pass binder object to caller after a
61 void BindToOnDeviceServerService(void* jni_env_void, jobject application,
62 absl::string_view package_name,
63 absl::string_view class_name) {
64 // Init gRPC library first so gpr_log works
65 grpc::internal::GrpcLibrary init_lib;
68 JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
71 grpc_binder::CallStaticJavaMethod(jni_env,
72 "io/grpc/binder/cpp/NativeConnectionHelper",
73 "tryEstablishConnection",
74 "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V",
75 application, std::string(package_name), std::string(class_name));
79 // BindToOndeviceServerService need to be called before this, in a different
80 // task (due to Android API design). (Reference:
81 // https://stackoverflow.com/a/3055749)
82 // TODO(mingcl): Support multiple endpoint binder objects
83 std::shared_ptr<grpc::Channel> CreateBinderChannel(
84 void* jni_env_void, jobject application, absl::string_view package_name,
85 absl::string_view class_name) {
86 return CreateCustomBinderChannel(jni_env_void, application, package_name,
87 class_name, ChannelArguments());
90 // BindToOndeviceServerService need to be called before this, in a different
91 // task (due to Android API design). (Reference:
92 // https://stackoverflow.com/a/3055749)
93 // TODO(mingcl): Support multiple endpoint binder objects
94 std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
95 void* jni_env_void, jobject /*application*/,
96 absl::string_view /*package_name*/, absl::string_view /*class_name*/,
97 const ChannelArguments& args) {
98 JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
101 jobject object = grpc_binder::CallStaticJavaMethodForObject(
103 "io/grpc/binder/cpp/NativeConnectionHelper",
105 "()Landroid/os/IBinder;");
108 grpc_channel_args channel_args;
109 args.SetChannelArgs(&channel_args);
110 return CreateChannelInternal(
112 ::grpc::internal::CreateChannelFromBinderImpl(
113 absl::make_unique<grpc_binder::BinderAndroid>(
114 grpc_binder::FromJavaBinder(jni_env, object)),
117 std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
120 } // namespace experimental
123 #else // !GPR_SUPPORT_BINDER_TRANSPORT
126 namespace experimental {
128 void BindToOnDeviceServerService(void*, jobject, absl::string_view,
133 std::shared_ptr<grpc::Channel> CreateBinderChannel(void*, jobject,
140 std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
141 void*, jobject, absl::string_view, absl::string_view,
142 const ChannelArguments&) {
147 } // namespace experimental
150 #endif // GPR_SUPPORT_BINDER_TRANSPORT
152 #endif // GPR_ANDROID