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