1 /*
2  * Copyright (c) 2022-2024 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 COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_NAPI_UTILS_H
17 #define COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_NAPI_UTILS_H
18 
19 #include <functional>
20 #include <string>
21 #include <vector>
22 
23 #include <napi/native_api.h>
24 #include <napi/native_common.h>
25 #include <uv.h>
26 
27 #include "netmanager_secure_data.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace NapiUtils {
32 napi_valuetype GetValueType(napi_env env, napi_value value);
33 
34 /* named property */
35 bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
36 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
37 void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
38 std::vector<std::string> GetPropertyNames(napi_env env, napi_value object);
39 
40 /* UINT32 */
41 napi_value CreateUint32(napi_env env, uint32_t code);
42 uint32_t GetUint32FromValue(napi_env env, napi_value value);
43 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName);
44 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value);
45 
46 /* INT32 */
47 napi_value CreateInt32(napi_env env, int32_t code);
48 int32_t GetInt32FromValue(napi_env env, napi_value value);
49 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName);
50 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value);
51 
52 /* INT64 */
53 napi_value CreateInt64(napi_env env, int64_t code);
54 int64_t GetInt64Property(napi_env env, napi_value object, const std::string &propertyName);
55 int64_t GetInt64FromValue(napi_env env, napi_value value);
56 void SetInt64Property(napi_env env, napi_value object, const std::string &name, int64_t value);
57 
58 /* String UTF8 */
59 napi_value CreateStringUtf8(napi_env env, const std::string &str);
60 std::string GetStringFromValueUtf8(napi_env env, napi_value value);
61 SecureData GetSecureDataFromValueUtf8(napi_env env, napi_value value);
62 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
63 SecureData GetSecureDataPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
64 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value);
65 
66 /* array buffer */
67 bool ValueIsArrayBuffer(napi_env env, napi_value value);
68 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length);
69 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data);
70 
71 /* object */
72 napi_value CreateObject(napi_env env);
73 
74 /* undefined */
75 napi_value GetUndefined(napi_env env);
76 
77 /* function */
78 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv);
79 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg);
80 
81 /* reference */
82 napi_ref CreateReference(napi_env env, napi_value callback);
83 napi_value GetReference(napi_env env, napi_ref callbackRef);
84 void DeleteReference(napi_env env, napi_ref callbackRef);
85 
86 /* boolean */
87 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName);
88 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value);
89 napi_value GetBoolean(napi_env env, bool value);
90 bool GetBooleanValue(napi_env env, napi_value value);
91 
92 /* define properties */
93 void DefineProperties(napi_env env, napi_value object,
94                       const std::initializer_list<napi_property_descriptor> &properties);
95 
96 /* array */
97 napi_value CreateArray(napi_env env, size_t length);
98 void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value);
99 bool IsArray(napi_env env, napi_value value);
100 uint32_t GetArrayLength(napi_env env, napi_value arr);
101 napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index);
102 
103 /* libuv */
104 void CreateUvQueueWork(napi_env env, void *data, void(handler)(uv_work_t *, int status));
105 
106 /* scope */
107 napi_handle_scope OpenScope(napi_env env);
108 void CloseScope(napi_env env, napi_handle_scope scope);
109 /* error */
110 napi_value CreateErrorMessage(napi_env env, int32_t errorCodeconst, const std::string &errorMessage);
111 
112 void HookForEnvCleanup(void *data);
113 void SetEnvValid(napi_env env);
114 bool IsEnvValid(napi_env env);
115 
116 using UvHandler = std::function<void(napi_env)>;
117 napi_value GetGlobal(napi_env env);
118 uint64_t CreateUvHandlerQueue(napi_env env);
119 napi_value GetValueFromGlobal(napi_env env, const std::string &className);
120 void CreateUvQueueWorkByModuleId(napi_env env, const UvHandler &handler, uint64_t id);
121 } // namespace NapiUtils
122 } // namespace NetManagerStandard
123 } // namespace OHOS
124 
125 #endif // COMMUNICATIONNETMANAGER_BASE_NETMANAGER_BASE_NAPI_UTILS_H
126