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