• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "js_input_monitor_util.h"
17 
18 #include <cinttypes>
19 
20 #include "define_multimodal.h"
21 #include "napi_constants.h"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsInputMonitorUtil" };
27 } // namespace
28 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,bool value)29 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, bool value)
30 {
31     napi_status status;
32     napi_value napiValue = nullptr;
33     status = napi_create_int32(env, value, &napiValue);
34     if (status != napi_ok) {
35         MMI_HILOGE("%{public}s=%{public}d failed", name.c_str(), value);
36         return status;
37     }
38     status = napi_set_named_property(env, object, name.c_str(), napiValue);
39     return status;
40 }
41 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,uint16_t value)42 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, uint16_t value)
43 {
44     napi_status status;
45     napi_value napiValue = nullptr;
46     status = napi_create_uint32(env, value, &napiValue);
47     if (status != napi_ok) {
48         MMI_HILOGE("%{public}s=%{public}u failed", name.c_str(), value);
49         return status;
50     }
51     status = napi_set_named_property(env, object, name.c_str(), napiValue);
52     return status;
53 }
54 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,uint32_t value)55 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, uint32_t value)
56 {
57     napi_status status;
58     napi_value napiValue = nullptr;
59     status = napi_create_uint32(env, value, &napiValue);
60     if (status != napi_ok) {
61         MMI_HILOGE("%{public}s=%{public}u failed", name.c_str(), value);
62         return status;
63     }
64     status = napi_set_named_property(env, object, name.c_str(), napiValue);
65     return status;
66 }
67 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,int32_t value)68 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, int32_t value)
69 {
70     napi_status status;
71     napi_value napiValue = nullptr;
72     status = napi_create_int32(env, value, &napiValue);
73     if (status != napi_ok) {
74         MMI_HILOGE("%{public}s=%{public}d failed", name.c_str(), value);
75         return status;
76     }
77     status = napi_set_named_property(env, object, name.c_str(), napiValue);
78     return status;
79 }
80 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,float value)81 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, float value)
82 {
83     napi_status status;
84     napi_value napiValue = nullptr;
85     status = napi_create_double(env, value, &napiValue);
86     if (status != napi_ok) {
87         MMI_HILOGE("%{public}s=%{public}f failed", name.c_str(), value);
88         return status;
89     }
90     status = napi_set_named_property(env, object, name.c_str(), napiValue);
91     return status;
92 }
93 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,double value)94 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, double value)
95 {
96     napi_status status;
97     napi_value napiValue = nullptr;
98     status = napi_create_double(env, value, &napiValue);
99     if (status != napi_ok) {
100         MMI_HILOGE("%{public}s=%{public}lf failed", name.c_str(), value);
101         return status;
102     }
103     status = napi_set_named_property(env, object, name.c_str(), napiValue);
104     return status;
105 }
106 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,int64_t value)107 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, int64_t value)
108 {
109     napi_status status;
110     napi_value napiValue = nullptr;
111     status = napi_create_int64(env, value, &napiValue);
112     if (status != napi_ok) {
113         MMI_HILOGE("%{public}s=%{public}" PRId64 " failed", name.c_str(), value);
114         return status;
115     }
116     status = napi_set_named_property(env, object, name.c_str(), napiValue);
117     return status;
118 }
119 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,std::string value)120 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, std::string value)
121 {
122     napi_status status;
123     napi_value napiValue = nullptr;
124     status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &napiValue);
125     if (status != napi_ok) {
126         MMI_HILOGE("%{public}s=%{public}s failed", name.c_str(), value.c_str());
127         return status;
128     }
129     status = napi_set_named_property(env, object, name.c_str(), napiValue);
130     return status;
131 }
132 
SetNameProperty(const napi_env & env,napi_value object,const std::string & name,napi_value value)133 napi_status SetNameProperty(const napi_env& env, napi_value object, const std::string& name, napi_value value)
134 {
135     auto status = napi_set_named_property(env, object, name.c_str(), value);
136     return status;
137 }
138 
GetNamePropertyBool(const napi_env & env,const napi_value & object,const std::string & name)139 bool GetNamePropertyBool(const napi_env& env, const napi_value& object, const std::string& name)
140 {
141     napi_value napiValue = {};
142     napi_get_named_property(env, object, name.c_str(), &napiValue);
143     napi_valuetype tmpType = napi_undefined;
144     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
145         MMI_HILOGE("Call napi_typeof failed");
146         return false;
147     }
148     bool value = false;
149     if (tmpType != napi_boolean) {
150         MMI_HILOGI("The value is not bool");
151         return value;
152     }
153 
154     napi_get_value_bool(env, napiValue, &value);
155     return value;
156 }
157 
GetNamePropertyString(const napi_env & env,const napi_value & object,const std::string & name)158 std::string GetNamePropertyString(const napi_env& env, const napi_value& object, const std::string& name)
159 {
160     std::string value = "";
161     napi_value napiValue = {};
162     napi_get_named_property(env, object, name.c_str(), &napiValue);
163     napi_valuetype tmpType = napi_undefined;
164     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
165         MMI_HILOGE("Call napi_typeof failed");
166         return value;
167     }
168     if (tmpType != napi_string) {
169         MMI_HILOGI("The value is not string");
170         return value;
171     }
172 
173     char tmpValue[MAX_STRING_LEN] = { 0 };
174     size_t typeLen = 0;
175     napi_get_value_string_utf8(env, napiValue, tmpValue, MAX_STRING_LEN - 1, &typeLen);
176     value = tmpValue;
177     return value;
178 }
179 
GetNamePropertyInt32(const napi_env & env,const napi_value & object,const std::string & name)180 int32_t GetNamePropertyInt32(const napi_env& env, const napi_value& object, const std::string& name)
181 {
182     int32_t value = 0;
183     napi_value napiValue = {};
184     napi_get_named_property(env, object, name.c_str(), &napiValue);
185     napi_valuetype tmpType = napi_undefined;
186     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
187         MMI_HILOGE("Call napi_typeof failed");
188         return value;
189     }
190     if (tmpType != napi_number) {
191         MMI_HILOGI("The value is not number");
192         return value;
193     }
194     napi_get_value_int32(env, napiValue, &value);
195     return value;
196 }
197 
GetNamePropertyInt64(const napi_env & env,const napi_value & object,const std::string & name)198 int64_t GetNamePropertyInt64(const napi_env& env, const napi_value& object, const std::string& name)
199 {
200     int64_t value = 0;
201     napi_value napiValue = {};
202     napi_get_named_property(env, object, name.c_str(), &napiValue);
203     napi_valuetype tmpType = napi_undefined;
204     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
205         MMI_HILOGE("Call napi_typeof failed");
206         return value;
207     }
208     if (tmpType != napi_number) {
209         MMI_HILOGI("The value is not number");
210         return value;
211     }
212     napi_get_value_int64(env, napiValue, &value);
213     return value;
214 }
215 
GetNamePropertyUint32(const napi_env & env,const napi_value & object,const std::string & name)216 uint32_t GetNamePropertyUint32(const napi_env& env, const napi_value& object, const std::string& name)
217 {
218     uint32_t value = 0;
219     napi_value napiValue = {};
220     napi_get_named_property(env, object, name.c_str(), &napiValue);
221     napi_valuetype tmpType = napi_undefined;
222     if (napi_typeof(env, napiValue, &tmpType) != napi_ok) {
223         MMI_HILOGE("Call napi_typeof failed");
224         return value;
225     }
226     if (tmpType != napi_number) {
227         MMI_HILOGI("The value is not number");
228         return value;
229     }
230     napi_get_value_uint32(env, napiValue, &value);
231     return value;
232 }
233 } // namespace MMI
234 } // namespace OHOS
235