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 DATASHARE_ASYNC_CALL_H
16 #define DATASHARE_ASYNC_CALL_H
17 
18 #include <functional>
19 #include <memory>
20 #include "datashare_js_utils.h"
21 #include "datashare_error_impl.h"
22 #include "napi/native_common.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS::DataShare {
27 class AsyncCall final {
28 public:
29     class Context {
30     public:
31         std::shared_ptr<Error> error;
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(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 
operator()48         virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self)
49         {
50             if (input_ == nullptr) {
51                 return napi_ok;
52             }
53             return input_(env, argc, argv, self);
54         }
55 
operator()56         virtual napi_status operator()(napi_env env, napi_value *result)
57         {
58             if (output_ == nullptr) {
59                 *result = nullptr;
60                 return napi_ok;
61             }
62             return output_(env, result);
63         }
64 
Exec()65         virtual void Exec()
66         {
67             if (exec_ == nullptr) {
68                 return;
69             }
70             exec_(this);
71         };
72     protected:
73         friend class AsyncCall;
74         InputAction input_ = nullptr;
75         OutputAction output_ = nullptr;
76         ExecAction exec_ = nullptr;
77     };
78 
79     // The default AsyncCallback in the parameters is at the end position.
80     static constexpr size_t ASYNC_DEFAULT_POS = -1;
81     AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context);
82     ~AsyncCall();
83     napi_value Call(napi_env env, Context::ExecAction exec = nullptr);
84     napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr);
85 private:
86     enum {
87         ARG_ERROR,
88         ARG_DATA,
89         ARG_BUTT
90     };
91     static void OnExecute(napi_env env, void *data);
92     static void OnComplete(napi_env env, napi_status status, void *data);
93     struct AsyncContext {
94         std::shared_ptr<Context> ctx = nullptr;
95         napi_ref callback = nullptr;
96         napi_ref self = nullptr;
97         napi_deferred defer = nullptr;
98         napi_async_work work = nullptr;
99     };
100     static void DeleteContext(napi_env env, AsyncContext *context);
101 
102     AsyncContext *context_ = nullptr;
103     napi_env env_ = nullptr;
104 };
105 }
106 
107 #endif // DATASHARE_ASYNC_CALL_H
108