• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "napi_print_utils.h"
17 
18 #include "ability.h"
19 #include "napi_base_context.h"
20 #include "print_constant.h"
21 #include "print_log.h"
22 #include "securec.h"
23 
24 namespace OHOS::Print {
25 static constexpr const int MAX_STRING_LENGTH = 65536;
GetValueType(napi_env env,napi_value value)26 napi_valuetype NapiPrintUtils::GetValueType(napi_env env, napi_value value)
27 {
28     if (value == nullptr) {
29         return napi_undefined;
30     }
31 
32     napi_valuetype valueType = napi_undefined;
33     PRINT_CALL_BASE(env, napi_typeof(env, value, &valueType), napi_undefined);
34     return valueType;
35 }
36 
37 /* named property */
HasNamedProperty(napi_env env,napi_value object,const std::string & propertyName)38 bool NapiPrintUtils::HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
39 {
40     bool hasProperty = false;
41     PRINT_CALL_BASE(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty), false);
42     return hasProperty;
43 }
44 
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)45 napi_value NapiPrintUtils::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
46 {
47     napi_value value = nullptr;
48     bool hasProperty = false;
49     PRINT_CALL(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty));
50     if (!hasProperty) {
51         return value;
52     }
53     PRINT_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
54     return value;
55 }
56 
SetNamedProperty(napi_env env,napi_value object,const std::string & name,napi_value value)57 void NapiPrintUtils::SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value)
58 {
59     (void)napi_set_named_property(env, object, name.c_str(), value);
60 }
61 
GetPropertyNames(napi_env env,napi_value object)62 std::vector<std::string> NapiPrintUtils::GetPropertyNames(napi_env env, napi_value object)
63 {
64     std::vector<std::string> ret;
65     napi_value names = nullptr;
66     PRINT_CALL_BASE(env, napi_get_property_names(env, object, &names), ret);
67     uint32_t length = 0;
68     PRINT_CALL_BASE(env, napi_get_array_length(env, names, &length), ret);
69     for (uint32_t index = 0; index < length; ++index) {
70         napi_value name = nullptr;
71         if (napi_get_element(env, names, index, &name) != napi_ok) {
72             continue;
73         }
74         if (GetValueType(env, name) != napi_string) {
75             continue;
76         }
77         ret.emplace_back(GetStringFromValueUtf8(env, name));
78     }
79     return ret;
80 }
81 
82 /* UINT32 */
CreateUint32(napi_env env,uint32_t code)83 napi_value NapiPrintUtils::CreateUint32(napi_env env, uint32_t code)
84 {
85     napi_value value = nullptr;
86     if (napi_create_uint32(env, code, &value) != napi_ok) {
87         return nullptr;
88     }
89     return value;
90 }
91 
GetUint32FromValue(napi_env env,napi_value value)92 uint32_t NapiPrintUtils::GetUint32FromValue(napi_env env, napi_value value)
93 {
94     uint32_t ret = 0;
95     PRINT_CALL_BASE(env, napi_get_value_uint32(env, value, &ret), 0);
96     return ret;
97 }
98 
GetUint32Property(napi_env env,napi_value object,const std::string & propertyName)99 uint32_t NapiPrintUtils::GetUint32Property(napi_env env, napi_value object, const std::string &propertyName)
100 {
101     if (!HasNamedProperty(env, object, propertyName)) {
102         return 0;
103     }
104     napi_value value = GetNamedProperty(env, object, propertyName);
105     return GetUint32FromValue(env, value);
106 }
107 
SetUint32Property(napi_env env,napi_value object,const std::string & name,uint32_t value)108 void NapiPrintUtils::SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value)
109 {
110     napi_value jsValue = CreateUint32(env, value);
111     if (GetValueType(env, jsValue) != napi_number) {
112         return;
113     }
114 
115     napi_set_named_property(env, object, name.c_str(), jsValue);
116 }
117 
118 /* INT32 */
CreateInt32(napi_env env,int32_t code)119 napi_value NapiPrintUtils::CreateInt32(napi_env env, int32_t code)
120 {
121     napi_value value = nullptr;
122     if (napi_create_int32(env, code, &value) != napi_ok) {
123         return nullptr;
124     }
125     return value;
126 }
127 
GetInt32FromValue(napi_env env,napi_value value)128 int32_t NapiPrintUtils::GetInt32FromValue(napi_env env, napi_value value)
129 {
130     int32_t ret = 0;
131     PRINT_CALL_BASE(env, napi_get_value_int32(env, value, &ret), 0);
132     return ret;
133 }
134 
GetInt32Property(napi_env env,napi_value object,const std::string & propertyName)135 int32_t NapiPrintUtils::GetInt32Property(napi_env env, napi_value object, const std::string &propertyName)
136 {
137     if (!HasNamedProperty(env, object, propertyName)) {
138         return 0;
139     }
140     napi_value value = GetNamedProperty(env, object, propertyName);
141     return GetInt32FromValue(env, value);
142 }
143 
SetInt32Property(napi_env env,napi_value object,const std::string & name,int32_t value)144 void NapiPrintUtils::SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value)
145 {
146     napi_value jsValue = CreateInt32(env, value);
147     if (GetValueType(env, jsValue) != napi_number) {
148         return;
149     }
150 
151     napi_set_named_property(env, object, name.c_str(), jsValue);
152 }
153 
154 /* String UTF8 */
CreateStringUtf8(napi_env env,const std::string & str)155 napi_value NapiPrintUtils::CreateStringUtf8(napi_env env, const std::string &str)
156 {
157     napi_value value = nullptr;
158     if (napi_create_string_utf8(env, str.c_str(), strlen(str.c_str()), &value) != napi_ok) {
159         return nullptr;
160     }
161     return value;
162 }
163 
GetStringFromValueUtf8(napi_env env,napi_value value)164 std::string NapiPrintUtils::GetStringFromValueUtf8(napi_env env, napi_value value)
165 {
166     std::string result;
167     std::vector<char> str(MAX_STRING_LENGTH + 1, '\0');
168     size_t length = 0;
169     PRINT_CALL_BASE(env, napi_get_value_string_utf8(env, value, &str[0], MAX_STRING_LENGTH, &length), result);
170     if (length > 0) {
171         return result.append(&str[0], length);
172     }
173     return result;
174 }
175 
GetStringPropertyUtf8(napi_env env,napi_value object,const std::string & propertyName)176 std::string NapiPrintUtils::GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName)
177 {
178     if (!HasNamedProperty(env, object, propertyName)) {
179         return "";
180     }
181     napi_value value = GetNamedProperty(env, object, propertyName);
182     return GetStringFromValueUtf8(env, value);
183 }
184 
SetStringPropertyUtf8(napi_env env,napi_value object,const std::string & name,const std::string & value)185 void NapiPrintUtils::SetStringPropertyUtf8(
186     napi_env env, napi_value object, const std::string &name, const std::string &value)
187 {
188     napi_value jsValue = CreateStringUtf8(env, value);
189     if (GetValueType(env, jsValue) != napi_string) {
190         return;
191     }
192     napi_set_named_property(env, object, name.c_str(), jsValue);
193 }
194 
195 /* array buffer */
196 
CreateArrayBuffer(napi_env env,size_t length,void ** data)197 napi_value NapiPrintUtils::CreateArrayBuffer(napi_env env, size_t length, void **data)
198 {
199     napi_value object = nullptr;
200     PRINT_CALL(env, napi_create_arraybuffer(env, length, data, &object));
201     return object;
202 }
203 
ValueIsArrayBuffer(napi_env env,napi_value value)204 bool NapiPrintUtils::ValueIsArrayBuffer(napi_env env, napi_value value)
205 {
206     bool isArrayBuffer = false;
207     PRINT_CALL_BASE(env, napi_is_arraybuffer(env, value, &isArrayBuffer), false);
208     return isArrayBuffer;
209 }
210 
GetInfoFromArrayBufferValue(napi_env env,napi_value value,size_t * length)211 void *NapiPrintUtils::GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length)
212 {
213     if (length == nullptr) {
214         return nullptr;
215     }
216 
217     void *data = nullptr;
218     PRINT_CALL(env, napi_get_arraybuffer_info(env, value, &data, length));
219     return data;
220 }
221 
222 /* object */
CreateObject(napi_env env)223 napi_value NapiPrintUtils::CreateObject(napi_env env)
224 {
225     napi_value object = nullptr;
226     PRINT_CALL(env, napi_create_object(env, &object));
227     return object;
228 }
229 
230 /* undefined */
GetUndefined(napi_env env)231 napi_value NapiPrintUtils::GetUndefined(napi_env env)
232 {
233     napi_value undefined = nullptr;
234     PRINT_CALL(env, napi_get_undefined(env, &undefined));
235     return undefined;
236 }
237 
238 /* function */
CallFunction(napi_env env,napi_value recv,napi_value func,size_t argc,const napi_value * argv)239 napi_value NapiPrintUtils::CallFunction(
240     napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv)
241 {
242     napi_value res = nullptr;
243     PRINT_CALL(env, napi_call_function(env, recv, func, argc, argv, &res));
244     return res;
245 }
246 
CreateFunction(napi_env env,const std::string & name,napi_callback func,void * arg)247 napi_value NapiPrintUtils::CreateFunction
248     (napi_env env, const std::string &name, napi_callback func, void *arg)
249 {
250     napi_value res = nullptr;
251     PRINT_CALL(env, napi_create_function(env, name.c_str(), NAPI_AUTO_LENGTH, func, arg, &res));
252     return res;
253 }
254 
255 /* reference */
CreateReference(napi_env env,napi_value callback)256 napi_ref NapiPrintUtils::CreateReference(napi_env env, napi_value callback)
257 {
258     napi_ref callbackRef = nullptr;
259     PRINT_CALL(env, napi_create_reference(env, callback, 1, &callbackRef));
260     return callbackRef;
261 }
262 
GetReference(napi_env env,napi_ref callbackRef)263 napi_value NapiPrintUtils::GetReference(napi_env env, napi_ref callbackRef)
264 {
265     napi_value callback = nullptr;
266     PRINT_CALL(env, napi_get_reference_value(env, callbackRef, &callback));
267     return callback;
268 }
269 
DeleteReference(napi_env env,napi_ref callbackRef)270 void NapiPrintUtils::DeleteReference(napi_env env, napi_ref callbackRef)
271 {
272     (void)napi_delete_reference(env, callbackRef);
273 }
274 
275 /* boolean */
GetBooleanProperty(napi_env env,napi_value object,const std::string & propertyName)276 bool NapiPrintUtils::GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName)
277 {
278     if (!HasNamedProperty(env, object, propertyName)) {
279         return false;
280     }
281     napi_value value = GetNamedProperty(env, object, propertyName);
282     bool ret = false;
283     PRINT_CALL_BASE(env, napi_get_value_bool(env, value, &ret), false);
284     return ret;
285 }
286 
SetBooleanProperty(napi_env env,napi_value object,const std::string & name,bool value)287 void NapiPrintUtils::SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value)
288 {
289     napi_value jsValue = nullptr;
290     PRINT_CALL_RETURN_VOID(env, napi_get_boolean(env, value, &jsValue));
291     if (GetValueType(env, jsValue) != napi_boolean) {
292         return;
293     }
294 
295     napi_set_named_property(env, object, name.c_str(), jsValue);
296 }
297 
298 /* define properties */
DefineProperties(napi_env env,napi_value object,const std::initializer_list<napi_property_descriptor> & properties)299 void NapiPrintUtils::DefineProperties(
300     napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor> &properties)
301 {
302     napi_property_descriptor descriptors[properties.size()];
303     std::copy(properties.begin(), properties.end(), descriptors);
304 
305     (void)napi_define_properties(env, object, properties.size(), descriptors);
306 }
307 
GetValueString(napi_env env,napi_value value)308 std::string NapiPrintUtils::GetValueString(napi_env env, napi_value value)
309 {
310     std::string resultValue = "";
311     char value_string[256];
312     size_t value_size = 256;
313     size_t result;
314     napi_get_value_string_utf8(env, value, value_string, value_size, &result);
315     resultValue = value_string;
316     return resultValue;
317 }
318 
GetJsVal(napi_env env,napi_callback_info info,napi_value argv[],size_t length)319 size_t NapiPrintUtils::GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length)
320 {
321     size_t argc = length;
322     napi_value thisVal = nullptr;
323     void *data = nullptr;
324     napi_get_cb_info(env, info, &argc, argv, &thisVal, &data);
325     return argc;
326 }
327 
VerifyProperty(std::vector<std::string> & names,std::map<std::string,PrintParamStatus> & propertyList)328 bool NapiPrintUtils::VerifyProperty(
329     std::vector<std::string> &names, std::map<std::string, PrintParamStatus> &propertyList)
330 {
331     for (auto name : names) {
332         if (propertyList.find(name) == propertyList.end()) {
333             PRINT_HILOGE("Invalid property: %{public}s", name.c_str());
334             return false;
335         }
336         propertyList[name] = PRINT_PARAM_SET;
337     }
338 
339     for (auto propertypItem : propertyList) {
340         if (propertypItem.second == PRINT_PARAM_NOT_SET) {
341             PRINT_HILOGE("Missing Property: %{public}s", propertypItem.first.c_str());
342             return false;
343         }
344     }
345     return true;
346 }
347 }  // namespace OHOS::Print
348