1 /*
2  * Copyright (c) 2022-2023 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 #define LOG_TAG "PasteBoardAsyncCall"
16 #include "async_call.h"
17 #include "pasteboard_hilog.h"
18 
19 using namespace OHOS::MiscServices;
20 
21 namespace OHOS::MiscServicesNapi {
AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos)22 AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos) : env_(env)
23 {
24     context_ = new AsyncContext();
25     size_t argc = 6;
26     napi_value self = nullptr;
27     napi_value argv[6] = { nullptr };
28     NAPI_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
29     pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
30     if (pos >= 0 && pos < argc) {
31         napi_valuetype valueType = napi_undefined;
32         napi_typeof(env, argv[pos], &valueType);
33         if (valueType == napi_function) {
34             napi_create_reference(env, argv[pos], 1, &context_->callback);
35         }
36     }
37     napi_status status = (*context)(env, argc, argv, self);
38     if (status != napi_ok) {
39         return;
40     }
41     context_->ctx = std::move(context);
42     napi_create_reference(env, self, 1, &context_->self);
43 }
44 
~AsyncCall()45 AsyncCall::~AsyncCall()
46 {
47     if (context_ == nullptr) {
48         return;
49     }
50 
51     DeleteContext(env_, context_);
52 }
53 
Call(napi_env env, Context::ExecAction exec)54 napi_value AsyncCall::Call(napi_env env, Context::ExecAction exec)
55 {
56     if (context_ == nullptr) {
57         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "context_ is null");
58         return nullptr;
59     }
60     if (context_->ctx == nullptr) {
61         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "context_->ctx is null");
62         return nullptr;
63     }
64 
65     context_->ctx->exec_ = std::move(exec);
66     napi_value promise = nullptr;
67     if (context_->callback == nullptr) {
68         napi_create_promise(env, &context_->defer, &promise);
69     } else {
70         napi_get_undefined(env, &promise);
71     }
72     napi_async_work work = context_->work;
73     napi_value resource = nullptr;
74     napi_create_string_utf8(env, LOG_TAG, NAPI_AUTO_LENGTH, &resource);
75     napi_create_async_work(env, nullptr, resource, AsyncCall::OnExecute, AsyncCall::OnComplete, context_, &work);
76     context_->work = work;
77     context_ = nullptr;
78     napi_queue_async_work_with_qos(env, work, napi_qos_user_initiated);
79     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "async call exec");
80     return promise;
81 }
82 
SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)83 napi_value AsyncCall::SyncCall(napi_env env, AsyncCall::Context::ExecAction exec)
84 {
85     if ((context_ == nullptr) || (context_->ctx == nullptr)) {
86         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "context_ or context_->ctx is null");
87         return nullptr;
88     }
89     context_->ctx->exec_ = std::move(exec);
90     napi_value promise = nullptr;
91     if (context_->callback == nullptr) {
92         napi_create_promise(env, &context_->defer, &promise);
93     } else {
94         napi_get_undefined(env, &promise);
95     }
96     AsyncCall::OnExecute(env, context_);
97     AsyncCall::OnComplete(env, napi_ok, context_);
98     return promise;
99 }
100 
OnExecute(napi_env env, void *data)101 void AsyncCall::OnExecute(napi_env env, void *data)
102 {
103     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
104     context->ctx->Exec();
105 }
106 
OnComplete(napi_env env, napi_status status, void *data)107 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
108 {
109     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
110     napi_value output = nullptr;
111     napi_status runStatus = (*context->ctx)(env, &output);
112     napi_value result[ARG_BUTT] = { 0 };
113     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI,
114         "run the js callback function:status[%{public}d]runStatus[%{public}d]", status, runStatus);
115     if (status == napi_ok && runStatus == napi_ok) {
116         napi_get_undefined(env, &result[ARG_ERROR]);
117         if (output != nullptr) {
118             result[ARG_DATA] = output;
119         } else {
120             PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "AsyncCall::OnComplete output == nullptr");
121             napi_get_undefined(env, &result[ARG_DATA]);
122         }
123     } else {
124         napi_value errCode = nullptr;
125         napi_value message = nullptr;
126         std::string errMsg("async call failed");
127         if (context->ctx->errCode_ != 0) {
128             napi_create_string_utf8(env, std::to_string(context->ctx->errCode_).c_str(), NAPI_AUTO_LENGTH, &errCode);
129         }
130         if (!context->ctx->errMsg_.empty()) {
131             errMsg = context->ctx->errMsg_;
132         }
133         napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
134         napi_create_error(env, errCode, message, &result[ARG_ERROR]);
135         napi_get_undefined(env, &result[ARG_DATA]);
136     }
137     if (context->defer != nullptr) {
138         // promise
139         if (status == napi_ok && runStatus == napi_ok) {
140             napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
141         } else {
142             napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
143         }
144     } else {
145         // callback
146         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "Callback to do!");
147         napi_value callback = nullptr;
148         napi_get_reference_value(env, context->callback, &callback);
149         napi_value returnValue;
150         napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
151     }
152     DeleteContext(env, context);
153 }
DeleteContext(napi_env env, AsyncContext *context)154 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
155 {
156     if (env != nullptr) {
157         napi_delete_reference(env, context->callback);
158         napi_delete_reference(env, context->self);
159         napi_delete_async_work(env, context->work);
160     }
161     delete context;
162 }
163 } // namespace OHOS::MiscServicesNapi