@ -24,66 +24,56 @@
namespace grpc_binder {
void CallStaticJavaMethod ( JNIEnv * env , const std : : string & clazz ,
const std : : string & method , const std : : string & type ,
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 ( ) ) ;
}
jmethodID mid = env - > GetStaticMethodID ( cl , method . c_str ( ) , type . c_str ( ) ) ;
if ( mid = = nullptr ) {
gpr_log ( GPR_ERROR , " No method id %s " , method . c_str ( ) ) ;
jclass FindNativeConnectionHelper ( JNIEnv * env ) {
auto do_find = [ env ] ( ) {
jclass cl = env - > FindClass ( " io/grpc/binder/cpp/NativeConnectionHelper " ) ;
if ( cl = = nullptr ) {
return cl ;
}
jclass global_cl = static_cast < jclass > ( env - > NewGlobalRef ( cl ) ) ;
GPR_ASSERT ( global_cl ! = nullptr ) ;
return global_cl ;
} ;
static jclass connection_helper_class = do_find ( ) ;
if ( connection_helper_class ! = nullptr ) {
return connection_helper_class ;
}
env - > CallStaticVoidMethod ( cl , mid , application ,
env - > NewStringUTF ( pkg . c_str ( ) ) ,
env - > NewStringUTF ( cls . c_str ( ) ) ) ;
// Some possible reasons:
// * There is no Java class in the call stack and this is not invoked
// from JNI_OnLoad
// * The APK does not correctly depends on the helper class, or the
// class get shrinked
gpr_log ( GPR_ERROR ,
" Cannot find binder transport Java helper class. Did you invoke "
" grpc::experimental::InitializeBinderChannelJavaClass correctly "
" beforehand? " ) ;
// TODO(mingcl): Maybe it is worth to try again so the failure can be fixed
// by invoking this function again at a different thread.
return nullptr ;
}
void CallStaticJavaMethod ( JNIEnv * env , const std : : string & clazz ,
const std : : string & method , const std : : string & type ,
jobject application , const std : : string & pkg ,
const std : : string & cls , const std : : string & conn_id ) {
jclass cl = env - > FindClass ( clazz . c_str ( ) ) ;
if ( cl = = nullptr ) {
gpr_log ( GPR_ERROR , " No class %s " , clazz . c_str ( ) ) ;
}
jmethodID mid = env - > GetStaticMethodID ( cl , method . c_str ( ) , type . c_str ( ) ) ;
if ( mid = = nullptr ) {
gpr_log ( GPR_ERROR , " No method id %s " , method . c_str ( ) ) ;
}
env - > CallStaticVoidMethod (
cl , mid , application , env - > NewStringUTF ( pkg . c_str ( ) ) ,
env - > NewStringUTF ( cls . c_str ( ) ) , env - > NewStringUTF ( conn_id . c_str ( ) ) ) ;
}
void TryEstablishConnection ( JNIEnv * env , jobject application ,
absl : : string_view pkg , absl : : string_view cls ,
absl : : string_view conn_id ) {
std : : string method = " tryEstablishConnection " ;
std : : string type =
" (Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/ "
" lang/String;)V " ;
jobject CallStaticJavaMethodForObject ( JNIEnv * env , const std : : string & clazz ,
const std : : string & method ,
const std : : string & type ) {
jclass cl = env - > FindClass ( clazz . c_str ( ) ) ;
jclass cl = FindNativeConnectionHelper ( env ) ;
if ( cl = = nullptr ) {
gpr_log ( GPR_ERROR , " No class %s " , clazz . c_str ( ) ) ;
return nullptr ;
return ;
}
jmethodID mid = env - > GetStaticMethodID ( cl , method . c_str ( ) , type . c_str ( ) ) ;
if ( mid = = nullptr ) {
gpr_log ( GPR_ERROR , " No method id %s " , method . c_str ( ) ) ;
return nullptr ;
}
jobject object = env - > CallStaticObjectMethod ( cl , mid ) ;
if ( object = = nullptr ) {
gpr_log ( GPR_ERROR , " Got null object from Java " ) ;
return nullptr ;
}
return object ;
env - > CallStaticVoidMethod ( cl , mid , application ,
env - > NewStringUTF ( std : : string ( pkg ) . c_str ( ) ) ,
env - > NewStringUTF ( std : : string ( cls ) . c_str ( ) ) ,
env - > NewStringUTF ( std : : string ( conn_id ) . c_str ( ) ) ) ;
}
} // namespace grpc_binder