1 /*
2  * Copyright (c) 2024-2024 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 #include "ability/camera_ability_napi.h"
17 #include "camera_log.h"
18 #include "camera_napi_utils.h"
19 #include "napi/native_common.h"
20 #include "napi/native_api.h"
21 #include "js_native_api_types.h"
22 #include "camera_napi_object_types.h"
23 #include "camera_napi_param_parser.h"
24 #include "camera_napi_security_utils.h"
25 #include "camera_error_code.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 using namespace std;
30 thread_local sptr<CameraAbility> CameraFunctionsNapi::sCameraAbility_ = nullptr;
31 thread_local napi_ref CameraFunctionsNapi::sPhotoConstructor_ = nullptr;
32 thread_local napi_ref CameraFunctionsNapi::sPhotoConflictConstructor_ = nullptr;
33 thread_local napi_ref CameraFunctionsNapi::sPortraitPhotoConstructor_ = nullptr;
34 thread_local napi_ref CameraFunctionsNapi::sPortraitPhotoConflictConstructor_ = nullptr;
35 thread_local napi_ref CameraFunctionsNapi::sVideoConstructor_ = nullptr;
36 thread_local napi_ref CameraFunctionsNapi::sVideoConflictConstructor_ = nullptr;
37 
38 const std::map<FunctionsType, const char*> CameraFunctionsNapi::functionsNameMap_ = {
39     {FunctionsType::PHOTO_FUNCTIONS, PHOTO_ABILITY_NAPI_CLASS_NAME},
40     {FunctionsType::PHOTO_CONFLICT_FUNCTIONS, PHOTO_CONFLICT_ABILITY_NAPI_CLASS_NAME},
41     {FunctionsType::PORTRAIT_PHOTO_FUNCTIONS, PORTRAIT_PHOTO_ABILITY_NAPI_CLASS_NAME},
42     {FunctionsType::PORTRAIT_PHOTO_CONFLICT_FUNCTIONS, PORTRAIT_PHOTO_CONFLICT_ABILITY_NAPI_CLASS_NAME},
43     {FunctionsType::VIDEO_FUNCTIONS, VIDEO_ABILITY_NAPI_CLASS_NAME},
44     {FunctionsType::VIDEO_CONFLICT_FUNCTIONS, VIDEO_CONFLICT_ABILITY_NAPI_CLASS_NAME}
45 };
46 
47 const std::vector<napi_property_descriptor> CameraFunctionsNapi::flash_query_props = {
48     DECLARE_NAPI_FUNCTION("hasFlash", CameraFunctionsNapi::HasFlash),
49     DECLARE_NAPI_FUNCTION("isFlashModeSupported", CameraFunctionsNapi::IsFlashModeSupported),
50     DECLARE_NAPI_FUNCTION("isLcdFlashSupported", CameraFunctionsNapi::IsLcdFlashSupported),
51 };
52 
53 const std::vector<napi_property_descriptor> CameraFunctionsNapi::auto_exposure_query_props = {
54     DECLARE_NAPI_FUNCTION("isExposureModeSupported", CameraFunctionsNapi::IsExposureModeSupported),
55     DECLARE_NAPI_FUNCTION("getExposureBiasRange", CameraFunctionsNapi::GetExposureBiasRange)
56 };
57 
58 const std::vector<napi_property_descriptor> CameraFunctionsNapi::focus_query_props = {
59     DECLARE_NAPI_FUNCTION("isFocusModeSupported", CameraFunctionsNapi::IsFocusModeSupported)
60 };
61 
62 const std::vector<napi_property_descriptor> CameraFunctionsNapi::zoom_query_props = {
63     DECLARE_NAPI_FUNCTION("getZoomRatioRange", CameraFunctionsNapi::GetZoomRatioRange)
64 };
65 
66 const std::vector<napi_property_descriptor> CameraFunctionsNapi::beauty_query_props = {
67     DECLARE_NAPI_FUNCTION("getSupportedBeautyTypes", CameraFunctionsNapi::GetSupportedBeautyTypes),
68     DECLARE_NAPI_FUNCTION("getSupportedBeautyRange", CameraFunctionsNapi::GetSupportedBeautyRange)
69 };
70 
71 const std::vector<napi_property_descriptor> CameraFunctionsNapi::color_effect_query_props = {
72     DECLARE_NAPI_FUNCTION("getSupportedColorEffects", CameraFunctionsNapi::GetSupportedColorEffects)
73 };
74 
75 const std::vector<napi_property_descriptor> CameraFunctionsNapi::color_management_query_props = {
76     DECLARE_NAPI_FUNCTION("getSupportedColorSpaces", CameraFunctionsNapi::GetSupportedColorSpaces)
77 };
78 
79 const std::vector<napi_property_descriptor> CameraFunctionsNapi::macro_query_props = {
80     DECLARE_NAPI_FUNCTION("isMacroSupported", CameraFunctionsNapi::IsMacroSupported)
81 };
82 
83 const std::vector<napi_property_descriptor> CameraFunctionsNapi::depth_fusion_query_props = {
84     DECLARE_NAPI_FUNCTION("isDepthFusionSupported", CameraFunctionsNapi::IsDepthFusionSupported),
85     DECLARE_NAPI_FUNCTION("getDepthFusionThreshold", CameraFunctionsNapi::GetDepthFusionThreshold)
86 };
87 
88 const std::vector<napi_property_descriptor> CameraFunctionsNapi::portrait_query_props = {
89     DECLARE_NAPI_FUNCTION("getSupportedPortraitEffects", CameraFunctionsNapi::GetSupportedPortraitEffects)
90 };
91 
92 const std::vector<napi_property_descriptor> CameraFunctionsNapi::aperture_query_props = {
93     DECLARE_NAPI_FUNCTION("getSupportedVirtualApertures", CameraFunctionsNapi::GetSupportedVirtualApertures),
94     DECLARE_NAPI_FUNCTION("getSupportedPhysicalApertures", CameraFunctionsNapi::GetSupportedPhysicalApertures)
95 };
96 
97 const std::vector<napi_property_descriptor> CameraFunctionsNapi::stabilization_query_props = {
98     DECLARE_NAPI_FUNCTION("isVideoStabilizationModeSupported", CameraFunctionsNapi::IsVideoStabilizationModeSupported)
99 };
100 
101 const std::vector<napi_property_descriptor> CameraFunctionsNapi::manual_exposure_query_props = {
102     DECLARE_NAPI_FUNCTION("getSupportedExposureRange", CameraFunctionsNapi::GetSupportedExposureRange),
103 };
104 
105 const std::vector<napi_property_descriptor> CameraFunctionsNapi::features_query_props = {
106     DECLARE_NAPI_FUNCTION("isSceneFeatureSupported", CameraFunctionsNapi::IsFeatureSupported),
107 };
108 
109 const std::map<FunctionsType, Descriptor> CameraFunctionsNapi::functionsDescMap_ = {
110     {FunctionsType::PHOTO_FUNCTIONS, {flash_query_props, auto_exposure_query_props, focus_query_props, zoom_query_props,
111         beauty_query_props, color_effect_query_props, color_management_query_props, macro_query_props,
112         depth_fusion_query_props, manual_exposure_query_props, features_query_props}},
113     {FunctionsType::PORTRAIT_PHOTO_FUNCTIONS, {flash_query_props, auto_exposure_query_props, focus_query_props,
114         zoom_query_props, beauty_query_props, color_effect_query_props, color_management_query_props,
115         portrait_query_props, aperture_query_props, features_query_props}},
116     {FunctionsType::VIDEO_FUNCTIONS, {flash_query_props, auto_exposure_query_props, focus_query_props, zoom_query_props,
117         stabilization_query_props, beauty_query_props, color_effect_query_props, color_management_query_props,
118         macro_query_props, manual_exposure_query_props, features_query_props}},
119     {FunctionsType::PHOTO_CONFLICT_FUNCTIONS, {zoom_query_props, macro_query_props}},
120     {FunctionsType::PORTRAIT_PHOTO_CONFLICT_FUNCTIONS, {zoom_query_props, portrait_query_props, aperture_query_props}},
121     {FunctionsType::VIDEO_CONFLICT_FUNCTIONS, {zoom_query_props, macro_query_props}}};
122 
Init(napi_env env,napi_value exports,FunctionsType type)123 napi_value CameraFunctionsNapi::Init(napi_env env, napi_value exports, FunctionsType type)
124 {
125     MEDIA_DEBUG_LOG("Init is called");
126     napi_status status;
127     napi_value ctorObj;
128     int32_t refCount = 1;
129     std::vector<std::vector<napi_property_descriptor>> descriptors;
130     auto nameIt = functionsNameMap_.find(type);
131     if  (nameIt == functionsNameMap_.end()) {
132         MEDIA_ERR_LOG("Init call Failed, className not find");
133         return nullptr;
134     }
135     auto className = nameIt->second;
136 
137     auto descIt = functionsDescMap_.find(type);
138     if  (descIt == functionsDescMap_.end()) {
139         MEDIA_ERR_LOG("Init call Failed, descriptors not find");
140         return nullptr;
141     }
142     std::vector<napi_property_descriptor> camera_ability_props = CameraNapiUtils::GetPropertyDescriptor(descIt->second);
143 
144     status = napi_define_class(env, className, NAPI_AUTO_LENGTH,
145                                CameraFunctionsNapiConstructor, nullptr,
146                                camera_ability_props.size(),
147                                camera_ability_props.data(), &ctorObj);
148     if (status == napi_ok) {
149         if (type == FunctionsType::PHOTO_FUNCTIONS) {
150             status = napi_create_reference(env, ctorObj, refCount, &sPhotoConstructor_);
151         } else if (type == FunctionsType::PHOTO_CONFLICT_FUNCTIONS) {
152             status = napi_create_reference(env, ctorObj, refCount, &sPhotoConflictConstructor_);
153         } else if (type == FunctionsType::PORTRAIT_PHOTO_FUNCTIONS) {
154             status = napi_create_reference(env, ctorObj, refCount, &sPortraitPhotoConstructor_);
155         } else if (type == FunctionsType::PORTRAIT_PHOTO_CONFLICT_FUNCTIONS) {
156             status = napi_create_reference(env, ctorObj, refCount, &sPortraitPhotoConflictConstructor_);
157         } else if (type == FunctionsType::VIDEO_FUNCTIONS) {
158             status = napi_create_reference(env, ctorObj, refCount, &sVideoConstructor_);
159         } else if (type == FunctionsType::VIDEO_CONFLICT_FUNCTIONS) {
160             status = napi_create_reference(env, ctorObj, refCount, &sVideoConflictConstructor_);
161         } else {
162             return nullptr;
163         }
164 
165         if (status == napi_ok) {
166             status = napi_set_named_property(env, exports, className, ctorObj);
167             if (status == napi_ok) {
168                 return exports;
169             }
170         }
171     }
172     MEDIA_ERR_LOG("Init call Failed");
173     return nullptr;
174 }
175 
CreateCameraFunctions(napi_env env,sptr<CameraAbility> functions,FunctionsType type)176 napi_value CameraFunctionsNapi::CreateCameraFunctions(napi_env env, sptr<CameraAbility> functions, FunctionsType type)
177 {
178     MEDIA_DEBUG_LOG("CreateCameraFunctions is called");
179     napi_status status;
180     napi_value result = nullptr;
181     napi_value constructor;
182     if (type == FunctionsType::PHOTO_FUNCTIONS) {
183         status = napi_get_reference_value(env, sPhotoConstructor_, &constructor);
184     } else if (type == FunctionsType::PHOTO_CONFLICT_FUNCTIONS) {
185         status = napi_get_reference_value(env, sPhotoConflictConstructor_, &constructor);
186     } else if (type == FunctionsType::PORTRAIT_PHOTO_FUNCTIONS) {
187         status = napi_get_reference_value(env, sPortraitPhotoConstructor_, &constructor);
188     } else if (type == FunctionsType::PORTRAIT_PHOTO_CONFLICT_FUNCTIONS) {
189         status = napi_get_reference_value(env, sPortraitPhotoConflictConstructor_, &constructor);
190     } else if (type == FunctionsType::VIDEO_FUNCTIONS) {
191         status = napi_get_reference_value(env, sVideoConstructor_, &constructor);
192     } else if (type == FunctionsType::VIDEO_CONFLICT_FUNCTIONS) {
193         status = napi_get_reference_value(env, sVideoConflictConstructor_, &constructor);
194     } else {
195         MEDIA_ERR_LOG("CreateCameraFunctions call Failed type not find");
196         napi_get_undefined(env, &result);
197         return result;
198     }
199 
200     if (status == napi_ok) {
201         sCameraAbility_ = functions;
202         status = napi_new_instance(env, constructor, 0, nullptr, &result);
203         sCameraAbility_ = nullptr;
204         if (status == napi_ok && result != nullptr) {
205             return result;
206         } else {
207             MEDIA_ERR_LOG("Failed to create camera functions instance");
208         }
209     }
210     MEDIA_ERR_LOG("CreateCameraFunctions call Failed");
211     napi_get_undefined(env, &result);
212     return result;
213 }
214 
CameraFunctionsNapi()215 CameraFunctionsNapi::CameraFunctionsNapi() : env_(nullptr), wrapper_(nullptr) {}
216 
~CameraFunctionsNapi()217 CameraFunctionsNapi::~CameraFunctionsNapi()
218 {
219     MEDIA_DEBUG_LOG("~CameraFunctionsNapi is called");
220     if (wrapper_ != nullptr) {
221         napi_delete_reference(env_, wrapper_);
222     }
223     if (cameraAbility_) {
224         cameraAbility_ = nullptr;
225     }
226 }
227 
CameraFunctionsNapiConstructor(napi_env env,napi_callback_info info)228 napi_value CameraFunctionsNapi::CameraFunctionsNapiConstructor(napi_env env, napi_callback_info info)
229 {
230     MEDIA_DEBUG_LOG("CameraFunctionsNapiConstructor is called");
231     napi_status status;
232     napi_value result = nullptr;
233     napi_value thisVar = nullptr;
234 
235     napi_get_undefined(env, &result);
236     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
237 
238     if (status == napi_ok && thisVar != nullptr) {
239         std::unique_ptr<CameraFunctionsNapi> obj = std::make_unique<CameraFunctionsNapi>();
240         obj->env_ = env;
241         obj->cameraAbility_ = sCameraAbility_;
242         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
243             CameraFunctionsNapi::CameraFunctionsNapiDestructor, nullptr, nullptr);
244         if (status == napi_ok) {
245             obj.release();
246             return thisVar;
247         } else {
248             MEDIA_ERR_LOG("Failure wrapping js to native napi");
249         }
250     }
251     MEDIA_ERR_LOG("CameraFunctionsNapiConstructor call Failed");
252     return result;
253 }
254 
CameraFunctionsNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)255 void CameraFunctionsNapi::CameraFunctionsNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
256 {
257     MEDIA_DEBUG_LOG("CameraFunctionsNapiDestructor is called");
258     CameraFunctionsNapi* cameraAbilityNapi = reinterpret_cast<CameraFunctionsNapi*>(nativeObject);
259     if (cameraAbilityNapi != nullptr) {
260         delete cameraAbilityNapi;
261     }
262 }
263 
264 template<typename U>
HandleQuery(napi_env env,napi_callback_info info,napi_value thisVar,U queryFunction)265 napi_value CameraFunctionsNapi::HandleQuery(napi_env env, napi_callback_info info, napi_value thisVar, U queryFunction)
266 {
267     napi_status status;
268     napi_value result = nullptr;
269     napi_get_undefined(env, &result);
270     CameraFunctionsNapi* napiObj = nullptr;
271     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&napiObj));
272     if (status == napi_ok && napiObj != nullptr && napiObj->GetNativeObj() != nullptr) {
273         auto queryResult = queryFunction(napiObj->GetNativeObj());
274         if constexpr(std::is_same_v<decltype(queryResult), bool>) {
275             napi_get_boolean(env, queryResult, &result);
276         } else if constexpr(std::is_same_v<decltype(queryResult), std::vector<int32_t>>
277                          || std::is_enum_v<typename decltype(queryResult)::value_type>) {
278             status = napi_create_array(env, &result);
279             CHECK_ERROR_RETURN_RET_LOG(status != napi_ok, result, "napi_create_array call Failed!");
280             for (size_t i = 0; i < queryResult.size(); i++) {
281                 int32_t value = queryResult[i];
282                 napi_value element;
283                 napi_create_int32(env, value, &element);
284                 napi_set_element(env, result, i, element);
285             }
286         } else if constexpr(std::is_same_v<decltype(queryResult), std::vector<uint32_t>>) {
287             status = napi_create_array(env, &result);
288             CHECK_ERROR_RETURN_RET_LOG(status != napi_ok, result, "napi_create_array call Failed!");
289             for (size_t i = 0; i < queryResult.size(); i++) {
290                 uint32_t value = queryResult[i];
291                 napi_value element;
292                 napi_create_uint32(env, value, &element);
293                 napi_set_element(env, result, i, element);
294             }
295         } else if constexpr(std::is_same_v<decltype(queryResult), std::vector<float>>) {
296             status = napi_create_array(env, &result);
297             CHECK_ERROR_RETURN_RET_LOG(status != napi_ok, result, "napi_create_array call Failed!");
298             for (size_t i = 0; i < queryResult.size(); i++) {
299                 float value = queryResult[i];
300                 napi_value element;
301                 napi_create_double(env, CameraNapiUtils::FloatToDouble(value), &element);
302                 napi_set_element(env, result, i, element);
303             }
304         } else {
305             MEDIA_ERR_LOG("Unhandled type in HandleQuery");
306         }
307     } else {
308         MEDIA_ERR_LOG("Query function call Failed!");
309     }
310     return result;
311 }
312 
HasFlash(napi_env env,napi_callback_info info)313 napi_value CameraFunctionsNapi::HasFlash(napi_env env, napi_callback_info info)
314 {
315     MEDIA_DEBUG_LOG("HasFlash is called");
316     size_t argc = ARGS_ZERO;
317     napi_value argv[ARGS_ZERO];
318     napi_value thisVar = nullptr;
319     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
320 
321     return HandleQuery(env, info, thisVar, [](auto ability) {
322         return ability->HasFlash();
323     });
324 }
325 
IsFlashModeSupported(napi_env env,napi_callback_info info)326 napi_value CameraFunctionsNapi::IsFlashModeSupported(napi_env env, napi_callback_info info)
327 {
328     MEDIA_DEBUG_LOG("IsFlashModeSupported is called");
329     size_t argc = ARGS_ONE;
330     napi_value argv[ARGS_ONE];
331     napi_value thisVar = nullptr;
332     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
333 
334     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
335         int32_t value;
336         napi_get_value_int32(env, argv[PARAM0], &value);
337         FlashMode flashMode = (FlashMode)value;
338         return ability->IsFlashModeSupported(flashMode);
339     });
340 }
341 
IsLcdFlashSupported(napi_env env,napi_callback_info info)342 napi_value CameraFunctionsNapi::IsLcdFlashSupported(napi_env env, napi_callback_info info)
343 {
344     MEDIA_DEBUG_LOG("IsLcdFlashSupported is called");
345     size_t argc = ARGS_ZERO;
346     napi_value argv[ARGS_ZERO];
347     napi_value thisVar = nullptr;
348     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
349 
350     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
351         return ability->IsLcdFlashSupported();
352     });
353 }
354 
IsExposureModeSupported(napi_env env,napi_callback_info info)355 napi_value CameraFunctionsNapi::IsExposureModeSupported(napi_env env, napi_callback_info info)
356 {
357     MEDIA_DEBUG_LOG("IsExposureModeSupported is called");
358     size_t argc = ARGS_ONE;
359     napi_value argv[ARGS_ONE];
360     napi_value thisVar = nullptr;
361     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
362 
363     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
364         int32_t value;
365         napi_get_value_int32(env, argv[PARAM0], &value);
366         ExposureMode exposureMode = (ExposureMode)value;
367         return ability->IsExposureModeSupported(exposureMode);
368     });
369 }
370 
GetExposureBiasRange(napi_env env,napi_callback_info info)371 napi_value CameraFunctionsNapi::GetExposureBiasRange(napi_env env, napi_callback_info info)
372 {
373     MEDIA_DEBUG_LOG("GetExposureBiasRange is called");
374     size_t argc = ARGS_ZERO;
375     napi_value argv[ARGS_ZERO];
376     napi_value thisVar = nullptr;
377     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
378 
379     return HandleQuery(env, info, thisVar, [](auto ability) {
380         return ability->GetExposureBiasRange();
381     });
382 }
383 
IsFocusModeSupported(napi_env env,napi_callback_info info)384 napi_value CameraFunctionsNapi::IsFocusModeSupported(napi_env env, napi_callback_info info)
385 {
386     MEDIA_DEBUG_LOG("IsFocusModeSupported is called");
387     size_t argc = ARGS_ONE;
388     napi_value argv[ARGS_ONE];
389     napi_value thisVar = nullptr;
390     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
391 
392     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
393         int32_t value;
394         napi_get_value_int32(env, argv[PARAM0], &value);
395         FocusMode focusMode = (FocusMode)value;
396         return ability->IsFocusModeSupported(focusMode);
397     });
398 }
399 
GetZoomRatioRange(napi_env env,napi_callback_info info)400 napi_value CameraFunctionsNapi::GetZoomRatioRange(napi_env env, napi_callback_info info)
401 {
402     MEDIA_DEBUG_LOG("GetZoomRatioRange is called");
403     size_t argc = ARGS_ZERO;
404     napi_value argv[ARGS_ZERO];
405     napi_value thisVar = nullptr;
406     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
407 
408     return HandleQuery(env, info, thisVar, [](auto ability) {
409         return ability->GetZoomRatioRange();
410     });
411 }
412 
GetSupportedBeautyTypes(napi_env env,napi_callback_info info)413 napi_value CameraFunctionsNapi::GetSupportedBeautyTypes(napi_env env, napi_callback_info info)
414 {
415     MEDIA_DEBUG_LOG("GetSupportedBeautyTypes is called");
416     size_t argc = ARGS_ZERO;
417     napi_value argv[ARGS_ZERO];
418     napi_value thisVar = nullptr;
419     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
420 
421     return HandleQuery(env, info, thisVar, [](auto ability) {
422         return ability->GetSupportedBeautyTypes();
423     });
424 }
425 
GetSupportedBeautyRange(napi_env env,napi_callback_info info)426 napi_value CameraFunctionsNapi::GetSupportedBeautyRange(napi_env env, napi_callback_info info)
427 {
428     MEDIA_DEBUG_LOG("GetSupportedBeautyRange is called");
429     size_t argc = ARGS_ONE;
430     napi_value argv[ARGS_ONE];
431     napi_value thisVar = nullptr;
432     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
433 
434     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
435         int32_t value;
436         napi_get_value_int32(env, argv[PARAM0], &value);
437         BeautyType beautyType = (BeautyType)value;
438         return ability->GetSupportedBeautyRange(beautyType);
439     });
440 }
441 
GetSupportedColorEffects(napi_env env,napi_callback_info info)442 napi_value CameraFunctionsNapi::GetSupportedColorEffects(napi_env env, napi_callback_info info)
443 {
444     MEDIA_DEBUG_LOG("GetSupportedColorEffects is called");
445     size_t argc = ARGS_ZERO;
446     napi_value argv[ARGS_ZERO];
447     napi_value thisVar = nullptr;
448     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
449 
450     return HandleQuery(env, info, thisVar, [](auto ability) {
451         return ability->GetSupportedColorEffects();
452     });
453 }
454 
GetSupportedColorSpaces(napi_env env,napi_callback_info info)455 napi_value CameraFunctionsNapi::GetSupportedColorSpaces(napi_env env, napi_callback_info info)
456 {
457     MEDIA_DEBUG_LOG("GetSupportedColorSpaces is called.");
458     size_t argc = ARGS_ZERO;
459     napi_value argv[ARGS_ZERO];
460     napi_value thisVar = nullptr;
461     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
462 
463     return HandleQuery(env, info, thisVar, [](auto ability) {
464         return ability->GetSupportedColorSpaces();
465     });
466 }
467 
IsMacroSupported(napi_env env,napi_callback_info info)468 napi_value CameraFunctionsNapi::IsMacroSupported(napi_env env, napi_callback_info info)
469 {
470     size_t argc = ARGS_ZERO;
471     napi_value argv[ARGS_ZERO];
472     napi_value thisVar = nullptr;
473     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
474 
475     return HandleQuery(env, info, thisVar, [](auto ability) {
476         return ability->IsMacroSupported();
477     });
478 }
479 
IsDepthFusionSupported(napi_env env,napi_callback_info info)480 napi_value CameraFunctionsNapi::IsDepthFusionSupported(napi_env env, napi_callback_info info)
481 {
482     if (!CameraNapiSecurity::CheckSystemApp(env)) {
483         MEDIA_ERR_LOG("SystemApi IsDepthFusionSupported is called!");
484         return nullptr;
485     }
486     napi_value thisVar = nullptr;
487     CameraFunctionsNapi* cameraFunctionsNapi = nullptr;
488     CameraNapiParamParser jsParamParser(env, info, cameraFunctionsNapi);
489     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
490         MEDIA_ERR_LOG("CameraFunctionsNapi::IsDepthFusionSupported parse parameter occur error");
491         return nullptr;
492     }
493     thisVar = jsParamParser.GetThisVar();
494 
495     return HandleQuery(env, info, thisVar, [](auto ability) {
496         return ability->IsDepthFusionSupported();
497     });
498 }
499 
GetDepthFusionThreshold(napi_env env,napi_callback_info info)500 napi_value CameraFunctionsNapi::GetDepthFusionThreshold(napi_env env, napi_callback_info info)
501 {
502     if (!CameraNapiSecurity::CheckSystemApp(env)) {
503         MEDIA_ERR_LOG("SystemApi GetDepthFusionThreshold is called!");
504         return nullptr;
505     }
506     napi_value thisVar = nullptr;
507     CameraFunctionsNapi* cameraFunctionsNapi = nullptr;
508     CameraNapiParamParser jsParamParser(env, info, cameraFunctionsNapi);
509     if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
510         MEDIA_ERR_LOG("CameraFunctionsNapi::GetDepthFusionThreshold parse parameter occur error");
511         return nullptr;
512     }
513     thisVar = jsParamParser.GetThisVar();
514 
515     return HandleQuery(env, info, thisVar, [](auto ability) {
516         return ability->GetDepthFusionThreshold();
517     });
518 }
519 
GetSupportedPortraitEffects(napi_env env,napi_callback_info info)520 napi_value CameraFunctionsNapi::GetSupportedPortraitEffects(napi_env env, napi_callback_info info)
521 {
522     size_t argc = ARGS_ZERO;
523     napi_value argv[ARGS_ZERO];
524     napi_value thisVar = nullptr;
525     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
526 
527     return HandleQuery(env, info, thisVar, [](auto ability) {
528         return ability->GetSupportedPortraitEffects();
529     });
530 }
531 
GetSupportedVirtualApertures(napi_env env,napi_callback_info info)532 napi_value CameraFunctionsNapi::GetSupportedVirtualApertures(napi_env env, napi_callback_info info)
533 {
534     size_t argc = ARGS_ZERO;
535     napi_value argv[ARGS_ZERO];
536     napi_value thisVar = nullptr;
537     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
538 
539     return HandleQuery(env, info, thisVar, [](auto ability) {
540         return ability->GetSupportedVirtualApertures();
541     });
542 }
543 
GetSupportedPhysicalApertures(napi_env env,napi_callback_info info)544 napi_value CameraFunctionsNapi::GetSupportedPhysicalApertures(napi_env env, napi_callback_info info)
545 {
546     MEDIA_DEBUG_LOG("GetSupportedPhysicalApertures is called");
547     size_t argc = ARGS_ZERO;
548     napi_value argv[ARGS_ZERO];
549     napi_value thisVar = nullptr;
550     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
551 
552     napi_status status;
553     napi_value result = nullptr;
554     napi_get_undefined(env, &result);
555     status = napi_create_array(env, &result);
556     if (status != napi_ok) {
557         MEDIA_ERR_LOG("napi_create_array call Failed!");
558         return result;
559     }
560     CameraFunctionsNapi*  cameraAbilityNapi = nullptr;
561     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&cameraAbilityNapi));
562     if (status == napi_ok && cameraAbilityNapi != nullptr && cameraAbilityNapi->GetNativeObj() !=nullptr) {
563         std::vector<std::vector<float>> physicalApertures =
564             cameraAbilityNapi->GetNativeObj()->GetSupportedPhysicalApertures();
565         MEDIA_INFO_LOG("GetSupportedPhysicalApertures len = %{public}zu", physicalApertures.size());
566         if (!physicalApertures.empty()) {
567             result = CameraNapiUtils::ProcessingPhysicalApertures(env, physicalApertures);
568         }
569     } else {
570         MEDIA_ERR_LOG("GetSupportedPhysicalApertures call Failed!");
571     }
572     return result;
573 }
574 
IsVideoStabilizationModeSupported(napi_env env,napi_callback_info info)575 napi_value CameraFunctionsNapi::IsVideoStabilizationModeSupported(napi_env env, napi_callback_info info)
576 {
577     MEDIA_DEBUG_LOG("IsVideoStabilizationModeSupported is called");
578     size_t argc = ARGS_ONE;
579     napi_value argv[ARGS_ONE];
580     napi_value thisVar = nullptr;
581     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
582 
583     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
584         int32_t value;
585         napi_get_value_int32(env, argv[PARAM0], &value);
586         VideoStabilizationMode stabilizationMode = (VideoStabilizationMode)value;
587         return ability->IsVideoStabilizationModeSupported(stabilizationMode);
588     });
589 }
590 
GetSupportedExposureRange(napi_env env,napi_callback_info info)591 napi_value CameraFunctionsNapi::GetSupportedExposureRange(napi_env env, napi_callback_info info)
592 {
593     MEDIA_DEBUG_LOG("GetSupportedExposureRange is called");
594     size_t argc = ARGS_ZERO;
595     napi_value argv[ARGS_ZERO];
596     napi_value thisVar = nullptr;
597     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
598 
599     return HandleQuery(env, info, thisVar, [](auto ability) {
600         return ability->GetSupportedExposureRange();
601     });
602 }
603 
IsFeatureSupported(napi_env env,napi_callback_info info)604 napi_value CameraFunctionsNapi::IsFeatureSupported(napi_env env, napi_callback_info info)
605 {
606     MEDIA_DEBUG_LOG("IsFeatureSupported is called");
607     size_t argc = ARGS_ONE;
608     napi_value argv[ARGS_ONE];
609     napi_value thisVar = nullptr;
610     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
611 
612     return HandleQuery(env, info, thisVar, [env, argv](auto ability) {
613         int32_t value;
614         napi_get_value_int32(env, argv[PARAM0], &value);
615         SceneFeature sceneFeature = (SceneFeature)value;
616         return ability->IsFeatureSupported(sceneFeature);
617     });
618 }
619 } // namespace CameraStandard
620 } // namespace OHOS