• 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     auto callStateWorker = std::make_shared<CallStateWorker>();
494     callStateWorker->info = info;
495     callStateWorker->callback = stateCallback_;
496     auto task = [callStateWorker]() {
497         ReportCallState(callStateWorker->info, callStateWorker->callback);
498     };
499     if (napi_status::napi_ok != napi_send_event(stateCallback_.env, task, napi_eprio_high)) {
500         TELEPHONY_LOGE("napi_send_event: Failed to Send UpdateCallStateInfo Event");
501         return TELEPHONY_ERROR;
502     }
503     return TELEPHONY_SUCCESS;
504 }
505 
UpdateMeeTimeStateInfo(const CallAttributeInfo & info)506 int32_t NapiCallAbilityCallback::UpdateMeeTimeStateInfo(const CallAttributeInfo &info)
507 {
508     if (meeTimeStateCallback_.thisVar == nullptr) {
509         return CALL_ERR_CALLBACK_NOT_EXIST;
510     }
511     auto meeTimeStateWorker = std::make_shared<MeeTimeStateWorker>();
512     meeTimeStateWorker->info = info;
513     meeTimeStateWorker->callback = meeTimeStateCallback_;
514     auto task = [meeTimeStateWorker]() {
515         ReportCallState(meeTimeStateWorker->info, meeTimeStateWorker->callback);
516     };
517     if (napi_status::napi_ok != napi_send_event(meeTimeStateCallback_.env, task, napi_eprio_high)) {
518         TELEPHONY_LOGE("napi_send_event: Failed to Send UpdateMeeTimeStateInfo Event");
519         return TELEPHONY_ERROR;
520     }
521     return TELEPHONY_SUCCESS;
522 }
523 
524 /**
525  * To notify an application of a call status change, register a callback with on() first.
526  */
ReportCallState(CallAttributeInfo & info,EventCallback stateCallback)527 int32_t NapiCallAbilityCallback::ReportCallState(CallAttributeInfo &info, EventCallback stateCallback)
528 {
529     napi_env env = stateCallback.env;
530     napi_handle_scope scope = nullptr;
531     napi_open_handle_scope(env, &scope);
532     if (scope == nullptr) {
533         TELEPHONY_LOGE("scope is nullptr");
534         napi_close_handle_scope(env, scope);
535         return TELEPHONY_ERROR;
536     }
537     napi_value callbackFunc = nullptr;
538     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
539     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
540     NapiCallManagerUtils::SetPropertyStringUtf8(
541         env, callbackValues[ARRAY_INDEX_FIRST], "accountNumber", info.accountNumber);
542     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "accountId", info.accountId);
543     NapiCallManagerUtils::SetPropertyInt32(
544         env, callbackValues[ARRAY_INDEX_FIRST], "videoState", static_cast<int32_t>(info.videoState));
545     NapiCallManagerUtils::SetPropertyInt64(env, callbackValues[ARRAY_INDEX_FIRST], "startTime", info.startTime);
546     NapiCallManagerUtils::SetPropertyBoolean(env, callbackValues[ARRAY_INDEX_FIRST], "isEcc", info.isEcc);
547     NapiCallManagerUtils::SetPropertyInt32(
548         env, callbackValues[ARRAY_INDEX_FIRST], "callType", static_cast<int32_t>(info.callType));
549     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", info.callId);
550     NapiCallManagerUtils::SetPropertyInt32(
551         env, callbackValues[ARRAY_INDEX_FIRST], "callState", static_cast<int32_t>(info.callState));
552     NapiCallManagerUtils::SetPropertyInt32(
553         env, callbackValues[ARRAY_INDEX_FIRST], "conferenceState", static_cast<int32_t>(info.conferenceState));
554     NapiCallManagerUtils::SetPropertyInt32(
555         env, callbackValues[ARRAY_INDEX_FIRST], "crsType", info.crsType);
556     NapiCallManagerUtils::SetPropertyInt32(
557         env, callbackValues[ARRAY_INDEX_FIRST], "originalCallType", info.originalCallType);
558     NapiCallManagerUtils::SetPropertyInt32(
559         env, callbackValues[ARRAY_INDEX_FIRST], "phoneOrWatch", info.phoneOrWatch);
560     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], std::string("extraParams").c_str(),
561         AppExecFwk::WrapWantParams(env, AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(info.extraParamsString)));
562     ReportCallAttribute(env, callbackValues, ARRAY_INDEX_THIRD, info);
563     napi_get_reference_value(env, stateCallback.callbackRef, &callbackFunc);
564     if (callbackFunc == nullptr) {
565         TELEPHONY_LOGE("callbackFunc is null!");
566         napi_close_handle_scope(env, scope);
567         return CALL_ERR_CALLBACK_NOT_EXIST;
568     }
569     napi_value thisVar = nullptr;
570     napi_get_reference_value(env, stateCallback.thisVar, &thisVar);
571     napi_value callbackResult = nullptr;
572     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
573     napi_close_handle_scope(env, scope);
574     return TELEPHONY_SUCCESS;
575 }
576 
ReportCallAttribute(napi_env & env,napi_value callbackValues[],const size_t callbackValuesCount,CallAttributeInfo & info)577 void NapiCallAbilityCallback::ReportCallAttribute(napi_env &env, napi_value callbackValues[],
578     const size_t callbackValuesCount, CallAttributeInfo &info)
579 {
580     std::string str(info.numberLocation);
581     if (str == "default") {
582         TELEPHONY_LOGE("numberLocation is default");
583         (void)memset_s(info.numberLocation, kMaxNumberLen, 0, kMaxNumberLen);
584     }
585     NapiCallManagerUtils::SetPropertyStringUtf8(
586         env, callbackValues[ARRAY_INDEX_FIRST], "numberLocation", info.numberLocation);
587     TELEPHONY_LOGI("ReportCallState crsType = %{public}d", info.crsType);
588     if (info.callType == CallType::TYPE_VOIP) {
589         napi_value voipObject = nullptr;
590         CreateVoipNapiValue(env, voipObject, info);
591         napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "voipCallAttribute", voipObject);
592     }
593     napi_value markInfoObject = nullptr;
594     CreateMarkInfoNapiValue(env, markInfoObject, info);
595     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "numberMarkInfo", markInfoObject);
596     NapiCallManagerUtils::SetPropertyStringUtf8(
597         env, callbackValues[ARRAY_INDEX_FIRST], "distributedContactName", info.contactName);
598 }
599 
CreateVoipNapiValue(napi_env & env,napi_value & voipObject,CallAttributeInfo & info)600 void NapiCallAbilityCallback::CreateVoipNapiValue(napi_env &env, napi_value &voipObject, CallAttributeInfo &info)
601 {
602     napi_create_object(env, &voipObject);
603     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "userName", info.voipCallInfo.userName);
604     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "abilityName", info.voipCallInfo.abilityName);
605     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "extensionId", info.voipCallInfo.extensionId);
606     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "voipBundleName", info.voipCallInfo.voipBundleName);
607     NapiCallManagerUtils::SetPropertyStringUtf8(env, voipObject, "voipCallId", info.voipCallInfo.voipCallId);
608     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "showBannerForIncomingCall",
609         info.voipCallInfo.showBannerForIncomingCall);
610     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "isConferenceCall", info.voipCallInfo.isConferenceCall);
611     NapiCallManagerUtils::SetPropertyBoolean(
612         env, voipObject, "isVoiceAnswerSupported", info.voipCallInfo.isVoiceAnswerSupported);
613     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "hasMicPermission", info.voipCallInfo.hasMicPermission);
614     NapiCallManagerUtils::SetPropertyBoolean(env, voipObject, "isCapsuleSticky", info.voipCallInfo.isCapsuleSticky);
615     NapiCallManagerUtils::SetPropertyInt32(env, voipObject, "uid", info.voipCallInfo.uid);
616     std::shared_ptr<Media::PixelMap> userProfile =
617         std::shared_ptr<Media::PixelMap>(Media::PixelMap::DecodeTlv(info.voipCallInfo.userProfile));
618     napi_value pixelMapObject = Media::PixelMapNapi::CreatePixelMap(env, userProfile);
619     napi_set_named_property(env, voipObject, "userProfile", pixelMapObject);
620 }
621 
CreateMarkInfoNapiValue(napi_env & env,napi_value & markInfoObject,CallAttributeInfo & info)622 void NapiCallAbilityCallback::CreateMarkInfoNapiValue(napi_env &env,
623     napi_value &markInfoObject, CallAttributeInfo &info)
624 {
625     napi_create_object(env, &markInfoObject);
626     NapiCallManagerUtils::SetPropertyInt32(env, markInfoObject, "markType",
627         static_cast<int32_t>(info.numberMarkInfo.markType));
628     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markContent", info.numberMarkInfo.markContent);
629     NapiCallManagerUtils::SetPropertyInt32(env, markInfoObject, "markCount",
630         static_cast<int32_t>(info.numberMarkInfo.markCount));
631     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markSource", info.numberMarkInfo.markSource);
632     NapiCallManagerUtils::SetPropertyBoolean(env, markInfoObject, "isCloud", info.numberMarkInfo.isCloud);
633     NapiCallManagerUtils::SetPropertyStringUtf8(env, markInfoObject, "markDetails", info.numberMarkInfo.markDetails);
634 }
635 
UpdateCallEvent(const CallEventInfo & info)636 int32_t NapiCallAbilityCallback::UpdateCallEvent(const CallEventInfo &info)
637 {
638     if (eventCallback_.thisVar == nullptr) {
639         TELEPHONY_LOGE("eventCallback is null!");
640         return CALL_ERR_CALLBACK_NOT_EXIST;
641     }
642     uv_loop_s *loop = nullptr;
643 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
644     napi_get_uv_event_loop(eventCallback_.env, &loop);
645 #endif
646     if (loop == nullptr) {
647         return TELEPHONY_ERR_LOCAL_PTR_NULL;
648     }
649     CallEventWorker *callEventWorker = std::make_unique<CallEventWorker>().release();
650     if (callEventWorker == nullptr) {
651         TELEPHONY_LOGE("callEventWorker is nullptr!");
652         return TELEPHONY_ERR_LOCAL_PTR_NULL;
653     }
654     callEventWorker->info = info;
655     callEventWorker->callback = eventCallback_;
656     uv_work_t *work = std::make_unique<uv_work_t>().release();
657     if (work == nullptr) {
658         delete callEventWorker;
659         callEventWorker = nullptr;
660         TELEPHONY_LOGE("work is nullptr!");
661         return TELEPHONY_ERR_LOCAL_PTR_NULL;
662     }
663     work->data = (void *)callEventWorker;
664     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
665         TELEPHONY_LOGD("UpdateCallEvent uv_queue_work_with_qos");
666     }, ReportCallEventWork, uv_qos_default);
667     if (errCode != 0) {
668         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
669         delete callEventWorker;
670         callEventWorker = nullptr;
671         delete work;
672         work = nullptr;
673         return TELEPHONY_ERROR;
674     }
675     return TELEPHONY_SUCCESS;
676 }
677 
ReportCallEventWork(uv_work_t * work,int32_t status)678 void NapiCallAbilityCallback::ReportCallEventWork(uv_work_t *work, int32_t status)
679 {
680     CallEventWorker *dataWorkerData = (CallEventWorker *)work->data;
681     if (dataWorkerData == nullptr) {
682         TELEPHONY_LOGE("dataWorkerData is nullptr!");
683         return;
684     }
685     int32_t ret = ReportCallEvent(dataWorkerData->info, dataWorkerData->callback);
686     TELEPHONY_LOGI("ReportCallEvent results %{public}d", ret);
687     delete dataWorkerData;
688     dataWorkerData = nullptr;
689     delete work;
690     work = nullptr;
691 }
692 
ReportCallEvent(CallEventInfo & info,EventCallback eventCallback)693 int32_t NapiCallAbilityCallback::ReportCallEvent(CallEventInfo &info, EventCallback eventCallback)
694 {
695     napi_env env = eventCallback.env;
696     napi_handle_scope scopeCallEvent = nullptr;
697     napi_open_handle_scope(env, &scopeCallEvent);
698     if (scopeCallEvent == nullptr) {
699         TELEPHONY_LOGE("scopeCallEvent is nullptr");
700         napi_close_handle_scope(env, scopeCallEvent);
701         return TELEPHONY_ERROR;
702     }
703     napi_value callEventCallbackFunc = nullptr;
704     napi_value callEventCallbackValues[ARRAY_INDEX_THIRD] = { 0 };
705     napi_create_object(env, &callEventCallbackValues[ARRAY_INDEX_FIRST]);
706     NapiCallManagerUtils::SetPropertyInt32(
707         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "eventId", static_cast<int32_t>(info.eventId));
708     NapiCallManagerUtils::SetPropertyStringUtf8(
709         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "accountNumber", info.phoneNum);
710     NapiCallManagerUtils::SetPropertyStringUtf8(
711         env, callEventCallbackValues[ARRAY_INDEX_FIRST], "bundleName", info.bundleName);
712     napi_get_reference_value(env, eventCallback.callbackRef, &callEventCallbackFunc);
713     if (callEventCallbackFunc == nullptr) {
714         TELEPHONY_LOGE("callEventCallbackFunc is null!");
715         napi_close_handle_scope(env, scopeCallEvent);
716         return CALL_ERR_CALLBACK_NOT_EXIST;
717     }
718     napi_value thisVar = nullptr;
719     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
720     napi_value callbackResult = nullptr;
721     napi_call_function(env, thisVar, callEventCallbackFunc, DATA_LENGTH_ONE, callEventCallbackValues, &callbackResult);
722     napi_close_handle_scope(env, scopeCallEvent);
723     return TELEPHONY_SUCCESS;
724 }
725 
UpdateCallDisconnectedCause(const DisconnectedDetails & details)726 int32_t NapiCallAbilityCallback::UpdateCallDisconnectedCause(const DisconnectedDetails &details)
727 {
728     if (callDisconnectCauseCallback_.thisVar == nullptr) {
729         TELEPHONY_LOGE("callDisconnectCauseCallback_ is null!");
730         return CALL_ERR_CALLBACK_NOT_EXIST;
731     }
732     uv_loop_s *loop = nullptr;
733 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
734     napi_get_uv_event_loop(callDisconnectCauseCallback_.env, &loop);
735 #endif
736     if (loop == nullptr) {
737         return TELEPHONY_ERR_LOCAL_PTR_NULL;
738     }
739     CallDisconnectedCauseWorker *callDisconnectedCauseWorker =
740         std::make_unique<CallDisconnectedCauseWorker>().release();
741     if (callDisconnectedCauseWorker == nullptr) {
742         TELEPHONY_LOGE("callDisconnectedCauseWorker is nullptr!");
743         return TELEPHONY_ERR_LOCAL_PTR_NULL;
744     }
745     callDisconnectedCauseWorker->details = details;
746     callDisconnectedCauseWorker->callback = callDisconnectCauseCallback_;
747     uv_work_t *work = std::make_unique<uv_work_t>().release();
748     if (work == nullptr) {
749         delete callDisconnectedCauseWorker;
750         callDisconnectedCauseWorker = nullptr;
751         TELEPHONY_LOGE("work is nullptr!");
752         return TELEPHONY_ERR_LOCAL_PTR_NULL;
753     }
754     work->data = (void *)callDisconnectedCauseWorker;
755     int32_t errCode = uv_queue_work_with_qos(
756         loop, work, [](uv_work_t *work) {
757             TELEPHONY_LOGD("UpdateCallDisconnectedCause uv_queue_work_with_qos");
758         }, ReportCallDisconnectedCauseWork, uv_qos_default);
759     if (errCode != 0) {
760         delete callDisconnectedCauseWorker;
761         callDisconnectedCauseWorker = nullptr;
762         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, errCode: %{public}d", errCode);
763         delete work;
764         work = nullptr;
765         return TELEPHONY_ERROR;
766     }
767     if (callDisconnectCauseCallback_.thisVar) {
768         (void)memset_s(&callDisconnectCauseCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
769     }
770     return TELEPHONY_SUCCESS;
771 }
772 
ReportCallDisconnectedCauseWork(uv_work_t * work,int32_t status)773 void NapiCallAbilityCallback::ReportCallDisconnectedCauseWork(uv_work_t *work, int32_t status)
774 {
775     CallDisconnectedCauseWorker *dataWorkerData = (CallDisconnectedCauseWorker *)work->data;
776     if (dataWorkerData == nullptr) {
777         TELEPHONY_LOGE("dataWorkerData is nullptr!");
778         return;
779     }
780     int32_t ret = ReportDisconnectedCause(dataWorkerData->details, dataWorkerData->callback);
781     TELEPHONY_LOGI("ReportDisconnectedCause results %{public}d", ret);
782     delete dataWorkerData;
783     dataWorkerData = nullptr;
784     delete work;
785     work = nullptr;
786 }
787 
ReportDisconnectedCause(const DisconnectedDetails & details,EventCallback eventCallback)788 int32_t NapiCallAbilityCallback::ReportDisconnectedCause(
789     const DisconnectedDetails &details, EventCallback eventCallback)
790 {
791     napi_env env = eventCallback.env;
792     napi_handle_scope disconnectedScope = nullptr;
793     napi_open_handle_scope(env, &disconnectedScope);
794     if (disconnectedScope == nullptr) {
795         TELEPHONY_LOGE("disconnectedScope is nullptr");
796         napi_close_handle_scope(env, disconnectedScope);
797         return TELEPHONY_ERROR;
798     }
799     napi_value callbackFunc = nullptr;
800     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
801     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
802     NapiCallManagerUtils::SetPropertyInt32(
803         env, callbackValues[ARRAY_INDEX_FIRST], "reason", static_cast<int32_t>(details.reason));
804     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "message", details.message);
805     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
806     if (callbackFunc == nullptr) {
807         TELEPHONY_LOGE("callbackFunc is null!");
808         napi_close_handle_scope(env, disconnectedScope);
809         return CALL_ERR_CALLBACK_NOT_EXIST;
810     }
811     napi_value thisVar = nullptr;
812     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
813     napi_value callbackResult = nullptr;
814     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
815     napi_close_handle_scope(env, disconnectedScope);
816     return TELEPHONY_SUCCESS;
817 }
818 
UpdateAsyncResultsInfo(const CallResultReportId reportId,AppExecFwk::PacMap & resultInfo)819 int32_t NapiCallAbilityCallback::UpdateAsyncResultsInfo(
820     const CallResultReportId reportId, AppExecFwk::PacMap &resultInfo)
821 {
822     int32_t result = TELEPHONY_ERR_FAIL;
823     TELEPHONY_LOGI("UpdateAsyncResultsInfo reportId = %{public}d", reportId);
824     auto itFunc = memberFuncMap_.find(reportId);
825     if (itFunc != memberFuncMap_.end() && itFunc->second != nullptr) {
826         auto memberFunc = itFunc->second;
827         result = memberFunc(resultInfo);
828     }
829     return result;
830 }
831 
UpdateMmiCodeResultsInfo(const MmiCodeInfo & info)832 int32_t NapiCallAbilityCallback::UpdateMmiCodeResultsInfo(const MmiCodeInfo &info)
833 {
834     if (mmiCodeCallback_.thisVar == nullptr) {
835         TELEPHONY_LOGE("mmiCodeCallback is null!");
836         return CALL_ERR_CALLBACK_NOT_EXIST;
837     }
838     uv_loop_s *loop = nullptr;
839 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
840     napi_get_uv_event_loop(mmiCodeCallback_.env, &loop);
841 #endif
842     if (loop == nullptr) {
843         return TELEPHONY_ERR_LOCAL_PTR_NULL;
844     }
845     MmiCodeWorker *mmiCodeWorker = std::make_unique<MmiCodeWorker>().release();
846     if (mmiCodeWorker == nullptr) {
847         TELEPHONY_LOGE("mmiCodeWorker is nullptr!");
848         return TELEPHONY_ERR_LOCAL_PTR_NULL;
849     }
850     mmiCodeWorker->info = info;
851     mmiCodeWorker->callback = mmiCodeCallback_;
852     uv_work_t *work = std::make_unique<uv_work_t>().release();
853     if (work == nullptr) {
854         delete mmiCodeWorker;
855         mmiCodeWorker = nullptr;
856         TELEPHONY_LOGE("work is nullptr!");
857         return TELEPHONY_ERR_LOCAL_PTR_NULL;
858     }
859     work->data = (void *)mmiCodeWorker;
860     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
861         TELEPHONY_LOGD("UpdateMmiCodeResultsInfo uv_queue_work_with_qos");
862     }, ReportMmiCodeWork, uv_qos_default);
863     if (errCode != 0) {
864         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
865         delete mmiCodeWorker;
866         mmiCodeWorker = nullptr;
867         delete work;
868         work = nullptr;
869         return TELEPHONY_ERROR;
870     }
871     return TELEPHONY_SUCCESS;
872 }
873 
ReportMmiCodeWork(uv_work_t * work,int32_t status)874 void NapiCallAbilityCallback::ReportMmiCodeWork(uv_work_t *work, int32_t status)
875 {
876     MmiCodeWorker *dataWorkerData = (MmiCodeWorker *)work->data;
877     if (dataWorkerData == nullptr) {
878         TELEPHONY_LOGE("dataWorkerData is nullptr!");
879         return;
880     }
881     int32_t ret = ReportMmiCode(dataWorkerData->info, dataWorkerData->callback);
882     TELEPHONY_LOGI("ReportMmiCode result = %{public}d", ret);
883     delete dataWorkerData;
884     dataWorkerData = nullptr;
885     delete work;
886     work = nullptr;
887 }
888 
889 /**
890  * To notify an application of MMI code result, register a callback with on() first.
891  */
ReportMmiCode(MmiCodeInfo & info,EventCallback eventCallback)892 int32_t NapiCallAbilityCallback::ReportMmiCode(MmiCodeInfo &info, EventCallback eventCallback)
893 {
894     napi_env env = eventCallback.env;
895     napi_handle_scope mmiCodeScope = nullptr;
896     napi_open_handle_scope(env, &mmiCodeScope);
897     if (mmiCodeScope == nullptr) {
898         TELEPHONY_LOGE("mmiCodeScope is nullptr");
899         napi_close_handle_scope(env, mmiCodeScope);
900         return TELEPHONY_ERROR;
901     }
902     napi_value callbackFunc = nullptr;
903     napi_value callbackValues[ARRAY_INDEX_FOURTH] = { 0 };
904     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
905     NapiCallManagerUtils::SetPropertyInt32(
906         env, callbackValues[ARRAY_INDEX_FIRST], "result", static_cast<int32_t>(info.result));
907     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "message", info.message);
908     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "mmiCodeType",
909         static_cast<int32_t>(info.mmiCodeType));
910     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "action",
911         static_cast<int32_t>(info.action));
912     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "status",
913         static_cast<int32_t>(info.status));
914     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "classCw",
915         static_cast<int32_t>(info.classCw));
916     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "reason",
917         static_cast<int32_t>(info.reason));
918     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "time",
919         static_cast<int32_t>(info.time));
920     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValues[ARRAY_INDEX_FIRST], "number", info.number);
921     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
922     if (callbackFunc == nullptr) {
923         TELEPHONY_LOGE("callbackFunc is null!");
924         napi_close_handle_scope(env, mmiCodeScope);
925         return CALL_ERR_CALLBACK_NOT_EXIST;
926     }
927     napi_value thisVar = nullptr;
928     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
929     napi_value callbackResult = nullptr;
930     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
931     napi_close_handle_scope(env, mmiCodeScope);
932     return TELEPHONY_SUCCESS;
933 }
934 
UpdateAudioDeviceInfo(const AudioDeviceInfo & info)935 int32_t NapiCallAbilityCallback::UpdateAudioDeviceInfo(const AudioDeviceInfo &info)
936 {
937     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
938     if (audioDeviceCallback_.thisVar == nullptr) {
939         return CALL_ERR_CALLBACK_NOT_EXIST;
940     }
941     uv_loop_s *loop = nullptr;
942 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
943     napi_get_uv_event_loop(audioDeviceCallback_.env, &loop);
944 #endif
945     if (loop == nullptr) {
946         return TELEPHONY_ERR_LOCAL_PTR_NULL;
947     }
948     AudioDeviceWork *audioDeviceWork = std::make_unique<AudioDeviceWork>().release();
949     if (audioDeviceWork == nullptr) {
950         TELEPHONY_LOGE("audioDeviceWork is nullptr!");
951         return TELEPHONY_ERR_LOCAL_PTR_NULL;
952     }
953     audioDeviceWork->info = info;
954     audioDeviceWork->callback = audioDeviceCallback_;
955     uv_work_t *work = std::make_unique<uv_work_t>().release();
956     if (work == nullptr) {
957         delete audioDeviceWork;
958         audioDeviceWork = nullptr;
959         TELEPHONY_LOGE("work is nullptr!");
960         return TELEPHONY_ERR_LOCAL_PTR_NULL;
961     }
962     work->data = (void *)audioDeviceWork;
963     int32_t errCode = uv_queue_work_with_qos(
964         loop, work, [](uv_work_t *work) {
965             TELEPHONY_LOGD("UpdateAudioDeviceInfo uv_queue_work_with_qos");
966         }, ReportAudioDeviceInfoWork, uv_qos_default);
967     if (errCode != 0) {
968         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
969         delete audioDeviceWork;
970         audioDeviceWork = nullptr;
971         delete work;
972         work = nullptr;
973         return TELEPHONY_ERROR;
974     }
975     return TELEPHONY_SUCCESS;
976 }
977 
ReportAudioDeviceInfoWork(uv_work_t * work,int32_t status)978 void NapiCallAbilityCallback::ReportAudioDeviceInfoWork(uv_work_t *work, int32_t status)
979 {
980     TELEPHONY_LOGI("report audio device info work.");
981     std::lock_guard<std::mutex> lock(audioDeviceCallbackMutex_);
982     AudioDeviceWork *dataWorkerData = (AudioDeviceWork *)work->data;
983     if (dataWorkerData == nullptr) {
984         TELEPHONY_LOGE("dataWorkerData is nullptr!");
985         return;
986     }
987     if (audioDeviceCallback_.thisVar && audioDeviceCallback_.callbackRef) {
988         int32_t ret = ReportAudioDeviceInfo(dataWorkerData->info, audioDeviceCallback_);
989         TELEPHONY_LOGI("ReportAudioDeviceInfo result = %{public}d", ret);
990         delete dataWorkerData;
991         dataWorkerData = nullptr;
992         delete work;
993         work = nullptr;
994     }
995 }
996 
ReportAudioDeviceInfo(AudioDeviceInfo & info,EventCallback eventCallback)997 int32_t NapiCallAbilityCallback::ReportAudioDeviceInfo(AudioDeviceInfo &info, EventCallback eventCallback)
998 {
999     TELEPHONY_LOGI("report audio device info.");
1000     napi_env env = eventCallback.env;
1001     napi_handle_scope AudioDeviceInfoScope = nullptr;
1002     napi_open_handle_scope(env, &AudioDeviceInfoScope);
1003     if (AudioDeviceInfoScope == nullptr) {
1004         TELEPHONY_LOGE("AudioDeviceInfoScope is nullptr");
1005         napi_close_handle_scope(env, AudioDeviceInfoScope);
1006         return TELEPHONY_ERROR;
1007     }
1008     napi_value callbackFunc = nullptr;
1009     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
1010     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
1011 
1012     NapiCallManagerUtils::SetPropertyBoolean(env, callbackValues[ARRAY_INDEX_FIRST], "isMuted", info.isMuted);
1013     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", info.callId);
1014 
1015     napi_value currentAudioDeviceValue = nullptr;
1016     napi_create_object(env, &currentAudioDeviceValue);
1017     NapiCallManagerUtils::SetPropertyInt32(env, currentAudioDeviceValue, "deviceType",
1018         static_cast<int32_t>(info.currentAudioDevice.deviceType));
1019     NapiCallManagerUtils::SetPropertyStringUtf8(
1020         env, currentAudioDeviceValue, "address", info.currentAudioDevice.address);
1021     NapiCallManagerUtils::SetPropertyStringUtf8(
1022         env, currentAudioDeviceValue, "deviceName", info.currentAudioDevice.deviceName);
1023     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "currentAudioDevice", currentAudioDeviceValue);
1024 
1025     napi_value audioDeviceListValue = nullptr;
1026     napi_create_array(env, &audioDeviceListValue);
1027     std::vector<AudioDevice>::iterator it = info.audioDeviceList.begin();
1028     int32_t i = 0;
1029     for (; it != info.audioDeviceList.end(); ++it) {
1030         napi_value value = nullptr;
1031         napi_create_object(env, &value);
1032         NapiCallManagerUtils::SetPropertyInt32(env, value, "deviceType", static_cast<int32_t>(it->deviceType));
1033         NapiCallManagerUtils::SetPropertyStringUtf8(env, value, "address", it->address);
1034         NapiCallManagerUtils::SetPropertyStringUtf8(env, value, "deviceName", it->deviceName);
1035         napi_set_element(env, audioDeviceListValue, i, value);
1036         ++i;
1037     }
1038     napi_set_named_property(env, callbackValues[ARRAY_INDEX_FIRST], "audioDeviceList", audioDeviceListValue);
1039     if (eventCallback.callbackRef == nullptr) {
1040         TELEPHONY_LOGE("eventCallback callbackRef is null!");
1041         napi_close_handle_scope(env, AudioDeviceInfoScope);
1042         return CALL_ERR_CALLBACK_NOT_EXIST;
1043     }
1044     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
1045     if (callbackFunc == nullptr) {
1046         TELEPHONY_LOGE("callbackFunc is null!");
1047         napi_close_handle_scope(env, AudioDeviceInfoScope);
1048         return CALL_ERR_CALLBACK_NOT_EXIST;
1049     }
1050     napi_value thisVar = nullptr;
1051     if (eventCallback.thisVar == nullptr) {
1052         TELEPHONY_LOGE("eventCallback thisVar is null!");
1053         napi_close_handle_scope(env, AudioDeviceInfoScope);
1054         return CALL_ERR_CALLBACK_NOT_EXIST;
1055     }
1056     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
1057     napi_value callbackResult = nullptr;
1058     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1059     napi_close_handle_scope(env, AudioDeviceInfoScope);
1060     return TELEPHONY_SUCCESS;
1061 }
1062 
OttCallRequest(OttCallRequestId requestId,AppExecFwk::PacMap & info)1063 int32_t NapiCallAbilityCallback::OttCallRequest(OttCallRequestId requestId, AppExecFwk::PacMap &info)
1064 {
1065     if (ottRequestCallback_.thisVar == nullptr) {
1066         TELEPHONY_LOGE("stateCallback is null!");
1067         return CALL_ERR_CALLBACK_NOT_EXIST;
1068     }
1069     uv_loop_s *loop = nullptr;
1070 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1071     napi_get_uv_event_loop(ottRequestCallback_.env, &loop);
1072 #endif
1073     if (loop == nullptr) {
1074         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1075     }
1076     CallOttWorker *callOttWorker = std::make_unique<CallOttWorker>().release();
1077     if (callOttWorker == nullptr) {
1078         TELEPHONY_LOGE("callOttWorker is nullptr!");
1079         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1080     }
1081     callOttWorker->requestId = requestId;
1082     callOttWorker->info = info;
1083     callOttWorker->callback = ottRequestCallback_;
1084     uv_work_t *work = std::make_unique<uv_work_t>().release();
1085     if (work == nullptr) {
1086         delete callOttWorker;
1087         callOttWorker = nullptr;
1088         TELEPHONY_LOGE("work is nullptr!");
1089         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1090     }
1091     work->data = (void *)callOttWorker;
1092     int32_t errCode = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {
1093         TELEPHONY_LOGD("OttCallRequest uv_queue_work_with_qos");
1094     }, ReportCallOttWork, uv_qos_default);
1095     if (errCode != 0) {
1096         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1097         delete callOttWorker;
1098         callOttWorker = nullptr;
1099         delete work;
1100         work = nullptr;
1101         return TELEPHONY_ERROR;
1102     }
1103     return TELEPHONY_SUCCESS;
1104 }
1105 
ReportGetWaitingInfo(AppExecFwk::PacMap & resultInfo)1106 int32_t NapiCallAbilityCallback::ReportGetWaitingInfo(AppExecFwk::PacMap &resultInfo)
1107 {
1108     std::lock_guard<std::mutex> lock(getWaitingCallbackMutex_);
1109     if (getWaitingCallback_.thisVar == nullptr) {
1110         TELEPHONY_LOGE("getWaitingCallback is null!");
1111         return CALL_ERR_CALLBACK_NOT_EXIST;
1112     }
1113     uv_loop_s *loop = nullptr;
1114 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1115     napi_get_uv_event_loop(getWaitingCallback_.env, &loop);
1116 #endif
1117     if (loop == nullptr) {
1118         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1119     }
1120     CallSupplementWorker *callSupplementWorkerData = std::make_unique<CallSupplementWorker>().release();
1121     if (callSupplementWorkerData == nullptr) {
1122         TELEPHONY_LOGE("callSupplementWorkerData is nullptr!");
1123         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1124     }
1125     callSupplementWorkerData->info = resultInfo;
1126     callSupplementWorkerData->callback = getWaitingCallback_;
1127     uv_work_t *work = std::make_unique<uv_work_t>().release();
1128     if (work == nullptr) {
1129         delete callSupplementWorkerData;
1130         callSupplementWorkerData = nullptr;
1131         TELEPHONY_LOGE("work is nullptr!");
1132         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1133     }
1134     work->data = (void *)callSupplementWorkerData;
1135     int32_t errorCode = uv_queue_work_with_qos(
1136         loop, work, [](uv_work_t *work) {
1137             TELEPHONY_LOGD("ReportGetWaitingInfo uv_queue_work_with_qos");
1138         }, ReportWaitAndLimitInfoWork, uv_qos_default);
1139     if (errorCode != 0) {
1140         delete callSupplementWorkerData;
1141         callSupplementWorkerData = nullptr;
1142         delete work;
1143         work = nullptr;
1144         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errorCode: %{public}d", errorCode);
1145         return TELEPHONY_ERROR;
1146     }
1147     if (getWaitingCallback_.thisVar) {
1148         (void)memset_s(&getWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1149     }
1150     return TELEPHONY_SUCCESS;
1151 }
1152 
ReportCloseUnFinishedUssdInfo(AppExecFwk::PacMap & resultInfo)1153 int32_t NapiCallAbilityCallback::ReportCloseUnFinishedUssdInfo(AppExecFwk::PacMap &resultInfo)
1154 {
1155     std::lock_guard<std::mutex> lock(closeUnfinishedUssdCallbackMutex_);
1156     if (closeUnfinishedUssdCallback_.thisVar == nullptr) {
1157         TELEPHONY_LOGE("closeUnfinishedUssdCallback is null!");
1158         return CALL_ERR_CALLBACK_NOT_EXIST;
1159     }
1160     uv_loop_s *loop = nullptr;
1161 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1162     napi_get_uv_event_loop(closeUnfinishedUssdCallback_.env, &loop);
1163 #endif
1164     if (loop == nullptr) {
1165         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1166     }
1167     CallSupplementWorker *callSupplementDataWorker = std::make_unique<CallSupplementWorker>().release();
1168     if (callSupplementDataWorker == nullptr) {
1169         TELEPHONY_LOGE("callSupplementDataWorker is nullptr!");
1170         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1171     }
1172     callSupplementDataWorker->info = resultInfo;
1173     callSupplementDataWorker->callback = closeUnfinishedUssdCallback_;
1174     uv_work_t *work = std::make_unique<uv_work_t>().release();
1175     if (work == nullptr) {
1176         delete callSupplementDataWorker;
1177         callSupplementDataWorker = nullptr;
1178         TELEPHONY_LOGE("work is nullptr!");
1179         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1180     }
1181     work->data = (void *)callSupplementDataWorker;
1182     int32_t errorCode = uv_queue_work_with_qos(
1183         loop, work, [](uv_work_t *work) {
1184             TELEPHONY_LOGD("ReportCloseUnFinishedUssdInfo uv_queue_work_with_qos");
1185         }, ReportExecutionResultWork, uv_qos_default);
1186     if (errorCode != 0) {
1187         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errorCode: %{public}d", errorCode);
1188         delete callSupplementDataWorker;
1189         callSupplementDataWorker = nullptr;
1190         delete work;
1191         work = nullptr;
1192         return TELEPHONY_ERROR;
1193     }
1194     if (closeUnfinishedUssdCallback_.thisVar) {
1195         (void)memset_s(&closeUnfinishedUssdCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1196     }
1197     return TELEPHONY_SUCCESS;
1198 }
1199 
ReportSetWaitingInfo(AppExecFwk::PacMap & resultInfo)1200 int32_t NapiCallAbilityCallback::ReportSetWaitingInfo(AppExecFwk::PacMap &resultInfo)
1201 {
1202     std::lock_guard<std::mutex> lock(setWaitingCallbackMutex_);
1203     if (setWaitingCallback_.thisVar == nullptr) {
1204         TELEPHONY_LOGE("setWaitingCallback is null!");
1205         return CALL_ERR_CALLBACK_NOT_EXIST;
1206     }
1207     uv_loop_s *loop = nullptr;
1208 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1209     napi_get_uv_event_loop(setWaitingCallback_.env, &loop);
1210 #endif
1211     if (loop == nullptr) {
1212         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1213     }
1214     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1215     if (callSupplementWorker == nullptr) {
1216         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1217         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1218     }
1219     callSupplementWorker->info = resultInfo;
1220     callSupplementWorker->callback = setWaitingCallback_;
1221     uv_work_t *work = std::make_unique<uv_work_t>().release();
1222     if (work == nullptr) {
1223         delete callSupplementWorker;
1224         callSupplementWorker = nullptr;
1225         TELEPHONY_LOGE("work is nullptr!");
1226         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1227     }
1228     work->data = (void *)callSupplementWorker;
1229     int32_t resultCode = uv_queue_work_with_qos(
1230         loop, work, [](uv_work_t *work) {
1231             TELEPHONY_LOGD("ReportSetWaitingInfo uv_queue_work_with_qos");
1232         }, ReportExecutionResultWork, uv_qos_default);
1233     if (resultCode != 0) {
1234         delete callSupplementWorker;
1235         callSupplementWorker = nullptr;
1236         delete work;
1237         work = nullptr;
1238         TELEPHONY_LOGE("failed to add uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1239         return TELEPHONY_ERROR;
1240     }
1241     if (setWaitingCallback_.thisVar) {
1242         (void)memset_s(&setWaitingCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1243     }
1244     return TELEPHONY_SUCCESS;
1245 }
1246 
ReportGetRestrictionInfo(AppExecFwk::PacMap & resultInfo)1247 int32_t NapiCallAbilityCallback::ReportGetRestrictionInfo(AppExecFwk::PacMap &resultInfo)
1248 {
1249     std::lock_guard<std::mutex> lock(getRestrictionCallbackMutex_);
1250     if (getRestrictionCallback_.thisVar == nullptr) {
1251         TELEPHONY_LOGE("getRestrictionCallback is null!");
1252         return CALL_ERR_CALLBACK_NOT_EXIST;
1253     }
1254     uv_loop_s *loop = nullptr;
1255 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1256     napi_get_uv_event_loop(getRestrictionCallback_.env, &loop);
1257 #endif
1258     if (loop == nullptr) {
1259         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1260     }
1261     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1262     if (callSupplementWorker == nullptr) {
1263         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1264         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1265     }
1266     callSupplementWorker->info = resultInfo;
1267     callSupplementWorker->callback = getRestrictionCallback_;
1268     uv_work_t *work = std::make_unique<uv_work_t>().release();
1269     if (work == nullptr) {
1270         delete callSupplementWorker;
1271         callSupplementWorker = nullptr;
1272         TELEPHONY_LOGE("work is nullptr!");
1273         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1274     }
1275     work->data = (void *)callSupplementWorker;
1276     int32_t resultCode = uv_queue_work_with_qos(
1277         loop, work, [](uv_work_t *work) {
1278             TELEPHONY_LOGD("ReportGetRestrictionInfo uv_queue_work_with_qos");
1279         }, ReportWaitAndLimitInfoWork, uv_qos_default);
1280     if (resultCode != 0) {
1281         delete callSupplementWorker;
1282         callSupplementWorker = nullptr;
1283         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1284         delete work;
1285         work = nullptr;
1286         return TELEPHONY_ERROR;
1287     }
1288     if (getRestrictionCallback_.thisVar) {
1289         (void)memset_s(&getRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1290     }
1291     return TELEPHONY_SUCCESS;
1292 }
1293 
ReportSetRestrictionInfo(AppExecFwk::PacMap & resultInfo)1294 int32_t NapiCallAbilityCallback::ReportSetRestrictionInfo(AppExecFwk::PacMap &resultInfo)
1295 {
1296     if (setRestrictionCallback_.thisVar == nullptr) {
1297         TELEPHONY_LOGE("setRestrictionCallback is null!");
1298         return CALL_ERR_CALLBACK_NOT_EXIST;
1299     }
1300     uv_loop_s *loop = nullptr;
1301 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1302     napi_get_uv_event_loop(setRestrictionCallback_.env, &loop);
1303 #endif
1304     if (loop == nullptr) {
1305         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1306     }
1307     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1308     if (callSupplementWorker == nullptr) {
1309         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1310         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1311     }
1312     callSupplementWorker->info = resultInfo;
1313     callSupplementWorker->callback = setRestrictionCallback_;
1314     uv_work_t *work = std::make_unique<uv_work_t>().release();
1315     if (work == nullptr) {
1316         delete callSupplementWorker;
1317         callSupplementWorker = nullptr;
1318         TELEPHONY_LOGE("work is nullptr!");
1319         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1320     }
1321     work->data = (void *)callSupplementWorker;
1322     int32_t resultCode = uv_queue_work_with_qos(
1323         loop, work, [](uv_work_t *work) {
1324             TELEPHONY_LOGD("ReportSetRestrictionInfo uv_queue_work_with_qos");
1325         }, ReportExecutionResultWork, uv_qos_default);
1326     if (resultCode != 0) {
1327         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, resultCode: %{public}d", resultCode);
1328         delete callSupplementWorker;
1329         callSupplementWorker = nullptr;
1330         delete work;
1331         work = nullptr;
1332         return TELEPHONY_ERROR;
1333     }
1334     if (setRestrictionCallback_.thisVar) {
1335         (void)memset_s(&setRestrictionCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1336     }
1337     return TELEPHONY_SUCCESS;
1338 }
1339 
ReportSetRestrictionPassword(AppExecFwk::PacMap & resultInfo)1340 int32_t NapiCallAbilityCallback::ReportSetRestrictionPassword(AppExecFwk::PacMap &resultInfo)
1341 {
1342     std::lock_guard<std::mutex> lock(setRestrictionPasswordCallbackMutex_);
1343     if (setRestrictionPasswordCallback_.thisVar == nullptr) {
1344         TELEPHONY_LOGE("setRestrictionPasswordCallback is null!");
1345         return CALL_ERR_CALLBACK_NOT_EXIST;
1346     }
1347     uv_loop_s *loop = nullptr;
1348 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1349     napi_get_uv_event_loop(setRestrictionPasswordCallback_.env, &loop);
1350 #endif
1351     if (loop == nullptr) {
1352         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1353     }
1354     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1355     if (callSupplementWorker == nullptr) {
1356         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1357         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1358     }
1359     callSupplementWorker->info = resultInfo;
1360     callSupplementWorker->callback = setRestrictionPasswordCallback_;
1361     uv_work_t *work = std::make_unique<uv_work_t>().release();
1362     if (work == nullptr) {
1363         delete callSupplementWorker;
1364         callSupplementWorker = nullptr;
1365         TELEPHONY_LOGE("work is nullptr!");
1366         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1367     }
1368     work->data = (void *)callSupplementWorker;
1369     int32_t errCode = uv_queue_work_with_qos(
1370         loop, work, [](uv_work_t *work) {
1371             TELEPHONY_LOGD("ReportSetRestrictionPassword uv_queue_work_with_qos");
1372         }, ReportExecutionResultWork, uv_qos_default);
1373     if (errCode != 0) {
1374         delete callSupplementWorker;
1375         callSupplementWorker = nullptr;
1376         delete work;
1377         work = nullptr;
1378         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1379         return TELEPHONY_ERROR;
1380     }
1381     if (setRestrictionPasswordCallback_.thisVar) {
1382         (void)memset_s(&setRestrictionPasswordCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1383     }
1384     return TELEPHONY_SUCCESS;
1385 }
1386 
ReportGetTransferInfo(AppExecFwk::PacMap & resultInfo)1387 int32_t NapiCallAbilityCallback::ReportGetTransferInfo(AppExecFwk::PacMap &resultInfo)
1388 {
1389     std::lock_guard<std::mutex> lock(getTransferCallbackMutex_);
1390     if (getTransferCallback_.thisVar == nullptr) {
1391         TELEPHONY_LOGE("getTransferCallback is null!");
1392         return CALL_ERR_CALLBACK_NOT_EXIST;
1393     }
1394     if (getCallTransferReason_ != resultInfo.GetIntValue("reason")) {
1395         TELEPHONY_LOGE("Transfer reason is different, require is %{public}d, now is %{public}d", getCallTransferReason_,
1396             resultInfo.GetIntValue("reason"));
1397         return CALL_ERR_CALLBACK_NOT_EXIST;
1398     }
1399     uv_loop_s *loop = nullptr;
1400 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1401     napi_get_uv_event_loop(getTransferCallback_.env, &loop);
1402 #endif
1403     if (loop == nullptr) {
1404         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1405     }
1406     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1407     if (callSupplementWorker == nullptr) {
1408         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1409         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1410     }
1411     callSupplementWorker->info = resultInfo;
1412     callSupplementWorker->callback = getTransferCallback_;
1413     uv_work_t *work = std::make_unique<uv_work_t>().release();
1414     if (work == nullptr) {
1415         delete callSupplementWorker;
1416         callSupplementWorker = nullptr;
1417         TELEPHONY_LOGE("work is nullptr!");
1418         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1419     }
1420     work->data = (void *)callSupplementWorker;
1421     int32_t errCode = uv_queue_work_with_qos(
1422         loop, work, [](uv_work_t *work) {
1423             TELEPHONY_LOGD("ReportGetTransferInfo uv_queue_work_with_qos");
1424         }, ReportSupplementInfoWork, uv_qos_default);
1425     if (errCode != 0) {
1426         TELEPHONY_LOGE("failed add to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1427         delete callSupplementWorker;
1428         callSupplementWorker = nullptr;
1429         delete work;
1430         work = nullptr;
1431         return TELEPHONY_ERROR;
1432     }
1433     if (getTransferCallback_.thisVar) {
1434         (void)memset_s(&getTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1435     }
1436     return TELEPHONY_SUCCESS;
1437 }
1438 
ReportSetTransferInfo(AppExecFwk::PacMap & resultInfo)1439 int32_t NapiCallAbilityCallback::ReportSetTransferInfo(AppExecFwk::PacMap &resultInfo)
1440 {
1441     std::lock_guard<std::mutex> lock(setTransferCallbackMutex_);
1442     if (setTransferCallback_.thisVar == nullptr) {
1443         TELEPHONY_LOGE("setTransferCallback is null!");
1444         return CALL_ERR_CALLBACK_NOT_EXIST;
1445     }
1446     uv_loop_s *loop = nullptr;
1447 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1448     napi_get_uv_event_loop(setTransferCallback_.env, &loop);
1449 #endif
1450     if (loop == nullptr) {
1451         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1452     }
1453     CallSupplementWorker *callSupplementWorker = std::make_unique<CallSupplementWorker>().release();
1454     if (callSupplementWorker == nullptr) {
1455         TELEPHONY_LOGE("callSupplementWorker is nullptr!");
1456         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1457     }
1458     callSupplementWorker->info = resultInfo;
1459     callSupplementWorker->callback = setTransferCallback_;
1460     uv_work_t *work = std::make_unique<uv_work_t>().release();
1461     if (work == nullptr) {
1462         delete callSupplementWorker;
1463         callSupplementWorker = nullptr;
1464         TELEPHONY_LOGE("work is nullptr!");
1465         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1466     }
1467     work->data = (void *)callSupplementWorker;
1468     int32_t errCode = uv_queue_work_with_qos(
1469         loop, work, [](uv_work_t *work) {
1470             TELEPHONY_LOGD("ReportSetTransferInfo uv_queue_work_with_qos");
1471         }, ReportExecutionResultWork, uv_qos_default);
1472     if (errCode != 0) {
1473         delete callSupplementWorker;
1474         callSupplementWorker = nullptr;
1475         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1476         delete work;
1477         work = nullptr;
1478         return TELEPHONY_ERROR;
1479     }
1480     if (setTransferCallback_.thisVar) {
1481         (void)memset_s(&setTransferCallback_, sizeof(EventCallback), 0, sizeof(EventCallback));
1482     }
1483     return TELEPHONY_SUCCESS;
1484 }
1485 
ReportWaitAndLimitInfoWork(uv_work_t * work,int32_t status)1486 void NapiCallAbilityCallback::ReportWaitAndLimitInfoWork(uv_work_t *work, int32_t status)
1487 {
1488     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1489     if (dataWorkerData == nullptr) {
1490         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1491         return;
1492     }
1493     ReportWaitAndLimitInfo(dataWorkerData->info, dataWorkerData->callback);
1494     delete dataWorkerData;
1495     dataWorkerData = nullptr;
1496     delete work;
1497     work = nullptr;
1498 }
1499 
ReportWaitAndLimitInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1500 void NapiCallAbilityCallback::ReportWaitAndLimitInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1501 {
1502     napi_env env = supplementInfo.env;
1503     int32_t result = resultInfo.GetIntValue("result");
1504     int32_t status = resultInfo.GetIntValue("status");
1505     napi_handle_scope limitScope = nullptr;
1506     napi_open_handle_scope(env, &limitScope);
1507     if (limitScope == nullptr) {
1508         TELEPHONY_LOGE("limitScope is nullptr");
1509         napi_close_handle_scope(env, limitScope);
1510         return;
1511     }
1512     if (supplementInfo.callbackRef != nullptr) {
1513         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1514         if (result == TELEPHONY_SUCCESS) {
1515             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1516             napi_create_int32(env, status, &callbackValues[ARRAY_INDEX_SECOND]);
1517         } else {
1518             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1519             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1520             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1521         }
1522         napi_value callbackFunc = nullptr;
1523         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1524         napi_value thisVar = nullptr;
1525         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1526         napi_value callbackResult = nullptr;
1527         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1528         napi_delete_reference(env, supplementInfo.callbackRef);
1529         napi_delete_reference(env, supplementInfo.thisVar);
1530     } else if (supplementInfo.deferred != nullptr) {
1531         if (result == TELEPHONY_SUCCESS) {
1532             napi_value promiseValue = nullptr;
1533             napi_create_int32(env, status, &promiseValue);
1534             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1535         } else {
1536             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1537             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1538         }
1539     }
1540     napi_close_handle_scope(env, limitScope);
1541 }
1542 
ReportSupplementInfoWork(uv_work_t * work,int32_t status)1543 void NapiCallAbilityCallback::ReportSupplementInfoWork(uv_work_t *work, int32_t status)
1544 {
1545     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1546     if (dataWorkerData == nullptr) {
1547         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1548         return;
1549     }
1550     ReportSupplementInfo(dataWorkerData->info, dataWorkerData->callback);
1551     delete dataWorkerData;
1552     dataWorkerData = nullptr;
1553     delete work;
1554     work = nullptr;
1555 }
1556 
ReportSupplementInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1557 void NapiCallAbilityCallback::ReportSupplementInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1558 {
1559     napi_env env = supplementInfo.env;
1560     napi_handle_scope supplementScope = nullptr;
1561     napi_open_handle_scope(env, &supplementScope);
1562     if (supplementScope == nullptr) {
1563         TELEPHONY_LOGE("supplementScope is nullptr");
1564         napi_close_handle_scope(env, supplementScope);
1565         return;
1566     }
1567     napi_value callbackValue = nullptr;
1568     napi_create_object(env, &callbackValue);
1569     NapiCallManagerUtils::SetPropertyInt32(env, callbackValue, "status", resultInfo.GetIntValue("status"));
1570     NapiCallManagerUtils::SetPropertyStringUtf8(env, callbackValue, "number", resultInfo.GetStringValue("number"));
1571     int32_t result = resultInfo.GetIntValue("result");
1572     if (supplementInfo.callbackRef != nullptr) {
1573         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1574         if (result == TELEPHONY_SUCCESS) {
1575             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1576             callbackValues[ARRAY_INDEX_SECOND] = callbackValue;
1577         } else {
1578             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1579             callbackValues[ARRAY_INDEX_FIRST] =
1580                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
1581             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1582         }
1583         napi_value callbackFunc = nullptr;
1584         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1585         napi_value thisVar = nullptr;
1586         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1587         napi_value callbackResult = nullptr;
1588         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1589         napi_delete_reference(env, supplementInfo.callbackRef);
1590         napi_delete_reference(env, supplementInfo.thisVar);
1591     } else if (supplementInfo.deferred != nullptr) {
1592         if (result == TELEPHONY_SUCCESS) {
1593             napi_resolve_deferred(env, supplementInfo.deferred, callbackValue);
1594         } else {
1595             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1596             napi_reject_deferred(env, supplementInfo.deferred,
1597                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode));
1598         }
1599     }
1600     napi_close_handle_scope(env, supplementScope);
1601 }
1602 
ReportExecutionResultWork(uv_work_t * work,int32_t status)1603 void NapiCallAbilityCallback::ReportExecutionResultWork(uv_work_t *work, int32_t status)
1604 {
1605     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1606     if (dataWorkerData == nullptr) {
1607         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1608         return;
1609     }
1610     ReportExecutionResult(dataWorkerData->callback, dataWorkerData->info);
1611     delete dataWorkerData;
1612     dataWorkerData = nullptr;
1613     delete work;
1614     work = nullptr;
1615 }
1616 
ReportExecutionResult(EventCallback & settingInfo,AppExecFwk::PacMap & resultInfo)1617 void NapiCallAbilityCallback::ReportExecutionResult(EventCallback &settingInfo, AppExecFwk::PacMap &resultInfo)
1618 {
1619     napi_env env = settingInfo.env;
1620     napi_handle_scope executionScope = nullptr;
1621     napi_open_handle_scope(env, &executionScope);
1622     if (executionScope == nullptr) {
1623         TELEPHONY_LOGE("executionScope is nullptr");
1624         napi_close_handle_scope(env, executionScope);
1625         return;
1626     }
1627     napi_value callbackValue = nullptr;
1628     napi_create_object(env, &callbackValue);
1629     int32_t result = resultInfo.GetIntValue("result");
1630     if (settingInfo.callbackRef != nullptr) {
1631         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1632         if (result == TELEPHONY_SUCCESS) {
1633             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1634             napi_get_null(env, &callbackValues[ARRAY_INDEX_SECOND]);
1635         } else {
1636             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1637             callbackValues[ARRAY_INDEX_FIRST] =
1638                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
1639             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1640         }
1641         napi_value callbackFunc = nullptr;
1642         napi_get_reference_value(env, settingInfo.callbackRef, &callbackFunc);
1643         napi_value thisVar = nullptr;
1644         napi_get_reference_value(env, settingInfo.thisVar, &thisVar);
1645         napi_value callbackResult = nullptr;
1646         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1647         napi_delete_reference(env, settingInfo.callbackRef);
1648         napi_delete_reference(env, settingInfo.thisVar);
1649     } else if (settingInfo.deferred != nullptr) {
1650         if (result == TELEPHONY_SUCCESS) {
1651             napi_value promiseValue = nullptr;
1652             napi_get_null(env, &promiseValue);
1653             napi_resolve_deferred(env, settingInfo.deferred, promiseValue);
1654         } else {
1655             JsError error = NapiUtil::ConverErrorMessageForJs(result);
1656             napi_reject_deferred(env, settingInfo.deferred,
1657                 NapiCallManagerUtils::CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode));
1658         }
1659     }
1660     napi_close_handle_scope(env, executionScope);
1661 }
1662 
ReportStartRttInfoWork(uv_work_t * work,int32_t status)1663 void NapiCallAbilityCallback::ReportStartRttInfoWork(uv_work_t *work, int32_t status)
1664 {
1665     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1666     if (dataWorkerData == nullptr) {
1667         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1668         return;
1669     }
1670     ReportStartRttInfo(dataWorkerData->info, dataWorkerData->callback);
1671     delete dataWorkerData;
1672     dataWorkerData = nullptr;
1673     delete work;
1674     work = nullptr;
1675 }
1676 
ReportStartRttInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1677 void NapiCallAbilityCallback::ReportStartRttInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1678 {
1679     napi_env env = supplementInfo.env;
1680     int32_t result = resultInfo.GetIntValue("result");
1681     napi_handle_scope startRttScope = nullptr;
1682     napi_open_handle_scope(env, &startRttScope);
1683     if (startRttScope == nullptr) {
1684         TELEPHONY_LOGE("startRttScope is nullptr");
1685         napi_close_handle_scope(env, startRttScope);
1686         return;
1687     }
1688     if (supplementInfo.callbackRef != nullptr) {
1689         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1690         if (result == TELEPHONY_SUCCESS) {
1691             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1692         } else {
1693             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1694             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1695             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1696         }
1697         napi_value callbackFunc = nullptr;
1698         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1699         napi_value thisVar = nullptr;
1700         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1701         napi_value callbackResult = nullptr;
1702         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1703         napi_delete_reference(env, supplementInfo.callbackRef);
1704         napi_delete_reference(env, supplementInfo.thisVar);
1705     } else if (supplementInfo.deferred != nullptr) {
1706         if (result == TELEPHONY_SUCCESS) {
1707             napi_value promiseValue = nullptr;
1708             napi_get_null(env, &promiseValue);
1709             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1710         } else {
1711             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1712             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1713         }
1714     }
1715     napi_close_handle_scope(env, startRttScope);
1716 }
1717 
ReportStopRttInfoWork(uv_work_t * work,int32_t status)1718 void NapiCallAbilityCallback::ReportStopRttInfoWork(uv_work_t *work, int32_t status)
1719 {
1720     CallSupplementWorker *dataWorkerData = (CallSupplementWorker *)work->data;
1721     if (dataWorkerData == nullptr) {
1722         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1723         return;
1724     }
1725     ReportStopRttInfo(dataWorkerData->info, dataWorkerData->callback);
1726     delete dataWorkerData;
1727     dataWorkerData = nullptr;
1728     delete work;
1729     work = nullptr;
1730 }
1731 
ReportStopRttInfo(AppExecFwk::PacMap & resultInfo,EventCallback supplementInfo)1732 void NapiCallAbilityCallback::ReportStopRttInfo(AppExecFwk::PacMap &resultInfo, EventCallback supplementInfo)
1733 {
1734     napi_env env = supplementInfo.env;
1735     int32_t result = resultInfo.GetIntValue("result");
1736     napi_handle_scope stopRttScope = nullptr;
1737     napi_open_handle_scope(env, &stopRttScope);
1738     if (stopRttScope == nullptr) {
1739         TELEPHONY_LOGE("stopRttScope is nullptr");
1740         napi_close_handle_scope(env, stopRttScope);
1741         return;
1742     }
1743     if (supplementInfo.callbackRef != nullptr) {
1744         napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1745         if (result == TELEPHONY_SUCCESS) {
1746             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateUndefined(env);
1747         } else {
1748             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1749             callbackValues[ARRAY_INDEX_FIRST] = NapiCallManagerUtils::CreateErrorMessage(env, errTip);
1750             callbackValues[ARRAY_INDEX_SECOND] = NapiCallManagerUtils::CreateUndefined(env);
1751         }
1752         napi_value callbackFunc = nullptr;
1753         napi_get_reference_value(env, supplementInfo.callbackRef, &callbackFunc);
1754         napi_value thisVar = nullptr;
1755         napi_get_reference_value(env, supplementInfo.thisVar, &thisVar);
1756         napi_value callbackResult = nullptr;
1757         napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_TWO, callbackValues, &callbackResult);
1758         napi_delete_reference(env, supplementInfo.callbackRef);
1759         napi_delete_reference(env, supplementInfo.thisVar);
1760     } else if (supplementInfo.deferred != nullptr) {
1761         if (result == TELEPHONY_SUCCESS) {
1762             napi_value promiseValue = nullptr;
1763             napi_get_null(env, &promiseValue);
1764             napi_resolve_deferred(env, supplementInfo.deferred, promiseValue);
1765         } else {
1766             std::string errTip = std::to_string(CALL_ERR_NAPI_INTERFACE_FAILED);
1767             napi_reject_deferred(env, supplementInfo.deferred, NapiCallManagerUtils::CreateErrorMessage(env, errTip));
1768         }
1769     }
1770     napi_close_handle_scope(env, stopRttScope);
1771 }
1772 
ReportCallOttWork(uv_work_t * work,int32_t status)1773 void NapiCallAbilityCallback::ReportCallOttWork(uv_work_t *work, int32_t status)
1774 {
1775     CallOttWorker *dataWorkerData = (CallOttWorker *)work->data;
1776     if (dataWorkerData == nullptr) {
1777         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1778         return;
1779     }
1780     ReportCallOtt(dataWorkerData->callback, dataWorkerData->info, dataWorkerData->requestId);
1781     delete dataWorkerData;
1782     dataWorkerData = nullptr;
1783     delete work;
1784     work = nullptr;
1785 }
1786 
ReportCallOtt(EventCallback & settingInfo,AppExecFwk::PacMap & resultInfo,OttCallRequestId requestId)1787 int32_t NapiCallAbilityCallback::ReportCallOtt(
1788     EventCallback &settingInfo, AppExecFwk::PacMap &resultInfo, OttCallRequestId requestId)
1789 {
1790     napi_env env = settingInfo.env;
1791     napi_handle_scope callOttscope = nullptr;
1792     napi_open_handle_scope(env, &callOttscope);
1793     if (callOttscope == nullptr) {
1794         TELEPHONY_LOGE("callOttscope is nullptr");
1795         napi_close_handle_scope(env, callOttscope);
1796         return TELEPHONY_ERROR;
1797     }
1798     napi_value callbackFunc = nullptr;
1799     napi_value callbackValues[ARRAY_INDEX_THIRD] = { 0 };
1800     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
1801     NapiCallManagerUtils::SetPropertyInt32(
1802         env, callbackValues[ARRAY_INDEX_FIRST], "requestId", static_cast<int32_t>(requestId));
1803     NapiCallManagerUtils::SetPropertyStringUtf8(
1804         env, callbackValues[ARRAY_INDEX_FIRST], "phoneNumber", resultInfo.GetStringValue("phoneNumber").c_str());
1805     NapiCallManagerUtils::SetPropertyStringUtf8(
1806         env, callbackValues[ARRAY_INDEX_FIRST], "bundleName", resultInfo.GetStringValue("bundleName").c_str());
1807     NapiCallManagerUtils::SetPropertyInt32(
1808         env, callbackValues[ARRAY_INDEX_FIRST], "videoState", resultInfo.GetIntValue("videoState"));
1809     switch (requestId) {
1810         case OttCallRequestId::OTT_REQUEST_INVITE_TO_CONFERENCE:
1811             NapiCallManagerUtils::SetPropertyStringUtf8(
1812                 env, callbackValues[ARRAY_INDEX_FIRST], "number", resultInfo.GetStringValue("number").c_str());
1813             break;
1814         case OttCallRequestId::OTT_REQUEST_UPDATE_CALL_MEDIA_MODE:
1815             NapiCallManagerUtils::SetPropertyInt32(
1816                 env, callbackValues[ARRAY_INDEX_FIRST], "callMediaMode", resultInfo.GetIntValue("callMediaMode"));
1817             break;
1818         default:
1819             break;
1820     }
1821 
1822     napi_get_reference_value(env, settingInfo.callbackRef, &callbackFunc);
1823     if (callbackFunc == nullptr) {
1824         TELEPHONY_LOGE("callbackFunc is null!");
1825         napi_close_handle_scope(env, callOttscope);
1826         return CALL_ERR_CALLBACK_NOT_EXIST;
1827     }
1828     napi_value thisVar = nullptr;
1829     napi_get_reference_value(env, settingInfo.thisVar, &thisVar);
1830     napi_value callbackResult = nullptr;
1831     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1832     napi_close_handle_scope(env, callOttscope);
1833     return TELEPHONY_SUCCESS;
1834 }
1835 
UpdatePostDialDelay(const std::string str)1836 int32_t NapiCallAbilityCallback::UpdatePostDialDelay(const std::string str)
1837 {
1838     if (postDialDelayCallback_.thisVar == nullptr) {
1839         TELEPHONY_LOGE("thisVar is nullptr!");
1840         return CALL_ERR_CALLBACK_NOT_EXIST;
1841     }
1842     uv_loop_s *loop = nullptr;
1843 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1844     napi_get_uv_event_loop(postDialDelayCallback_.env, &loop);
1845 #endif
1846     if (loop == nullptr) {
1847         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1848     }
1849     PostDialDelayWorker *postDialDelayWorker = std::make_unique<PostDialDelayWorker>().release();
1850     if (postDialDelayWorker == nullptr) {
1851         TELEPHONY_LOGE("postDialDelayWorker is nullptr!");
1852         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1853     }
1854     postDialDelayWorker->postDialStr = str;
1855     postDialDelayWorker->callback = postDialDelayCallback_;
1856     uv_work_t *work = std::make_unique<uv_work_t>().release();
1857     if (work == nullptr) {
1858         delete postDialDelayWorker;
1859         postDialDelayWorker = nullptr;
1860         TELEPHONY_LOGE("work is nullptr");
1861         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1862     }
1863     work->data = (void *)postDialDelayWorker;
1864     int32_t errCode = uv_queue_work_with_qos(
1865         loop, work, [](uv_work_t *work) {
1866             TELEPHONY_LOGD("UpdatePostDialDelay uv_queue_work_with_qos");
1867         }, ReportPostDialDelayWork, uv_qos_default);
1868     if (errCode != 0) {
1869         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1870         delete postDialDelayWorker;
1871         postDialDelayWorker = nullptr;
1872         delete work;
1873         work = nullptr;
1874         return TELEPHONY_ERROR;
1875     }
1876     return TELEPHONY_SUCCESS;
1877 }
1878 
ReportPostDialDelayWork(uv_work_t * work,int32_t status)1879 void NapiCallAbilityCallback::ReportPostDialDelayWork(uv_work_t *work, int32_t status)
1880 {
1881     PostDialDelayWorker *dataWorkerData = (PostDialDelayWorker *)work->data;
1882     if (dataWorkerData == nullptr) {
1883         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1884         return;
1885     }
1886     int32_t ret = ReportPostDialDelay(dataWorkerData->postDialStr, dataWorkerData->callback);
1887     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
1888     delete dataWorkerData;
1889     dataWorkerData = nullptr;
1890     delete work;
1891     work = nullptr;
1892 }
1893 
ReportPostDialDelay(std::string postDialStr,EventCallback eventCallback)1894 int32_t NapiCallAbilityCallback::ReportPostDialDelay(std::string postDialStr, EventCallback eventCallback)
1895 {
1896     napi_env env = eventCallback.env;
1897     napi_handle_scope scope = nullptr;
1898     napi_open_handle_scope(env, &scope);
1899     if (scope == nullptr) {
1900         TELEPHONY_LOGE("scope is nullptr");
1901         napi_close_handle_scope(env, scope);
1902         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1903     }
1904     napi_value callbackFunc = nullptr;
1905     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
1906     napi_create_string_utf8(env, postDialStr.c_str(), postDialStr.length(), &callbackValues[ARRAY_INDEX_FIRST]);
1907     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
1908     if (callbackFunc == nullptr) {
1909         TELEPHONY_LOGE("callbackFunc is null!");
1910         napi_close_handle_scope(env, scope);
1911         return CALL_ERR_CALLBACK_NOT_EXIST;
1912     }
1913     napi_value thisVar = nullptr;
1914     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
1915     napi_value callbackResult = nullptr;
1916     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
1917     napi_close_handle_scope(env, scope);
1918     return TELEPHONY_SUCCESS;
1919 }
1920 
UpdateImsCallModeChange(const CallMediaModeInfo & imsCallModeInfo)1921 int32_t NapiCallAbilityCallback::UpdateImsCallModeChange(const CallMediaModeInfo &imsCallModeInfo)
1922 {
1923     if (imsCallModeCallback_.thisVar == nullptr) {
1924         TELEPHONY_LOGE("thisVar is nullptr!");
1925         return CALL_ERR_CALLBACK_NOT_EXIST;
1926     }
1927     uv_loop_s *loop = nullptr;
1928 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
1929     napi_get_uv_event_loop(imsCallModeCallback_.env, &loop);
1930 #endif
1931     if (loop == nullptr) {
1932         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1933     }
1934     ImsCallModeInfoWorker *imsCallModeInfoWorker = std::make_unique<ImsCallModeInfoWorker>().release();
1935     if (imsCallModeInfoWorker == nullptr) {
1936         TELEPHONY_LOGE("imsCallModeInfoWorker is nullptr!");
1937         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1938     }
1939     imsCallModeInfoWorker->callModeInfo = imsCallModeInfo;
1940     imsCallModeInfoWorker->callback = imsCallModeCallback_;
1941     uv_work_t *work = std::make_unique<uv_work_t>().release();
1942     if (work == nullptr) {
1943         delete imsCallModeInfoWorker;
1944         imsCallModeInfoWorker = nullptr;
1945         TELEPHONY_LOGE("work is nullptr");
1946         return TELEPHONY_ERR_LOCAL_PTR_NULL;
1947     }
1948     work->data = (void *)imsCallModeInfoWorker;
1949     int32_t errCode = uv_queue_work_with_qos(
1950         loop, work, [](uv_work_t *work) {
1951             TELEPHONY_LOGD("UpdateImsCallModeChange uv_queue_work_with_qos");
1952         }, ReportCallMediaModeInfoWork, uv_qos_default);
1953     if (errCode != 0) {
1954         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
1955         delete imsCallModeInfoWorker;
1956         imsCallModeInfoWorker = nullptr;
1957         delete work;
1958         work = nullptr;
1959         return TELEPHONY_ERROR;
1960     }
1961     return TELEPHONY_SUCCESS;
1962 }
1963 
ReportCallMediaModeInfoWork(uv_work_t * work,int32_t status)1964 void NapiCallAbilityCallback::ReportCallMediaModeInfoWork(uv_work_t *work, int32_t status)
1965 {
1966     ImsCallModeInfoWorker *dataWorkerData = (ImsCallModeInfoWorker *)work->data;
1967     if (dataWorkerData == nullptr) {
1968         TELEPHONY_LOGE("dataWorkerData is nullptr!");
1969         return;
1970     }
1971     int32_t ret = ReportCallMediaModeInfo(dataWorkerData->callModeInfo, dataWorkerData->callback);
1972     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
1973     delete dataWorkerData;
1974     dataWorkerData = nullptr;
1975     delete work;
1976     work = nullptr;
1977 }
1978 
ReportCallMediaModeInfo(CallMediaModeInfo & imsCallModeInfo,EventCallback eventCallback)1979 int32_t NapiCallAbilityCallback::ReportCallMediaModeInfo(
1980     CallMediaModeInfo &imsCallModeInfo, EventCallback eventCallback)
1981 {
1982     napi_env env = eventCallback.env;
1983     napi_handle_scope CallMediaModeInfoScope = nullptr;
1984     napi_open_handle_scope(env, &CallMediaModeInfoScope);
1985     if (CallMediaModeInfoScope == nullptr) {
1986         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
1987         napi_close_handle_scope(env, CallMediaModeInfoScope);
1988         return TELEPHONY_ERROR;
1989     }
1990     napi_value callbackFunc = nullptr;
1991     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
1992 
1993     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
1994     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", imsCallModeInfo.callId);
1995     NapiCallManagerUtils::SetPropertyBoolean(
1996         env, callbackValues[ARRAY_INDEX_FIRST], "isRequestInfo", imsCallModeInfo.isRequestInfo);
1997     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "result", imsCallModeInfo.result);
1998     NapiCallManagerUtils::SetPropertyInt32(
1999         env, callbackValues[ARRAY_INDEX_FIRST], "imsCallMode", static_cast<int32_t>(imsCallModeInfo.callMode));
2000 
2001     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2002     if (callbackFunc == nullptr) {
2003         TELEPHONY_LOGE("callbackFunc is null!");
2004         napi_close_handle_scope(env, CallMediaModeInfoScope);
2005         return CALL_ERR_CALLBACK_NOT_EXIST;
2006     }
2007     napi_value thisVar = nullptr;
2008     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2009     napi_value callbackResult = nullptr;
2010     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2011     napi_close_handle_scope(env, CallMediaModeInfoScope);
2012     return TELEPHONY_SUCCESS;
2013 }
2014 
CallSessionEventChange(const CallSessionEvent & callSessionEvent)2015 int32_t NapiCallAbilityCallback::CallSessionEventChange(const CallSessionEvent &callSessionEvent)
2016 {
2017     if (callSessionEventCallback_.thisVar == nullptr) {
2018         TELEPHONY_LOGE("thisVar is nullptr!");
2019         return CALL_ERR_CALLBACK_NOT_EXIST;
2020     }
2021     uv_loop_s *loop = nullptr;
2022 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2023     napi_get_uv_event_loop(callSessionEventCallback_.env, &loop);
2024 #endif
2025     if (loop == nullptr) {
2026         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2027     }
2028     CallSessionEventWorker *callSessionEventWorker = std::make_unique<CallSessionEventWorker>().release();
2029     if (callSessionEventWorker == nullptr) {
2030         TELEPHONY_LOGE("callSessionEventWorker is nullptr!");
2031         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2032     }
2033     callSessionEventWorker->sessionEvent = callSessionEvent;
2034     callSessionEventWorker->callback = callSessionEventCallback_;
2035     uv_work_t *work = std::make_unique<uv_work_t>().release();
2036     if (work == nullptr) {
2037         delete callSessionEventWorker;
2038         callSessionEventWorker = nullptr;
2039         TELEPHONY_LOGE("work is nullptr");
2040         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2041     }
2042     work->data = (void *)callSessionEventWorker;
2043     int32_t errCode = uv_queue_work_with_qos(
2044         loop, work, [](uv_work_t *work) {
2045             TELEPHONY_LOGD("CallSessionEventChange uv_queue_work_with_qos");
2046         }, ReportCallSessionEventWork, uv_qos_default);
2047     if (errCode != 0) {
2048         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2049         delete callSessionEventWorker;
2050         callSessionEventWorker = nullptr;
2051         delete work;
2052         work = nullptr;
2053         return TELEPHONY_ERROR;
2054     }
2055     return TELEPHONY_SUCCESS;
2056 }
2057 
ReportCallSessionEventWork(uv_work_t * work,int32_t status)2058 void NapiCallAbilityCallback::ReportCallSessionEventWork(uv_work_t *work, int32_t status)
2059 {
2060     CallSessionEventWorker *dataWorkerData = (CallSessionEventWorker *)work->data;
2061     if (dataWorkerData == nullptr) {
2062         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2063         return;
2064     }
2065     int32_t ret = ReportCallSessionEvent(dataWorkerData->sessionEvent, dataWorkerData->callback);
2066     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2067     delete dataWorkerData;
2068     dataWorkerData = nullptr;
2069     delete work;
2070     work = nullptr;
2071 }
2072 
ReportCallSessionEvent(CallSessionEvent & sessionEvent,EventCallback eventCallback)2073 int32_t NapiCallAbilityCallback::ReportCallSessionEvent(
2074     CallSessionEvent &sessionEvent, EventCallback eventCallback)
2075 {
2076     napi_env env = eventCallback.env;
2077     napi_handle_scope CallSessionEventScope = nullptr;
2078     napi_open_handle_scope(env, &CallSessionEventScope);
2079     if (CallSessionEventScope == nullptr) {
2080         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2081         napi_close_handle_scope(env, CallSessionEventScope);
2082         return TELEPHONY_ERROR;
2083     }
2084     napi_value callbackFunc = nullptr;
2085     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2086 
2087     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2088     NapiCallManagerUtils::SetPropertyInt32(env, callbackValues[ARRAY_INDEX_FIRST], "callId", sessionEvent.callId);
2089     NapiCallManagerUtils::SetPropertyInt32(
2090         env, callbackValues[ARRAY_INDEX_FIRST], "eventId", static_cast<int32_t>(sessionEvent.eventId));
2091 
2092     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2093     if (callbackFunc == nullptr) {
2094         TELEPHONY_LOGE("callbackFunc is null!");
2095         napi_close_handle_scope(env, CallSessionEventScope);
2096         return CALL_ERR_CALLBACK_NOT_EXIST;
2097     }
2098     napi_value thisVar = nullptr;
2099     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2100     napi_value callbackResult = nullptr;
2101     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2102     napi_close_handle_scope(env, CallSessionEventScope);
2103     return TELEPHONY_SUCCESS;
2104 }
2105 
PeerDimensionsChange(const PeerDimensionsDetail & peerDimensionsDetail)2106 int32_t NapiCallAbilityCallback::PeerDimensionsChange(const PeerDimensionsDetail &peerDimensionsDetail)
2107 {
2108     if (peerDimensionsCallback_.thisVar == nullptr) {
2109         TELEPHONY_LOGE("thisVar is nullptr!");
2110         return CALL_ERR_CALLBACK_NOT_EXIST;
2111     }
2112     uv_loop_s *loop = nullptr;
2113 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2114     napi_get_uv_event_loop(peerDimensionsCallback_.env, &loop);
2115 #endif
2116     if (loop == nullptr) {
2117         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2118     }
2119     PeerDimensionsWorker *peerDimensionsWorker = std::make_unique<PeerDimensionsWorker>().release();
2120     if (peerDimensionsWorker == nullptr) {
2121         TELEPHONY_LOGE("peerDimensionsWorker is nullptr!");
2122         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2123     }
2124     peerDimensionsWorker->peerDimensionsDetail = peerDimensionsDetail;
2125     peerDimensionsWorker->callback = peerDimensionsCallback_;
2126     uv_work_t *work = std::make_unique<uv_work_t>().release();
2127     if (work == nullptr) {
2128         delete peerDimensionsWorker;
2129         peerDimensionsWorker = nullptr;
2130         TELEPHONY_LOGE("work is nullptr");
2131         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2132     }
2133     work->data = (void *)peerDimensionsWorker;
2134     int32_t errCode = uv_queue_work_with_qos(
2135         loop, work, [](uv_work_t *work) {
2136             TELEPHONY_LOGD("PeerDimensionsChange uv_queue_work_with_qos");
2137         }, ReportPeerDimensionsWork, uv_qos_default);
2138     if (errCode != 0) {
2139         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2140         delete peerDimensionsWorker;
2141         peerDimensionsWorker = nullptr;
2142         delete work;
2143         work = nullptr;
2144         return TELEPHONY_ERROR;
2145     }
2146     return TELEPHONY_SUCCESS;
2147 }
2148 
ReportPeerDimensionsWork(uv_work_t * work,int32_t status)2149 void NapiCallAbilityCallback::ReportPeerDimensionsWork(uv_work_t *work, int32_t status)
2150 {
2151     PeerDimensionsWorker *dataWorkerData = (PeerDimensionsWorker *)work->data;
2152     if (dataWorkerData == nullptr) {
2153         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2154         return;
2155     }
2156     int32_t ret = ReportPeerDimensions(dataWorkerData->peerDimensionsDetail, dataWorkerData->callback);
2157     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2158     delete dataWorkerData;
2159     dataWorkerData = nullptr;
2160     delete work;
2161     work = nullptr;
2162 }
2163 
ReportPeerDimensions(PeerDimensionsDetail & peerDimensionsDetail,EventCallback eventCallback)2164 int32_t NapiCallAbilityCallback::ReportPeerDimensions(
2165     PeerDimensionsDetail &peerDimensionsDetail, EventCallback eventCallback)
2166 {
2167     napi_env env = eventCallback.env;
2168     napi_handle_scope PeerDimensionsDetailScope = nullptr;
2169     napi_open_handle_scope(env, &PeerDimensionsDetailScope);
2170     if (PeerDimensionsDetailScope == nullptr) {
2171         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2172         napi_close_handle_scope(env, PeerDimensionsDetailScope);
2173         return TELEPHONY_ERROR;
2174     }
2175     napi_value callbackFunc = nullptr;
2176     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2177 
2178     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2179     NapiCallManagerUtils::SetPropertyInt32(
2180         env, callbackValues[ARRAY_INDEX_FIRST], "callId", peerDimensionsDetail.callId);
2181     NapiCallManagerUtils::SetPropertyInt32(
2182         env, callbackValues[ARRAY_INDEX_FIRST], "width", peerDimensionsDetail.width);
2183     NapiCallManagerUtils::SetPropertyInt32(
2184         env, callbackValues[ARRAY_INDEX_FIRST], "height", peerDimensionsDetail.height);
2185 
2186     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2187     if (callbackFunc == nullptr) {
2188         TELEPHONY_LOGE("callbackFunc is null!");
2189         napi_close_handle_scope(env, PeerDimensionsDetailScope);
2190         return CALL_ERR_CALLBACK_NOT_EXIST;
2191     }
2192     napi_value thisVar = nullptr;
2193     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2194     napi_value callbackResult = nullptr;
2195     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2196     napi_close_handle_scope(env, PeerDimensionsDetailScope);
2197     return TELEPHONY_SUCCESS;
2198 }
2199 
CallDataUsageChange(const int64_t dataUsage)2200 int32_t NapiCallAbilityCallback::CallDataUsageChange(const int64_t dataUsage)
2201 {
2202     if (callDataUsageCallback_.thisVar == nullptr) {
2203         TELEPHONY_LOGE("thisVar is nullptr!");
2204         return CALL_ERR_CALLBACK_NOT_EXIST;
2205     }
2206     uv_loop_s *loop = nullptr;
2207 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2208     napi_get_uv_event_loop(callDataUsageCallback_.env, &loop);
2209 #endif
2210     if (loop == nullptr) {
2211         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2212     }
2213     CallDataUsageWorker *callDataUsageWorker = std::make_unique<CallDataUsageWorker>().release();
2214     if (callDataUsageWorker == nullptr) {
2215         TELEPHONY_LOGE("callDataUsageWorker is nullptr!");
2216         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2217     }
2218     callDataUsageWorker->callDataUsage = dataUsage;
2219     callDataUsageWorker->callback = callDataUsageCallback_;
2220     uv_work_t *work = std::make_unique<uv_work_t>().release();
2221     if (work == nullptr) {
2222         delete callDataUsageWorker;
2223         callDataUsageWorker = nullptr;
2224         TELEPHONY_LOGE("work is nullptr");
2225         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2226     }
2227     work->data = (void *)callDataUsageWorker;
2228     int32_t errCode = uv_queue_work_with_qos(
2229         loop, work, [](uv_work_t *work) {
2230             TELEPHONY_LOGD("CallDataUsageChange uv_queue_work_with_qos");
2231         }, ReportCallDataUsageWork, uv_qos_default);
2232     if (errCode != 0) {
2233         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2234         delete callDataUsageWorker;
2235         callDataUsageWorker = nullptr;
2236         delete work;
2237         work = nullptr;
2238         return TELEPHONY_ERROR;
2239     }
2240     return TELEPHONY_SUCCESS;
2241 }
2242 
ReportCallDataUsageWork(uv_work_t * work,int32_t status)2243 void NapiCallAbilityCallback::ReportCallDataUsageWork(uv_work_t *work, int32_t status)
2244 {
2245     CallDataUsageWorker *dataWorkerData = (CallDataUsageWorker *)work->data;
2246     if (dataWorkerData == nullptr) {
2247         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2248         return;
2249     }
2250     int32_t ret = ReportCallDataUsage(dataWorkerData->callDataUsage, dataWorkerData->callback);
2251     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2252     delete dataWorkerData;
2253     dataWorkerData = nullptr;
2254     delete work;
2255     work = nullptr;
2256 }
2257 
ReportCallDataUsage(int64_t dataUsage,EventCallback eventCallback)2258 int32_t NapiCallAbilityCallback::ReportCallDataUsage(int64_t dataUsage, EventCallback eventCallback)
2259 {
2260     napi_env env = eventCallback.env;
2261     napi_handle_scope CallDataUsageScope = nullptr;
2262     napi_open_handle_scope(env, &CallDataUsageScope);
2263     if (CallDataUsageScope == nullptr) {
2264         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2265         napi_close_handle_scope(env, CallDataUsageScope);
2266         return TELEPHONY_ERROR;
2267     }
2268     napi_value callbackFunc = nullptr;
2269     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2270     napi_create_int64(env, dataUsage, &callbackValues[ARRAY_INDEX_FIRST]);
2271 
2272     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2273     if (callbackFunc == nullptr) {
2274         TELEPHONY_LOGE("callbackFunc is null!");
2275         napi_close_handle_scope(env, CallDataUsageScope);
2276         return CALL_ERR_CALLBACK_NOT_EXIST;
2277     }
2278     napi_value thisVar = nullptr;
2279     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2280     napi_value callbackResult = nullptr;
2281     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2282     napi_close_handle_scope(env, CallDataUsageScope);
2283     return TELEPHONY_SUCCESS;
2284 }
2285 
UpdateCameraCapabilities(const CameraCapabilities & cameraCapabilities)2286 int32_t NapiCallAbilityCallback::UpdateCameraCapabilities(const CameraCapabilities &cameraCapabilities)
2287 {
2288     if (cameraCapabilitiesCallback_.thisVar == nullptr) {
2289         TELEPHONY_LOGE("thisVar is nullptr!");
2290         return CALL_ERR_CALLBACK_NOT_EXIST;
2291     }
2292     uv_loop_s *loop = nullptr;
2293 #if defined(NAPI_VERSION) && NAPI_VERSION >= 2
2294     napi_get_uv_event_loop(cameraCapabilitiesCallback_.env, &loop);
2295 #endif
2296     if (loop == nullptr) {
2297         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2298     }
2299     CameraCapbilitiesWorker *cameraCapbilitiesWorker = std::make_unique<CameraCapbilitiesWorker>().release();
2300     if (cameraCapbilitiesWorker == nullptr) {
2301         TELEPHONY_LOGE("cameraCapbilitiesWorker is nullptr!");
2302         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2303     }
2304     cameraCapbilitiesWorker->cameraCapabilities = cameraCapabilities;
2305     cameraCapbilitiesWorker->callback = cameraCapabilitiesCallback_;
2306     uv_work_t *work = std::make_unique<uv_work_t>().release();
2307     if (work == nullptr) {
2308         delete cameraCapbilitiesWorker;
2309         cameraCapbilitiesWorker = nullptr;
2310         TELEPHONY_LOGE("work is nullptr");
2311         return TELEPHONY_ERR_LOCAL_PTR_NULL;
2312     }
2313     work->data = (void *)cameraCapbilitiesWorker;
2314     int32_t errCode = uv_queue_work_with_qos(
2315         loop, work, [](uv_work_t *work) {
2316             TELEPHONY_LOGD("UpdateCameraCapabilities uv_queue_work_with_qos");
2317         }, ReportCameraCapabilitiesInfoWork, uv_qos_default);
2318     if (errCode != 0) {
2319         TELEPHONY_LOGE("failed to uv_queue_work_with_qos, errCode: %{public}d", errCode);
2320         delete cameraCapbilitiesWorker;
2321         cameraCapbilitiesWorker = nullptr;
2322         delete work;
2323         work = nullptr;
2324         return TELEPHONY_ERROR;
2325     }
2326     return TELEPHONY_SUCCESS;
2327 }
2328 
ReportCameraCapabilitiesInfoWork(uv_work_t * work,int32_t status)2329 void NapiCallAbilityCallback::ReportCameraCapabilitiesInfoWork(uv_work_t *work, int32_t status)
2330 {
2331     CameraCapbilitiesWorker *dataWorkerData = (CameraCapbilitiesWorker *)work->data;
2332     if (dataWorkerData == nullptr) {
2333         TELEPHONY_LOGE("dataWorkerData is nullptr!");
2334         return;
2335     }
2336     int32_t ret = ReportCameraCapabilitiesInfo(dataWorkerData->cameraCapabilities, dataWorkerData->callback);
2337     TELEPHONY_LOGI("ReportPostDialDelay result = %{public}d", ret);
2338     delete dataWorkerData;
2339     dataWorkerData = nullptr;
2340     delete work;
2341     work = nullptr;
2342 }
2343 
ReportCameraCapabilitiesInfo(CameraCapabilities & cameraCapabilities,EventCallback eventCallback)2344 int32_t NapiCallAbilityCallback::ReportCameraCapabilitiesInfo(
2345     CameraCapabilities &cameraCapabilities, EventCallback eventCallback)
2346 {
2347     napi_env env = eventCallback.env;
2348     napi_handle_scope cameraCapabilitiesScope = nullptr;
2349     napi_open_handle_scope(env, &cameraCapabilitiesScope);
2350     if (cameraCapabilitiesScope == nullptr) {
2351         TELEPHONY_LOGE("CallMediaModeInfoScope is nullptr");
2352         napi_close_handle_scope(env, cameraCapabilitiesScope);
2353         return TELEPHONY_ERROR;
2354     }
2355     napi_value callbackFunc = nullptr;
2356     napi_value callbackValues[ARRAY_INDEX_SECOND] = { 0 };
2357 
2358     napi_create_object(env, &callbackValues[ARRAY_INDEX_FIRST]);
2359     NapiCallManagerUtils::SetPropertyInt32(
2360         env, callbackValues[ARRAY_INDEX_FIRST], "callId", cameraCapabilities.callId);
2361     NapiCallManagerUtils::SetPropertyInt32(
2362         env, callbackValues[ARRAY_INDEX_FIRST], "width", cameraCapabilities.width);
2363     NapiCallManagerUtils::SetPropertyInt32(
2364         env, callbackValues[ARRAY_INDEX_FIRST], "height", cameraCapabilities.height);
2365 
2366     napi_get_reference_value(env, eventCallback.callbackRef, &callbackFunc);
2367     if (callbackFunc == nullptr) {
2368         TELEPHONY_LOGE("callbackFunc is null!");
2369         napi_close_handle_scope(env, cameraCapabilitiesScope);
2370         return CALL_ERR_CALLBACK_NOT_EXIST;
2371     }
2372     napi_value thisVar = nullptr;
2373     napi_get_reference_value(env, eventCallback.thisVar, &thisVar);
2374     napi_value callbackResult = nullptr;
2375     napi_call_function(env, thisVar, callbackFunc, DATA_LENGTH_ONE, callbackValues, &callbackResult);
2376     napi_close_handle_scope(env, cameraCapabilitiesScope);
2377     return TELEPHONY_SUCCESS;
2378 }
2379 } // namespace Telephony
2380 } // namespace OHOS
2381