• 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 "screenlock_callback.h"
16 
17 #include <cstdint>
18 #include <new>
19 #include <string>
20 
21 #include "js_native_api.h"
22 #include "js_native_api_types.h"
23 #include "napi_screenlock_ability.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 };
ScreenlockCallback(const EventListener & eventListener)33 ScreenlockCallback::ScreenlockCallback(const EventListener &eventListener)
34 {
35     eventListener_ = eventListener;
36 }
37 
~ScreenlockCallback()38 ScreenlockCallback::~ScreenlockCallback()
39 {
40 }
41 
SetErrorInfo(const ErrorInfo & errorInfo)42 void ScreenlockCallback::SetErrorInfo(const ErrorInfo &errorInfo)
43 {
44     errorInfo_ = errorInfo;
45 }
46 
UvWorkOnCallBackInt(uv_work_t * work,int status)47 void UvWorkOnCallBackInt(uv_work_t *work, int status)
48 {
49     if (work == nullptr) {
50         SCLOCK_HILOGE("UvWorkNotifyMissionChanged, work is null");
51         return;
52     }
53     ScreenlockOnCallBack *callBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
54     if (callBackPtr == nullptr) {
55         SCLOCK_HILOGE("UvWorkOnCallBackInt, callBackPtr is null");
56         SAFE_DELETE(work);
57         return;
58     }
59     int32_t onCallbackResult = -1;
60     if (!StrToInt(callBackPtr->systemEvent.params_, onCallbackResult)) {
61         SAFE_DELETE(callBackPtr);
62         SAFE_DELETE(work);
63         return;
64     }
65     napi_handle_scope scope = nullptr;
66     napi_open_handle_scope(callBackPtr->env, &scope);
67     napi_value result[ARGS_SIZE_TWO] = { 0 };
68     napi_get_undefined(callBackPtr->env, &result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
69     if (onCallbackResult == SCREEN_SUCC) {
70         napi_get_undefined(callBackPtr->env, &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
71         if (callBackPtr->callBackResult) {
72             napi_get_boolean(callBackPtr->env, true, &result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
73         }
74     } else {
75         AsyncCall::GenerateBusinessError(
76             callBackPtr->env, callBackPtr->errorInfo, &result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
77         if (callBackPtr->callBackResult) {
78             napi_get_boolean(callBackPtr->env, false, &result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
79         }
80     }
81     if (callBackPtr->deferred) {
82         if (onCallbackResult == SCREEN_SUCC) {
83             napi_resolve_deferred(
84                 callBackPtr->env, callBackPtr->deferred, result[static_cast<int32_t>(ARG_INFO::ARG_DATA)]);
85         } else {
86             napi_reject_deferred(
87                 callBackPtr->env, callBackPtr->deferred, result[static_cast<int32_t>(ARG_INFO::ARG_ERROR)]);
88         }
89     } else {
90         napi_value callbackFunc = nullptr;
91         napi_value callbackResult = nullptr;
92         napi_get_reference_value(callBackPtr->env, callBackPtr->callbackref, &callbackFunc);
93         napi_call_function(callBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_TWO, result, &callbackResult);
94         napi_delete_reference(callBackPtr->env, callBackPtr->callbackref);
95     }
96     napi_close_handle_scope(callBackPtr->env, scope);
97     SAFE_DELETE(callBackPtr);
98     SAFE_DELETE(work);
99 }
100 
OnCallBack(const SystemEvent & systemEvent)101 void ScreenlockCallback::OnCallBack(const SystemEvent &systemEvent)
102 {
103     ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
104     if (screenlockOnCallBack == nullptr) {
105         SCLOCK_HILOGE("new  ScreenlockOnCallBack failed");
106         return;
107     }
108     screenlockOnCallBack->env = eventListener_.env;
109     screenlockOnCallBack->callbackref = eventListener_.callbackRef;
110     screenlockOnCallBack->systemEvent = systemEvent;
111     screenlockOnCallBack->deferred = eventListener_.deferred;
112     screenlockOnCallBack->errorInfo = errorInfo_;
113     screenlockOnCallBack->callBackResult = eventListener_.callBackResult;
114     bool bRet = UvQueue::Call(eventListener_.env, static_cast<void *>(screenlockOnCallBack), UvWorkOnCallBackInt);
115     if (!bRet) {
116         SCLOCK_HILOGE("ScreenlockCallback::OnCallBack failed, event=%{public}s,result=%{public}s",
117             systemEvent.eventType_.c_str(), systemEvent.params_.c_str());
118     }
119 }
120 } // namespace ScreenLock
121 } // namespace OHOS
122