| 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/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/ext/transport/binder/client/jni_utils.h" |
| 18 |
|
| 19 |
#ifndef GRPC_NO_BINDER |
| 20 |
|
| 21 |
#include <grpc/support/log.h> |
| 22 |
|
| 23 |
#if defined(ANDROID) || defined(__ANDROID__) |
| 24 |
|
| 25 |
namespace grpc_binder { |
| 26 |
|
| 27 |
jclass FindNativeConnectionHelper(JNIEnv* env) { |
| 28 |
return FindNativeConnectionHelper( |
| 29 |
env, [env](std::string cl) { return env->FindClass(cl.c_str()); }); |
| 30 |
} |
| 31 |
|
| 32 |
jclass FindNativeConnectionHelper( |
| 33 |
JNIEnv* env, std::function<void*(std::string)> class_finder) { |
| 34 |
auto do_find = [env, class_finder]() { |
| 35 |
jclass cl = static_cast<jclass>( |
| 36 |
class_finder("io/grpc/binder/cpp/NativeConnectionHelper")); |
| 37 |
if (cl == nullptr) { |
| 38 |
return cl; |
| 39 |
} |
| 40 |
jclass global_cl = static_cast<jclass>(env->NewGlobalRef(cl)); |
| 41 |
env->DeleteLocalRef(cl); |
| 42 |
GPR_ASSERT(global_cl != nullptr); |
| 43 |
return global_cl; |
| 44 |
}; |
| 45 |
static jclass connection_helper_class = do_find(); |
| 46 |
if (connection_helper_class != nullptr) { |
| 47 |
return connection_helper_class; |
| 48 |
} |
| 49 |
// Some possible reasons: |
| 50 |
// * There is no Java class in the call stack and this is not invoked |
| 51 |
// from JNI_OnLoad |
| 52 |
// * The APK does not correctly depends on the helper class, or the |
| 53 |
// class get shrinked |
| 54 |
gpr_log(GPR_ERROR, |
| 55 |
"Cannot find binder transport Java helper class. Did you invoke " |
| 56 |
"grpc::experimental::InitializeBinderChannelJavaClass correctly " |
| 57 |
"beforehand? Did the APK correctly include the connection helper " |
| 58 |
"class (i.e depends on build target " |
| 59 |
"src/core/ext/transport/binder/java/io/grpc/binder/" |
| 60 |
"cpp:connection_helper) ?"); |
| 61 |
// TODO(mingcl): Maybe it is worth to try again so the failure can be fixed |
| 62 |
// by invoking this function again at a different thread. |
| 63 |
return nullptr; |
| 64 |
} |
| 65 |
|
| 66 |
void TryEstablishConnection(JNIEnv* env, jobject application, |
| 67 |
absl::string_view pkg, absl::string_view cls, |
| 68 |
absl::string_view action_name, |
| 69 |
absl::string_view conn_id) { |
| 70 |
std::string method = "tryEstablishConnection"; |
| 71 |
std::string type = |
| 72 |
"(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/" |
| 73 |
"lang/String;Ljava/lang/String;)V"; |
| 74 |
|
| 75 |
jclass cl = FindNativeConnectionHelper(env); |
| 76 |
if (cl == nullptr) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
jmethodID mid = env->GetStaticMethodID(cl, method.c_str(), type.c_str()); |
| 81 |
if (mid == nullptr) { |
| 82 |
gpr_log(GPR_ERROR, "No method id %s", method.c_str()); |
| 83 |
} |
| 84 |
|
| 85 |
env->CallStaticVoidMethod(cl, mid, application, |
| 86 |
env->NewStringUTF(std::string(pkg).c_str()), |
| 87 |
env->NewStringUTF(std::string(cls).c_str()), |
| 88 |
env->NewStringUTF(std::string(action_name).c_str()), |
| 89 |
env->NewStringUTF(std::string(conn_id).c_str())); |
| 90 |
} |
| 91 |
|
| 92 |
void TryEstablishConnectionWithUri(JNIEnv* env, jobject application, |
| 93 |
absl::string_view uri, |
| 94 |
absl::string_view conn_id) { |
| 95 |
std::string method = "tryEstablishConnectionWithUri"; |
| 96 |
std::string type = |
| 97 |
"(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)V"; |
| 98 |
|
| 99 |
jclass cl = FindNativeConnectionHelper(env); |
| 100 |
if (cl == nullptr) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
jmethodID mid = env->GetStaticMethodID(cl, method.c_str(), type.c_str()); |
| 105 |
if (mid == nullptr) { |
| 106 |
gpr_log(GPR_ERROR, "No method id %s", method.c_str()); |
| 107 |
} |
| 108 |
|
| 109 |
env->CallStaticVoidMethod(cl, mid, application, |
| 110 |
env->NewStringUTF(std::string(uri).c_str()), |
| 111 |
env->NewStringUTF(std::string(conn_id).c_str())); |
| 112 |
} |
| 113 |
|
| 114 |
bool IsSignatureMatch(JNIEnv* env, jobject context, int uid1, int uid2) { |
| 115 |
const std::string method = "isSignatureMatch"; |
| 116 |
const std::string type = "(Landroid/content/Context;II)Z"; |
| 117 |
|
| 118 |
jclass cl = FindNativeConnectionHelper(env); |
| 119 |
if (cl == nullptr) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
jmethodID mid = env->GetStaticMethodID(cl, method.c_str(), type.c_str()); |
| 124 |
if (mid == nullptr) { |
| 125 |
gpr_log(GPR_ERROR, "No method id %s", method.c_str()); |
| 126 |
} |
| 127 |
|
| 128 |
jboolean result = env->CallStaticBooleanMethod(cl, mid, context, uid1, uid2); |
| 129 |
return result == JNI_TRUE; |
| 130 |
} |
| 131 |
|
| 132 |
} // namespace grpc_binder |
| 133 |
|
| 134 |
#endif |
| 135 |
#endif |