1 /*
2  * Copyright (c) 2021 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 OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_common.h"
21 #include "napi/native_node_api.h"
22 #include "napi_common_data.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 static constexpr int32_t DEFAULT_BUF_SIZE = 1024;
27 static constexpr int32_t ASYNC_RST_SIZE = 2;
28 
29 bool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType);
30 bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize);
31 
32 napi_value WrapVoidToJS(napi_env env);
33 napi_value WrapUndefinedToJS(napi_env env);
34 
35 napi_value CreateJSObject(napi_env env);
36 
37 napi_value WrapInt32ToJS(napi_env env, int32_t value);
38 int UnwrapInt32FromJS(napi_env env, napi_value param, int defaultValue = 0);
39 bool UnwrapInt32FromJS2(napi_env env, napi_value param, int &value);
40 
41 napi_value WrapLongToJS(napi_env env, long value);
42 long UnwrapLongFromJS(napi_env env, napi_value param, long defaultValue = 0);
43 bool UnwrapLongFromJS2(napi_env env, napi_value param, long &value);
44 
45 napi_value WrapInt64ToJS(napi_env env, int64_t value);
46 int64_t UnwrapInt64FromJS(napi_env env, napi_value param, int64_t defaultValue = 0);
47 bool UnwrapInt64FromJS2(napi_env env, napi_value param, int64_t &value);
48 
49 napi_value WrapBoolToJS(napi_env env, bool value);
50 bool UnWrapBoolFromJS(napi_env env, napi_value param, bool defaultValue = false);
51 bool UnwrapBoolFromJS2(napi_env env, napi_value param, bool &value);
52 
53 napi_value WrapDoubleToJS(napi_env env, double value);
54 double UnWrapDoubleFromJS(napi_env env, napi_value param, double defaultValue = 0.0);
55 bool UnWrapDoubleFromJS2(napi_env env, napi_value param, double &value);
56 
57 napi_value WrapStringToJS(napi_env env, const std::string &value);
58 std::string UnwrapStringFromJS(napi_env env, napi_value param, const std::string &defaultValue = "");
59 bool UnwrapStringFromJS2(napi_env env, napi_value param, std::string &value);
60 
61 napi_value WrapArrayInt32ToJS(napi_env env, const std::vector<int> &value);
62 bool UnwrapArrayInt32FromJS(napi_env env, napi_value param, std::vector<int> &value);
63 
64 napi_value WrapArrayLongToJS(napi_env env, const std::vector<long> &value);
65 bool UnwrapArrayLongFromJS(napi_env env, napi_value param, std::vector<long> &value);
66 
67 napi_value WrapArrayInt64ToJS(napi_env env, const std::vector<int64_t> &value);
68 bool UnwrapArrayInt64FromJS(napi_env env, napi_value param, std::vector<int64_t> &value);
69 
70 napi_value WrapArrayDoubleToJS(napi_env env, const std::vector<double> &value);
71 bool UnwrapArrayDoubleFromJS(napi_env env, napi_value param, std::vector<double> &value);
72 
73 napi_value WrapArrayBoolToJS(napi_env env, const std::vector<bool> &value);
74 bool UnwrapArrayBoolFromJS(napi_env env, napi_value param, std::vector<bool> &value);
75 
76 napi_value WrapArrayStringToJS(napi_env env, const std::vector<std::string> &value);
77 bool UnwrapArrayStringFromJS(napi_env env, napi_value param, std::vector<std::string> &value);
78 
79 bool UnwrapArrayComplexFromJS(napi_env env, napi_value param, ComplexArrayData &value);
80 
81 /**
82  * @brief Indicates the specified attribute exists in the object passed by JS.
83  *
84  * @param env The environment that the Node-API call is invoked under.
85  * @param jsObject Indicates object passed by JS.
86  * @param propertyName Indicates the name of the property.
87  *
88  * @return Returns true if the attribute exists, else returns false.
89  */
90 bool IsExistsByPropertyName(napi_env env, napi_value jsObject, const char *propertyName);
91 
92 /**
93  * @brief Get the JSValue of the specified name from the JS object.
94  *
95  * @param env The environment that the Node-API call is invoked under.
96  * @param jsObject Indicates object passed by JS.
97  * @param propertyName Indicates the name of the property.
98  * @param expectType Indicates expected JS data type.
99  *
100  * @return Return the property value of the specified property name int jsObject on success, otherwise return nullptr.
101  */
102 napi_value GetPropertyValueByPropertyName(
103     napi_env env, napi_value jsObject, const char *propertyName, napi_valuetype expectType);
104 
105 bool SetPropertyValueByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, napi_value value);
106 
107 /**
108  * @brief Get the native number(int32) from the JSObject of the given property name.
109  *
110  * @param env The environment that the Node-API call is invoked under.
111  * @param jsObject Indicates object passed by JS.
112  * @param propertyName Indicates the name of the property.
113  * @param value Indicates the returned native value.
114  *
115  * @return Return true if successful, else return false.
116  */
117 bool UnwrapInt32ByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, int32_t &value);
118 
119 /**
120  * @brief Get the native array number(int32) from the JSObject of the given property name.
121  *
122  * @param env The environment that the Node-API call is invoked under.
123  * @param jsObject Indicates object passed by JS.
124  * @param propertyName Indicates the name of the property.
125  * @param value Indicates the returned native value.
126  *
127  * @return Return true if successful, else return false.
128  */
129 bool UnwrapInt32ArrayByPropertyName(napi_env env, napi_value jsObject, const char *propertyName,
130     std::vector<int32_t> &value);
131 
132 /**
133  * @brief Get the native number(double) from the JSObject of the given property name.
134  *
135  * @param env The environment that the Node-API call is invoked under.
136  * @param jsObject Indicates object passed by JS.
137  * @param propertyName Indicates the name of the property.
138  * @param value Indicates the returned native value.
139  *
140  * @return Return true if successful, else return false.
141  */
142 bool UnwrapDoubleByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, double &value);
143 
144 /**
145  * @brief Get the native boolean from the JSObject of the given property name.
146  *
147  * @param env The environment that the Node-API call is invoked under.
148  * @param jsObject Indicates object passed by JS.
149  * @param propertyName Indicates the name of the property.
150  * @param value Indicates the returned native value.
151  *
152  * @return Return true if successful, else return false.
153  */
154 bool UnwrapBooleanByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, bool &value);
155 bool UnwrapBooleanArrayByPropertyName(
156     napi_env env, napi_value jsObject, const char *propertyName, std::vector<bool> &value);
157 
158 /**
159  * @brief Get the native string from the JSObject of the given property name.
160  *
161  * @param env The environment that the Node-API call is invoked under.
162  * @param jsObject Indicates object passed by JS.
163  * @param propertyName Indicates the name of the property.
164  * @param value Indicates the returned native value.
165  *
166  * @return Return true if successful, else return false.
167  */
168 bool UnwrapStringByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, std::string &value);
169 bool UnwrapStringArrayByPropertyName(
170     napi_env env, napi_value jsObject, const char *propertyName, std::vector<std::string> &value);
171 
172 bool UnwrapComplexArrayByPropertyName(
173     napi_env env, napi_value jsObject, const char *propertyName, ComplexArrayData &value);
174 
175 void ClearThreadReturnData(ThreadReturnData *data);
176 
177 napi_value GetCallbackErrorValue(napi_env env, int errCode);
178 
179 /**
180  * @brief Create asynchronous data.
181  *
182  * @param env The environment that the Node-API call is invoked under.
183  *
184  * @return Return a pointer to AsyncJSCallbackInfo on success, nullptr on failure
185  */
186 AsyncJSCallbackInfo *CreateAsyncJSCallbackInfo(napi_env env);
187 void FreeAsyncJSCallbackInfo(AsyncJSCallbackInfo **asyncCallbackInfo);
188 
189 /**
190  * @brief Convert local data to JS data.
191  *
192  * @param env The environment that the Node-API call is invoked under.
193  * @param data The local data.
194  * @param value the JS data.
195  *
196  * @return The return value from NAPI C++ to JS for the module.
197  */
198 bool WrapThreadReturnData(napi_env env, const ThreadReturnData *data, napi_value *value);
199 
200 /**
201  * @brief Create asynchronous data.
202  *
203  * @param env The environment that the Node-API call is invoked under.
204  * @param param Parameter list.
205  * @param callback Point to asynchronous processing of data.
206  *
207  * @return Return true successfully, otherwise return false.
208  */
209 bool CreateAsyncCallback(napi_env env, napi_value param, AsyncJSCallbackInfo *callback);
210 
211 napi_ref CreateCallbackRefFromJS(napi_env env, napi_value param);
212 
213 /**
214  * @brief Asynchronous callback processing.
215  *
216  * @param env The environment that the Node-API call is invoked under.
217  * @param asyncCallbackInfo Process data asynchronously.
218  * @param param other param.
219  *
220  * @return Return JS data successfully, otherwise return nullptr.
221  */
222 napi_value ExecuteAsyncCallbackWork(napi_env env, AsyncJSCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param);
223 
224 /**
225  * @brief Asynchronous promise processing.
226  *
227  * @param env The environment that the Node-API call is invoked under.
228  * @param asyncCallbackInfo Process data asynchronously.
229  * @param param other param.
230  *
231  * @return Return JS data successfully, otherwise return nullptr.
232  */
233 napi_value ExecutePromiseCallbackWork(napi_env env, AsyncJSCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param);
234 
235 /**
236  * @brief The callback at the end of the asynchronous callback.
237  *
238  * @param env The environment that the Node-API call is invoked under.
239  * @param data Point to asynchronous processing of data.
240  */
241 void CompleteAsyncCallbackWork(napi_env env, napi_status status, void *data);
242 
243 /**
244  * @brief The callback at the end of the asynchronous callback.
245  *
246  * @param env The environment that the Node-API call is invoked under.
247  * @param data Point to asynchronous processing of data.
248  */
249 void CompleteAsyncVoidCallbackWork(napi_env env, napi_status status, void *data);
250 
251 /**
252  * @brief The callback at the end of the Promise callback.
253  *
254  * @param env The environment that the Node-API call is invoked under.
255  * @param data Point to asynchronous processing of data.
256  */
257 void CompletePromiseCallbackWork(napi_env env, napi_status status, void *data);
258 
259 /**
260  * @brief The callback at the end of the Promise callback.
261  *
262  * @param env The environment that the Node-API call is invoked under.
263  * @param data Point to asynchronous processing of data.
264  */
265 void CompletePromiseVoidCallbackWork(napi_env env, napi_status status, void *data);
266 
267 std::vector<uint8_t> ConvertU8Vector(napi_env env, napi_value jsValue);
268 
269 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue);
270 }  // namespace AppExecFwk
271 }  // namespace OHOS
272 #endif  // OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
273