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 #ifndef NAPI_PRINT_UTILS_H
17 #define NAPI_PRINT_UTILS_H
18 
19 #include <string>
20 #include <map>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "print_constant.h"
25 
26 namespace OHOS::Print {
27 class NapiPrintUtils {
28 public:
29     static constexpr size_t MAX_ARGC = 6;
30     static constexpr size_t ARGC_ZERO = 0;
31     static constexpr size_t ARGC_ONE = 1;
32     static constexpr size_t ARGC_TWO = 2;
33     static constexpr size_t ARGC_THREE = 3;
34     static constexpr size_t ARGC_FOUR = 4;
35     static constexpr size_t ARGC_FIVE = 5;
36     static constexpr size_t ARGC_SIX = 6;
37 
38     static constexpr uint32_t INDEX_ZERO = 0;
39     static constexpr uint32_t INDEX_ONE = 1;
40     static constexpr uint32_t INDEX_TWO = 2;
41     static constexpr uint32_t INDEX_THREE = 3;
42     static constexpr uint32_t INDEX_FOUR = 4;
43     static constexpr uint32_t INDEX_FIVE = 5;
44 
45     static napi_valuetype GetValueType(napi_env env, napi_value value);
46     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
47     static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
48     static void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
49     static std::vector<std::string> GetPropertyNames(napi_env env, napi_value object);
50     static napi_value CreateUint32(napi_env env, uint32_t code);
51     static uint32_t GetUint32FromValue(napi_env env, napi_value value);
52     static uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName);
53     static void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value);
54     static napi_value CreateInt32(napi_env env, int32_t code);
55     static int32_t GetInt32FromValue(napi_env env, napi_value value);
56     static int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName);
57     static void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value);
58     static napi_value CreateStringUtf8(napi_env env, const std::string &str);
59     static std::string GetStringFromValueUtf8(napi_env env, napi_value value);
60     static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
61     static void SetStringPropertyUtf8(
62         napi_env env, napi_value object, const std::string &name, const std::string &value);
63     static std::string GetValueString(napi_env env, napi_value value);
64     static bool ValueIsArrayBuffer(napi_env env, napi_value value);
65     static void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length);
66     static napi_value CreateArrayBuffer(napi_env env, size_t length, void **data);
67     static napi_value CreateObject(napi_env env);
68     static napi_value GetUndefined(napi_env env);
69     static napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv);
70     static napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg);
71     static napi_ref CreateReference(napi_env env, napi_value callback);
72     static napi_value GetReference(napi_env env, napi_ref callbackRef);
73     static void DeleteReference(napi_env env, napi_ref callbackRef);
74     static bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName);
75     static void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value);
76     static void DefineProperties(
77         napi_env env, napi_value object, const std::initializer_list<napi_property_descriptor> &properties);
78 
79     static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length);
80     static bool VerifyProperty(std::vector<std::string> &names, std::map<std::string, PrintParamStatus> &propertyList);
81     static std::string GetPrintErrorMsg(int32_t errorCode);
82     static bool CheckCallerIsSystemApp();
83 };
84 } // namespace OHOS::Print
85 #endif // NAPI_PRINT_UTILS_H