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 SCREENLOCK_ASYNC_CALL_H 16 #define SCREENLOCK_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 "screenlock_callback_interface.h" 25 26 namespace OHOS::ScreenLock { 27 28 class AsyncCall final { 29 public: 30 class Context { 31 public: 32 using InputAction = std::function<napi_status(napi_env, size_t, napi_value[], napi_value)>; 33 using OutputAction = std::function<napi_status(napi_env, napi_value *)>; 34 using ExecAction = std::function<void(Context *)>; Context(InputAction input,OutputAction output)35 Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)){}; ~Context()36 virtual ~Context(){}; 37 void SetAction(const InputAction input, const OutputAction output = nullptr) 38 { 39 input_ = input; 40 output_ = output; 41 } SetAction(OutputAction output)42 void SetAction(OutputAction output) 43 { 44 SetAction(nullptr, std::move(output)); 45 } SetErrorInfo(const ErrorInfo & errorInfo)46 void SetErrorInfo(const ErrorInfo &errorInfo) 47 { 48 errorInfo_ = errorInfo; 49 } operator()50 napi_status operator()(const napi_env env, size_t argc, napi_value argv[], napi_value self) 51 { 52 if (input_ == nullptr) { 53 return napi_ok; 54 } 55 if (self == nullptr) { 56 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 57 } 58 return input_(env, argc, argv, self); 59 } operator()60 napi_status operator()(const napi_env env, napi_value *result) 61 { 62 if (output_ == nullptr) { 63 *result = nullptr; 64 return napi_ok; 65 } 66 if (status_ != napi_ok) { 67 return status_; 68 } 69 return output_(env, result); 70 } Exec()71 virtual void Exec() 72 { 73 if (exec_ == nullptr) { 74 return; 75 } 76 exec_(this); 77 }; SetStatus(napi_status status)78 void SetStatus(napi_status status) 79 { 80 status_ = status; 81 } 82 83 protected: 84 friend class AsyncCall; 85 InputAction input_ = nullptr; 86 OutputAction output_ = nullptr; 87 ExecAction exec_ = nullptr; 88 ErrorInfo errorInfo_; 89 napi_status status_ = napi_generic_failure; 90 }; 91 92 // The default AsyncCallback in the parameters is at the end position. 93 static constexpr size_t ASYNC_DEFAULT_POS = -1; 94 AsyncCall(napi_env env, napi_callback_info info, Context *context, size_t pos = ASYNC_DEFAULT_POS); 95 ~AsyncCall(); 96 napi_value Call( 97 const napi_env env, Context::ExecAction exec = nullptr, const std::string &resourceName = "AsyncCall"); 98 napi_value SyncCall(const napi_env env, Context::ExecAction exec = nullptr); 99 static void GenerateBusinessError(napi_env env, const ErrorInfo &errorInfo, napi_value *result); 100 101 private: 102 enum class ARG_INFO { ARG_ERROR, ARG_DATA, ARG_BUTT }; 103 static void OnExecute(const napi_env env, void *data); 104 static void OnComplete(const napi_env env, napi_status status, void *data); 105 struct AsyncContext { 106 ~AsyncContext(); 107 Context *ctx = nullptr; 108 napi_env env = nullptr; 109 napi_ref callback = nullptr; 110 napi_ref self = nullptr; 111 napi_deferred defer = nullptr; 112 napi_async_work work = nullptr; 113 }; 114 AsyncContext *context_ = nullptr; 115 }; 116 } // namespace OHOS::ScreenLock 117 118 #endif // SCREENLOCK_ASYNC_CALL_H 119