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) {
57             ZLOGD("AsyncCall callback");
58         }, UvQueue::Work);
59     if (retVal != 0) {
60         delete (reinterpret_cast<UvEntry*>(work->data));
61         work->data = nullptr;
62         delete work;
63         work = nullptr;
64     }
65 }
66 
Work(uv_work_t * work,int uvStatus)67 void UvQueue::Work(uv_work_t* work, int uvStatus)
68 {
69     std::shared_ptr<UvEntry> entry(static_cast<UvEntry*>(work->data), [work](UvEntry* data) {
70         delete data;
71         delete work;
72     });
73     napi_handle_scope scope = nullptr;
74     napi_open_handle_scope(entry->env, &scope);
75     napi_value method = entry->callback(entry->env);
76     if (method == nullptr) {
77         ZLOGE("the callback is invalid, maybe is cleared!");
78         if (scope != nullptr) {
79             napi_close_handle_scope(entry->env, scope);
80         }
81         return;
82     }
83     int argc = 0;
84     napi_value argv[ARGC_MAX] = { nullptr };
85     if (entry->args) {
86         argc = ARGC_MAX;
87         entry->args(entry->env, argc, argv);
88     }
89     ZLOGD("queue uv_after_work_cb");
90     napi_value global = nullptr;
91     napi_get_global(entry->env, &global);
92     napi_value result;
93     napi_status status = napi_call_function(entry->env, global, method, argc, argv, &result);
94     if (status != napi_ok) {
95         ZLOGE("notify data change failed status:%{public}d.", status);
96     }
97     if (scope != nullptr) {
98         napi_close_handle_scope(entry->env, scope);
99     }
100 }
101 
GetEnv()102 napi_env UvQueue::GetEnv()
103 {
104     return env_;
105 }
106 } // namespace OHOS::DistributedData
107