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 NAPI_COMMON_H
17 #define NAPI_COMMON_H
18 
19 #include <string>
20 #include <vector>
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "base_context.h"
24 #include "event_context.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 // define callback ref count
29 constexpr uint32_t CALLBACK_REF_CNT = 1;
30 // define property max bytes
31 constexpr uint32_t PROPERTY_MAX_BYTE = 128;
32 // define constant of 64 bytes
33 constexpr uint32_t BUFFER_BYTE = 64;
34 constexpr int32_t MAX_TEXT_LENGTH = 4096;
35 
36 enum JS_CALLBACK_ARGV {
37     CALLBACK_ARGV_INDEX_0 = 0,
38     CALLBACK_ARGV_INDEX_1,
39     CALLBACK_ARGV_CNT,
40 };
41 
42 enum JS_ARGV_NUM {
43     ARGV_NUM_0 = 0,
44     ARGV_NUM_1,
45     ARGV_NUM_2,
46     ARGV_NUM_3,
47     ARGV_NUM_4,
48     ARGV_NUM_5,
49 };
50 
51 enum JS_ARGV_INDEX {
52     ARGV_INDEX_0 = 0,
53     ARGV_INDEX_1,
54     ARGV_INDEX_2,
55     ARGV_INDEX_3,
56     ARGV_INDEX_4,
57 };
58 
59 class NapiCommon {
60 public:
61     static bool IsValidEvent(const std::string &eventInfo, int32_t &eventId);
62     static napi_value CreateCodeMessage(napi_env env, const std::string &msg, int32_t code);
63     static napi_value NapiValueByInt32(napi_env env, int32_t property);
64     static void SetPropertyBool(napi_env env, napi_value object, const std::string &propertyName, bool property);
65     static void SetPropertyInt32(napi_env env, napi_value object, const std::string &propertyName, int32_t property);
66     static void SetPropertyInt64(napi_env env, napi_value object, const std::string &propertyName, int64_t property);
67     static void SetPropertyUint32(napi_env env, napi_value object, const std::string &propertyName, uint32_t property);
68     static void SetPropertyString(napi_env env, napi_value object, const std::string &propertyName,
69                                   const std::string &property);
70     static void GetPropertyString(napi_env env, napi_value object, const std::string &propertyName,
71                                   std::string &property);
72     static void GetPropertyInt32(napi_env env, napi_value object, const std::string &propertyName, int32_t &property);
73     static void GetPropertyInt64(napi_env env, napi_value object, const std::string &propertyName, int64_t &property);
74     static std::string GetNapiStringValue(napi_env env, napi_value napiValue, const std::string &name,
75                                           const std::string &defValue = "");
76     static std::string GetStringFromValue(napi_env env, napi_value value);
77     static napi_value GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
78     static int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, const std::string &name,
79                                      const int32_t &defValue = 0);
80     static int64_t GetNapiInt64Value(napi_env env, napi_value napiValue, const std::string &name,
81                                      const int64_t &defValue = 0);
82     static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType);
83     static bool MatchParameters(napi_env env, const napi_value parameters[],
84                                 std::initializer_list<napi_valuetype> valueTypes);
85     static napi_value CreateUndefined(napi_env env);
86     static napi_value HandleAsyncWork(napi_env env, BaseContext *baseContext, const std::string &workName,
87                                       napi_async_execute_callback execute, napi_async_complete_callback complete);
88     static void Handle1ValueCallback(napi_env env, BaseContext *baseContext, napi_value callbackValue);
89     static void Handle2ValueCallback(napi_env env, BaseContext *baseContext, napi_value callbackValue);
90     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
91     static bool MatchObjectProperty(napi_env env, napi_value object,
92                                     std::initializer_list<std::pair<std::string, napi_valuetype>> pairList);
93     static bool HasNamedTypeProperty(napi_env env, napi_value object, napi_valuetype type,
94                                      const std::string &propertyName);
95     static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info);
96     static napi_value CreateObject(napi_env env);
97     static napi_value CreateErrorMessage(napi_env env, int32_t errorCode, const std::string &errorMessage);
98 };
99 } // namespace NetManagerStandard
100 } // namespace OHOS
101 #endif // NAPI_COMMON_H
102