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