15ccb8f90Sopenharmony_ci/* 25ccb8f90Sopenharmony_ci * Copyright (c) 2022 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 <memory> 175ccb8f90Sopenharmony_ci 185ccb8f90Sopenharmony_ci#include "napi/native_api.h" 195ccb8f90Sopenharmony_ci#include "napi/native_common.h" 205ccb8f90Sopenharmony_ci#include "napi/native_node_api.h" 215ccb8f90Sopenharmony_ci 225ccb8f90Sopenharmony_ci#include "power_log.h" 235ccb8f90Sopenharmony_ci#include "running_lock_info.h" 245ccb8f90Sopenharmony_ci#include "runninglock_entity.h" 255ccb8f90Sopenharmony_ci#include "runninglock_interface.h" 265ccb8f90Sopenharmony_ci#include "runninglock_napi.h" 275ccb8f90Sopenharmony_ci 285ccb8f90Sopenharmony_ciusing namespace OHOS::PowerMgr; 295ccb8f90Sopenharmony_ci 305ccb8f90Sopenharmony_cinamespace { 315ccb8f90Sopenharmony_cistatic thread_local napi_ref g_napiRunningLockIns; 325ccb8f90Sopenharmony_ci} 335ccb8f90Sopenharmony_ci 345ccb8f90Sopenharmony_cistatic napi_value RunningLockConstructor(napi_env env, napi_callback_info info) 355ccb8f90Sopenharmony_ci{ 365ccb8f90Sopenharmony_ci napi_value thisVar = nullptr; 375ccb8f90Sopenharmony_ci void* data = nullptr; 385ccb8f90Sopenharmony_ci napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data); 395ccb8f90Sopenharmony_ci 405ccb8f90Sopenharmony_ci std::unique_ptr<RunningLockEntity> runningLockEntity = std::make_unique<RunningLockEntity>(); 415ccb8f90Sopenharmony_ci napi_status status = napi_wrap( 425ccb8f90Sopenharmony_ci env, thisVar, runningLockEntity.get(), 435ccb8f90Sopenharmony_ci [](napi_env env, void* data, void* hint) { 445ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "Destructor"); 455ccb8f90Sopenharmony_ci auto entity = reinterpret_cast<RunningLockEntity*>(data); 465ccb8f90Sopenharmony_ci delete entity; 475ccb8f90Sopenharmony_ci }, 485ccb8f90Sopenharmony_ci nullptr, nullptr); 495ccb8f90Sopenharmony_ci if (status == napi_ok) { 505ccb8f90Sopenharmony_ci runningLockEntity.release(); 515ccb8f90Sopenharmony_ci } 525ccb8f90Sopenharmony_ci 535ccb8f90Sopenharmony_ci return thisVar; 545ccb8f90Sopenharmony_ci} 555ccb8f90Sopenharmony_ci 565ccb8f90Sopenharmony_cistatic napi_value CreateRunningLockClass(napi_env env, napi_value exports) 575ccb8f90Sopenharmony_ci{ 585ccb8f90Sopenharmony_ci napi_property_descriptor desc[] = { 595ccb8f90Sopenharmony_ci // Old interfaces deprecated since 9 605ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("unlock", RunningLockInterface::Unlock), 615ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isUsed", RunningLockInterface::IsUsed), 625ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("lock", RunningLockInterface::Lock), 635ccb8f90Sopenharmony_ci // New interfaces 645ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("hold", RunningLockNapi::Hold), 655ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isHolding", RunningLockNapi::IsHolding), 665ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("unhold", RunningLockNapi::UnHold), 675ccb8f90Sopenharmony_ci }; 685ccb8f90Sopenharmony_ci 695ccb8f90Sopenharmony_ci napi_value result = nullptr; 705ccb8f90Sopenharmony_ci napi_define_class(env, "RunningLock", NAPI_AUTO_LENGTH, RunningLockConstructor, nullptr, 715ccb8f90Sopenharmony_ci sizeof(desc) / sizeof(*desc), desc, &result); 725ccb8f90Sopenharmony_ci 735ccb8f90Sopenharmony_ci napi_create_reference(env, result, 1, &g_napiRunningLockIns); 745ccb8f90Sopenharmony_ci napi_set_named_property(env, exports, "RunningLock", result); 755ccb8f90Sopenharmony_ci return exports; 765ccb8f90Sopenharmony_ci} 775ccb8f90Sopenharmony_ci 785ccb8f90Sopenharmony_cistatic napi_value RunningLockTypeConstructor(napi_env env, napi_callback_info info) 795ccb8f90Sopenharmony_ci{ 805ccb8f90Sopenharmony_ci napi_value thisArg = nullptr; 815ccb8f90Sopenharmony_ci void* data = nullptr; 825ccb8f90Sopenharmony_ci napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data); 835ccb8f90Sopenharmony_ci napi_value global = nullptr; 845ccb8f90Sopenharmony_ci napi_get_global(env, &global); 855ccb8f90Sopenharmony_ci return thisArg; 865ccb8f90Sopenharmony_ci} 875ccb8f90Sopenharmony_ci 885ccb8f90Sopenharmony_cistatic napi_value CreateRunningLockType(napi_env env, napi_value exports) 895ccb8f90Sopenharmony_ci{ 905ccb8f90Sopenharmony_ci napi_value background = nullptr; 915ccb8f90Sopenharmony_ci napi_value proximityscreencontrol = nullptr; 925ccb8f90Sopenharmony_ci 935ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)RunningLockType::RUNNINGLOCK_BACKGROUND, &background); 945ccb8f90Sopenharmony_ci napi_create_int32(env, (int32_t)RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, &proximityscreencontrol); 955ccb8f90Sopenharmony_ci 965ccb8f90Sopenharmony_ci napi_property_descriptor desc[] = { 975ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("BACKGROUND", background), 985ccb8f90Sopenharmony_ci DECLARE_NAPI_STATIC_PROPERTY("PROXIMITY_SCREEN_CONTROL", proximityscreencontrol), 995ccb8f90Sopenharmony_ci }; 1005ccb8f90Sopenharmony_ci napi_value result = nullptr; 1015ccb8f90Sopenharmony_ci napi_define_class(env, "RunningLockType", NAPI_AUTO_LENGTH, RunningLockTypeConstructor, nullptr, 1025ccb8f90Sopenharmony_ci sizeof(desc) / sizeof(*desc), desc, &result); 1035ccb8f90Sopenharmony_ci 1045ccb8f90Sopenharmony_ci napi_set_named_property(env, exports, "RunningLockType", result); 1055ccb8f90Sopenharmony_ci return exports; 1065ccb8f90Sopenharmony_ci} 1075ccb8f90Sopenharmony_ci 1085ccb8f90Sopenharmony_cistatic napi_value CreateRunningLock(napi_env env, napi_callback_info info) 1095ccb8f90Sopenharmony_ci{ 1105ccb8f90Sopenharmony_ci return RunningLockInterface::CreateRunningLock(env, info, g_napiRunningLockIns); 1115ccb8f90Sopenharmony_ci} 1125ccb8f90Sopenharmony_ci 1135ccb8f90Sopenharmony_cistatic napi_value Create(napi_env env, napi_callback_info info) 1145ccb8f90Sopenharmony_ci{ 1155ccb8f90Sopenharmony_ci return RunningLockNapi::Create(env, info, g_napiRunningLockIns); 1165ccb8f90Sopenharmony_ci} 1175ccb8f90Sopenharmony_ci 1185ccb8f90Sopenharmony_ciEXTERN_C_START 1195ccb8f90Sopenharmony_ci/* 1205ccb8f90Sopenharmony_ci * function for module exports 1215ccb8f90Sopenharmony_ci */ 1225ccb8f90Sopenharmony_cistatic napi_value RunningLockInit(napi_env env, napi_value exports) 1235ccb8f90Sopenharmony_ci{ 1245ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "Initialize the RunningLock module"); 1255ccb8f90Sopenharmony_ci 1265ccb8f90Sopenharmony_ci CreateRunningLockType(env, exports); 1275ccb8f90Sopenharmony_ci CreateRunningLockClass(env, exports); 1285ccb8f90Sopenharmony_ci napi_property_descriptor desc[] = { 1295ccb8f90Sopenharmony_ci // Old interfaces deprecated since 9 1305ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("createRunningLock", CreateRunningLock), 1315ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("isRunningLockTypeSupported", RunningLockInterface::IsRunningLockTypeSupported), 1325ccb8f90Sopenharmony_ci // New interfaces 1335ccb8f90Sopenharmony_ci DECLARE_NAPI_FUNCTION("create", Create), DECLARE_NAPI_FUNCTION("isSupported", RunningLockNapi::IsSupported)}; 1345ccb8f90Sopenharmony_ci 1355ccb8f90Sopenharmony_ci NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); 1365ccb8f90Sopenharmony_ci POWER_HILOGD(FEATURE_RUNNING_LOCK, "The initialization of the RunningLock module is complete"); 1375ccb8f90Sopenharmony_ci 1385ccb8f90Sopenharmony_ci return exports; 1395ccb8f90Sopenharmony_ci} 1405ccb8f90Sopenharmony_ciEXTERN_C_END 1415ccb8f90Sopenharmony_ci 1425ccb8f90Sopenharmony_ci/* 1435ccb8f90Sopenharmony_ci * Module definition 1445ccb8f90Sopenharmony_ci */ 1455ccb8f90Sopenharmony_cistatic napi_module g_module = {.nm_version = 1, 1465ccb8f90Sopenharmony_ci .nm_flags = 0, 1475ccb8f90Sopenharmony_ci .nm_filename = "runningLock", 1485ccb8f90Sopenharmony_ci .nm_register_func = RunningLockInit, 1495ccb8f90Sopenharmony_ci .nm_modname = "runningLock", 1505ccb8f90Sopenharmony_ci .nm_priv = ((void*)0), 1515ccb8f90Sopenharmony_ci .reserved = {0}}; 1525ccb8f90Sopenharmony_ci 1535ccb8f90Sopenharmony_ci/* 1545ccb8f90Sopenharmony_ci * Module registration 1555ccb8f90Sopenharmony_ci */ 1565ccb8f90Sopenharmony_ciextern "C" __attribute__((constructor)) void RegisterRunninglockModule(void) 1575ccb8f90Sopenharmony_ci{ 1585ccb8f90Sopenharmony_ci napi_module_register(&g_module); 1595ccb8f90Sopenharmony_ci} 160