• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "napi_screenlock_ability.h"
16 
17 #include <hitrace_meter.h>
18 #include <napi/native_api.h>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <uv.h>
22 
23 #include <map>
24 
25 #include "event_listener.h"
26 #include "ipc_skeleton.h"
27 #include "sclock_log.h"
28 #include "screenlock_app_manager.h"
29 #include "screenlock_callback.h"
30 #include "screenlock_common.h"
31 #include "screenlock_js_util.h"
32 #include "screenlock_manager.h"
33 #include "screenlock_system_ability_callback.h"
34 
35 using namespace OHOS;
36 using namespace OHOS::ScreenLock;
37 
38 namespace OHOS {
39 namespace ScreenLock {
40 constexpr const char *PERMISSION_VALIDATION_FAILED = "Permission verification failed.";
41 constexpr const char *PARAMETER_VALIDATION_FAILED = "Parameter verification failed.";
42 constexpr const char *CANCEL_UNLOCK_OPENATION = "The user canceled the unlock openation.";
43 constexpr const char *SERVICE_IS_ABNORMAL = "The screenlock management service is abnormal.";
44 constexpr const char *NON_SYSTEM_APP = "Permission verification failed, application which is not a system application "
45                                        "uses system API.";
46 const std::map<int, uint32_t> ERROR_CODE_CONVERSION = {
47     { E_SCREENLOCK_NO_PERMISSION, JsErrorCode::ERR_NO_PERMISSION },
48     { E_SCREENLOCK_PARAMETERS_INVALID, JsErrorCode::ERR_INVALID_PARAMS },
49     { E_SCREENLOCK_WRITE_PARCEL_ERROR, JsErrorCode::ERR_SERVICE_ABNORMAL },
50     { E_SCREENLOCK_NULLPTR, JsErrorCode::ERR_SERVICE_ABNORMAL },
51     { E_SCREENLOCK_SENDREQUEST_FAILED, JsErrorCode::ERR_SERVICE_ABNORMAL },
52     { E_SCREENLOCK_NOT_SYSTEM_APP, JsErrorCode::ERR_NOT_SYSTEM_APP },
53 };
54 const std::map<uint32_t, std::string> ERROR_INFO_MAP = {
55     { JsErrorCode::ERR_NO_PERMISSION, PERMISSION_VALIDATION_FAILED },
56     { JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED },
57     { JsErrorCode::ERR_CANCEL_UNLOCK, CANCEL_UNLOCK_OPENATION },
58     { JsErrorCode::ERR_SERVICE_ABNORMAL, SERVICE_IS_ABNORMAL },
59     { JsErrorCode::ERR_NOT_SYSTEM_APP, NON_SYSTEM_APP },
60 };
61 
Init(napi_env env,napi_value exports)62 napi_status Init(napi_env env, napi_value exports)
63 {
64     napi_property_descriptor exportFuncs[] = {
65         DECLARE_NAPI_FUNCTION("isScreenLocked", OHOS::ScreenLock::NAPI_IsScreenLocked),
66         DECLARE_NAPI_FUNCTION("isLocked", OHOS::ScreenLock::NAPI_IsLocked),
67         DECLARE_NAPI_FUNCTION("lock", OHOS::ScreenLock::NAPI_Lock),
68         DECLARE_NAPI_FUNCTION("unlockScreen", OHOS::ScreenLock::NAPI_UnlockScreen),
69         DECLARE_NAPI_FUNCTION("unlock", OHOS::ScreenLock::NAPI_Unlock),
70         DECLARE_NAPI_FUNCTION("isSecureMode", OHOS::ScreenLock::NAPI_IsSecureMode),
71         DECLARE_NAPI_FUNCTION("onSystemEvent", NAPI_OnSystemEvent),
72         DECLARE_NAPI_FUNCTION("sendScreenLockEvent", OHOS::ScreenLock::NAPI_ScreenLockSendEvent),
73     };
74     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
75     return napi_ok;
76 }
77 
IsVaildEvent(const std::string & type)78 napi_status IsVaildEvent(const std::string &type)
79 {
80     if (type == UNLOCK_SCREEN_RESULT || type == SCREEN_DRAWDONE || type == LOCK_SCREEN_RESULT) {
81         return napi_ok;
82     }
83     return napi_invalid_arg;
84 }
85 
CheckParamType(napi_env env,napi_value param,napi_valuetype jsType)86 napi_status CheckParamType(napi_env env, napi_value param, napi_valuetype jsType)
87 {
88     napi_valuetype valueType = napi_undefined;
89     napi_status status = napi_typeof(env, param, &valueType);
90     if (status != napi_ok || valueType != jsType) {
91         return napi_invalid_arg;
92     }
93     return napi_ok;
94 }
95 
CheckParamNumber(size_t argc,std::uint32_t paramNumber)96 napi_status CheckParamNumber(size_t argc, std::uint32_t paramNumber)
97 {
98     if (argc < paramNumber) {
99         return napi_invalid_arg;
100     }
101     return napi_ok;
102 }
103 
ThrowError(napi_env env,const uint32_t & code,const std::string & msg)104 void ThrowError(napi_env env, const uint32_t &code, const std::string &msg)
105 {
106     SCLOCK_HILOGD("ThrowError start");
107     std::string errorCode = std::to_string(code);
108     napi_status status = napi_throw_error(env, errorCode.c_str(), msg.c_str());
109     if (status != napi_ok) {
110         SCLOCK_HILOGD("Failed to napi_throw_error");
111     }
112     SCLOCK_HILOGD("ThrowError end");
113 }
114 
GetErrorInfo(int32_t errorCode,ErrorInfo & errorInfo)115 void GetErrorInfo(int32_t errorCode, ErrorInfo &errorInfo)
116 {
117     std::map<int, uint32_t>::const_iterator iter = ERROR_CODE_CONVERSION.find(errorCode);
118     if (iter != ERROR_CODE_CONVERSION.end()) {
119         errorInfo.errorCode_ = iter->second;
120         errorInfo.message_ = GetErrorMessage(errorInfo.errorCode_);
121         SCLOCK_HILOGD("GetErrorInfo errorInfo.code: %{public}d, errorInfo.message: %{public}s", errorInfo.errorCode_,
122             errorInfo.message_.c_str());
123     } else {
124         SCLOCK_HILOGD("GetErrorInfo errCode: %{public}d", errorCode);
125     }
126 }
127 
GetErrorMessage(const uint32_t & code)128 std::string GetErrorMessage(const uint32_t &code)
129 {
130     std::string message;
131     std::map<uint32_t, std::string>::const_iterator iter = ERROR_INFO_MAP.find(code);
132     if (iter != ERROR_INFO_MAP.end()) {
133         message = iter->second;
134     }
135     SCLOCK_HILOGD("GetErrorMessage: message is %{public}s", message.c_str());
136     return message;
137 }
138 
NAPI_IsScreenLocked(napi_env env,napi_callback_info info)139 napi_value NAPI_IsScreenLocked(napi_env env, napi_callback_info info)
140 {
141     SCLOCK_HILOGD("NAPI_IsScreenLocked begin");
142     AsyncScreenLockInfo *context = new AsyncScreenLockInfo();
143     auto input = [context](napi_env env, size_t argc, napi_value argv[], napi_value self) -> napi_status {
144         NAPI_ASSERT_BASE(
145             env, argc == ARGS_SIZE_ZERO || argc == ARGS_SIZE_ONE, " should 0 or 1 parameters!", napi_invalid_arg);
146         SCLOCK_HILOGD("input ---- argc : %{public}zu", argc);
147         return napi_ok;
148     };
149     auto output = [context](napi_env env, napi_value *result) -> napi_status {
150         napi_status status = napi_get_boolean(env, context->allowed, result);
151         SCLOCK_HILOGD("output ---- napi_get_boolean[%{public}d]", status);
152         return napi_ok;
153     };
154     auto exec = [context](AsyncCall::Context *ctx) {
155         context->allowed = ScreenLockManager::GetInstance()->IsScreenLocked();
156         SCLOCK_HILOGD("NAPI_IsScreenLocked exec allowed = %{public}d ", context->allowed);
157         context->SetStatus(napi_ok);
158     };
159     context->SetAction(std::move(input), std::move(output));
160     AsyncCall asyncCall(env, info, context, ARGS_SIZE_ZERO);
161     return asyncCall.Call(env, exec);
162 }
163 
NAPI_IsLocked(napi_env env,napi_callback_info info)164 napi_value NAPI_IsLocked(napi_env env, napi_callback_info info)
165 {
166     napi_value result = nullptr;
167     bool isLocked = false;
168     int32_t status = ScreenLockManager::GetInstance()->IsLocked(isLocked);
169     SCLOCK_HILOGD("is screen locked ret: %{public}d ", status);
170     if (status != E_SCREENLOCK_OK) {
171         ThrowError(env, JsErrorCode::ERR_NOT_SYSTEM_APP, NON_SYSTEM_APP);
172         return result;
173     }
174     napi_get_boolean(env, isLocked, &result);
175     return result;
176 }
177 
CompleteAsyncWork(napi_env env,napi_status status,void * data)178 static void CompleteAsyncWork(napi_env env, napi_status status, void *data)
179 {
180     EventListener *eventListener = reinterpret_cast<EventListener *>(data);
181     if (eventListener == nullptr) {
182         return;
183     }
184     if (eventListener->work != nullptr) {
185         napi_delete_async_work(env, eventListener->work);
186     }
187     delete eventListener;
188 }
189 
AsyncCallFunc(napi_env env,EventListener * listener)190 void AsyncCallFunc(napi_env env, EventListener *listener)
191 {
192     napi_value resource = nullptr;
193     auto execute = [](napi_env env, void *data) {
194         EventListener *eventListener = reinterpret_cast<EventListener *>(data);
195         if (eventListener == nullptr) {
196             return;
197         }
198 
199         sptr<ScreenLockSystemAbilityInterface> callback = new (std::nothrow) ScreenlockCallback(*eventListener);
200         if (callback == nullptr) {
201             if (eventListener->callbackRef != nullptr) {
202                 napi_delete_reference(env, eventListener->callbackRef);
203             }
204             return;
205         }
206         int32_t status = 0;
207         if (eventListener->action == Action::LOCK) {
208             status = ScreenLockManager::GetInstance()->Lock(callback);
209         } else if (eventListener->action == Action::UNLOCK || eventListener->action == Action::UNLOCKSCREEN) {
210             status = ScreenLockManager::GetInstance()->Unlock(eventListener->action, callback);
211         }
212         if (status != E_SCREENLOCK_OK) {
213             ErrorInfo errInfo(static_cast<uint32_t>(status));
214             GetErrorInfo(status, errInfo);
215             callback->SetErrorInfo(errInfo);
216             SystemEvent systemEvent("", std::to_string(status));
217             callback->OnCallBack(systemEvent);
218         }
219     };
220     NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, "AsyncCall", NAPI_AUTO_LENGTH, &resource));
221     NAPI_CALL_RETURN_VOID(env, napi_create_async_work(env, nullptr, resource, execute, CompleteAsyncWork,
222                                    static_cast<void *>(listener), &(listener->work)));
223     NAPI_CALL_RETURN_VOID(env, napi_queue_async_work(env, listener->work));
224 }
225 
NAPI_Lock(napi_env env,napi_callback_info info)226 napi_value NAPI_Lock(napi_env env, napi_callback_info info)
227 {
228     SCLOCK_HILOGD("NAPI_Lock begin");
229     napi_value ret = nullptr;
230     size_t argc = ARGS_SIZE_ONE;
231     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
232     napi_value thisVar = nullptr;
233     void *data = nullptr;
234     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
235     napi_ref callbackRef = nullptr;
236     EventListener *eventListener = nullptr;
237     if (argc == ARGS_SIZE_ONE) {
238         SCLOCK_HILOGD("NAPI_Lock callback");
239         if (CheckParamType(env, argv[ARGV_ZERO], napi_function) != napi_ok) {
240             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
241             return ret;
242         }
243 
244         SCLOCK_HILOGD("NAPI_Lock create callback");
245         napi_create_reference(env, argv[ARGV_ZERO], 1, &callbackRef);
246         eventListener = new (std::nothrow) EventListener{ .env = env,
247             .thisVar = thisVar,
248             .callbackRef = callbackRef,
249             .callBackResult = true,
250             .action = Action::LOCK };
251         if (eventListener == nullptr) {
252             SCLOCK_HILOGE("eventListener is nullptr");
253             return nullptr;
254         }
255     }
256     if (callbackRef == nullptr) {
257         SCLOCK_HILOGD("NAPI_Lock create promise");
258         napi_deferred deferred;
259         napi_create_promise(env, &deferred, &ret);
260         eventListener = new (std::nothrow) EventListener{ .env = env,
261             .thisVar = thisVar,
262             .deferred = deferred,
263             .callBackResult = true,
264             .action = Action::LOCK };
265         if (eventListener == nullptr) {
266             SCLOCK_HILOGE("eventListener is nullptr");
267             return nullptr;
268         }
269     } else {
270         SCLOCK_HILOGD("NAPI_Lock create callback");
271         napi_get_undefined(env, &ret);
272     }
273     AsyncCallFunc(env, eventListener);
274     return ret;
275 }
276 
NAPI_UnlockScreen(napi_env env,napi_callback_info info)277 napi_value NAPI_UnlockScreen(napi_env env, napi_callback_info info)
278 {
279     SCLOCK_HILOGD("NAPI_UnlockScreen begin");
280     StartAsyncTrace(HITRACE_TAG_MISC, "NAPI_UnlockScreen start", HITRACE_UNLOCKSCREEN);
281     napi_value ret = nullptr;
282     size_t argc = ARGS_SIZE_ONE;
283     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
284     napi_value thisVar = nullptr;
285     void *data = nullptr;
286     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
287     NAPI_ASSERT(env, argc == ARGS_SIZE_ZERO || argc == ARGS_SIZE_ONE, "Wrong number of arguments, requires one");
288     napi_ref callbackRef = nullptr;
289     EventListener *eventListener = nullptr;
290     napi_valuetype valueType = napi_undefined;
291     if (argc == ARGS_SIZE_ONE) {
292         napi_typeof(env, argv[ARGV_ZERO], &valueType);
293         SCLOCK_HILOGD("NAPI_UnlockScreen callback");
294         NAPI_ASSERT(env, valueType == napi_function, "callback is not a function");
295         SCLOCK_HILOGD("NAPI_UnlockScreen create callback");
296         napi_create_reference(env, argv[ARGV_ZERO], 1, &callbackRef);
297         eventListener = new (std::nothrow)
298             EventListener{ .env = env, .thisVar = thisVar, .callbackRef = callbackRef, .action = Action::UNLOCKSCREEN };
299         if (eventListener == nullptr) {
300             SCLOCK_HILOGE("eventListener is nullptr");
301             return nullptr;
302         }
303     }
304     if (callbackRef == nullptr) {
305         SCLOCK_HILOGD("NAPI_UnlockScreen create promise");
306         napi_deferred deferred;
307         napi_create_promise(env, &deferred, &ret);
308         eventListener = new (std::nothrow)
309             EventListener{ .env = env, .thisVar = thisVar, .deferred = deferred, .action = Action::UNLOCKSCREEN };
310         if (eventListener == nullptr) {
311             SCLOCK_HILOGE("eventListener is nullptr");
312             return nullptr;
313         }
314     } else {
315         SCLOCK_HILOGD("NAPI_UnlockScreen create callback");
316         napi_get_undefined(env, &ret);
317     }
318     AsyncCallFunc(env, eventListener);
319     return ret;
320 }
321 
NAPI_Unlock(napi_env env,napi_callback_info info)322 napi_value NAPI_Unlock(napi_env env, napi_callback_info info)
323 {
324     SCLOCK_HILOGD("NAPI_Unlock begin");
325     napi_value ret = nullptr;
326     size_t argc = ARGS_SIZE_ONE;
327     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
328     napi_value thisVar = nullptr;
329     void *data = nullptr;
330     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
331     napi_ref callbackRef = nullptr;
332     EventListener *eventListener = nullptr;
333     if (argc == ARGS_SIZE_ONE) {
334         SCLOCK_HILOGD("NAPI_Unlock callback");
335         if (CheckParamType(env, argv[ARGV_ZERO], napi_function) != napi_ok) {
336             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
337             return nullptr;
338         }
339 
340         SCLOCK_HILOGD("NAPI_Unlock create callback");
341         napi_create_reference(env, argv[ARGV_ZERO], 1, &callbackRef);
342         eventListener = new (std::nothrow) EventListener{ .env = env,
343             .thisVar = thisVar,
344             .callbackRef = callbackRef,
345             .callBackResult = true,
346             .action = Action::UNLOCK };
347         if (eventListener == nullptr) {
348             SCLOCK_HILOGE("eventListener is nullptr");
349             return nullptr;
350         }
351     }
352     if (callbackRef == nullptr) {
353         SCLOCK_HILOGD("NAPI_Unlock create promise");
354         napi_deferred deferred;
355         napi_create_promise(env, &deferred, &ret);
356         eventListener = new (std::nothrow) EventListener{ .env = env,
357             .thisVar = thisVar,
358             .deferred = deferred,
359             .callBackResult = true,
360             .action = Action::UNLOCK };
361         if (eventListener == nullptr) {
362             SCLOCK_HILOGE("eventListener is nullptr");
363             return nullptr;
364         }
365     } else {
366         SCLOCK_HILOGD("NAPI_Unlock create callback");
367         napi_get_undefined(env, &ret);
368     }
369     AsyncCallFunc(env, eventListener);
370     return ret;
371 }
372 
NAPI_IsSecureMode(napi_env env,napi_callback_info info)373 napi_value NAPI_IsSecureMode(napi_env env, napi_callback_info info)
374 {
375     SCLOCK_HILOGD("NAPI_IsSecureMode begin");
376     AsyncScreenLockInfo *context = new AsyncScreenLockInfo();
377     auto input = [context](napi_env env, size_t argc, napi_value argv[], napi_value self) -> napi_status {
378         SCLOCK_HILOGD("input ---- argc : %{public}zu", argc);
379         return napi_ok;
380     };
381     auto output = [context](napi_env env, napi_value *result) -> napi_status {
382         napi_status status = napi_get_boolean(env, context->allowed, result);
383         SCLOCK_HILOGD("output ---- napi_get_boolean[%{public}d]", status);
384         return napi_ok;
385     };
386     auto exec = [context](AsyncCall::Context *ctx) {
387         SCLOCK_HILOGD("exec ---- NAPI_IsSecureMode begin");
388         context->allowed = ScreenLockManager::GetInstance()->GetSecure();
389         SCLOCK_HILOGD("NAPI_IsSecureMode exec allowed = %{public}d ", context->allowed);
390         context->SetStatus(napi_ok);
391     };
392     context->SetAction(std::move(input), std::move(output));
393     AsyncCall asyncCall(env, info, context, ARGS_SIZE_ZERO);
394     return asyncCall.Call(env, exec);
395 }
396 
NAPI_OnSystemEvent(napi_env env,napi_callback_info info)397 napi_value NAPI_OnSystemEvent(napi_env env, napi_callback_info info)
398 {
399     SCLOCK_HILOGD("NAPI_OnSystemEvent in");
400     napi_value result = nullptr;
401     bool status = false;
402     napi_get_boolean(env, status, &result);
403     size_t argc = ARGS_SIZE_ONE;
404     napi_value argv = { nullptr };
405     napi_value thisVar = nullptr;
406     void *data = nullptr;
407     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data));
408     if (CheckParamNumber(argc, ARGS_SIZE_ONE) != napi_ok) {
409         ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
410         return result;
411     }
412     if (CheckParamType(env, argv, napi_function) != napi_ok) {
413         ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
414         return result;
415     }
416     napi_ref callbackRef = nullptr;
417     napi_create_reference(env, argv, ARGS_SIZE_ONE, &callbackRef);
418     EventListener eventListener{ .env = env, .thisVar = thisVar, .callbackRef = callbackRef };
419     sptr<ScreenLockSystemAbilityInterface> listener = new (std::nothrow) ScreenlockSystemAbilityCallback(eventListener);
420     if (listener != nullptr) {
421         int32_t retCode = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener);
422         if (retCode != E_SCREENLOCK_OK) {
423             ErrorInfo errInfo(static_cast<uint32_t>(retCode));
424             GetErrorInfo(retCode, errInfo);
425             ThrowError(env, errInfo.errorCode_, errInfo.message_);
426             status = false;
427         } else {
428             status = true;
429         }
430     }
431     SCLOCK_HILOGD("on system event  status=%{public}d", status);
432     napi_get_boolean(env, status, &result);
433     return result;
434 }
435 
NAPI_ScreenLockSendEvent(napi_env env,napi_callback_info info)436 napi_value NAPI_ScreenLockSendEvent(napi_env env, napi_callback_info info)
437 {
438     SCLOCK_HILOGD("NAPI_ScreenLockSendEvent begin");
439     SendEventInfo *context = new SendEventInfo();
440     auto input = [context](napi_env env, size_t argc, napi_value argv[], napi_value self) -> napi_status {
441         SCLOCK_HILOGD("input ---- argc : %{public}zu", argc);
442         if (CheckParamNumber(argc, ARGS_SIZE_TWO) != napi_ok) {
443             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
444             return napi_invalid_arg;
445         }
446         if (CheckParamType(env, argv[ARGV_ZERO], napi_string) != napi_ok) {
447             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
448             return napi_invalid_arg;
449         }
450         char event[MAX_VALUE_LEN] = { 0 };
451         size_t len;
452         napi_get_value_string_utf8(env, argv[ARGV_ZERO], event, MAX_VALUE_LEN, &len);
453         context->eventInfo = event;
454         std::string type = event;
455         if (IsVaildEvent(type) != napi_ok) {
456             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
457             return napi_invalid_arg;
458         }
459         if (CheckParamType(env, argv[ARGV_ONE], napi_number) != napi_ok) {
460             ThrowError(env, JsErrorCode::ERR_INVALID_PARAMS, PARAMETER_VALIDATION_FAILED);
461             return napi_invalid_arg;
462         }
463         napi_get_value_int32(env, argv[ARGV_ONE], &context->param);
464         return napi_ok;
465     };
466     auto output = [context](napi_env env, napi_value *result) -> napi_status {
467         napi_status status = napi_get_boolean(env, context->allowed, result);
468         SCLOCK_HILOGD("output ---- napi_get_boolean[%{public}d]", status);
469         return napi_ok;
470     };
471     auto exec = [context](AsyncCall::Context *ctx) {
472         int32_t retCode = ScreenLockAppManager::GetInstance()->SendScreenLockEvent(context->eventInfo, context->param);
473         if (retCode != E_SCREENLOCK_OK) {
474             ErrorInfo errInfo(static_cast<uint32_t>(retCode));
475             GetErrorInfo(retCode, errInfo);
476             context->SetErrorInfo(errInfo);
477             context->allowed = false;
478         } else {
479             context->SetStatus(napi_ok);
480             context->allowed = true;
481         }
482     };
483     context->SetAction(std::move(input), std::move(output));
484     AsyncCall asyncCall(env, info, context, ARGV_TWO);
485     return asyncCall.Call(env, exec);
486 }
487 
ScreenlockInit(napi_env env,napi_value exports)488 static napi_value ScreenlockInit(napi_env env, napi_value exports)
489 {
490     napi_status ret = Init(env, exports);
491     if (ret != napi_ok) {
492         SCLOCK_HILOGE("ModuleInit failed!");
493     }
494     return exports;
495 }
496 
RegisterModule(void)497 extern "C" __attribute__((constructor)) void RegisterModule(void)
498 {
499     napi_module module = {.nm_version = 1, // NAPI v1
500         .nm_flags = 0,                     // normal
501         .nm_filename = nullptr,
502         .nm_register_func = ScreenlockInit,
503         .nm_modname = "screenLock",
504         .nm_priv = nullptr,
505         .reserved = {}};
506     napi_module_register(&module);
507 }
508 } // namespace ScreenLock
509 } // namespace OHOS
510