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