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_hasimage_callback.h"
17
18 #include "business_error.h"
19 #include "napi_parse_utils.h"
20 #include "nweb_log.h"
21 #include "web_errors.h"
22
23 namespace OHOS::NWeb {
24 using namespace NWebError;
25
OnReceiveValue(bool result)26 void WebviewHasImageCallback::OnReceiveValue(bool result)
27 {
28 uv_loop_s *loop = nullptr;
29 uv_work_t *work = nullptr;
30
31 napi_get_uv_event_loop(env_, &loop);
32 if (loop == nullptr) {
33 return;
34 }
35 work = new (std::nothrow) uv_work_t;
36 if (work == nullptr) {
37 return;
38 }
39
40 HasImageParam *param = new (std::nothrow) HasImageParam();
41 if (param == nullptr) {
42 delete work;
43 work = nullptr;
44 return;
45 }
46 param->env_ = env_;
47 param->callbackRef_ = callbackRef_;
48 param->deferred_ = deferred_;
49 param->result_ = result;
50
51 work->data = reinterpret_cast<void*>(param);
52
53 int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, UvAfterWorkCb, uv_qos_user_initiated);
54 if (ret != 0) {
55 if (param != nullptr) {
56 delete param;
57 param = nullptr;
58 }
59 if (work != nullptr) {
60 delete work;
61 work = nullptr;
62 }
63 }
64 }
65
UvAfterWorkCb(uv_work_t * work,int status)66 void WebviewHasImageCallback::UvAfterWorkCb(uv_work_t* work, int status)
67 {
68 (void)status;
69 if (!work) {
70 return;
71 }
72 HasImageParam *param = reinterpret_cast<HasImageParam*>(work->data);
73 if (!param) {
74 delete work;
75 work = nullptr;
76 return;
77 }
78 napi_handle_scope scope = nullptr;
79 napi_open_handle_scope(param->env_, &scope);
80 if (scope == nullptr) {
81 return;
82 }
83
84 if (param->callbackRef_) {
85 UvAfterWorkCbAsync(param->env_, param->callbackRef_, param->result_);
86 } else if (param->deferred_) {
87 UvAfterWorkCbPromise(param->env_, param->deferred_, param->result_);
88 }
89
90 napi_close_handle_scope(param->env_, scope);
91 delete param;
92 param = nullptr;
93 delete work;
94 work = nullptr;
95 }
96
UvAfterWorkCbAsync(napi_env env,napi_ref callbackRef,bool result)97 void WebviewHasImageCallback::UvAfterWorkCbAsync(napi_env env, napi_ref callbackRef,
98 bool result)
99 {
100 napi_value setResult[INTEGER_TWO] = {0};
101 napi_get_undefined(env, &setResult[INTEGER_ZERO]);
102 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult[INTEGER_ONE]);
103 if (getBooleanResult != napi_ok) {
104 napi_get_boolean(env, false, &setResult[INTEGER_ONE]);
105 }
106 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]};
107 napi_value callback = nullptr;
108 napi_get_reference_value(env, callbackRef, &callback);
109 napi_value callbackResult = nullptr;
110 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult);
111
112 napi_delete_reference(env, callbackRef);
113 }
114
UvAfterWorkCbPromise(napi_env env,napi_deferred deferred,bool result)115 void WebviewHasImageCallback::UvAfterWorkCbPromise(napi_env env, napi_deferred deferred,
116 bool result)
117 {
118 napi_value setResult;
119 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult);
120 if (getBooleanResult != napi_ok) {
121 napi_get_boolean(env, false, &setResult);
122 }
123 napi_resolve_deferred(env, deferred, setResult);
124 }
125
126 } // namespace NWeb