• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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             return input_(env, argc, argv, self);
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             return output_(env, result);
69         }
70 
Exec()71         virtual void Exec()
72         {
73             if (exec_ == nullptr) {
74                 return;
75             }
76             exec_(this);
77         };
78 
79     protected:
80         friend class AsyncCall;
81         InputAction input_ = nullptr;
82         OutputAction output_ = nullptr;
83         ExecAction exec_ = nullptr;
84         int32_t errCode_ = 0;
85         std::string errMsg_;
86     };
87 
88     // The default AsyncCallback in the parameters is at the end position.
89     static constexpr size_t ASYNC_DEFAULT_POS = -1;
90     AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS);
91     ~AsyncCall();
92     napi_value Call(napi_env env, Context::ExecAction exec = nullptr);
93     napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr);
94 
95 private:
96     enum arg : int { ARG_ERROR, ARG_DATA, ARG_BUTT };
97     static void OnExecute(napi_env env, void *data);
98     static void OnComplete(napi_env env, napi_status status, void *data);
99     struct AsyncContext {
100         std::shared_ptr<Context> ctx = nullptr;
101         napi_ref callback = nullptr;
102         napi_ref self = nullptr;
103         napi_deferred defer = nullptr;
104         napi_async_work work = nullptr;
105     };
106     static void DeleteContext(napi_env env, AsyncContext *context);
107 
108     AsyncContext *context_ = nullptr;
109     napi_env env_ = nullptr;
110 };
111 } // namespace OHOS::MiscServicesNapi
112 
113 #endif // PASTEBOARD_ASYNC_CALL_H
114