jni_utils.cc
Go to the documentation of this file.
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 
16 
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
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,
68  absl::string_view action_name,
69  absl::string_view conn_id) {
70  std::string method = "tryEstablishConnection";
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 bool IsSignatureMatch(JNIEnv* env, jobject context, int uid1, int uid2) {
93  const std::string method = "isSignatureMatch";
94  const std::string type = "(Landroid/content/Context;II)Z";
95 
96  jclass cl = FindNativeConnectionHelper(env);
97  if (cl == nullptr) {
98  return false;
99  }
100 
101  jmethodID mid = env->GetStaticMethodID(cl, method.c_str(), type.c_str());
102  if (mid == nullptr) {
103  gpr_log(GPR_ERROR, "No method id %s", method.c_str());
104  }
105 
106  jboolean result = env->CallStaticBooleanMethod(cl, mid, context, uid1, uid2);
107  return result == JNI_TRUE;
108 }
109 
110 } // namespace grpc_binder
111 
112 #endif
113 #endif
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
log.h
generate.env
env
Definition: generate.py:37
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_binder
Definition: connection_id_generator.cc:45
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
jni_utils.h
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
env
Definition: env.py:1
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
method
NSString * method
Definition: ProtoMethod.h:28
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:24