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 return;
44 }
45 param->env_ = env_;
46 param->callbackRef_ = callbackRef_;
47 param->deferred_ = deferred_;
48 param->result_ = result;
49
50 work->data = reinterpret_cast<void*>(param);
51
52 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvAfterWorkCb);
53 if (ret != 0) {
54 if (param != nullptr) {
55 delete param;
56 param = nullptr;
57 }
58 if (work != nullptr) {
59 delete work;
60 work = nullptr;
61 }
62 }
63 }
64
UvAfterWorkCb(uv_work_t * work,int status)65 void WebviewHasImageCallback::UvAfterWorkCb(uv_work_t* work, int status)
66 {
67 (void)status;
68 if (!work) {
69 return;
70 }
71 HasImageParam *param = reinterpret_cast<HasImageParam*>(work->data);
72 if (!param) {
73 delete work;
74 work = nullptr;
75 return;
76 }
77 napi_handle_scope scope = nullptr;
78 napi_open_handle_scope(param->env_, &scope);
79 if (scope == nullptr) {
80 return;
81 }
82
83 if (param->callbackRef_) {
84 UvAfterWorkCbAsync(param->env_, param->callbackRef_, param->result_);
85 } else if (param->deferred_) {
86 UvAfterWorkCbPromise(param->env_, param->deferred_, param->result_);
87 }
88
89 napi_close_handle_scope(param->env_, scope);
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 napi_value setResult[INTEGER_TWO] = {0};
100 napi_get_undefined(env, &setResult[INTEGER_ZERO]);
101 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult[INTEGER_ONE]);
102 if (getBooleanResult != napi_ok) {
103 napi_get_boolean(env, false, &setResult[INTEGER_ONE]);
104 }
105 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]};
106 napi_value callback = nullptr;
107 napi_get_reference_value(env, callbackRef, &callback);
108 napi_value callbackResult = nullptr;
109 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult);
110
111 napi_delete_reference(env, callbackRef);
112 }
113
UvAfterWorkCbPromise(napi_env env,napi_deferred deferred,bool result)114 void WebviewHasImageCallback::UvAfterWorkCbPromise(napi_env env, napi_deferred deferred,
115 bool result)
116 {
117 napi_value setResult;
118 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult);
119 if (getBooleanResult != napi_ok) {
120 napi_get_boolean(env, false, &setResult);
121 }
122 napi_resolve_deferred(env, deferred, setResult);
123 }
124
125 } // namespace NWeb