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