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 24namespace OHOS::NWeb { 25using namespace NWebError; 26 27void 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 67void 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 napi_handle_scope scope = nullptr; 80 napi_open_handle_scope(param->env_, &scope); 81 if (scope == nullptr) { 82 return; 83 } 84 85 if (param->callbackRef_) { 86 UvAfterWorkCbAsync(param->env_, param->callbackRef_, param->result_); 87 } else if (param->deferred_) { 88 UvAfterWorkCbPromise(param->env_, param->deferred_, param->result_); 89 } 90 91 napi_close_handle_scope(param->env_, scope); 92 delete param; 93 param = nullptr; 94 delete work; 95 work = nullptr; 96} 97 98void WebviewHasImageCallback::UvAfterWorkCbAsync(napi_env env, napi_ref callbackRef, 99 bool result) 100{ 101 OHOS::NApiScope scope(env); 102 napi_value setResult[INTEGER_TWO] = {0}; 103 napi_get_undefined(env, &setResult[INTEGER_ZERO]); 104 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult[INTEGER_ONE]); 105 if (getBooleanResult != napi_ok) { 106 napi_get_boolean(env, false, &setResult[INTEGER_ONE]); 107 } 108 napi_value args[INTEGER_TWO] = {setResult[INTEGER_ZERO], setResult[INTEGER_ONE]}; 109 napi_value callback = nullptr; 110 napi_get_reference_value(env, callbackRef, &callback); 111 napi_value callbackResult = nullptr; 112 napi_call_function(env, nullptr, callback, INTEGER_TWO, args, &callbackResult); 113 114 napi_delete_reference(env, callbackRef); 115} 116 117void WebviewHasImageCallback::UvAfterWorkCbPromise(napi_env env, napi_deferred deferred, 118 bool result) 119{ 120 napi_value setResult; 121 napi_status getBooleanResult = napi_get_boolean(env, result, &setResult); 122 if (getBooleanResult != napi_ok) { 123 napi_get_boolean(env, false, &setResult); 124 } 125 napi_resolve_deferred(env, deferred, setResult); 126} 127 128} // namespace NWeb