• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ErrCode DistributedOperationCallback::OnOperationCallback(const int32_t operationResult)
32 {
33     OperationOnCallBack *operationOnCallBack = new (std::nothrow) OperationOnCallBack();
34     if (operationOnCallBack == nullptr) {
35         ANS_LOGE("null operationOnCallBack");
36         return ERR_INVALID_DATA;
37     }
38 
39     if (operationResult != ERR_OK) {
40         operationOnCallBack->operationResult = OHOS::Notification::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_LOGE("OnCallBack failed");
49     }
50     return ERR_OK;
51 }
52 
UvWorkOnCallBack(uv_work_t * work,int32_t status)53 void DistributedOperationCallback::UvWorkOnCallBack(uv_work_t *work, int32_t status)
54 {
55     if (work == nullptr) {
56         ANS_LOGE("null work");
57         return;
58     }
59     OperationOnCallBack *callBackPtr = static_cast<OperationOnCallBack *>(work->data);
60     if (callBackPtr == nullptr) {
61         ANS_LOGE("null callBackPtr");
62         if (work != nullptr) {
63             delete work;
64             work = nullptr;
65         }
66         return;
67     }
68 
69     napi_handle_scope scope = nullptr;
70     napi_open_handle_scope(callBackPtr->env, &scope);
71     if (callBackPtr->deferred) {
72         Common::SetPromise(callBackPtr->env, callBackPtr->deferred, callBackPtr->operationResult,
73             Common::NapiGetNull(callBackPtr->env), false);
74     }
75     napi_close_handle_scope(callBackPtr->env, scope);
76     if (callBackPtr != nullptr) {
77         delete callBackPtr;
78         callBackPtr = nullptr;
79     }
80     if (work != nullptr) {
81         delete work;
82         work = nullptr;
83     }
84     ANS_LOGD("end");
85 }
86 }  // namespace NotificationNapi
87 }  // namespace OHOS
88