• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 
16 #include "napi_call_ability_callback.h"
17 
18 #include <securec.h>
19 #include <ctime>
20 
21 #include "call_manager_errors.h"
22 #include "napi_call_manager_utils.h"
23 #include "napi_util.h"
24 #include "pixel_map.h"
25 #include "pixel_map_napi.h"
26 #include "telephony_log_wrapper.h"
27 #include "napi_common_want.h"
28 #include "want_params_wrapper.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 EventCallback NapiCallAbilityCallback::audioDeviceCallback_;
33 std::mutex NapiCallAbilityCallback::audioDeviceCallbackMutex_;
NapiCallAbilityCallback()34 NapiCallAbilityCallback::NapiCallAbilityCallback()
35 {
36     (void)memset_s(&stateCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
37     (void)memset_s(&meeTimeStateCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
38     (void)memset_s(&eventCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
39     (void)memset_s(&ottRequestCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
40     (void)memset_s(&setRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
41     (void)memset_s(&startRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
42     (void)memset_s(&stopRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
43     (void)memset_s(&callDisconnectCauseCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
44     (void)memset_s(&mmiCodeCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
45     (void)memset_s(&postDialDelayCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
46     (void)memset_s(&imsCallModeCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
47     (void)memset_s(&peerDimensionsCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
48     (void)memset_s(&callDataUsageCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
49     (void)memset_s(&cameraCapabilitiesCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
50     (void)memset_s(&callSessionEventCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
51     memberFuncMap_[CallResultReportId::GET_CALL_WAITING_REPORT_ID] =
52         [this](AppExecFwk::PacMap &resultInfo) { return ReportGetWaitingInfo(resultInfo); };
53     memberFuncMap_[CallResultReportId::SET_CALL_WAITING_REPORT_ID] =
54         [this](AppExecFwk::PacMap &resultInfo) { return ReportSetWaitingInfo(resultInfo); };
55     memberFuncMap_[CallResultReportId::GET_CALL_RESTRICTION_REPORT_ID] =
56         [this](AppExecFwk::PacMap &resultInfo) { return ReportGetRestrictionInfo(resultInfo); };
57     memberFuncMap_[CallResultReportId::SET_CALL_RESTRICTION_REPORT_ID] =
58         [this](AppExecFwk::PacMap &resultInfo) { return ReportSetRestrictionInfo(resultInfo); };
59     memberFuncMap_[CallResultReportId::SET_CALL_RESTRICTION_PWD_REPORT_ID] =
60         [this](AppExecFwk::PacMap &resultInfo) { return ReportSetRestrictionPassword(resultInfo); };
61     memberFuncMap_[CallResultReportId::GET_CALL_TRANSFER_REPORT_ID] =
62         [this](AppExecFwk::PacMap &resultInfo) { return ReportGetTransferInfo(resultInfo); };
63     memberFuncMap_[CallResultReportId::SET_CALL_TRANSFER_REPORT_ID] =
64         [this](AppExecFwk::PacMap &resultInfo) { return ReportSetTransferInfo(resultInfo); };
65     memberFuncMap_[CallResultReportId::START_RTT_REPORT_ID] =
66         [this](AppExecFwk::PacMap &resultInfo) { return ReportStartRttInfo(resultInfo); };
67     memberFuncMap_[CallResultReportId::STOP_RTT_REPORT_ID] =
68         [this](AppExecFwk::PacMap &resultInfo) { return ReportStopRttInfo(resultInfo); };
69     memberFuncMap_[CallResultReportId::CLOSE_UNFINISHED_USSD_REPORT_ID] =
70         [this](AppExecFwk::PacMap &resultInfo) { return ReportCloseUnFinishedUssdInfo(resultInfo); };
71     UnRegisterGetWaitingCallback();
72     UnRegisterSetWaitingCallback();
73     UnRegisterGetRestrictionCallback();
74     UnRegisterSetRestrictionPasswordCallback();
75     UnRegisterGetTransferCallback();
76     UnRegisterSetTransferCallback();
77 }
78 
~NapiCallAbilityCallback()79 NapiCallAbilityCallback::~NapiCallAbilityCallback()
80 {
81     TELEPHONY_LOGI("~NapiCallAbilityCallback");
82 }
83 
RegisterCallStateCallback(EventCallback stateCallback)84 void NapiCallAbilityCallback::RegisterCallStateCallback(EventCallback stateCallback)
85 {
86     stateCallback_ = stateCallback;
87 }
88 
UnRegisterCallStateCallback()89 void NapiCallAbilityCallback::UnRegisterCallStateCallback()
90 {
91     if (stateCallback_.callbackRef) {
92         (void)memset_s(&stateCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
93     }
94 }
95 
RegisterMmiCodeCallback(EventCallback eventCallback)96 void NapiCallAbilityCallback::RegisterMmiCodeCallback(EventCallback eventCallback)
97 {
98     mmiCodeCallback_ = eventCallback;
99 }
100 
UnRegisterMmiCodeCallback()101 void NapiCallAbilityCallback::UnRegisterMmiCodeCallback()
102 {
103     if (mmiCodeCallback_.callbackRef) {
104         (void)memset_s(&mmiCodeCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
105     }
106 }
107 
RegisterAudioDeviceCallback(EventCallback eventCallback)108 void NapiCallAbilityCallback::RegisterAudioDeviceCallback(EventCallback eventCallback)
109 {
110     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
111     audioDeviceCallback_ = eventCallback;
112 }
113 
UnRegisterAudioDeviceCallback()114 void NapiCallAbilityCallback::UnRegisterAudioDeviceCallback()
115 {
116     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
117     if (audioDeviceCallback_.callbackRef) {
118         napi_delete_reference(audioDeviceCallback_.env, audioDeviceCallback_.callbackRef);
119         napi_delete_reference(audioDeviceCallback_.env, audioDeviceCallback_.thisVar);
120         (void)memset_s(&audioDeviceCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
121         TELEPHONY_LOGI("unregister audio device callback end.");
122     }
123 }
124 
RegisterPostDialDelay(EventCallback eventCallback)125 void NapiCallAbilityCallback::RegisterPostDialDelay(EventCallback eventCallback)
126 {
127     postDialDelayCallback_ = eventCallback;
128 }
129 
UnRegisterPostDialDelayCallback()130 void NapiCallAbilityCallback::UnRegisterPostDialDelayCallback()
131 {
132     if (postDialDelayCallback_.callbackRef) {
133         napi_delete_reference(postDialDelayCallback_.env, postDialDelayCallback_.callbackRef);
134         napi_delete_reference(postDialDelayCallback_.env, postDialDelayCallback_.thisVar);
135         (void)memset_s(&postDialDelayCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
136     }
137 }
138 
RegisterCallEventCallback(EventCallback eventCallback)139 void NapiCallAbilityCallback::RegisterCallEventCallback(EventCallback eventCallback)
140 {
141     eventCallback_ = eventCallback;
142 }
143 
UnRegisterCallEventCallback()144 void NapiCallAbilityCallback::UnRegisterCallEventCallback()
145 {
146     if (eventCallback_.callbackRef) {
147         (void)memset_s(&eventCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
148     }
149 }
150 
RegisterImsCallModeChangeCallback(EventCallback eventCallback)151 void NapiCallAbilityCallback::RegisterImsCallModeChangeCallback(EventCallback eventCallback)
152 {
153     imsCallModeCallback_ = eventCallback;
154 }
155 
UnRegisterImsCallModeChangeCallback()156 void NapiCallAbilityCallback::UnRegisterImsCallModeChangeCallback()
157 {
158     if (eventCallback_.callbackRef) {
159         (void)memset_s(&imsCallModeCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
160     }
161 }
162 
RegisterPeerDimensionsChangeCallback(EventCallback eventCallback)163 void NapiCallAbilityCallback::RegisterPeerDimensionsChangeCallback(EventCallback eventCallback)
164 {
165     peerDimensionsCallback_ = eventCallback;
166 }
167 
UnRegisterPeerDimensionsChangeCallback()168 void NapiCallAbilityCallback::UnRegisterPeerDimensionsChangeCallback()
169 {
170     if (eventCallback_.callbackRef) {
171         (void)memset_s(&peerDimensionsCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
172     }
173 }
174 
RegisterCallDataUsageChangeCallback(EventCallback eventCallback)175 void NapiCallAbilityCallback::RegisterCallDataUsageChangeCallback(EventCallback eventCallback)
176 {
177     callDataUsageCallback_ = eventCallback;
178 }
179 
UnRegisterCallDataUsageChangeCallback()180 void NapiCallAbilityCallback::UnRegisterCallDataUsageChangeCallback()
181 {
182     if (eventCallback_.callbackRef) {
183         (void)memset_s(&callDataUsageCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
184     }
185 }
186 
RegisterCameraCapabilitiesChangeCallback(EventCallback eventCallback)187 void NapiCallAbilityCallback::RegisterCameraCapabilitiesChangeCallback(EventCallback eventCallback)
188 {
189     cameraCapabilitiesCallback_ = eventCallback;
190 }
191 
UnRegisterCameraCapabilitiesChangeCallback()192 void NapiCallAbilityCallback::UnRegisterCameraCapabilitiesChangeCallback()
193 {
194     if (eventCallback_.callbackRef) {
195         (void)memset_s(&cameraCapabilitiesCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
196     }
197 }
198 
RegisterCallSessionEventChangeCallback(EventCallback eventCallback)199 void NapiCallAbilityCallback::RegisterCallSessionEventChangeCallback(EventCallback eventCallback)
200 {
201     callSessionEventCallback_ = eventCallback;
202 }
203 
UnRegisterCallSessionEventChangeCallback()204 void NapiCallAbilityCallback::UnRegisterCallSessionEventChangeCallback()
205 {
206     if (eventCallback_.callbackRef) {
207         (void)memset_s(&callSessionEventCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
208     }
209 }
210 
RegisterDisconnectedCauseCallback(EventCallback eventCallback)211 void NapiCallAbilityCallback::RegisterDisconnectedCauseCallback(EventCallback eventCallback)
212 {
213     callDisconnectCauseCallback_ = eventCallback;
214 }
215 
UnRegisterDisconnectedCauseCallback()216 void NapiCallAbilityCallback::UnRegisterDisconnectedCauseCallback()
217 {
218     if (callDisconnectCauseCallback_.callbackRef) {
219         (void)memset_s(&callDisconnectCauseCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
220     }
221 }
222 
RegisterCallOttRequestCallback(EventCallback ottRequestCallback)223 void NapiCallAbilityCallback::RegisterCallOttRequestCallback(EventCallback ottRequestCallback)
224 {
225     ottRequestCallback_ = ottRequestCallback;
226 }
227 
UnRegisterCallOttRequestCallback()228 void NapiCallAbilityCallback::UnRegisterCallOttRequestCallback()
229 {
230     if (ottRequestCallback_.callbackRef) {
231         (void)memset_s(&ottRequestCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
232     }
233 }
234 
RegisterGetWaitingCallback(EventCallback callback)235 int32_t NapiCallAbilityCallback::RegisterGetWaitingCallback(EventCallback callback)
236 {
237     std::lock_guard<std::mutex> lock(getWaitingCallbackMutex_);
238     if (getWaitingCallback_.thisVar) {
239         TELEPHONY_LOGE("callback already exist!");
240         return CALL_ERR_CALLBACK_ALREADY_EXIST;
241     }
242     getWaitingCallback_ = callback;
243     return TELEPHONY_SUCCESS;
244 }
245 
UnRegisterGetWaitingCallback()246 void NapiCallAbilityCallback::UnRegisterGetWaitingCallback()
247 {
248     std::lock_guard<std::mutex> lock(getWaitingCallbackMutex_);
249     (void)memset_s(&getWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
250 }
251 
RegisterCloseUnFinishedUssdCallback(EventCallback callback)252 int32_t NapiCallAbilityCallback::RegisterCloseUnFinishedUssdCallback(EventCallback callback)
253 {
254     std::lock_guard<std::mutex> lock(closeUnfinishedUssdCallbackMutex_);
255     if (closeUnfinishedUssdCallback_.thisVar) {
256         TELEPHONY_LOGE("callback already exist!");
257         return CALL_ERR_CALLBACK_ALREADY_EXIST;
258     }
259     closeUnfinishedUssdCallback_ = callback;
260     return TELEPHONY_SUCCESS;
261 }
262 
UnRegisterCloseUnFinishedUssdCallback()263 void NapiCallAbilityCallback::UnRegisterCloseUnFinishedUssdCallback()
264 {
265     std::lock_guard<std::mutex> lock(closeUnfinishedUssdCallbackMutex_);
266     (void)memset_s(&closeUnfinishedUssdCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
267 }
268 
RegisterSetWaitingCallback(EventCallback callback)269 int32_t NapiCallAbilityCallback::RegisterSetWaitingCallback(EventCallback callback)
270 {
271     std::lock_guard<std::mutex> lock(setWaitingCallbackMutex_);
272     if (setWaitingCallback_.thisVar) {
273         TELEPHONY_LOGE("callback already exist!");
274         return CALL_ERR_CALLBACK_ALREADY_EXIST;
275     }
276     setWaitingCallback_ = callback;
277     return TELEPHONY_SUCCESS;
278 }
279 
UnRegisterSetWaitingCallback()280 void NapiCallAbilityCallback::UnRegisterSetWaitingCallback()
281 {
282     std::lock_guard<std::mutex> lock(setWaitingCallbackMutex_);
283     (void)memset_s(&setWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
284 }
285 
RegisterGetRestrictionCallback(EventCallback callback)286 int32_t NapiCallAbilityCallback::RegisterGetRestrictionCallback(EventCallback callback)
287 {
288     std::lock_guard<std::mutex> lock(getRestrictionCallbackMutex_);
289     if (getRestrictionCallback_.thisVar) {
290         TELEPHONY_LOGE("callback already exist!");
291         return CALL_ERR_CALLBACK_ALREADY_EXIST;
292     }
293     getRestrictionCallback_ = callback;
294     return TELEPHONY_SUCCESS;
295 }
296 
UnRegisterGetRestrictionCallback()297 void NapiCallAbilityCallback::UnRegisterGetRestrictionCallback()
298 {
299     std::lock_guard<std::mutex> lock(getRestrictionCallbackMutex_);
300     (void)memset_s(&getRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
301 }
302 
RegisterSetRestrictionCallback(EventCallback callback)303 int32_t NapiCallAbilityCallback::RegisterSetRestrictionCallback(EventCallback callback)
304 {
305     if (setRestrictionCallback_.thisVar) {
306         TELEPHONY_LOGE("callback already exist!");
307         return CALL_ERR_CALLBACK_ALREADY_EXIST;
308     }
309     setRestrictionCallback_ = callback;
310     return TELEPHONY_SUCCESS;
311 }
312 
UnRegisterSetRestrictionCallback()313 void NapiCallAbilityCallback::UnRegisterSetRestrictionCallback()
314 {
315     (void)memset_s(&setRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
316 }
317 
RegisterSetRestrictionPasswordCallback(EventCallback callback)318 int32_t NapiCallAbilityCallback::RegisterSetRestrictionPasswordCallback(EventCallback callback)
319 {
320     std::lock_guard<std::mutex> lock(setRestrictionPasswordCallbackMutex_);
321     if (setRestrictionPasswordCallback_.thisVar) {
322         TELEPHONY_LOGE("callback already exist!");
323         return CALL_ERR_CALLBACK_ALREADY_EXIST;
324     }
325     setRestrictionPasswordCallback_ = callback;
326     return TELEPHONY_SUCCESS;
327 }
328 
UnRegisterSetRestrictionPasswordCallback()329 void NapiCallAbilityCallback::UnRegisterSetRestrictionPasswordCallback()
330 {
331     std::lock_guard<std::mutex> lock(setRestrictionPasswordCallbackMutex_);
332     (void)memset_s(&setRestrictionPasswordCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
333 }
334 
RegisterGetTransferCallback(EventCallback callback,int32_t type)335 int32_t NapiCallAbilityCallback::RegisterGetTransferCallback(EventCallback callback, int32_t type)
336 {
337     std::lock_guard<std::mutex> lock(getTransferCallbackMutex_);
338     getTransferCallback_ = callback;
339     getCallTransferReason_ = type;
340     return TELEPHONY_SUCCESS;
341 }
342 
UnRegisterGetTransferCallback()343 void NapiCallAbilityCallback::UnRegisterGetTransferCallback()
344 {
345     std::lock_guard<std::mutex> lock(getTransferCallbackMutex_);
346     (void)memset_s(&getTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
347 }
348 
RegisterSetTransferCallback(EventCallback callback)349 int32_t NapiCallAbilityCallback::RegisterSetTransferCallback(EventCallback callback)
350 {
351     std::lock_guard<std::mutex> lock(setTransferCallbackMutex_);
352     if (setTransferCallback_.thisVar) {
353         TELEPHONY_LOGE("callback already exist!");
354         return CALL_ERR_CALLBACK_ALREADY_EXIST;
355     }
356     setTransferCallback_ = callback;
357     return TELEPHONY_SUCCESS;
358 }
359 
UnRegisterSetTransferCallback()360 void NapiCallAbilityCallback::UnRegisterSetTransferCallback()
361 {
362     std::lock_guard<std::mutex> lock(setTransferCallbackMutex_);
363     (void)memset_s(&setTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
364 }
365 
RegisterStartRttCallback(EventCallback callback)366 int32_t NapiCallAbilityCallback::RegisterStartRttCallback(EventCallback callback)
367 {
368     if (startRttCallback_.thisVar) {
369         TELEPHONY_LOGE("callback already exist!");
370         return CALL_ERR_CALLBACK_ALREADY_EXIST;
371     }
372     startRttCallback_ = callback;
373     return TELEPHONY_SUCCESS;
374 }
375 
UnRegisterStartRttCallback()376 void NapiCallAbilityCallback::UnRegisterStartRttCallback()
377 {
378     (void)memset_s(&startRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
379 }
380 
RegisterStopRttCallback(EventCallback callback)381 int32_t NapiCallAbilityCallback::RegisterStopRttCallback(EventCallback callback)
382 {
383     if (stopRttCallback_.thisVar) {
384         TELEPHONY_LOGE("callback already exist!");
385         return CALL_ERR_CALLBACK_ALREADY_EXIST;
386     }
387     stopRttCallback_ = callback;
388     return TELEPHONY_SUCCESS;
389 }
390 
UnRegisterStopRttCallback()391 void NapiCallAbilityCallback::UnRegisterStopRttCallback()
392 {
393     (void)memset_s(&stopRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
394 }
395 
ReportStartRttInfo(AppExecFwk::PacMap & resultInfo)396 int32_t NapiCallAbilityCallback::ReportStartRttInfo(AppExecFwk::PacMap &resultInfo)
397 {
398     if (startRttCallback_.thisVar == nullptr) {
399         TELEPHONY_LOGE("startRttCallback_ is null!");
400         return CALL_ERR_CALLBACK_NOT_EXIST;
401     }
402     uv_loop_s *loop = nullptr;
403 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
404     napi_get_uv_event_loop(startRttCallback_.env, &loop);
405 #endif
406     if (loop == nullptr) {
407         return TELEPHONY_ERR_LOCAL_PTR_NULL;
408     }
409     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
410     if (callSupplementWorker == nullptr) {
411         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
412         return TELEPHONY_ERR_LOCAL_PTR_NULL;
413     }
414     callSupplementWorker->info = resultInfo;
415     callSupplementWorker->callback = startRttCallback_;
416     uv_work_t *work = std::make_unique<uv_work_t>().release();
417     if (work == nullptr) {
418         delete callSupplementWorker;
419         callSupplementWorker = nullptr;
420         TELEPHONY_LOGE("work is nullptr!");
421         return TELEPHONY_ERR_LOCAL_PTR_NULL;
422     }
423     work->data = (void *)callSupplementWorker;
424     int32_t errCode = uv_queue_work_with_qos(
425         loop, work, [](uv_work_t *work) {
426             TELEPHONY_LOGD("ReportStartRttInfo uv_queue_work_with_qos");
427         }, ReportStartRttInfoWork, uv_qos_default);
428     if (errCode != 0) {
429         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
430         delete callSupplementWorker;
431         callSupplementWorker = nullptr;
432         delete work;
433         work = nullptr;
434         return TELEPHONY_ERROR;
435     }
436     if (startRttCallback_.thisVar) {
437         (void)memset_s(&startRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
438     }
439     return TELEPHONY_SUCCESS;
440 }
441 
ReportStopRttInfo(AppExecFwk::PacMap & resultInfo)442 int32_t NapiCallAbilityCallback::ReportStopRttInfo(AppExecFwk::PacMap &resultInfo)
443 {
444     if (stopRttCallback_.thisVar == nullptr) {
445         TELEPHONY_LOGE("startRttCallback_ is null!");
446         return CALL_ERR_CALLBACK_NOT_EXIST;
447     }
448     uv_loop_s *loop = nullptr;
449 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
450     napi_get_uv_event_loop(stopRttCallback_.env, &loop);
451 #endif
452     if (loop == nullptr) {
453         return TELEPHONY_ERR_LOCAL_PTR_NULL;
454     }
455     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
456     if (callSupplementWorker == nullptr) {
457         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
458         return TELEPHONY_ERR_LOCAL_PTR_NULL;
459     }
460     callSupplementWorker->info = resultInfo;
461     callSupplementWorker->callback = stopRttCallback_;
462     uv_work_t *work = std::make_unique<uv_work_t>().release();
463     if (work == nullptr) {
464         delete callSupplementWorker;
465         callSupplementWorker = nullptr;
466         TELEPHONY_LOGE("work is nullptr!");
467         return TELEPHONY_ERR_LOCAL_PTR_NULL;
468     }
469     work->data = (void *)callSupplementWorker;
470     int32_t resultCode = uv_queue_work_with_qos(
471         loop, work, [](uv_work_t *work) {
472             TELEPHONY_LOGD("ReportStopRttInfo uv_queue_work_with_qos");
473         }, ReportStopRttInfoWork, uv_qos_default);
474     if (resultCode != 0) {
475         delete callSupplementWorker;
476         callSupplementWorker = nullptr;
477         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
478         delete work;
479         work = nullptr;
480         return TELEPHONY_ERROR;
481     }
482     if (stopRttCallback_.thisVar) {
483         (void)memset_s(&stopRttCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
484     }
485     return TELEPHONY_SUCCESS;
486 }
487 
UpdateCallStateInfo(const CallAttributeInfo & info)488 int32_t NapiCallAbilityCallback::UpdateCallStateInfo(const CallAttributeInfo &info)
489 {
490     if (stateCallback_.thisVar == nullptr) {
491         return CALL_ERR_CALLBACK_NOT_EXIST;
492     }
493     uv_loop_s *loop = nullptr;
494 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
495     napi_get_uv_event_loop(stateCallback_.env, &loop);
496 #endif
497     if (loop == nullptr) {
498         return TELEPHONY_ERR_LOCAL_PTR_NULL;
499     }
500     CallStateWorker *callStateWorker = std::make_unique<CallStateWorker>().release();
501     if (callStateWorker == nullptr) {
502         TELEPHONY_LOGE("callStateWorker is nullptr!");
503         return TELEPHONY_ERR_LOCAL_PTR_NULL;
504     }
505     callStateWorker->info = info;
506     callStateWorker->callback = stateCallback_;
507     uv_work_t *work = std::make_unique<uv_work_t>().release();
508     if (work == nullptr) {
509         delete callStateWorker;
510         callStateWorker = nullptr;
511         TELEPHONY_LOGE("work is nullptr!");
512         return TELEPHONY_ERR_LOCAL_PTR_NULL;
513     }
514     work->data = (void *)callStateWorker;
515     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
516         TELEPHONY_LOGD("UpdateCallStateInfo uv_queue_work_with_qos");
517     }, ReportCallStateWork, uv_qos_default);
518     if (errCode != 0) {
519         delete callStateWorker;
520         callStateWorker = nullptr;
521         delete work;
522         work = nullptr;
523         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, errCode: %{public}d", errCode);
524         return TELEPHONY_ERROR;
525     }
526     return TELEPHONY_SUCCESS;
527 }
528 
UpdateMeeTimeStateInfo(const CallAttributeInfo & info)529 int32_t NapiCallAbilityCallback::UpdateMeeTimeStateInfo(const CallAttributeInfo &info)
530 {
531     if (meeTimeStateCallback_.thisVar == nullptr) {
532         return CALL_ERR_CALLBACK_NOT_EXIST;
533     }
534     uv_loop_s *loop = nullptr;
535 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
536     napi_get_uv_event_loop(meeTimeStateCallback_.env, &loop);
537 #endif
538     if (loop == nullptr) {
539         return TELEPHONY_ERR_LOCAL_PTR_NULL;
540     }
541     MeeTimeStateWorker *meeTimeStateWorker = std::make_unique<MeeTimeStateWorker>().release();
542     if (meeTimeStateWorker == nullptr) {
543         TELEPHONY_LOGE("meeTimeStateWorker is nullptr!");
544         return TELEPHONY_ERR_LOCAL_PTR_NULL;
545     }
546     meeTimeStateWorker->info = info;
547     meeTimeStateWorker->callback = meeTimeStateCallback_;
548     uv_work_t *work = std::make_unique<uv_work_t>().release();
549     if (work == nullptr) {
550         delete meeTimeStateWorker;
551         meeTimeStateWorker = nullptr;
552         TELEPHONY_LOGE("work is nullptr!");
553         return TELEPHONY_ERR_LOCAL_PTR_NULL;
554     }
555     work->data = (void *)meeTimeStateWorker;
556     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
557         TELEPHONY_LOGD("UpdateMeeTimeStateInfo uv_queue_work_with_qos");
558     }, ReportMeeTimeStateWork, uv_qos_default);
559     if (errCode != 0) {
560         delete meeTimeStateWorker;
561         meeTimeStateWorker = nullptr;
562         delete work;
563         work = nullptr;
564         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, errCode: %{public}d", errCode);
565         return TELEPHONY_ERROR;
566     }
567     return TELEPHONY_SUCCESS;
568 }
569 
ReportCallStateWork(uv_work_t * work,int32_t status)570 void NapiCallAbilityCallback::ReportCallStateWork(uv_work_t *work, int32_t status)
571 {
572     CallStateWorker *dataWorkerData = (CallStateWorker *)work->data;
573     if (dataWorkerData == nullptr) {
574         TELEPHONY_LOGE("dataWorkerData is nullptr!");
575         return;
576     }
577     int32_t ret = ReportCallState(dataWorkerData->info, dataWorkerData->callback);
578     TELEPHONY_LOGI("ReportCallState result = %{public}d", ret);
579     delete dataWorkerData;
580     dataWorkerData = nullptr;
581     delete work;
582     work = nullptr;
583 }
584 
ReportMeeTimeStateWork(uv_work_t * work,int32_t status)585 void NapiCallAbilityCallback::ReportMeeTimeStateWork(uv_work_t *work, int32_t status)
586 {
587     MeeTimeStateWorker *dataWorkerData = (MeeTimeStateWorker *)work->data;
588     if (dataWorkerData == nullptr) {
589         TELEPHONY_LOGE("meeTimeStateWorker is nullptr!");
590         return;
591     }
592     int32_t ret = ReportCallState(dataWorkerData->info, dataWorkerData->callback);
593     TELEPHONY_LOGI("ReportMeeTimeState result = %{public}d", ret);
594     delete dataWorkerData;
595     dataWorkerData = nullptr;
596     delete work;
597     work = nullptr;
598 }
599 
600 /**
601  * To notify an application of a call status change, register a callback with on() first.
602  */
ReportCallState(CallAttributeInfo & info,EventCallback stateCallback)603 int32_t NapiCallAbilityCallback::ReportCallState(CallAttributeInfo &info, EventCallback stateCallback)
604 {
605     napi_env env = stateCallback.env;
606     napi_handle_scope scope = nullptr;
607     napi_open_handle_scope(env, &scope);
608     if (scope == nullptr) {
609         TELEPHONY_LOGE("scope is nullptr");
610         napi_close_handle_scope(env, scope);
611         return TELEPHONY_ERROR;
612     }
613     napi_value callbackFunc = nullptr;
614     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
615     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
616     NapiCallManagerUtils::SetPropertyStringUtf8(
617         env, callbackValues[ARRAY_INDEX_FIRST], "accountNumber", info.accountNumber);
618     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "accountId", info.accountId);
619     NapiCallManagerUtils::SetPropertyInt32(
620         env, callbackValues[ARRAY_INDEX_FIRST], "videoState", static_cast<int32_t>(info.videoState));
621     NapiCallManagerUtils::SetPropertyInt64(env, callbackValues[ARRAY_INDEX_FIRST], "startTime", info.startTime);
622     NapiCallManagerUtils::SetPropertyBoolean(env, callbackValues[ARRAY_INDEX_FIRST], "isEcc", info.isEcc);
623     NapiCallManagerUtils::SetPropertyInt32(
624         env, callbackValues[ARRAY_INDEX_FIRST], "callType", static_cast<int32_t>(info.callType));
625     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", info.callId);
626     NapiCallManagerUtils::SetPropertyInt32(
627         env, callbackValues[ARRAY_INDEX_FIRST], "callState", static_cast<int32_t>(info.callState));
628     NapiCallManagerUtils::SetPropertyInt32(
629         env, callbackValues[ARRAY_INDEX_FIRST], "conferenceState", static_cast<int32_t>(info.conferenceState));
630     NapiCallManagerUtils::SetPropertyInt32(
631         env, callbackValues[ARRAY_INDEX_FIRST], "crsType", info.crsType);
632     NapiCallManagerUtils::SetPropertyInt32(
633         env, callbackValues[ARRAY_INDEX_FIRST], "originalCallType", info.originalCallType);
634     NapiCallManagerUtils::SetPropertyInt32(
635         env, callbackValues[ARRAY_INDEX_FIRST], "phoneOrWatch", info.phoneOrWatch);
636     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], std::string("extraParams").c_str(),
637         AppExecFwk::WrapWantParams(env, AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(info.extraParamsString)));
638     ReportCallAttribute(env, callbackValues, ARRAY_INDEX_THIRD, info);
639     napi_get_reference_value(env, stateCallback.callbackRef, &callbackFunc);
640     if (callbackFunc == nullptr) {
641         TELEPHONY_LOGE("callbackFunc is null!");
642         napi_close_handle_scope(env, scope);
643         return CALL_ERR_CALLBACK_NOT_EXIST;
644     }
645     napi_value thisVar = nullptr;
646     napi_get_reference_value(env, stateCallback.thisVar, &thisVar);
647     napi_value callbackResult = nullptr;
648     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
649     napi_close_handle_scope(env, scope);
650     return TELEPHONY_SUCCESS;
651 }
652 
ReportCallAttribute(napi_env & env,napi_value callbackValues[],const size_t callbackValuesCount,CallAttributeInfo & info)653 void NapiCallAbilityCallback::ReportCallAttribute(napi_env &env, napi_value callbackValues[],
654     const size_t callbackValuesCount, CallAttributeInfo &info)
655 {
656     std::string str(info.numberLocation);
657     if (str == "default") {
658         TELEPHONY_LOGE("numberLocation is default");
659         (void)memset_s(info.numberLocation, kMaxNumberLen, 0, kMaxNumberLen);
660     }
661     NapiCallManagerUtils::SetPropertyStringUtf8(
662         env, callbackValues[ARRAY_INDEX_FIRST], "numberLocation", info.numberLocation);
663     TELEPHONY_LOGI("ReportCallState crsType = %{public}d", info.crsType);
664     if (info.callType == CallType::TYPE_VOIP) {
665         napi_value voipObject = nullptr;
666         CreateVoipNapiValue(env, voipObject, info);
667         napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "voipCallAttribute", voipObject);
668     }
669     napi_value markInfoObject = nullptr;
670     CreateMarkInfoNapiValue(env, markInfoObject, info);
671     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "numberMarkInfo", markInfoObject);
672     NapiCallManagerUtils::SetPropertyStringUtf8(
673         env, callbackValues[ARRAY_INDEX_FIRST], "distributedContactName", info.contactName);
674 }
675 
CreateVoipNapiValue(napi_env & env,napi_value & voipObject,CallAttributeInfo & info)676 void NapiCallAbilityCallback::CreateVoipNapiValue(napi_env &env, napi_value &voipObject, CallAttributeInfo &info)
677 {
678     napi_create_object(env, &voipObject);
679     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "userName", info.voipCallInfo.userName);
680     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "abilityName", info.voipCallInfo.abilityName);
681     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "extensionId", info.voipCallInfo.extensionId);
682     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "voipBundleName", info.voipCallInfo.voipBundleName);
683     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "voipCallId", info.voipCallInfo.voipCallId);
684     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "showBannerForIncomingCall",
685         info.voipCallInfo.showBannerForIncomingCall);
686     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "isConferenceCall", info.voipCallInfo.isConferenceCall);
687     NapiCallManagerUtils::SetPropertyBoolean(
688         env, voipObject, "isVoiceAnswerSupported", info.voipCallInfo.isVoiceAnswerSupported);
689     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "hasMicPermission", info.voipCallInfo.hasMicPermission);
690     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "isCapsuleSticky", info.voipCallInfo.isCapsuleSticky);
691     NapiCallManagerUtils::SetPropertyInt32(env, voipObject, "uid", info.voipCallInfo.uid);
692     std::shared_ptr<Media::PixelMap> userProfile =
693         std::shared_ptr<Media::PixelMap>(Media::PixelMap::DecodeTlv(info.voipCallInfo.userProfile));
694     napi_value pixelMapObject = Media::PixelMapNapi::CreatePixelMap(env, userProfile);
695     napi_set_named_property(env, voipObject, "userProfile", pixelMapObject);
696 }
697 
CreateMarkInfoNapiValue(napi_env & env,napi_value & markInfoObject,CallAttributeInfo & info)698 void NapiCallAbilityCallback::CreateMarkInfoNapiValue(napi_env &env,
699     napi_value &markInfoObject, CallAttributeInfo &info)
700 {
701     napi_create_object(env, &markInfoObject);
702     NapiCallManagerUtils::SetPropertyInt32(env, markInfoObject, "markType",
703         static_cast<int32_t>(info.numberMarkInfo.markType));
704     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markContent", info.numberMarkInfo.markContent);
705     NapiCallManagerUtils::SetPropertyInt32(env, markInfoObject, "markCount",
706         static_cast<int32_t>(info.numberMarkInfo.markCount));
707     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markSource", info.numberMarkInfo.markSource);
708     NapiCallManagerUtils::SetPropertyBoolean(env, markInfoObject, "isCloud", info.numberMarkInfo.isCloud);
709     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markDetails", info.numberMarkInfo.markDetails);
710 }
711 
UpdateCallEvent(const CallEventInfo & info)712 int32_t NapiCallAbilityCallback::UpdateCallEvent(const CallEventInfo &info)
713 {
714     if (eventCallback_.thisVar == nullptr) {
715         TELEPHONY_LOGE("eventCallback is null!");
716         return CALL_ERR_CALLBACK_NOT_EXIST;
717     }
718     uv_loop_s *loop = nullptr;
719 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
720     napi_get_uv_event_loop(eventCallback_.env, &loop);
721 #endif
722     if (loop == nullptr) {
723         return TELEPHONY_ERR_LOCAL_PTR_NULL;
724     }
725     CallEventWorker *callEventWorker = std::make_unique<CallEventWorker>().release();
726     if (callEventWorker == nullptr) {
727         TELEPHONY_LOGE("callEventWorker is nullptr!");
728         return TELEPHONY_ERR_LOCAL_PTR_NULL;
729     }
730     callEventWorker->info = info;
731     callEventWorker->callback = eventCallback_;
732     uv_work_t *work = std::make_unique<uv_work_t>().release();
733     if (work == nullptr) {
734         delete callEventWorker;
735         callEventWorker = nullptr;
736         TELEPHONY_LOGE("work is nullptr!");
737         return TELEPHONY_ERR_LOCAL_PTR_NULL;
738     }
739     work->data = (void *)callEventWorker;
740     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
741         TELEPHONY_LOGD("UpdateCallEvent uv_queue_work_with_qos");
742     }, ReportCallEventWork, uv_qos_default);
743     if (errCode != 0) {
744         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
745         delete callEventWorker;
746         callEventWorker = nullptr;
747         delete work;
748         work = nullptr;
749         return TELEPHONY_ERROR;
750     }
751     return TELEPHONY_SUCCESS;
752 }
753 
ReportCallEventWork(uv_work_t * work,int32_t status)754 void NapiCallAbilityCallback::ReportCallEventWork(uv_work_t *work, int32_t status)
755 {
756     CallEventWorker *dataWorkerData = (CallEventWorker *)work->data;
757     if (dataWorkerData == nullptr) {
758         TELEPHONY_LOGE("dataWorkerData is nullptr!");
759         return;
760     }
761     int32_t ret = ReportCallEvent(dataWorkerData->info, dataWorkerData->callback);
762     TELEPHONY_LOGI("ReportCallEvent results %{public}d", ret);
763     delete dataWorkerData;
764     dataWorkerData = nullptr;
765     delete work;
766     work = nullptr;
767 }
768 
ReportCallEvent(CallEventInfo & info,EventCallback eventCallback)769 int32_t NapiCallAbilityCallback::ReportCallEvent(CallEventInfo &info, EventCallback eventCallback)
770 {
771     napi_env env = eventCallback.env;
772     napi_handle_scope scopeCallEvent = nullptr;
773     napi_open_handle_scope(env, &scopeCallEvent);
774     if (scopeCallEvent == nullptr) {
775         TELEPHONY_LOGE("scopeCallEvent is nullptr");
776         napi_close_handle_scope(env, scopeCallEvent);
777         return TELEPHONY_ERROR;
778     }
779     napi_value callEventCallbackFunc = nullptr;
780     napi_value callEventCallbackValues[ARRAY_INDEX_THIRD] = { 0 };
781     napi_create_object(env, &callEventCallbackValues[ARRAY_INDEX_FIRST]);
782     NapiCallManagerUtils::SetPropertyInt32(
783         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "eventId", static_cast<int32_t>(info.eventId));
784     NapiCallManagerUtils::SetPropertyStringUtf8(
785         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "accountNumber", info.phoneNum);
786     NapiCallManagerUtils::SetPropertyStringUtf8(
787         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "bundleName", info.bundleName);
788     napi_get_reference_value(env, eventCallback.callbackRef, &callEventCallbackFunc);
789     if (callEventCallbackFunc == nullptr) {
790         TELEPHONY_LOGE("callEventCallbackFunc is null!");
791         napi_close_handle_scope(env, scopeCallEvent);
792         return CALL_ERR_CALLBACK_NOT_EXIST;
793     }
794     napi_value thisVar = nullptr;
795     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
796     napi_value callbackResult = nullptr;
797     napi_call_function(env, thisVar, callEventCallbackFunc, DATA_LENGTH_ONE, callEventCallbackValues, &callbackResult);
798     napi_close_handle_scope(env, scopeCallEvent);
799     return TELEPHONY_SUCCESS;
800 }
801 
UpdateCallDisconnectedCause(const DisconnectedDetails & details)802 int32_t NapiCallAbilityCallback::UpdateCallDisconnectedCause(const DisconnectedDetails &details)
803 {
804     if (callDisconnectCauseCallback_.thisVar == nullptr) {
805         TELEPHONY_LOGE("callDisconnectCauseCallback_ is null!");
806         return CALL_ERR_CALLBACK_NOT_EXIST;
807     }
808     uv_loop_s *loop = nullptr;
809 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
810     napi_get_uv_event_loop(callDisconnectCauseCallback_.env, &loop);
811 #endif
812     if (loop == nullptr) {
813         return TELEPHONY_ERR_LOCAL_PTR_NULL;
814     }
815     CallDisconnectedCauseWorker *callDisconnectedCauseWorker =
816         std::make_unique<CallDisconnectedCauseWorker>().release();
817     if (callDisconnectedCauseWorker == nullptr) {
818         TELEPHONY_LOGE("callDisconnectedCauseWorker is nullptr!");
819         return TELEPHONY_ERR_LOCAL_PTR_NULL;
820     }
821     callDisconnectedCauseWorker->details = details;
822     callDisconnectedCauseWorker->callback = callDisconnectCauseCallback_;
823     uv_work_t *work = std::make_unique<uv_work_t>().release();
824     if (work == nullptr) {
825         delete callDisconnectedCauseWorker;
826         callDisconnectedCauseWorker = nullptr;
827         TELEPHONY_LOGE("work is nullptr!");
828         return TELEPHONY_ERR_LOCAL_PTR_NULL;
829     }
830     work->data = (void *)callDisconnectedCauseWorker;
831     int32_t errCode = uv_queue_work_with_qos(
832         loop, work, [](uv_work_t *work) {
833             TELEPHONY_LOGD("UpdateCallDisconnectedCause uv_queue_work_with_qos");
834         }, ReportCallDisconnectedCauseWork, uv_qos_default);
835     if (errCode != 0) {
836         delete callDisconnectedCauseWorker;
837         callDisconnectedCauseWorker = nullptr;
838         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, errCode: %{public}d", errCode);
839         delete work;
840         work = nullptr;
841         return TELEPHONY_ERROR;
842     }
843     if (callDisconnectCauseCallback_.thisVar) {
844         (void)memset_s(&callDisconnectCauseCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
845     }
846     return TELEPHONY_SUCCESS;
847 }
848 
ReportCallDisconnectedCauseWork(uv_work_t * work,int32_t status)849 void NapiCallAbilityCallback::ReportCallDisconnectedCauseWork(uv_work_t *work, int32_t status)
850 {
851     CallDisconnectedCauseWorker *dataWorkerData = (CallDisconnectedCauseWorker *)work->data;
852     if (dataWorkerData == nullptr) {
853         TELEPHONY_LOGE("dataWorkerData is nullptr!");
854         return;
855     }
856     int32_t ret = ReportDisconnectedCause(dataWorkerData->details, dataWorkerData->callback);
857     TELEPHONY_LOGI("ReportDisconnectedCause results %{public}d", ret);
858     delete dataWorkerData;
859     dataWorkerData = nullptr;
860     delete work;
861     work = nullptr;
862 }
863 
ReportDisconnectedCause(const DisconnectedDetails & details,EventCallback eventCallback)864 int32_t NapiCallAbilityCallback::ReportDisconnectedCause(
865     const DisconnectedDetails &details, EventCallback eventCallback)
866 {
867     napi_env env = eventCallback.env;
868     napi_handle_scope disconnectedScope = nullptr;
869     napi_open_handle_scope(env, &disconnectedScope);
870     if (disconnectedScope == nullptr) {
871         TELEPHONY_LOGE("disconnectedScope is nullptr");
872         napi_close_handle_scope(env, disconnectedScope);
873         return TELEPHONY_ERROR;
874     }
875     napi_value callbackFunc = nullptr;
876     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
877     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
878     NapiCallManagerUtils::SetPropertyInt32(
879         env, callbackValues[ARRAY_INDEX_FIRST], "reason", static_cast<int32_t>(details.reason));
880     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "message", details.message);
881     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
882     if (callbackFunc == nullptr) {
883         TELEPHONY_LOGE("callbackFunc is null!");
884         napi_close_handle_scope(env, disconnectedScope);
885         return CALL_ERR_CALLBACK_NOT_EXIST;
886     }
887     napi_value thisVar = nullptr;
888     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
889     napi_value callbackResult = nullptr;
890     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
891     napi_close_handle_scope(env, disconnectedScope);
892     return TELEPHONY_SUCCESS;
893 }
894 
UpdateAsyncResultsInfo(const CallResultReportId reportId,AppExecFwk::PacMap & resultInfo)895 int32_t NapiCallAbilityCallback::UpdateAsyncResultsInfo(
896     const CallResultReportId reportId, AppExecFwk::PacMap &resultInfo)
897 {
898     int32_t result = TELEPHONY_ERR_FAIL;
899     TELEPHONY_LOGI("UpdateAsyncResultsInfo reportId = %{public}d", reportId);
900     auto itFunc = memberFuncMap_.find(reportId);
901     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
902         auto memberFunc = itFunc->second;
903         result = memberFunc(resultInfo);
904     }
905     return result;
906 }
907 
UpdateMmiCodeResultsInfo(const MmiCodeInfo & info)908 int32_t NapiCallAbilityCallback::UpdateMmiCodeResultsInfo(const MmiCodeInfo &info)
909 {
910     if (mmiCodeCallback_.thisVar == nullptr) {
911         TELEPHONY_LOGE("mmiCodeCallback is null!");
912         return CALL_ERR_CALLBACK_NOT_EXIST;
913     }
914     uv_loop_s *loop = nullptr;
915 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
916     napi_get_uv_event_loop(mmiCodeCallback_.env, &loop);
917 #endif
918     if (loop == nullptr) {
919         return TELEPHONY_ERR_LOCAL_PTR_NULL;
920     }
921     MmiCodeWorker *mmiCodeWorker = std::make_unique<MmiCodeWorker>().release();
922     if (mmiCodeWorker == nullptr) {
923         TELEPHONY_LOGE("mmiCodeWorker is nullptr!");
924         return TELEPHONY_ERR_LOCAL_PTR_NULL;
925     }
926     mmiCodeWorker->info = info;
927     mmiCodeWorker->callback = mmiCodeCallback_;
928     uv_work_t *work = std::make_unique<uv_work_t>().release();
929     if (work == nullptr) {
930         delete mmiCodeWorker;
931         mmiCodeWorker = nullptr;
932         TELEPHONY_LOGE("work is nullptr!");
933         return TELEPHONY_ERR_LOCAL_PTR_NULL;
934     }
935     work->data = (void *)mmiCodeWorker;
936     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
937         TELEPHONY_LOGD("UpdateMmiCodeResultsInfo uv_queue_work_with_qos");
938     }, ReportMmiCodeWork, uv_qos_default);
939     if (errCode != 0) {
940         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
941         delete mmiCodeWorker;
942         mmiCodeWorker = nullptr;
943         delete work;
944         work = nullptr;
945         return TELEPHONY_ERROR;
946     }
947     return TELEPHONY_SUCCESS;
948 }
949 
ReportMmiCodeWork(uv_work_t * work,int32_t status)950 void NapiCallAbilityCallback::ReportMmiCodeWork(uv_work_t *work, int32_t status)
951 {
952     MmiCodeWorker *dataWorkerData = (MmiCodeWorker *)work->data;
953     if (dataWorkerData == nullptr) {
954         TELEPHONY_LOGE("dataWorkerData is nullptr!");
955         return;
956     }
957     int32_t ret = ReportMmiCode(dataWorkerData->info, dataWorkerData->callback);
958     TELEPHONY_LOGI("ReportMmiCode result = %{public}d", ret);
959     delete dataWorkerData;
960     dataWorkerData = nullptr;
961     delete work;
962     work = nullptr;
963 }
964 
965 /**
966  * To notify an application of MMI code result, register a callback with on() first.
967  */
ReportMmiCode(MmiCodeInfo & info,EventCallback eventCallback)968 int32_t NapiCallAbilityCallback::ReportMmiCode(MmiCodeInfo &info, EventCallback eventCallback)
969 {
970     napi_env env = eventCallback.env;
971     napi_handle_scope mmiCodeScope = nullptr;
972     napi_open_handle_scope(env, &mmiCodeScope);
973     if (mmiCodeScope == nullptr) {
974         TELEPHONY_LOGE("mmiCodeScope is nullptr");
975         napi_close_handle_scope(env, mmiCodeScope);
976         return TELEPHONY_ERROR;
977     }
978     napi_value callbackFunc = nullptr;
979     napi_value callbackValues[ARRAY_INDEX_FOURTH] = { 0 };
980     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
981     NapiCallManagerUtils::SetPropertyInt32(
982         env, callbackValues[ARRAY_INDEX_FIRST], "result", static_cast<int32_t>(info.result));
983     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "message", info.message);
984     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "mmiCodeType",
985         static_cast<int32_t>(info.mmiCodeType));
986     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "action",
987         static_cast<int32_t>(info.action));
988     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "status",
989         static_cast<int32_t>(info.status));
990     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "classCw",
991         static_cast<int32_t>(info.classCw));
992     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "reason",
993         static_cast<int32_t>(info.reason));
994     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "time",
995         static_cast<int32_t>(info.time));
996     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "number", info.number);
997     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
998     if (callbackFunc == nullptr) {
999         TELEPHONY_LOGE("callbackFunc is null!");
1000         napi_close_handle_scope(env, mmiCodeScope);
1001         return CALL_ERR_CALLBACK_NOT_EXIST;
1002     }
1003     napi_value thisVar = nullptr;
1004     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
1005     napi_value callbackResult = nullptr;
1006     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1007     napi_close_handle_scope(env, mmiCodeScope);
1008     return TELEPHONY_SUCCESS;
1009 }
1010 
UpdateAudioDeviceInfo(const AudioDeviceInfo & info)1011 int32_t NapiCallAbilityCallback::UpdateAudioDeviceInfo(const AudioDeviceInfo &info)
1012 {
1013     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
1014     if (audioDeviceCallback_.thisVar == nullptr) {
1015         return CALL_ERR_CALLBACK_NOT_EXIST;
1016     }
1017     uv_loop_s *loop = nullptr;
1018 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1019     napi_get_uv_event_loop(audioDeviceCallback_.env, &loop);
1020 #endif
1021     if (loop == nullptr) {
1022         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1023     }
1024     AudioDeviceWork *audioDeviceWork = std::make_unique<AudioDeviceWork>().release();
1025     if (audioDeviceWork == nullptr) {
1026         TELEPHONY_LOGE("audioDeviceWork is nullptr!");
1027         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1028     }
1029     audioDeviceWork->info = info;
1030     audioDeviceWork->callback = audioDeviceCallback_;
1031     uv_work_t *work = std::make_unique<uv_work_t>().release();
1032     if (work == nullptr) {
1033         delete audioDeviceWork;
1034         audioDeviceWork = nullptr;
1035         TELEPHONY_LOGE("work is nullptr!");
1036         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1037     }
1038     work->data = (void *)audioDeviceWork;
1039     int32_t errCode = uv_queue_work_with_qos(
1040         loop, work, [](uv_work_t *work) {
1041             TELEPHONY_LOGD("UpdateAudioDeviceInfo uv_queue_work_with_qos");
1042         }, ReportAudioDeviceInfoWork, uv_qos_default);
1043     if (errCode != 0) {
1044         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1045         delete audioDeviceWork;
1046         audioDeviceWork = nullptr;
1047         delete work;
1048         work = nullptr;
1049         return TELEPHONY_ERROR;
1050     }
1051     return TELEPHONY_SUCCESS;
1052 }
1053 
ReportAudioDeviceInfoWork(uv_work_t * work,int32_t status)1054 void NapiCallAbilityCallback::ReportAudioDeviceInfoWork(uv_work_t *work, int32_t status)
1055 {
1056     TELEPHONY_LOGI("report audio device info work.");
1057     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
1058     AudioDeviceWork *dataWorkerData = (AudioDeviceWork *)work->data;
1059     if (dataWorkerData == nullptr) {
1060         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1061         return;
1062     }
1063     if (audioDeviceCallback_.thisVar && audioDeviceCallback_.callbackRef) {
1064         int32_t ret = ReportAudioDeviceInfo(dataWorkerData->info, audioDeviceCallback_);
1065         TELEPHONY_LOGI("ReportAudioDeviceInfo result = %{public}d", ret);
1066         delete dataWorkerData;
1067         dataWorkerData = nullptr;
1068         delete work;
1069         work = nullptr;
1070     }
1071 }
1072 
ReportAudioDeviceInfo(AudioDeviceInfo & info,EventCallback eventCallback)1073 int32_t NapiCallAbilityCallback::ReportAudioDeviceInfo(AudioDeviceInfo &info, EventCallback eventCallback)
1074 {
1075     TELEPHONY_LOGI("report audio device info.");
1076     napi_env env = eventCallback.env;
1077     napi_handle_scope AudioDeviceInfoScope = nullptr;
1078     napi_open_handle_scope(env, &AudioDeviceInfoScope);
1079     if (AudioDeviceInfoScope == nullptr) {
1080         TELEPHONY_LOGE("AudioDeviceInfoScope is nullptr");
1081         napi_close_handle_scope(env, AudioDeviceInfoScope);
1082         return TELEPHONY_ERROR;
1083     }
1084     napi_value callbackFunc = nullptr;
1085     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
1086     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
1087 
1088     NapiCallManagerUtils::SetPropertyBoolean(env, callbackValues[ARRAY_INDEX_FIRST], "isMuted", info.isMuted);
1089     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", info.callId);
1090 
1091     napi_value currentAudioDeviceValue = nullptr;
1092     napi_create_object(env, &currentAudioDeviceValue);
1093     NapiCallManagerUtils::SetPropertyInt32(env, currentAudioDeviceValue, "deviceType",
1094         static_cast<int32_t>(info.currentAudioDevice.deviceType));
1095     NapiCallManagerUtils::SetPropertyStringUtf8(
1096         env, currentAudioDeviceValue, "address", info.currentAudioDevice.address);
1097     NapiCallManagerUtils::SetPropertyStringUtf8(
1098         env, currentAudioDeviceValue, "deviceName", info.currentAudioDevice.deviceName);
1099     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "currentAudioDevice", currentAudioDeviceValue);
1100 
1101     napi_value audioDeviceListValue = nullptr;
1102     napi_create_array(env, &audioDeviceListValue);
1103     std::vector<AudioDevice>::iterator it = info.audioDeviceList.begin();
1104     int32_t i = 0;
1105     for (; it != info.audioDeviceList.end(); ++it) {
1106         napi_value value = nullptr;
1107         napi_create_object(env, &value);
1108         NapiCallManagerUtils::SetPropertyInt32(env, value, "deviceType", static_cast<int32_t>(it->deviceType));
1109         NapiCallManagerUtils::SetPropertyStringUtf8(env, value, "address", it->address);
1110         NapiCallManagerUtils::SetPropertyStringUtf8(env, value, "deviceName", it->deviceName);
1111         napi_set_element(env, audioDeviceListValue, i, value);
1112         ++i;
1113     }
1114     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "audioDeviceList", audioDeviceListValue);
1115     if (eventCallback.callbackRef == nullptr) {
1116         TELEPHONY_LOGE("eventCallback callbackRef is null!");
1117         napi_close_handle_scope(env, AudioDeviceInfoScope);
1118         return CALL_ERR_CALLBACK_NOT_EXIST;
1119     }
1120     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
1121     if (callbackFunc == nullptr) {
1122         TELEPHONY_LOGE("callbackFunc is null!");
1123         napi_close_handle_scope(env, AudioDeviceInfoScope);
1124         return CALL_ERR_CALLBACK_NOT_EXIST;
1125     }
1126     napi_value thisVar = nullptr;
1127     if (eventCallback.thisVar == nullptr) {
1128         TELEPHONY_LOGE("eventCallback thisVar is null!");
1129         napi_close_handle_scope(env, AudioDeviceInfoScope);
1130         return CALL_ERR_CALLBACK_NOT_EXIST;
1131     }
1132     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
1133     napi_value callbackResult = nullptr;
1134     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1135     napi_close_handle_scope(env, AudioDeviceInfoScope);
1136     return TELEPHONY_SUCCESS;
1137 }
1138 
OttCallRequest(OttCallRequestId requestId,AppExecFwk::PacMap & info)1139 int32_t NapiCallAbilityCallback::OttCallRequest(OttCallRequestId requestId, AppExecFwk::PacMap &info)
1140 {
1141     if (ottRequestCallback_.thisVar == nullptr) {
1142         TELEPHONY_LOGE("stateCallback is null!");
1143         return CALL_ERR_CALLBACK_NOT_EXIST;
1144     }
1145     uv_loop_s *loop = nullptr;
1146 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1147     napi_get_uv_event_loop(ottRequestCallback_.env, &loop);
1148 #endif
1149     if (loop == nullptr) {
1150         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1151     }
1152     CallOttWorker *callOttWorker = std::make_unique<CallOttWorker>().release();
1153     if (callOttWorker == nullptr) {
1154         TELEPHONY_LOGE("callOttWorker is nullptr!");
1155         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1156     }
1157     callOttWorker->requestId = requestId;
1158     callOttWorker->info = info;
1159     callOttWorker->callback = ottRequestCallback_;
1160     uv_work_t *work = std::make_unique<uv_work_t>().release();
1161     if (work == nullptr) {
1162         delete callOttWorker;
1163         callOttWorker = nullptr;
1164         TELEPHONY_LOGE("work is nullptr!");
1165         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1166     }
1167     work->data = (void *)callOttWorker;
1168     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
1169         TELEPHONY_LOGD("OttCallRequest uv_queue_work_with_qos");
1170     }, ReportCallOttWork, uv_qos_default);
1171     if (errCode != 0) {
1172         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1173         delete callOttWorker;
1174         callOttWorker = nullptr;
1175         delete work;
1176         work = nullptr;
1177         return TELEPHONY_ERROR;
1178     }
1179     return TELEPHONY_SUCCESS;
1180 }
1181 
ReportGetWaitingInfo(AppExecFwk::PacMap & resultInfo)1182 int32_t NapiCallAbilityCallback::ReportGetWaitingInfo(AppExecFwk::PacMap &resultInfo)
1183 {
1184     std::lock_guard<std::mutex> lock(getWaitingCallbackMutex_);
1185     if (getWaitingCallback_.thisVar == nullptr) {
1186         TELEPHONY_LOGE("getWaitingCallback is null!");
1187         return CALL_ERR_CALLBACK_NOT_EXIST;
1188     }
1189     uv_loop_s *loop = nullptr;
1190 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1191     napi_get_uv_event_loop(getWaitingCallback_.env, &loop);
1192 #endif
1193     if (loop == nullptr) {
1194         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1195     }
1196     CallSupplementWorker *callSupplementWorkerData = std::make_unique<CallSupplementWorker>().release();
1197     if (callSupplementWorkerData == nullptr) {
1198         TELEPHONY_LOGE("callSupplementWorkerData is nullptr!");
1199         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1200     }
1201     callSupplementWorkerData->info = resultInfo;
1202     callSupplementWorkerData->callback = getWaitingCallback_;
1203     uv_work_t *work = std::make_unique<uv_work_t>().release();
1204     if (work == nullptr) {
1205         delete callSupplementWorkerData;
1206         callSupplementWorkerData = nullptr;
1207         TELEPHONY_LOGE("work is nullptr!");
1208         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1209     }
1210     work->data = (void *)callSupplementWorkerData;
1211     int32_t errorCode = uv_queue_work_with_qos(
1212         loop, work, [](uv_work_t *work) {
1213             TELEPHONY_LOGD("ReportGetWaitingInfo uv_queue_work_with_qos");
1214         }, ReportWaitAndLimitInfoWork, uv_qos_default);
1215     if (errorCode != 0) {
1216         delete callSupplementWorkerData;
1217         callSupplementWorkerData = nullptr;
1218         delete work;
1219         work = nullptr;
1220         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errorCode: %{public}d", errorCode);
1221         return TELEPHONY_ERROR;
1222     }
1223     if (getWaitingCallback_.thisVar) {
1224         (void)memset_s(&getWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1225     }
1226     return TELEPHONY_SUCCESS;
1227 }
1228 
ReportCloseUnFinishedUssdInfo(AppExecFwk::PacMap & resultInfo)1229 int32_t NapiCallAbilityCallback::ReportCloseUnFinishedUssdInfo(AppExecFwk::PacMap &resultInfo)
1230 {
1231     std::lock_guard<std::mutex> lock(closeUnfinishedUssdCallbackMutex_);
1232     if (closeUnfinishedUssdCallback_.thisVar == nullptr) {
1233         TELEPHONY_LOGE("closeUnfinishedUssdCallback is null!");
1234         return CALL_ERR_CALLBACK_NOT_EXIST;
1235     }
1236     uv_loop_s *loop = nullptr;
1237 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1238     napi_get_uv_event_loop(closeUnfinishedUssdCallback_.env, &loop);
1239 #endif
1240     if (loop == nullptr) {
1241         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1242     }
1243     CallSupplementWorker *callSupplementDataWorker = std::make_unique<CallSupplementWorker>().release();
1244     if (callSupplementDataWorker == nullptr) {
1245         TELEPHONY_LOGE("callSupplementDataWorker is nullptr!");
1246         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1247     }
1248     callSupplementDataWorker->info = resultInfo;
1249     callSupplementDataWorker->callback = closeUnfinishedUssdCallback_;
1250     uv_work_t *work = std::make_unique<uv_work_t>().release();
1251     if (work == nullptr) {
1252         delete callSupplementDataWorker;
1253         callSupplementDataWorker = nullptr;
1254         TELEPHONY_LOGE("work is nullptr!");
1255         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1256     }
1257     work->data = (void *)callSupplementDataWorker;
1258     int32_t errorCode = uv_queue_work_with_qos(
1259         loop, work, [](uv_work_t *work) {
1260             TELEPHONY_LOGD("ReportCloseUnFinishedUssdInfo uv_queue_work_with_qos");
1261         }, ReportExecutionResultWork, uv_qos_default);
1262     if (errorCode != 0) {
1263         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errorCode: %{public}d", errorCode);
1264         delete callSupplementDataWorker;
1265         callSupplementDataWorker = nullptr;
1266         delete work;
1267         work = nullptr;
1268         return TELEPHONY_ERROR;
1269     }
1270     if (closeUnfinishedUssdCallback_.thisVar) {
1271         (void)memset_s(&closeUnfinishedUssdCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1272     }
1273     return TELEPHONY_SUCCESS;
1274 }
1275 
ReportSetWaitingInfo(AppExecFwk::PacMap & resultInfo)1276 int32_t NapiCallAbilityCallback::ReportSetWaitingInfo(AppExecFwk::PacMap &resultInfo)
1277 {
1278     std::lock_guard<std::mutex> lock(setWaitingCallbackMutex_);
1279     if (setWaitingCallback_.thisVar == nullptr) {
1280         TELEPHONY_LOGE("setWaitingCallback is null!");
1281         return CALL_ERR_CALLBACK_NOT_EXIST;
1282     }
1283     uv_loop_s *loop = nullptr;
1284 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1285     napi_get_uv_event_loop(setWaitingCallback_.env, &loop);
1286 #endif
1287     if (loop == nullptr) {
1288         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1289     }
1290     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1291     if (callSupplementWorker == nullptr) {
1292         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1293         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1294     }
1295     callSupplementWorker->info = resultInfo;
1296     callSupplementWorker->callback = setWaitingCallback_;
1297     uv_work_t *work = std::make_unique<uv_work_t>().release();
1298     if (work == nullptr) {
1299         delete callSupplementWorker;
1300         callSupplementWorker = nullptr;
1301         TELEPHONY_LOGE("work is nullptr!");
1302         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1303     }
1304     work->data = (void *)callSupplementWorker;
1305     int32_t resultCode = uv_queue_work_with_qos(
1306         loop, work, [](uv_work_t *work) {
1307             TELEPHONY_LOGD("ReportSetWaitingInfo uv_queue_work_with_qos");
1308         }, ReportExecutionResultWork, uv_qos_default);
1309     if (resultCode != 0) {
1310         delete callSupplementWorker;
1311         callSupplementWorker = nullptr;
1312         delete work;
1313         work = nullptr;
1314         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1315         return TELEPHONY_ERROR;
1316     }
1317     if (setWaitingCallback_.thisVar) {
1318         (void)memset_s(&setWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1319     }
1320     return TELEPHONY_SUCCESS;
1321 }
1322 
ReportGetRestrictionInfo(AppExecFwk::PacMap & resultInfo)1323 int32_t NapiCallAbilityCallback::ReportGetRestrictionInfo(AppExecFwk::PacMap &resultInfo)
1324 {
1325     std::lock_guard<std::mutex> lock(getRestrictionCallbackMutex_);
1326     if (getRestrictionCallback_.thisVar == nullptr) {
1327         TELEPHONY_LOGE("getRestrictionCallback is null!");
1328         return CALL_ERR_CALLBACK_NOT_EXIST;
1329     }
1330     uv_loop_s *loop = nullptr;
1331 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1332     napi_get_uv_event_loop(getRestrictionCallback_.env, &loop);
1333 #endif
1334     if (loop == nullptr) {
1335         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1336     }
1337     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1338     if (callSupplementWorker == nullptr) {
1339         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1340         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1341     }
1342     callSupplementWorker->info = resultInfo;
1343     callSupplementWorker->callback = getRestrictionCallback_;
1344     uv_work_t *work = std::make_unique<uv_work_t>().release();
1345     if (work == nullptr) {
1346         delete callSupplementWorker;
1347         callSupplementWorker = nullptr;
1348         TELEPHONY_LOGE("work is nullptr!");
1349         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1350     }
1351     work->data = (void *)callSupplementWorker;
1352     int32_t resultCode = uv_queue_work_with_qos(
1353         loop, work, [](uv_work_t *work) {
1354             TELEPHONY_LOGD("ReportGetRestrictionInfo uv_queue_work_with_qos");
1355         }, ReportWaitAndLimitInfoWork, uv_qos_default);
1356     if (resultCode != 0) {
1357         delete callSupplementWorker;
1358         callSupplementWorker = nullptr;
1359         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1360         delete work;
1361         work = nullptr;
1362         return TELEPHONY_ERROR;
1363     }
1364     if (getRestrictionCallback_.thisVar) {
1365         (void)memset_s(&getRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1366     }
1367     return TELEPHONY_SUCCESS;
1368 }
1369 
ReportSetRestrictionInfo(AppExecFwk::PacMap & resultInfo)1370 int32_t NapiCallAbilityCallback::ReportSetRestrictionInfo(AppExecFwk::PacMap &resultInfo)
1371 {
1372     if (setRestrictionCallback_.thisVar == nullptr) {
1373         TELEPHONY_LOGE("setRestrictionCallback is null!");
1374         return CALL_ERR_CALLBACK_NOT_EXIST;
1375     }
1376     uv_loop_s *loop = nullptr;
1377 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1378     napi_get_uv_event_loop(setRestrictionCallback_.env, &loop);
1379 #endif
1380     if (loop == nullptr) {
1381         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1382     }
1383     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1384     if (callSupplementWorker == nullptr) {
1385         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1386         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1387     }
1388     callSupplementWorker->info = resultInfo;
1389     callSupplementWorker->callback = setRestrictionCallback_;
1390     uv_work_t *work = std::make_unique<uv_work_t>().release();
1391     if (work == nullptr) {
1392         delete callSupplementWorker;
1393         callSupplementWorker = nullptr;
1394         TELEPHONY_LOGE("work is nullptr!");
1395         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1396     }
1397     work->data = (void *)callSupplementWorker;
1398     int32_t resultCode = uv_queue_work_with_qos(
1399         loop, work, [](uv_work_t *work) {
1400             TELEPHONY_LOGD("ReportSetRestrictionInfo uv_queue_work_with_qos");
1401         }, ReportExecutionResultWork, uv_qos_default);
1402     if (resultCode != 0) {
1403         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1404         delete callSupplementWorker;
1405         callSupplementWorker = nullptr;
1406         delete work;
1407         work = nullptr;
1408         return TELEPHONY_ERROR;
1409     }
1410     if (setRestrictionCallback_.thisVar) {
1411         (void)memset_s(&setRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1412     }
1413     return TELEPHONY_SUCCESS;
1414 }
1415 
ReportSetRestrictionPassword(AppExecFwk::PacMap & resultInfo)1416 int32_t NapiCallAbilityCallback::ReportSetRestrictionPassword(AppExecFwk::PacMap &resultInfo)
1417 {
1418     std::lock_guard<std::mutex> lock(setRestrictionPasswordCallbackMutex_);
1419     if (setRestrictionPasswordCallback_.thisVar == nullptr) {
1420         TELEPHONY_LOGE("setRestrictionPasswordCallback is null!");
1421         return CALL_ERR_CALLBACK_NOT_EXIST;
1422     }
1423     uv_loop_s *loop = nullptr;
1424 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1425     napi_get_uv_event_loop(setRestrictionPasswordCallback_.env, &loop);
1426 #endif
1427     if (loop == nullptr) {
1428         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1429     }
1430     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1431     if (callSupplementWorker == nullptr) {
1432         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1433         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1434     }
1435     callSupplementWorker->info = resultInfo;
1436     callSupplementWorker->callback = setRestrictionPasswordCallback_;
1437     uv_work_t *work = std::make_unique<uv_work_t>().release();
1438     if (work == nullptr) {
1439         delete callSupplementWorker;
1440         callSupplementWorker = nullptr;
1441         TELEPHONY_LOGE("work is nullptr!");
1442         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1443     }
1444     work->data = (void *)callSupplementWorker;
1445     int32_t errCode = uv_queue_work_with_qos(
1446         loop, work, [](uv_work_t *work) {
1447             TELEPHONY_LOGD("ReportSetRestrictionPassword uv_queue_work_with_qos");
1448         }, ReportExecutionResultWork, uv_qos_default);
1449     if (errCode != 0) {
1450         delete callSupplementWorker;
1451         callSupplementWorker = nullptr;
1452         delete work;
1453         work = nullptr;
1454         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1455         return TELEPHONY_ERROR;
1456     }
1457     if (setRestrictionPasswordCallback_.thisVar) {
1458         (void)memset_s(&setRestrictionPasswordCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1459     }
1460     return TELEPHONY_SUCCESS;
1461 }
1462 
ReportGetTransferInfo(AppExecFwk::PacMap & resultInfo)1463 int32_t NapiCallAbilityCallback::ReportGetTransferInfo(AppExecFwk::PacMap &resultInfo)
1464 {
1465     std::lock_guard<std::mutex> lock(getTransferCallbackMutex_);
1466     if (getTransferCallback_.thisVar == nullptr) {
1467         TELEPHONY_LOGE("getTransferCallback is null!");
1468         return CALL_ERR_CALLBACK_NOT_EXIST;
1469     }
1470     if (getCallTransferReason_ != resultInfo.GetIntValue("reason")) {
1471         TELEPHONY_LOGE("Transfer reason is different, require is %{public}d, now is %{public}d", getCallTransferReason_,
1472             resultInfo.GetIntValue("reason"));
1473         return CALL_ERR_CALLBACK_NOT_EXIST;
1474     }
1475     uv_loop_s *loop = nullptr;
1476 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1477     napi_get_uv_event_loop(getTransferCallback_.env, &loop);
1478 #endif
1479     if (loop == nullptr) {
1480         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1481     }
1482     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1483     if (callSupplementWorker == nullptr) {
1484         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1485         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1486     }
1487     callSupplementWorker->info = resultInfo;
1488     callSupplementWorker->callback = getTransferCallback_;
1489     uv_work_t *work = std::make_unique<uv_work_t>().release();
1490     if (work == nullptr) {
1491         delete callSupplementWorker;
1492         callSupplementWorker = nullptr;
1493         TELEPHONY_LOGE("work is nullptr!");
1494         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1495     }
1496     work->data = (void *)callSupplementWorker;
1497     int32_t errCode = uv_queue_work_with_qos(
1498         loop, work, [](uv_work_t *work) {
1499             TELEPHONY_LOGD("ReportGetTransferInfo uv_queue_work_with_qos");
1500         }, ReportSupplementInfoWork, uv_qos_default);
1501     if (errCode != 0) {
1502         TELEPHONY_LOGE("failed add to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1503         delete callSupplementWorker;
1504         callSupplementWorker = nullptr;
1505         delete work;
1506         work = nullptr;
1507         return TELEPHONY_ERROR;
1508     }
1509     if (getTransferCallback_.thisVar) {
1510         (void)memset_s(&getTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1511     }
1512     return TELEPHONY_SUCCESS;
1513 }
1514 
ReportSetTransferInfo(AppExecFwk::PacMap & resultInfo)1515 int32_t NapiCallAbilityCallback::ReportSetTransferInfo(AppExecFwk::PacMap &resultInfo)
1516 {
1517     std::lock_guard<std::mutex> lock(setTransferCallbackMutex_);
1518     if (setTransferCallback_.thisVar == nullptr) {
1519         TELEPHONY_LOGE("setTransferCallback is null!");
1520         return CALL_ERR_CALLBACK_NOT_EXIST;
1521     }
1522     uv_loop_s *loop = nullptr;
1523 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1524     napi_get_uv_event_loop(setTransferCallback_.env, &loop);
1525 #endif
1526     if (loop == nullptr) {
1527         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1528     }
1529     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1530     if (callSupplementWorker == nullptr) {
1531         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1532         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1533     }
1534     callSupplementWorker->info = resultInfo;
1535     callSupplementWorker->callback = setTransferCallback_;
1536     uv_work_t *work = std::make_unique<uv_work_t>().release();
1537     if (work == nullptr) {
1538         delete callSupplementWorker;
1539         callSupplementWorker = nullptr;
1540         TELEPHONY_LOGE("work is nullptr!");
1541         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1542     }
1543     work->data = (void *)callSupplementWorker;
1544     int32_t errCode = uv_queue_work_with_qos(
1545         loop, work, [](uv_work_t *work) {
1546             TELEPHONY_LOGD("ReportSetTransferInfo uv_queue_work_with_qos");
1547         }, ReportExecutionResultWork, uv_qos_default);
1548     if (errCode != 0) {
1549         delete callSupplementWorker;
1550         callSupplementWorker = nullptr;
1551         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1552         delete work;
1553         work = nullptr;
1554         return TELEPHONY_ERROR;
1555     }
1556     if (setTransferCallback_.thisVar) {
1557         (void)memset_s(&setTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1558     }
1559     return TELEPHONY_SUCCESS;
1560 }
1561 
ReportWaitAndLimitInfoWork(uv_work_t * work,int32_t status)1562 void NapiCallAbilityCallback::ReportWaitAndLimitInfoWork(uv_work_t *work, int32_t status)
1563 {
1564     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1565     if (dataWorkerData == nullptr) {
1566         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1567         return;
1568     }
1569     ReportWaitAndLimitInfo(dataWorkerData->info, dataWorkerData->callback);
1570     delete dataWorkerData;
1571     dataWorkerData = nullptr;
1572     delete work;
1573     work = nullptr;
1574 }
1575 
ReportWaitAndLimitInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1576 void NapiCallAbilityCallback::ReportWaitAndLimitInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1577 {
1578     napi_env env = supplementInfo.env;
1579     int32_t result = resultInfo.GetIntValue("result");
1580     int32_t status = resultInfo.GetIntValue("status");
1581     napi_handle_scope limitScope = nullptr;
1582     napi_open_handle_scope(env, &limitScope);
1583     if (limitScope == nullptr) {
1584         TELEPHONY_LOGE("limitScope is nullptr");
1585         napi_close_handle_scope(env, limitScope);
1586         return;
1587     }
1588     if (supplementInfo.callbackRef != nullptr) {
1589         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1590         if (result == TELEPHONY_SUCCESS) {
1591             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1592             napi_create_int32(env, status, &callbackValues[ARRAY_INDEX_SECOND]);
1593         } else {
1594             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1595             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1596             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1597         }
1598         napi_value callbackFunc = nullptr;
1599         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1600         napi_value thisVar = nullptr;
1601         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1602         napi_value callbackResult = nullptr;
1603         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1604         napi_delete_reference(env, supplementInfo.callbackRef);
1605         napi_delete_reference(env, supplementInfo.thisVar);
1606     } else if (supplementInfo.deferred != nullptr) {
1607         if (result == TELEPHONY_SUCCESS) {
1608             napi_value promiseValue = nullptr;
1609             napi_create_int32(env, status, &promiseValue);
1610             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1611         } else {
1612             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1613             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1614         }
1615     }
1616     napi_close_handle_scope(env, limitScope);
1617 }
1618 
ReportSupplementInfoWork(uv_work_t * work,int32_t status)1619 void NapiCallAbilityCallback::ReportSupplementInfoWork(uv_work_t *work, int32_t status)
1620 {
1621     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1622     if (dataWorkerData == nullptr) {
1623         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1624         return;
1625     }
1626     ReportSupplementInfo(dataWorkerData->info, dataWorkerData->callback);
1627     delete dataWorkerData;
1628     dataWorkerData = nullptr;
1629     delete work;
1630     work = nullptr;
1631 }
1632 
ReportSupplementInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1633 void NapiCallAbilityCallback::ReportSupplementInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1634 {
1635     napi_env env = supplementInfo.env;
1636     napi_handle_scope supplementScope = nullptr;
1637     napi_open_handle_scope(env, &supplementScope);
1638     if (supplementScope == nullptr) {
1639         TELEPHONY_LOGE("supplementScope is nullptr");
1640         napi_close_handle_scope(env, supplementScope);
1641         return;
1642     }
1643     napi_value callbackValue = nullptr;
1644     napi_create_object(env, &callbackValue);
1645     NapiCallManagerUtils::SetPropertyInt32(env, callbackValue, "status", resultInfo.GetIntValue("status"));
1646     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValue, "number", resultInfo.GetStringValue("number"));
1647     int32_t result = resultInfo.GetIntValue("result");
1648     if (supplementInfo.callbackRef != nullptr) {
1649         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1650         if (result == TELEPHONY_SUCCESS) {
1651             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1652             callbackValues[ARRAY_INDEX_SECOND] = callbackValue;
1653         } else {
1654             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1655             callbackValues[ARRAY_INDEX_FIRST] =
1656                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
1657             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1658         }
1659         napi_value callbackFunc = nullptr;
1660         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1661         napi_value thisVar = nullptr;
1662         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1663         napi_value callbackResult = nullptr;
1664         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1665         napi_delete_reference(env, supplementInfo.callbackRef);
1666         napi_delete_reference(env, supplementInfo.thisVar);
1667     } else if (supplementInfo.deferred != nullptr) {
1668         if (result == TELEPHONY_SUCCESS) {
1669             napi_resolve_deferred(env, supplementInfo.deferred, callbackValue);
1670         } else {
1671             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1672             napi_reject_deferred(env, supplementInfo.deferred,
1673                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode));
1674         }
1675     }
1676     napi_close_handle_scope(env, supplementScope);
1677 }
1678 
ReportExecutionResultWork(uv_work_t * work,int32_t status)1679 void NapiCallAbilityCallback::ReportExecutionResultWork(uv_work_t *work, int32_t status)
1680 {
1681     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1682     if (dataWorkerData == nullptr) {
1683         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1684         return;
1685     }
1686     ReportExecutionResult(dataWorkerData->callback, dataWorkerData->info);
1687     delete dataWorkerData;
1688     dataWorkerData = nullptr;
1689     delete work;
1690     work = nullptr;
1691 }
1692 
ReportExecutionResult(EventCallback & settingInfo,AppExecFwk::PacMap & resultInfo)1693 void NapiCallAbilityCallback::ReportExecutionResult(EventCallback &settingInfo, AppExecFwk::PacMap &resultInfo)
1694 {
1695     napi_env env = settingInfo.env;
1696     napi_handle_scope executionScope = nullptr;
1697     napi_open_handle_scope(env, &executionScope);
1698     if (executionScope == nullptr) {
1699         TELEPHONY_LOGE("executionScope is nullptr");
1700         napi_close_handle_scope(env, executionScope);
1701         return;
1702     }
1703     napi_value callbackValue = nullptr;
1704     napi_create_object(env, &callbackValue);
1705     int32_t result = resultInfo.GetIntValue("result");
1706     if (settingInfo.callbackRef != nullptr) {
1707         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1708         if (result == TELEPHONY_SUCCESS) {
1709             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1710             napi_get_null(env, &callbackValues[ARRAY_INDEX_SECOND]);
1711         } else {
1712             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1713             callbackValues[ARRAY_INDEX_FIRST] =
1714                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
1715             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1716         }
1717         napi_value callbackFunc = nullptr;
1718         napi_get_reference_value(env, settingInfo.callbackRef, &callbackFunc);
1719         napi_value thisVar = nullptr;
1720         napi_get_reference_value(env, settingInfo.thisVar, &thisVar);
1721         napi_value callbackResult = nullptr;
1722         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1723         napi_delete_reference(env, settingInfo.callbackRef);
1724         napi_delete_reference(env, settingInfo.thisVar);
1725     } else if (settingInfo.deferred != nullptr) {
1726         if (result == TELEPHONY_SUCCESS) {
1727             napi_value promiseValue = nullptr;
1728             napi_get_null(env, &promiseValue);
1729             napi_resolve_deferred(env, settingInfo.deferred, promiseValue);
1730         } else {
1731             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1732             napi_reject_deferred(env, settingInfo.deferred,
1733                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode));
1734         }
1735     }
1736     napi_close_handle_scope(env, executionScope);
1737 }
1738 
ReportStartRttInfoWork(uv_work_t * work,int32_t status)1739 void NapiCallAbilityCallback::ReportStartRttInfoWork(uv_work_t *work, int32_t status)
1740 {
1741     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1742     if (dataWorkerData == nullptr) {
1743         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1744         return;
1745     }
1746     ReportStartRttInfo(dataWorkerData->info, dataWorkerData->callback);
1747     delete dataWorkerData;
1748     dataWorkerData = nullptr;
1749     delete work;
1750     work = nullptr;
1751 }
1752 
ReportStartRttInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1753 void NapiCallAbilityCallback::ReportStartRttInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1754 {
1755     napi_env env = supplementInfo.env;
1756     int32_t result = resultInfo.GetIntValue("result");
1757     napi_handle_scope startRttScope = nullptr;
1758     napi_open_handle_scope(env, &startRttScope);
1759     if (startRttScope == nullptr) {
1760         TELEPHONY_LOGE("startRttScope is nullptr");
1761         napi_close_handle_scope(env, startRttScope);
1762         return;
1763     }
1764     if (supplementInfo.callbackRef != nullptr) {
1765         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1766         if (result == TELEPHONY_SUCCESS) {
1767             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1768         } else {
1769             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1770             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1771             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1772         }
1773         napi_value callbackFunc = nullptr;
1774         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1775         napi_value thisVar = nullptr;
1776         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1777         napi_value callbackResult = nullptr;
1778         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1779         napi_delete_reference(env, supplementInfo.callbackRef);
1780         napi_delete_reference(env, supplementInfo.thisVar);
1781     } else if (supplementInfo.deferred != nullptr) {
1782         if (result == TELEPHONY_SUCCESS) {
1783             napi_value promiseValue = nullptr;
1784             napi_get_null(env, &promiseValue);
1785             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1786         } else {
1787             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1788             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1789         }
1790     }
1791     napi_close_handle_scope(env, startRttScope);
1792 }
1793 
ReportStopRttInfoWork(uv_work_t * work,int32_t status)1794 void NapiCallAbilityCallback::ReportStopRttInfoWork(uv_work_t *work, int32_t status)
1795 {
1796     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1797     if (dataWorkerData == nullptr) {
1798         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1799         return;
1800     }
1801     ReportStopRttInfo(dataWorkerData->info, dataWorkerData->callback);
1802     delete dataWorkerData;
1803     dataWorkerData = nullptr;
1804     delete work;
1805     work = nullptr;
1806 }
1807 
ReportStopRttInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1808 void NapiCallAbilityCallback::ReportStopRttInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1809 {
1810     napi_env env = supplementInfo.env;
1811     int32_t result = resultInfo.GetIntValue("result");
1812     napi_handle_scope stopRttScope = nullptr;
1813     napi_open_handle_scope(env, &stopRttScope);
1814     if (stopRttScope == nullptr) {
1815         TELEPHONY_LOGE("stopRttScope is nullptr");
1816         napi_close_handle_scope(env, stopRttScope);
1817         return;
1818     }
1819     if (supplementInfo.callbackRef != nullptr) {
1820         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1821         if (result == TELEPHONY_SUCCESS) {
1822             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1823         } else {
1824             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1825             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1826             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1827         }
1828         napi_value callbackFunc = nullptr;
1829         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1830         napi_value thisVar = nullptr;
1831         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1832         napi_value callbackResult = nullptr;
1833         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1834         napi_delete_reference(env, supplementInfo.callbackRef);
1835         napi_delete_reference(env, supplementInfo.thisVar);
1836     } else if (supplementInfo.deferred != nullptr) {
1837         if (result == TELEPHONY_SUCCESS) {
1838             napi_value promiseValue = nullptr;
1839             napi_get_null(env, &promiseValue);
1840             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1841         } else {
1842             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1843             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1844         }
1845     }
1846     napi_close_handle_scope(env, stopRttScope);
1847 }
1848 
ReportCallOttWork(uv_work_t * work,int32_t status)1849 void NapiCallAbilityCallback::ReportCallOttWork(uv_work_t *work, int32_t status)
1850 {
1851     CallOttWorker *dataWorkerData = (CallOttWorker *)work->data;
1852     if (dataWorkerData == nullptr) {
1853         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1854         return;
1855     }
1856     ReportCallOtt(dataWorkerData->callback, dataWorkerData->info, dataWorkerData->requestId);
1857     delete dataWorkerData;
1858     dataWorkerData = nullptr;
1859     delete work;
1860     work = nullptr;
1861 }
1862 
ReportCallOtt(EventCallback & settingInfo,AppExecFwk::PacMap & resultInfo,OttCallRequestId requestId)1863 int32_t NapiCallAbilityCallback::ReportCallOtt(
1864     EventCallback &settingInfo, AppExecFwk::PacMap &resultInfo, OttCallRequestId requestId)
1865 {
1866     napi_env env = settingInfo.env;
1867     napi_handle_scope callOttscope = nullptr;
1868     napi_open_handle_scope(env, &callOttscope);
1869     if (callOttscope == nullptr) {
1870         TELEPHONY_LOGE("callOttscope is nullptr");
1871         napi_close_handle_scope(env, callOttscope);
1872         return TELEPHONY_ERROR;
1873     }
1874     napi_value callbackFunc = nullptr;
1875     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1876     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
1877     NapiCallManagerUtils::SetPropertyInt32(
1878         env, callbackValues[ARRAY_INDEX_FIRST], "requestId", static_cast<int32_t>(requestId));
1879     NapiCallManagerUtils::SetPropertyStringUtf8(
1880         env, callbackValues[ARRAY_INDEX_FIRST], "phoneNumber", resultInfo.GetStringValue("phoneNumber").c_str());
1881     NapiCallManagerUtils::SetPropertyStringUtf8(
1882         env, callbackValues[ARRAY_INDEX_FIRST], "bundleName", resultInfo.GetStringValue("bundleName").c_str());
1883     NapiCallManagerUtils::SetPropertyInt32(
1884         env, callbackValues[ARRAY_INDEX_FIRST], "videoState", resultInfo.GetIntValue("videoState"));
1885     switch (requestId) {
1886         case OttCallRequestId::OTT_REQUEST_INVITE_TO_CONFERENCE:
1887             NapiCallManagerUtils::SetPropertyStringUtf8(
1888                 env, callbackValues[ARRAY_INDEX_FIRST], "number", resultInfo.GetStringValue("number").c_str());
1889             break;
1890         case OttCallRequestId::OTT_REQUEST_UPDATE_CALL_MEDIA_MODE:
1891             NapiCallManagerUtils::SetPropertyInt32(
1892                 env, callbackValues[ARRAY_INDEX_FIRST], "callMediaMode", resultInfo.GetIntValue("callMediaMode"));
1893             break;
1894         default:
1895             break;
1896     }
1897 
1898     napi_get_reference_value(env, settingInfo.callbackRef, &callbackFunc);
1899     if (callbackFunc == nullptr) {
1900         TELEPHONY_LOGE("callbackFunc is null!");
1901         napi_close_handle_scope(env, callOttscope);
1902         return CALL_ERR_CALLBACK_NOT_EXIST;
1903     }
1904     napi_value thisVar = nullptr;
1905     napi_get_reference_value(env, settingInfo.thisVar, &thisVar);
1906     napi_value callbackResult = nullptr;
1907     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1908     napi_close_handle_scope(env, callOttscope);
1909     return TELEPHONY_SUCCESS;
1910 }
1911 
UpdatePostDialDelay(const std::string str)1912 int32_t NapiCallAbilityCallback::UpdatePostDialDelay(const std::string str)
1913 {
1914     if (postDialDelayCallback_.thisVar == nullptr) {
1915         TELEPHONY_LOGE("thisVar is nullptr!");
1916         return CALL_ERR_CALLBACK_NOT_EXIST;
1917     }
1918     uv_loop_s *loop = nullptr;
1919 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1920     napi_get_uv_event_loop(postDialDelayCallback_.env, &loop);
1921 #endif
1922     if (loop == nullptr) {
1923         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1924     }
1925     PostDialDelayWorker *postDialDelayWorker = std::make_unique<PostDialDelayWorker>().release();
1926     if (postDialDelayWorker == nullptr) {
1927         TELEPHONY_LOGE("postDialDelayWorker is nullptr!");
1928         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1929     }
1930     postDialDelayWorker->postDialStr = str;
1931     postDialDelayWorker->callback = postDialDelayCallback_;
1932     uv_work_t *work = std::make_unique<uv_work_t>().release();
1933     if (work == nullptr) {
1934         delete postDialDelayWorker;
1935         postDialDelayWorker = nullptr;
1936         TELEPHONY_LOGE("work is nullptr");
1937         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1938     }
1939     work->data = (void *)postDialDelayWorker;
1940     int32_t errCode = uv_queue_work_with_qos(
1941         loop, work, [](uv_work_t *work) {
1942             TELEPHONY_LOGD("UpdatePostDialDelay uv_queue_work_with_qos");
1943         }, ReportPostDialDelayWork, uv_qos_default);
1944     if (errCode != 0) {
1945         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1946         delete postDialDelayWorker;
1947         postDialDelayWorker = nullptr;
1948         delete work;
1949         work = nullptr;
1950         return TELEPHONY_ERROR;
1951     }
1952     return TELEPHONY_SUCCESS;
1953 }
1954 
ReportPostDialDelayWork(uv_work_t * work,int32_t status)1955 void NapiCallAbilityCallback::ReportPostDialDelayWork(uv_work_t *work, int32_t status)
1956 {
1957     PostDialDelayWorker *dataWorkerData = (PostDialDelayWorker *)work->data;
1958     if (dataWorkerData == nullptr) {
1959         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1960         return;
1961     }
1962     int32_t ret = ReportPostDialDelay(dataWorkerData->postDialStr, dataWorkerData->callback);
1963     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
1964     delete dataWorkerData;
1965     dataWorkerData = nullptr;
1966     delete work;
1967     work = nullptr;
1968 }
1969 
ReportPostDialDelay(std::string postDialStr,EventCallback eventCallback)1970 int32_t NapiCallAbilityCallback::ReportPostDialDelay(std::string postDialStr, EventCallback eventCallback)
1971 {
1972     napi_env env = eventCallback.env;
1973     napi_handle_scope scope = nullptr;
1974     napi_open_handle_scope(env, &scope);
1975     if (scope == nullptr) {
1976         TELEPHONY_LOGE("scope is nullptr");
1977         napi_close_handle_scope(env, scope);
1978         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1979     }
1980     napi_value callbackFunc = nullptr;
1981     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
1982     napi_create_string_utf8(env, postDialStr.c_str(), postDialStr.length(), &callbackValues[ARRAY_INDEX_FIRST]);
1983     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
1984     if (callbackFunc == nullptr) {
1985         TELEPHONY_LOGE("callbackFunc is null!");
1986         napi_close_handle_scope(env, scope);
1987         return CALL_ERR_CALLBACK_NOT_EXIST;
1988     }
1989     napi_value thisVar = nullptr;
1990     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
1991     napi_value callbackResult = nullptr;
1992     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1993     napi_close_handle_scope(env, scope);
1994     return TELEPHONY_SUCCESS;
1995 }
1996 
UpdateImsCallModeChange(const CallMediaModeInfo & imsCallModeInfo)1997 int32_t NapiCallAbilityCallback::UpdateImsCallModeChange(const CallMediaModeInfo &imsCallModeInfo)
1998 {
1999     if (imsCallModeCallback_.thisVar == nullptr) {
2000         TELEPHONY_LOGE("thisVar is nullptr!");
2001         return CALL_ERR_CALLBACK_NOT_EXIST;
2002     }
2003     uv_loop_s *loop = nullptr;
2004 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2005     napi_get_uv_event_loop(imsCallModeCallback_.env, &loop);
2006 #endif
2007     if (loop == nullptr) {
2008         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2009     }
2010     ImsCallModeInfoWorker *imsCallModeInfoWorker = std::make_unique<ImsCallModeInfoWorker>().release();
2011     if (imsCallModeInfoWorker == nullptr) {
2012         TELEPHONY_LOGE("imsCallModeInfoWorker is nullptr!");
2013         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2014     }
2015     imsCallModeInfoWorker->callModeInfo = imsCallModeInfo;
2016     imsCallModeInfoWorker->callback = imsCallModeCallback_;
2017     uv_work_t *work = std::make_unique<uv_work_t>().release();
2018     if (work == nullptr) {
2019         delete imsCallModeInfoWorker;
2020         imsCallModeInfoWorker = nullptr;
2021         TELEPHONY_LOGE("work is nullptr");
2022         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2023     }
2024     work->data = (void *)imsCallModeInfoWorker;
2025     int32_t errCode = uv_queue_work_with_qos(
2026         loop, work, [](uv_work_t *work) {
2027             TELEPHONY_LOGD("UpdateImsCallModeChange uv_queue_work_with_qos");
2028         }, ReportCallMediaModeInfoWork, uv_qos_default);
2029     if (errCode != 0) {
2030         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2031         delete imsCallModeInfoWorker;
2032         imsCallModeInfoWorker = nullptr;
2033         delete work;
2034         work = nullptr;
2035         return TELEPHONY_ERROR;
2036     }
2037     return TELEPHONY_SUCCESS;
2038 }
2039 
ReportCallMediaModeInfoWork(uv_work_t * work,int32_t status)2040 void NapiCallAbilityCallback::ReportCallMediaModeInfoWork(uv_work_t *work, int32_t status)
2041 {
2042     ImsCallModeInfoWorker *dataWorkerData = (ImsCallModeInfoWorker *)work->data;
2043     if (dataWorkerData == nullptr) {
2044         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2045         return;
2046     }
2047     int32_t ret = ReportCallMediaModeInfo(dataWorkerData->callModeInfo, dataWorkerData->callback);
2048     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2049     delete dataWorkerData;
2050     dataWorkerData = nullptr;
2051     delete work;
2052     work = nullptr;
2053 }
2054 
ReportCallMediaModeInfo(CallMediaModeInfo & imsCallModeInfo,EventCallback eventCallback)2055 int32_t NapiCallAbilityCallback::ReportCallMediaModeInfo(
2056     CallMediaModeInfo &imsCallModeInfo, EventCallback eventCallback)
2057 {
2058     napi_env env = eventCallback.env;
2059     napi_handle_scope CallMediaModeInfoScope = nullptr;
2060     napi_open_handle_scope(env, &CallMediaModeInfoScope);
2061     if (CallMediaModeInfoScope == nullptr) {
2062         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2063         napi_close_handle_scope(env, CallMediaModeInfoScope);
2064         return TELEPHONY_ERROR;
2065     }
2066     napi_value callbackFunc = nullptr;
2067     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2068 
2069     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2070     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", imsCallModeInfo.callId);
2071     NapiCallManagerUtils::SetPropertyBoolean(
2072         env, callbackValues[ARRAY_INDEX_FIRST], "isRequestInfo", imsCallModeInfo.isRequestInfo);
2073     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "result", imsCallModeInfo.result);
2074     NapiCallManagerUtils::SetPropertyInt32(
2075         env, callbackValues[ARRAY_INDEX_FIRST], "imsCallMode", static_cast<int32_t>(imsCallModeInfo.callMode));
2076 
2077     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2078     if (callbackFunc == nullptr) {
2079         TELEPHONY_LOGE("callbackFunc is null!");
2080         napi_close_handle_scope(env, CallMediaModeInfoScope);
2081         return CALL_ERR_CALLBACK_NOT_EXIST;
2082     }
2083     napi_value thisVar = nullptr;
2084     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2085     napi_value callbackResult = nullptr;
2086     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2087     napi_close_handle_scope(env, CallMediaModeInfoScope);
2088     return TELEPHONY_SUCCESS;
2089 }
2090 
CallSessionEventChange(const CallSessionEvent & callSessionEvent)2091 int32_t NapiCallAbilityCallback::CallSessionEventChange(const CallSessionEvent &callSessionEvent)
2092 {
2093     if (callSessionEventCallback_.thisVar == nullptr) {
2094         TELEPHONY_LOGE("thisVar is nullptr!");
2095         return CALL_ERR_CALLBACK_NOT_EXIST;
2096     }
2097     uv_loop_s *loop = nullptr;
2098 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2099     napi_get_uv_event_loop(callSessionEventCallback_.env, &loop);
2100 #endif
2101     if (loop == nullptr) {
2102         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2103     }
2104     CallSessionEventWorker *callSessionEventWorker = std::make_unique<CallSessionEventWorker>().release();
2105     if (callSessionEventWorker == nullptr) {
2106         TELEPHONY_LOGE("callSessionEventWorker is nullptr!");
2107         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2108     }
2109     callSessionEventWorker->sessionEvent = callSessionEvent;
2110     callSessionEventWorker->callback = callSessionEventCallback_;
2111     uv_work_t *work = std::make_unique<uv_work_t>().release();
2112     if (work == nullptr) {
2113         delete callSessionEventWorker;
2114         callSessionEventWorker = nullptr;
2115         TELEPHONY_LOGE("work is nullptr");
2116         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2117     }
2118     work->data = (void *)callSessionEventWorker;
2119     int32_t errCode = uv_queue_work_with_qos(
2120         loop, work, [](uv_work_t *work) {
2121             TELEPHONY_LOGD("CallSessionEventChange uv_queue_work_with_qos");
2122         }, ReportCallSessionEventWork, uv_qos_default);
2123     if (errCode != 0) {
2124         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2125         delete callSessionEventWorker;
2126         callSessionEventWorker = nullptr;
2127         delete work;
2128         work = nullptr;
2129         return TELEPHONY_ERROR;
2130     }
2131     return TELEPHONY_SUCCESS;
2132 }
2133 
ReportCallSessionEventWork(uv_work_t * work,int32_t status)2134 void NapiCallAbilityCallback::ReportCallSessionEventWork(uv_work_t *work, int32_t status)
2135 {
2136     CallSessionEventWorker *dataWorkerData = (CallSessionEventWorker *)work->data;
2137     if (dataWorkerData == nullptr) {
2138         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2139         return;
2140     }
2141     int32_t ret = ReportCallSessionEvent(dataWorkerData->sessionEvent, dataWorkerData->callback);
2142     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2143     delete dataWorkerData;
2144     dataWorkerData = nullptr;
2145     delete work;
2146     work = nullptr;
2147 }
2148 
ReportCallSessionEvent(CallSessionEvent & sessionEvent,EventCallback eventCallback)2149 int32_t NapiCallAbilityCallback::ReportCallSessionEvent(
2150     CallSessionEvent &sessionEvent, EventCallback eventCallback)
2151 {
2152     napi_env env = eventCallback.env;
2153     napi_handle_scope CallSessionEventScope = nullptr;
2154     napi_open_handle_scope(env, &CallSessionEventScope);
2155     if (CallSessionEventScope == nullptr) {
2156         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2157         napi_close_handle_scope(env, CallSessionEventScope);
2158         return TELEPHONY_ERROR;
2159     }
2160     napi_value callbackFunc = nullptr;
2161     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2162 
2163     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2164     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", sessionEvent.callId);
2165     NapiCallManagerUtils::SetPropertyInt32(
2166         env, callbackValues[ARRAY_INDEX_FIRST], "eventId", static_cast<int32_t>(sessionEvent.eventId));
2167 
2168     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2169     if (callbackFunc == nullptr) {
2170         TELEPHONY_LOGE("callbackFunc is null!");
2171         napi_close_handle_scope(env, CallSessionEventScope);
2172         return CALL_ERR_CALLBACK_NOT_EXIST;
2173     }
2174     napi_value thisVar = nullptr;
2175     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2176     napi_value callbackResult = nullptr;
2177     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2178     napi_close_handle_scope(env, CallSessionEventScope);
2179     return TELEPHONY_SUCCESS;
2180 }
2181 
PeerDimensionsChange(const PeerDimensionsDetail & peerDimensionsDetail)2182 int32_t NapiCallAbilityCallback::PeerDimensionsChange(const PeerDimensionsDetail &peerDimensionsDetail)
2183 {
2184     if (peerDimensionsCallback_.thisVar == nullptr) {
2185         TELEPHONY_LOGE("thisVar is nullptr!");
2186         return CALL_ERR_CALLBACK_NOT_EXIST;
2187     }
2188     uv_loop_s *loop = nullptr;
2189 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2190     napi_get_uv_event_loop(peerDimensionsCallback_.env, &loop);
2191 #endif
2192     if (loop == nullptr) {
2193         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2194     }
2195     PeerDimensionsWorker *peerDimensionsWorker = std::make_unique<PeerDimensionsWorker>().release();
2196     if (peerDimensionsWorker == nullptr) {
2197         TELEPHONY_LOGE("peerDimensionsWorker is nullptr!");
2198         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2199     }
2200     peerDimensionsWorker->peerDimensionsDetail = peerDimensionsDetail;
2201     peerDimensionsWorker->callback = peerDimensionsCallback_;
2202     uv_work_t *work = std::make_unique<uv_work_t>().release();
2203     if (work == nullptr) {
2204         delete peerDimensionsWorker;
2205         peerDimensionsWorker = nullptr;
2206         TELEPHONY_LOGE("work is nullptr");
2207         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2208     }
2209     work->data = (void *)peerDimensionsWorker;
2210     int32_t errCode = uv_queue_work_with_qos(
2211         loop, work, [](uv_work_t *work) {
2212             TELEPHONY_LOGD("PeerDimensionsChange uv_queue_work_with_qos");
2213         }, ReportPeerDimensionsWork, uv_qos_default);
2214     if (errCode != 0) {
2215         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2216         delete peerDimensionsWorker;
2217         peerDimensionsWorker = nullptr;
2218         delete work;
2219         work = nullptr;
2220         return TELEPHONY_ERROR;
2221     }
2222     return TELEPHONY_SUCCESS;
2223 }
2224 
ReportPeerDimensionsWork(uv_work_t * work,int32_t status)2225 void NapiCallAbilityCallback::ReportPeerDimensionsWork(uv_work_t *work, int32_t status)
2226 {
2227     PeerDimensionsWorker *dataWorkerData = (PeerDimensionsWorker *)work->data;
2228     if (dataWorkerData == nullptr) {
2229         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2230         return;
2231     }
2232     int32_t ret = ReportPeerDimensions(dataWorkerData->peerDimensionsDetail, dataWorkerData->callback);
2233     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2234     delete dataWorkerData;
2235     dataWorkerData = nullptr;
2236     delete work;
2237     work = nullptr;
2238 }
2239 
ReportPeerDimensions(PeerDimensionsDetail & peerDimensionsDetail,EventCallback eventCallback)2240 int32_t NapiCallAbilityCallback::ReportPeerDimensions(
2241     PeerDimensionsDetail &peerDimensionsDetail, EventCallback eventCallback)
2242 {
2243     napi_env env = eventCallback.env;
2244     napi_handle_scope PeerDimensionsDetailScope = nullptr;
2245     napi_open_handle_scope(env, &PeerDimensionsDetailScope);
2246     if (PeerDimensionsDetailScope == nullptr) {
2247         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2248         napi_close_handle_scope(env, PeerDimensionsDetailScope);
2249         return TELEPHONY_ERROR;
2250     }
2251     napi_value callbackFunc = nullptr;
2252     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2253 
2254     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2255     NapiCallManagerUtils::SetPropertyInt32(
2256         env, callbackValues[ARRAY_INDEX_FIRST], "callId", peerDimensionsDetail.callId);
2257     NapiCallManagerUtils::SetPropertyInt32(
2258         env, callbackValues[ARRAY_INDEX_FIRST], "width", peerDimensionsDetail.width);
2259     NapiCallManagerUtils::SetPropertyInt32(
2260         env, callbackValues[ARRAY_INDEX_FIRST], "height", peerDimensionsDetail.height);
2261 
2262     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2263     if (callbackFunc == nullptr) {
2264         TELEPHONY_LOGE("callbackFunc is null!");
2265         napi_close_handle_scope(env, PeerDimensionsDetailScope);
2266         return CALL_ERR_CALLBACK_NOT_EXIST;
2267     }
2268     napi_value thisVar = nullptr;
2269     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2270     napi_value callbackResult = nullptr;
2271     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2272     napi_close_handle_scope(env, PeerDimensionsDetailScope);
2273     return TELEPHONY_SUCCESS;
2274 }
2275 
CallDataUsageChange(const int64_t dataUsage)2276 int32_t NapiCallAbilityCallback::CallDataUsageChange(const int64_t dataUsage)
2277 {
2278     if (callDataUsageCallback_.thisVar == nullptr) {
2279         TELEPHONY_LOGE("thisVar is nullptr!");
2280         return CALL_ERR_CALLBACK_NOT_EXIST;
2281     }
2282     uv_loop_s *loop = nullptr;
2283 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2284     napi_get_uv_event_loop(callDataUsageCallback_.env, &loop);
2285 #endif
2286     if (loop == nullptr) {
2287         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2288     }
2289     CallDataUsageWorker *callDataUsageWorker = std::make_unique<CallDataUsageWorker>().release();
2290     if (callDataUsageWorker == nullptr) {
2291         TELEPHONY_LOGE("callDataUsageWorker is nullptr!");
2292         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2293     }
2294     callDataUsageWorker->callDataUsage = dataUsage;
2295     callDataUsageWorker->callback = callDataUsageCallback_;
2296     uv_work_t *work = std::make_unique<uv_work_t>().release();
2297     if (work == nullptr) {
2298         delete callDataUsageWorker;
2299         callDataUsageWorker = nullptr;
2300         TELEPHONY_LOGE("work is nullptr");
2301         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2302     }
2303     work->data = (void *)callDataUsageWorker;
2304     int32_t errCode = uv_queue_work_with_qos(
2305         loop, work, [](uv_work_t *work) {
2306             TELEPHONY_LOGD("CallDataUsageChange uv_queue_work_with_qos");
2307         }, ReportCallDataUsageWork, uv_qos_default);
2308     if (errCode != 0) {
2309         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2310         delete callDataUsageWorker;
2311         callDataUsageWorker = nullptr;
2312         delete work;
2313         work = nullptr;
2314         return TELEPHONY_ERROR;
2315     }
2316     return TELEPHONY_SUCCESS;
2317 }
2318 
ReportCallDataUsageWork(uv_work_t * work,int32_t status)2319 void NapiCallAbilityCallback::ReportCallDataUsageWork(uv_work_t *work, int32_t status)
2320 {
2321     CallDataUsageWorker *dataWorkerData = (CallDataUsageWorker *)work->data;
2322     if (dataWorkerData == nullptr) {
2323         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2324         return;
2325     }
2326     int32_t ret = ReportCallDataUsage(dataWorkerData->callDataUsage, dataWorkerData->callback);
2327     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2328     delete dataWorkerData;
2329     dataWorkerData = nullptr;
2330     delete work;
2331     work = nullptr;
2332 }
2333 
ReportCallDataUsage(int64_t dataUsage,EventCallback eventCallback)2334 int32_t NapiCallAbilityCallback::ReportCallDataUsage(int64_t dataUsage, EventCallback eventCallback)
2335 {
2336     napi_env env = eventCallback.env;
2337     napi_handle_scope CallDataUsageScope = nullptr;
2338     napi_open_handle_scope(env, &CallDataUsageScope);
2339     if (CallDataUsageScope == nullptr) {
2340         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2341         napi_close_handle_scope(env, CallDataUsageScope);
2342         return TELEPHONY_ERROR;
2343     }
2344     napi_value callbackFunc = nullptr;
2345     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2346     napi_create_int64(env, dataUsage, &callbackValues[ARRAY_INDEX_FIRST]);
2347 
2348     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2349     if (callbackFunc == nullptr) {
2350         TELEPHONY_LOGE("callbackFunc is null!");
2351         napi_close_handle_scope(env, CallDataUsageScope);
2352         return CALL_ERR_CALLBACK_NOT_EXIST;
2353     }
2354     napi_value thisVar = nullptr;
2355     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2356     napi_value callbackResult = nullptr;
2357     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2358     napi_close_handle_scope(env, CallDataUsageScope);
2359     return TELEPHONY_SUCCESS;
2360 }
2361 
UpdateCameraCapabilities(const CameraCapabilities & cameraCapabilities)2362 int32_t NapiCallAbilityCallback::UpdateCameraCapabilities(const CameraCapabilities &cameraCapabilities)
2363 {
2364     if (cameraCapabilitiesCallback_.thisVar == nullptr) {
2365         TELEPHONY_LOGE("thisVar is nullptr!");
2366         return CALL_ERR_CALLBACK_NOT_EXIST;
2367     }
2368     uv_loop_s *loop = nullptr;
2369 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2370     napi_get_uv_event_loop(cameraCapabilitiesCallback_.env, &loop);
2371 #endif
2372     if (loop == nullptr) {
2373         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2374     }
2375     CameraCapbilitiesWorker *cameraCapbilitiesWorker = std::make_unique<CameraCapbilitiesWorker>().release();
2376     if (cameraCapbilitiesWorker == nullptr) {
2377         TELEPHONY_LOGE("cameraCapbilitiesWorker is nullptr!");
2378         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2379     }
2380     cameraCapbilitiesWorker->cameraCapabilities = cameraCapabilities;
2381     cameraCapbilitiesWorker->callback = cameraCapabilitiesCallback_;
2382     uv_work_t *work = std::make_unique<uv_work_t>().release();
2383     if (work == nullptr) {
2384         delete cameraCapbilitiesWorker;
2385         cameraCapbilitiesWorker = nullptr;
2386         TELEPHONY_LOGE("work is nullptr");
2387         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2388     }
2389     work->data = (void *)cameraCapbilitiesWorker;
2390     int32_t errCode = uv_queue_work_with_qos(
2391         loop, work, [](uv_work_t *work) {
2392             TELEPHONY_LOGD("UpdateCameraCapabilities uv_queue_work_with_qos");
2393         }, ReportCameraCapabilitiesInfoWork, uv_qos_default);
2394     if (errCode != 0) {
2395         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2396         delete cameraCapbilitiesWorker;
2397         cameraCapbilitiesWorker = nullptr;
2398         delete work;
2399         work = nullptr;
2400         return TELEPHONY_ERROR;
2401     }
2402     return TELEPHONY_SUCCESS;
2403 }
2404 
ReportCameraCapabilitiesInfoWork(uv_work_t * work,int32_t status)2405 void NapiCallAbilityCallback::ReportCameraCapabilitiesInfoWork(uv_work_t *work, int32_t status)
2406 {
2407     CameraCapbilitiesWorker *dataWorkerData = (CameraCapbilitiesWorker *)work->data;
2408     if (dataWorkerData == nullptr) {
2409         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2410         return;
2411     }
2412     int32_t ret = ReportCameraCapabilitiesInfo(dataWorkerData->cameraCapabilities, dataWorkerData->callback);
2413     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2414     delete dataWorkerData;
2415     dataWorkerData = nullptr;
2416     delete work;
2417     work = nullptr;
2418 }
2419 
ReportCameraCapabilitiesInfo(CameraCapabilities & cameraCapabilities,EventCallback eventCallback)2420 int32_t NapiCallAbilityCallback::ReportCameraCapabilitiesInfo(
2421     CameraCapabilities &cameraCapabilities, EventCallback eventCallback)
2422 {
2423     napi_env env = eventCallback.env;
2424     napi_handle_scope cameraCapabilitiesScope = nullptr;
2425     napi_open_handle_scope(env, &cameraCapabilitiesScope);
2426     if (cameraCapabilitiesScope == nullptr) {
2427         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2428         napi_close_handle_scope(env, cameraCapabilitiesScope);
2429         return TELEPHONY_ERROR;
2430     }
2431     napi_value callbackFunc = nullptr;
2432     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2433 
2434     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2435     NapiCallManagerUtils::SetPropertyInt32(
2436         env, callbackValues[ARRAY_INDEX_FIRST], "callId", cameraCapabilities.callId);
2437     NapiCallManagerUtils::SetPropertyInt32(
2438         env, callbackValues[ARRAY_INDEX_FIRST], "width", cameraCapabilities.width);
2439     NapiCallManagerUtils::SetPropertyInt32(
2440         env, callbackValues[ARRAY_INDEX_FIRST], "height", cameraCapabilities.height);
2441 
2442     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2443     if (callbackFunc == nullptr) {
2444         TELEPHONY_LOGE("callbackFunc is null!");
2445         napi_close_handle_scope(env, cameraCapabilitiesScope);
2446         return CALL_ERR_CALLBACK_NOT_EXIST;
2447     }
2448     napi_value thisVar = nullptr;
2449     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2450     napi_value callbackResult = nullptr;
2451     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2452     napi_close_handle_scope(env, cameraCapabilitiesScope);
2453     return TELEPHONY_SUCCESS;
2454 }
2455 } // namespace Telephony
2456 } // namespace OHOS
2457