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