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 "scan_async_call.h"
17 
18 #include "napi_scan_utils.h"
19 #include "scan_log.h"
20 
21 namespace OHOS::Scan {
22 
23 std::map<uint32_t, std::string> ScanAsyncCall::scanErrorCodeMap = {{E_SCAN_NO_PERMISSION, "E_SCAN_NO_PERMISSION"},
24     {E_SCAN_INVALID_PARAMETER, "E_SCAN_INVALID_PARAMETER"},
25     {E_SCAN_GENERIC_FAILURE, "E_SCAN_GENERIC_FAILURE"},
26     {E_SCAN_RPC_FAILURE, "E_SCAN_RPC_FAILURE"},
27     {E_SCAN_SERVER_FAILURE, "E_SCAN_SERVER_FAILURE"},
28     {E_SCAN_GOOD, "E_SCAN_GOOD"},
29     {E_SCAN_UNSUPPORTED, "E_SCAN_UNSUPPORTED"},
30     {E_SCAN_CANCELLED, "E_SCAN_CANCELLED"},
31     {E_SCAN_DEVICE_BUSY, "E_SCAN_DEVICE_BUSY"},
32     {E_SCAN_INVAL, "E_SCAN_INVAL"},
33     {E_SCAN_EOF, "E_SCAN_EOF"},
34     {E_SCAN_JAMMED, "E_SCAN_JAMMED"},
35     {E_SCAN_NO_DOCS, "E_SCAN_NO_DOCS"},
36     {E_SCAN_COVER_OPEN, "E_SCAN_COVER_OPEN"},
37     {E_SCAN_IO_ERROR, "E_SCAN_IO_ERROR"},
38     {E_SCAN_NO_MEM, "E_SCAN_NO_MEM"},
39     {E_SCAN_ACCESS_DENIED, "E_SCAN_ACCESS_DENIED"}};
40 
ScanAsyncCall(napi_env env,napi_callback_info info,std::shared_ptr<Context> context,size_t pos)41 ScanAsyncCall::ScanAsyncCall(napi_env env, napi_callback_info info,
42     std::shared_ptr<Context> context, size_t pos) : env_(env)
43 {
44     context_ = new AsyncContext();
45     size_t argc = NapiScanUtils::MAX_ARGC;
46     napi_value self = nullptr;
47     napi_value argv[NapiScanUtils::MAX_ARGC] = { nullptr };
48     SCAN_CALL_RETURN_VOID(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr));
49     if (argc > 0) {
50         pos = ((pos == ASYNC_DEFAULT_POS) ? (argc - 1) : pos);
51     }
52     if (pos >= 0 && pos < argc) {
53         napi_valuetype valueType = napi_undefined;
54         napi_typeof(env, argv[pos], &valueType);
55         if (valueType == napi_function) {
56             napi_create_reference(env, argv[pos], 1, &context_->callback);
57             argc = pos;
58         }
59     }
60     context_->paramStatus = (*context)(env, argc, argv, self);
61     context_->ctx = std::move(context);
62     napi_create_reference(env, self, 1, &context_->self);
63 }
64 
~ScanAsyncCall()65 ScanAsyncCall::~ScanAsyncCall()
66 {
67     if (context_ == nullptr) {
68         return;
69     }
70 
71     DeleteContext(env_, context_);
72 }
73 
Call(napi_env env,Context::ExecAction exec)74 napi_value ScanAsyncCall::Call(napi_env env, Context::ExecAction exec)
75 {
76     SCAN_HILOGD("async call exec");
77     if (context_ != nullptr && context_->ctx != nullptr) {
78         context_->ctx->exec_ = std::move(exec);
79     } else {
80         SCAN_HILOGE("context_ is null or context->ctx is null");
81         return nullptr;
82     }
83     napi_value promise = nullptr;
84     if (context_->callback == nullptr) {
85         napi_create_promise(env, &context_->defer, &promise);
86     } else {
87         napi_get_undefined(env, &promise);
88     }
89     napi_async_work work = context_->work;
90     napi_value resource = nullptr;
91     napi_create_string_utf8(env, "ScanAsyncCall", NAPI_AUTO_LENGTH, &resource);
92     napi_create_async_work(env, nullptr, resource, ScanAsyncCall::OnExecute,
93         ScanAsyncCall::OnComplete, context_, &work);
94     context_->work = work;
95     context_ = nullptr;
96     napi_queue_async_work(env, work);
97     SCAN_HILOGD("async call exec");
98     return promise;
99 }
100 
SyncCall(napi_env env,ScanAsyncCall::Context::ExecAction exec)101 napi_value ScanAsyncCall::SyncCall(napi_env env, ScanAsyncCall::Context::ExecAction exec)
102 {
103     if (context_ != nullptr && context_->ctx != nullptr) {
104         context_->ctx->exec_ = std::move(exec);
105     } else {
106         SCAN_HILOGE("context_ is null or context->ctx is null");
107         return nullptr;
108     }
109     napi_value promise = nullptr;
110     if (context_->callback == nullptr) {
111         napi_create_promise(env, &context_->defer, &promise);
112     } else {
113         napi_get_undefined(env, &promise);
114     }
115     ScanAsyncCall::OnExecute(env, context_);
116     ScanAsyncCall::OnComplete(env, napi_ok, context_);
117     return promise;
118 }
119 
OnExecute(napi_env env,void * data)120 void ScanAsyncCall::OnExecute(napi_env env, void *data)
121 {
122     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
123     if (context->ctx == nullptr) {
124         SCAN_HILOGE("context->ctx is null");
125         return;
126     }
127 
128     SCAN_HILOGD("run the async runnable");
129     if (context->ctx->GetErrorIndex() == E_SCAN_NONE) {
130         context->ctx->Exec();
131     }
132 }
133 
OnComplete(napi_env env,napi_status status,void * data)134 void ScanAsyncCall::OnComplete(napi_env env, napi_status status, void *data)
135 {
136     AsyncContext *context = reinterpret_cast<AsyncContext *>(data);
137     if (context->ctx == nullptr || context->ctx->GetErrorIndex() != E_SCAN_NONE) {
138         status = napi_generic_failure;
139     }
140     napi_value output = nullptr;
141     napi_status runStatus = napi_generic_failure;
142     if (context->ctx != nullptr) {
143         runStatus = (*context->ctx)(env, &output);
144     }
145     SCAN_HILOGD("runStatus: [%{public}d], status: [%{public}d]", runStatus, status);
146     napi_value result[ARG_BUTT] = { 0 };
147     if (status == napi_ok && runStatus == napi_ok) {
148         napi_get_undefined(env, &result[ARG_ERROR]);
149         if (output != nullptr) {
150             result[ARG_DATA] = output;
151             SCAN_HILOGD("async call napi_ok.");
152         } else {
153             napi_get_undefined(env, &result[ARG_DATA]);
154         }
155     } else {
156         napi_value message = nullptr;
157         uint32_t errorIndex = E_SCAN_NONE;
158         if (context->paramStatus != napi_ok) {
159             errorIndex = E_SCAN_INVALID_PARAMETER;
160         } else if (context->ctx != nullptr) {
161             errorIndex = context->ctx->GetErrorIndex();
162         } else {
163             errorIndex = E_SCAN_GENERIC_FAILURE;
164         }
165         SCAN_HILOGE("ErrorMessage: [%{public}s], ErrorIndex:[%{public}d]",
166             GetErrorText(errorIndex).c_str(), errorIndex);
167         napi_create_uint32(env, errorIndex, &message);
168         result[ARG_ERROR] = message;
169         napi_get_undefined(env, &result[ARG_DATA]);
170     }
171     if (context->defer != nullptr) {
172         if (status == napi_ok && runStatus == napi_ok) {
173             napi_resolve_deferred(env, context->defer, result[ARG_DATA]);
174         } else {
175             napi_reject_deferred(env, context->defer, result[ARG_ERROR]);
176         }
177     } else {
178         napi_value callback = nullptr;
179         napi_get_reference_value(env, context->callback, &callback);
180         napi_value returnValue;
181         napi_call_function(env, nullptr, callback, ARG_BUTT, result, &returnValue);
182     }
183     DeleteContext(env, context);
184 }
185 
DeleteContext(napi_env env,AsyncContext * context)186 void ScanAsyncCall::DeleteContext(napi_env env, AsyncContext *context)
187 {
188     if (env != nullptr) {
189         napi_delete_reference(env, context->callback);
190         napi_delete_reference(env, context->self);
191         napi_delete_async_work(env, context->work);
192     }
193     delete context;
194 }
195 
GetErrorText(uint32_t code)196 std::string ScanAsyncCall::GetErrorText(uint32_t code)
197 {
198     SCAN_HILOGD("GetErrorText from map start");
199     auto it = scanErrorCodeMap.find(code);
200     if (it != scanErrorCodeMap.end()) {
201         return it->second;
202     } else {
203         SCAN_HILOGD("ErrorText not found");
204         return "E_SCAN_NONE";
205     }
206 }
207 } // namespace OHOS::Scan
208