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 
71     if (screenLockSuccess || callBackPtr->action == Action::UNLOCK) {
72         napi_get_null(callBackPtr->env, &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
73     } else {
74         AsyncCall::GenerateBusinessError(callBackPtr->env, callBackPtr->errorInfo,
75             &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
76     }
77 
78     if (callBackPtr->deferred) {
79         if (screenLockSuccess || callBackPtr->action == Action::UNLOCK) {
80             napi_resolve_deferred(callBackPtr->env, callBackPtr->deferred,
81                 result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
82         } else {
83             napi_reject_deferred(callBackPtr->env, callBackPtr->deferred,
84                 result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
85         }
86     } else {
87         napi_value callbackFunc = nullptr;
88         napi_value callbackResult = nullptr;
89         napi_get_reference_value(callBackPtr->env, callBackPtr->callbackRef, &callbackFunc);
90         napi_call_function(callBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_TWO, result, &callbackResult);
91         napi_delete_reference(callBackPtr->env, callBackPtr->callbackRef);
92     }
93     napi_close_handle_scope(callBackPtr->env, scope);
94     SAFE_DELETE(callBackPtr);
95     SAFE_DELETE(work);
96     SCLOCK_HILOGI("UvWorkOnCallBack end");
97 }
98 
OnCallBack(const int32_t screenLockResult)99 void ScreenlockCallback::OnCallBack(const int32_t screenLockResult)
100 {
101     ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
102     if (screenlockOnCallBack == nullptr) {
103         SCLOCK_HILOGE("new  ScreenlockOnCallBack failed");
104         return;
105     }
106     if (screenLockResult == SCREEN_CANCEL) {
107         errorInfo_.message_ = CANCEL_UNLOCK_OPERATION;
108     } else if (screenLockResult == SCREEN_FAIL) {
109         errorInfo_.message_ = SCREENLOCK_FAIL;
110     }
111     screenlockOnCallBack->env = eventListener_.env;
112     screenlockOnCallBack->callbackRef = eventListener_.callbackRef;
113     screenlockOnCallBack->deferred = eventListener_.deferred;
114     screenlockOnCallBack->action = eventListener_.action;
115     screenlockOnCallBack->errorInfo = errorInfo_;
116     screenlockOnCallBack->screenLockResult = screenLockResult;
117     bool bRet = UvQueue::Call(eventListener_.env, screenlockOnCallBack, UvWorkOnCallBack);
118     if (!bRet) {
119         SCLOCK_HILOGE("ScreenlockCallback::OnCallBack g_screenLockCallback failed");
120     }
121 }
122 } // namespace ScreenLock
123 } // namespace OHOS
124