/* * Copyright (C) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define MLOG_TAG "PickerNapiUtils" #include "picker_napi_utils.h" #include "ipc_skeleton.h" #include "picker_n_exporter.h" using namespace std; namespace OHOS { namespace Picker { static const string EMPTY_STRING = ""; void PickerNapiUtils::CreateNapiErrorObject(napi_env env, napi_value &errorObj, const int32_t errCode, const string errMsg) { napi_status statusError; napi_value napiErrorCode = nullptr; napi_value napiErrorMsg = nullptr; statusError = napi_create_string_utf8(env, to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &napiErrorCode); if (statusError == napi_ok) { statusError = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &napiErrorMsg); if (statusError == napi_ok) { statusError = napi_create_error(env, napiErrorCode, napiErrorMsg, &errorObj); if (statusError == napi_ok) { HILOG_DEBUG("napi_create_error success"); } } } } void PickerNapiUtils::InvokeJSAsyncMethod(napi_env env, napi_deferred deferred, napi_ref callbackRef, napi_async_work work, const JSAsyncContextOutput &asyncContext) { HILOG_INFO("modal picker: InvokeJSAsyncMethod begin."); if (asyncContext.status) { napi_resolve_deferred(env, deferred, asyncContext.data); } else { napi_reject_deferred(env, deferred, asyncContext.error); } napi_delete_async_work(env, work); } template napi_value PickerNapiUtils::NapiCreateAsyncWork(napi_env env, unique_ptr &asyncContext, const string &resourceName, void (*execute)(napi_env, void *), void (*complete)(napi_env, napi_status, void *)) { napi_value result = nullptr; napi_value resource = nullptr; napi_create_promise(env, &(asyncContext->deferred), &(result)); napi_create_string_utf8(env, resourceName.c_str(), NAPI_AUTO_LENGTH, &(resource)); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, execute, complete, static_cast(asyncContext.get()), &asyncContext->work)); NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); asyncContext.release(); return result; } template napi_value PickerNapiUtils::NapiCreateAsyncWork(napi_env env, unique_ptr &asyncContext, const string &resourceName, void (*execute)(napi_env, void *), void (*complete)(napi_env, napi_status, void *)); } // namespace Picker } // namespace OHOS