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 WALLPAPER_ASYNC_CALL_H 16 #define WALLPAPER_ASYNC_CALL_H 17 18 #include <functional> 19 #include <memory> 20 #include "wallpaper_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::WallpaperNAPI { 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 SetErrInfo(int32_t errCode,std::string errMsg)46 void SetErrInfo(int32_t errCode, std::string errMsg) 47 { 48 errCode_ = errCode; 49 errMsg_ = errMsg; 50 } 51 operator()52 virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) 53 { 54 if (input_ == nullptr) { 55 return napi_ok; 56 } 57 auto ret = input_(env, argc, argv, self); 58 input_ = nullptr; 59 return ret; 60 } 61 operator()62 virtual napi_status operator()(napi_env env, napi_value *result) 63 { 64 if (output_ == nullptr) { 65 *result = nullptr; 66 return napi_ok; 67 } 68 auto ret = output_(env, result); 69 output_ = nullptr; 70 return ret; 71 } 72 Exec()73 virtual void Exec() 74 { 75 if (exec_ == nullptr) { 76 return; 77 } 78 exec_(this); 79 exec_ = nullptr; 80 }; 81 protected: 82 friend class AsyncCall; 83 InputAction input_ = nullptr; 84 OutputAction output_ = nullptr; 85 ExecAction exec_ = nullptr; 86 int32_t errCode_ = 0; 87 std::string errMsg_; 88 }; 89 90 // The default AsyncCallback in the parameters is at the end position. 91 static constexpr size_t ASYNC_DEFAULT_POS = -1; 92 AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS); 93 ~AsyncCall(); 94 napi_value Call(napi_env env, Context::ExecAction exec = nullptr); 95 napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr); 96 97 enum ErrorCode : int32_t { 98 OK = napi_ok, 99 NOT_SUPPORT = 801 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 118 AsyncContext *context_ = nullptr; 119 napi_env env_ = nullptr; 120 }; 121 } 122 123 #endif // WALLPAPER_ASYNC_CALL_H 124