1 /*
2 * Copyright (C) 2021-2024 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
16 #include "event_listener_handler.h"
17
18 #include <cinttypes>
19
20 #include "event_listener_manager.h"
21 #include "inner_event.h"
22 #include "napi_parameter_util.h"
23 #include "napi_radio_types.h"
24 #include "napi_state_registry.h"
25 #include "napi_telephony_observer.h"
26 #include "napi_util.h"
27 #include "singleton.h"
28 #include "telephony_errors.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_state_manager.h"
31 #include "update_contexts.h"
32
33 namespace OHOS {
34 namespace Telephony {
35 namespace {
WrapRegState(int32_t nativeState)36 int32_t WrapRegState(int32_t nativeState)
37 {
38 RegServiceState state = static_cast<RegServiceState>(nativeState);
39 switch (state) {
40 case RegServiceState::REG_STATE_NO_SERVICE:
41 case RegServiceState::REG_STATE_SEARCH: {
42 return RegStatus::REGISTRATION_STATE_NO_SERVICE;
43 }
44 case RegServiceState::REG_STATE_IN_SERVICE: {
45 return RegStatus::REGISTRATION_STATE_IN_SERVICE;
46 }
47 case RegServiceState::REG_STATE_EMERGENCY_ONLY: {
48 return RegStatus::REGISTRATION_STATE_EMERGENCY_CALL_ONLY;
49 }
50 case RegServiceState::REG_STATE_UNKNOWN: {
51 return RegStatus::REGISTRATION_STATE_POWER_OFF;
52 }
53 default:
54 return RegStatus::REGISTRATION_STATE_POWER_OFF;
55 }
56 }
57
WrapCallState(int32_t callState)58 int32_t WrapCallState(int32_t callState)
59 {
60 switch (callState) {
61 case (int32_t)Telephony::CallStatus::CALL_STATUS_ACTIVE:
62 case (int32_t)Telephony::CallStatus::CALL_STATUS_HOLDING:
63 case (int32_t)Telephony::CallStatus::CALL_STATUS_DIALING:
64 case (int32_t)Telephony::CallStatus::CALL_STATUS_ALERTING:
65 return static_cast<int32_t>(CallState::CALL_STATE_OFFHOOK);
66 case (int32_t)Telephony::CallStatus::CALL_STATUS_WAITING:
67 case (int32_t)Telephony::CallStatus::CALL_STATUS_INCOMING:
68 return static_cast<int32_t>(CallState::CALL_STATE_RINGING);
69 case (int32_t)Telephony::CallStatus::CALL_STATUS_DISCONNECTING:
70 case (int32_t)Telephony::CallStatus::CALL_STATUS_DISCONNECTED:
71 case (int32_t)Telephony::CallStatus::CALL_STATUS_IDLE:
72 return static_cast<int32_t>(CallState::CALL_STATE_IDLE);
73 case (int32_t)Telephony::CallStatus::CALL_STATUS_ANSWERED:
74 return static_cast<int32_t>(CallState::CALL_STATE_ANSWERED);
75 default:
76 return static_cast<int32_t>(CallState::CALL_STATE_UNKNOWN);
77 }
78 }
79
WrapNetworkType(SignalInformation::NetworkType nativeNetworkType)80 int32_t WrapNetworkType(SignalInformation::NetworkType nativeNetworkType)
81 {
82 NetworkType jsNetworkType = NetworkType::NETWORK_TYPE_UNKNOWN;
83 switch (nativeNetworkType) {
84 case SignalInformation::NetworkType::GSM: {
85 jsNetworkType = NetworkType::NETWORK_TYPE_GSM;
86 break;
87 }
88 case SignalInformation::NetworkType::CDMA: {
89 jsNetworkType = NetworkType::NETWORK_TYPE_CDMA;
90 break;
91 }
92 case SignalInformation::NetworkType::LTE: {
93 jsNetworkType = NetworkType::NETWORK_TYPE_LTE;
94 break;
95 }
96 case SignalInformation::NetworkType::TDSCDMA: {
97 jsNetworkType = NetworkType::NETWORK_TYPE_TDSCDMA;
98 break;
99 }
100 case SignalInformation::NetworkType::WCDMA: {
101 jsNetworkType = NetworkType::NETWORK_TYPE_WCDMA;
102 break;
103 }
104 default: {
105 jsNetworkType = NetworkType::NETWORK_TYPE_UNKNOWN;
106 }
107 }
108 return static_cast<int32_t>(jsNetworkType);
109 }
110
WrapRadioTech(int32_t radioTechType)111 int32_t WrapRadioTech(int32_t radioTechType)
112 {
113 RadioTech techType = static_cast<RadioTech>(radioTechType);
114 switch (techType) {
115 case RadioTech::RADIO_TECHNOLOGY_GSM:
116 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_GSM);
117 case RadioTech::RADIO_TECHNOLOGY_LTE:
118 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_LTE);
119 case RadioTech::RADIO_TECHNOLOGY_WCDMA:
120 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_WCDMA);
121 case RadioTech::RADIO_TECHNOLOGY_1XRTT:
122 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_1XRTT);
123 case RadioTech::RADIO_TECHNOLOGY_HSPA:
124 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_HSPA);
125 case RadioTech::RADIO_TECHNOLOGY_HSPAP:
126 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_HSPAP);
127 case RadioTech::RADIO_TECHNOLOGY_TD_SCDMA:
128 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_TD_SCDMA);
129 case RadioTech::RADIO_TECHNOLOGY_EVDO:
130 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_EVDO);
131 case RadioTech::RADIO_TECHNOLOGY_EHRPD:
132 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_EHRPD);
133 case RadioTech::RADIO_TECHNOLOGY_LTE_CA:
134 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_LTE_CA);
135 case RadioTech::RADIO_TECHNOLOGY_IWLAN:
136 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_IWLAN);
137 case RadioTech::RADIO_TECHNOLOGY_NR:
138 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_NR);
139 default:
140 return static_cast<int32_t>(RatType::RADIO_TECHNOLOGY_UNKNOWN);
141 }
142 }
143
NapiReturnToJS(napi_env env,napi_ref callbackRef,napi_value callbackVal,std::unique_lock<std::mutex> & lock)144 napi_status NapiReturnToJS(
145 napi_env env, napi_ref callbackRef, napi_value callbackVal, std::unique_lock<std::mutex> &lock)
146 {
147 napi_handle_scope scope = nullptr;
148 napi_open_handle_scope(env, &scope);
149 if (scope == nullptr) {
150 TELEPHONY_LOGE("scope is nullptr");
151 napi_close_handle_scope(env, scope);
152 return napi_ok;
153 }
154 if (callbackRef == nullptr) {
155 TELEPHONY_LOGE("NapiReturnToJS callbackRef is nullptr");
156 napi_close_handle_scope(env, scope);
157 return napi_ok;
158 }
159 napi_value callbackFunc = nullptr;
160 napi_get_reference_value(env, callbackRef, &callbackFunc);
161 lock.unlock();
162 napi_value callbackValues[] = { callbackVal };
163 napi_value recv = nullptr;
164 napi_get_undefined(env, &recv);
165 napi_value callbackResult = nullptr;
166 napi_status status =
167 napi_call_function(env, recv, callbackFunc, std::size(callbackValues), callbackValues, &callbackResult);
168 if (status != napi_ok) {
169 TELEPHONY_LOGE("NapiReturnToJS napi_call_function return error : %{public}d", status);
170 }
171 napi_close_handle_scope(env, scope);
172 return status;
173 }
174
SignalInfoConversion(napi_env env,int32_t type,int32_t level,int32_t signalIntensity)175 napi_value SignalInfoConversion(napi_env env, int32_t type, int32_t level, int32_t signalIntensity)
176 {
177 napi_value val = nullptr;
178 napi_create_object(env, &val);
179 SetPropertyToNapiObject(env, val, "signalType", type);
180 SetPropertyToNapiObject(env, val, "signalLevel", level);
181 SetPropertyToNapiObject(env, val, "dBm", signalIntensity);
182 return val;
183 }
184
DataOfNetworkConversion(napi_env env,const GsmCellInformation & info)185 napi_value DataOfNetworkConversion(napi_env env, const GsmCellInformation &info)
186 {
187 napi_value val = nullptr;
188 napi_create_object(env, &val);
189 SetPropertyToNapiObject(env, val, "lac", info.GetLac());
190 SetPropertyToNapiObject(env, val, "cellId", info.GetCellId());
191 SetPropertyToNapiObject(env, val, "arfcn", info.GetArfcn());
192 SetPropertyToNapiObject(env, val, "basic", info.GetBsic());
193 SetPropertyToNapiObject(env, val, "mcc", info.GetMcc());
194 SetPropertyToNapiObject(env, val, "mnc", info.GetMnc());
195 return val;
196 }
197
DataOfNetworkConversion(napi_env env,const LteCellInformation & info)198 napi_value DataOfNetworkConversion(napi_env env, const LteCellInformation &info)
199 {
200 napi_value val = nullptr;
201 napi_create_object(env, &val);
202 SetPropertyToNapiObject(env, val, "cgi", info.GetCellId());
203 SetPropertyToNapiObject(env, val, "pci", info.GetPci());
204 SetPropertyToNapiObject(env, val, "tac", info.GetTac());
205 SetPropertyToNapiObject(env, val, "earfcn", info.GetArfcn());
206 SetPropertyToNapiObject(env, val, "bandwith", 0);
207 SetPropertyToNapiObject(env, val, "mcc", info.GetMcc());
208 SetPropertyToNapiObject(env, val, "mnc", info.GetMnc());
209 SetPropertyToNapiObject(env, val, "isSupportEndc", false);
210 return val;
211 }
212
DataOfNetworkConversion(napi_env env,const WcdmaCellInformation & info)213 napi_value DataOfNetworkConversion(napi_env env, const WcdmaCellInformation &info)
214 {
215 napi_value val = nullptr;
216 napi_create_object(env, &val);
217 SetPropertyToNapiObject(env, val, "lac", info.GetLac());
218 SetPropertyToNapiObject(env, val, "cellId", info.GetCellId());
219 SetPropertyToNapiObject(env, val, "psc", info.GetPsc());
220 SetPropertyToNapiObject(env, val, "uarfcn", info.GetArfcn());
221 SetPropertyToNapiObject(env, val, "mcc", info.GetMcc());
222 SetPropertyToNapiObject(env, val, "mnc", info.GetMnc());
223 return val;
224 }
225
DataOfNetworkConversion(napi_env env,const NrCellInformation & info)226 napi_value DataOfNetworkConversion(napi_env env, const NrCellInformation &info)
227 {
228 napi_value val = nullptr;
229 napi_create_object(env, &val);
230 SetPropertyToNapiObject(env, val, "nrArfcn", info.GetArfcn());
231 SetPropertyToNapiObject(env, val, "pci", info.GetPci());
232 SetPropertyToNapiObject(env, val, "tac", info.GetTac());
233 SetPropertyToNapiObject(env, val, "nci", info.GetNci());
234 SetPropertyToNapiObject(env, val, "mcc", info.GetMcc());
235 SetPropertyToNapiObject(env, val, "mnc", info.GetMnc());
236 return val;
237 }
238
CellInfoConversion(napi_env env,const CellInformation & info)239 napi_value CellInfoConversion(napi_env env, const CellInformation &info)
240 {
241 napi_value val = nullptr;
242 napi_create_object(env, &val);
243 CellInformation::CellType networkType = info.GetNetworkType();
244 SetPropertyToNapiObject(env, val, "networkType", static_cast<int32_t>(networkType));
245 SetPropertyToNapiObject(env, val, "isCamped", info.GetIsCamped());
246 SetPropertyToNapiObject(env, val, "timeStamp", static_cast<int64_t>(info.GetTimeStamp()));
247 SetPropertyToNapiObject(env, val, "signalInformation",
248 SignalInfoConversion(env, static_cast<int32_t>(networkType), info.GetSignalLevel(), info.GetSignalIntensity()));
249
250 switch (networkType) {
251 case CellInformation::CellType::CELL_TYPE_GSM:
252 napi_set_named_property(
253 env, val, "data", DataOfNetworkConversion(env, static_cast<const GsmCellInformation &>(info)));
254 break;
255 case CellInformation::CellType::CELL_TYPE_LTE:
256 napi_set_named_property(
257 env, val, "data", DataOfNetworkConversion(env, static_cast<const LteCellInformation &>(info)));
258 break;
259 case CellInformation::CellType::CELL_TYPE_WCDMA:
260 napi_set_named_property(
261 env, val, "data", DataOfNetworkConversion(env, static_cast<const WcdmaCellInformation &>(info)));
262 break;
263 case CellInformation::CellType::CELL_TYPE_NR:
264 napi_set_named_property(
265 env, val, "data", DataOfNetworkConversion(env, static_cast<const NrCellInformation &>(info)));
266 break;
267 default:
268 break;
269 }
270 return val;
271 }
272
InitLoop(napi_env env,uv_loop_s ** loop)273 bool InitLoop(napi_env env, uv_loop_s **loop)
274 {
275 #if NAPI_VERSION >= 2
276 napi_status status = napi_get_uv_event_loop(env, loop);
277 if (status != napi_ok) {
278 TELEPHONY_LOGE("napi_get_uv_event_loop napi_status = %{public}d", status);
279 return false;
280 }
281 #endif // NAPI_VERSION >= 2
282 return *loop != nullptr;
283 }
284 } // namespace
285
286 std::map<TelephonyUpdateEventType,
287 void (*)(uv_work_t *work, std::unique_lock<std::mutex> &lock)> EventListenerHandler::workFuncMap_;
288 std::mutex EventListenerHandler::operatorMutex_;
289
EventListenerHandler()290 EventListenerHandler::EventListenerHandler() : AppExecFwk::EventHandler(AppExecFwk::EventRunner::Create())
291 {
292 AddBasicHandlerToMap();
293 AddNetworkHandlerToMap();
294 AddWorkFuncToMap();
295 }
296
~EventListenerHandler()297 EventListenerHandler::~EventListenerHandler()
298 {
299 handleFuncMap_.clear();
300 workFuncMap_.clear();
301 listenerList_.clear();
302 }
303
AddBasicHandlerToMap()304 void EventListenerHandler::AddBasicHandlerToMap()
305 {
306 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_CALL_STATE_UPDATE] =
307 [this](const AppExecFwk::InnerEvent::Pointer &event) {
308 HandleCallbackInfoUpdate<CallStateContext, CallStateUpdateInfo,
309 TelephonyUpdateEventType::EVENT_CALL_STATE_UPDATE>(event);
310 };
311 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_SIM_STATE_UPDATE] =
312 [this](const AppExecFwk::InnerEvent::Pointer &event) {
313 HandleCallbackInfoUpdate<SimStateContext, SimStateUpdateInfo,
314 TelephonyUpdateEventType::EVENT_SIM_STATE_UPDATE>(event);
315 };
316 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_CELLULAR_DATA_CONNECTION_UPDATE] =
317 [this](const AppExecFwk::InnerEvent::Pointer &event) {
318 HandleCallbackInfoUpdate<CellularDataConnectStateContext, CellularDataConnectState,
319 TelephonyUpdateEventType::EVENT_DATA_CONNECTION_UPDATE>(event);
320 };
321 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_CELLULAR_DATA_FLOW_UPDATE] =
322 [this](const AppExecFwk::InnerEvent::Pointer &event) {
323 HandleCallbackInfoUpdate<CellularDataFlowContext, CellularDataFlowUpdate,
324 TelephonyUpdateEventType::EVENT_CELLULAR_DATA_FLOW_UPDATE>(event);
325 };
326 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_CFU_INDICATOR_UPDATE] =
327 [this](const AppExecFwk::InnerEvent::Pointer &event) {
328 HandleCallbackInfoUpdate<CfuIndicatorContext, CfuIndicatorUpdate,
329 TelephonyUpdateEventType::EVENT_CFU_INDICATOR_UPDATE>(event);
330 };
331 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_VOICE_MAIL_MSG_INDICATOR_UPDATE] =
332 [this](const AppExecFwk::InnerEvent::Pointer &event) {
333 HandleCallbackInfoUpdate<VoiceMailMsgIndicatorContext, VoiceMailMsgIndicatorUpdate,
334 TelephonyUpdateEventType::EVENT_VOICE_MAIL_MSG_INDICATOR_UPDATE>(event);
335 };
336 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_ICC_ACCOUNT_UPDATE] =
337 [this](const AppExecFwk::InnerEvent::Pointer &event) {
338 HandleCallbackVoidUpdate<TelephonyUpdateEventType::EVENT_ICC_ACCOUNT_CHANGE>(event);
339 };
340 }
341
AddNetworkHandlerToMap()342 void EventListenerHandler::AddNetworkHandlerToMap()
343 {
344 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_SIGNAL_INFO_UPDATE] =
345 [this](const AppExecFwk::InnerEvent::Pointer &event) {
346 HandleCallbackInfoUpdate<SignalListContext, SignalUpdateInfo,
347 TelephonyUpdateEventType::EVENT_SIGNAL_STRENGTHS_UPDATE>(event);
348 };
349 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_NETWORK_STATE_UPDATE] =
350 [this](const AppExecFwk::InnerEvent::Pointer &event) {
351 HandleCallbackInfoUpdate<NetworkStateContext, NetworkStateUpdateInfo,
352 TelephonyUpdateEventType::EVENT_NETWORK_STATE_UPDATE>(event);
353 };
354 handleFuncMap_[TelephonyCallbackEventId::EVENT_ON_CELL_INFOMATION_UPDATE] =
355 [this](const AppExecFwk::InnerEvent::Pointer &event) {
356 HandleCallbackInfoUpdate<CellInfomationContext, CellInfomationUpdate,
357 TelephonyUpdateEventType::EVENT_CELL_INFO_UPDATE>(event);
358 };
359 }
360
AddWorkFuncToMap()361 void EventListenerHandler::AddWorkFuncToMap()
362 {
363 workFuncMap_[TelephonyUpdateEventType::EVENT_CALL_STATE_UPDATE] = &EventListenerHandler::WorkCallStateUpdated;
364 workFuncMap_[TelephonyUpdateEventType::EVENT_SIGNAL_STRENGTHS_UPDATE] = &EventListenerHandler::WorkSignalUpdated;
365 workFuncMap_[TelephonyUpdateEventType::EVENT_NETWORK_STATE_UPDATE] = &EventListenerHandler::WorkNetworkStateUpdated;
366 workFuncMap_[TelephonyUpdateEventType::EVENT_SIM_STATE_UPDATE] = &EventListenerHandler::WorkSimStateUpdated;
367 workFuncMap_[TelephonyUpdateEventType::EVENT_CELL_INFO_UPDATE] = &EventListenerHandler::WorkCellInfomationUpdated;
368 workFuncMap_[TelephonyUpdateEventType::EVENT_DATA_CONNECTION_UPDATE] =
369 &EventListenerHandler::WorkCellularDataConnectStateUpdated;
370 workFuncMap_[TelephonyUpdateEventType::EVENT_CELLULAR_DATA_FLOW_UPDATE] =
371 &EventListenerHandler::WorkCellularDataFlowUpdated;
372 workFuncMap_[TelephonyUpdateEventType::EVENT_CFU_INDICATOR_UPDATE] = &EventListenerHandler::WorkCfuIndicatorUpdated;
373 workFuncMap_[TelephonyUpdateEventType::EVENT_VOICE_MAIL_MSG_INDICATOR_UPDATE] =
374 &EventListenerHandler::WorkVoiceMailMsgIndicatorUpdated;
375 workFuncMap_[TelephonyUpdateEventType::EVENT_ICC_ACCOUNT_CHANGE] = &EventListenerHandler::WorkIccAccountUpdated;
376 }
377
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)378 void EventListenerHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
379 {
380 if (event == nullptr) {
381 TELEPHONY_LOGE("EventListenerHandler::ProcessEvent event is nullptr");
382 return;
383 }
384 auto eventId = static_cast<TelephonyCallbackEventId>(event->GetInnerEventId());
385 if (eventId == TelephonyCallbackEventId::EVENT_ON_SIGNAL_INFO_UPDATE) {
386 TELEPHONY_LOGD("process event %{public}d", eventId);
387 } else {
388 TELEPHONY_LOGI("process event %{public}d", eventId);
389 }
390 auto itor = handleFuncMap_.find(eventId);
391 if (itor != handleFuncMap_.end()) {
392 itor->second(event);
393 }
394 }
395
CheckEventListenerRegister(EventListener & eventListener)396 int32_t EventListenerHandler::CheckEventListenerRegister(EventListener &eventListener)
397 {
398 int32_t flag = EVENT_LISTENER_DIFF;
399 for (auto &listen : listenerList_) {
400 if (eventListener.env == listen.env && eventListener.slotId == listen.slotId &&
401 eventListener.eventType == listen.eventType &&
402 IsCallBackRegister(eventListener.env, eventListener.callbackRef, listen.callbackRef)) {
403 flag = EVENT_LISTENER_SAME;
404 return flag;
405 }
406 if (eventListener.slotId == listen.slotId && eventListener.eventType == listen.eventType) {
407 flag = EVENT_LISTENER_SLOTID_AND_EVENTTYPE_SAME;
408 }
409 }
410 return flag;
411 }
412
RegisterEventListener(EventListener & eventListener)413 int32_t EventListenerHandler::RegisterEventListener(EventListener &eventListener)
414 {
415 std::unique_lock<std::mutex> lock(operatorMutex_);
416 int32_t registerStatus = CheckEventListenerRegister(eventListener);
417 if (registerStatus == EVENT_LISTENER_SAME) {
418 return TELEPHONY_ERR_CALLBACK_ALREADY_REGISTERED;
419 }
420 if (registerStatus != EVENT_LISTENER_SLOTID_AND_EVENTTYPE_SAME) {
421 NapiTelephonyObserver *telephonyObserver = std::make_unique<NapiTelephonyObserver>().release();
422 if (telephonyObserver == nullptr) {
423 TELEPHONY_LOGE("error by telephonyObserver nullptr");
424 return TELEPHONY_ERR_LOCAL_PTR_NULL;
425 }
426 sptr<TelephonyObserverBroker> observer(telephonyObserver);
427 if (observer == nullptr) {
428 TELEPHONY_LOGE("error by observer nullptr");
429 return TELEPHONY_ERR_LOCAL_PTR_NULL;
430 }
431 bool isUpdate = (eventListener.eventType == TelephonyUpdateEventType::EVENT_CALL_STATE_UPDATE ||
432 eventListener.eventType == TelephonyUpdateEventType::EVENT_SIM_STATE_UPDATE);
433 int32_t addResult = TelephonyStateManager::AddStateObserver(
434 observer, eventListener.slotId, ToUint32t(eventListener.eventType), isUpdate);
435 if (addResult != TELEPHONY_SUCCESS) {
436 TELEPHONY_LOGE("AddStateObserver failed, ret=%{public}d!", addResult);
437 return addResult;
438 }
439 }
440 listenerList_.push_back(eventListener);
441 TELEPHONY_LOGI("EventListenerHandler::RegisterEventListener listenerList_ size=%{public}d",
442 static_cast<int32_t>(listenerList_.size()));
443 return TELEPHONY_SUCCESS;
444 }
445
SetEventListenerDeleting(std::shared_ptr<bool> isDeleting)446 void EventListenerHandler::SetEventListenerDeleting(std::shared_ptr<bool> isDeleting)
447 {
448 if (isDeleting == nullptr) {
449 TELEPHONY_LOGE("isDeleting is nullptr");
450 return;
451 }
452 *isDeleting = true;
453 }
454
CheckEventTypeExist(int32_t slotId,TelephonyUpdateEventType eventType)455 bool EventListenerHandler::CheckEventTypeExist(int32_t slotId, TelephonyUpdateEventType eventType)
456 {
457 for (auto &listen : listenerList_) {
458 if (slotId == listen.slotId && eventType == listen.eventType) {
459 return true;
460 }
461 }
462 return false;
463 }
464
RemoveEventListenerRegister(napi_env env,TelephonyUpdateEventType eventType,napi_ref ref,std::list<EventListener> & removeListenerList,std::set<int32_t> & soltIdSet)465 void EventListenerHandler::RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType, napi_ref ref,
466 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet)
467 {
468 std::list<EventListener>::iterator it = listenerList_.begin();
469 while (it != listenerList_.end()) {
470 if (env == it->env && eventType == it->eventType && IsCallBackRegister(env, ref, it->callbackRef)) {
471 SetEventListenerDeleting(it->isDeleting);
472 soltIdSet.insert(it->slotId);
473 removeListenerList.push_back(*it);
474 it = listenerList_.erase(it);
475 } else {
476 ++it;
477 }
478 }
479 }
480
RemoveEventListenerRegister(napi_env env,TelephonyUpdateEventType eventType,std::list<EventListener> & removeListenerList,std::set<int32_t> & soltIdSet)481 void EventListenerHandler::RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType,
482 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet)
483 {
484 std::list<EventListener>::iterator it = listenerList_.begin();
485 while (it != listenerList_.end()) {
486 if (env == it->env && eventType == it->eventType) {
487 SetEventListenerDeleting(it->isDeleting);
488 soltIdSet.insert(it->slotId);
489 removeListenerList.push_back(*it);
490 it = listenerList_.erase(it);
491 } else {
492 ++it;
493 }
494 }
495 }
496
CheckRemoveStateObserver(TelephonyUpdateEventType eventType,int32_t slotId,int32_t & result)497 void EventListenerHandler::CheckRemoveStateObserver(TelephonyUpdateEventType eventType, int32_t slotId, int32_t &result)
498 {
499 if (!CheckEventTypeExist(slotId, eventType)) {
500 int32_t removeRet = TelephonyStateManager::RemoveStateObserver(slotId, ToUint32t(eventType));
501 if (removeRet != TELEPHONY_SUCCESS) {
502 TELEPHONY_LOGE("EventListenerHandler::RemoveStateObserver slotId %{public}d, eventType %{public}d fail!",
503 slotId, static_cast<int32_t>(eventType));
504 result = removeRet;
505 }
506 }
507 }
508
UnregisterEventListener(napi_env env,TelephonyUpdateEventType eventType,napi_ref ref,std::list<EventListener> & removeListenerList)509 int32_t EventListenerHandler::UnregisterEventListener(
510 napi_env env, TelephonyUpdateEventType eventType, napi_ref ref, std::list<EventListener> &removeListenerList)
511 {
512 std::unique_lock<std::mutex> lock(operatorMutex_);
513 if (listenerList_.empty()) {
514 TELEPHONY_LOGI("UnregisterEventListener listener list is empty.");
515 return TELEPHONY_SUCCESS;
516 }
517
518 std::set<int32_t> soltIdSet;
519 RemoveEventListenerRegister(env, eventType, ref, removeListenerList, soltIdSet);
520 int32_t result = TELEPHONY_SUCCESS;
521 for (int32_t slotId : soltIdSet) {
522 CheckRemoveStateObserver(eventType, slotId, result);
523 }
524 TELEPHONY_LOGI("EventListenerHandler::UnregisterEventListener listenerList_ size=%{public}d",
525 static_cast<int32_t>(listenerList_.size()));
526 return result;
527 }
528
UnregisterEventListener(napi_env env,TelephonyUpdateEventType eventType,std::list<EventListener> & removeListenerList)529 int32_t EventListenerHandler::UnregisterEventListener(
530 napi_env env, TelephonyUpdateEventType eventType, std::list<EventListener> &removeListenerList)
531 {
532 std::unique_lock<std::mutex> lock(operatorMutex_);
533 if (listenerList_.empty()) {
534 TELEPHONY_LOGI("UnregisterEventListener listener list is empty.");
535 return TELEPHONY_SUCCESS;
536 }
537
538 std::set<int32_t> soltIdSet;
539 RemoveEventListenerRegister(env, eventType, removeListenerList, soltIdSet);
540 int32_t result = TELEPHONY_SUCCESS;
541 for (int32_t slotId : soltIdSet) {
542 CheckRemoveStateObserver(eventType, slotId, result);
543 }
544 TELEPHONY_LOGI("EventListenerHandler::UnregisterEventListener listenerList_ size=%{public}d",
545 static_cast<int32_t>(listenerList_.size()));
546 return result;
547 }
548
UnRegisterAllListener(napi_env env)549 void EventListenerHandler::UnRegisterAllListener(napi_env env)
550 {
551 std::unique_lock<std::mutex> lock(operatorMutex_);
552 if (listenerList_.empty()) {
553 TELEPHONY_LOGI("UnRegisterAllListener listener list is empty.");
554 return;
555 } else {
556 TELEPHONY_LOGI("UnRegisterAllListener listener list size start: %{public}d",
557 static_cast<int32_t>(listenerList_.size()));
558 }
559 std::map<int32_t, std::set<TelephonyUpdateEventType>> removeTypeMap;
560 listenerList_.remove_if([&](EventListener listener) -> bool {
561 bool matched = listener.env == env;
562 if (matched) {
563 SetEventListenerDeleting(listener.isDeleting);
564 if (!removeTypeMap.count(listener.slotId)) {
565 std::set<TelephonyUpdateEventType> eventTypeSet;
566 eventTypeSet.insert(listener.eventType);
567 removeTypeMap.insert(std::make_pair(listener.slotId, eventTypeSet));
568 } else {
569 removeTypeMap[listener.slotId].insert(listener.eventType);
570 }
571 if (listener.env != nullptr && listener.callbackRef != nullptr) {
572 napi_delete_reference(listener.env, listener.callbackRef);
573 listener.callbackRef = nullptr;
574 }
575 }
576
577 return matched;
578 });
579
580 int32_t result = TELEPHONY_SUCCESS;
581 for (auto &elem : removeTypeMap) {
582 for (auto &innerElem : elem.second) {
583 CheckRemoveStateObserver(innerElem, elem.first, result);
584 }
585 }
586 TELEPHONY_LOGI(
587 "UnRegisterAllListener listener list size finish: %{public}d", static_cast<int32_t>(listenerList_.size()));
588 }
589
IsCallBackRegister(napi_env env,napi_ref ref,napi_ref registeredRef) const590 bool EventListenerHandler::IsCallBackRegister(napi_env env, napi_ref ref, napi_ref registeredRef) const
591 {
592 napi_value callback = nullptr;
593 napi_get_reference_value(env, ref, &callback);
594 napi_value existCallBack = nullptr;
595 napi_get_reference_value(env, registeredRef, &existCallBack);
596 bool result = false;
597 napi_strict_equals(env, callback, existCallBack, &result);
598 return result;
599 }
600
601 template<typename T, typename D, TelephonyUpdateEventType eventType>
HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer & event)602 void EventListenerHandler::HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer &event)
603 {
604 if (event == nullptr) {
605 TELEPHONY_LOGE("event nullptr");
606 return;
607 }
608
609 std::unique_ptr<D> info = event->GetUniqueObject<D>();
610 if (info == nullptr) {
611 TELEPHONY_LOGE("update info nullptr");
612 return;
613 }
614
615 std::unique_lock<std::mutex> lock(operatorMutex_);
616 for (const EventListener &listen : listenerList_) {
617 if ((listen.eventType == eventType) && (listen.slotId == info->slotId_)) {
618 uv_loop_s *loop = nullptr;
619 if (!InitLoop(listen.env, &loop)) {
620 TELEPHONY_LOGE("loop is null");
621 break;
622 }
623 T *context = std::make_unique<T>().release();
624 if (context == nullptr) {
625 TELEPHONY_LOGE("make context failed");
626 break;
627 }
628 *(static_cast<EventListener *>(context)) = listen;
629 *context = *info;
630 uv_work_t *work = std::make_unique<uv_work_t>().release();
631 if (work == nullptr) {
632 TELEPHONY_LOGE("make work failed");
633 break;
634 }
635 work->data = static_cast<void *>(context);
636 int32_t resultCode =
637 uv_queue_work_with_qos(loop, work, [](uv_work_t *) {}, WorkUpdated, uv_qos_default);
638 if (resultCode != 0) {
639 delete context;
640 context = nullptr;
641 TELEPHONY_LOGE("HandleCallbackInfoUpdate failed, result: %{public}d", resultCode);
642 delete work;
643 work = nullptr;
644 return;
645 }
646 }
647 }
648 }
649
650 template<TelephonyUpdateEventType eventType>
HandleCallbackVoidUpdate(const AppExecFwk::InnerEvent::Pointer & event)651 void EventListenerHandler::HandleCallbackVoidUpdate(const AppExecFwk::InnerEvent::Pointer &event)
652 {
653 if (event == nullptr) {
654 TELEPHONY_LOGE("event nullptr");
655 return;
656 }
657
658 std::unique_lock<std::mutex> lock(operatorMutex_);
659 for (const EventListener &listen : listenerList_) {
660 if ((listen.eventType == eventType)) {
661 uv_loop_s *loop = nullptr;
662 if (!InitLoop(listen.env, &loop)) {
663 TELEPHONY_LOGE("loop is null");
664 break;
665 }
666 uv_work_t *work = std::make_unique<uv_work_t>().release();
667 if (work == nullptr) {
668 TELEPHONY_LOGE("make work failed");
669 break;
670 }
671 EventListener *listener = new EventListener();
672 listener->env = listen.env;
673 listener->eventType = listen.eventType;
674 listener->slotId = listen.slotId;
675 listener->callbackRef = listen.callbackRef;
676 listener->isDeleting = listen.isDeleting;
677 work->data = static_cast<void *>(listener);
678 int32_t retVal =
679 uv_queue_work_with_qos(loop, work, [](uv_work_t *) {}, WorkUpdated, uv_qos_default);
680 if (retVal != 0) {
681 delete listener;
682 listener = nullptr;
683 TELEPHONY_LOGE("HandleCallbackVoidUpdate failed, result: %{public}d", retVal);
684 delete work;
685 work = nullptr;
686 return;
687 }
688 }
689 }
690 }
691
WorkUpdated(uv_work_t * work,int status)692 void EventListenerHandler::WorkUpdated(uv_work_t *work, int status)
693 {
694 std::unique_lock<std::mutex> lock(operatorMutex_);
695 EventListener *listener = static_cast<EventListener *>(work->data);
696 if (listener->eventType == TelephonyUpdateEventType::EVENT_SIGNAL_STRENGTHS_UPDATE) {
697 TELEPHONY_LOGD("WorkUpdated eventType is %{public}d", listener->eventType);
698 } else {
699 TELEPHONY_LOGI("WorkUpdated eventType is %{public}d", listener->eventType);
700 }
701 if (listener->isDeleting == nullptr || *(listener->isDeleting)) {
702 TELEPHONY_LOGI("listener is deleting");
703 delete listener;
704 listener = nullptr;
705 return;
706 }
707 if (workFuncMap_.find(listener->eventType) == workFuncMap_.end() ||
708 workFuncMap_.find(listener->eventType)->second == nullptr) {
709 TELEPHONY_LOGE("listener state update is nullptr");
710 delete listener;
711 listener = nullptr;
712 return;
713 }
714 workFuncMap_.find(listener->eventType)->second(work, lock);
715 }
716
WorkCallStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)717 void EventListenerHandler::WorkCallStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
718 {
719 std::unique_ptr<CallStateContext> callStateInfo(static_cast<CallStateContext *>(work->data));
720 const napi_env &env = callStateInfo->env;
721 napi_handle_scope scope = nullptr;
722 napi_open_handle_scope(env, &scope);
723 if (scope == nullptr) {
724 TELEPHONY_LOGE("scope is nullptr");
725 }
726 napi_value callbackValue = nullptr;
727 napi_create_object(callStateInfo->env, &callbackValue);
728 int32_t wrappedCallState = WrapCallState(callStateInfo->callState);
729 std::string number = NapiUtil::ToUtf8(callStateInfo->phoneNumber);
730 SetPropertyToNapiObject(callStateInfo->env, callbackValue, "state", wrappedCallState);
731 SetPropertyToNapiObject(callStateInfo->env, callbackValue, "number", number);
732 NapiReturnToJS(callStateInfo->env, callStateInfo->callbackRef, callbackValue, lock);
733 napi_close_handle_scope(env, scope);
734 }
735
WorkSignalUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)736 void EventListenerHandler::WorkSignalUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
737 {
738 napi_handle_scope scope = nullptr;
739 std::unique_ptr<SignalListContext> infoListUpdateInfo(static_cast<SignalListContext *>(work->data));
740 napi_value callbackValue = nullptr;
741 const napi_env &env = infoListUpdateInfo->env;
742 napi_open_handle_scope(env, &scope);
743 if (scope == nullptr) {
744 TELEPHONY_LOGE("scope is nullptr");
745 }
746 napi_create_array(env, &callbackValue);
747 size_t infoSize = infoListUpdateInfo->signalInfoList.size();
748 for (size_t i = 0; i < infoSize; ++i) {
749 sptr<SignalInformation> infoItem = infoListUpdateInfo->signalInfoList[i];
750 napi_value info = nullptr;
751 napi_create_object(env, &info);
752 SetPropertyToNapiObject(env, info, "signalType", WrapNetworkType(infoItem->GetNetworkType()));
753 SetPropertyToNapiObject(env, info, "signalLevel", infoItem->GetSignalLevel());
754 SetPropertyToNapiObject(env, info, "dBm", infoItem->GetSignalIntensity());
755 napi_set_element(env, callbackValue, i, info);
756 }
757 NapiReturnToJS(env, infoListUpdateInfo->callbackRef, callbackValue, lock);
758 napi_close_handle_scope(env, scope);
759 }
760
WorkNetworkStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)761 void EventListenerHandler::WorkNetworkStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
762 {
763 napi_handle_scope scope = nullptr;
764 std::unique_ptr<NetworkStateContext> networkStateUpdateInfo(static_cast<NetworkStateContext *>(work->data));
765 napi_value callbackValue = nullptr;
766 const napi_env &env = networkStateUpdateInfo->env;
767 napi_open_handle_scope(env, &scope);
768 if (scope == nullptr) {
769 TELEPHONY_LOGE("scope is nullptr");
770 }
771 const sptr<NetworkState> &networkState = networkStateUpdateInfo->networkState;
772 napi_create_object(env, &callbackValue);
773 std::string longOperatorName = networkState->GetLongOperatorName();
774 std::string shortOperatorName = networkState->GetShortOperatorName();
775 std::string plmnNumeric = networkState->GetPlmnNumeric();
776 bool isRoaming = networkState->IsRoaming();
777 int32_t regStatus = static_cast<int32_t>(networkState->GetRegStatus());
778 bool isEmergency = networkState->IsEmergency();
779 int32_t cfgTech = static_cast<int32_t>(networkState->GetCfgTech());
780 int32_t nsaState = static_cast<int32_t>(networkState->GetNrState());
781 SetPropertyToNapiObject(env, callbackValue, "longOperatorName", longOperatorName);
782 SetPropertyToNapiObject(env, callbackValue, "shortOperatorName", shortOperatorName);
783 SetPropertyToNapiObject(env, callbackValue, "plmnNumeric", plmnNumeric);
784 SetPropertyToNapiObject(env, callbackValue, "isRoaming", isRoaming);
785 SetPropertyToNapiObject(env, callbackValue, "regState", WrapRegState(regStatus));
786 SetPropertyToNapiObject(env, callbackValue, "isEmergency", isEmergency);
787 SetPropertyToNapiObject(env, callbackValue, "cfgTech", WrapRadioTech(cfgTech));
788 SetPropertyToNapiObject(env, callbackValue, "nsaState", nsaState);
789 SetPropertyToNapiObject(env, callbackValue, "isCaActive", false);
790 NapiReturnToJS(env, networkStateUpdateInfo->callbackRef, callbackValue, lock);
791 napi_close_handle_scope(env, scope);
792 }
793
WorkSimStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)794 void EventListenerHandler::WorkSimStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
795 {
796 napi_handle_scope scope = nullptr;
797 std::unique_ptr<SimStateContext> simStateUpdateInfo(static_cast<SimStateContext *>(work->data));
798 const napi_env &env = simStateUpdateInfo->env;
799 napi_open_handle_scope(env, &scope);
800 if (scope == nullptr) {
801 TELEPHONY_LOGE("scope is nullptr");
802 }
803 napi_value callbackValue = nullptr;
804 int32_t cardType = static_cast<int32_t>(simStateUpdateInfo->cardType);
805 int32_t simState = static_cast<int32_t>(simStateUpdateInfo->simState);
806 int32_t lockReason = static_cast<int32_t>(simStateUpdateInfo->reason);
807 napi_create_object(simStateUpdateInfo->env, &callbackValue);
808 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "type", cardType);
809 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "state", simState);
810 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "reason", lockReason);
811 NapiReturnToJS(simStateUpdateInfo->env, simStateUpdateInfo->callbackRef, callbackValue, lock);
812 napi_close_handle_scope(env, scope);
813 }
814
WorkCellInfomationUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)815 void EventListenerHandler::WorkCellInfomationUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
816 {
817 std::unique_ptr<CellInfomationContext> cellInfo(static_cast<CellInfomationContext *>(work->data));
818 napi_value callbackValue = nullptr;
819 const napi_env &env = cellInfo->env;
820 napi_handle_scope scope = nullptr;
821 napi_open_handle_scope(env, &scope);
822 if (scope == nullptr) {
823 TELEPHONY_LOGE("scope is nullptr");
824 }
825 napi_create_array(cellInfo->env, &callbackValue);
826 for (size_t i = 0; i < cellInfo->cellInfoVec.size(); i++) {
827 napi_value val = CellInfoConversion(cellInfo->env, *(cellInfo->cellInfoVec[i]));
828 napi_set_element(cellInfo->env, callbackValue, i, val);
829 }
830 NapiReturnToJS(cellInfo->env, cellInfo->callbackRef, callbackValue, lock);
831 napi_close_handle_scope(env, scope);
832 }
833
WorkCellularDataConnectStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)834 void EventListenerHandler::WorkCellularDataConnectStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
835 {
836 napi_handle_scope scope = nullptr;
837 std::unique_ptr<CellularDataConnectStateContext> context(
838 static_cast<CellularDataConnectStateContext *>(work->data));
839 const napi_env &env = context->env;
840 napi_open_handle_scope(env, &scope);
841 if (scope == nullptr) {
842 TELEPHONY_LOGE("scope is nullptr");
843 }
844 napi_value callbackValue = nullptr;
845 napi_create_object(context->env, &callbackValue);
846 SetPropertyToNapiObject(context->env, callbackValue, "state", context->dataState);
847 SetPropertyToNapiObject(context->env, callbackValue, "network", context->networkType);
848 NapiReturnToJS(context->env, context->callbackRef, callbackValue, lock);
849 napi_close_handle_scope(env, scope);
850 }
851
WorkCellularDataFlowUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)852 void EventListenerHandler::WorkCellularDataFlowUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
853 {
854 std::unique_ptr<CellularDataFlowContext> dataFlowInfo(static_cast<CellularDataFlowContext *>(work->data));
855 const napi_env &env = dataFlowInfo->env;
856 napi_handle_scope scope = nullptr;
857 napi_open_handle_scope(env, &scope);
858 if (scope == nullptr) {
859 TELEPHONY_LOGE("scope is nullptr");
860 }
861 napi_value callbackValue = GetNapiValue(dataFlowInfo->env, dataFlowInfo->flowType_);
862 NapiReturnToJS(dataFlowInfo->env, dataFlowInfo->callbackRef, callbackValue, lock);
863 napi_close_handle_scope(env, scope);
864 }
865
WorkCfuIndicatorUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)866 void EventListenerHandler::WorkCfuIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
867 {
868 if (work == nullptr) {
869 TELEPHONY_LOGE("work is null");
870 return;
871 }
872 std::unique_ptr<CfuIndicatorContext> cfuIndicatorInfo(static_cast<CfuIndicatorContext *>(work->data));
873 const napi_env &env = cfuIndicatorInfo->env;
874 napi_handle_scope scope = nullptr;
875 napi_open_handle_scope(env, &scope);
876 if (scope == nullptr) {
877 TELEPHONY_LOGE("scope is nullptr");
878 }
879 napi_value callbackValue = GetNapiValue(cfuIndicatorInfo->env, cfuIndicatorInfo->cfuResult_);
880 NapiReturnToJS(cfuIndicatorInfo->env, cfuIndicatorInfo->callbackRef, callbackValue, lock);
881 napi_close_handle_scope(env, scope);
882 }
883
WorkVoiceMailMsgIndicatorUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)884 void EventListenerHandler::WorkVoiceMailMsgIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
885 {
886 if (work == nullptr) {
887 TELEPHONY_LOGE("work is null");
888 return;
889 }
890 std::unique_ptr<VoiceMailMsgIndicatorContext> voiceMailMsgIndicatorInfo(
891 static_cast<VoiceMailMsgIndicatorContext *>(work->data));
892 const napi_env &env = voiceMailMsgIndicatorInfo->env;
893 napi_handle_scope scope = nullptr;
894 napi_open_handle_scope(env, &scope);
895 if (scope == nullptr) {
896 TELEPHONY_LOGE("scope is nullptr");
897 }
898 napi_value callbackValue =
899 GetNapiValue(voiceMailMsgIndicatorInfo->env, voiceMailMsgIndicatorInfo->voiceMailMsgResult_);
900 NapiReturnToJS(voiceMailMsgIndicatorInfo->env, voiceMailMsgIndicatorInfo->callbackRef, callbackValue, lock);
901 napi_close_handle_scope(env, scope);
902 }
903
WorkIccAccountUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)904 void EventListenerHandler::WorkIccAccountUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
905 {
906 if (work == nullptr) {
907 TELEPHONY_LOGE("work is null");
908 return;
909 }
910 std::unique_ptr<EventListener> UpdateIccAccount(static_cast<EventListener *>(work->data));
911 const napi_env &env = UpdateIccAccount->env;
912 napi_handle_scope scope = nullptr;
913 napi_open_handle_scope(env, &scope);
914 if (scope == nullptr) {
915 TELEPHONY_LOGE("scope is nullptr");
916 }
917 napi_value callbackValue = nullptr;
918 napi_create_object(UpdateIccAccount->env, &callbackValue);
919 NapiReturnToJS(UpdateIccAccount->env, UpdateIccAccount->callbackRef, callbackValue, lock);
920 napi_close_handle_scope(env, scope);
921 }
922 } // namespace Telephony
923 } // namespace OHOS
924