1 /*
2 * Copyright (c) 2022 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 "print_async_call.h"
17
18 #include "napi_print_utils.h"
19 #include "print_log.h"
20
21 namespace OHOS::Print {
PrintAsyncCall(napi_env env,napi_callback_info info,std::shared_ptr<Context> context,size_t pos)22 PrintAsyncCall::PrintAsyncCall(napi_env env, napi_callback_info info,
23 std::shared_ptr<Context> context, size_t pos) : env_(env)
24 {
25 context_ = new AsyncContext();
26 size_t argc = NapiPrintUtils::MAX_ARGC;
27 napi_value self = nullptr;
28 napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
29 PRINT_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 argc = pos;
37 }
38 }
39 context_->paramStatus = (*context)(env, argc, argv, self);
40 context_->ctx = std::move(context);
41 napi_create_reference(env, self, 1, &context_->self);
42 }
43
~PrintAsyncCall()44 PrintAsyncCall::~PrintAsyncCall()
45 {
46 if (context_ == nullptr) {
47 return;
48 }
49
50 DeleteContext(env_, context_);
51 }
52
Call(napi_env env,Context::ExecAction exec)53 napi_value PrintAsyncCall::Call(napi_env env, Context::ExecAction exec)
54 {
55 PRINT_HILOGD("async call exec");
56 if (context_->ctx != nullptr) {
57 context_->ctx->exec_ = std::move(exec);
58 }
59 napi_value promise = nullptr;
60 if (context_->callback == nullptr) {
61 napi_create_promise(env, &context_->defer, &promise);
62 } else {
63 napi_get_undefined(env, &promise);
64 }
65 napi_async_work work = context_->work;
66 napi_value resource = nullptr;
67 napi_create_string_utf8(env, "PrintAsyncCall", NAPI_AUTO_LENGTH, &resource);
68 napi_create_async_work(env, nullptr, resource, PrintAsyncCall::OnExecute,
69 PrintAsyncCall::OnComplete, context_, &work);
70 context_->work = work;
71 context_ = nullptr;
72 napi_queue_async_work(env, work);
73 PRINT_HILOGD("async call exec");
74 return promise;
75 }
76
SyncCall(napi_env env,PrintAsyncCall::Context::ExecAction exec)77 napi_value PrintAsyncCall::SyncCall(napi_env env, PrintAsyncCall::Context::ExecAction exec)
78 {
79 if (context_ != nullptr && context_->ctx != nullptr) {
80 context_->ctx->exec_ = std::move(exec);
81 }
82 napi_value promise = nullptr;
83 if (context_->callback == nullptr) {
84 napi_create_promise(env, &context_->defer, &promise);
85 } else {
86 napi_get_undefined(env, &promise);
87 }
88 PrintAsyncCall::OnExecute(env, context_);
89 PrintAsyncCall::OnComplete(env, napi_ok, context_);
90 return promise;
91 }
92
OnExecute(napi_env env,void * data)93 void PrintAsyncCall::OnExecute(napi_env env, void *data)
94 {
95 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
96 if (context->ctx == nullptr) {
97 PRINT_HILOGE("context->ctx is null");
98 return;
99 }
100
101 PRINT_HILOGD("run the async runnable");
102 if (context->ctx->GetErrorIndex() == E_PRINT_NONE) {
103 context->ctx->Exec();
104 }
105 }
106
OnComplete(napi_env env,napi_status status,void * data)107 void PrintAsyncCall::OnComplete(napi_env env, napi_status status, void *data)
108 {
109 AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
110 if (context->ctx == nullptr || context->ctx->GetErrorIndex() != E_PRINT_NONE) {
111 status = napi_generic_failure;
112 }
113 napi_value output = nullptr;
114 napi_status runStatus = napi_generic_failure;
115 if (context->ctx != nullptr) {
116 runStatus = (*context->ctx)(env, &output);
117 }
118 PRINT_HILOGD("runStatus: [%{public}d], status: [%{public}d]", runStatus, status);
119 napi_value result[ARG_BUTT] = { 0 };
120 if (status == napi_ok && runStatus == napi_ok) {
121 napi_get_undefined(env, &result[ARG_ERROR]);
122 if (output != nullptr) {
123 result[ARG_DATA] = output;
124 PRINT_HILOGD("async call napi_ok.");
125 } else {
126 napi_get_undefined(env, &result[ARG_DATA]);
127 }
128 } else {
129 napi_value message = nullptr;
130 uint32_t errorIndex = E_PRINT_NONE;
131 if (context->paramStatus != napi_ok) {
132 errorIndex = E_PRINT_INVALID_PARAMETER;
133 } else {
134 errorIndex = context->ctx->GetErrorIndex();
135 }
136 PRINT_HILOGE("ErrorMessage: [%{public}s], ErrorIndex:[%{public}d]",
137 GetErrorText(errorIndex).c_str(), errorIndex);
138 napi_create_uint32(env, errorIndex, &message);
139 result[ARG_ERROR] = message;
140 napi_get_undefined(env, &result[ARG_DATA]);
141 }
142 if (context->defer != nullptr) {
143 if (status == napi_ok && runStatus == napi_ok) {
144 napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
145 } else {
146 napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
147 }
148 } else {
149 napi_value callback = nullptr;
150 napi_get_reference_value(env, context->callback, &callback);
151 napi_value returnValue;
152 napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
153 }
154 DeleteContext(env, context);
155 }
156
DeleteContext(napi_env env,AsyncContext * context)157 void PrintAsyncCall::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
GetErrorText(uint32_t code)167 std::string PrintAsyncCall::GetErrorText(uint32_t code)
168 {
169 std::string errorText = "E_PRINT_NONE";
170
171 switch (code) {
172 case E_PRINT_NO_PERMISSION:
173 errorText = "E_PRINT_NO_PERMISSION";
174 break;
175
176 case E_PRINT_INVALID_PARAMETER:
177 errorText = "E_PRINT_INVALID_PARAMETER";
178 break;
179
180 case E_PRINT_GENERIC_FAILURE:
181 errorText = "E_PRINT_GENERIC_FAILURE";
182 break;
183
184 case E_PRINT_RPC_FAILURE:
185 errorText = "E_PRINT_RPC_FAILURE";
186 break;
187
188 case E_PRINT_SERVER_FAILURE:
189 errorText = "E_PRINT_SERVER_FAILURE";
190 break;
191
192 case E_PRINT_INVALID_EXTENSION:
193 errorText = "E_PRINT_INVALID_EXTENSION";
194 break;
195
196 case E_PRINT_INVALID_PRINTER:
197 errorText = "E_PRINT_INVALID_PRINTER";
198 break;
199
200 case E_PRINT_INVALID_PRINTJOB:
201 errorText = "E_PRINT_INVALID_PRINTJOB";
202 break;
203
204 case E_PRINT_FILE_IO:
205 errorText = "E_PRINT_FILE_IO";
206 break;
207
208 case E_PRINT_UNKNOWN:
209 errorText = "E_PRINT_UNKNOWN";
210 break;
211
212 default:
213 break;
214 }
215 return errorText;
216 }
217 } // namespace OHOS::Print
218