1 /*
2 * Copyright (c) 2025 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 "distributed_operation_callback.h"
17 #include "ans_inner_errors.h"
18 #include "uv_queue.h"
19
20 namespace OHOS {
21 namespace NotificationNapi {
DistributedOperationCallback(const AsyncOperationCallbackInfo & asyncCallbackInfo)22 DistributedOperationCallback::DistributedOperationCallback(const AsyncOperationCallbackInfo &asyncCallbackInfo)
23 {
24 asyncCallbackInfo_ = asyncCallbackInfo;
25 }
26
~DistributedOperationCallback()27 DistributedOperationCallback::~DistributedOperationCallback()
28 {
29 }
30
OnOperationCallback(const int32_t operationResult)31 void DistributedOperationCallback::OnOperationCallback(const int32_t operationResult)
32 {
33 OperationOnCallBack *operationOnCallBack = new (std::nothrow) OperationOnCallBack();
34 if (operationOnCallBack == nullptr) {
35 ANS_LOGW("new operationOnCallBack failed");
36 return;
37 }
38
39 if (operationResult != ERR_OK) {
40 operationOnCallBack->operationResult = Common::ErrorToExternal(operationResult);
41 } else {
42 operationOnCallBack->operationResult = operationResult;
43 }
44 operationOnCallBack->env = asyncCallbackInfo_.env;
45 operationOnCallBack->deferred = asyncCallbackInfo_.deferred;
46 bool bRet = UvQueue::Call(asyncCallbackInfo_.env, operationOnCallBack, UvWorkOnCallBack);
47 if (!bRet) {
48 ANS_LOGW("DistributedOperationCallback::OnCallBack failed");
49 }
50 }
51
UvWorkOnCallBack(uv_work_t * work,int32_t status)52 void DistributedOperationCallback::UvWorkOnCallBack(uv_work_t *work, int32_t status)
53 {
54 if (work == nullptr) {
55 ANS_LOGW("UvWorkOnCallBack, work is null");
56 return;
57 }
58 OperationOnCallBack *callBackPtr = static_cast<OperationOnCallBack *>(work->data);
59 if (callBackPtr == nullptr) {
60 ANS_LOGW("UvWorkOnCallBack, callBackPtr is null");
61 if (work != nullptr) {
62 delete work;
63 work = nullptr;
64 }
65 return;
66 }
67
68 napi_handle_scope scope = nullptr;
69 napi_open_handle_scope(callBackPtr->env, &scope);
70 if (callBackPtr->deferred) {
71 Common::SetPromise(callBackPtr->env, callBackPtr->deferred, callBackPtr->operationResult,
72 Common::NapiGetNull(callBackPtr->env), false);
73 }
74 napi_close_handle_scope(callBackPtr->env, scope);
75 if (callBackPtr != nullptr) {
76 delete callBackPtr;
77 callBackPtr = nullptr;
78 }
79 if (work != nullptr) {
80 delete work;
81 work = nullptr;
82 }
83 ANS_LOGD("UvWorkOnCallBack end");
84 }
85 } // namespace NotificationNapi
86 } // namespace OHOS
87