From b10ec580381905a0d19e38d08309ca47f54731cf Mon Sep 17 00:00:00 2001 From: jason Date: Tue, 28 May 2024 07:59:09 +0000 Subject: [PATCH] fix: rectify the Warning Signed-off-by: jason --- .../source/napi_remote_proxy_holder.cpp | 88 ++++++++++++++----- 1 file changed, 64 insertions(+), 24 deletions(-) diff --git a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp index 27d0cfb3..783e07a2 100644 --- a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp @@ -32,36 +32,67 @@ NAPIDeathRecipient::NAPIDeathRecipient(napi_env env, napi_value jsDeathRecipient napi_status status = napi_create_reference(env_, jsDeathRecipient, 1, &deathRecipientRef_); NAPI_ASSERT_RETURN_VOID(env, status == napi_ok, "failed to create ref to js death recipient"); } - -void NAPIDeathRecipient::OnRemoteDied(const wptr &object) -{ - if (deathRecipientRef_ == nullptr) { +static bool ValidateDeathRecipient(napi_ref deathRecipientRef, napi_env env) { + if (deathRecipientRef == nullptr) { ZLOGE(LOG_LABEL, "js death recipient has already removed"); - return; + return false; } - if (env_ == nullptr) { + if (env == nullptr) { ZLOGE(LOG_LABEL, "js env has been destructed"); - return; + return false; } - uv_loop_s *loop = nullptr; - napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { + return true; +} + +static bool GetEventLoop(napi_env env, uv_loop_s** loop) { + napi_get_uv_event_loop(env, loop); + if (*loop == nullptr) { ZLOGE(LOG_LABEL, "loop is nullptr"); - return; + return false; } - uv_work_t *work = new(std::nothrow) uv_work_t; - if (work == nullptr) { + return true; +} + +static bool AllocateWorkAndParam(uv_work_t** work, OnRemoteDiedParam** param, napi_env env, napi_ref deathRecipientRef) { + *work = new(std::nothrow) uv_work_t; + if (*work == nullptr) { ZLOGE(LOG_LABEL, "failed to new uv_work_t"); + return false; + } + + *param = new(std::nothrow) OnRemoteDiedParam{.env = env, .deathRecipientRef = deathRecipientRef}; + if (*param == nullptr) { + ZLOGE(LOG_LABEL, "failed to new OnRemoteDiedParam"); + delete *work; + return false; + } + + (*work)->data = reinterpret_cast(*param); + return true; +} + +static void CleanUp(uv_work_t* work, OnRemoteDiedParam* param, napi_handle_scope scope) { + if (scope != nullptr) { + napi_close_handle_scope(param->env, scope); + } + delete param; + delete work; +} +void NAPIDeathRecipient::OnRemoteDied(const wptr &object) +{ + if (!ValidateDeathRecipient(deathRecipientRef_, env_)) { + return; + } + + uv_loop_s *loop = nullptr; + OnRemoteDiedParam *param = nullptr; + if (!AllocateWorkAndParam(&work, ¶m, env_, deathRecipientRef_)) { return; } - OnRemoteDiedParam *param = new OnRemoteDiedParam { - .env = env_, - .deathRecipientRef = deathRecipientRef_ - }; - work->data = reinterpret_cast(param); + ZLOGI(LOG_LABEL, "start to queue"); uv_queue_work(loop, work, [](uv_work_t *work) { ZLOGD(LOG_LABEL, "enter work pool."); @@ -72,10 +103,18 @@ void NAPIDeathRecipient::OnRemoteDied(const wptr &object) napi_open_handle_scope(param->env, &scope); napi_value jsDeathRecipient = nullptr; napi_get_reference_value(param->env, param->deathRecipientRef, &jsDeathRecipient); - NAPI_ASSERT_RETURN_VOID(param->env, jsDeathRecipient != nullptr, "failed to get js death recipient"); + if (jsDeathRecipient == nullptr) { + ZLOGE(LOG_LABEL, "failed to get js death recipient"); + CleanUp(work, param, scope); + return; + } napi_value onRemoteDied = nullptr; napi_get_named_property(param->env, jsDeathRecipient, "onRemoteDied", &onRemoteDied); - NAPI_ASSERT_RETURN_VOID(param->env, onRemoteDied != nullptr, "failed to get property onRemoteDied"); + if (onRemoteDied == nullptr) { + ZLOGE(LOG_LABEL, "failed to get property onRemoteDied"); + CleanUp(work, param, scope); + return; + } napi_value returnVal = nullptr; napi_call_function(param->env, jsDeathRecipient, onRemoteDied, 0, nullptr, &returnVal); if (returnVal == nullptr) { @@ -83,10 +122,11 @@ void NAPIDeathRecipient::OnRemoteDied(const wptr &object) } napi_status napiStatus = napi_delete_reference(param->env, param->deathRecipientRef); - napi_close_handle_scope(param->env, scope); - NAPI_ASSERT_RETURN_VOID(param->env, napiStatus == napi_ok, "failed to delete ref to js death recipient"); - delete param; - delete work; + if (napiStatus != napi_ok) { + ZLOGE(LOG_LABEL, "failed to delete ref to js death recipient"); + } + + CleanUp(work, param, scope); }); } -- Gitee