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 "screenlock_callback.h"
16
17 #include <cstdint>
18 #include <new>
19 #include <string>
20
21 #include "async_call.h"
22 #include "js_native_api.h"
23 #include "js_native_api_types.h"
24 #include "node_api.h"
25 #include "sclock_log.h"
26 #include "screenlock_common.h"
27 #include "string_ex.h"
28 #include "uv_queue.h"
29
30 namespace OHOS {
31 namespace ScreenLock {
32 enum class ARG_INFO { ARG_ERROR, ARG_DATA, ARG_BUTT };
33 constexpr const char *CANCEL_UNLOCK_OPERATION = "The user canceled the unlock operation.";
34 constexpr const char *SCREENLOCK_FAIL = "ScreenLock failed.";
ScreenlockCallback(const EventListener & eventListener)35 ScreenlockCallback::ScreenlockCallback(const EventListener &eventListener)
36 {
37 eventListener_ = eventListener;
38 }
39
~ScreenlockCallback()40 ScreenlockCallback::~ScreenlockCallback()
41 {
42 }
43
SetErrorInfo(const ErrorInfo & errorInfo)44 void ScreenlockCallback::SetErrorInfo(const ErrorInfo &errorInfo)
45 {
46 errorInfo_ = errorInfo;
47 }
48
UvWorkOnCallBack(uv_work_t * work,int32_t status)49 void ScreenlockCallback::UvWorkOnCallBack(uv_work_t *work, int32_t status)
50 {
51 if (work == nullptr) {
52 SCLOCK_HILOGE("UvWorkOnCallBack, work is null");
53 return;
54 }
55 ScreenlockOnCallBack *callBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
56 if (callBackPtr == nullptr) {
57 SCLOCK_HILOGE("UvWorkOnCallBack, callBackPtr is null");
58 SAFE_DELETE(work);
59 return;
60 }
61 napi_handle_scope scope = nullptr;
62 napi_open_handle_scope(callBackPtr->env, &scope);
63 napi_value result[ARGS_SIZE_TWO] = { 0 };
64 bool screenLockSuccess = callBackPtr->screenLockResult == SCREEN_SUCC;
65 if (callBackPtr->action == Action::UNLOCKSCREEN) {
66 napi_get_undefined(callBackPtr->env, &result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
67 } else {
68 napi_get_boolean(callBackPtr->env, screenLockSuccess, &result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
69 }
70 if (screenLockSuccess) {
71 napi_get_null(callBackPtr->env, &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
72 } else {
73 AsyncCall::GenerateBusinessError(callBackPtr->env, callBackPtr->errorInfo,
74 &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
75 }
76 if (callBackPtr->deferred) {
77 if (screenLockSuccess) {
78 napi_resolve_deferred(callBackPtr->env, callBackPtr->deferred,
79 result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
80 } else {
81 napi_reject_deferred(callBackPtr->env, callBackPtr->deferred,
82 result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
83 }
84 } else {
85 napi_value callbackFunc = nullptr;
86 napi_value callbackResult = nullptr;
87 napi_get_reference_value(callBackPtr->env, callBackPtr->callbackRef, &callbackFunc);
88 napi_call_function(callBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_TWO, result, &callbackResult);
89 napi_delete_reference(callBackPtr->env, callBackPtr->callbackRef);
90 }
91 napi_close_handle_scope(callBackPtr->env, scope);
92 SAFE_DELETE(callBackPtr);
93 SAFE_DELETE(work);
94 }
95
OnCallBack(const int32_t screenLockResult)96 void ScreenlockCallback::OnCallBack(const int32_t screenLockResult)
97 {
98 ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
99 if (screenlockOnCallBack == nullptr) {
100 SCLOCK_HILOGE("new ScreenlockOnCallBack failed");
101 return;
102 }
103 if (screenLockResult == SCREEN_CANCEL) {
104 errorInfo_.message_ = CANCEL_UNLOCK_OPERATION;
105 } else if (screenLockResult == SCREEN_FAIL) {
106 errorInfo_.message_ = SCREENLOCK_FAIL;
107 }
108 screenlockOnCallBack->env = eventListener_.env;
109 screenlockOnCallBack->callbackRef = eventListener_.callbackRef;
110 screenlockOnCallBack->deferred = eventListener_.deferred;
111 screenlockOnCallBack->action = eventListener_.action;
112 screenlockOnCallBack->errorInfo = errorInfo_;
113 screenlockOnCallBack->screenLockResult = screenLockResult;
114 bool bRet = UvQueue::Call(eventListener_.env, screenlockOnCallBack, UvWorkOnCallBack);
115 if (!bRet) {
116 SCLOCK_HILOGE("ScreenlockCallback::OnCallBack g_screenLockCallback failed");
117 }
118 }
119 } // namespace ScreenLock
120 } // namespace OHOS
121