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 #include "input/camera_manager.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 using namespace std;
23
24 thread_local napi_ref SecureCameraSessionNapi::sConstructor_ = nullptr;
25
SecureCameraSessionNapi()26 SecureCameraSessionNapi::SecureCameraSessionNapi() : env_(nullptr) {}
~SecureCameraSessionNapi()27 SecureCameraSessionNapi::~SecureCameraSessionNapi()
28 {
29 MEDIA_DEBUG_LOG("~SecureCameraSessionNapi is called");
30 }
SecureCameraSessionNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)31 void SecureCameraSessionNapi::SecureCameraSessionNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
32 {
33 MEDIA_DEBUG_LOG("SecureCameraSessionNapiDestructor is called");
34 SecureCameraSessionNapi* cameraObj = reinterpret_cast<SecureCameraSessionNapi*>(nativeObject);
35 if (cameraObj != nullptr) {
36 delete cameraObj;
37 }
38 }
Init(napi_env env,napi_value exports)39 napi_value SecureCameraSessionNapi::Init(napi_env env, napi_value exports)
40 {
41 MEDIA_DEBUG_LOG("Init is called");
42 napi_status status;
43 napi_value ctorObj;
44 std::vector<napi_property_descriptor> add_secure_output_props = {
45 DECLARE_NAPI_FUNCTION("addSecureOutput", SecureCameraSessionNapi::AddSecureOutput)
46 };
47 std::vector<std::vector<napi_property_descriptor>> descriptors = { camera_process_props, camera_process_sys_props,
48 stabilization_props, flash_props, flash_sys_props, auto_exposure_props, focus_props, focus_sys_props,
49 zoom_props, zoom_sys_props, filter_props, macro_props, color_management_props, add_secure_output_props,
50 white_balance_props };
51 std::vector<napi_property_descriptor> secure_camera_session_props =
52 CameraNapiUtils::GetPropertyDescriptor(descriptors);
53 status = napi_define_class(env, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
54 SecureCameraSessionNapiConstructor, nullptr,
55 secure_camera_session_props.size(),
56 secure_camera_session_props.data(), &ctorObj);
57 if (status == napi_ok) {
58 status = NapiRefManager::CreateMemSafetyRef(env, ctorObj, &sConstructor_);
59 if (status == napi_ok) {
60 status = napi_set_named_property(env, exports, SECURE_CAMERA_SESSION_NAPI_CLASS_NAME, ctorObj);
61 CHECK_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 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_value argv[ARGS_ONE] = {0};
101 napi_status status;
102 napi_value result = nullptr;
103 size_t argc = ARGS_ONE;
104 napi_value thisVar = nullptr;
105
106 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
107 CHECK_RETURN_RET(!CameraNapiUtils::CheckInvalidArgument(env, argc, ARGS_ONE, argv, ADD_OUTPUT), result);
108
109 napi_get_undefined(env, &result);
110 SecureCameraSessionNapi* secureCameraSessionNapi = nullptr;
111 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&secureCameraSessionNapi));
112 if (status == napi_ok && secureCameraSessionNapi != nullptr) {
113 sptr<CaptureOutput> cameraOutput = nullptr;
114 MEDIA_INFO_LOG("AddSecureOutput GetJSArgsForCameraOutput is called");
115 result = GetJSArgsForCameraOutput(env, argc, argv, cameraOutput);
116 CHECK_EXECUTE(cameraOutput == nullptr, NAPI_ASSERT(env, false, "type mismatch"));
117 int32_t ret = secureCameraSessionNapi->secureCameraSession_->AddSecureOutput(cameraOutput);
118 CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, ret), nullptr);
119 } else {
120 MEDIA_ERR_LOG("AddOutput call Failed!");
121 }
122 return result;
123 }
124
SecureCameraSessionNapiConstructor(napi_env env,napi_callback_info info)125 napi_value SecureCameraSessionNapi::SecureCameraSessionNapiConstructor(napi_env env, napi_callback_info info)
126 {
127 MEDIA_DEBUG_LOG("SecureCameraSessionNapiConstructor is called");
128 napi_status status;
129 napi_value result = nullptr;
130 napi_value thisVar = nullptr;
131
132 napi_get_undefined(env, &result);
133 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
134
135 if (status == napi_ok && thisVar != nullptr) {
136 std::unique_ptr<SecureCameraSessionNapi> obj = std::make_unique<SecureCameraSessionNapi>();
137 obj->env_ = env;
138 CHECK_RETURN_RET_ELOG(sCameraSession_ == nullptr, result, "sCameraSession_ is null");
139 obj->secureCameraSession_ = static_cast<SecureCameraSession*>(sCameraSession_.GetRefPtr());
140 obj->cameraSession_ = obj->secureCameraSession_;
141 CHECK_RETURN_RET_ELOG(obj->secureCameraSession_ == nullptr, result, "secureCameraSession_ is null");
142 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
143 SecureCameraSessionNapi::SecureCameraSessionNapiDestructor, nullptr, nullptr);
144 if (status == napi_ok) {
145 obj.release();
146 return thisVar;
147 } else {
148 MEDIA_ERR_LOG("SecureCameraSessionNapi Failure wrapping js to native napi");
149 }
150 }
151 MEDIA_ERR_LOG("SecureCameraSessionNapi call Failed!");
152 return result;
153 }
154 } // namespace CameraStandard
155 } // namespace OHOS