From c8e8397ee6d5414304bfba7a7e3bf8f15b22a5e5 Mon Sep 17 00:00:00 2001 From: xinying-yuan Date: Thu, 9 Feb 2023 00:27:49 +0800 Subject: [PATCH 1/2] fixed 8336c9c from https://gitee.com/yuanxinying/multimodalinput_input/pulls/2295 solve crash problem by binding ets obj and c++ obj Signed-off-by: yuanxinying --- .../include/js_register_module.h | 10 +++++++++ .../input_consumer/src/js_register_module.cpp | 21 ++++++++++++++++++- .../input_consumer/src/js_register_util.cpp | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/frameworks/napi/input_consumer/include/js_register_module.h b/frameworks/napi/input_consumer/include/js_register_module.h index 43666703ec..a94c20af7f 100644 --- a/frameworks/napi/input_consumer/include/js_register_module.h +++ b/frameworks/napi/input_consumer/include/js_register_module.h @@ -54,6 +54,16 @@ struct KeyEventMonitorInfo { napi_ref callback[1] { nullptr }; int32_t subscribeId { 0 }; std::shared_ptr keyOption { nullptr }; + bool valid { true }; + std::mutex refLock; + void SetValid(bool flag) { + std::lock_guard lock(refLock); + valid = flag; + } + bool IsValid() { + std::lock_guard lock(refLock); + return valid; + } }; static std::mutex sCallBacksMutex_; typedef std::map> Callbacks; diff --git a/frameworks/napi/input_consumer/src/js_register_module.cpp b/frameworks/napi/input_consumer/src/js_register_module.cpp index 1200a31d12..ffab2c6edf 100644 --- a/frameworks/napi/input_consumer/src/js_register_module.cpp +++ b/frameworks/napi/input_consumer/src/js_register_module.cpp @@ -239,11 +239,22 @@ static napi_value JsOn(napi_env env, napi_callback_info info) CALL_DEBUG_ENTER; size_t argc = 3; napi_value argv[3] = { 0 }; - CHKRP(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), GET_CB_INFO); + napi_value thisArg = nullptr; + CHKRP(napi_get_cb_info(env, info, &argc, argv, &thisArg, nullptr), GET_CB_INFO); if (argc < 3) { THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "parameter number error"); return nullptr; } + if (thisArg == nullptr) { + MMI_HILOGE("%{public}s, This argument is nullptr.", __func__); + return nullptr; + } + napi_valuetype valueOfThis = napi_undefined; + CHKRP(napi_typeof(env, thisArg, &valueOfThis), TYPEOF); + if (valueOfThis == napi_undefined) { + MMI_HILOGE("%{public}s, Wrong value of this.", __func__); + return nullptr; + } KeyEventMonitorInfo *event = new (std::nothrow) KeyEventMonitorInfo { .env = env, .asyncWork = nullptr, @@ -283,6 +294,14 @@ static napi_value JsOn(napi_env env, napi_callback_info info) MMI_HILOGE("AddEventCallback failed"); return nullptr; } + std::shared_ptr* cbInfo = new std::shared_ptr(event); + napi_wrap(env, thisArg, (void*)cbInfo, [](napi_env env, void* data, void* hint) { + std::shared_ptr* cbInfo = static_cast*>(data); + if (cbInfo != nullptr && *cbInfo != nullptr) { + (*cbInfo)->SetValid(false); + delete cbInfo; + } + }, nullptr, nullptr); return nullptr; } diff --git a/frameworks/napi/input_consumer/src/js_register_util.cpp b/frameworks/napi/input_consumer/src/js_register_util.cpp index 667f02cf87..84220e656c 100644 --- a/frameworks/napi/input_consumer/src/js_register_util.cpp +++ b/frameworks/napi/input_consumer/src/js_register_util.cpp @@ -313,6 +313,10 @@ void EmitAsyncCallbackWork(KeyEventMonitorInfo *reportEvent) { CALL_DEBUG_ENTER; CHKPV(reportEvent); + if (!reportEvent->IsValid()) { + MMI_HILOGE("%{public}s, module exported object is invalid.", __func__); + return; + } uv_loop_s *loop = nullptr; CHKRV(napi_get_uv_event_loop(reportEvent->env, &loop), GET_UV_EVENT_LOOP); uv_work_t *work = new (std::nothrow) uv_work_t; -- Gitee From e48310052ab1b478450ca6c5890a28d56987444b Mon Sep 17 00:00:00 2001 From: Yuanxinying Date: Thu, 9 Feb 2023 01:21:41 +0000 Subject: [PATCH 2/2] update frameworks/napi/input_consumer/include/js_register_module.h. solve compiling error by mistaking mutext as mutex Signed-off-by: Yuanxinying --- frameworks/napi/input_consumer/include/js_register_module.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/napi/input_consumer/include/js_register_module.h b/frameworks/napi/input_consumer/include/js_register_module.h index a94c20af7f..124bc7feb7 100644 --- a/frameworks/napi/input_consumer/include/js_register_module.h +++ b/frameworks/napi/input_consumer/include/js_register_module.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -57,7 +58,7 @@ struct KeyEventMonitorInfo { bool valid { true }; std::mutex refLock; void SetValid(bool flag) { - std::lock_guard lock(refLock); + std::lock_guard lock(refLock); valid = flag; } bool IsValid() { -- Gitee