1 /*
2 * Copyright (C) 2021 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 "send_callback.h"
17
18 #include <uv.h>
19
20 #include "telephony_log_wrapper.h"
21 #include "napi_util.h"
22
23 namespace OHOS {
24 namespace Telephony {
WrapSmsSendResult(const ISendShortMessageCallback::SmsSendResult result)25 SendSmsResult SendCallback::WrapSmsSendResult(const ISendShortMessageCallback::SmsSendResult result)
26 {
27 switch (result) {
28 case ISendShortMessageCallback::SmsSendResult::SEND_SMS_SUCCESS: {
29 return SendSmsResult::SEND_SMS_SUCCESS;
30 }
31 case ISendShortMessageCallback::SmsSendResult::SEND_SMS_FAILURE_RADIO_OFF: {
32 return SendSmsResult::SEND_SMS_FAILURE_RADIO_OFF;
33 }
34 case ISendShortMessageCallback::SmsSendResult::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE: {
35 return SendSmsResult::SEND_SMS_FAILURE_SERVICE_UNAVAILABLE;
36 }
37 default: {
38 return SendSmsResult::SEND_SMS_FAILURE_UNKNOWN;
39 }
40 }
41 }
42
SendCallback(bool hasCallback,napi_env env,napi_ref thisVarRef,napi_ref callbackRef)43 SendCallback::SendCallback(bool hasCallback, napi_env env, napi_ref thisVarRef, napi_ref callbackRef)
44 : hasCallback_(hasCallback), env_(env), thisVarRef_(thisVarRef), callbackRef_(callbackRef)
45 {}
46
~SendCallback()47 SendCallback::~SendCallback()
48 {
49 env_ = nullptr;
50 thisVarRef_ = nullptr;
51 callbackRef_ = nullptr;
52 }
53
CompleteSmsSendWork(uv_work_t * work,int status)54 void CompleteSmsSendWork(uv_work_t *work, int status)
55 {
56 TELEPHONY_LOGI("CompleteSmsSendWork start");
57 if (work == nullptr) {
58 TELEPHONY_LOGE("CompleteSmsSendWork work is nullptr!");
59 return;
60 }
61 std::unique_ptr<SendCallbackContext> pContext(static_cast<SendCallbackContext *>(work->data));
62 if (pContext == nullptr) {
63 TELEPHONY_LOGE("CompleteSmsSendWork pContext is nullptr!");
64 return;
65 }
66 napi_env env_ = pContext->env;
67 napi_ref thisVarRef_ = pContext->thisVarRef;
68 napi_ref callbackRef_ = pContext->callbackRef;
69 SendSmsResult wrapResult = pContext->result;
70 napi_handle_scope scope = nullptr;
71 napi_open_handle_scope(env_, &scope);
72 if (scope == nullptr) {
73 TELEPHONY_LOGE("scope is nullptr");
74 napi_close_handle_scope(env_, scope);
75 return;
76 }
77 napi_value callbackFunc = nullptr;
78 napi_get_reference_value(env_, callbackRef_, &callbackFunc);
79 napi_value callbackValues[2] = {0};
80 callbackValues[0] = NapiUtil::CreateUndefined(env_);
81 napi_create_object(env_, &callbackValues[1]);
82 napi_value sendResultValue = nullptr;
83 napi_create_int32(env_, wrapResult, &sendResultValue);
84 napi_set_named_property(env_, callbackValues[1], "result", sendResultValue);
85 napi_value callbackResult = nullptr;
86 napi_value thisVar = nullptr;
87 napi_get_reference_value(env_, thisVarRef_, &thisVar);
88 int satatus = napi_call_function(env_, thisVar, callbackFunc, 2, callbackValues, &callbackResult);
89 TELEPHONY_LOGI("OnSmsSendResult napi_call_function satatus = %d", satatus);
90 napi_close_handle_scope(env_, scope);
91 if (work != nullptr) {
92 delete work;
93 work = nullptr;
94 }
95 TELEPHONY_LOGI("CompleteSmsSendWork end");
96 }
97
OnSmsSendResult(const ISendShortMessageCallback::SmsSendResult result)98 void SendCallback::OnSmsSendResult(const ISendShortMessageCallback::SmsSendResult result)
99 {
100 TELEPHONY_LOGI("OnSmsSendResult hasCallback_ = %d", hasCallback_);
101 if (hasCallback_) {
102 uv_loop_s *loop = nullptr;
103 napi_get_uv_event_loop(env_, &loop);
104 uv_work_t *work = new uv_work_t;
105 if (work == nullptr) {
106 TELEPHONY_LOGE("OnSmsSendResult work is nullptr!");
107 return;
108 }
109 SendCallbackContext *pContext = std::make_unique<SendCallbackContext>().release();
110 if (pContext == nullptr) {
111 TELEPHONY_LOGE("OnSmsSendResult pContext is nullptr!");
112 delete work;
113 work = nullptr;
114 return;
115 }
116 pContext->env = env_;
117 pContext->thisVarRef = thisVarRef_;
118 pContext->callbackRef = callbackRef_;
119 pContext->result = WrapSmsSendResult(result);
120 work->data = static_cast<void *>(pContext);
121 uv_queue_work_with_qos(
122 loop, work, [](uv_work_t *work) {},
123 [](uv_work_t *work, int status) { CompleteSmsSendWork(work, status); }, uv_qos_default);
124 }
125 }
126 } // namespace Telephony
127 } // namespace OHOS