1 /*
2  * Copyright (c) 2023 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_DEFINE_H
17 #define NAPI_COMMON_DEFINE_H
18 
19 #include <algorithm>
20 #include <list>
21 
22 #include "hilog/log.h"
23 #include "node_api.h"
24 #include "update_log.h"
25 
26 namespace OHOS::UpdateEngine {
27 constexpr int32_t COMPONENT_ERR = 11500000;
28 #define PARAM_CHECK(validCheck, exper, ...)  \
29 if (!(validCheck)) {                     \
30     ENGINE_LOGE(__VA_ARGS__);            \
31     exper;                               \
32 }
33 
34 #define PARAM_CHECK_NAPI_CALL(env, assertion, exper, message) \
35 if (!(assertion)) {                     \
36     ENGINE_LOGE(message);               \
37     exper;                              \
38 }
39 
40 #define INDEX(x) ((x) - 1)
41 
42 enum class ClientStatus {
43     CLIENT_SUCCESS = 0,
44     CLIENT_INVALID_PARAM = 1000,
45     CLIENT_INVALID_TYPE,
46     CLIENT_REPEAT_REQ,
47     CLIENT_FAIL,
48     CLIENT_CHECK_NEW_FIRST
49 };
50 
51 enum ARG_NUM {
52     ARG_NUM_ONE = 1,
53     ARG_NUM_TWO,
54     ARG_NUM_THREE,
55     ARG_NUM_FOUR,
56     MAX_ARGC
57 };
58 
59 enum CALLBACK_POSITION {
60     CALLBACK_POSITION_ONE = 1,
61     CALLBACK_POSITION_TWO,
62     CALLBACK_POSITION_THREE,
63     CALLBACK_POSITION_FOUR,
64     CALLBACK_MAX_POSITION
65 };
66 
67 struct NativeClass {
68     std::string className;
69     napi_callback constructor;
70     napi_ref *constructorRef;
71     napi_property_descriptor *desc;
72     int descSize;
73 };
74 
75 template<typename T>
CreateJsObject(napi_env env,napi_callback_info info,napi_ref constructorRef,napi_value & jsObject)76 T *CreateJsObject(napi_env env, napi_callback_info info, napi_ref constructorRef, napi_value &jsObject)
77 {
78     napi_value constructor = nullptr;
79     napi_status status = napi_get_reference_value(env, constructorRef, &constructor);
80     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr,
81         "CreateJsObject error, napi_get_reference_value fail");
82 
83     size_t argc = MAX_ARGC;
84     napi_value args[MAX_ARGC] = {0};
85     status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
86     if (status != napi_ok) {
87         ENGINE_LOGI("CreateJsObject, napi_get_cb_info error");
88     }
89     status = napi_new_instance(env, constructor, argc, args, &jsObject);
90     PARAM_CHECK_NAPI_CALL(env, status == napi_ok, return nullptr, "CreateJsObject error, napi_new_instance fail");
91 
92     T *nativeObject = nullptr;
93     status = napi_unwrap(env, jsObject, (void **) &nativeObject);
94     if (status != napi_ok) {
95         ENGINE_LOGE("CreateJsObject error, napi_unwrap fail");
96         napi_remove_wrap(env, jsObject, (void **) &nativeObject);
97         jsObject = nullptr;
98         return nullptr;
99     }
100     return nativeObject;
101 }
102 
UnwrapJsObject(napi_env env,napi_callback_info info)103 template <typename T> T *UnwrapJsObject(napi_env env, napi_callback_info info)
104 {
105     size_t argc = ARG_NUM_ONE;
106     napi_value argv[ARG_NUM_ONE] = { 0 };
107     napi_value thisVar = nullptr;
108     void *data = nullptr;
109     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
110 
111     T *nativeObject = nullptr;
112     napi_unwrap(env, thisVar, (void **)&nativeObject);
113     return nativeObject;
114 }
115 
116 template <typename T>
IsValidEnum(const std::list<T> & enumList,int32_t number)117 bool IsValidEnum(const std::list<T> &enumList, int32_t number)
118 {
119     return std::any_of(enumList.begin(), enumList.end(), [=](T key) { return number == static_cast<int32_t>(key); });
120 }
121 } // namespace OHOS::UpdateEngine
122 #endif // NAPI_COMMON_DEFINE_H