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