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