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 #ifndef COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H
17 #define COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <iosfwd>
22 #include <vector>
23 #include <functional>
24 #include <queue>
25 #include <mutex>
26 
27 #include "initializer_list"
28 #include "napi/native_api.h"
29 #include "uv.h"
30 
31 namespace OHOS::NetStack::NapiUtils {
32 static constexpr int NETSTACK_NAPI_INTERNAL_ERROR = 2300002;
33 using UvHandler = std::function<void()>;
34 struct UvHandlerQueue : public std::queue<UvHandler> {
35     UvHandler Pop();
36     void Push(const UvHandler &handler);
37 private:
38     std::mutex mutex;
39 };
40 
41 napi_valuetype GetValueType(napi_env env, napi_value value);
42 
43 bool IsInstanceOf(napi_env env, napi_value object, const std::string &name);
44 
45 /* named property */
46 bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
47 
48 napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
49 
50 void SetNamedProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
51 
52 std::vector<std::string> GetPropertyNames(napi_env env, napi_value object);
53 
54 /* UINT32 */
55 napi_value CreateUint32(napi_env env, uint32_t code);
56 
57 napi_value CreateUint64(napi_env env, uint64_t code);
58 
59 int64_t GetInt64FromValue(napi_env env, napi_value value);
60 
61 int64_t GetInt64Property(napi_env env, napi_value object, const std::string &propertyName);
62 
63 uint32_t GetUint32FromValue(napi_env env, napi_value value);
64 
65 uint32_t GetUint32Property(napi_env env, napi_value object, const std::string &propertyName);
66 
67 void SetUint32Property(napi_env env, napi_value object, const std::string &name, uint32_t value);
68 
69 void SetUint64Property(napi_env env, napi_value object, const std::string &name, uint64_t value);
70 
71 /* INT32 */
72 napi_value CreateInt32(napi_env env, int32_t code);
73 
74 int32_t GetInt32FromValue(napi_env env, napi_value value);
75 
76 int32_t GetInt32Property(napi_env env, napi_value object, const std::string &propertyName);
77 
78 void SetInt32Property(napi_env env, napi_value object, const std::string &name, int32_t value);
79 
80 void SetDoubleProperty(napi_env env, napi_value object, const std::string &name, double value);
81 
82 /* String UTF8 */
83 napi_value CreateStringUtf8(napi_env env, const std::string &str);
84 
85 std::string GetStringFromValueUtf8(napi_env env, napi_value value);
86 
87 std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
88 
89 std::string NapiValueToString(napi_env env, napi_value value);
90 
91 void SetStringPropertyUtf8(napi_env env, napi_value object, const std::string &name, const std::string &value);
92 
93 std::vector<std::string> GetDnsServers(napi_env env, napi_value object, const std::string &propertyName);
94 
95 /* array buffer */
96 bool ValueIsArrayBuffer(napi_env env, napi_value value);
97 
98 void *GetInfoFromArrayBufferValue(napi_env env, napi_value value, size_t *length);
99 
100 napi_value CreateArrayBuffer(napi_env env, size_t length, void **data);
101 
102 /* object */
103 napi_value CreateObject(napi_env env);
104 
105 /* undefined */
106 napi_value GetUndefined(napi_env env);
107 
108 /* function */
109 napi_value CallFunction(napi_env env, napi_value recv, napi_value func, size_t argc, const napi_value *argv);
110 
111 napi_value CreateFunction(napi_env env, const std::string &name, napi_callback func, void *arg);
112 
113 /* reference */
114 napi_ref CreateReference(napi_env env, napi_value callback);
115 
116 napi_value GetReference(napi_env env, napi_ref callbackRef);
117 
118 void DeleteReference(napi_env env, napi_ref callbackRef);
119 
120 /* boolean */
121 bool GetBooleanProperty(napi_env env, napi_value object, const std::string &propertyName);
122 
123 void SetBooleanProperty(napi_env env, napi_value object, const std::string &name, bool value);
124 
125 napi_value GetBoolean(napi_env env, bool value);
126 
127 bool GetBooleanFromValue(napi_env env, napi_value value);
128 
129 /* define properties */
130 void DefineProperties(napi_env env, napi_value object,
131                       const std::initializer_list<napi_property_descriptor> &properties);
132 
133 /* array */
134 napi_value CreateArray(napi_env env, size_t length);
135 void SetArrayElement(napi_env env, napi_value array, uint32_t index, napi_value value);
136 bool IsArray(napi_env env, napi_value value);
137 void SetArrayProperty(napi_env env, napi_value object, const std::string &name, napi_value value);
138 uint32_t GetArrayLength(napi_env env, napi_value arr);
139 napi_value GetArrayElement(napi_env env, napi_value arr, uint32_t index);
140 
141 /* JSON */
142 napi_value JsonStringify(napi_env env, napi_value object);
143 
144 napi_value JsonParse(napi_env env, const std::string &inStr);
145 
146 /* libuv */
147 void CreateUvQueueWork(napi_env env, void *data, void(handler)(uv_work_t *, int status));
148 
149 void CreateUvQueueWorkEnhanced(napi_env env, void *data, void (*handler)(napi_env env, napi_status status, void *data));
150 
151 /* scope */
152 napi_handle_scope OpenScope(napi_env env);
153 
154 void CloseScope(napi_env env, napi_handle_scope scope);
155 
156 /* error */
157 napi_value CreateErrorMessage(napi_env env, int32_t errorCodeconst, const std::string &errorMessage);
158 
159 napi_value GetGlobal(napi_env env);
160 
161 napi_value GetValueFromGlobal(napi_env env, const std::string &className);
162 
163 void CreateUvQueueWorkByModuleId(napi_env env, const UvHandler &handler, uint64_t id);
164 
165 uint64_t CreateUvHandlerQueue(napi_env env);
166 
167 void HookForEnvCleanup(void *data);
168 void SetEnvValid(napi_env env);
169 bool IsEnvValid(napi_env env);
170 } // namespace OHOS::NetStack::NapiUtils
171 
172 #endif /* COMMUNICATIONNETSTACK_NETSTACK_NAPI_UTILS_H */
173