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