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