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 #include "ohos_rpc_message_option.h"
17 #include "ipc_debug.h"
18 #include "log_tags.h"
19 
20 namespace OHOS {
21 struct JMessageOption {
22     jclass klazz;
23     jfieldID flagsField;
24     jfieldID waitTimeField;
25     jmethodID initMethod;
26 } g_jMessageOption;
27 
28 static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC_OTHER, "IPCJni" };
29 /*
30  * Get flags field from ohos.rpc.MessageOption.
31  */
JavaOhosRpcMessageOptionGetFlags(JNIEnv * env,jobject object)32 int JavaOhosRpcMessageOptionGetFlags(JNIEnv *env, jobject object)
33 {
34     ZLOGD(LABEL, "enter");
35     if ((g_jMessageOption.flagsField != nullptr) && (env != nullptr)) {
36         return env->GetIntField(object, g_jMessageOption.flagsField);
37     }
38     return JNI_OK;
39 }
40 
41 /*
42  * Get wait time field from ohos.rpc.MessageOption.
43  */
JavaOhosRpcMessageOptionGetWaitTime(JNIEnv * env,jobject object)44 int JavaOhosRpcMessageOptionGetWaitTime(JNIEnv *env, jobject object)
45 {
46     ZLOGD(LABEL, "enter");
47     if ((g_jMessageOption.waitTimeField != nullptr) && (env != nullptr)) {
48         return env->GetIntField(object, g_jMessageOption.waitTimeField);
49     }
50     return JNI_OK;
51 }
52 
53 /*
54  * Set flags to ohos.rpc.MessageOption
55  */
JavaOhosRpcMessageOptionSetFlags(JNIEnv * env,jobject object,int flags)56 void JavaOhosRpcMessageOptionSetFlags(JNIEnv *env, jobject object, int flags)
57 {
58     ZLOGD(LABEL, "enter");
59     if (env != nullptr) {
60         env->SetIntField(object, g_jMessageOption.flagsField, flags);
61     }
62 }
63 
64 /*
65  * Set wait time to ohos.rpc.MessageOption
66  */
JavaOhosRpcMessageOptionSetWaitTime(JNIEnv * env,jobject object,int waitTime)67 void JavaOhosRpcMessageOptionSetWaitTime(JNIEnv *env, jobject object, int waitTime)
68 {
69     ZLOGD(LABEL, "waitTime:%{public}s", waitTime);
70     if (env != nullptr) {
71         env->SetIntField(object, g_jMessageOption.waitTimeField, waitTime);
72     }
73 }
74 
75 /*
76  * Create java object for ohos.rpc.MessageOption.
77  */
JavaOhosRpcMessageOptionNewJavaObject(JNIEnv * env,int flags,int waitTime)78 jobject JavaOhosRpcMessageOptionNewJavaObject(JNIEnv *env, int flags, int waitTime)
79 {
80     ZLOGD(LABEL, "enter");
81     jobject option = nullptr;
82     if (env != nullptr) {
83         option = env->NewObject(g_jMessageOption.klazz, g_jMessageOption.initMethod, flags, waitTime);
84     }
85     return option;
86 }
87 
88 /*
89  * Get and create native instance of ohos.rpc.MessageOption.
90  */
JavaOhosRpcMessageOptionGetNative(JNIEnv * env,jobject object)91 MessageOptionPtr JavaOhosRpcMessageOptionGetNative(JNIEnv *env, jobject object)
92 {
93     ZLOGD(LABEL, "enter");
94     int flags = JavaOhosRpcMessageOptionGetFlags(env, object);
95     int waitTime = JavaOhosRpcMessageOptionGetWaitTime(env, object);
96     auto option = std::make_shared<MessageOption>();
97     option->SetFlags(flags);
98     option->SetWaitTime(waitTime);
99     return option;
100 }
101 
102 /*
103  * register native methods fopr ohos.rpc.MessageOption.
104  */
JavaOhosRpcMessageOptionRegisterNativeMethods(JNIEnv * env)105 int JavaOhosRpcMessageOptionRegisterNativeMethods(JNIEnv *env)
106 {
107     ZLOGD(LABEL, "enter");
108     if (env == nullptr) {
109         return JNI_ERR;
110     }
111     jclass klazz = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("ohos/rpc/MessageOption")));
112     if (klazz == nullptr) {
113         ZLOGE(LABEL, "could not find class for MessageOption");
114         return JNI_ERR;
115     }
116     g_jMessageOption.klazz = reinterpret_cast<jclass>(env->NewGlobalRef(klazz));
117     g_jMessageOption.initMethod = env->GetMethodID(g_jMessageOption.klazz, "<init>", "(II)V");
118     if (g_jMessageOption.initMethod == nullptr) {
119         ZLOGE(LABEL, "could not get initMethod from MessageOption");
120         if (g_jMessageOption.klazz != nullptr) {
121             env->DeleteGlobalRef(g_jMessageOption.klazz);
122         }
123         env->DeleteGlobalRef(klazz);
124         return JNI_ERR;
125     }
126 
127     g_jMessageOption.flagsField = env->GetFieldID(g_jMessageOption.klazz, "mFlags", "I");
128     if (g_jMessageOption.flagsField == nullptr) {
129         ZLOGE(LABEL, "could not get flags fields from MessageOption");
130         if (g_jMessageOption.klazz != nullptr) {
131             env->DeleteGlobalRef(g_jMessageOption.klazz);
132         }
133         env->DeleteGlobalRef(klazz);
134         return JNI_ERR;
135     }
136 
137     g_jMessageOption.waitTimeField = env->GetFieldID(g_jMessageOption.klazz, "mWaitTime", "I");
138     if (g_jMessageOption.waitTimeField == nullptr) {
139         ZLOGE(LABEL, "could not get mWaitTime fields from MessageOption");
140         if (g_jMessageOption.klazz != nullptr) {
141             env->DeleteGlobalRef(g_jMessageOption.klazz);
142         }
143         env->DeleteGlobalRef(klazz);
144         return JNI_ERR;
145     }
146     return JNI_OK;
147 }
148 } // namespace OHOS