From cc9c8c06d9a9583846b7d95ae0201d07bd809824 Mon Sep 17 00:00:00 2001 From: Ming-Chuan Date: Tue, 24 Aug 2021 06:51:39 +0800 Subject: [PATCH] BinderTransport customizable server location (#27083) When bind to service, allow user to spacify service's package name and class name --- .../ext/transport/binder/client/channel_create.cc | 14 ++++++-------- src/core/ext/transport/binder/client/jni_utils.cc | 11 +++++++++-- src/core/ext/transport/binder/client/jni_utils.h | 9 +++++++-- .../io/grpc/binder/cpp/NativeConnectionHelper.java | 4 ++-- .../io/grpc/binder/cpp/SyncServiceConnection.java | 7 ++----- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/core/ext/transport/binder/client/channel_create.cc b/src/core/ext/transport/binder/client/channel_create.cc index f434f6d76f4..0739787ec75 100644 --- a/src/core/ext/transport/binder/client/channel_create.cc +++ b/src/core/ext/transport/binder/client/channel_create.cc @@ -44,13 +44,11 @@ namespace grpc { namespace experimental { // This should be called before calling CreateBinderChannel -// TODO(mingcl): Pass package_name and class_name down to connection helper // TODO(mingcl): Invoke a callback and pass binder object to caller after a // successful bind void BindToOnDeviceServerService(void* jni_env_void, jobject application, - absl::string_view /*package_name*/, - absl::string_view /*class_name*/ -) { + absl::string_view package_name, + absl::string_view class_name) { // Init gRPC library first so gpr_log works grpc::internal::GrpcLibrary init_lib; init_lib.init(); @@ -58,11 +56,11 @@ void BindToOnDeviceServerService(void* jni_env_void, jobject application, JNIEnv* jni_env = static_cast(jni_env_void); // clang-format off - CallStaticJavaMethod(jni_env, + grpc_binder::CallStaticJavaMethod(jni_env, "io/grpc/binder/cpp/NativeConnectionHelper", "tryEstablishConnection", - "(Landroid/content/Context;)V", - application); + "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V", + application, std::string(package_name), std::string(class_name)); // clang-format on } @@ -76,7 +74,7 @@ std::shared_ptr CreateBinderChannel( JNIEnv* jni_env = static_cast(jni_env_void); // clang-format off - jobject object = CallStaticJavaMethodForObject( + jobject object = grpc_binder::CallStaticJavaMethodForObject( jni_env, "io/grpc/binder/cpp/NativeConnectionHelper", "getServiceBinder", diff --git a/src/core/ext/transport/binder/client/jni_utils.cc b/src/core/ext/transport/binder/client/jni_utils.cc index 6159a3a283e..fcdea61a4b0 100644 --- a/src/core/ext/transport/binder/client/jni_utils.cc +++ b/src/core/ext/transport/binder/client/jni_utils.cc @@ -20,9 +20,12 @@ #if defined(ANDROID) || defined(__ANDROID__) +namespace grpc_binder { + void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz, const std::string& method, const std::string& type, - jobject application) { + jobject application, const std::string& pkg, + const std::string& cls) { jclass cl = env->FindClass(clazz.c_str()); if (cl == nullptr) { gpr_log(GPR_ERROR, "No class %s", clazz.c_str()); @@ -33,7 +36,9 @@ void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz, gpr_log(GPR_ERROR, "No method id %s", method.c_str()); } - env->CallStaticVoidMethod(cl, mid, application); + env->CallStaticVoidMethod(cl, mid, application, + env->NewStringUTF(pkg.c_str()), + env->NewStringUTF(cls.c_str())); } jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz, @@ -60,4 +65,6 @@ jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz, return object; } +} // namespace grpc_binder + #endif diff --git a/src/core/ext/transport/binder/client/jni_utils.h b/src/core/ext/transport/binder/client/jni_utils.h index 0780d5ba6ba..b7c55d4c195 100644 --- a/src/core/ext/transport/binder/client/jni_utils.h +++ b/src/core/ext/transport/binder/client/jni_utils.h @@ -23,15 +23,20 @@ #include -// TODO(mingcl): Put these functions in a proper namespace +namespace grpc_binder { + // TODO(mingcl): Use string_view +// For now we hard code the arguments of the Java function because this is only +// used to call that single function. void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz, const std::string& method, const std::string& type, - jobject application); + jobject application, const std::string& pkg, + const std::string& cls); jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz, const std::string& method, const std::string& type); +} // namespace grpc_binder #endif diff --git a/src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java b/src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java index 8913e184f59..6dd1755086a 100644 --- a/src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java +++ b/src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java @@ -25,9 +25,9 @@ import android.os.Parcel; final class NativeConnectionHelper { static SyncServiceConnection s; - static void tryEstablishConnection(Context context) { + static void tryEstablishConnection(Context context, String pkg, String cls) { s = new SyncServiceConnection(context); - s.tryConnect(); + s.tryConnect(pkg, cls); } // TODO(mingcl): We should notify C++ once we got the service binder so they don't need to call diff --git a/src/core/ext/transport/binder/java/io/grpc/binder/cpp/SyncServiceConnection.java b/src/core/ext/transport/binder/java/io/grpc/binder/cpp/SyncServiceConnection.java index 69e17295ba0..66848f39da6 100644 --- a/src/core/ext/transport/binder/java/io/grpc/binder/cpp/SyncServiceConnection.java +++ b/src/core/ext/transport/binder/java/io/grpc/binder/cpp/SyncServiceConnection.java @@ -45,13 +45,10 @@ public class SyncServiceConnection implements ServiceConnection { Log.e(logTag, "Service has disconnected: "); } - public void tryConnect() { + public void tryConnect(String pkg, String cls) { synchronized (this) { Intent intent = new Intent("grpc.io.action.BIND"); - // TODO(mingcl): The component name is currently hard-coded here and should be changed - // manually before compile. We should pump the component name from C++ to here instead after - // we have a server ready for integration test. - ComponentName compName = new ComponentName("redacted", "redacted"); + ComponentName compName = new ComponentName(pkg, cls); intent.setComponent(compName); // Will return true if the system is in the process of bringing up a service that your client // has permission to bind to; false if the system couldn't find the service or if your client