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