• 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 NAPI_QUEUE_H
16 #define NAPI_QUEUE_H
17 
18 #include "napi/native_node_api.h"
19 #include "object_error.h"
20 
21 namespace OHOS::ObjectStore {
22 using NapiCbInfoParser = std::function<void(size_t argc, napi_value *argv)>;
23 using NapiAsyncExecute = std::function<void(void)>;
24 using NapiAsyncComplete = std::function<void(napi_value &)>;
25 static constexpr size_t ARGC_MAX = 6;
26 struct ContextBase {
27     virtual ~ContextBase();
28     void GetCbInfo(
29         napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser(), bool sync = false);
30 
31     inline void GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser())
32     {
33         /* sync = true, means no callback, not AsyncWork. */
34         GetCbInfo(env, info, parse, true);
35     }
36 
SetErrorContextBase37     void SetError(std::shared_ptr<Error> err)
38     {
39         error = err;
40     }
41 
42     napi_env env = nullptr;
43     napi_value output = nullptr;
44     napi_status status = napi_invalid_arg;
45     std::string message;
46     std::shared_ptr<Error> error;
47     napi_value self = nullptr;
48     void *native = nullptr;
49 
50 private:
51     napi_deferred deferred = nullptr;
52     napi_ref callbackRef = nullptr;
53     napi_ref selfRef = nullptr;
54 
55     NapiAsyncExecute execute = nullptr;
56     NapiAsyncComplete complete = nullptr;
57     std::shared_ptr<ContextBase> hold; /* cross thread data */
58 
59     friend class NapiQueue;
60 };
61 
62 /* check condition related to argc/argv, return and logging. */
63 #define INVALID_ARGS_RETURN_ERROR(ctxt, condition, message, err)    \
64     do {                                                         \
65         if (!(condition)) {                                      \
66             (ctxt)->status = napi_invalid_arg;                   \
67             (ctxt)->error = err;                                 \
68             LOG_ERROR("test (" #condition ") failed: " message); \
69             return;                                              \
70         }                                                        \
71     } while (0)
72 
73 #define INVALID_STATUS_RETURN_ERROR(ctxt, error)                           \
74     do {                                                                \
75         if ((ctxt)->status != napi_ok) {                                \
76             (ctxt)->message = error;                                    \
77             LOG_ERROR("test (ctxt->status == napi_ok) failed: " error); \
78             return;                                                     \
79         }                                                               \
80     } while (0)
81 
82 class NapiQueue {
83 public:
84     static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> contextBase, const std::string &name,
85         NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete());
86     static void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error);
87 
88 private:
89     enum {
90         /* AsyncCallback / Promise output result index  */
91         RESULT_ERROR = 0,
92         RESULT_DATA = 1,
93         RESULT_ALL = 2
94     };
95     struct AsyncContext {
96         std::shared_ptr<ContextBase> ctxt;
97         NapiAsyncExecute execute = nullptr;
98         NapiAsyncComplete complete = nullptr;
99         napi_deferred deferred = nullptr;
100         napi_async_work work = nullptr;
101     };
102     static void GenerateOutput(ContextBase *ctxt);
103 };
104 } // namespace OHOS::ObjectStore
105 #endif // OHOS_NAPI_QUEUE_H
106