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