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