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 REQUEST_ASYNC_CALL_H 16 #define REQUEST_ASYNC_CALL_H 17 18 #include <functional> 19 #include <memory> 20 #include "js_util.h" 21 #include "napi/native_common.h" 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 25 namespace OHOS::Request::UploadNapi { 26 class AsyncCall final { 27 public: 28 class Context { 29 public: 30 using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>; 31 using OutputAction = std::function<napi_status(napi_env, napi_value *)>; 32 using ExecAction = std::function<void(Context *)>; Context(InputAction input,OutputAction output)33 Context(InputAction input, OutputAction output): input_(std::move(input)), output_(std::move(output)) {}; ~Context()34 virtual ~Context() {}; 35 void SetAction(InputAction input, OutputAction output = nullptr) 36 { 37 input_ = input; 38 output_ = output; 39 } 40 SetAction(OutputAction output)41 void SetAction(OutputAction output) 42 { 43 SetAction(nullptr, std::move(output)); 44 } 45 operator()46 virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) 47 { 48 if (input_ == nullptr) { 49 return napi_ok; 50 } 51 napi_status ret = input_(env, argc, argv, self); 52 input_ = nullptr; 53 return ret; 54 } 55 operator()56 virtual napi_status operator()(napi_env env, napi_value *result) 57 { 58 if (output_ == nullptr) { 59 *result = nullptr; 60 return napi_ok; 61 } 62 napi_status ret = output_(env, result); 63 output_ = nullptr; 64 return ret; 65 } 66 Exec()67 virtual void Exec() 68 { 69 if (exec_ == nullptr) { 70 return; 71 } 72 exec_(this); 73 exec_ = nullptr; 74 }; 75 protected: 76 friend class AsyncCall; 77 InputAction input_ = nullptr; 78 OutputAction output_ = nullptr; 79 ExecAction exec_ = nullptr; 80 }; 81 82 AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context); 83 ~AsyncCall(); 84 napi_value Call(napi_env env, Context::ExecAction exec = nullptr); 85 napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr); 86 private: 87 enum { 88 ARG_ERROR, 89 ARG_DATA, 90 ARG_BUTT 91 }; 92 static void OnExecute(napi_env env, void *data); 93 static void OnComplete(napi_env env, napi_status status, void *data); 94 struct AsyncContext { 95 std::shared_ptr<Context> ctx = nullptr; 96 napi_ref callback = nullptr; 97 napi_ref self = nullptr; 98 napi_deferred defer = nullptr; 99 napi_async_work work = nullptr; 100 }; 101 static void DeleteContext(napi_env env, AsyncContext *context); 102 103 AsyncContext *context_ = nullptr; 104 napi_env env_ = nullptr; 105 }; 106 } 107 108 #endif // REQUEST_ASYNC_CALL_H 109