1 /*
2 * Copyright (c) 2025 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ETS_ANI_INCLUDE_STS_COMMON_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_ETS_ANI_INCLUDE_STS_COMMON_H
18
19 #include "ani.h"
20 #include <map>
21 #include <string>
22 #include <vector>
23 #include "ans_log_wrapper.h"
24
25 namespace OHOS {
26 namespace NotificationSts {
27 constexpr int32_t STR_MAX_SIZE = 204;
28 constexpr int32_t LONG_STR_MAX_SIZE = 1028;
29 constexpr int32_t COMMON_TEXT_SIZE = 3074;
30 constexpr int32_t SHORT_TEXT_SIZE = 1026;
31 std::string GetResizeStr(std::string instr, int32_t length);
32
33 bool IsUndefine(ani_env *env, const ani_object &obj);
34 ani_object CreateBoolean(ani_env *env, bool value);
35 ani_object CreateDouble(ani_env *env, ani_double value);
36 ani_object CreateInt(ani_env *env, int32_t value);
37 bool CreateDate(ani_env *env, int64_t time, ani_object &outObj);
38 bool GetDateByObject(ani_env *env, ani_object timeObj, int64_t &time);
39 ani_status GetAniStringByString(ani_env* env, const std::string str, ani_string &aniStr);
40 ani_status GetStringByAniString(ani_env *env, ani_string str, std::string &res);
41 bool GetStringArrayByAniObj(ani_env *env, const ani_object ani_obj, std::vector<std::string> &stdVString);
42 ani_object GetAniStringArrayByVectorString(ani_env *env, std::vector<std::string> &strs);
43 ani_object newArrayClass(ani_env *env, int length);
44 ani_object newRecordClass(ani_env *env);
45 ani_object ConvertArrayDoubleToAniObj(ani_env *env, const std::vector<std::int64_t> values);
46
47 ani_status GetPropertyString(ani_env *env, ani_object obj, const char *name,
48 ani_boolean &isUndefined, std::string &outStr);
49 ani_status GetPropertyBool(ani_env *env, ani_object obj, const char *name,
50 ani_boolean &isUndefined, bool &outvalue);
51 ani_status GetPropertyDouble(ani_env *env, ani_object obj, const char *name,
52 ani_boolean &isUndefined, ani_double &outvalue);
53 ani_status GetPropertyRef(ani_env *env, ani_object obj, const char *name,
54 ani_boolean &isUndefined, ani_ref &outRef);
55 ani_status GetPropertyStringArray(ani_env *env, ani_object param, const char *name,
56 ani_boolean &isUndefined, std::vector<std::string> &res);
57 ani_status GetPropertyNumberArray(ani_env *env, ani_object param, const char *name,
58 ani_boolean &isUndefined, std::vector<int64_t> &res);
59
60 void GetPropertyRefValue(ani_env *env, ani_object obj, const char *name, ani_boolean &isUndefined, ani_ref &outRef);
61
62 bool SetFieldString(ani_env *env, ani_class cls, ani_object &object,
63 const std::string fieldName, const std::string value);
64 bool SetOptionalFieldBoolean(ani_env *env, ani_class cls, ani_object &object,
65 const std::string fieldName, bool value);
66 bool SetOptionalFieldDouble(ani_env *env, ani_class cls, ani_object &object,
67 const std::string fieldName, double value);
68 bool SetOptionalFieldArrayDouble(ani_env *env, ani_class cls, ani_object &object, const std::string &fieldName,
69 const std::vector<std::int64_t> &values);
70
71 // property
72 bool SetPropertyOptionalByBoolean(ani_env *env, ani_object &object, const char *name, bool value);
73 bool SetPropertyOptionalByDouble(ani_env *env, ani_object &object, const char *name, double value);
74 bool SetPropertyOptionalByString(ani_env *env, ani_object &object, const char *name, const std::string value);
75 bool SetPropertyOptionalByInt(ani_env *env, ani_object &object, const char *name, int32_t value);
76 bool SetPropertyByRef(ani_env *env, ani_object &object, const char *name, ani_ref value);
77
78 bool CreateClassObjByClassName(ani_env *env, const char *className, ani_class &cls, ani_object &outAniObj);
79
AniBooleanToBool(ani_boolean value)80 inline bool AniBooleanToBool(ani_boolean value)
81 {
82 return value == ANI_TRUE;
83 }
84
BoolToAniBoolean(bool value)85 inline ani_boolean BoolToAniBoolean(bool value)
86 {
87 return value ? ANI_TRUE : ANI_FALSE;
88 }
89
90 template<typename valueType>
CallSetter(ani_env * env,ani_class cls,ani_object & object,const char * propertyName,valueType value)91 static bool CallSetter(ani_env* env, ani_class cls, ani_object &object, const char* propertyName, valueType value)
92 {
93 if (env == nullptr || cls == nullptr || object == nullptr) {
94 return false;
95 }
96 std::string setterName("<set>");
97 setterName.append(propertyName);
98 ani_method setter;
99 ani_status status = env->Class_FindMethod(cls, setterName.c_str(), nullptr, &setter);
100 if (status != ANI_OK) {
101 ANS_LOGE("Class_FindMethod %{public}s failed %{public}d", propertyName, status);
102 return false;
103 }
104 if constexpr (std::is_same_v<valueType, ani_byte> || std::is_same_v<valueType, ani_short> ||
105 std::is_same_v<valueType, ani_int> || std::is_same_v<valueType, uint32_t> ||
106 std::is_same_v<valueType, ani_long> || std::is_same_v<valueType, int32_t> ||
107 std::is_same_v<valueType, ani_float> || std::is_same_v<valueType, ani_double>) {
108 status = env->Object_CallMethod_Void(object, setter, static_cast<double>(value));
109 } else {
110 status = env->Object_CallMethod_Void(object, setter, value);
111 }
112 if (status != ANI_OK) {
113 ANS_LOGE("Object_CallMethod_Void %{public}s failed %{public}d", propertyName, status);
114 return false;
115 }
116 return true;
117 }
118
CallSetterNull(ani_env * env,ani_class cls,ani_object & object,const char * propertyName)119 [[maybe_unused]]static bool CallSetterNull(ani_env* env, ani_class cls, ani_object &object, const char* propertyName)
120 {
121 ani_ref nullRef = nullptr;
122 ani_status status = env->GetNull(&nullRef);
123 if (status != ANI_OK) {
124 ANS_LOGE("GetNull %{public}s failed %{public}d", propertyName, status);
125 return false;
126 }
127 return CallSetter(env, cls, object, propertyName, nullRef);
128 }
129
130 template <class T>
EnumConvertAniToNative(ani_env * env,ani_enum_item enumItem,T & result)131 static bool EnumConvertAniToNative(ani_env *env, ani_enum_item enumItem, T &result)
132 {
133 ani_status status = ANI_ERROR;
134 if constexpr (std::is_enum<T>::value || std::is_integral<T>::value) {
135 ani_int intValue{};
136 status = env->EnumItem_GetValue_Int(enumItem, &intValue);
137 if (ANI_OK != status) {
138 ANS_LOGD("EnumConvert_StsToNative failed, status : %{public}d", status);
139 return false;
140 }
141 result = static_cast<T>(intValue);
142 return true;
143 } else if constexpr (std::is_same<T, std::string>::value) {
144 ani_string strValue{};
145 status = env->EnumItem_GetValue_String(enumItem, &strValue);
146 if (ANI_OK != status) {
147 ANS_LOGD("EnumItem_GetValue_String failed, status : %{public}d", status);
148 return false;
149 }
150 status = GetStdString(env, strValue, result);
151 if (ANI_OK != status) {
152 ANS_LOGD("EnumConvertAniToNative GetStdString failed, status : %{public}d", status);
153 return false;
154 }
155 } else {
156 ANS_LOGD("Enum convert failed: type not supported");
157 return false;
158 }
159 }
160
161 template<class T>
deletePoint(T & result)162 void deletePoint(T &result)
163 {
164 delete result;
165 result = nullptr;
166 }
167
168 template<class T>
deleteVectorWithPoints(std::vector<T> & results)169 void deleteVectorWithPoints(std::vector<T> &results)
170 {
171 for (auto result : results) {
172 deletePoint(result);
173 }
174 results.clear();
175 }
176
177 template<class T>
deleteVectorWithSpPoints(std::vector<std::shared_ptr<T>> & results)178 void deleteVectorWithSpPoints(std::vector<std::shared_ptr<T>> &results)
179 {
180 for (auto result : results) {
181 result = nullptr;
182 }
183 results.clear();
184 }
185
186 template<class T>
deleteVectorWithArraySpPoints(std::map<std::string,std::vector<std::shared_ptr<T>>> & results)187 void deleteVectorWithArraySpPoints(std::map<std::string, std::vector<std::shared_ptr<T>>> &results)
188 {
189 for (auto it = results.begin(); it != results.end(); ++it) {
190 auto vt = static_cast<std::vector<std::shared_ptr<T>>>(it -> second);
191 for (auto pt : vt) {
192 pt = nullptr;
193 }
194 vt.clear();
195 }
196 results.clear();
197 }
198
199 template<class T>
EnumConvertAniToNative(ani_env * env,ani_object enumItem,T & result)200 static bool EnumConvertAniToNative(ani_env *env, ani_object enumItem, T &result)
201 {
202 return EnumConvertAniToNative<T>(env, static_cast<ani_enum_item>(enumItem), result);
203 }
204
205 template <class T>
EnumConvertNativeToAni(ani_env * env,const char * enumName,const T enumValue,ani_enum_item & result)206 static bool EnumConvertNativeToAni(ani_env *env, const char *enumName, const T enumValue, ani_enum_item &result)
207 {
208 ani_enum aniEnum{};
209 ani_status status = env->FindEnum(enumName, &aniEnum);
210 if (ANI_OK != status) {
211 ANS_LOGD("Enum convert FindEnum failed: %{public}s status: %{public}d", enumName, status);
212 return false;
213 }
214 constexpr int32_t loopMaxNum = 1000;
215 for (int32_t index = 0U; index < loopMaxNum; index++) {
216 ani_enum_item enumItem{};
217 status = env->Enum_GetEnumItemByIndex(aniEnum, index, &enumItem);
218 if (ANI_OK != status) {
219 ANS_LOGD(
220 "Enum_GetEnumItemByIndex failed: enumName:%{public}s index:%{public}d, status:%{public}d",
221 enumName, index, status);
222 return false;
223 }
224 // compare value
225 T tmpValue{};
226 if (EnumConvertAniToNative<T>(env, enumItem, tmpValue) && tmpValue == enumValue) {
227 result = enumItem;
228 return true;
229 }
230 }
231 ANS_LOGD("EnumConvert_NativeToSts failed enumName: %{public}s", enumName);
232 return false;
233 }
234
235 } // namespace NotificationSts
236 } // OHOS
237 #endif