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/secure_camera_session_napi.h"
17 
18 namespace OHOS {
19 namespace CameraStandard {
20 using namespace std;
21 
22 thread_local napi_ref SecureCameraSessionNapi::sConstructor_ = nullptr;
23 
SecureCameraSessionNapi()24 SecureCameraSessionNapi::SecureCameraSessionNapi() : env_(nullptr) {}
~SecureCameraSessionNapi()25 SecureCameraSessionNapi::~SecureCameraSessionNapi()
26 {
27     MEDIA_DEBUG_LOG("~SecureCameraSessionNapi is called");
28 }
SecureCameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)29 void SecureCameraSessionNapi::SecureCameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
30 {
31     MEDIA_DEBUG_LOG("SecureCameraSessionNapiDestructor is called");
32     SecureCameraSessionNapi* cameraObj = reinterpret_cast<SecureCameraSessionNapi*>(nativeObject);
33     if (cameraObj != nullptr) {
34         delete cameraObj;
35     }
36 }
Init(napi_env env,napi_value exports)37 napi_value SecureCameraSessionNapi::Init(napi_env env, napi_value exports)
38 {
39     MEDIA_DEBUG_LOG("Init is called");
40     napi_status status;
41     napi_value ctorObj;
42     std::vector<napi_property_descriptor> manual_exposure_props = {
43             DECLARE_NAPI_FUNCTION("addSecureOutput", SecureCameraSessionNapi::AddSecureOutput)
44     };
45     std::vector<std::vector<napi_property_descriptor>> descriptors = {camera_process_props, stabilization_props,
46         flash_props, auto_exposure_props, focus_props, zoom_props, filter_props, beauty_props,
47         color_effect_props, macro_props, color_management_props, manual_exposure_props};
48     std::vector<napi_property_descriptor> secure_camera_session_props =
49         CameraNapiUtils::GetPropertyDescriptor(descriptors);
50     status = napi_define_class(env, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
51         SecureCameraSessionNapiConstructor, nullptr,
52         secure_camera_session_props.size(),
53         secure_camera_session_props.data(), &ctorObj);
54     if (status == napi_ok) {
55         int32_t refCount = 1;
56         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
57         if (status == napi_ok) {
58             status = napi_set_named_property(env, exports, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, ctorObj);
59             if (status == napi_ok) {
60                 return exports;
61             }
62         }
63     }
64     MEDIA_ERR_LOG("Init call Failed!");
65     return nullptr;
66 }
67 
CreateCameraSession(napi_env env)68 napi_value SecureCameraSessionNapi::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::SECURE);
78         if (sCameraSession_ == nullptr) {
79             MEDIA_ERR_LOG("Failed to create Camera 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 Camera session napi instance");
87             return result;
88         } else {
89             MEDIA_ERR_LOG("Failed to create Camera session napi instance");
90         }
91     }
92     MEDIA_ERR_LOG("Failed to create Camera session napi instance last");
93     napi_get_undefined(env, &result);
94     return result;
95 }
96 
AddSecureOutput(napi_env env,napi_callback_info info)97 napi_value SecureCameraSessionNapi::AddSecureOutput(napi_env env, napi_callback_info info)
98 {
99     MEDIA_INFO_LOG("AddSecureOutput is called");
100     napi_status status;
101     napi_value result = nullptr;
102     size_t argc = ARGS_ONE;
103     napi_value argv[ARGS_ONE] = {0};
104     napi_value thisVar = nullptr;
105 
106     CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
107     if (!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT)) {
108         return result;
109     }
110 
111     napi_get_undefined(env, &result);
112     SecureCameraSessionNapi* secureCameraSessionNapi = nullptr;
113     status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&secureCameraSessionNapi));
114     if (status == napi_ok && secureCameraSessionNapi != nullptr) {
115         sptr<CaptureOutput> cameraOutput = nullptr;
116         MEDIA_INFO_LOG("AddSecureOutput GetJSArgsForCameraOutput is called");
117         result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
118         int32_t ret = secureCameraSessionNapi->secureCameraSession_->AddSecureOutput(cameraOutput);
119         if (!CameraNapiUtils::CheckError(env, ret)) {
120             return nullptr;
121         }
122     } else {
123         MEDIA_ERR_LOG("AddOutput call Failed!");
124     }
125     return result;
126 }
127 
SecureCameraSessionNapiConstructor(napi_env env,napi_callback_info info)128 napi_value SecureCameraSessionNapi::SecureCameraSessionNapiConstructor(napi_env env, napi_callback_info info)
129 {
130     MEDIA_DEBUG_LOG("SecureCameraSessionNapiConstructor is called");
131     napi_status status;
132     napi_value result = nullptr;
133     napi_value thisVar = nullptr;
134 
135     napi_get_undefined(env, &result);
136     CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
137 
138     if (status == napi_ok && thisVar != nullptr) {
139         std::unique_ptr<SecureCameraSessionNapi> obj = std::make_unique<SecureCameraSessionNapi>();
140         obj->env_ = env;
141         if (sCameraSession_ == nullptr) {
142             MEDIA_ERR_LOG("sCameraSession_ is null");
143             return result;
144         }
145         obj->secureCameraSession_ = static_cast<SecureCameraSession*>(sCameraSession_.GetRefPtr());
146         obj->cameraSession_ = obj->secureCameraSession_;
147         if (obj->secureCameraSession_ == nullptr) {
148             MEDIA_ERR_LOG("secureCameraSession_ is null");
149             return result;
150         }
151         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
152                            SecureCameraSessionNapi::SecureCameraSessionNapiDestructor, nullptr, nullptr);
153         if (status == napi_ok) {
154             obj.release();
155             return thisVar;
156         } else {
157             MEDIA_ERR_LOG("SecureCameraSessionNapi Failure wrapping js to native napi");
158         }
159     }
160     MEDIA_ERR_LOG("SecureCameraSessionNapi call Failed!");
161     return result;
162 }
163 } // namespace CameraStandard
164 } // namespace OHOS