1 /*
2 * Copyright (c) 2022 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 <memory>
17
18 #include "napi/native_api.h"
19 #include "napi/native_common.h"
20 #include "napi/native_node_api.h"
21
22 #include "power_log.h"
23 #include "running_lock_info.h"
24 #include "runninglock_entity.h"
25 #include "runninglock_interface.h"
26 #include "runninglock_napi.h"
27
28 using namespace OHOS::PowerMgr;
29
30 namespace {
31 static thread_local napi_ref g_napiRunningLockIns;
32 }
33
RunningLockConstructor(napi_env env,napi_callback_info info)34 static napi_value RunningLockConstructor(napi_env env, napi_callback_info info)
35 {
36 napi_value thisVar = nullptr;
37 void* data = nullptr;
38 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
39
40 std::unique_ptr<RunningLockEntity> runningLockEntity = std::make_unique<RunningLockEntity>();
41 napi_status status = napi_wrap(
42 env, thisVar, runningLockEntity.get(),
43 [](napi_env env, void* data, void* hint) {
44 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Destructor");
45 auto entity = reinterpret_cast<RunningLockEntity*>(data);
46 delete entity;
47 },
48 nullptr, nullptr);
49 if (status == napi_ok) {
50 runningLockEntity.release();
51 }
52
53 return thisVar;
54 }
55
CreateRunningLockClass(napi_env env,napi_value exports)56 static napi_value CreateRunningLockClass(napi_env env, napi_value exports)
57 {
58 napi_property_descriptor desc[] = {
59 // Old interfaces deprecated since 9
60 DECLARE_NAPI_FUNCTION("unlock", RunningLockInterface::Unlock),
61 DECLARE_NAPI_FUNCTION("isUsed", RunningLockInterface::IsUsed),
62 DECLARE_NAPI_FUNCTION("lock", RunningLockInterface::Lock),
63 // New interfaces
64 DECLARE_NAPI_FUNCTION("hold", RunningLockNapi::Hold),
65 DECLARE_NAPI_FUNCTION("isHolding", RunningLockNapi::IsHolding),
66 DECLARE_NAPI_FUNCTION("unhold", RunningLockNapi::UnHold),
67 };
68
69 napi_value result = nullptr;
70 napi_define_class(env, "RunningLock", NAPI_AUTO_LENGTH, RunningLockConstructor, nullptr,
71 sizeof(desc) / sizeof(*desc), desc, &result);
72
73 napi_create_reference(env, result, 1, &g_napiRunningLockIns);
74 napi_set_named_property(env, exports, "RunningLock", result);
75 return exports;
76 }
77
RunningLockTypeConstructor(napi_env env,napi_callback_info info)78 static napi_value RunningLockTypeConstructor(napi_env env, napi_callback_info info)
79 {
80 napi_value thisArg = nullptr;
81 void* data = nullptr;
82 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
83 napi_value global = nullptr;
84 napi_get_global(env, &global);
85 return thisArg;
86 }
87
CreateRunningLockType(napi_env env,napi_value exports)88 static napi_value CreateRunningLockType(napi_env env, napi_value exports)
89 {
90 napi_value background = nullptr;
91 napi_value proximityscreencontrol = nullptr;
92
93 napi_create_int32(env, (int32_t)RunningLockType::RUNNINGLOCK_BACKGROUND, &background);
94 napi_create_int32(env, (int32_t)RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL, &proximityscreencontrol);
95
96 napi_property_descriptor desc[] = {
97 DECLARE_NAPI_STATIC_PROPERTY("BACKGROUND", background),
98 DECLARE_NAPI_STATIC_PROPERTY("PROXIMITY_SCREEN_CONTROL", proximityscreencontrol),
99 };
100 napi_value result = nullptr;
101 napi_define_class(env, "RunningLockType", NAPI_AUTO_LENGTH, RunningLockTypeConstructor, nullptr,
102 sizeof(desc) / sizeof(*desc), desc, &result);
103
104 napi_set_named_property(env, exports, "RunningLockType", result);
105 return exports;
106 }
107
CreateRunningLock(napi_env env,napi_callback_info info)108 static napi_value CreateRunningLock(napi_env env, napi_callback_info info)
109 {
110 return RunningLockInterface::CreateRunningLock(env, info, g_napiRunningLockIns);
111 }
112
Create(napi_env env,napi_callback_info info)113 static napi_value Create(napi_env env, napi_callback_info info)
114 {
115 return RunningLockNapi::Create(env, info, g_napiRunningLockIns);
116 }
117
118 EXTERN_C_START
119 /*
120 * function for module exports
121 */
RunningLockInit(napi_env env,napi_value exports)122 static napi_value RunningLockInit(napi_env env, napi_value exports)
123 {
124 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Initialize the RunningLock module");
125
126 CreateRunningLockType(env, exports);
127 CreateRunningLockClass(env, exports);
128 napi_property_descriptor desc[] = {
129 // Old interfaces deprecated since 9
130 DECLARE_NAPI_FUNCTION("createRunningLock", CreateRunningLock),
131 DECLARE_NAPI_FUNCTION("isRunningLockTypeSupported", RunningLockInterface::IsRunningLockTypeSupported),
132 // New interfaces
133 DECLARE_NAPI_FUNCTION("create", Create), DECLARE_NAPI_FUNCTION("isSupported", RunningLockNapi::IsSupported)};
134
135 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
136 POWER_HILOGD(FEATURE_RUNNING_LOCK, "The initialization of the RunningLock module is complete");
137
138 return exports;
139 }
140 EXTERN_C_END
141
142 /*
143 * Module definition
144 */
145 static napi_module g_module = {.nm_version = 1,
146 .nm_flags = 0,
147 .nm_filename = "runningLock",
148 .nm_register_func = RunningLockInit,
149 .nm_modname = "runningLock",
150 .nm_priv = ((void*)0),
151 .reserved = {0}};
152
153 /*
154 * Module registration
155 */
RegisterRunninglockModule(void)156 extern "C" __attribute__((constructor)) void RegisterRunninglockModule(void)
157 {
158 napi_module_register(&g_module);
159 }
160