1 /* 2 * Copyright (c) 2022-2023 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 #ifndef POWERMGR_POWER_MANAGER_RUNNINGLOCK_INTERFACE_H 17 #define POWERMGR_POWER_MANAGER_RUNNINGLOCK_INTERFACE_H 18 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi/native_node_api.h" 24 25 #include "running_lock.h" 26 #include "running_lock_info.h" 27 28 namespace OHOS { 29 namespace PowerMgr { 30 struct RunningLockAsyncInfo { 31 napi_async_work asyncWork = nullptr; 32 napi_ref callbackRef = nullptr; 33 napi_deferred deferred = nullptr; 34 std::string name; 35 size_t nameLen = 0; 36 RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT; 37 bool isSupported = false; 38 std::shared_ptr<RunningLock> runningLock = nullptr; 39 napi_ref napiRunningLockIns = nullptr; 40 }; 41 42 class RunningLockInterface { 43 public: 44 static napi_value CreateRunningLock(napi_env env, napi_callback_info info, napi_ref& napiRunningLock); 45 static napi_value IsRunningLockTypeSupported(napi_env env, napi_callback_info info); 46 static napi_value Lock(napi_env env, napi_callback_info info); 47 static napi_value IsUsed(napi_env env, napi_callback_info info); 48 static napi_value Unlock(napi_env env, napi_callback_info info); 49 50 private: 51 static napi_value CreateInstanceForRunningLock(napi_env env, RunningLockAsyncInfo* asyncInfo); 52 static void CreateRunningLockCallBack(napi_env env, std::unique_ptr<RunningLockAsyncInfo>& asyncInfo); 53 static void IsRunningLockTypeSupportedCallBack(napi_env env, std::unique_ptr<RunningLockAsyncInfo>& asyncInfo); 54 static bool IsTypeSupported(RunningLockType type); 55 }; 56 } // namespace PowerMgr 57 } // namespace OHOS 58 59 #endif // POWERMGR_POWER_MANAGER_RUNNINGLOCK_INTERFACE_H 60