Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / ext / transport / binder / client / channel_create.cc
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/impl/codegen/port_platform.h>
16
17 #include "src/core/ext/transport/binder/client/channel_create.h"
18
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
24
25 #ifdef GPR_ANDROID
26
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_posix.h>
29
30 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
31
32 #include <grpc/support/port_platform.h>
33
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>
38
39 #include "absl/memory/memory.h"
40 #include "absl/time/clock.h"
41 #include "absl/time/time.h"
42
43 #include <grpc/support/log.h>
44 #include <grpcpp/impl/grpc_library.h>
45
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"
54
55 namespace grpc {
56 namespace experimental {
57
58 // This should be called before calling CreateBinderChannel
59 // TODO(mingcl): Invoke a callback and pass binder object to caller after a
60 // successful bind
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;
66   init_lib.init();
67
68   JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
69
70   // clang-format off
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));
76   // clang-format on
77 }
78
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());
88 }
89
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);
99
100   // clang-format off
101   jobject object = grpc_binder::CallStaticJavaMethodForObject(
102       jni_env,
103       "io/grpc/binder/cpp/NativeConnectionHelper",
104       "getServiceBinder",
105       "()Landroid/os/IBinder;");
106   // clang-format on
107
108   grpc_channel_args channel_args;
109   args.SetChannelArgs(&channel_args);
110   return CreateChannelInternal(
111       "",
112       ::grpc::internal::CreateChannelFromBinderImpl(
113           absl::make_unique<grpc_binder::BinderAndroid>(
114               grpc_binder::FromJavaBinder(jni_env, object)),
115           &channel_args),
116       std::vector<
117           std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>());
118 }
119
120 }  // namespace experimental
121 }  // namespace grpc
122
123 #else  // !GPR_SUPPORT_BINDER_TRANSPORT
124
125 namespace grpc {
126 namespace experimental {
127
128 void BindToOnDeviceServerService(void*, jobject, absl::string_view,
129                                  absl::string_view) {
130   GPR_ASSERT(0);
131 }
132
133 std::shared_ptr<grpc::Channel> CreateBinderChannel(void*, jobject,
134                                                    absl::string_view,
135                                                    absl::string_view) {
136   GPR_ASSERT(0);
137   return {};
138 }
139
140 std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
141     void*, jobject, absl::string_view, absl::string_view,
142     const ChannelArguments&) {
143   GPR_ASSERT(0);
144   return {};
145 }
146
147 }  // namespace experimental
148 }  // namespace grpc
149
150 #endif  // GPR_SUPPORT_BINDER_TRANSPORT
151
152 #endif  // GPR_ANDROID