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