1 /*
2 * Copyright (c) 2025-2025 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 "camera_log.h"
17 #include "camera_manager.h"
18 #include "camera_napi_param_parser.h"
19 #include "control_center_session.h"
20 #include "js_native_api.h"
21 #include "js_native_api_types.h"
22 #include "napi/native_common.h"
23 #include "session/camera_session_napi.h"
24 #include "session/control_center_session_napi.h"
25 #include <cstddef>
26 #include <memory>
27 #include <cstdint>
28 #include <vector>
29 #include "capture_session.h"
30 #include "camera_napi_security_utils.h"
31
32 namespace OHOS {
33 namespace CameraStandard {
34
AsyncCompleteCallback(napi_env env,napi_status status,void * data)35 void AsyncCompleteCallback(napi_env env, napi_status status, void* data)
36 {
37 auto context = static_cast<ControlCenterSessionAsyncContext*>(data);
38 CHECK_RETURN_ELOG(context == nullptr, "ControlCenterSessionNapi AsyncCompleteCallback context is null");
39 MEDIA_INFO_LOG("ControlCenterSessionNapi AsyncCompleteCallback %{public}s, status = %{public}d",
40 context->funcName.c_str(), context->status);
41 std::unique_ptr<JSAsyncContextOutput> jsContext = std::make_unique<JSAsyncContextOutput>();
42 jsContext->status = context->status;
43 if (!context->status) {
44 CameraNapiUtils::CreateNapiErrorObject(env, context->errorCode, context->errorMsg.c_str(), jsContext);
45 } else {
46 napi_get_undefined(env, &jsContext->data);
47 }
48 if (!context->funcName.empty() && context->taskId > 0) {
49 // Finish async trace
50 CAMERA_FINISH_ASYNC_TRACE(context->funcName, context->taskId);
51 jsContext->funcName = context->funcName;
52 }
53 if (context->work != nullptr) {
54 CameraNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef, context->work, *jsContext);
55 }
56 context->FreeHeldNapiValue(env);
57 delete context;
58 }
59
60 thread_local napi_ref ControlCenterSessionNapi::sConstructor_ = nullptr;
61 thread_local sptr<ControlCenterSession> ControlCenterSessionNapi::sControlCenterSession_ = nullptr;
62 thread_local uint32_t ControlCenterSessionNapi::controlCenterSessionTaskId = CONTROL_CENTER_SESSION_TASKID;
63
ControlCenterSessionNapi()64 ControlCenterSessionNapi::ControlCenterSessionNapi() : env_(nullptr) {}
65
~ControlCenterSessionNapi()66 ControlCenterSessionNapi::~ControlCenterSessionNapi()
67 {
68 MEDIA_INFO_LOG("~ControlCenterSessionNapi is called");
69 }
70
ControlCenterSessionDestructor(napi_env env,void * nativeObject,void * finalize_hint)71 void ControlCenterSessionNapi::ControlCenterSessionDestructor(napi_env env, void* nativeObject, void* finalize_hint)
72 {
73 MEDIA_INFO_LOG("ControlCenterSessionDestructor is called");
74 ControlCenterSessionNapi* cameraObj = reinterpret_cast<ControlCenterSessionNapi*>(nativeObject);
75 if (cameraObj != nullptr) {
76 cameraObj->controlCenterSession_ = nullptr;
77 delete cameraObj;
78 }
79 }
80
ControlCenterSessionConstructor(napi_env env,napi_callback_info info)81 napi_value ControlCenterSessionNapi::ControlCenterSessionConstructor(napi_env env, napi_callback_info info)
82 {
83 MEDIA_INFO_LOG("ControlCenterSessionConstructor is called");
84 napi_status status;
85 napi_value result = nullptr;
86 napi_value thisVar = nullptr;
87
88 napi_get_undefined(env, &result);
89 CAMERA_NAPI_GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
90
91 if (status == napi_ok && thisVar != nullptr) {
92 std::unique_ptr<ControlCenterSessionNapi> obj = std::make_unique<ControlCenterSessionNapi>();
93 obj->env_ = env;
94 if (sControlCenterSession_ == nullptr) {
95 MEDIA_ERR_LOG("sControlCenterSession_ is null");
96 return result;
97 }
98 obj->controlCenterSession_ = sControlCenterSession_;
99 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
100 ControlCenterSessionNapi::ControlCenterSessionDestructor, nullptr, nullptr);
101 if (status == napi_ok) {
102 (void) obj.release();
103 return thisVar;
104 } else {
105 MEDIA_ERR_LOG("ControlCenterSessionConstructor failed wrapping js to native napi");
106 }
107 }
108 MEDIA_ERR_LOG("ControlCenterSessionConstructor call Failed!");
109 return result;
110 }
111
Init(napi_env env,napi_value exports)112 napi_value ControlCenterSessionNapi::Init(napi_env env, napi_value exports)
113 {
114 MEDIA_INFO_LOG("ControlCenterSession::Init is called");
115 napi_status status;
116 napi_value ctorObj;
117
118 napi_property_descriptor control_center_session_props[] = {
119 DECLARE_NAPI_FUNCTION("getSupportedVirtualApertures", ControlCenterSessionNapi::GetSupportedVirtualApertures),
120 DECLARE_NAPI_FUNCTION("getSupportedPhysicalApertures",
121 ControlCenterSessionNapi::GetSupportedPhysicalApertures),
122 DECLARE_NAPI_FUNCTION("getVirtualAperture", ControlCenterSessionNapi::GetVirtualAperture),
123 DECLARE_NAPI_FUNCTION("setVirtualAperture", ControlCenterSessionNapi::SetVirtualAperture),
124 DECLARE_NAPI_FUNCTION("getPhysicalAperture", ControlCenterSessionNapi::GetPhysicalAperture),
125 DECLARE_NAPI_FUNCTION("setPhysicalAperture", ControlCenterSessionNapi::SetPhysicalAperture),
126
127 DECLARE_NAPI_FUNCTION("getSupportedBeautyTypes", ControlCenterSessionNapi::GetSupportedBeautyTypes),
128 DECLARE_NAPI_FUNCTION("getSupportedBeautyRange", ControlCenterSessionNapi::GetSupportedBeautyRange),
129 DECLARE_NAPI_FUNCTION("getBeauty", ControlCenterSessionNapi::GetBeauty),
130 DECLARE_NAPI_FUNCTION("setBeauty", ControlCenterSessionNapi::SetBeauty),
131 DECLARE_NAPI_FUNCTION("getSupportedPortraitThemeTypes",
132 ControlCenterSessionNapi::GetSupportedPortraitThemeTypes),
133 DECLARE_NAPI_FUNCTION("isPortraitThemeSupported", ControlCenterSessionNapi::IsPortraitThemeSupported),
134 DECLARE_NAPI_FUNCTION("setPortraitThemeType", ControlCenterSessionNapi::SetPortraitThemeType),
135 DECLARE_NAPI_FUNCTION("release", Release)
136 };
137
138 status = napi_define_class(env, CONTROL_CENTER_SESSION_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
139 ControlCenterSessionConstructor, nullptr,
140 sizeof(control_center_session_props) / sizeof(control_center_session_props[0]),
141 control_center_session_props, &ctorObj);
142 if (status == napi_ok) {
143 status = NapiRefManager::CreateMemSafetyRef(env, ctorObj, &sConstructor_);
144 if (status == napi_ok) {
145 status = napi_set_named_property(env, exports, CONTROL_CENTER_SESSION_NAPI_CLASS_NAME, ctorObj);
146 if (status == napi_ok) {
147 return exports;
148 }
149 }
150 }
151 MEDIA_ERR_LOG("ControlCenterSessionNapi::Init Failed!");
152 return nullptr;
153 }
154
CreateControlCenterSession(napi_env env)155 napi_value ControlCenterSessionNapi::CreateControlCenterSession(napi_env env)
156 {
157 MEDIA_INFO_LOG("ControlCenterSessionNapi::CreateControlCenterSession is called");
158 napi_status status;
159 napi_value result = nullptr;
160 napi_value constructor;
161
162 status = napi_get_reference_value(env, sConstructor_, &constructor);
163 if (status == napi_ok) {
164 int retCode = CameraManager::GetInstance()->CreateControlCenterSession(sControlCenterSession_);
165 if (!CameraNapiUtils::CheckError(env, retCode)) {
166 return nullptr;
167 }
168 if (sControlCenterSession_ == nullptr) {
169 MEDIA_ERR_LOG("failed to create ControlCenterSession");
170 return result;
171 }
172 status = napi_new_instance(env, constructor, 0, nullptr, &result);
173 sControlCenterSession_ = nullptr;
174 if (status == napi_ok && result != nullptr) {
175 return result;
176 } else {
177 MEDIA_ERR_LOG("Failed to create ControlCenterSession instance.");
178 }
179 }
180 MEDIA_ERR_LOG("CreateControlCenterSession call Failed! %{public}d", status);
181 napi_get_undefined(env, &result);
182 return result;
183 }
184
GetSupportedVirtualApertures(napi_env env,napi_callback_info info)185 napi_value ControlCenterSessionNapi::GetSupportedVirtualApertures(napi_env env, napi_callback_info info)
186 {
187 if (!CameraNapiSecurity::CheckSystemApp(env)) {
188 MEDIA_ERR_LOG("SystemApi GetSupportedVirtualApertures is called!");
189 return nullptr;
190 }
191 MEDIA_DEBUG_LOG("GetSupportedVirtualApertures is called");
192 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
193 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
194 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
195 MEDIA_ERR_LOG("controlCenterSessionNapi::GetSupportedVirtualApertures parse parameter occur error");
196 return nullptr;
197 }
198
199 napi_status status;
200 napi_value result = nullptr;
201 status = napi_create_array(env, &result);
202 if (status != napi_ok) {
203 MEDIA_ERR_LOG("napi_create_array call Failed!");
204 return nullptr;
205 }
206
207 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
208 std::vector<float> virtualApertures = {};
209 int32_t retCode =
210 controlCenterSessionNapi->controlCenterSession_->GetSupportedVirtualApertures(virtualApertures);
211 MEDIA_INFO_LOG("GetSupportedVirtualApertures virtualApertures len = %{public}zu", virtualApertures.size());
212 if (!CameraNapiUtils::CheckError(env, retCode)) {
213 return nullptr;
214 }
215 if (!virtualApertures.empty()) {
216 for (size_t i = 0; i < virtualApertures.size(); i++) {
217 float virtualAperture = virtualApertures[i];
218 napi_value value;
219 napi_create_double(env, CameraNapiUtils::FloatToDouble(virtualAperture), &value);
220 napi_set_element(env, result, i, value);
221 }
222 }
223 } else {
224 MEDIA_ERR_LOG("GetSupportedVirtualApertures call Failed!");
225 }
226 return result;
227 }
228
GetVirtualAperture(napi_env env,napi_callback_info info)229 napi_value ControlCenterSessionNapi::GetVirtualAperture(napi_env env, napi_callback_info info)
230 {
231 if (!CameraNapiSecurity::CheckSystemApp(env)) {
232 MEDIA_ERR_LOG("SystemApi GetVirtualAperture is called!");
233 return nullptr;
234 }
235 MEDIA_DEBUG_LOG("GetVirtualAperture is called");
236 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
237 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
238 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
239 MEDIA_ERR_LOG("controlCenterSessionNapi::GetVirtualAperture parse parameter occur error");
240 return nullptr;
241 }
242 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
243 float virtualAperture;
244 int32_t retCode = controlCenterSessionNapi->controlCenterSession_->GetVirtualAperture(virtualAperture);
245 if (!CameraNapiUtils::CheckError(env, retCode)) {
246 return nullptr;
247 }
248 napi_value result;
249 napi_create_double(env, CameraNapiUtils::FloatToDouble(virtualAperture), &result);
250 return result;
251 } else {
252 MEDIA_ERR_LOG("GetVirtualAperture call Failed!");
253 }
254 return CameraNapiUtils::GetUndefinedValue(env);
255 }
256
SetVirtualAperture(napi_env env,napi_callback_info info)257 napi_value ControlCenterSessionNapi::SetVirtualAperture(napi_env env, napi_callback_info info)
258 {
259 if (!CameraNapiSecurity::CheckSystemApp(env)) {
260 MEDIA_ERR_LOG("SystemApi SetVirtualAperture is called!");
261 return nullptr;
262 }
263 MEDIA_INFO_LOG("SetVirtualAperture is called");
264 double virtualAperture;
265 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
266 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi, virtualAperture);
267 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
268 MEDIA_ERR_LOG("controlCenterSessionNapi::SetVirtualAperture parse parameter occur error");
269 return nullptr;
270 }
271 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
272 int32_t retCode =
273 controlCenterSessionNapi->controlCenterSession_->SetVirtualAperture(static_cast<float>(virtualAperture));
274 MEDIA_INFO_LOG("SetVirtualAperture set virtualAperture %{public}f!", virtualAperture);
275 CHECK_RETURN_RET(!CameraNapiUtils::CheckError(env, retCode), nullptr);
276 } else {
277 MEDIA_ERR_LOG("SetVirtualAperture call Failed!");
278 }
279 return CameraNapiUtils::GetUndefinedValue(env);
280 }
281
GetSupportedPhysicalApertures(napi_env env,napi_callback_info info)282 napi_value ControlCenterSessionNapi::GetSupportedPhysicalApertures(napi_env env, napi_callback_info info)
283 {
284 if (!CameraNapiSecurity::CheckSystemApp(env)) {
285 MEDIA_ERR_LOG("SystemApi GetSupportedPhysicalApertures is called!");
286 return nullptr;
287 }
288 MEDIA_DEBUG_LOG("GetSupportedPhysicalApertures is called");
289 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
290 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
291 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
292 MEDIA_ERR_LOG("controlCenterSessionNapi::GetSupportedPhysicalApertures parse parameter occur error");
293 return nullptr;
294 }
295
296 napi_status status;
297 napi_value result = nullptr;
298 status = napi_create_array(env, &result);
299 if (status != napi_ok) {
300 MEDIA_ERR_LOG("napi_create_array call Failed!");
301 return nullptr;
302 }
303
304 if (status == napi_ok && controlCenterSessionNapi->controlCenterSession_ != nullptr) {
305 std::vector<std::vector<float>> physicalApertures = {};
306 int32_t retCode =
307 controlCenterSessionNapi->controlCenterSession_->GetSupportedPhysicalApertures(physicalApertures);
308 MEDIA_INFO_LOG("GetSupportedPhysicalApertures len = %{public}zu", physicalApertures.size());
309 if (!CameraNapiUtils::CheckError(env, retCode)) {
310 return nullptr;
311 }
312 if (!physicalApertures.empty()) {
313 result = CameraNapiUtils::ProcessingPhysicalApertures(env, physicalApertures);
314 }
315 } else {
316 MEDIA_ERR_LOG("GetSupportedPhysicalApertures call Failed!");
317 }
318 return result;
319 }
320
GetPhysicalAperture(napi_env env,napi_callback_info info)321 napi_value ControlCenterSessionNapi::GetPhysicalAperture(napi_env env, napi_callback_info info)
322 {
323 if (!CameraNapiSecurity::CheckSystemApp(env)) {
324 MEDIA_ERR_LOG("SystemApi GetPhysicalAperture is called!");
325 return nullptr;
326 }
327 MEDIA_DEBUG_LOG("GetPhysicalAperture is called");
328 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
329 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
330 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
331 MEDIA_ERR_LOG("controlCenterSessionNapi::GetPhysicalAperture parse parameter occur error");
332 return nullptr;
333 }
334
335 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
336 float physicalAperture = 0.0;
337 int32_t retCode = controlCenterSessionNapi->controlCenterSession_->GetPhysicalAperture(physicalAperture);
338 if (!CameraNapiUtils::CheckError(env, retCode)) {
339 return nullptr;
340 }
341 napi_value result = nullptr;
342 napi_create_double(env, CameraNapiUtils::FloatToDouble(physicalAperture), &result);
343 return result;
344 } else {
345 MEDIA_ERR_LOG("GetPhysicalAperture call Failed!");
346 }
347 return CameraNapiUtils::GetUndefinedValue(env);
348 }
349
SetPhysicalAperture(napi_env env,napi_callback_info info)350 napi_value ControlCenterSessionNapi::SetPhysicalAperture(napi_env env, napi_callback_info info)
351 {
352 if (!CameraNapiSecurity::CheckSystemApp(env)) {
353 MEDIA_ERR_LOG("SystemApi SetPhysicalAperture is called!");
354 return nullptr;
355 }
356 MEDIA_DEBUG_LOG("SetPhysicalAperture is called");
357 double physicalAperture;
358 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
359 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi, physicalAperture);
360 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
361 MEDIA_ERR_LOG("controlCenterSessionNapi::SetPhysicalAperture parse parameter occur error");
362 return nullptr;
363 }
364
365 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
366 int32_t retCode =
367 controlCenterSessionNapi->controlCenterSession_->SetPhysicalAperture(static_cast<float>(physicalAperture));
368 MEDIA_INFO_LOG("SetPhysicalAperture set physicalAperture %{public}f!", ConfusingNumber(physicalAperture));
369 if (!CameraNapiUtils::CheckError(env, retCode)) {
370 return nullptr;
371 }
372 } else {
373 MEDIA_ERR_LOG("SetPhysicalAperture call Failed!");
374 }
375 return CameraNapiUtils::GetUndefinedValue(env);
376 }
377
GetSupportedBeautyTypes(napi_env env,napi_callback_info info)378 napi_value ControlCenterSessionNapi::GetSupportedBeautyTypes(napi_env env, napi_callback_info info)
379 {
380 if (!CameraNapiSecurity::CheckSystemApp(env)) {
381 MEDIA_ERR_LOG("SystemApi GetSupportedBeautyTypes is called!");
382 return nullptr;
383 }
384 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
385 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
386 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
387 MEDIA_ERR_LOG("controlCenterSessionNapi::GetSupportedBeautyTypes parse parameter occur error");
388 return nullptr;
389 }
390
391 MEDIA_DEBUG_LOG("GetSupportedBeautyTypes is called");
392 napi_status status;
393 napi_value result = nullptr;
394 size_t argc = ARGS_ZERO;
395 napi_value argv[ARGS_ZERO];
396 napi_value thisVar = nullptr;
397
398 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
399
400 napi_get_undefined(env, &result);
401 status = napi_create_array(env, &result);
402 if (status != napi_ok) {
403 MEDIA_ERR_LOG("napi_create_array call Failed!");
404 return result;
405 }
406
407 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&controlCenterSessionNapi));
408 if (status == napi_ok && controlCenterSessionNapi != nullptr
409 && controlCenterSessionNapi->controlCenterSession_ != nullptr) {
410 std::vector<int32_t> beautyTypes = controlCenterSessionNapi->controlCenterSession_->GetSupportedBeautyTypes();
411 MEDIA_INFO_LOG("controlCenterSessionNapi::GetSupportedBeautyTypes len = %{public}zu",
412 beautyTypes.size());
413 if (!beautyTypes.empty() && status == napi_ok) {
414 for (size_t i = 0; i < beautyTypes.size(); i++) {
415 int32_t beautyType = beautyTypes[i];
416 napi_value value;
417 napi_create_int32(env, beautyType, &value);
418 napi_set_element(env, result, i, value);
419 }
420 }
421 } else {
422 MEDIA_ERR_LOG("GetSupportedBeautyTypes call Failed!");
423 }
424 return result;
425 }
426
GetSupportedBeautyRange(napi_env env,napi_callback_info info)427 napi_value ControlCenterSessionNapi::GetSupportedBeautyRange(napi_env env, napi_callback_info info)
428 {
429 if (!CameraNapiSecurity::CheckSystemApp(env)) {
430 MEDIA_ERR_LOG("SystemApi GetSupportedBeautyRange is called!");
431 return nullptr;
432 }
433 MEDIA_DEBUG_LOG("GetSupportedBeautyRange is called");
434 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
435 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
436 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
437 MEDIA_ERR_LOG("controlCenterSessionNapi::GetSupportedBeautyTypes parse parameter occur error");
438 return nullptr;
439 }
440 napi_status status;
441 napi_value result = nullptr;
442 size_t argc = ARGS_ONE;
443 napi_value argv[ARGS_ONE];
444 napi_value thisVar = nullptr;
445
446 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
447
448 napi_get_undefined(env, &result);
449 status = napi_create_array(env, &result);
450 if (status != napi_ok) {
451 MEDIA_ERR_LOG("napi_create_array call Failed!");
452 return result;
453 }
454 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&controlCenterSessionNapi));
455 if (status == napi_ok && controlCenterSessionNapi != nullptr
456 && controlCenterSessionNapi->controlCenterSession_ != nullptr) {
457 int32_t beautyType;
458 napi_get_value_int32(env, argv[PARAM0], &beautyType);
459 std::vector<int32_t> beautyRanges =
460 controlCenterSessionNapi->controlCenterSession_->GetSupportedBeautyRange(
461 static_cast<BeautyType>(beautyType));
462 MEDIA_INFO_LOG("controlCenterSessionNapi::GetSupportedBeautyRange beautyType = %{public}d, len = %{public}zu",
463 beautyType, beautyRanges.size());
464 if (!beautyRanges.empty()) {
465 for (size_t i = 0; i < beautyRanges.size(); i++) {
466 int beautyRange = beautyRanges[i];
467 napi_value value;
468 napi_create_int32(env, beautyRange, &value);
469 napi_set_element(env, result, i, value);
470 }
471 }
472 } else {
473 MEDIA_ERR_LOG("GetSupportedBeautyRange call Failed!");
474 }
475 return result;
476 }
477
GetBeauty(napi_env env,napi_callback_info info)478 napi_value ControlCenterSessionNapi::GetBeauty(napi_env env, napi_callback_info info)
479 {
480 MEDIA_INFO_LOG("ControlCenterSessionNapi::GetBeauty is called.");
481 if (!CameraNapiSecurity::CheckSystemApp(env)) {
482 MEDIA_ERR_LOG("SystemApi GetBeauty is called!");
483 return nullptr;
484 }
485 MEDIA_DEBUG_LOG("GetBeauty is called");
486 napi_status status;
487 napi_value result = nullptr;
488 size_t argc = ARGS_ONE;
489 napi_value argv[ARGS_ONE];
490 napi_value thisVar = nullptr;
491
492 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
493 napi_get_undefined(env, &result);
494 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
495 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&controlCenterSessionNapi));
496 if (status == napi_ok && controlCenterSessionNapi != nullptr
497 && controlCenterSessionNapi->controlCenterSession_ != nullptr) {
498 int32_t beautyType;
499 napi_get_value_int32(env, argv[PARAM0], &beautyType);
500 int32_t beautyStrength =
501 controlCenterSessionNapi->controlCenterSession_->GetBeauty(static_cast<BeautyType>(beautyType));
502
503 napi_create_int32(env, beautyStrength, &result);
504 } else {
505 MEDIA_ERR_LOG("GetBeauty call Failed!");
506 }
507 return result;
508 }
509
SetBeauty(napi_env env,napi_callback_info info)510 napi_value ControlCenterSessionNapi::SetBeauty(napi_env env, napi_callback_info info)
511 {
512 MEDIA_INFO_LOG("ControlCenterSessionNapi::SetBeauty is called.");
513 if (!CameraNapiSecurity::CheckSystemApp(env)) {
514 MEDIA_ERR_LOG("SystemApi SetBeauty is called!");
515 return nullptr;
516 }
517 napi_status status;
518 napi_value result = nullptr;
519 size_t argc = ARGS_TWO;
520 napi_value argv[ARGS_TWO];
521 napi_value thisVar = nullptr;
522
523 CAMERA_NAPI_GET_JS_ARGS(env, info, argc, argv, thisVar);
524 napi_get_undefined(env, &result);
525 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
526 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&controlCenterSessionNapi));
527 MEDIA_INFO_LOG("ControlCenterSessionNapi::SetBeauty is called.");
528 if (status == napi_ok && controlCenterSessionNapi != nullptr
529 && controlCenterSessionNapi->controlCenterSession_ != nullptr) {
530 int32_t beautyType;
531 napi_get_value_int32(env, argv[PARAM0], &beautyType);
532 int32_t beautyStrength;
533 napi_get_value_int32(env, argv[PARAM1], &beautyStrength);
534 controlCenterSessionNapi->controlCenterSession_->SetBeauty(
535 static_cast<BeautyType>(beautyType), beautyStrength);
536 } else {
537 MEDIA_ERR_LOG("ControlCenterSessionNapi::SetBeauty Failed!");
538 }
539 return result;
540 }
541
GetSupportedPortraitThemeTypes(napi_env env,napi_callback_info info)542 napi_value ControlCenterSessionNapi::GetSupportedPortraitThemeTypes(napi_env env, napi_callback_info info)
543 {
544 if (!CameraNapiSecurity::CheckSystemApp(env)) {
545 MEDIA_ERR_LOG("SystemApi GetSupportedPortraitThemeTypes is called!");
546 return nullptr;
547 }
548 MEDIA_DEBUG_LOG("GetSupportedPortraitThemeTypes is called");
549 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
550 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
551 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
552 MEDIA_ERR_LOG("controlCenterSessionNapi::GetSupportedPortraitThemeTypes parse parameter occur error");
553 return nullptr;
554 }
555
556 napi_status status;
557 napi_value result = nullptr;
558 status = napi_create_array(env, &result);
559 if (status != napi_ok) {
560 MEDIA_ERR_LOG("napi_create_array call Failed!");
561 return nullptr;
562 }
563
564 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
565 std::vector<PortraitThemeType> themeTypes;
566 int32_t retCode = controlCenterSessionNapi->controlCenterSession_->GetSupportedPortraitThemeTypes(themeTypes);
567 if (!CameraNapiUtils::CheckError(env, retCode)) {
568 return nullptr;
569 }
570 MEDIA_INFO_LOG("controlCenterSessionNapi::GetSupportedPortraitThemeTypes len = %{public}zu",
571 themeTypes.size());
572 if (!themeTypes.empty()) {
573 for (size_t i = 0; i < themeTypes.size(); i++) {
574 napi_value value;
575 napi_create_int32(env, static_cast<int32_t>(themeTypes[i]), &value);
576 napi_set_element(env, result, i, value);
577 }
578 }
579 } else {
580 MEDIA_ERR_LOG("GetSupportedPortraitThemeTypes call Failed!");
581 }
582 return result;
583 }
584
IsPortraitThemeSupported(napi_env env,napi_callback_info info)585 napi_value ControlCenterSessionNapi::IsPortraitThemeSupported(napi_env env, napi_callback_info info)
586 {
587 if (!CameraNapiSecurity::CheckSystemApp(env)) {
588 MEDIA_ERR_LOG("SystemApi IsPortraitThemeSupported is called!");
589 return nullptr;
590 }
591 MEDIA_DEBUG_LOG("CameraSessionNapi::IsPortraitThemeSupported is called");
592 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
593 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi);
594 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
595 MEDIA_ERR_LOG("CameraSessionNapi::IsPortraitThemeSupported parse parameter occur error");
596 return nullptr;
597 }
598 auto result = CameraNapiUtils::GetUndefinedValue(env);
599 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
600 bool isSupported;
601 int32_t retCode = controlCenterSessionNapi->controlCenterSession_->IsPortraitThemeSupported(isSupported);
602 if (!CameraNapiUtils::CheckError(env, retCode)) {
603 return nullptr;
604 }
605 napi_get_boolean(env, isSupported, &result);
606 } else {
607 MEDIA_ERR_LOG("CameraSessionNapi::IsPortraitThemeSupported get native object fail");
608 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
609 return nullptr;
610 }
611 return result;
612 }
613
SetPortraitThemeType(napi_env env,napi_callback_info info)614 napi_value ControlCenterSessionNapi::SetPortraitThemeType(napi_env env, napi_callback_info info)
615 {
616 if (!CameraNapiSecurity::CheckSystemApp(env)) {
617 MEDIA_ERR_LOG("SystemApi SetPortraitThemeType is called!");
618 return nullptr;
619 }
620 MEDIA_DEBUG_LOG("CameraSessionNapi::SetPortraitThemeType is called");
621 int32_t type = 0;
622 ControlCenterSessionNapi* controlCenterSessionNapi = nullptr;
623 CameraNapiParamParser jsParamParser(env, info, controlCenterSessionNapi, type);
624 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "parse parameter occur error")) {
625 MEDIA_ERR_LOG("controlCenterSessionNapi::SetPortraitThemeType parse parameter occur error");
626 return nullptr;
627 }
628
629 if (controlCenterSessionNapi->controlCenterSession_ != nullptr) {
630 PortraitThemeType portraitThemeType = static_cast<PortraitThemeType>(type);
631 MEDIA_INFO_LOG("CameraSessionNapi::SetPortraitThemeType:%{public}d", portraitThemeType);
632 int32_t retCode = controlCenterSessionNapi->controlCenterSession_->SetPortraitThemeType(portraitThemeType);
633 if (!CameraNapiUtils::CheckError(env, retCode)) {
634 MEDIA_ERR_LOG("CameraSessionNapi::SetPortraitThemeType fail %{public}d", retCode);
635 return nullptr;
636 }
637 } else {
638 MEDIA_ERR_LOG("CameraSessionNapi::SetPortraitThemeType get native object fail");
639 CameraNapiUtils::ThrowError(env, INVALID_ARGUMENT, "get native object fail");
640 return nullptr;
641 }
642 return CameraNapiUtils::GetUndefinedValue(env);
643 }
644
Release(napi_env env,napi_callback_info info)645 napi_value ControlCenterSessionNapi::Release(napi_env env, napi_callback_info info)
646 {
647 MEDIA_INFO_LOG("ControlCenterSessionNapi::Release is called");
648 std::unique_ptr<ControlCenterSessionAsyncContext> asyncContext = std::make_unique<ControlCenterSessionAsyncContext>(
649 "ControlCenterSessionNapi::Release", CameraNapiUtils::IncrementAndGet(controlCenterSessionTaskId));
650 auto asyncFunction =
651 std::make_shared<CameraNapiAsyncFunction>(env, "Release", asyncContext->callbackRef, asyncContext->deferred);
652 CameraNapiParamParser jsParamParser(env, info, asyncContext->objectInfo, asyncFunction);
653 if (!jsParamParser.AssertStatus(INVALID_ARGUMENT, "invalid argument")) {
654 MEDIA_ERR_LOG("ControlCenterSessionNapi::Release invalid argument");
655 return nullptr;
656 }
657 return CameraNapiUtils::GetUndefinedValue(env);
658 }
659
660
661 } // namespace CameraStandard
662 } // namespace OHOS
663