1/*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
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
16#ifndef OHOS_IPC_JNI_HELPERS_H
17#define OHOS_IPC_JNI_HELPERS_H
18
19#include <jni.h>
20
21namespace OHOS {
22class JNIEnvHelper {
23public:
24    JNIEnvHelper();
25    ~JNIEnvHelper();
26    JNIEnv *Get();
27    JNIEnv *operator->();
28    static void nativeInit(JavaVM *vm);
29
30private:
31    JNIEnvHelper(const JNIEnvHelper &) = delete;
32    JNIEnvHelper &operator = (const JNIEnvHelper &) = delete;
33
34private:
35    JNIEnv *env_;
36    bool nativeThread_;
37    static JavaVM *javaVm_;
38};
39void JniHelperThrowException(JNIEnv *env, const char *className, const char *msg);
40void JniHelperThrowNullPointerException(JNIEnv *env, const char *msg);
41void JniHelperThrowIllegalStateException(JNIEnv *env, const char *msg);
42
43
44/*
45 * JNI may throws exception when native code call java scenario.
46 * we should convert local exception to native status code.
47 * Check and clear local exception.
48 */
49jboolean JniHelperCheckAndClearLocalException(JNIEnv *env);
50
51/*
52 * Get an int file descriptor from a java.io.FileDescriptor
53 */
54int JniHelperJavaIoGetFdFromFileDescriptor(JNIEnv *env, jobject fileDescriptor);
55
56/*
57 * Set the descriptor of a java.io.FileDescriptor
58 */
59void JniHelperJavaIoSetFdToFileDescriptor(JNIEnv *env, jobject fileDescriptor, int value);
60
61/*
62 * Create a java.io.FileDescriptor given an integer fd
63 */
64jobject JniHelperJavaIoCreateFileDescriptor(JNIEnv *env, int fd);
65
66int JniHelperRegisterNativeMethods(JNIEnv *env);
67} // namespace OHOS
68#endif // OHOS_IPC_JNI_HELPERS_H
69