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 int32_t addResult = TelephonyStateManager::AddStateObserver(
432 observer, eventListener.slotId, ToUint32t(eventListener.eventType),
433 eventListener.eventType == TelephonyUpdateEventType::EVENT_CALL_STATE_UPDATE);
434 if (addResult != TELEPHONY_SUCCESS) {
435 TELEPHONY_LOGE("AddStateObserver failed, ret=%{public}d!", addResult);
436 return addResult;
437 }
438 }
439 listenerList_.push_back(eventListener);
440 TELEPHONY_LOGI("EventListenerHandler::RegisterEventListener listenerList_ size=%{public}d",
441 static_cast<int32_t>(listenerList_.size()));
442 return TELEPHONY_SUCCESS;
443 }
444
SetEventListenerDeleting(std::shared_ptr<bool> isDeleting)445 void EventListenerHandler::SetEventListenerDeleting(std::shared_ptr<bool> isDeleting)
446 {
447 if (isDeleting == nullptr) {
448 TELEPHONY_LOGE("isDeleting is nullptr");
449 return;
450 }
451 *isDeleting = true;
452 }
453
CheckEventTypeExist(int32_t slotId,TelephonyUpdateEventType eventType)454 bool EventListenerHandler::CheckEventTypeExist(int32_t slotId, TelephonyUpdateEventType eventType)
455 {
456 for (auto &listen : listenerList_) {
457 if (slotId == listen.slotId && eventType == listen.eventType) {
458 return true;
459 }
460 }
461 return false;
462 }
463
RemoveEventListenerRegister(napi_env env,TelephonyUpdateEventType eventType,napi_ref ref,std::list<EventListener> & removeListenerList,std::set<int32_t> & soltIdSet)464 void EventListenerHandler::RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType, napi_ref ref,
465 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet)
466 {
467 std::list<EventListener>::iterator it = listenerList_.begin();
468 while (it != listenerList_.end()) {
469 if (env == it->env && eventType == it->eventType && IsCallBackRegister(env, ref, it->callbackRef)) {
470 SetEventListenerDeleting(it->isDeleting);
471 soltIdSet.insert(it->slotId);
472 removeListenerList.push_back(*it);
473 it = listenerList_.erase(it);
474 } else {
475 ++it;
476 }
477 }
478 }
479
RemoveEventListenerRegister(napi_env env,TelephonyUpdateEventType eventType,std::list<EventListener> & removeListenerList,std::set<int32_t> & soltIdSet)480 void EventListenerHandler::RemoveEventListenerRegister(napi_env env, TelephonyUpdateEventType eventType,
481 std::list<EventListener> &removeListenerList, std::set<int32_t> &soltIdSet)
482 {
483 std::list<EventListener>::iterator it = listenerList_.begin();
484 while (it != listenerList_.end()) {
485 if (env == it->env && eventType == it->eventType) {
486 SetEventListenerDeleting(it->isDeleting);
487 soltIdSet.insert(it->slotId);
488 removeListenerList.push_back(*it);
489 it = listenerList_.erase(it);
490 } else {
491 ++it;
492 }
493 }
494 }
495
CheckRemoveStateObserver(TelephonyUpdateEventType eventType,int32_t slotId,int32_t & result)496 void EventListenerHandler::CheckRemoveStateObserver(TelephonyUpdateEventType eventType, int32_t slotId, int32_t &result)
497 {
498 if (!CheckEventTypeExist(slotId, eventType)) {
499 int32_t removeRet = TelephonyStateManager::RemoveStateObserver(slotId, ToUint32t(eventType));
500 if (removeRet != TELEPHONY_SUCCESS) {
501 TELEPHONY_LOGE("EventListenerHandler::RemoveStateObserver slotId %{public}d, eventType %{public}d fail!",
502 slotId, static_cast<int32_t>(eventType));
503 result = removeRet;
504 }
505 }
506 }
507
UnregisterEventListener(napi_env env,TelephonyUpdateEventType eventType,napi_ref ref,std::list<EventListener> & removeListenerList)508 int32_t EventListenerHandler::UnregisterEventListener(
509 napi_env env, TelephonyUpdateEventType eventType, napi_ref ref, std::list<EventListener> &removeListenerList)
510 {
511 std::unique_lock<std::mutex> lock(operatorMutex_);
512 if (listenerList_.empty()) {
513 TELEPHONY_LOGI("UnregisterEventListener listener list is empty.");
514 return TELEPHONY_SUCCESS;
515 }
516
517 std::set<int32_t> soltIdSet;
518 RemoveEventListenerRegister(env, eventType, ref, removeListenerList, soltIdSet);
519 int32_t result = TELEPHONY_SUCCESS;
520 for (int32_t slotId : soltIdSet) {
521 CheckRemoveStateObserver(eventType, slotId, result);
522 }
523 TELEPHONY_LOGI("EventListenerHandler::UnregisterEventListener listenerList_ size=%{public}d",
524 static_cast<int32_t>(listenerList_.size()));
525 return result;
526 }
527
UnregisterEventListener(napi_env env,TelephonyUpdateEventType eventType,std::list<EventListener> & removeListenerList)528 int32_t EventListenerHandler::UnregisterEventListener(
529 napi_env env, TelephonyUpdateEventType eventType, std::list<EventListener> &removeListenerList)
530 {
531 std::unique_lock<std::mutex> lock(operatorMutex_);
532 if (listenerList_.empty()) {
533 TELEPHONY_LOGI("UnregisterEventListener listener list is empty.");
534 return TELEPHONY_SUCCESS;
535 }
536
537 std::set<int32_t> soltIdSet;
538 RemoveEventListenerRegister(env, eventType, removeListenerList, soltIdSet);
539 int32_t result = TELEPHONY_SUCCESS;
540 for (int32_t slotId : soltIdSet) {
541 CheckRemoveStateObserver(eventType, slotId, result);
542 }
543 TELEPHONY_LOGI("EventListenerHandler::UnregisterEventListener listenerList_ size=%{public}d",
544 static_cast<int32_t>(listenerList_.size()));
545 return result;
546 }
547
UnRegisterAllListener(napi_env env)548 void EventListenerHandler::UnRegisterAllListener(napi_env env)
549 {
550 std::unique_lock<std::mutex> lock(operatorMutex_);
551 if (listenerList_.empty()) {
552 TELEPHONY_LOGI("UnRegisterAllListener listener list is empty.");
553 return;
554 } else {
555 TELEPHONY_LOGI("UnRegisterAllListener listener list size start: %{public}d",
556 static_cast<int32_t>(listenerList_.size()));
557 }
558 std::map<int32_t, std::set<TelephonyUpdateEventType>> removeTypeMap;
559 listenerList_.remove_if([&](EventListener listener) -> bool {
560 bool matched = listener.env == env;
561 if (matched) {
562 SetEventListenerDeleting(listener.isDeleting);
563 if (!removeTypeMap.count(listener.slotId)) {
564 std::set<TelephonyUpdateEventType> eventTypeSet;
565 eventTypeSet.insert(listener.eventType);
566 removeTypeMap.insert(std::make_pair(listener.slotId, eventTypeSet));
567 } else {
568 removeTypeMap[listener.slotId].insert(listener.eventType);
569 }
570 if (listener.env != nullptr && listener.callbackRef != nullptr) {
571 napi_delete_reference(listener.env, listener.callbackRef);
572 listener.callbackRef = nullptr;
573 }
574 }
575
576 return matched;
577 });
578
579 int32_t result = TELEPHONY_SUCCESS;
580 for (auto &elem : removeTypeMap) {
581 for (auto &innerElem : elem.second) {
582 CheckRemoveStateObserver(innerElem, elem.first, result);
583 }
584 }
585 TELEPHONY_LOGI(
586 "UnRegisterAllListener listener list size finish: %{public}d", static_cast<int32_t>(listenerList_.size()));
587 }
588
IsCallBackRegister(napi_env env,napi_ref ref,napi_ref registeredRef) const589 bool EventListenerHandler::IsCallBackRegister(napi_env env, napi_ref ref, napi_ref registeredRef) const
590 {
591 napi_value callback = nullptr;
592 napi_get_reference_value(env, ref, &callback);
593 napi_value existCallBack = nullptr;
594 napi_get_reference_value(env, registeredRef, &existCallBack);
595 bool result = false;
596 napi_strict_equals(env, callback, existCallBack, &result);
597 return result;
598 }
599
600 template<typename T, typename D, TelephonyUpdateEventType eventType>
HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer & event)601 void EventListenerHandler::HandleCallbackInfoUpdate(const AppExecFwk::InnerEvent::Pointer &event)
602 {
603 if (event == nullptr) {
604 TELEPHONY_LOGE("event nullptr");
605 return;
606 }
607
608 std::unique_ptr<D> info = event->GetUniqueObject<D>();
609 if (info == nullptr) {
610 TELEPHONY_LOGE("update info nullptr");
611 return;
612 }
613
614 std::unique_lock<std::mutex> lock(operatorMutex_);
615 for (const EventListener &listen : listenerList_) {
616 if ((listen.eventType == eventType) && (listen.slotId == info->slotId_)) {
617 uv_loop_s *loop = nullptr;
618 if (!InitLoop(listen.env, &loop)) {
619 TELEPHONY_LOGE("loop is null");
620 break;
621 }
622 T *context = std::make_unique<T>().release();
623 if (context == nullptr) {
624 TELEPHONY_LOGE("make context failed");
625 break;
626 }
627 *(static_cast<EventListener *>(context)) = listen;
628 *context = *info;
629 uv_work_t *work = std::make_unique<uv_work_t>().release();
630 if (work == nullptr) {
631 TELEPHONY_LOGE("make work failed");
632 break;
633 }
634 work->data = static_cast<void *>(context);
635 int32_t resultCode =
636 uv_queue_work_with_qos(loop, work, [](uv_work_t *) {}, WorkUpdated, uv_qos_default);
637 if (resultCode != 0) {
638 delete context;
639 context = nullptr;
640 TELEPHONY_LOGE("HandleCallbackInfoUpdate failed, result: %{public}d", resultCode);
641 delete work;
642 work = nullptr;
643 return;
644 }
645 }
646 }
647 }
648
649 template<TelephonyUpdateEventType eventType>
HandleCallbackVoidUpdate(const AppExecFwk::InnerEvent::Pointer & event)650 void EventListenerHandler::HandleCallbackVoidUpdate(const AppExecFwk::InnerEvent::Pointer &event)
651 {
652 if (event == nullptr) {
653 TELEPHONY_LOGE("event nullptr");
654 return;
655 }
656
657 std::unique_lock<std::mutex> lock(operatorMutex_);
658 for (const EventListener &listen : listenerList_) {
659 if ((listen.eventType == eventType)) {
660 uv_loop_s *loop = nullptr;
661 if (!InitLoop(listen.env, &loop)) {
662 TELEPHONY_LOGE("loop is null");
663 break;
664 }
665 uv_work_t *work = std::make_unique<uv_work_t>().release();
666 if (work == nullptr) {
667 TELEPHONY_LOGE("make work failed");
668 break;
669 }
670 EventListener *listener = new EventListener();
671 listener->env = listen.env;
672 listener->eventType = listen.eventType;
673 listener->slotId = listen.slotId;
674 listener->callbackRef = listen.callbackRef;
675 listener->isDeleting = listen.isDeleting;
676 work->data = static_cast<void *>(listener);
677 int32_t retVal =
678 uv_queue_work_with_qos(loop, work, [](uv_work_t *) {}, WorkUpdated, uv_qos_default);
679 if (retVal != 0) {
680 delete listener;
681 listener = nullptr;
682 TELEPHONY_LOGE("HandleCallbackVoidUpdate failed, result: %{public}d", retVal);
683 delete work;
684 work = nullptr;
685 return;
686 }
687 }
688 }
689 }
690
WorkUpdated(uv_work_t * work,int status)691 void EventListenerHandler::WorkUpdated(uv_work_t *work, int status)
692 {
693 std::unique_lock<std::mutex> lock(operatorMutex_);
694 EventListener *listener = static_cast<EventListener *>(work->data);
695 if (listener->eventType == TelephonyUpdateEventType::EVENT_SIGNAL_STRENGTHS_UPDATE) {
696 TELEPHONY_LOGD("WorkUpdated eventType is %{public}d", listener->eventType);
697 } else {
698 TELEPHONY_LOGI("WorkUpdated eventType is %{public}d", listener->eventType);
699 }
700 if (listener->isDeleting == nullptr || *(listener->isDeleting)) {
701 TELEPHONY_LOGI("listener is deleting");
702 delete listener;
703 listener = nullptr;
704 return;
705 }
706 if (workFuncMap_.find(listener->eventType) == workFuncMap_.end() ||
707 workFuncMap_.find(listener->eventType)->second == nullptr) {
708 TELEPHONY_LOGE("listener state update is nullptr");
709 delete listener;
710 listener = nullptr;
711 return;
712 }
713 workFuncMap_.find(listener->eventType)->second(work, lock);
714 }
715
WorkCallStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)716 void EventListenerHandler::WorkCallStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
717 {
718 std::unique_ptr<CallStateContext> callStateInfo(static_cast<CallStateContext *>(work->data));
719 const napi_env &env = callStateInfo->env;
720 napi_handle_scope scope = nullptr;
721 napi_open_handle_scope(env, &scope);
722 if (scope == nullptr) {
723 TELEPHONY_LOGE("scope is nullptr");
724 }
725 napi_value callbackValue = nullptr;
726 napi_create_object(callStateInfo->env, &callbackValue);
727 int32_t wrappedCallState = WrapCallState(callStateInfo->callState);
728 std::string number = NapiUtil::ToUtf8(callStateInfo->phoneNumber);
729 SetPropertyToNapiObject(callStateInfo->env, callbackValue, "state", wrappedCallState);
730 SetPropertyToNapiObject(callStateInfo->env, callbackValue, "number", number);
731 NapiReturnToJS(callStateInfo->env, callStateInfo->callbackRef, callbackValue, lock);
732 napi_close_handle_scope(env, scope);
733 }
734
WorkSignalUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)735 void EventListenerHandler::WorkSignalUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
736 {
737 napi_handle_scope scope = nullptr;
738 std::unique_ptr<SignalListContext> infoListUpdateInfo(static_cast<SignalListContext *>(work->data));
739 napi_value callbackValue = nullptr;
740 const napi_env &env = infoListUpdateInfo->env;
741 napi_open_handle_scope(env, &scope);
742 if (scope == nullptr) {
743 TELEPHONY_LOGE("scope is nullptr");
744 }
745 napi_create_array(env, &callbackValue);
746 size_t infoSize = infoListUpdateInfo->signalInfoList.size();
747 for (size_t i = 0; i < infoSize; ++i) {
748 sptr<SignalInformation> infoItem = infoListUpdateInfo->signalInfoList[i];
749 napi_value info = nullptr;
750 napi_create_object(env, &info);
751 SetPropertyToNapiObject(env, info, "signalType", WrapNetworkType(infoItem->GetNetworkType()));
752 SetPropertyToNapiObject(env, info, "signalLevel", infoItem->GetSignalLevel());
753 SetPropertyToNapiObject(env, info, "dBm", infoItem->GetSignalIntensity());
754 napi_set_element(env, callbackValue, i, info);
755 }
756 NapiReturnToJS(env, infoListUpdateInfo->callbackRef, callbackValue, lock);
757 napi_close_handle_scope(env, scope);
758 }
759
WorkNetworkStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)760 void EventListenerHandler::WorkNetworkStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
761 {
762 napi_handle_scope scope = nullptr;
763 std::unique_ptr<NetworkStateContext> networkStateUpdateInfo(static_cast<NetworkStateContext *>(work->data));
764 napi_value callbackValue = nullptr;
765 const napi_env &env = networkStateUpdateInfo->env;
766 napi_open_handle_scope(env, &scope);
767 if (scope == nullptr) {
768 TELEPHONY_LOGE("scope is nullptr");
769 }
770 const sptr<NetworkState> &networkState = networkStateUpdateInfo->networkState;
771 napi_create_object(env, &callbackValue);
772 std::string longOperatorName = networkState->GetLongOperatorName();
773 std::string shortOperatorName = networkState->GetShortOperatorName();
774 std::string plmnNumeric = networkState->GetPlmnNumeric();
775 bool isRoaming = networkState->IsRoaming();
776 int32_t regStatus = static_cast<int32_t>(networkState->GetRegStatus());
777 bool isEmergency = networkState->IsEmergency();
778 int32_t cfgTech = static_cast<int32_t>(networkState->GetCfgTech());
779 int32_t nsaState = static_cast<int32_t>(networkState->GetNrState());
780 SetPropertyToNapiObject(env, callbackValue, "longOperatorName", longOperatorName);
781 SetPropertyToNapiObject(env, callbackValue, "shortOperatorName", shortOperatorName);
782 SetPropertyToNapiObject(env, callbackValue, "plmnNumeric", plmnNumeric);
783 SetPropertyToNapiObject(env, callbackValue, "isRoaming", isRoaming);
784 SetPropertyToNapiObject(env, callbackValue, "regState", WrapRegState(regStatus));
785 SetPropertyToNapiObject(env, callbackValue, "isEmergency", isEmergency);
786 SetPropertyToNapiObject(env, callbackValue, "cfgTech", WrapRadioTech(cfgTech));
787 SetPropertyToNapiObject(env, callbackValue, "nsaState", nsaState);
788 SetPropertyToNapiObject(env, callbackValue, "isCaActive", false);
789 NapiReturnToJS(env, networkStateUpdateInfo->callbackRef, callbackValue, lock);
790 napi_close_handle_scope(env, scope);
791 }
792
WorkSimStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)793 void EventListenerHandler::WorkSimStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
794 {
795 napi_handle_scope scope = nullptr;
796 std::unique_ptr<SimStateContext> simStateUpdateInfo(static_cast<SimStateContext *>(work->data));
797 const napi_env &env = simStateUpdateInfo->env;
798 napi_open_handle_scope(env, &scope);
799 if (scope == nullptr) {
800 TELEPHONY_LOGE("scope is nullptr");
801 }
802 napi_value callbackValue = nullptr;
803 int32_t cardType = static_cast<int32_t>(simStateUpdateInfo->cardType);
804 int32_t simState = static_cast<int32_t>(simStateUpdateInfo->simState);
805 int32_t lockReason = static_cast<int32_t>(simStateUpdateInfo->reason);
806 napi_create_object(simStateUpdateInfo->env, &callbackValue);
807 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "type", cardType);
808 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "state", simState);
809 SetPropertyToNapiObject(simStateUpdateInfo->env, callbackValue, "reason", lockReason);
810 NapiReturnToJS(simStateUpdateInfo->env, simStateUpdateInfo->callbackRef, callbackValue, lock);
811 napi_close_handle_scope(env, scope);
812 }
813
WorkCellInfomationUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)814 void EventListenerHandler::WorkCellInfomationUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
815 {
816 std::unique_ptr<CellInfomationContext> cellInfo(static_cast<CellInfomationContext *>(work->data));
817 napi_value callbackValue = nullptr;
818 const napi_env &env = cellInfo->env;
819 napi_handle_scope scope = nullptr;
820 napi_open_handle_scope(env, &scope);
821 if (scope == nullptr) {
822 TELEPHONY_LOGE("scope is nullptr");
823 }
824 napi_create_array(cellInfo->env, &callbackValue);
825 for (size_t i = 0; i < cellInfo->cellInfoVec.size(); i++) {
826 napi_value val = CellInfoConversion(cellInfo->env, *(cellInfo->cellInfoVec[i]));
827 napi_set_element(cellInfo->env, callbackValue, i, val);
828 }
829 NapiReturnToJS(cellInfo->env, cellInfo->callbackRef, callbackValue, lock);
830 napi_close_handle_scope(env, scope);
831 }
832
WorkCellularDataConnectStateUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)833 void EventListenerHandler::WorkCellularDataConnectStateUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
834 {
835 napi_handle_scope scope = nullptr;
836 std::unique_ptr<CellularDataConnectStateContext> context(
837 static_cast<CellularDataConnectStateContext *>(work->data));
838 const napi_env &env = context->env;
839 napi_open_handle_scope(env, &scope);
840 if (scope == nullptr) {
841 TELEPHONY_LOGE("scope is nullptr");
842 }
843 napi_value callbackValue = nullptr;
844 napi_create_object(context->env, &callbackValue);
845 SetPropertyToNapiObject(context->env, callbackValue, "state", context->dataState);
846 SetPropertyToNapiObject(context->env, callbackValue, "network", context->networkType);
847 NapiReturnToJS(context->env, context->callbackRef, callbackValue, lock);
848 napi_close_handle_scope(env, scope);
849 }
850
WorkCellularDataFlowUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)851 void EventListenerHandler::WorkCellularDataFlowUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
852 {
853 std::unique_ptr<CellularDataFlowContext> dataFlowInfo(static_cast<CellularDataFlowContext *>(work->data));
854 const napi_env &env = dataFlowInfo->env;
855 napi_handle_scope scope = nullptr;
856 napi_open_handle_scope(env, &scope);
857 if (scope == nullptr) {
858 TELEPHONY_LOGE("scope is nullptr");
859 }
860 napi_value callbackValue = GetNapiValue(dataFlowInfo->env, dataFlowInfo->flowType_);
861 NapiReturnToJS(dataFlowInfo->env, dataFlowInfo->callbackRef, callbackValue, lock);
862 napi_close_handle_scope(env, scope);
863 }
864
WorkCfuIndicatorUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)865 void EventListenerHandler::WorkCfuIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
866 {
867 if (work == nullptr) {
868 TELEPHONY_LOGE("work is null");
869 return;
870 }
871 std::unique_ptr<CfuIndicatorContext> cfuIndicatorInfo(static_cast<CfuIndicatorContext *>(work->data));
872 const napi_env &env = cfuIndicatorInfo->env;
873 napi_handle_scope scope = nullptr;
874 napi_open_handle_scope(env, &scope);
875 if (scope == nullptr) {
876 TELEPHONY_LOGE("scope is nullptr");
877 }
878 napi_value callbackValue = GetNapiValue(cfuIndicatorInfo->env, cfuIndicatorInfo->cfuResult_);
879 NapiReturnToJS(cfuIndicatorInfo->env, cfuIndicatorInfo->callbackRef, callbackValue, lock);
880 napi_close_handle_scope(env, scope);
881 }
882
WorkVoiceMailMsgIndicatorUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)883 void EventListenerHandler::WorkVoiceMailMsgIndicatorUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
884 {
885 if (work == nullptr) {
886 TELEPHONY_LOGE("work is null");
887 return;
888 }
889 std::unique_ptr<VoiceMailMsgIndicatorContext> voiceMailMsgIndicatorInfo(
890 static_cast<VoiceMailMsgIndicatorContext *>(work->data));
891 const napi_env &env = voiceMailMsgIndicatorInfo->env;
892 napi_handle_scope scope = nullptr;
893 napi_open_handle_scope(env, &scope);
894 if (scope == nullptr) {
895 TELEPHONY_LOGE("scope is nullptr");
896 }
897 napi_value callbackValue =
898 GetNapiValue(voiceMailMsgIndicatorInfo->env, voiceMailMsgIndicatorInfo->voiceMailMsgResult_);
899 NapiReturnToJS(voiceMailMsgIndicatorInfo->env, voiceMailMsgIndicatorInfo->callbackRef, callbackValue, lock);
900 napi_close_handle_scope(env, scope);
901 }
902
WorkIccAccountUpdated(uv_work_t * work,std::unique_lock<std::mutex> & lock)903 void EventListenerHandler::WorkIccAccountUpdated(uv_work_t *work, std::unique_lock<std::mutex> &lock)
904 {
905 if (work == nullptr) {
906 TELEPHONY_LOGE("work is null");
907 return;
908 }
909 std::unique_ptr<EventListener> UpdateIccAccount(static_cast<EventListener *>(work->data));
910 const napi_env &env = UpdateIccAccount->env;
911 napi_handle_scope scope = nullptr;
912 napi_open_handle_scope(env, &scope);
913 if (scope == nullptr) {
914 TELEPHONY_LOGE("scope is nullptr");
915 }
916 napi_value callbackValue = nullptr;
917 napi_create_object(UpdateIccAccount->env, &callbackValue);
918 NapiReturnToJS(UpdateIccAccount->env, UpdateIccAccount->callbackRef, callbackValue, lock);
919 napi_close_handle_scope(env, scope);
920 }
921 } // namespace Telephony
922 } // namespace OHOS
923