• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 DISTRIBUTEDDATAMGR_APPDATAMGR_UV_QUEUE_H
16 #define DISTRIBUTEDDATAMGR_APPDATAMGR_UV_QUEUE_H
17 #include <functional>
18 
19 #include "event_handler.h"
20 #include "napi/native_api.h"
21 #include "napi/native_common.h"
22 #include "napi/native_node_api.h"
23 #include "uv.h"
24 
25 namespace OHOS::AppDataMgrJsKit {
26 class UvQueue final {
27 public:
28     using Task = std::function<void()>;
29     using Args = std::function<void(napi_env env, int &argc, napi_value *argv)>;
30     using Result = std::function<void(napi_env env, size_t count, napi_value *values, bool exception)>;
31     using Callbacker = std::function<napi_value(napi_env env)>;
32     enum { ARG_ERROR, ARG_DATA, ARG_BUTT };
33     struct UvCallback {
34         napi_ref object_ = nullptr;
35         napi_ref callback_ = nullptr;
36         Callbacker getter_;
37         bool repeat_ = false;
callback_UvCallback38         UvCallback(napi_ref callback, bool repeat = false) : callback_(callback), repeat_(repeat) { }
UvCallbackUvCallback39         UvCallback(napi_ref object, napi_ref callback) : object_(object), callback_(callback) { }
object_UvCallback40         UvCallback(Callbacker getter, napi_ref object = nullptr) : object_(object), getter_(std::move(getter)) { }
IsNullUvCallback41         bool IsNull()
42         {
43             return (callback_ == nullptr && getter_ == nullptr);
44         }
45     };
46 
47     struct UvPromise {
48         napi_deferred defer_ = nullptr;
UvPromiseUvPromise49         UvPromise(napi_deferred defer) : defer_(defer) { }
IsNullUvPromise50         bool IsNull()
51         {
52             return (defer_ == nullptr);
53         }
54     };
55 
56     explicit UvQueue(napi_env env);
57     ~UvQueue();
58 
59     napi_env GetEnv();
60     void AsyncCall(UvCallback callback, Args args = Args(), Result result = Result());
61     void AsyncCallInOrder(UvCallback callback, Args args = Args(), Result result = Result());
62     void AsyncPromise(UvPromise promise, Args args = Args());
63     void Execute(Task task);
64 
65 private:
66     static constexpr char RESOLVED[] = "resolved";
67     static constexpr char REJECTED[] = "rejected";
68     static constexpr size_t RESOLVED_SIZE = sizeof(RESOLVED);
69     static constexpr size_t REJECTED_SIZE = sizeof(REJECTED);
70 
71     static napi_value Resolved(napi_env env, napi_callback_info info);
72     static napi_value Rejected(napi_env env, napi_callback_info info);
73     static napi_value Future(napi_env env, napi_callback_info info, bool exception);
74     static void DoExecute(uv_work_t *work);
75 
76     struct UvEntry {
77         napi_env env_ = nullptr;
78         napi_ref object_ = nullptr;
79         napi_ref callback_ = nullptr;
80         napi_deferred defer_ = nullptr;
81         bool repeat_ = false;
82         Callbacker getter_;
83         Args args_;
84         Result result_;
85         ~UvEntry();
86         napi_value GetCallback();
87         napi_value GetObject();
88         void BindPromise(napi_value promise);
89         void DelReference();
90         Result *StealResult();
91         int32_t GetArgv(napi_value *argv, int32_t max);
92     };
93     static Task GenCallbackTask(std::shared_ptr<UvEntry> entry);
94     static Task GenPromiseTask(std::shared_ptr<UvEntry> entry);
95     napi_env env_ = nullptr;
96     uv_loop_s *loop_ = nullptr;
97     std::shared_ptr<AppExecFwk::EventHandler> handler_;
98 };
99 } // namespace OHOS::AppDataMgrJsKit
100 #endif // DISTRIBUTEDDATAMGR_APPDATAMGR_UV_QUEUE_H
101