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