1 /*
2 * Copyright (c) 2023 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 "util_napi_value.h"
17
18 #include "key_event_napi.h"
19 #include "napi_constants.h"
20 #include "util_napi.h"
21
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "UtilNapiValue" };
26 } // namespace
27
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,bool value)28 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, bool value)
29 {
30 napi_value napiValue{};
31 auto status = napi_get_boolean(env, value, &napiValue);
32 CHKRR(status, "create bool", status);
33 status = napi_set_named_property(env, object, name.c_str(), napiValue);
34 CHKRR(status, "set property", status);
35 return status;
36 }
37
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,uint16_t value)38 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, uint16_t value)
39 {
40 napi_value napiValue{};
41 auto status = napi_create_uint32(env, value, &napiValue);
42 CHKRR(status, "create bool", status);
43 status = napi_set_named_property(env, object, name.c_str(), napiValue);
44 CHKRR(status, "set property", status);
45 return status;
46 }
47
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,uint32_t value)48 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, uint32_t value)
49 {
50 napi_value napiValue{};
51 auto status = napi_create_uint32(env, value, &napiValue);
52 CHKRR(status, "create uint32", status);
53 status = napi_set_named_property(env, object, name.c_str(), napiValue);
54 CHKRR(status, "set property", status);
55 return status;
56 }
57
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,int32_t value)58 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, int32_t value)
59 {
60 napi_value napiValue{};
61 auto status = napi_create_int32(env, value, &napiValue);
62 CHKRR(status, "create int32", status);
63 status = napi_set_named_property(env, object, name.c_str(), napiValue);
64 CHKRR(status, "set property", status);
65 return status;
66 }
67
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,float value)68 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, float value)
69 {
70 napi_value napiValue{};
71 auto status = napi_create_double(env, value, &napiValue);
72 CHKRR(status, "create uint32", status);
73 status = napi_set_named_property(env, object, name.c_str(), napiValue);
74 CHKRR(status, "set property", status);
75 return status;
76 }
77
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,double value)78 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, double value)
79 {
80 napi_value napiValue{};
81 auto status = napi_create_double(env, value, &napiValue);
82 CHKRR(status, "create double", status);
83 status = napi_set_named_property(env, object, name.c_str(), napiValue);
84 CHKRR(status, "set property", status);
85 return status;
86 }
87
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,int64_t value)88 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, int64_t value)
89 {
90 napi_value napiValue{};
91 auto status = napi_create_int64(env, value, &napiValue);
92 CHKRR(status, "create int64", status);
93 status = napi_set_named_property(env, object, name.c_str(), napiValue);
94 CHKRR(status, "set property", status);
95 return status;
96 }
97
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,std::string value)98 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, std::string value)
99 {
100 napi_value napiValue{};
101 auto status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &napiValue);
102 CHKRR(status, "create utf8", status);
103 status = napi_set_named_property(env, object, name.c_str(), napiValue);
104 CHKRR(status, "set property", status);
105 return status;
106 }
107
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,std::optional<KeyEvent::KeyItem> & value)108 napi_status SetNameProperty(
109 const napi_env &env, napi_value &object, const std::string &name, std::optional<KeyEvent::KeyItem> &value)
110 {
111 napi_value napiObject{};
112 auto status = napi_create_object(env, &napiObject);
113 CHECK_RETURN((status == napi_ok) && (napiObject != nullptr), "create object", status);
114
115 status = KeyEventNapi::CreateKeyItem(env, value, napiObject);
116 CHKRR(status, "create key property", status);
117 status = napi_set_named_property(env, object, name.c_str(), napiObject);
118 CHKRR(status, "set key property", status);
119 return napi_ok;
120 }
121
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,std::vector<KeyEvent::KeyItem> & value)122 napi_status SetNameProperty(
123 const napi_env &env, napi_value &object, const std::string &name, std::vector<KeyEvent::KeyItem> &value)
124 {
125 napi_value napikeyItems{};
126 auto status = napi_create_array(env, &napikeyItems);
127 CHKRR(status, "create array", status);
128 uint32_t idx = 0;
129 for (auto &keyItem : value) {
130 napi_value napiKeyItem{};
131 status = napi_create_object(env, &napiKeyItem);
132 CHECK_RETURN((status == napi_ok) && (napiKeyItem != nullptr), "create object", status);
133
134 std::optional<KeyEvent::KeyItem> opt = std::make_optional(keyItem);
135 status = KeyEventNapi::CreateKeyItem(env, opt, napiKeyItem);
136 CHKRR(status, "create key property", status);
137
138 status = napi_set_element(env, napikeyItems, idx, napiKeyItem);
139 CHKRR(status, "set element", status);
140 ++idx;
141 }
142 status = napi_set_named_property(env, object, "keys", napikeyItems);
143 CHKRR(status, "set keys property", status);
144 return napi_ok;
145 }
146
SetNameProperty(const napi_env & env,napi_value & object,const std::string & name,napi_value value)147 napi_status SetNameProperty(const napi_env &env, napi_value &object, const std::string &name, napi_value value)
148 {
149 auto status = napi_set_named_property(env, object, name.c_str(), value);
150 return status;
151 }
152
GetNamePropertyBool(const napi_env & env,const napi_value & object,const std::string & name)153 bool GetNamePropertyBool(const napi_env &env, const napi_value &object, const std::string &name)
154 {
155 napi_value napiValue = {};
156 napi_get_named_property(env, object, name.c_str(), &napiValue);
157 napi_valuetype tmpType = napi_undefined;
158 if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
159 MMI_HILOGE("Call napi_typeof failed");
160 return false;
161 }
162 bool value = false;
163 if (tmpType != napi_boolean) {
164 MMI_HILOGI("The value is not bool");
165 return value;
166 }
167
168 napi_get_value_bool(env, napiValue, &value);
169 return value;
170 }
171
GetNamePropertyString(const napi_env & env,const napi_value & object,const std::string & name)172 std::string GetNamePropertyString(const napi_env &env, const napi_value &object, const std::string &name)
173 {
174 std::string value = "";
175 napi_value napiValue = {};
176 napi_get_named_property(env, object, name.c_str(), &napiValue);
177 napi_valuetype tmpType = napi_undefined;
178 if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
179 MMI_HILOGE("Call napi_typeof failed");
180 return value;
181 }
182 if (tmpType != napi_string) {
183 MMI_HILOGI("The value is not string");
184 return value;
185 }
186
187 char tmpValue[MAX_STRING_LEN] = { 0 };
188 size_t typeLen = 0;
189 napi_get_value_string_utf8(env, napiValue, tmpValue, MAX_STRING_LEN - 1, &typeLen);
190 value = tmpValue;
191 return value;
192 }
193
GetNamePropertyInt32(const napi_env & env,const napi_value & object,const std::string & name)194 int32_t GetNamePropertyInt32(const napi_env &env, const napi_value &object, const std::string &name)
195 {
196 int32_t value = 0;
197 napi_value napiValue = {};
198 napi_get_named_property(env, object, name.c_str(), &napiValue);
199 napi_valuetype tmpType = napi_undefined;
200 if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
201 MMI_HILOGE("Call napi_typeof failed");
202 return value;
203 }
204 if (tmpType != napi_number) {
205 MMI_HILOGI("The value is not number");
206 return value;
207 }
208 napi_get_value_int32(env, napiValue, &value);
209 return value;
210 }
211
GetNamePropertyInt64(const napi_env & env,const napi_value & object,const std::string & name)212 int64_t GetNamePropertyInt64(const napi_env &env, const napi_value &object, const std::string &name)
213 {
214 int64_t value = 0;
215 napi_value napiValue = {};
216 napi_get_named_property(env, object, name.c_str(), &napiValue);
217 napi_valuetype tmpType = napi_undefined;
218 if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
219 MMI_HILOGE("Call napi_typeof failed");
220 return value;
221 }
222 if (tmpType != napi_number) {
223 MMI_HILOGI("The value is not number");
224 return value;
225 }
226 napi_get_value_int64(env, napiValue, &value);
227 return value;
228 }
229
GetNamePropertyUint32(const napi_env & env,const napi_value & object,const std::string & name)230 uint32_t GetNamePropertyUint32(const napi_env &env, const napi_value &object, const std::string &name)
231 {
232 uint32_t value = 0;
233 napi_value napiValue = {};
234 napi_get_named_property(env, object, name.c_str(), &napiValue);
235 napi_valuetype tmpType = napi_undefined;
236 if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
237 MMI_HILOGE("Call napi_typeof failed");
238 return value;
239 }
240 if (tmpType != napi_number) {
241 MMI_HILOGI("The value is not number");
242 return value;
243 }
244 napi_get_value_uint32(env, napiValue, &value);
245 return value;
246 }
247
GetNamePropertyKeyItem(const napi_env & env,const napi_value & object,const std::string & name)248 KeyEvent::KeyItem GetNamePropertyKeyItem(const napi_env &env, const napi_value &object, const std::string &name)
249 {
250 napi_value napiValue{};
251 auto status = napi_get_named_property(env, object, name.c_str(), &napiValue);
252 CHKRR(status, "get KeyItem property failed", {});
253 KeyEvent::KeyItem keyItem;
254 int32_t keyCode = GetNamePropertyInt32(env, napiValue, "code");
255 keyItem.SetKeyCode(keyCode);
256 int64_t pressedTime = GetNamePropertyInt64(env, napiValue, "pressedTime");
257 keyItem.SetDownTime(pressedTime);
258 int32_t deviceId = GetNamePropertyInt32(env, napiValue, "deviceId");
259 keyItem.SetDeviceId(deviceId);
260 return keyItem;
261 }
262
GetNamePropertyKeyItems(const napi_env & env,const napi_value & object,const std::string & name)263 std::vector<KeyEvent::KeyItem> GetNamePropertyKeyItems(
264 const napi_env &env, const napi_value &object, const std::string &name)
265 {
266 napi_value napiValue = {};
267 auto status = napi_get_named_property(env, object, name.c_str(), &napiValue);
268 CHKRR(status, "get property", {});
269
270 uint32_t length;
271 status = napi_get_array_length(env, napiValue, &length);
272 CHKRR(status, "get array length", {});
273
274 std::vector<KeyEvent::KeyItem> keyItems;
275 for (uint32_t i = 0; i < length; ++i) {
276 napi_value element = {};
277 status = napi_get_element(env, napiValue, i, &element);
278 CHECK_RETURN((status == napi_ok) && (element != nullptr), "get element", {});
279 KeyEvent::KeyItem keyItem;
280 status = KeyEventNapi::GetKeyItem(env, element, keyItem);
281 CHKRR(status, "read keyItem property", {});
282 keyItems.push_back(keyItem);
283 }
284 return keyItems;
285 }
286 } // namespace MMI
287 } // namespace OHOS