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