• 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 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_deferred deferred = nullptr;
53     napi_async_work work = nullptr;
54     napi_ref callbackRef = nullptr;
55     napi_ref selfRef = nullptr;
56 
57     NapiAsyncExecute execute = nullptr;
58     NapiAsyncComplete complete = nullptr;
59     std::shared_ptr<ContextBase> hold; /* cross thread data */
60 
61     friend class NapiQueue;
62 };
63 
64 /* check condition related to argc/argv, return and logging. */
65 #define CHECK_ARGS_RETURN_VOID(ctxt, condition, message)               \
66     do {                                                               \
67         if (!(condition)) {                                            \
68             (ctxt)->status = napi_invalid_arg;                         \
69             (ctxt)->error = std::string(message);                      \
70             ZLOGE("test (" #condition ") failed: " message);           \
71             return;                                                    \
72         }                                                              \
73     } while (0)
74 
75 #define CHECK_STATUS_RETURN_VOID(ctxt, message)                        \
76     do {                                                               \
77         if ((ctxt)->status != napi_ok) {                               \
78             (ctxt)->error = std::string(message);                      \
79             ZLOGE("test (ctxt->status == napi_ok) failed: " message);  \
80             return;                                                    \
81         }                                                              \
82     } while (0)
83 
84 /* check condition, return and logging if condition not true. */
85 #define CHECK_RETURN(condition, message, retVal)             \
86     do {                                                     \
87         if (!(condition)) {                                  \
88             ZLOGE("test (" #condition ") failed: " message); \
89             return retVal;                                   \
90         }                                                    \
91     } while (0)
92 
93 #define CHECK_RETURN_VOID(condition, message)                \
94     do {                                                     \
95         if (!(condition)) {                                  \
96             ZLOGE("test (" #condition ") failed: " message); \
97             return;                                          \
98         }                                                    \
99     } while (0)
100 
101 #define ASSERT_CALL(env, theCall, object)                    \
102     do {                                                     \
103         if ((theCall) != napi_ok) {                          \
104             delete (object);                                 \
105             GET_AND_THROW_LAST_ERROR((env));                 \
106             return nullptr;                                  \
107         }                                                    \
108     } while (0)
109 
110 class NapiQueue {
111 public:
112     static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> ctxt, const std::string& name,
113         NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete());
114 
115 private:
116     enum {
117         /* AsyncCallback / Promise output result index  */
118         RESULT_ERROR = 0,
119         RESULT_DATA = 1,
120         RESULT_ALL = 2
121     };
122     static void GenerateOutput(ContextBase* ctxt);
123 };
124 } // namespace OHOS::DistributedData
125 #endif // OHOS_NAPI_QUEUE_H
126