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 #include "uv_queue.h"
16 
17 #include <memory>
18 
19 #include "log_print.h"
20 
21 namespace OHOS::PreferencesJsKit {
22 constexpr size_t MAX_CALLBACK_ARG_NUM = 6;
UvQueue(napi_env env)23 UvQueue::UvQueue(napi_env env)
24     : env_(env)
25 {
26     if (env != nullptr) {
27         napi_get_uv_event_loop(env, &loop_);
28     }
29 }
30 
~UvQueue()31 UvQueue::~UvQueue()
32 {
33     LOG_DEBUG("no memory leak for queue-callback");
34     env_ = nullptr;
35 }
36 
AsyncCall(NapiCallbackGetter getter,NapiArgsGenerator genArgs,bool sendable)37 void UvQueue::AsyncCall(NapiCallbackGetter getter, NapiArgsGenerator genArgs, bool sendable)
38 {
39     if (loop_ == nullptr || !getter) {
40         LOG_ERROR("loop_ or callback is nullptr");
41         return;
42     }
43 
44     uv_work_t* work = new (std::nothrow) uv_work_t;
45     if (work == nullptr) {
46         return;
47     }
48     work->data = new (std::nothrow) UvEntry{ env_, getter, std::move(genArgs), sendable };
49     if (work->data == nullptr) {
50         delete work;
51         return;
52     }
53     int ret = uv_queue_work(loop_, work, [](uv_work_t* work) {}, UvQueue::Work);
54     if (ret != 0) {
55         LOG_ERROR("Failed to uv_queue_work.");
56         delete static_cast<UvEntry *>(work->data);
57         delete work;
58     }
59 }
60 
Work(uv_work_t * work,int uvstatus)61 void UvQueue::Work(uv_work_t* work, int uvstatus)
62 {
63     std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
64         delete data;
65         delete work;
66     });
67     napi_handle_scope scope = nullptr;
68     napi_open_handle_scope(entry->env, &scope);
69     if (scope == nullptr) {
70         return;
71     }
72     napi_value method = entry->callback(entry->env);
73     if (method == nullptr) {
74         LOG_WARN("the callback is invalid, maybe is cleared!");
75         napi_close_handle_scope(entry->env, scope);
76         return ;
77     }
78     int argc = 0;
79     napi_value argv[MAX_CALLBACK_ARG_NUM] = { nullptr };
80     if (entry->args) {
81         argc = MAX_CALLBACK_ARG_NUM;
82         entry->args(entry->env, entry->sendable, argc, argv);
83     }
84     LOG_DEBUG("queue uv_after_work_cb");
85     napi_value global = nullptr;
86     napi_get_global(entry->env, &global);
87     napi_value result;
88     napi_status status = napi_call_function(entry->env, global, method, argc, argv, &result);
89     if (status != napi_ok) {
90         LOG_ERROR("notify data change failed status:%{public}d.", status);
91     }
92     napi_close_handle_scope(entry->env, scope);
93 }
94 
GetEnv()95 napi_env UvQueue::GetEnv()
96 {
97     return env_;
98 }
99 } // namespace OHOS::DistributedKVStore
100