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