• 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_system_ability_callback.h"
16 
17 #include <uv.h>
18 
19 #include "sclock_log.h"
20 #include "screenlock_common.h"
21 
22 namespace OHOS {
23 namespace ScreenLock {
ScreenlockSystemAbilityCallback(int32_t eventType,std::list<EventListener> & listenerList)24 ScreenlockSystemAbilityCallback::ScreenlockSystemAbilityCallback(
25     int32_t eventType, std::list<EventListener> &listenerList) : eventType_(eventType)
26 {
27     listenerList_ = &listenerList;
28     SCLOCK_HILOGD("eventType_----> %{public}d", eventType_);
29 }
30 
~ScreenlockSystemAbilityCallback()31 ScreenlockSystemAbilityCallback::~ScreenlockSystemAbilityCallback()
32 {
33 }
34 
GetEventType(const std::string & type)35 int32_t ScreenlockSystemAbilityCallback::GetEventType(const std::string &type)
36 {
37     if (MatchEventType(type, BEGIN_WAKEUP)) {
38         return static_cast<uint32_t>(SCREENLOCK_BEGIN_WAKEUP);
39     }
40     if (MatchEventType(type, END_WAKEUP)) {
41         return static_cast<uint32_t>(SCREENLOCK_END_WAKEUP);
42     }
43     if (MatchEventType(type, BEGIN_SCREEN_ON)) {
44         return static_cast<uint32_t>(SCREENLOCK_BEGIN_SCREEN_ON);
45     }
46     if (MatchEventType(type, END_SCREEN_ON)) {
47         return static_cast<uint32_t>(SCREENLOCK_END_SCREEN_ON);
48     }
49     if (MatchEventType(type, BEGIN_SCREEN_OFF)) {
50         return static_cast<uint32_t>(SCREENLOCK_BEGIN_SCREEN_OFF);
51     }
52     if (MatchEventType(type, END_SCREEN_OFF)) {
53         return static_cast<uint32_t>(SCREENLOCK_END_SCREEN_OFF);
54     }
55     if (MatchEventType(type, EXIT_ANIMATION)) {
56         return static_cast<uint32_t>(SCREENLOCK_EXIT_ANIMATION);
57     }
58     if (MatchEventType(type, UNLOCKSCREEN)) {
59         return static_cast<uint32_t>(SCREENLOCK_UNLOCK_SCREEN);
60     }
61     if (MatchEventType(type, BEGIN_SLEEP)) {
62         return static_cast<uint32_t>(SCREENLOCK_BEGIN_SLEEP);
63     }
64     if (MatchEventType(type, END_SLEEP)) {
65         return static_cast<uint32_t>(SCREENLOCK_END_SLEEP);
66     }
67     if (MatchEventType(type, CHANGE_USER)) {
68         return static_cast<uint32_t>(SCREENLOCK_CHANGE_USER);
69     }
70     if (MatchEventType(type, SCREENLOCK_ENABLED)) {
71         return static_cast<uint32_t>(SCREENLOCK_SCREENLOCK_ENABLED);
72     }
73     if (MatchEventType(type, SYSTEM_READY)) {
74         return static_cast<uint32_t>(SCREENLOCK_SYSTEM_READY);
75     }
76     return static_cast<uint32_t>(NONE_EVENT_TYPE);
77 }
78 
MatchEventType(const std::string & type,const std::string & goalTypeStr)79 bool ScreenlockSystemAbilityCallback::MatchEventType(const std::string &type, const std::string &goalTypeStr)
80 {
81     return goalTypeStr.compare(type) == 0;
82 }
83 
__anonff50c7710102(uv_work_t *work, int status) 84 auto OnUvWorkBoolCallback = [](uv_work_t *work, int status) {
85     SCLOCK_HILOGD("OnUvWorkBoolCallback status = %{public}d", status);
86     ScreenlockOnCallBack *screenlockOnCallBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
87     if (screenlockOnCallBackPtr == nullptr) {
88         delete work;
89         return;
90     }
91     napi_value undefined = 0;
92     napi_get_undefined(screenlockOnCallBackPtr->env, &undefined);
93     napi_value callbackFunc = nullptr;
94     napi_get_reference_value(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->callbackref, &callbackFunc);
95     napi_value callbackResult = nullptr;
96     napi_value callbackValues[2] = {0};
97     napi_get_undefined(screenlockOnCallBackPtr->env, &callbackValues[0]);
98     napi_get_boolean(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->boolCallBackValue, &callbackValues[1]);
99     napi_call_function(
100         screenlockOnCallBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_TWO, callbackValues, &callbackResult);
101     if (screenlockOnCallBackPtr != nullptr) {
102         delete screenlockOnCallBackPtr;
103         screenlockOnCallBackPtr = nullptr;
104     }
105     if (work != nullptr) {
106         delete work;
107         work = nullptr;
108     }
109 };
110 
OnCallBack(const std::string & event,bool result)111 void ScreenlockSystemAbilityCallback::OnCallBack(const std::string &event, bool result)
112 {
113     SCLOCK_HILOGD("ONCALLBACK_BOOL event---->%{public}s,result---->%{public}d", event.c_str(), result);
114     for (auto iter = listenerList_->begin(); iter != listenerList_->end(); iter++) {
115         if (iter->eventType == GetEventType(event)) {
116             uv_loop_s *loop = nullptr;
117             napi_get_uv_event_loop(iter->env, &loop);
118             if (loop == nullptr) {
119                 return;
120             }
121             uv_work_t *work = std::make_unique<uv_work_t>().release();
122             if (work == nullptr) {
123                 return;
124             }
125             ScreenlockOnCallBack *screenlockOnCallBack = std::make_unique<ScreenlockOnCallBack>().release();
126             if (screenlockOnCallBack == nullptr) {
127                 delete work;
128                 work = nullptr;
129                 return;
130             }
131             screenlockOnCallBack->env = iter->env;
132             screenlockOnCallBack->callbackref = iter->callbackRef;
133             screenlockOnCallBack->boolCallBackValue = result;
134             screenlockOnCallBack->thisVar = iter->thisVar;
135             work->data = (void *)screenlockOnCallBack;
136             int ret = uv_queue_work(
137                 loop, work, [](uv_work_t *work) {}, OnUvWorkBoolCallback);
138             if (ret != 0) {
139                 delete screenlockOnCallBack;
140                 screenlockOnCallBack = nullptr;
141                 delete work;
142                 work = nullptr;
143             }
144         }
145     }
146 }
147 
__anonff50c7710302(uv_work_t *work, int status) 148 auto OnVoidUvWorkCallback = [](uv_work_t *work, int status) {
149     SCLOCK_HILOGD("OnVoidUvWorkCallback status = %{public}d", status);
150     ScreenlockOnCallBack *screenlockOnCallBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
151     if (screenlockOnCallBackPtr == nullptr) {
152         delete work;
153         return;
154     }
155     napi_value undefined = 0;
156     napi_get_undefined(screenlockOnCallBackPtr->env, &undefined);
157     napi_value callbackFunc = nullptr;
158     napi_get_reference_value(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->callbackref, &callbackFunc);
159     napi_value callbackResult = nullptr;
160     napi_value callbackValues[1] = {0};
161     napi_get_undefined(screenlockOnCallBackPtr->env, &callbackValues[0]);
162     napi_call_function(
163         screenlockOnCallBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_ONE, callbackValues, &callbackResult);
164     if (screenlockOnCallBackPtr != nullptr) {
165         delete screenlockOnCallBackPtr;
166         screenlockOnCallBackPtr = nullptr;
167     }
168     if (work != nullptr) {
169         delete work;
170         work = nullptr;
171     }
172 };
173 
OnCallBack(const std::string & event)174 void ScreenlockSystemAbilityCallback::OnCallBack(const std::string &event)
175 {
176     SCLOCK_HILOGD("ScreenlockSystemAbilityCallback  ONCALLBACK_VOID event---->%{public}s", event.c_str());
177     for (auto iter = listenerList_->begin(); iter != listenerList_->end(); iter++) {
178         if (iter->eventType == GetEventType(event)) {
179             uv_loop_s *loop = nullptr;
180             napi_get_uv_event_loop(iter->env, &loop);
181             if (loop == nullptr) {
182                 return;
183             }
184             uv_work_t *work = new (std::nothrow) uv_work_t;
185             if (work == nullptr) {
186                 return;
187             }
188             ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
189             if (screenlockOnCallBack == nullptr) {
190                 delete work;
191                 work = nullptr;
192                 return;
193             }
194             screenlockOnCallBack->env = iter->env;
195             screenlockOnCallBack->callbackref = iter->callbackRef;
196             screenlockOnCallBack->thisVar = iter->thisVar;
197             work->data = (void *)screenlockOnCallBack;
198             int ret = uv_queue_work(
199                 loop, work, [](uv_work_t *work) {}, OnVoidUvWorkCallback);
200             if (ret != 0) {
201                 delete screenlockOnCallBack;
202                 screenlockOnCallBack = nullptr;
203                 delete work;
204                 work = nullptr;
205             }
206         }
207     }
208 }
209 
__anonff50c7710502(uv_work_t *work, int status) 210 auto OnIntUvWorkCallback = [](uv_work_t *work, int status) {
211     SCLOCK_HILOGD("OnIntUvWorkCallback status = %{public}d", status);
212     ScreenlockOnCallBack *screenlockOnCallBackPtr = static_cast<ScreenlockOnCallBack *>(work->data);
213     if (screenlockOnCallBackPtr == nullptr) {
214         delete work;
215         return;
216     }
217     napi_value undefined = 0;
218     napi_get_undefined(screenlockOnCallBackPtr->env, &undefined);
219     napi_value callbackFunc = nullptr;
220     napi_get_reference_value(screenlockOnCallBackPtr->env, screenlockOnCallBackPtr->callbackref, &callbackFunc);
221     napi_value callbackResult = nullptr;
222     napi_value callbackValues[2] = {0};
223     napi_get_undefined(screenlockOnCallBackPtr->env, &callbackValues[0]);
224     napi_create_int32(screenlockOnCallBackPtr->env, static_cast<int32_t>(screenlockOnCallBackPtr->intCallbackValue),
225         &callbackValues[1]);
226     napi_call_function(
227         screenlockOnCallBackPtr->env, nullptr, callbackFunc, ARGS_SIZE_TWO, callbackValues, &callbackResult);
228     if (screenlockOnCallBackPtr != nullptr) {
229         delete screenlockOnCallBackPtr;
230         screenlockOnCallBackPtr = nullptr;
231     }
232     if (work != nullptr) {
233         delete work;
234         work = nullptr;
235     }
236 };
237 
OnCallBack(const std::string & event,int result)238 void ScreenlockSystemAbilityCallback::OnCallBack(const std::string &event, int result)
239 {
240     SCLOCK_HILOGD("ONCALLBACK_INT event---->%{public}s,result---->%{public}d", event.c_str(), result);
241     for (auto iter = listenerList_->begin(); iter != listenerList_->end(); iter++) {
242         if (iter->eventType == GetEventType(event)) {
243             uv_loop_s *loop = nullptr;
244             napi_get_uv_event_loop(iter->env, &loop);
245             if (loop == nullptr) {
246                 return;
247             }
248             uv_work_t *work = new (std::nothrow) uv_work_t;
249             if (work == nullptr) {
250                 return;
251             }
252             ScreenlockOnCallBack *screenlockOnCallBack = new (std::nothrow) ScreenlockOnCallBack;
253             if (screenlockOnCallBack == nullptr) {
254                 delete work;
255                 work = nullptr;
256                 return;
257             }
258             screenlockOnCallBack->env = iter->env;
259             screenlockOnCallBack->callbackref = iter->callbackRef;
260             screenlockOnCallBack->intCallbackValue = result;
261             screenlockOnCallBack->thisVar = iter->thisVar;
262             work->data = (void *)screenlockOnCallBack;
263             int ret = uv_queue_work(
264                 loop, work, [](uv_work_t *work) {}, OnIntUvWorkCallback);
265             if (ret != 0) {
266                 delete screenlockOnCallBack;
267                 screenlockOnCallBack = nullptr;
268                 delete work;
269                 work = nullptr;
270             }
271         }
272     }
273 }
274 } // namespace ScreenLock
275 } // namespace OHOS
276