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 OHOS_NAPI_QUEUE_H
16 #define OHOS_NAPI_QUEUE_H
17 #include <functional>
18 #include <memory>
19 #include <string>
20 
21 #include "log_print.h"
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24 #include "napi/native_node_api.h"
25 
26 namespace OHOS::DistributedData {
27 constexpr size_t ARGC_MAX = 6;
28 using NapiCbInfoParser = std::function<void(size_t argc, napi_value* argv)>;
29 using NapiAsyncExecute = std::function<void(void)>;
30 using NapiAsyncComplete = std::function<void(napi_value&)>;
31 
32 struct ContextBase {
33     virtual ~ContextBase();
34     void GetCbInfo(
35         napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser(), bool sync = false);
36 
37     inline void GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser())
38     {
39         /* sync = true, means no callback, not AsyncWork. */
40         GetCbInfo(env, info, parse, true);
41     }
42 
43     napi_env env = nullptr;
44     napi_value output = nullptr;
45     napi_status status = napi_invalid_arg;
46     std::string error;
47 
48     napi_value self = nullptr;
49     void* native = nullptr;
50 
51 private:
52     napi_ref callbackRef = nullptr;
53     napi_ref selfRef = nullptr;
54     friend class NapiQueue;
55 };
56 
57 /* check condition related to argc/argv, return and logging. */
58 #define CHECK_ARGS_RETURN_VOID(ctxt, condition, message)               \
59     do {                                                               \
60         if (!(condition)) {                                            \
61             (ctxt)->status = napi_invalid_arg;                         \
62             (ctxt)->error = std::string(message);                      \
63             ZLOGE("test (" #condition ") failed: " message);           \
64             return;                                                    \
65         }                                                              \
66     } while (0)
67 
68 #define CHECK_STATUS_RETURN_VOID(ctxt, message)                        \
69     do {                                                               \
70         if ((ctxt)->status != napi_ok) {                               \
71             (ctxt)->error = std::string(message);                      \
72             ZLOGE("test (ctxt->status == napi_ok) failed: " message);  \
73             return;                                                    \
74         }                                                              \
75     } while (0)
76 
77 /* check condition, return and logging if condition not true. */
78 #define CHECK_RETURN(condition, message, retVal)             \
79     do {                                                     \
80         if (!(condition)) {                                  \
81             ZLOGE("test (" #condition ") failed: " message); \
82             return retVal;                                   \
83         }                                                    \
84     } while (0)
85 
86 #define CHECK_RETURN_VOID(condition, message)                \
87     do {                                                     \
88         if (!(condition)) {                                  \
89             ZLOGE("test (" #condition ") failed: " message); \
90             return;                                          \
91         }                                                    \
92     } while (0)
93 
94 #define ASSERT_CALL(env, theCall, object)                              \
95     do {                                                               \
96         if ((theCall) != napi_ok) {                                    \
97             delete (object);                                           \
98             GET_AND_THROW_LAST_ERROR((env));                           \
99             return nullptr;                                            \
100         }                                                              \
101     } while (0)
102 
103 class NapiQueue {
104 public:
105     static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> ctxt, const std::string& name,
106         NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete());
107 
108 private:
109     enum {
110         /* AsyncCallback / Promise output result index  */
111         RESULT_ERROR = 0,
112         RESULT_DATA = 1,
113         RESULT_ALL = 2
114     };
115 
116     struct AsyncContext {
117         napi_env env = nullptr;
118         std::shared_ptr<ContextBase> ctx;
119         NapiAsyncExecute execute = nullptr;
120         NapiAsyncComplete complete = nullptr;
121         napi_deferred deferred = nullptr;
122         napi_async_work work = nullptr;
~AsyncContextAsyncContext123         ~AsyncContext()
124         {
125             execute = nullptr;
126             complete = nullptr;
127             ctx = nullptr;
128             if (env != nullptr) {
129                 if (work != nullptr) {
130                     napi_delete_async_work(env, work);
131                 }
132             }
133         }
134     };
135     static void GenerateOutput(AsyncContext &ctx, napi_value output);
136 };
137 } // namespace OHOS::DistributedData
138 #endif // OHOS_NAPI_QUEUE_H
139