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 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "async call exec");
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(env, work);
80 PASTEBOARD_HILOGI(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 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "run the async runnable");
105 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
106 context->ctx->Exec();
107 }
108
OnComplete(napi_env env,napi_status status,void * data)109 void AsyncCall::OnComplete(napi_env env, napi_status status, void *data)
110 {
111 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "run the js callback function");
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_HILOGI(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 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "AsyncCall::OnComplete output != nullptr");
122 result[ARG_DATA] = output;
123 } else {
124 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "AsyncCall::OnComplete output == nullptr");
125 napi_get_undefined(env, &result[ARG_DATA]);
126 }
127 } else {
128 napi_value errCode = nullptr;
129 napi_value message = nullptr;
130 std::string errMsg("async call failed");
131 if (context->ctx->errCode_ != 0) {
132 napi_create_string_utf8(env, std::to_string(context->ctx->errCode_).c_str(), NAPI_AUTO_LENGTH, &errCode);
133 }
134 if (!context->ctx->errMsg_.empty()) {
135 errMsg = context->ctx->errMsg_;
136 }
137 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
138 napi_create_error(env, errCode, message, &result[ARG_ERROR]);
139 napi_get_undefined(env, &result[ARG_DATA]);
140 }
141 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI,
142 "run the js callback function:(context->defer != nullptr)?[%{public}d]", context->defer != nullptr);
143 if (context->defer != nullptr) {
144 // promise
145 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "Promise to do!");
146 if (status == napi_ok && runStatus == napi_ok) {
147 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
148 } else {
149 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
150 }
151 } else {
152 // callback
153 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_JS_NAPI, "Callback to do!");
154 napi_value callback = nullptr;
155 napi_get_reference_value(env, context->callback, &callback);
156 napi_value returnValue;
157 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
158 }
159 DeleteContext(env, context);
160 }
DeleteContext(napi_env env,AsyncContext * context)161 void AsyncCall::DeleteContext(napi_env env, AsyncContext *context)
162 {
163 if (env != nullptr) {
164 napi_delete_reference(env, context->callback);
165 napi_delete_reference(env, context->self);
166 napi_delete_async_work(env, context->work);
167 }
168 delete context;
169 }
170 } // namespace OHOS::MiscServicesNapi