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     if (argc > 0) {
31         pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
32     }
33     if (pos >= 0 && pos < argc) {
34         napi_valuetype valueType = napi_undefined;
35         napi_typeof(env, argv[pos], &valueType);
36         if (valueType == napi_function) {
37             napi_create_reference(env, argv[pos], 1, &context_->callback);
38             argc = pos;
39         }
40     }
41     context_->paramStatus = (*context)(env, argc, argv, self, info);
42     context_->ctx = std::move(context);
43     napi_create_reference(env, self, 1, &context_->self);
44 }
45 
~PrintAsyncCall()46 PrintAsyncCall::~PrintAsyncCall()
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 PrintAsyncCall::Call(napi_env env, Context::ExecAction exec)
56 {
57     PRINT_HILOGD("async call exec");
58     if (context_->ctx != nullptr) {
59         context_->ctx->exec_ = std::move(exec);
60     }
61     napi_value promise = nullptr;
62     if (context_->callback == nullptr) {
63         napi_create_promise(env, &context_->defer, &promise);
64     } else {
65         napi_get_undefined(env, &promise);
66     }
67     napi_async_work work = context_->work;
68     napi_value resource = nullptr;
69     napi_create_string_utf8(env, "PrintAsyncCall", NAPI_AUTO_LENGTH, &resource);
70     napi_create_async_work(env, nullptr, resource, PrintAsyncCall::OnExecute,
71         PrintAsyncCall::OnComplete, context_, &work);
72     context_->work = work;
73     context_ = nullptr;
74     napi_queue_async_work(env, work);
75     PRINT_HILOGD("async call exec");
76     return promise;
77 }
78 
SyncCall(napi_env env,PrintAsyncCall::Context::ExecAction exec)79 napi_value PrintAsyncCall::SyncCall(napi_env env, PrintAsyncCall::Context::ExecAction exec)
80 {
81     if (context_ != nullptr && context_->ctx != nullptr) {
82         context_->ctx->exec_ = std::move(exec);
83     }
84     napi_value promise = nullptr;
85     if (context_ != nullptr && context_->callback == nullptr) {
86         napi_create_promise(env, &context_->defer, &promise);
87     } else {
88         napi_get_undefined(env, &promise);
89     }
90     PrintAsyncCall::OnExecute(env, context_);
91     PrintAsyncCall::OnComplete(env, napi_ok, context_);
92     return promise;
93 }
94 
OnExecute(napi_env env,void * data)95 void PrintAsyncCall::OnExecute(napi_env env, void *data)
96 {
97     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
98     if (context->ctx == nullptr) {
99         PRINT_HILOGE("context->ctx is null");
100         return;
101     }
102 
103     PRINT_HILOGD("run the async runnable");
104     if (context->ctx->GetErrorIndex() == E_PRINT_NONE) {
105         context->ctx->Exec();
106     }
107 }
108 
GetErrorIndex(AsyncContext * context)109 uint32_t PrintAsyncCall::GetErrorIndex(AsyncContext *context)
110 {
111     uint32_t errorIndex = E_PRINT_NONE;
112     if (context->paramStatus != napi_ok) {
113         errorIndex = E_PRINT_INVALID_PARAMETER;
114     } else {
115         if (context->ctx == nullptr) {
116             errorIndex = E_PRINT_GENERIC_FAILURE;
117         } else {
118             errorIndex = context->ctx->GetErrorIndex();
119         }
120     }
121     return errorIndex;
122 }
123 
OnComplete(napi_env env,napi_status status,void * data)124 void PrintAsyncCall::OnComplete(napi_env env, napi_status status, void *data)
125 {
126     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
127     if (context->ctx == nullptr || context->ctx->GetErrorIndex() != E_PRINT_NONE) {
128         status = napi_generic_failure;
129     }
130     napi_value output = nullptr;
131     napi_status runStatus = napi_generic_failure;
132     if (context->ctx != nullptr) {
133         runStatus = (*context->ctx)(env, &output);
134     }
135     PRINT_HILOGD("runStatus: [%{public}d], status: [%{public}d]", runStatus, status);
136     napi_value result[ARG_BUTT] = { 0 };
137     if (status == napi_ok && runStatus == napi_ok) {
138         napi_get_undefined(env, &result[ARG_ERROR]);
139         if (output != nullptr) {
140             result[ARG_DATA] = output;
141             PRINT_HILOGD("async call napi_ok.");
142         } else {
143             napi_get_undefined(env, &result[ARG_DATA]);
144         }
145     } else {
146         napi_value message = nullptr;
147         uint32_t errorIndex = GetErrorIndex(context);
148         PRINT_HILOGE("ErrorMessage: [%{public}s], ErrorIndex:[%{public}d]",
149             GetErrorText(errorIndex).c_str(), errorIndex);
150         napi_create_uint32(env, errorIndex, &message);
151         result[ARG_ERROR] = message;
152         napi_get_undefined(env, &result[ARG_DATA]);
153     }
154     if (context->defer != nullptr) {
155         if (status == napi_ok && runStatus == napi_ok) {
156             napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
157         } else {
158             napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
159         }
160     } else {
161         napi_value callback = nullptr;
162         napi_get_reference_value(env, context->callback, &callback);
163         napi_value returnValue;
164         napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
165     }
166     DeleteContext(env, context);
167 }
168 
DeleteContext(napi_env env,AsyncContext * context)169 void PrintAsyncCall::DeleteContext(napi_env env, AsyncContext *context)
170 {
171     if (env != nullptr) {
172         napi_delete_reference(env, context->callback);
173         napi_delete_reference(env, context->self);
174         napi_delete_async_work(env, context->work);
175     }
176     delete context;
177 }
178 
GetErrorText(uint32_t code)179 std::string PrintAsyncCall::GetErrorText(uint32_t code)
180 {
181     std::string errorText = "E_PRINT_NONE";
182 
183     switch (code) {
184         case E_PRINT_NO_PERMISSION:
185             errorText = "E_PRINT_NO_PERMISSION";
186             break;
187 
188         case E_PRINT_INVALID_PARAMETER:
189             errorText = "E_PRINT_INVALID_PARAMETER";
190             break;
191 
192         case E_PRINT_GENERIC_FAILURE:
193             errorText = "E_PRINT_GENERIC_FAILURE";
194             break;
195 
196         case E_PRINT_RPC_FAILURE:
197             errorText = "E_PRINT_RPC_FAILURE";
198             break;
199 
200         case E_PRINT_SERVER_FAILURE:
201             errorText = "E_PRINT_SERVER_FAILURE";
202             break;
203 
204         case E_PRINT_INVALID_EXTENSION:
205             errorText = "E_PRINT_INVALID_EXTENSION";
206             break;
207 
208         case E_PRINT_INVALID_PRINTER:
209             errorText = "E_PRINT_INVALID_PRINTER";
210             break;
211 
212         case E_PRINT_INVALID_PRINTJOB:
213             errorText = "E_PRINT_INVALID_PRINTJOB";
214             break;
215 
216         case E_PRINT_FILE_IO:
217             errorText = "E_PRINT_FILE_IO";
218             break;
219 
220         case E_PRINT_UNKNOWN:
221             errorText = "E_PRINT_UNKNOWN";
222             break;
223 
224         default:
225             break;
226     }
227     return errorText;
228 }
229 } // namespace OHOS::Print
230