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