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