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 #include "napi_common.h"
17 #include "net_mgr_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
IsValidEvent(const std::string & eventInfo,int32_t & eventId)21 bool NapiCommon::IsValidEvent(const std::string &eventInfo, int32_t &eventId)
22 {
23 bool isEvent = true;
24 if (eventInfo == NET_UID_POLICY_CHANGE) {
25 eventId = EVENT_NET_UID_POLICY_CHANGE;
26 } else if (eventInfo == NET_CELLULAR_POLICY_CHANGE) {
27 eventId = EVENT_NET_CELLULAR_POLICY_CHANGE;
28 } else if (eventInfo == NET_STRATEGY_SWITCH_CHANGE) {
29 eventId = EVENT_NET_STRATEGY_SWITCH_CHANGE;
30 } else if (eventInfo == NET_AVAILABLE_CHANGE) {
31 eventId = EVENT_NET_AVAILABLE_CHANGE;
32 } else if (eventInfo == NET_CAPABILITIES_CHANGE) {
33 eventId = EVENT_NET_CAPABILITIES_CHANGE;
34 } else if (eventInfo == NET_CONNECTION_CHANGE) {
35 eventId = EVENT_NET_CONNECTION_CHANGE;
36 } else if (eventInfo == NET_LOST_CHANGE) {
37 eventId = EVENT_NET_LOST_CHANGE;
38 } else if (eventInfo == NET_STATS_CHANGE) {
39 eventId = EVENT_NET_STATS_CHANGE;
40 } else if (eventInfo == NET_BLOCK_STATUS_CHANGE) {
41 eventId = EVENT_NET_BLOCK_STATUS_CHANGE;
42 } else if (eventInfo == NET_UNAVAILABLE_CHANGE) {
43 eventId = EVENT_NET_UNAVAILABLE_CHANGE;
44 } else {
45 isEvent = false;
46 }
47 return isEvent;
48 }
49
CreateCodeMessage(napi_env env,const std::string & msg,int32_t code)50 napi_value NapiCommon::CreateCodeMessage(napi_env env, const std::string &msg, int32_t code)
51 {
52 napi_value messageCodeInfo = nullptr;
53 napi_value messageInfo = nullptr;
54 napi_value codeInfo = nullptr;
55 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &messageInfo));
56 NAPI_CALL(
57 env, napi_create_string_utf8(env, std::to_string(code).c_str(), std::to_string(code).length(), &codeInfo));
58 NAPI_CALL(env, napi_create_error(env, codeInfo, messageInfo, &messageCodeInfo));
59 return messageCodeInfo;
60 }
61
SetPropertyBool(napi_env env,napi_value object,const std::string & propertyName,bool property)62 void NapiCommon::SetPropertyBool(
63 napi_env env, napi_value object, const std::string &propertyName, bool property)
64 {
65 napi_value propertyDest = nullptr;
66 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, property, &propertyDest));
67 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest));
68 }
69
SetPropertyInt32(napi_env env,napi_value object,const std::string & propertyName,int32_t property)70 void NapiCommon::SetPropertyInt32(
71 napi_env env, napi_value object, const std::string &propertyName, int32_t property)
72 {
73 napi_value propertyDest = nullptr;
74 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, property, &propertyDest));
75 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest));
76 }
77
SetPropertyInt64(napi_env env,napi_value object,const std::string & propertyName,int64_t property)78 void NapiCommon::SetPropertyInt64(
79 napi_env env, napi_value object, const std::string &propertyName, int64_t property)
80 {
81 napi_value propertyDest = nullptr;
82 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, property, &propertyDest));
83 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest));
84 }
85
SetPropertyUint32(napi_env env,napi_value object,const std::string & propertyName,uint32_t property)86 void NapiCommon::SetPropertyUint32(
87 napi_env env, napi_value object, const std::string &propertyName, uint32_t property)
88 {
89 napi_value propertyDest = nullptr;
90 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, property, &propertyDest));
91 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest));
92 }
93
SetPropertyString(napi_env env,napi_value object,const std::string & propertyName,const std::string & property)94 void NapiCommon::SetPropertyString(
95 napi_env env, napi_value object, const std::string &propertyName, const std::string &property)
96 {
97 napi_value propertyDest = nullptr;
98 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, property.c_str(), property.length(), &propertyDest));
99 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, propertyName.c_str(), propertyDest));
100 }
101
GetPropertyString(napi_env env,napi_value object,const std::string & propertyName,std::string & property)102 void NapiCommon::GetPropertyString(
103 napi_env env, napi_value object, const std::string &propertyName, std::string &property)
104 {
105 napi_value value = nullptr;
106 char propertyBuffer[PROPERTY_MAX_BYTE] = {0};
107 size_t realByte = 0;
108 NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
109 NAPI_CALL_RETURN_VOID(
110 env, napi_get_value_string_utf8(env, value, propertyBuffer, PROPERTY_MAX_BYTE, &realByte));
111 property = propertyBuffer;
112 }
113
GetPropertyInt32(napi_env env,napi_value object,const std::string & propertyName,int32_t & property)114 void NapiCommon::GetPropertyInt32(
115 napi_env env, napi_value object, const std::string &propertyName, int32_t &property)
116 {
117 napi_value value = nullptr;
118 NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
119 NAPI_CALL_RETURN_VOID(env, napi_get_value_int32(env, value, &property));
120 }
121
GetPropertyInt64(napi_env env,napi_value object,const std::string & propertyName,int64_t & property)122 void NapiCommon::GetPropertyInt64(
123 napi_env env, napi_value object, const std::string &propertyName, int64_t &property)
124 {
125 napi_value value = nullptr;
126 NAPI_CALL_RETURN_VOID(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
127 NAPI_CALL_RETURN_VOID(env, napi_get_value_int64(env, value, &property));
128 }
129
NapiValueByInt32(napi_env env,int32_t property)130 napi_value NapiCommon::NapiValueByInt32(napi_env env, int32_t property)
131 {
132 napi_value value = nullptr;
133 NAPI_CALL(env, napi_create_int32(env, property, &value));
134 return value;
135 }
136
GetNapiStringValue(napi_env env,napi_value napiValue,const std::string & name,const std::string & defValue)137 std::string NapiCommon::GetNapiStringValue(
138 napi_env env, napi_value napiValue, const std::string &name, const std::string &defValue)
139 {
140 napi_value value = GetNamedProperty(env, napiValue, name);
141 if (value != nullptr) {
142 return GetStringFromValue(env, value);
143 } else {
144 return defValue;
145 }
146 }
147
GetStringFromValue(napi_env env,napi_value value)148 std::string NapiCommon::GetStringFromValue(napi_env env, napi_value value)
149 {
150 char msgChars[MAX_TEXT_LENGTH] = {0};
151 size_t msgLength = 0;
152 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, msgChars, MAX_TEXT_LENGTH, &msgLength), "");
153 if (msgLength > 0) {
154 return std::string(msgChars, 0, msgLength);
155 } else {
156 return "";
157 }
158 }
159
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)160 napi_value NapiCommon::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
161 {
162 napi_value value = nullptr;
163 bool hasProperty = false;
164 NAPI_CALL(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty));
165 if (hasProperty) {
166 NAPI_CALL(env, napi_get_named_property(env, object, propertyName.data(), &value));
167 }
168 return value;
169 }
170
GetNapiInt32Value(napi_env env,napi_value napiValue,const std::string & name,const int32_t & defValue)171 int32_t NapiCommon::GetNapiInt32Value(
172 napi_env env, napi_value napiValue, const std::string &name, const int32_t &defValue)
173 {
174 napi_value value = GetNamedProperty(env, napiValue, name);
175 if (value != nullptr) {
176 int32_t intValue = 0;
177 napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
178 if (getIntStatus == napi_ok) {
179 return intValue;
180 }
181 }
182 return defValue;
183 }
184
GetNapiInt64Value(napi_env env,napi_value napiValue,const std::string & name,const int64_t & defValue)185 int64_t NapiCommon::GetNapiInt64Value(
186 napi_env env, napi_value napiValue, const std::string &name, const int64_t &defValue)
187 {
188 napi_value value = GetNamedProperty(env, napiValue, name);
189 if (value != nullptr) {
190 int64_t intValue = 0;
191 napi_status getIntStatus = napi_get_value_int64(env, value, &intValue);
192 if (getIntStatus == napi_ok) {
193 return intValue;
194 }
195 }
196 return defValue;
197 }
198
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)199 bool NapiCommon::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
200 {
201 napi_valuetype valueType = napi_undefined;
202 NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), false);
203 return valueType == targetType;
204 }
205
MatchParameters(napi_env env,const napi_value parameters[],std::initializer_list<napi_valuetype> valueTypes)206 bool NapiCommon::MatchParameters(
207 napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes)
208 {
209 if (parameters == nullptr) {
210 return false;
211 }
212 int i = 0;
213 for (auto beg = valueTypes.begin(); beg != valueTypes.end(); ++beg) {
214 if (!MatchValueType(env, parameters[i], *beg)) {
215 return false;
216 }
217 ++i;
218 }
219 return true;
220 }
221
CreateErrorMessage(napi_env env,const std::string & msg,int32_t errorCode)222 napi_value NapiCommon::CreateErrorMessage(napi_env env, const std::string &msg, int32_t errorCode)
223 {
224 napi_value result = nullptr;
225 napi_value message = nullptr;
226 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &message));
227 napi_value codeValue = nullptr;
228 std::string errCode = std::to_string(errorCode);
229 NAPI_CALL(env, napi_create_string_utf8(env, errCode.c_str(), errCode.length(), &codeValue));
230 NAPI_CALL(env, napi_create_error(env, codeValue, message, &result));
231 return result;
232 }
233
CreateUndefined(napi_env env)234 napi_value NapiCommon::CreateUndefined(napi_env env)
235 {
236 napi_value result = nullptr;
237 NAPI_CALL(env, napi_get_undefined(env, &result));
238 return result;
239 }
240
HandleAsyncWork(napi_env env,BaseContext * baseContext,const std::string & workName,napi_async_execute_callback execute,napi_async_complete_callback complete)241 napi_value NapiCommon::HandleAsyncWork(napi_env env, BaseContext *baseContext, const std::string &workName,
242 napi_async_execute_callback execute, napi_async_complete_callback complete)
243 {
244 NETMGR_LOG_I("NapiCommon HandleAsyncWork workName = %{public}s", workName.c_str());
245 std::unique_ptr<BaseContext> context(baseContext);
246 if (context == nullptr) {
247 std::string errorCode = std::to_string(napi_invalid_arg);
248 std::string errorMessage = "error at baseContext is nullptr";
249 NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
250 napi_value res = nullptr;
251 NAPI_CALL(env, napi_get_undefined(env, &res));
252 return res;
253 }
254 napi_value result = nullptr;
255 if (context->callbackRef == nullptr) {
256 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
257 } else {
258 NAPI_CALL(env, napi_get_undefined(env, &result));
259 }
260 napi_value resource = CreateUndefined(env);
261 napi_value resourceName = nullptr;
262 NAPI_CALL(env, napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName));
263 NAPI_CALL(env,
264 napi_create_async_work(
265 env, resource, resourceName, execute, complete, (void *)context.get(), &context->work));
266 napi_status queueWorkStatus = napi_queue_async_work(env, context->work);
267 if (queueWorkStatus == napi_ok) {
268 context.release();
269 NETMGR_LOG_I("NapiCommon HandleAsyncWork napi_queue_async_work ok");
270 } else {
271 std::string errorCode = std::to_string(queueWorkStatus);
272 std::string errorMessage = "error at napi_queue_async_work";
273 NAPI_CALL(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
274 }
275 NETMGR_LOG_I("NapiCommon HandleAsyncWork end");
276 return result;
277 }
278
Handle1ValueCallback(napi_env env,BaseContext * baseContext,napi_value callbackValue)279 void NapiCommon::Handle1ValueCallback(napi_env env, BaseContext *baseContext, napi_value callbackValue)
280 {
281 NETMGR_LOG_I("Handle1ValueCallback start");
282 if (baseContext == nullptr) {
283 NETMGR_LOG_I("Handle1ValueCallback serious error baseContext nullptr");
284 std::string errorCode = std::to_string(napi_invalid_arg);
285 std::string errorMessage = "error at baseContext is nullptr";
286 NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
287 return;
288 }
289 if (baseContext->callbackRef != nullptr) {
290 NETMGR_LOG_I("Handle1ValueCallback start normal callback");
291 napi_value recv = CreateUndefined(env);
292 napi_value callbackFunc = nullptr;
293 NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, baseContext->callbackRef, &callbackFunc));
294 napi_value callbackValues[] = {callbackValue};
295 napi_value result = nullptr;
296 NAPI_CALL_RETURN_VOID(
297 env, napi_call_function(env, recv, callbackFunc, std::size(callbackValues), callbackValues, &result));
298 NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, baseContext->callbackRef));
299 NETMGR_LOG_I("Handle1ValueCallback normal callback end");
300 } else if (baseContext->deferred != nullptr) {
301 NETMGR_LOG_I("Handle1ValueCallback start promise callback");
302 if (baseContext->resolved) {
303 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, baseContext->deferred, callbackValue));
304 NETMGR_LOG_I("Handle1ValueCallback napi_resolve_deferred end");
305 } else {
306 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, baseContext->deferred, callbackValue));
307 NETMGR_LOG_I("Handle1ValueCallback napi_reject_deferred end");
308 }
309 NETMGR_LOG_I("Handle1ValueCallback promise callback end");
310 }
311 napi_delete_async_work(env, baseContext->work);
312 delete baseContext;
313 NETMGR_LOG_I("Handle1ValueCallback end");
314 }
315
Handle2ValueCallback(napi_env env,BaseContext * baseContext,napi_value callbackValue)316 void NapiCommon::Handle2ValueCallback(napi_env env, BaseContext *baseContext, napi_value callbackValue)
317 {
318 NETMGR_LOG_I("Handle2ValueCallback start");
319 if (baseContext == nullptr) {
320 NETMGR_LOG_I("Handle2ValueCallback serious error baseContext nullptr");
321 std::string errorCode = std::to_string(napi_invalid_arg);
322 std::string errorMessage = "error at baseContext is nullptr";
323 NAPI_CALL_RETURN_VOID(env, napi_throw_error(env, errorCode.c_str(), errorMessage.c_str()));
324 return;
325 }
326 if (baseContext->callbackRef != nullptr) {
327 NETMGR_LOG_I("Handle2ValueCallback start normal callback");
328 napi_value recv = CreateUndefined(env);
329 napi_value callbackFunc = nullptr;
330 NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, baseContext->callbackRef, &callbackFunc));
331 napi_value callbackValues[] = {nullptr, nullptr};
332 callbackValues[0] = baseContext->resolved ? CreateUndefined(env) : callbackValue;
333 callbackValues[1] = baseContext->resolved ? callbackValue : CreateUndefined(env);
334 napi_value result = nullptr;
335 NAPI_CALL_RETURN_VOID(
336 env, napi_call_function(env, recv, callbackFunc, std::size(callbackValues), callbackValues, &result));
337 NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, baseContext->callbackRef));
338 NETMGR_LOG_I("Handle2ValueCallback normal callback end");
339 } else if (baseContext->deferred != nullptr) {
340 NETMGR_LOG_I("Handle2ValueCallback start promise callback");
341 if (baseContext->resolved) {
342 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, baseContext->deferred, callbackValue));
343 } else {
344 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, baseContext->deferred, callbackValue));
345 }
346 NETMGR_LOG_I("Handle2ValueCallback promise callback end");
347 }
348 napi_delete_async_work(env, baseContext->work);
349 delete baseContext;
350 NETMGR_LOG_I("Handle2ValueCallback end");
351 }
352
HasNamedTypeProperty(napi_env env,napi_value object,napi_valuetype type,std::string propertyName)353 bool NapiCommon::HasNamedTypeProperty(
354 napi_env env, napi_value object, napi_valuetype type, std::string propertyName)
355 {
356 bool hasProperty = false;
357 NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty), false);
358 if (hasProperty) {
359 napi_value value = nullptr;
360 NAPI_CALL_BASE(env, napi_get_named_property(env, object, propertyName.data(), &value), false);
361 return NapiCommon::MatchValueType(env, value, type);
362 }
363 return false;
364 }
365
HasNamedProperty(napi_env env,napi_value object,std::string propertyName)366 bool NapiCommon::HasNamedProperty(napi_env env, napi_value object, std::string propertyName)
367 {
368 bool hasProperty = false;
369 NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.data(), &hasProperty), false);
370 return hasProperty;
371 }
372
MatchObjectProperty(napi_env env,napi_value object,std::initializer_list<std::pair<std::string,napi_valuetype>> pairList)373 bool NapiCommon::MatchObjectProperty(
374 napi_env env, napi_value object, std::initializer_list<std::pair<std::string, napi_valuetype>> pairList)
375 {
376 if (object == nullptr) {
377 return false;
378 }
379 for (auto beg = pairList.begin(); beg != pairList.end(); ++beg) {
380 if (!HasNamedTypeProperty(env, object, beg->second, beg->first)) {
381 return false;
382 }
383 }
384 return true;
385 }
386
CreateEnumConstructor(napi_env env,napi_callback_info info)387 napi_value NapiCommon::CreateEnumConstructor(napi_env env, napi_callback_info info)
388 {
389 napi_value thisArg = nullptr;
390 void *data = nullptr;
391 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
392 napi_value global = nullptr;
393 napi_get_global(env, &global);
394 return thisArg;
395 }
396 } // namespace NetManagerStandard
397 } // namespace OHOS
398