1 /* 2 * Copyright (c) 2021 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 ASYN_CALL_H 16 #define ASYN_CALL_H 17 18 #include "input_method_info.h" 19 #include "js_utils.h" 20 #include "napi/native_api.h" 21 #include "napi/native_common.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS { 25 namespace MiscServices { 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 SetErrorCode(int32_t errorCode)41 void SetErrorCode(int32_t errorCode) 42 { 43 errorCode_ = errorCode; 44 } 45 SetState(const napi_status & status)46 void SetState(const napi_status &status) 47 { 48 status_ = status; 49 } 50 SetAction(OutputAction output)51 void SetAction(OutputAction output) 52 { 53 SetAction(nullptr, std::move(output)); 54 } 55 operator()56 virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) 57 { 58 if (input_ == nullptr) { 59 return napi_ok; 60 } 61 auto ret = input_(env, argc, argv, self); 62 input_ = nullptr; 63 return ret; 64 } 65 operator()66 virtual napi_status operator()(napi_env env, napi_value *result) 67 { 68 if (output_ == nullptr) { 69 *result = nullptr; 70 return napi_ok; 71 } 72 auto ret = output_(env, result); 73 output_ = nullptr; 74 return ret; 75 } 76 Exec()77 virtual void Exec() 78 { 79 if (exec_ == nullptr) { 80 return; 81 } 82 exec_(this); 83 exec_ = nullptr; 84 }; 85 86 protected: 87 friend class AsyncCall; 88 InputAction input_ = nullptr; 89 OutputAction output_ = nullptr; 90 ExecAction exec_ = nullptr; 91 napi_status status_ = napi_generic_failure; 92 int32_t errorCode_ = 0; 93 }; 94 static constexpr size_t ARGC_MAX = 6; 95 static constexpr size_t ASYNC_DEFAULT_POS = -1; 96 AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS); 97 ~AsyncCall(); 98 napi_value Call(napi_env env, Context::ExecAction exec = nullptr); 99 napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr); 100 101 private: 102 enum arg : int { 103 ARG_ERROR, 104 ARG_DATA, 105 ARG_BUTT 106 }; 107 static void OnExecute(napi_env env, void *data); 108 static void OnComplete(napi_env env, napi_status status, void *data); 109 struct AsyncContext { 110 std::shared_ptr<Context> ctx = nullptr; 111 napi_ref callback = nullptr; 112 napi_ref self = nullptr; 113 napi_deferred defer = nullptr; 114 napi_async_work work = nullptr; 115 }; 116 static void DeleteContext(napi_env env, AsyncContext *context); 117 AsyncContext *context_ = nullptr; 118 napi_env env_ = nullptr; 119 }; 120 } // namespace MiscServices 121 } // namespace OHOS 122 #endif // ASYNC_CALL_H