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 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_JNI_UTILS_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_JNI_UTILS_H
17 
18 #if defined(ANDROID) || defined(__ANDROID__)
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <jni.h>
23 
24 #include <functional>
25 #include <string>
26 
27 #include "absl/strings/string_view.h"
28 
29 namespace grpc_binder {
30 
31 // Finds NativeConnectionHelper Java class and caches it. This is useful because
32 // FindClass only works when there is a Java class in the call stack. Typically
33 // user might want to call this once in a place that is called from Java (ex.
34 // JNI_OnLoad) so subsequent BinderTransport code can find Java class
35 jclass FindNativeConnectionHelper(JNIEnv* env);
36 
37 jclass FindNativeConnectionHelper(
38     JNIEnv* env, std::function<void*(std::string)> class_finder);
39 
40 // Calls Java method NativeConnectionHelper.tryEstablishConnection
41 void TryEstablishConnection(JNIEnv* env, jobject application,
42                             absl::string_view pkg, absl::string_view cls,
43                             absl::string_view action_name,
44                             absl::string_view conn_id);
45 
46 void TryEstablishConnectionWithUri(JNIEnv* env, jobject application,
47                                    absl::string_view uri,
48                                    absl::string_view conn_id);
49 
50 // Calls Java method NativeConnectionHelper.isSignatureMatch.
51 // Will also return false if failed to invoke Java.
52 bool IsSignatureMatch(JNIEnv* env, jobject context, int uid1, int uid2);
53 
54 }  // namespace grpc_binder
55 
56 #endif
57 
58 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_CLIENT_JNI_UTILS_H
59