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
16 #include "webview_web_inited_callback.h"
17
18 #include "nweb_log.h"
19 #include "uv.h"
20
21 namespace OHOS::NWeb {
RunInitedCallback()22 void WebRunInitedCallbackImpl::RunInitedCallback()
23 {
24 uv_loop_s *loop = nullptr;
25 uv_work_t *work = nullptr;
26 napi_get_uv_event_loop(param_->env_, &loop);
27
28 if (loop == nullptr) {
29 WVLOG_E("get uv event loop failed");
30 return;
31 }
32 work = new (std::nothrow) uv_work_t;
33 if (work == nullptr) {
34 WVLOG_E("new uv work failed");
35 return;
36 }
37 work->data = reinterpret_cast<void*>(param_);
38 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) {
39 if (work == nullptr) {
40 WVLOG_E("uv work is null");
41 return;
42 }
43 WebInitedCallbackParam *data = reinterpret_cast<WebInitedCallbackParam*>(work->data);
44 if (data == nullptr) {
45 delete work;
46 work = nullptr;
47 return;
48 }
49 napi_handle_scope scope = nullptr;
50 napi_open_handle_scope(data->env_, &scope);
51 if (scope == nullptr) {
52 return;
53 }
54 napi_value webInitedResult = nullptr;
55 napi_value jsWebInitedCallback = nullptr;
56 napi_get_reference_value(data->env_, data->webInitedCallback_, &jsWebInitedCallback);
57 napi_call_function(data->env_, nullptr, jsWebInitedCallback, 0, {}, &webInitedResult);
58
59 napi_close_handle_scope(data->env_, scope);
60 delete data;
61 data = nullptr;
62 delete work;
63 work = nullptr;
64 });
65 if (ret != 0) {
66 if (param_ != nullptr) {
67 delete param_;
68 param_ = nullptr;
69 }
70 if (work != nullptr) {
71 delete work;
72 work = nullptr;
73 }
74 }
75 }
76 }