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/fluorescence_photo_session_napi.h"
17 
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 
22 thread_local napi_ref FluorescencePhotoSessionNapi::sConstructor_ = nullptr;
23 
FluorescencePhotoSessionNapi()24 FluorescencePhotoSessionNapi::FluorescencePhotoSessionNapi() : env_(nullptr)
25 {
26 }
27 
~FluorescencePhotoSessionNapi()28 FluorescencePhotoSessionNapi::~FluorescencePhotoSessionNapi()
29 {
30     MEDIA_DEBUG_LOG("~FluorescencePhotoSessionNapi is called");
31 }
32 
FluorescencePhotoSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)33 void FluorescencePhotoSessionNapi::FluorescencePhotoSessionNapiDestructor(napi_env env,
34     void* nativeObject, void* finalize_hint)
35 {
36     MEDIA_DEBUG_LOG("FluorescencePhotoSessionNapiDestructor is called");
37     FluorescencePhotoSessionNapi* cameraObj = reinterpret_cast<FluorescencePhotoSessionNapi*>(nativeObject);
38     if (cameraObj != nullptr) {
39         delete cameraObj;
40     }
41 }
42 
Init(napi_env env,napi_value exports)43 napi_value FluorescencePhotoSessionNapi::Init(napi_env env, napi_value exports)
44 {
45     MEDIA_DEBUG_LOG("Init is called");
46     napi_status status;
47     napi_value ctorObj;
48     std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props,
49         auto_exposure_props, focus_props, zoom_props};
50     std::vector<napi_property_descriptor> fluorescence_photo_session_props =
51         CameraNapiUtils::GetPropertyDescriptor(descriptors);
52     status = napi_define_class(env, FLUORESCENCE_PHOTO_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
53                                FluorescencePhotoSessionNapiConstructor, nullptr,
54                                fluorescence_photo_session_props.size(),
55                                fluorescence_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, FLUORESCENCE_PHOTO_SESSION_NAPI_CLASS_NAME, ctorObj);
61             if (status == napi_ok) {
62                 return exports;
63             }
64         }
65     }
66     MEDIA_ERR_LOG("Init call Failed!");
67     return nullptr;
68 }
69 
CreateCameraSession(napi_env env)70 napi_value FluorescencePhotoSessionNapi::CreateCameraSession(napi_env env)
71 {
72     MEDIA_DEBUG_LOG("CreateCameraSession is called");
73     CAMERA_SYNC_TRACE;
74     napi_status status;
75     napi_value result = nullptr;
76     napi_value constructor;
77     status = napi_get_reference_value(env, sConstructor_, &constructor);
78     if (status == napi_ok) {
79         sCameraSession_ = CameraManager::GetInstance()->CreateCaptureSession(SceneMode::FLUORESCENCE_PHOTO);
80         if (sCameraSession_ == nullptr) {
81             MEDIA_ERR_LOG("Failed to create Fluorescence photo session instance");
82             napi_get_undefined(env, &result);
83             return result;
84         }
85         status = napi_new_instance(env, constructor, 0, nullptr, &result);
86         sCameraSession_ = nullptr;
87         if (status == napi_ok && result != nullptr) {
88             MEDIA_DEBUG_LOG("success to create Fluorescence photo session napi instance");
89             return result;
90         } else {
91             MEDIA_ERR_LOG("Failed to create Fluorescence photo session napi instance");
92         }
93     } else {
94         MEDIA_ERR_LOG("Failed to create napi reference value instance");
95     }
96     MEDIA_ERR_LOG("Failed to create Fluorescence photo session napi instance last");
97     napi_get_undefined(env, &result);
98     return result;
99 }
100 
FluorescencePhotoSessionNapiConstructor(napi_env env,napi_callback_info info)101 napi_value FluorescencePhotoSessionNapi::FluorescencePhotoSessionNapiConstructor(napi_env env, napi_callback_info info)
102 {
103     MEDIA_DEBUG_LOG("FluorescencePhotoSessionNapiConstructor 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<FluorescencePhotoSessionNapi> obj = std::make_unique<FluorescencePhotoSessionNapi>();
113         obj->env_ = env;
114         if (sCameraSession_ == nullptr) {
115             MEDIA_ERR_LOG("sCameraSession_ is null");
116             return result;
117         }
118         obj->fluorescencePhotoSession_ = static_cast<FluorescencePhotoSession*>(sCameraSession_.GetRefPtr());
119         obj->cameraSession_ = obj->fluorescencePhotoSession_;
120         if (obj->fluorescencePhotoSession_ == nullptr) {
121             MEDIA_ERR_LOG("fluorescencePhotoSession_ is null");
122             return result;
123         }
124         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
125             FluorescencePhotoSessionNapi::FluorescencePhotoSessionNapiDestructor, nullptr, nullptr);
126         if (status == napi_ok) {
127             obj.release();
128             return thisVar;
129         } else {
130             MEDIA_ERR_LOG("FluorescencePhotoSessionNapi Failure wrapping js to native napi");
131         }
132     }
133     MEDIA_ERR_LOG("FluorescencePhotoSessionNapi call Failed!");
134     return result;
135 }
136 } // namespace CameraStandard
137 } // namespace OHOS