1 /*
2  * Copyright (c) 2021-2022 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 CAMERA_NAPI_UTILS_H_
17 #define CAMERA_NAPI_UTILS_H_
18 
19 #include <cstddef>
20 #include "camera_error_code.h"
21 #include "camera_napi_const.h"
22 #include "js_native_api.h"
23 #include "js_native_api_types.h"
24 #include "istream_metadata.h"
25 #include "napi/native_api.h"
26 
27 #ifdef NAPI_ASSERT
28 #undef NAPI_ASSERT
29 #endif
30 
31 #define CAMERA_NAPI_VALUE napi_value
32 
33 #define NAPI_ASSERT(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, nullptr)
34 
35 #define CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar)        \
36     do {                                                               \
37         void* data;                                                    \
38         napi_get_cb_info(env, info, &(argc), argv, &(thisVar), &data); \
39     } while (0)
40 
41 #define CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar)          \
42     do {                                                                           \
43         void* data;                                                                \
44         status = napi_get_cb_info(env, info, nullptr, nullptr, &(thisVar), &data); \
45     } while (0)
46 
47 #define CAMERA_NAPI_GET_JS_ASYNC_CB_REF(env, arg, count, cbRef) \
48     do {                                                        \
49         napi_valuetype valueType = napi_undefined;              \
50         napi_typeof(env, arg, &valueType);                      \
51         if (valueType == napi_function) {                       \
52             napi_create_reference(env, arg, count, &(cbRef));   \
53         } else {                                                \
54             NAPI_ASSERT(env, false, "type mismatch");           \
55         }                                                       \
56     } while (0)
57 
58 #define CAMERA_NAPI_ASSERT_NULLPTR_CHECK(env, result) \
59     do {                                              \
60         if ((result) == nullptr) {                    \
61             napi_get_undefined(env, &(result));       \
62             return result;                            \
63         }                                             \
64     } while (0)
65 
66 #define CAMERA_NAPI_CREATE_PROMISE(env, callbackRef, deferred, result) \
67     do {                                                               \
68         if ((callbackRef) == nullptr) {                                \
69             napi_create_promise(env, &(deferred), &(result));          \
70         }                                                              \
71     } while (0)
72 
73 #define CAMERA_NAPI_CREATE_RESOURCE_NAME(env, resource, resourceName)              \
74     do {                                                                           \
75         napi_create_string_utf8(env, resourceName, NAPI_AUTO_LENGTH, &(resource)); \
76     } while (0)
77 
78 #define CAMERA_NAPI_CHECK_NULL_PTR_RETURN_UNDEFINED(env, ptr, ret, message, ...) \
79     do {                                                                         \
80         if ((ptr) == nullptr) {                                                  \
81             MEDIA_ERR_LOG(message, ##__VA_ARGS__);                               \
82             napi_get_undefined(env, &(ret));                                     \
83             return ret;                                                          \
84         }                                                                        \
85     } while (0)
86 
87 #define CAMERA_NAPI_CHECK_NULL_PTR_RETURN_VOID(ptr, message, ...) \
88     do {                                                          \
89         if ((ptr) == nullptr) {                                   \
90             MEDIA_ERR_LOG(message, ##__VA_ARGS__);                \
91             return;                                               \
92         }                                                         \
93     } while (0)
94 
95 #define CAMERA_NAPI_ASSERT_EQUAL(condition, errMsg, ...) \
96     do {                                                 \
97         if (!(condition)) {                              \
98             MEDIA_ERR_LOG(errMsg, ##__VA_ARGS__);        \
99             return;                                      \
100         }                                                \
101     } while (0)
102 
103 #define CAMERA_NAPI_CHECK_AND_BREAK_LOG(cond, fmt, ...) \
104     do {                                                \
105         if (!(cond)) {                                  \
106             MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);          \
107             break;                                      \
108         }                                               \
109     } while (0)
110 
111 #define CAMERA_NAPI_CHECK_AND_RETURN_LOG(cond, fmt, ...) \
112     do {                                                 \
113         if (!(cond)) {                                   \
114             MEDIA_ERR_LOG(fmt, ##__VA_ARGS__);           \
115             return;                                      \
116         }                                                \
117     } while (0)
118 
119 namespace OHOS {
120 namespace CameraStandard {
121 /* Util class used by napi asynchronous methods for making call to js callback function */
122 class CameraNapiUtils {
123 public:
124     static void CreateNapiErrorObject(
125         napi_env env, int32_t errorCode, const char* errString, std::unique_ptr<JSAsyncContextOutput>& jsContext);
126 
127     static void InvokeJSAsyncMethod(napi_env env, napi_deferred deferred, napi_ref callbackRef, napi_async_work work,
128         const JSAsyncContextOutput& asyncContext);
129 
130     static int32_t IncrementAndGet(uint32_t& num);
131 
132     static void IsEnableSecureCamera(bool isEnable);
133 
134     static bool GetEnableSecureCamera();
135 
136     static bool CheckInvalidArgument(napi_env env, size_t argc, int32_t length,
137                                      napi_value *argv, CameraSteps step);
138 
139     static bool CheckError(napi_env env, int32_t retCode);
140 
141     static double FloatToDouble(float val);
142 
143     static std::string GetStringArgument(napi_env env, napi_value value);
144 
145     static bool IsSameNapiValue(napi_env env, napi_value valueSrc, napi_value valueDst);
146 
147     static napi_status CallPromiseFun(
148         napi_env env, napi_value promiseValue, void* data, napi_callback thenCallback, napi_callback catchCallback);
149 
150     static std::vector<napi_property_descriptor> GetPropertyDescriptor(
151         std::vector<std::vector<napi_property_descriptor>> descriptors);
152 
153     static napi_status CreateObjectWithPropName(
154         napi_env env, napi_value* result, size_t property_count, const char** keys);
155 
156     static napi_status CreateObjectWithPropNameAndValues(napi_env env, napi_value* result, size_t property_count,
157         const char** keys, const std::vector<std::string> values);
158 
159     static size_t GetNapiArgs(napi_env env, napi_callback_info callbackInfo);
160 
GetUndefinedValue(napi_env env)161     inline static napi_value GetUndefinedValue(napi_env env)
162     {
163         napi_value result = nullptr;
164         napi_get_undefined(env, &result);
165         return result;
166     }
167 
GetBooleanValue(napi_env env,bool value)168     inline static napi_value GetBooleanValue(napi_env env, bool value)
169     {
170         napi_value result = nullptr;
171         napi_get_boolean(env, value, &result);
172         return result;
173     }
174 
ThrowError(napi_env env,int32_t code,const char * message)175     inline static void ThrowError(napi_env env, int32_t code, const char* message)
176     {
177         std::string errorCode = std::to_string(code);
178         napi_throw_error(env, errorCode.c_str(), message);
179     }
180 
181     static void CreateFrameRateJSArray(napi_env env, std::vector<int32_t> frameRateRange, napi_value &result);
182 
183     static napi_value CreateSupportFrameRatesJSArray(
184         napi_env env, std::vector<std::vector<int32_t>> supportedFrameRatesRange);
185 
186     static napi_value ParseMetadataObjectTypes(napi_env env, napi_value arrayParam,
187                                     std::vector<MetadataObjectType> &metadataObjectTypes);
188 
189     static napi_value CreateJSArray(napi_env env, napi_status status, std::vector<int32_t> nativeArray);
190 
191     static napi_value ProcessingPhysicalApertures(napi_env env, std::vector<std::vector<float>> physicalApertures);
192 
193 private:
CameraNapiUtils()194     explicit CameraNapiUtils() {};
195 
196     static bool mEnableSecure;
197 }; // namespace CameraNapiUtils
198 } // namespace CameraStandard
199 } // namespace OHOS
200 #endif /* CAMERA_NAPI_UTILS_H_ */
201