15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License. 55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at 65ccb8f90Sopenharmony_ci * 75ccb8f90Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ccb8f90Sopenharmony_ci * 95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and 135ccb8f90Sopenharmony_ci * limitations under the License. 145ccb8f90Sopenharmony_ci */ 155ccb8f90Sopenharmony_ci 165ccb8f90Sopenharmony_ci#include "runninglock_interface.h" 175ccb8f90Sopenharmony_ci 185ccb8f90Sopenharmony_ci#include <cstdint> 195ccb8f90Sopenharmony_ci#include <memory> 205ccb8f90Sopenharmony_ci#include <string> 215ccb8f90Sopenharmony_ci 225ccb8f90Sopenharmony_ci#include "power_log.h" 235ccb8f90Sopenharmony_ci#include "power_mgr_client.h" 245ccb8f90Sopenharmony_ci#include "runninglock_entity.h" 255ccb8f90Sopenharmony_ci#include "xpower_event_js.h" 265ccb8f90Sopenharmony_ci 275ccb8f90Sopenharmony_cinamespace OHOS { 285ccb8f90Sopenharmony_cinamespace PowerMgr { 295ccb8f90Sopenharmony_cinamespace { 305ccb8f90Sopenharmony_ciconstexpr int RESULT_SIZE = 2; 315ccb8f90Sopenharmony_ciconstexpr int RUNNINGLOCK_NAME_MAX = 512; 325ccb8f90Sopenharmony_ciconstexpr int CREATRUNNINGLOCK_ARGC = 3; 335ccb8f90Sopenharmony_ciconstexpr int ISRUNNINGLOCKTYPESUPPORTED_ARGC = 2; 345ccb8f90Sopenharmony_ciconstexpr int32_t INDEX_0 = 0; 355ccb8f90Sopenharmony_ciconstexpr int32_t INDEX_1 = 1; 365ccb8f90Sopenharmony_ciconstexpr int32_t INDEX_2 = 2; 375ccb8f90Sopenharmony_cistatic PowerMgrClient& g_powerMgrClient = PowerMgrClient::GetInstance(); 385ccb8f90Sopenharmony_ci} 395ccb8f90Sopenharmony_ci 405ccb8f90Sopenharmony_cinapi_value RunningLockInterface::CreateRunningLock(napi_env env, napi_callback_info info, napi_ref& napiRunningLock) 415ccb8f90Sopenharmony_ci{ 425ccb8f90Sopenharmony_ci size_t argc = CREATRUNNINGLOCK_ARGC; 435ccb8f90Sopenharmony_ci napi_value argv[CREATRUNNINGLOCK_ARGC] = {0}; 445ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 455ccb8f90Sopenharmony_ci void* data = nullptr; 465ccb8f90Sopenharmony_ci 475ccb8f90Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisArg, &data); 485ccb8f90Sopenharmony_ci NAPI_ASSERT(env, (status == napi_ok) && (argc >= CREATRUNNINGLOCK_ARGC - 1), "Failed to get cb info"); 495ccb8f90Sopenharmony_ci 505ccb8f90Sopenharmony_ci std::unique_ptr<RunningLockAsyncInfo> asyncInfo = std::make_unique<RunningLockAsyncInfo>(); 515ccb8f90Sopenharmony_ci if (asyncInfo == nullptr) { 525ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "asyncInfo is nullptr"); 535ccb8f90Sopenharmony_ci return nullptr; 545ccb8f90Sopenharmony_ci } 555ccb8f90Sopenharmony_ci asyncInfo->napiRunningLockIns = napiRunningLock; 565ccb8f90Sopenharmony_ci 575ccb8f90Sopenharmony_ci napi_valuetype valueType = napi_undefined; 585ccb8f90Sopenharmony_ci napi_typeof(env, argv[INDEX_0], &valueType); 595ccb8f90Sopenharmony_ci NAPI_ASSERT(env, valueType == napi_string, "The input parameter type is not string"); 605ccb8f90Sopenharmony_ci char name[RUNNINGLOCK_NAME_MAX] = {0}; 615ccb8f90Sopenharmony_ci napi_get_value_string_utf8(env, argv[INDEX_0], name, RUNNINGLOCK_NAME_MAX + 1, &asyncInfo->nameLen); 625ccb8f90Sopenharmony_ci asyncInfo->name = name; 635ccb8f90Sopenharmony_ci 645ccb8f90Sopenharmony_ci napi_typeof(env, argv[INDEX_1], &valueType); 655ccb8f90Sopenharmony_ci NAPI_ASSERT(env, valueType == napi_number, "The input parameter type is not number"); 665ccb8f90Sopenharmony_ci int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_BUTT); 675ccb8f90Sopenharmony_ci napi_get_value_int32(env, argv[INDEX_1], &type); 685ccb8f90Sopenharmony_ci asyncInfo->type = static_cast<RunningLockType>(type); 695ccb8f90Sopenharmony_ci 705ccb8f90Sopenharmony_ci if (argc == CREATRUNNINGLOCK_ARGC) { 715ccb8f90Sopenharmony_ci napi_typeof(env, argv[INDEX_2], &valueType); 725ccb8f90Sopenharmony_ci NAPI_ASSERT(env, valueType == napi_function, "The input parameter type is not function"); 735ccb8f90Sopenharmony_ci napi_create_reference(env, argv[INDEX_2], 1, &asyncInfo->callbackRef); 745ccb8f90Sopenharmony_ci } 755ccb8f90Sopenharmony_ci napi_value result = nullptr; 765ccb8f90Sopenharmony_ci if (asyncInfo->callbackRef == nullptr) { 775ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "callbackRef is null"); 785ccb8f90Sopenharmony_ci napi_create_promise(env, &asyncInfo->deferred, &result); 795ccb8f90Sopenharmony_ci } else { 805ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "callbackRef is not null"); 815ccb8f90Sopenharmony_ci napi_get_undefined(env, &result); 825ccb8f90Sopenharmony_ci } 835ccb8f90Sopenharmony_ci CreateRunningLockCallBack(env, asyncInfo); 845ccb8f90Sopenharmony_ci return result; 855ccb8f90Sopenharmony_ci} 865ccb8f90Sopenharmony_ci 875ccb8f90Sopenharmony_cinapi_value RunningLockInterface::IsRunningLockTypeSupported(napi_env env, napi_callback_info info) 885ccb8f90Sopenharmony_ci{ 895ccb8f90Sopenharmony_ci size_t argc = ISRUNNINGLOCKTYPESUPPORTED_ARGC; 905ccb8f90Sopenharmony_ci napi_value argv[ISRUNNINGLOCKTYPESUPPORTED_ARGC] = {0}; 915ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 925ccb8f90Sopenharmony_ci void* data = nullptr; 935ccb8f90Sopenharmony_ci 945ccb8f90Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisArg, &data); 955ccb8f90Sopenharmony_ci NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); 965ccb8f90Sopenharmony_ci 975ccb8f90Sopenharmony_ci std::unique_ptr<RunningLockAsyncInfo> asyncInfo = std::make_unique<RunningLockAsyncInfo>(); 985ccb8f90Sopenharmony_ci if (asyncInfo == nullptr) { 995ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "asyncInfo is nullptr"); 1005ccb8f90Sopenharmony_ci return nullptr; 1015ccb8f90Sopenharmony_ci } 1025ccb8f90Sopenharmony_ci 1035ccb8f90Sopenharmony_ci napi_valuetype valueType = napi_undefined; 1045ccb8f90Sopenharmony_ci napi_typeof(env, argv[INDEX_0], &valueType); 1055ccb8f90Sopenharmony_ci NAPI_ASSERT(env, valueType == napi_number, "The input parameter type is not number"); 1065ccb8f90Sopenharmony_ci int32_t type = static_cast<int32_t>(RunningLockType::RUNNINGLOCK_BUTT); 1075ccb8f90Sopenharmony_ci napi_get_value_int32(env, argv[INDEX_0], &type); 1085ccb8f90Sopenharmony_ci asyncInfo->type = static_cast<RunningLockType>(type); 1095ccb8f90Sopenharmony_ci 1105ccb8f90Sopenharmony_ci if (argc == ISRUNNINGLOCKTYPESUPPORTED_ARGC) { 1115ccb8f90Sopenharmony_ci napi_typeof(env, argv[INDEX_1], &valueType); 1125ccb8f90Sopenharmony_ci NAPI_ASSERT(env, valueType == napi_function, "The input parameter type is not function"); 1135ccb8f90Sopenharmony_ci napi_create_reference(env, argv[INDEX_1], 1, &asyncInfo->callbackRef); 1145ccb8f90Sopenharmony_ci } 1155ccb8f90Sopenharmony_ci 1165ccb8f90Sopenharmony_ci napi_value result = nullptr; 1175ccb8f90Sopenharmony_ci if (asyncInfo->callbackRef == nullptr) { 1185ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "callbackRef is null"); 1195ccb8f90Sopenharmony_ci napi_create_promise(env, &asyncInfo->deferred, &result); 1205ccb8f90Sopenharmony_ci } else { 1215ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "callbackRef is not null"); 1225ccb8f90Sopenharmony_ci napi_get_undefined(env, &result); 1235ccb8f90Sopenharmony_ci } 1245ccb8f90Sopenharmony_ci IsRunningLockTypeSupportedCallBack(env, asyncInfo); 1255ccb8f90Sopenharmony_ci return result; 1265ccb8f90Sopenharmony_ci} 1275ccb8f90Sopenharmony_ci 1285ccb8f90Sopenharmony_cinapi_value RunningLockInterface::Lock(napi_env env, napi_callback_info info) 1295ccb8f90Sopenharmony_ci{ 1305ccb8f90Sopenharmony_ci size_t argc = 1; 1315ccb8f90Sopenharmony_ci napi_value args[1] = {0}; 1325ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 1335ccb8f90Sopenharmony_ci void* data = nullptr; 1345ccb8f90Sopenharmony_ci 1355ccb8f90Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, &data); 1365ccb8f90Sopenharmony_ci NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); 1375ccb8f90Sopenharmony_ci napi_valuetype type = napi_undefined; 1385ccb8f90Sopenharmony_ci NAPI_CALL(env, napi_typeof(env, args[0], &type)); 1395ccb8f90Sopenharmony_ci NAPI_ASSERT(env, type == napi_number, "Wrong argument type. number expected."); 1405ccb8f90Sopenharmony_ci 1415ccb8f90Sopenharmony_ci int32_t timeOut; 1425ccb8f90Sopenharmony_ci status = napi_get_value_int32(env, args[0], &timeOut); 1435ccb8f90Sopenharmony_ci if (status != napi_ok) { 1445ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "napi_get_value_uint32 failed"); 1455ccb8f90Sopenharmony_ci return nullptr; 1465ccb8f90Sopenharmony_ci } 1475ccb8f90Sopenharmony_ci RunningLockEntity* entity = nullptr; 1485ccb8f90Sopenharmony_ci status = napi_unwrap(env, thisArg, (void**)&entity); 1495ccb8f90Sopenharmony_ci if (status != napi_ok) { 1505ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Cannot unwrap for pointer"); 1515ccb8f90Sopenharmony_ci return nullptr; 1525ccb8f90Sopenharmony_ci } 1535ccb8f90Sopenharmony_ci if (entity == nullptr || entity->runningLock == nullptr) { 1545ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Entity runningLock is nullptr"); 1555ccb8f90Sopenharmony_ci return nullptr; 1565ccb8f90Sopenharmony_ci } 1575ccb8f90Sopenharmony_ci entity->runningLock->Lock(timeOut); 1585ccb8f90Sopenharmony_ci OHOS::HiviewDFX::ReportXPowerJsStackSysEvent(env, "RunningLockNapi::Lock"); 1595ccb8f90Sopenharmony_ci return nullptr; 1605ccb8f90Sopenharmony_ci} 1615ccb8f90Sopenharmony_ci 1625ccb8f90Sopenharmony_cinapi_value RunningLockInterface::IsUsed(napi_env env, napi_callback_info info) 1635ccb8f90Sopenharmony_ci{ 1645ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 1655ccb8f90Sopenharmony_ci napi_value result = nullptr; 1665ccb8f90Sopenharmony_ci void* data = nullptr; 1675ccb8f90Sopenharmony_ci 1685ccb8f90Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); 1695ccb8f90Sopenharmony_ci NAPI_ASSERT(env, (status == napi_ok), "Failed to get cb info"); 1705ccb8f90Sopenharmony_ci napi_get_boolean(env, false, &result); 1715ccb8f90Sopenharmony_ci RunningLockEntity* entity = nullptr; 1725ccb8f90Sopenharmony_ci status = napi_unwrap(env, thisArg, (void**)&entity); 1735ccb8f90Sopenharmony_ci if (status != napi_ok) { 1745ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Cannot unwrap for pointer"); 1755ccb8f90Sopenharmony_ci return result; 1765ccb8f90Sopenharmony_ci } 1775ccb8f90Sopenharmony_ci if (entity == nullptr || entity->runningLock == nullptr) { 1785ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Entity runningLock is nullptr"); 1795ccb8f90Sopenharmony_ci return result; 1805ccb8f90Sopenharmony_ci } 1815ccb8f90Sopenharmony_ci bool isUsed = entity->runningLock->IsUsed(); 1825ccb8f90Sopenharmony_ci napi_get_boolean(env, isUsed, &result); 1835ccb8f90Sopenharmony_ci return result; 1845ccb8f90Sopenharmony_ci} 1855ccb8f90Sopenharmony_ci 1865ccb8f90Sopenharmony_cinapi_value RunningLockInterface::Unlock(napi_env env, napi_callback_info info) 1875ccb8f90Sopenharmony_ci{ 1885ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 1895ccb8f90Sopenharmony_ci void* data = nullptr; 1905ccb8f90Sopenharmony_ci 1915ccb8f90Sopenharmony_ci napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); 1925ccb8f90Sopenharmony_ci NAPI_ASSERT(env, (status == napi_ok), "Unlock: failed to get cb info"); 1935ccb8f90Sopenharmony_ci 1945ccb8f90Sopenharmony_ci RunningLockEntity* entity = nullptr; 1955ccb8f90Sopenharmony_ci status = napi_unwrap(env, thisArg, (void**)&entity); 1965ccb8f90Sopenharmony_ci if (status != napi_ok) { 1975ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Cannot unwrap for pointer"); 1985ccb8f90Sopenharmony_ci return nullptr; 1995ccb8f90Sopenharmony_ci } 2005ccb8f90Sopenharmony_ci if (entity == nullptr || entity->runningLock == nullptr) { 2015ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Entity runningLock is nullptr"); 2025ccb8f90Sopenharmony_ci return nullptr; 2035ccb8f90Sopenharmony_ci } 2045ccb8f90Sopenharmony_ci entity->runningLock->UnLock(); 2055ccb8f90Sopenharmony_ci return nullptr; 2065ccb8f90Sopenharmony_ci} 2075ccb8f90Sopenharmony_ci 2085ccb8f90Sopenharmony_cinapi_value RunningLockInterface::CreateInstanceForRunningLock(napi_env env, RunningLockAsyncInfo* asyncInfo) 2095ccb8f90Sopenharmony_ci{ 2105ccb8f90Sopenharmony_ci napi_value cons = nullptr; 2115ccb8f90Sopenharmony_ci napi_value instance = nullptr; 2125ccb8f90Sopenharmony_ci napi_status callBackStatus; 2135ccb8f90Sopenharmony_ci RunningLockEntity* entity = nullptr; 2145ccb8f90Sopenharmony_ci 2155ccb8f90Sopenharmony_ci if (asyncInfo->runningLock == nullptr) { 2165ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "RunningLock is nullptr"); 2175ccb8f90Sopenharmony_ci return nullptr; 2185ccb8f90Sopenharmony_ci } 2195ccb8f90Sopenharmony_ci callBackStatus = napi_get_reference_value(env, asyncInfo->napiRunningLockIns, &cons); 2205ccb8f90Sopenharmony_ci if (callBackStatus != napi_ok) { 2215ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "NAPI failed to create a reference value"); 2225ccb8f90Sopenharmony_ci return nullptr; 2235ccb8f90Sopenharmony_ci } 2245ccb8f90Sopenharmony_ci callBackStatus = napi_new_instance(env, cons, 0, nullptr, &instance); 2255ccb8f90Sopenharmony_ci if (callBackStatus != napi_ok || instance == nullptr) { 2265ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "NAPI failed to create a reference"); 2275ccb8f90Sopenharmony_ci return nullptr; 2285ccb8f90Sopenharmony_ci } 2295ccb8f90Sopenharmony_ci callBackStatus = napi_unwrap(env, instance, (void**)&entity); 2305ccb8f90Sopenharmony_ci if (callBackStatus != napi_ok || entity == nullptr) { 2315ccb8f90Sopenharmony_ci POWER_HILOGE(FEATURE_RUNNING_LOCK, "Cannot unwrap entity from instance"); 2325ccb8f90Sopenharmony_ci return nullptr; 2335ccb8f90Sopenharmony_ci } 2345ccb8f90Sopenharmony_ci entity->runningLock = asyncInfo->runningLock; 2355ccb8f90Sopenharmony_ci return instance; 2365ccb8f90Sopenharmony_ci} 2375ccb8f90Sopenharmony_ci 2385ccb8f90Sopenharmony_civoid RunningLockInterface::CreateRunningLockCallBack(napi_env env, std::unique_ptr<RunningLockAsyncInfo>& asyncInfo) 2395ccb8f90Sopenharmony_ci{ 2405ccb8f90Sopenharmony_ci napi_value resource = nullptr; 2415ccb8f90Sopenharmony_ci napi_create_string_utf8(env, "createRunningLock", NAPI_AUTO_LENGTH, &resource); 2425ccb8f90Sopenharmony_ci napi_create_async_work( 2435ccb8f90Sopenharmony_ci env, nullptr, resource, 2445ccb8f90Sopenharmony_ci [](napi_env env, void* data) { 2455ccb8f90Sopenharmony_ci auto* asyncInfo = reinterpret_cast<RunningLockAsyncInfo*>(data); 2465ccb8f90Sopenharmony_ci if (!IsTypeSupported(asyncInfo->type)) { 2475ccb8f90Sopenharmony_ci auto type = static_cast<uint32_t>(asyncInfo->type); 2485ccb8f90Sopenharmony_ci POWER_HILOGW(FEATURE_RUNNING_LOCK, "type=%{public}u not supported", type); 2495ccb8f90Sopenharmony_ci return; 2505ccb8f90Sopenharmony_ci } 2515ccb8f90Sopenharmony_ci asyncInfo->runningLock = g_powerMgrClient.CreateRunningLock(std::string(asyncInfo->name), asyncInfo->type); 2525ccb8f90Sopenharmony_ci }, 2535ccb8f90Sopenharmony_ci [](napi_env env, napi_status status, void* data) { 2545ccb8f90Sopenharmony_ci auto* asyncInfo = reinterpret_cast<RunningLockAsyncInfo*>(data); 2555ccb8f90Sopenharmony_ci napi_value result[RESULT_SIZE] = {0}; 2565ccb8f90Sopenharmony_ci result[1] = CreateInstanceForRunningLock(env, asyncInfo); 2575ccb8f90Sopenharmony_ci if (result[1] == nullptr) { 2585ccb8f90Sopenharmony_ci napi_value message = nullptr; 2595ccb8f90Sopenharmony_ci napi_create_string_utf8(env, "runningLock create failed", NAPI_AUTO_LENGTH, &message); 2605ccb8f90Sopenharmony_ci napi_create_error(env, nullptr, message, &result[0]); 2615ccb8f90Sopenharmony_ci } 2625ccb8f90Sopenharmony_ci if (asyncInfo->deferred) { 2635ccb8f90Sopenharmony_ci if (result[1] != nullptr) { 2645ccb8f90Sopenharmony_ci napi_resolve_deferred(env, asyncInfo->deferred, result[1]); 2655ccb8f90Sopenharmony_ci } else { 2665ccb8f90Sopenharmony_ci napi_reject_deferred(env, asyncInfo->deferred, result[0]); 2675ccb8f90Sopenharmony_ci } 2685ccb8f90Sopenharmony_ci } else { 2695ccb8f90Sopenharmony_ci napi_value tmp = nullptr; 2705ccb8f90Sopenharmony_ci napi_value callback = nullptr; 2715ccb8f90Sopenharmony_ci napi_get_undefined(env, &result[0]); 2725ccb8f90Sopenharmony_ci napi_get_reference_value(env, asyncInfo->callbackRef, &callback); 2735ccb8f90Sopenharmony_ci napi_call_function(env, nullptr, callback, RESULT_SIZE, result, &tmp); 2745ccb8f90Sopenharmony_ci napi_delete_reference(env, asyncInfo->callbackRef); 2755ccb8f90Sopenharmony_ci } 2765ccb8f90Sopenharmony_ci napi_delete_async_work(env, asyncInfo->asyncWork); 2775ccb8f90Sopenharmony_ci delete asyncInfo; 2785ccb8f90Sopenharmony_ci }, 2795ccb8f90Sopenharmony_ci reinterpret_cast<void*>(asyncInfo.get()), &asyncInfo->asyncWork); 2805ccb8f90Sopenharmony_ci if (napi_ok == napi_queue_async_work_with_qos(env, asyncInfo->asyncWork, napi_qos_utility)) { 2815ccb8f90Sopenharmony_ci asyncInfo.release(); 2825ccb8f90Sopenharmony_ci } 2835ccb8f90Sopenharmony_ci} 2845ccb8f90Sopenharmony_ci 2855ccb8f90Sopenharmony_civoid RunningLockInterface::IsRunningLockTypeSupportedCallBack( 2865ccb8f90Sopenharmony_ci napi_env env, std::unique_ptr<RunningLockAsyncInfo>& asyncInfo) 2875ccb8f90Sopenharmony_ci{ 2885ccb8f90Sopenharmony_ci napi_value resource = nullptr; 2895ccb8f90Sopenharmony_ci napi_create_string_utf8(env, "isRunningLockTypeSupported", NAPI_AUTO_LENGTH, &resource); 2905ccb8f90Sopenharmony_ci napi_create_async_work( 2915ccb8f90Sopenharmony_ci env, nullptr, resource, 2925ccb8f90Sopenharmony_ci [](napi_env env, void* data) { 2935ccb8f90Sopenharmony_ci auto* asyncInfo = reinterpret_cast<RunningLockAsyncInfo*>(data); 2945ccb8f90Sopenharmony_ci asyncInfo->isSupported = IsTypeSupported(asyncInfo->type); 2955ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "type=%{public}u, isSupported=%{public}s", 2965ccb8f90Sopenharmony_ci static_cast<uint32_t>(asyncInfo->type), asyncInfo->isSupported ? "true" : "false"); 2975ccb8f90Sopenharmony_ci }, 2985ccb8f90Sopenharmony_ci [](napi_env env, napi_status status, void* data) { 2995ccb8f90Sopenharmony_ci auto* asyncInfo = reinterpret_cast<RunningLockAsyncInfo*>(data); 3005ccb8f90Sopenharmony_ci napi_value result[RESULT_SIZE] = {0}; 3015ccb8f90Sopenharmony_ci napi_get_boolean(env, asyncInfo->isSupported, &result[1]); 3025ccb8f90Sopenharmony_ci if (asyncInfo->deferred) { 3035ccb8f90Sopenharmony_ci napi_resolve_deferred(env, asyncInfo->deferred, result[1]); 3045ccb8f90Sopenharmony_ci } else { 3055ccb8f90Sopenharmony_ci napi_value tmp = nullptr; 3065ccb8f90Sopenharmony_ci napi_value callback = nullptr; 3075ccb8f90Sopenharmony_ci napi_get_reference_value(env, asyncInfo->callbackRef, &callback); 3085ccb8f90Sopenharmony_ci napi_get_undefined(env, &result[0]); 3095ccb8f90Sopenharmony_ci napi_call_function(env, nullptr, callback, RESULT_SIZE, result, &tmp); 3105ccb8f90Sopenharmony_ci napi_delete_reference(env, asyncInfo->callbackRef); 3115ccb8f90Sopenharmony_ci } 3125ccb8f90Sopenharmony_ci napi_delete_async_work(env, asyncInfo->asyncWork); 3135ccb8f90Sopenharmony_ci delete asyncInfo; 3145ccb8f90Sopenharmony_ci }, 3155ccb8f90Sopenharmony_ci reinterpret_cast<void*>(asyncInfo.get()), &asyncInfo->asyncWork); 3165ccb8f90Sopenharmony_ci if (napi_ok == napi_queue_async_work_with_qos(env, asyncInfo->asyncWork, napi_qos_utility)) { 3175ccb8f90Sopenharmony_ci asyncInfo.release(); 3185ccb8f90Sopenharmony_ci } 3195ccb8f90Sopenharmony_ci} 3205ccb8f90Sopenharmony_ci 3215ccb8f90Sopenharmony_cibool RunningLockInterface::IsTypeSupported(RunningLockType type) 3225ccb8f90Sopenharmony_ci{ 3235ccb8f90Sopenharmony_ci return type == RunningLockType::RUNNINGLOCK_BACKGROUND || 3245ccb8f90Sopenharmony_ci type == RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL; 3255ccb8f90Sopenharmony_ci} 3265ccb8f90Sopenharmony_ci} // namespace PowerMgr 3275ccb8f90Sopenharmony_ci} // namespace OHOS 328