• 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 #ifndef SCAN_ASYNC_CALL_H
16 #define SCAN_ASYNC_CALL_H
17 
18 #include <functional>
19 #include <memory>
20 #include <unordered_map>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi/native_node_api.h"
25 #include "scan_constant.h"
26 #define TDD_ENABLE 1
27 
28 namespace OHOS::Scan {
29 struct AsyncResult {
30     napi_value error = nullptr;
31     napi_value data = nullptr;
32 };
33 
34 class ScanAsyncCall final {
35 public:
36     class Context {
37     public:
38         using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>;
39         using OutputAction = std::function<napi_status(napi_env, napi_value *)>;
40         using ExecAction = std::function<void(Context *)>;
Context(InputAction input,OutputAction output)41         Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {};
42 
~Context()43         virtual ~Context() {};
44         void SetAction(InputAction input, OutputAction output = nullptr)
45         {
46             input_ = input;
47             output_ = output;
48         }
49 
SetAction(OutputAction output)50         void SetAction(OutputAction output)
51         {
52             SetAction(nullptr, std::move(output));
53         }
54 
operator()55         virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self)
56         {
57             if (input_ == nullptr) {
58                 return napi_ok;
59             }
60             return input_(env, argc, argv, self);
61         }
62 
operator()63         virtual napi_status operator()(napi_env env, napi_value *result)
64         {
65             if (output_ == nullptr) {
66                 *result = nullptr;
67                 return napi_ok;
68             }
69             return output_(env, result);
70         }
71 
Exec()72         virtual void Exec()
73         {
74             if (exec_ == nullptr) {
75                 return;
76             }
77             exec_(this);
78         };
79 
SetErrorIndex(uint32_t errorIndex)80         void SetErrorIndex(uint32_t errorIndex)
81         {
82             errorIndex_ = errorIndex;
83         }
84 
GetErrorIndex()85         uint32_t GetErrorIndex()
86         {
87             return errorIndex_;
88         }
89     #ifndef TDD_ENABLE
90     protected:
91     #endif
92         friend class ScanAsyncCall;
93         InputAction input_ = nullptr;
94         OutputAction output_ = nullptr;
95         ExecAction exec_ = nullptr;
96         uint32_t errorIndex_ = E_SCAN_NONE;
97     };
98 
99     ScanAsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context);
100     ~ScanAsyncCall();
101     napi_value Call(napi_env env, Context::ExecAction exec);
102 
103 #ifndef TDD_ENABLE
104 private:
105 #endif
106     static void OnExecute(napi_env env, void *data);
107     static void OnComplete(napi_env env, napi_status status, void *data);
108     static std::string GetErrorText(uint32_t code);
109     struct AsyncContext {
110         std::shared_ptr<Context> ctx = nullptr;
111         napi_ref self = nullptr;
112         napi_deferred defer = nullptr;
113         napi_async_work work = nullptr;
114         napi_status paramStatus = napi_ok;
115     };
116     static void PrepareSuccessResult(napi_env env, napi_value output, AsyncResult& result);
117     static void PrepareErrorResult(napi_env env, const AsyncContext* context, AsyncResult& result);
118     static void DeleteContext(napi_env env, AsyncContext *context);
119     static void SetErrorText(uint32_t& code, std::string& message);
120 
121     AsyncContext *context_ = nullptr;
122     napi_env env_ = nullptr;
123 };
124 } // namespace OHOS::Scan
125 #endif // REQUEST_ASYNC_CALL_H
126