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_system_ability_callback.h"
16
17 #include <memory>
18 #include <new>
19
20 #include "js_native_api.h"
21 #include "js_native_api_types.h"
22 #include "node_api.h"
23 #include "sclock_log.h"
24 #include "screenlock_common.h"
25 #include "uv_queue.h"
26
27 namespace OHOS {
28 namespace ScreenLock {
ScreenlockSystemAbilityCallback(const EventListener & eventListener)29 ScreenlockSystemAbilityCallback::ScreenlockSystemAbilityCallback(const EventListener &eventListener)
30 : eventListener_(eventListener)
31 {
32 }
33
~ScreenlockSystemAbilityCallback()34 ScreenlockSystemAbilityCallback::~ScreenlockSystemAbilityCallback()
35 {
36 }
37
__anon48a012b30102(uv_work_t *work, int status) 38 auto g_onUvWorkCallback = [](uv_work_t *work, int status) {
39 SCLOCK_HILOGD("g_onUvWorkCallback status = %{public}d", status);
40 if (work == nullptr) {
41 return;
42 }
43 ScreenlockOnCallBack *screenlockOnCallBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
44 if (screenlockOnCallBackPtr == nullptr) {
45 delete work;
46 work = nullptr;
47 return;
48 }
49 napi_handle_scope scope = nullptr;
50 napi_open_handle_scope(screenlockOnCallBackPtr->env, &scope);
51 napi_value undefined = nullptr;
52 napi_get_undefined(screenlockOnCallBackPtr->env, &undefined);
53 napi_value callbackFunc = nullptr;
54 napi_get_reference_value(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->callbackRef, &callbackFunc);
55 napi_value callbackResult = nullptr;
56 napi_value callbackValue = nullptr;
57
58 napi_value result = nullptr;
59 napi_create_object(screenlockOnCallBackPtr->env, &result);
60 napi_value eventType = nullptr;
61 napi_value params = nullptr;
62 napi_create_string_utf8(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->systemEvent.eventType_.c_str(),
63 NAPI_AUTO_LENGTH, &eventType);
64 napi_create_string_utf8(
65 screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->systemEvent.params_.c_str(), NAPI_AUTO_LENGTH, ¶ms);
66 napi_set_named_property(screenlockOnCallBackPtr->env, result, "eventType", eventType);
67 napi_set_named_property(screenlockOnCallBackPtr->env, result, "params", params);
68 callbackValue = result;
69 napi_call_function(
70 screenlockOnCallBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_ONE, &callbackValue, &callbackResult);
71 napi_close_handle_scope(screenlockOnCallBackPtr->env, scope);
72 if (screenlockOnCallBackPtr != nullptr) {
73 delete screenlockOnCallBackPtr;
74 screenlockOnCallBackPtr = nullptr;
75 }
76 if (work != nullptr) {
77 delete work;
78 work = nullptr;
79 }
80 };
81
OnCallBack(const SystemEvent & systemEvent)82 void ScreenlockSystemAbilityCallback::OnCallBack(const SystemEvent &systemEvent)
83 {
84 SCLOCK_HILOGD("ScreenlockSystemAbilityCallback ONCALLBACK");
85 ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
86 if (screenlockOnCallBack == nullptr) {
87 return;
88 }
89 screenlockOnCallBack->env = eventListener_.env;
90 screenlockOnCallBack->callbackRef = eventListener_.callbackRef;
91 screenlockOnCallBack->thisVar = eventListener_.thisVar;
92 screenlockOnCallBack->systemEvent = systemEvent;
93 bool bRet = UvQueue::Call(eventListener_.env, screenlockOnCallBack, g_onUvWorkCallback);
94 if (!bRet) {
95 SCLOCK_HILOGE("ScreenlockCallback::OnCallBack failed, event=%{public}s,result=%{public}s",
96 systemEvent.eventType_.c_str(), systemEvent.params_.c_str());
97 }
98 }
99 } // namespace ScreenLock
100 } // namespace OHOS
101