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