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 "mode/quick_shot_photo_session_napi.h"
17
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21
22 thread_local napi_ref QuickShotPhotoSessionNapi::sConstructor_ = nullptr;
23
QuickShotPhotoSessionNapi()24 QuickShotPhotoSessionNapi::QuickShotPhotoSessionNapi() : env_(nullptr), wrapper_(nullptr) {}
25
~QuickShotPhotoSessionNapi()26 QuickShotPhotoSessionNapi::~QuickShotPhotoSessionNapi()
27 {
28 MEDIA_DEBUG_LOG("~QuickShotPhotoSessionNapi is called");
29 CHECK_EXECUTE(wrapper_ != nullptr, napi_delete_reference(env_, wrapper_));
30 }
31
QuickShotPhotoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)32 void QuickShotPhotoSessionNapi::QuickShotPhotoSessionNapiDestructor(
33 napi_env env, void* nativeObject, void* finalize_hint)
34 {
35 MEDIA_DEBUG_LOG("QuickShotPhotoSessionNapiDestructor is called");
36 QuickShotPhotoSessionNapi* cameraObj = reinterpret_cast<QuickShotPhotoSessionNapi*>(nativeObject);
37 if (cameraObj != nullptr) {
38 delete cameraObj;
39 }
40 }
41
Init(napi_env env,napi_value exports)42 napi_value QuickShotPhotoSessionNapi::Init(napi_env env, napi_value exports)
43 {
44 MEDIA_DEBUG_LOG("Init is called");
45 napi_status status;
46 napi_value ctorObj;
47 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, auto_exposure_props,
48 color_effect_props, color_management_props, effect_suggestion_props, flash_props, focus_props, zoom_props };
49
50 std::vector<napi_property_descriptor> quick_shot_photo_session_props =
51 CameraNapiUtils::GetPropertyDescriptor(descriptors);
52
53 status = napi_define_class(env, QUICK_SHOT_PHOTO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
54 QuickShotPhotoSessionNapiConstructor, nullptr, quick_shot_photo_session_props.size(),
55 quick_shot_photo_session_props.data(), &ctorObj);
56 if (status == napi_ok) {
57 int32_t refCount = 1;
58 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
59 if (status == napi_ok) {
60 status = napi_set_named_property(env, exports, QUICK_SHOT_PHOTO_SESSION_NAPI_CLASS_NAME, ctorObj);
61 CHECK_ERROR_RETURN_RET(status == napi_ok, exports);
62 }
63 }
64 MEDIA_ERR_LOG("Init call Failed!");
65 return nullptr;
66 }
67
CreateCameraSession(napi_env env)68 napi_value QuickShotPhotoSessionNapi::CreateCameraSession(napi_env env)
69 {
70 MEDIA_DEBUG_LOG("CreateCameraSession is called");
71 CAMERA_SYNC_TRACE;
72 napi_status status;
73 napi_value result = nullptr;
74 napi_value constructor;
75 status = napi_get_reference_value(env, sConstructor_, &constructor);
76 if (status == napi_ok) {
77 sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::QUICK_SHOT_PHOTO);
78 if (sCameraSession_ == nullptr) {
79 MEDIA_ERR_LOG("Failed to create Photo session instance");
80 napi_get_undefined(env, &result);
81 return result;
82 }
83 status = napi_new_instance(env, constructor, 0, nullptr, &result);
84 sCameraSession_ = nullptr;
85 if (status == napi_ok && result != nullptr) {
86 MEDIA_DEBUG_LOG("success to create Photo session napi instance");
87 return result;
88 } else {
89 MEDIA_ERR_LOG("Failed to create Photo session napi instance");
90 }
91 }
92 MEDIA_ERR_LOG("Failed to create Photo session napi instance last");
93 napi_get_undefined(env, &result);
94 return result;
95 }
96
QuickShotPhotoSessionNapiConstructor(napi_env env,napi_callback_info info)97 napi_value QuickShotPhotoSessionNapi::QuickShotPhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
98 {
99 MEDIA_DEBUG_LOG("QuickShotPhotoSessionNapiConstructor is called");
100 napi_status status;
101 napi_value result = nullptr;
102 napi_value thisVar = nullptr;
103
104 napi_get_undefined(env, &result);
105 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
106
107 if (status == napi_ok && thisVar != nullptr) {
108 std::unique_ptr<QuickShotPhotoSessionNapi> obj = std::make_unique<QuickShotPhotoSessionNapi>();
109 obj->env_ = env;
110 CHECK_ERROR_RETURN_RET_LOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
111 obj->quickShotPhotoSession_ = static_cast<QuickShotPhotoSession*>(sCameraSession_.GetRefPtr());
112 obj->cameraSession_ = obj->quickShotPhotoSession_;
113 CHECK_ERROR_RETURN_RET_LOG(obj->quickShotPhotoSession_ == nullptr, result, "quickShotPhotoSession_ is null");
114 status = napi_wrap(
115 env, thisVar, reinterpret_cast<void*>(obj.get()), QuickShotPhotoSessionNapiDestructor, nullptr, nullptr);
116 if (status == napi_ok) {
117 obj.release();
118 return thisVar;
119 } else {
120 MEDIA_ERR_LOG("QuickShotPhotoSessionNapiConstructor Failure wrapping js to native napi");
121 }
122 }
123 MEDIA_ERR_LOG("QuickShotPhotoSessionNapiConstructor call Failed!");
124 return result;
125 }
126 } // namespace CameraStandard
127 } // namespace OHOS
128