• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         uint32_t errorIndex = GetErrorIndex(context);
147         PRINT_HILOGE("ErrorMessage: [%{public}s], ErrorIndex:[%{public}d]",
148             GetErrorText(errorIndex).c_str(), errorIndex);
149         result[ARG_ERROR] = NapiPrintUtils::CreateJsError(env, errorIndex);
150         napi_get_undefined(env, &result[ARG_DATA]);
151     }
152     if (context->defer != nullptr) {
153         if (status == napi_ok && runStatus == napi_ok) {
154             napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
155         } else {
156             napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
157         }
158     } else {
159         napi_value callback = nullptr;
160         napi_get_reference_value(env, context->callback, &callback);
161         napi_value returnValue;
162         napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
163     }
164     DeleteContext(env, context);
165 }
166 
DeleteContext(napi_env env,AsyncContext * context)167 void PrintAsyncCall::DeleteContext(napi_env env, AsyncContext *context)
168 {
169     if (env != nullptr) {
170         napi_delete_reference(env, context->callback);
171         napi_delete_reference(env, context->self);
172         napi_delete_async_work(env, context->work);
173     }
174     delete context;
175 }
176 
GetErrorText(uint32_t code)177 std::string PrintAsyncCall::GetErrorText(uint32_t code)
178 {
179     std::string errorText = "E_PRINT_NONE";
180 
181     switch (code) {
182         case E_PRINT_NO_PERMISSION:
183             errorText = "E_PRINT_NO_PERMISSION";
184             break;
185 
186         case E_PRINT_INVALID_PARAMETER:
187             errorText = "E_PRINT_INVALID_PARAMETER";
188             break;
189 
190         case E_PRINT_GENERIC_FAILURE:
191             errorText = "E_PRINT_GENERIC_FAILURE";
192             break;
193 
194         case E_PRINT_RPC_FAILURE:
195             errorText = "E_PRINT_RPC_FAILURE";
196             break;
197 
198         case E_PRINT_SERVER_FAILURE:
199             errorText = "E_PRINT_SERVER_FAILURE";
200             break;
201 
202         case E_PRINT_INVALID_EXTENSION:
203             errorText = "E_PRINT_INVALID_EXTENSION";
204             break;
205 
206         case E_PRINT_INVALID_PRINTER:
207             errorText = "E_PRINT_INVALID_PRINTER";
208             break;
209 
210         case E_PRINT_INVALID_PRINTJOB:
211             errorText = "E_PRINT_INVALID_PRINTJOB";
212             break;
213 
214         case E_PRINT_FILE_IO:
215             errorText = "E_PRINT_FILE_IO";
216             break;
217 
218         case E_PRINT_UNKNOWN:
219             errorText = "E_PRINT_UNKNOWN";
220             break;
221 
222         default:
223             break;
224     }
225     return errorText;
226 }
227 } // namespace OHOS::Print
228