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 <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 30 class ScanAsyncCall final { 31 public: 32 class Context { 33 public: 34 using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>; 35 using OutputAction = std::function<napi_status(napi_env, napi_value *)>; 36 using ExecAction = std::function<void(Context *)>; Context(InputAction input,OutputAction output)37 Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {}; 38 ~Context()39 virtual ~Context() {}; 40 void SetAction(InputAction input, OutputAction output = nullptr) 41 { 42 input_ = input; 43 output_ = output; 44 } 45 SetAction(OutputAction output)46 void SetAction(OutputAction output) 47 { 48 SetAction(nullptr, std::move(output)); 49 } 50 operator()51 virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) 52 { 53 if (input_ == nullptr) { 54 return napi_ok; 55 } 56 return input_(env, argc, argv, self); 57 } 58 operator()59 virtual napi_status operator()(napi_env env, napi_value *result) 60 { 61 if (output_ == nullptr) { 62 *result = nullptr; 63 return napi_ok; 64 } 65 return output_(env, result); 66 } 67 Exec()68 virtual void Exec() 69 { 70 if (exec_ == nullptr) { 71 return; 72 } 73 exec_(this); 74 }; 75 SetErrorIndex(uint32_t errorIndex)76 void SetErrorIndex(uint32_t errorIndex) 77 { 78 errorIndex_ = errorIndex; 79 } 80 GetErrorIndex()81 uint32_t GetErrorIndex() 82 { 83 return errorIndex_; 84 } 85 #ifndef TDD_ENABLE 86 protected: 87 #endif 88 friend class ScanAsyncCall; 89 InputAction input_ = nullptr; 90 OutputAction output_ = nullptr; 91 ExecAction exec_ = nullptr; 92 uint32_t errorIndex_ = E_SCAN_NONE; 93 }; 94 95 // The default AsyncCallback in the parameters is at the end position. 96 static constexpr size_t ASYNC_DEFAULT_POS = -1; 97 ScanAsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, 98 size_t pos = ASYNC_DEFAULT_POS); 99 ~ScanAsyncCall(); 100 napi_value Call(napi_env env, Context::ExecAction exec = nullptr); 101 napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr); 102 103 #ifndef TDD_ENABLE 104 private: 105 #endif 106 enum { ARG_ERROR, ARG_DATA, ARG_BUTT }; 107 static void OnExecute(napi_env env, void *data); 108 static void OnComplete(napi_env env, napi_status status, void *data); 109 static std::string GetErrorText(uint32_t code); 110 struct AsyncContext { 111 std::shared_ptr<Context> ctx = nullptr; 112 napi_ref callback = nullptr; 113 napi_ref self = nullptr; 114 napi_deferred defer = nullptr; 115 napi_async_work work = nullptr; 116 napi_status paramStatus = napi_ok; 117 }; 118 static void DeleteContext(napi_env env, AsyncContext *context); 119 static std::map<uint32_t, std::string> scanErrorCodeMap; 120 121 AsyncContext *context_ = nullptr; 122 napi_env env_ = nullptr; 123 }; 124 } // namespace OHOS::Scan 125 #endif // REQUEST_ASYNC_CALL_H 126