BinderTransport customizable server location (#27083)

When bind to service, allow user to spacify service's package name and
class name
pull/27091/head
Ming-Chuan 3 years ago committed by GitHub
parent 13171a8b29
commit cc9c8c06d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      src/core/ext/transport/binder/client/channel_create.cc
  2. 11
      src/core/ext/transport/binder/client/jni_utils.cc
  3. 9
      src/core/ext/transport/binder/client/jni_utils.h
  4. 4
      src/core/ext/transport/binder/java/io/grpc/binder/cpp/NativeConnectionHelper.java
  5. 7
      src/core/ext/transport/binder/java/io/grpc/binder/cpp/SyncServiceConnection.java

@ -44,13 +44,11 @@ namespace grpc {
namespace experimental { namespace experimental {
// This should be called before calling CreateBinderChannel // 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 // TODO(mingcl): Invoke a callback and pass binder object to caller after a
// successful bind // successful bind
void BindToOnDeviceServerService(void* jni_env_void, jobject application, void BindToOnDeviceServerService(void* jni_env_void, jobject application,
absl::string_view /*package_name*/, absl::string_view package_name,
absl::string_view /*class_name*/ absl::string_view class_name) {
) {
// Init gRPC library first so gpr_log works // Init gRPC library first so gpr_log works
grpc::internal::GrpcLibrary init_lib; grpc::internal::GrpcLibrary init_lib;
init_lib.init(); init_lib.init();
@ -58,11 +56,11 @@ void BindToOnDeviceServerService(void* jni_env_void, jobject application,
JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void); JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
// clang-format off // clang-format off
CallStaticJavaMethod(jni_env, grpc_binder::CallStaticJavaMethod(jni_env,
"io/grpc/binder/cpp/NativeConnectionHelper", "io/grpc/binder/cpp/NativeConnectionHelper",
"tryEstablishConnection", "tryEstablishConnection",
"(Landroid/content/Context;)V", "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V",
application); application, std::string(package_name), std::string(class_name));
// clang-format on // clang-format on
} }
@ -76,7 +74,7 @@ std::shared_ptr<grpc::Channel> CreateBinderChannel(
JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void); JNIEnv* jni_env = static_cast<JNIEnv*>(jni_env_void);
// clang-format off // clang-format off
jobject object = CallStaticJavaMethodForObject( jobject object = grpc_binder::CallStaticJavaMethodForObject(
jni_env, jni_env,
"io/grpc/binder/cpp/NativeConnectionHelper", "io/grpc/binder/cpp/NativeConnectionHelper",
"getServiceBinder", "getServiceBinder",

@ -20,9 +20,12 @@
#if defined(ANDROID) || defined(__ANDROID__) #if defined(ANDROID) || defined(__ANDROID__)
namespace grpc_binder {
void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz, void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz,
const std::string& method, const std::string& type, 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()); jclass cl = env->FindClass(clazz.c_str());
if (cl == nullptr) { if (cl == nullptr) {
gpr_log(GPR_ERROR, "No class %s", clazz.c_str()); 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()); 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, jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz,
@ -60,4 +65,6 @@ jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz,
return object; return object;
} }
} // namespace grpc_binder
#endif #endif

@ -23,15 +23,20 @@
#include <string> #include <string>
// TODO(mingcl): Put these functions in a proper namespace namespace grpc_binder {
// TODO(mingcl): Use string_view // 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, void CallStaticJavaMethod(JNIEnv* env, const std::string& clazz,
const std::string& method, const std::string& type, 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, jobject CallStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz,
const std::string& method, const std::string& method,
const std::string& type); const std::string& type);
} // namespace grpc_binder
#endif #endif

@ -25,9 +25,9 @@ import android.os.Parcel;
final class NativeConnectionHelper { final class NativeConnectionHelper {
static SyncServiceConnection s; static SyncServiceConnection s;
static void tryEstablishConnection(Context context) { static void tryEstablishConnection(Context context, String pkg, String cls) {
s = new SyncServiceConnection(context); 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 // TODO(mingcl): We should notify C++ once we got the service binder so they don't need to call

@ -45,13 +45,10 @@ public class SyncServiceConnection implements ServiceConnection {
Log.e(logTag, "Service has disconnected: "); Log.e(logTag, "Service has disconnected: ");
} }
public void tryConnect() { public void tryConnect(String pkg, String cls) {
synchronized (this) { synchronized (this) {
Intent intent = new Intent("grpc.io.action.BIND"); Intent intent = new Intent("grpc.io.action.BIND");
// TODO(mingcl): The component name is currently hard-coded here and should be changed ComponentName compName = new ComponentName(pkg, cls);
// 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");
intent.setComponent(compName); intent.setComponent(compName);
// Will return true if the system is in the process of bringing up a service that your client // 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 // has permission to bind to; false if the system couldn't find the service or if your client

Loading…
Cancel
Save