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