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 "nweb_napi_scope.h"
22 #include "web_errors.h"
23
24 namespace OHOS::NWeb {
25 using namespace NWebError;
26
OnReceiveValue(bool result)27 void WebviewHasImageCallback::OnReceiveValue(bool result)
28 {
29 uv_loop_s *loop = nullptr;
30 uv_work_t *work = nullptr;
31
32 napi_get_uv_event_loop(env_, &loop);
33 if (loop == nullptr) {
34 return;
35 }
36 work = new (std::nothrow) uv_work_t;
37 if (work == nullptr) {
38 return;
39 }
40
41 HasImageParam *param = new (std::nothrow) HasImageParam();
42 if (param == nullptr) {
43 delete work;
44 work = nullptr;
45 return;
46 }
47 param->env_ = env_;
48 param->callbackRef_ = callbackRef_;
49 param->deferred_ = deferred_;
50 param->result_ = result;
51
52 work->data = reinterpret_cast<void*>(param);
53
54 int ret = uv_queue_work_with_qos(loop, work, [](uv_work_t *work) {}, UvAfterWorkCb, uv_qos_user_initiated);
55 if (ret != 0) {
56 if (param != nullptr) {
57 delete param;
58 param = nullptr;
59 }
60 if (work != nullptr) {
61 delete work;
62 work = nullptr;
63 }
64 }
65 }
66
UvAfterWorkCb(uv_work_t * work,int status)67 void WebviewHasImageCallback::UvAfterWorkCb(uv_work_t* work, int status)
68 {
69 (void)status;
70 if (!work) {
71 return;
72 }
73 HasImageParam *param = reinterpret_cast<HasImageParam*>(work->data);
74 if (!param) {
75 delete work;
76 work = nullptr;
77 return;
78 }
79 NApiScope scope(param->env_);
80 if (!scope.IsVaild()) {
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 delete param;
91 param = nullptr;
92 delete work;
93 work = nullptr;
94 }
95
UvAfterWorkCbAsync(napi_env env,napi_ref callbackRef,bool result)96 void WebviewHasImageCallback::UvAfterWorkCbAsync(napi_env env, napi_ref callbackRef,
97 bool result)
98 {
99 OHOS::NApiScope scope(env);
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