• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "delivery_callback.h"
17 
18 #include <uv.h>
19 
20 #include "napi_util.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
DeliveryCallback(bool hasCallback,napi_env env,napi_ref thisVarRef,napi_ref callbackRef)25 DeliveryCallback::DeliveryCallback(bool hasCallback, napi_env env, napi_ref thisVarRef, napi_ref callbackRef)
26     : hasCallback_(hasCallback), env_(env), thisVarRef_(thisVarRef), callbackRef_(callbackRef)
27 {}
28 
~DeliveryCallback()29 DeliveryCallback::~DeliveryCallback()
30 {
31     env_ = nullptr;
32     thisVarRef_ = nullptr;
33     callbackRef_ = nullptr;
34 }
35 
CompleteSmsDeliveryWork(uv_work_t * work,int status)36 void CompleteSmsDeliveryWork(uv_work_t *work, int status)
37 {
38     TELEPHONY_LOGI("CompleteSmsDeliveryWork start");
39     if (work == nullptr) {
40         TELEPHONY_LOGE("CompleteSmsDeliveryWork work is nullptr");
41         return;
42     }
43     std::unique_ptr<DeliveryCallbackContext> pContext(static_cast<DeliveryCallbackContext *>(work->data));
44     if (pContext == nullptr) {
45         TELEPHONY_LOGE("CompleteSmsDeliveryWork pContext is nullptr!");
46         return;
47     }
48     napi_env env_ = pContext->env;
49     napi_ref thisVarRef_ = pContext->thisVarRef;
50     napi_ref callbackRef_ = pContext->callbackRef;
51     std::string pduStr_ = pContext->pduStr;
52     napi_handle_scope scope = nullptr;
53     napi_open_handle_scope(env_, &scope);
54     if (scope == nullptr) {
55         TELEPHONY_LOGE("scope is nullptr");
56         napi_close_handle_scope(env_, scope);
57         return;
58     }
59     napi_value callbackFunc = nullptr;
60     napi_get_reference_value(env_, callbackRef_, &callbackFunc);
61     napi_value callbackValues[2] = {0};
62     if (!pduStr_.empty()) {
63         callbackValues[0] = NapiUtil::CreateUndefined(env_);
64         napi_create_object(env_, &callbackValues[1]);
65         napi_value arrayValue = nullptr;
66         napi_create_array(env_, &arrayValue);
67         for (uint32_t i = 0; i < static_cast<uint32_t>(pduStr_.size()); ++i) {
68             napi_value element = nullptr;
69             int32_t intValue = pduStr_[i];
70             napi_create_int32(env_, intValue, &element);
71             napi_set_element(env_, arrayValue, i, element);
72         }
73         std::string pduStr = "pdu";
74         napi_set_named_property(env_, callbackValues[1], pduStr.c_str(), arrayValue);
75     } else {
76         callbackValues[0] = NapiUtil::CreateErrorMessage(env_, "invalid delivery report");
77         callbackValues[1] = NapiUtil::CreateUndefined(env_);
78     }
79     napi_value callbackResult = nullptr;
80     napi_value thisVar = nullptr;
81     size_t argc = sizeof(callbackValues) / sizeof(callbackValues[0]);
82     napi_get_reference_value(env_, thisVarRef_, &thisVar);
83     napi_call_function(env_, thisVar, callbackFunc, argc, callbackValues, &callbackResult);
84     napi_delete_reference(env_, thisVarRef_);
85     napi_delete_reference(env_, callbackRef_);
86     napi_close_handle_scope(env_, scope);
87     delete work;
88     work = nullptr;
89     TELEPHONY_LOGI("CompleteSmsDeliveryWork end");
90 }
91 
OnSmsDeliveryResult(const std::u16string & pdu)92 void DeliveryCallback::OnSmsDeliveryResult(const std::u16string &pdu)
93 {
94     TELEPHONY_LOGI("OnSmsDeliveryResult start");
95     if (hasCallback_) {
96         uv_loop_s *loop = nullptr;
97         napi_get_uv_event_loop(env_, &loop);
98         uv_work_t *work = new uv_work_t;
99         if (work == nullptr) {
100             TELEPHONY_LOGE("OnSmsDeliveryResult work is nullptr!");
101             return;
102         }
103         DeliveryCallbackContext *pContext = std::make_unique<DeliveryCallbackContext>().release();
104         if (pContext == nullptr) {
105             TELEPHONY_LOGE("OnSmsDeliveryResult pContext is nullptr!");
106             delete work;
107             work = nullptr;
108             return;
109         }
110         pContext->env = env_;
111         pContext->thisVarRef = thisVarRef_;
112         pContext->callbackRef = callbackRef_;
113         pContext->pduStr = NapiUtil::ToUtf8(pdu);
114         work->data = static_cast<void *>(pContext);
115         uv_queue_work(
116             loop, work, [](uv_work_t *work) {},
117             [](uv_work_t *work, int status) { CompleteSmsDeliveryWork(work, status); });
118     }
119 }
120 } // namespace Telephony
121 } // namespace OHOS