diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp index 9f916479..adcc9094 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -302,7 +302,7 @@ void JsGetInputMethodController::RegisterListener( jsCbMap_[type].push_back(std::move(callbackObj)); } -void JsGetInputMethodController::UnRegisterListener(std::string type) +void JsGetInputMethodController::UnRegisterListener(napi_value callback, std::string type) { IMSA_HILOGI("UnRegisterListener %{public}s", type.c_str()); std::lock_guard lock(mutex_); @@ -310,7 +310,22 @@ void JsGetInputMethodController::UnRegisterListener(std::string type) IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str()); return; } - jsCbMap_.erase(type); + if (callback == nullptr) { + jsCbMap_.erase(type); + IMSA_HILOGE("callback is nullptr"); + return; + } + + for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) { + if ((callback != nullptr) + && (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_))) { + jsCbMap_[type].erase(item); + break; + } + } + if (jsCbMap_[type].empty()) { + jsCbMap_.erase(type); + } } napi_value JsGetInputMethodController::Subscribe(napi_env env, napi_callback_info info) @@ -364,12 +379,16 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str()); return nullptr; } + // If the type of optional parameter is wrong, make it nullptr + if (JsUtil::GetType(env, argv[1]) != napi_function) { + argv[1] = nullptr; + } IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str()); auto engine = reinterpret_cast(JsUtils::GetNativeSelf(env, info)); if (engine == nullptr) { return nullptr; } - engine->UnRegisterListener(type); + engine->UnRegisterListener(argv[1], type); napi_value result = nullptr; napi_get_null(env, &result); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h index 0939f9bf..74aae69f 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.h @@ -192,7 +192,7 @@ private: static napi_value CreateSelectMovement(napi_env env, int32_t direction); static napi_value CreateSendFunctionKey(napi_env env, int32_t functionKey); void RegisterListener(napi_value callback, std::string type, std::shared_ptr callbackObj); - void UnRegisterListener(std::string type); + void UnRegisterListener(napi_value callback, std::string type); static napi_status ParseAttachInput( napi_env env, size_t argc, napi_value *argv, const std::shared_ptr &ctxt); static bool GetValue(napi_env env, napi_value in, CursorInfo &out); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp index a6af570b..d5f7c384 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_textchange_listener.cpp @@ -67,7 +67,7 @@ void JsGetInputMethodTextChangedListener::HandleExtendAction(int32_t action) std::u16string JsGetInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number) { - return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor" ,number); + return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor", number); } std::u16string JsGetInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number) diff --git a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp index 3290c7b1..3889992b 100644 --- a/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp +++ b/frameworks/native/inputmethod_controller/src/input_data_channel_stub.cpp @@ -115,8 +115,8 @@ int32_t InputDataChannelStub::SelectByRangeOnRemote(MessageParcel &data, Message int32_t start = 0; int32_t end = 0; int ret = SendMessage([&data, &start, &end](MessageParcel &parcel) { - return ITypesUtil::Unmarshal(data, start, end) && ITypesUtil::Marshal(parcel, start, end) ? new (std::nothrow) - Message(MessageID::MSG_ID_SELECT_BY_RANGE, &parcel) : nullptr; + return ITypesUtil::Unmarshal(data, start, end) && ITypesUtil::Marshal(parcel, start, end) ? + new (std::nothrow)Message(MessageID::MSG_ID_SELECT_BY_RANGE, &parcel) : nullptr; }); if (!ITypesUtil::Marshal(reply, ret)) { IMSA_HILOGE("failed to write reply"); @@ -148,8 +148,8 @@ int32_t InputDataChannelStub::HandleExtendActionOnRemote(MessageParcel &data, Me IMSA_HILOGD("InputDataChannelStub run in"); int32_t action = 0; auto ret = SendMessage([&data, &action](MessageParcel &parcel) { - return ITypesUtil::Unmarshal(data, action) && ITypesUtil::Marshal(parcel, action) ? new (std::nothrow) - Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, &parcel) : nullptr; + return ITypesUtil::Unmarshal(data, action) && ITypesUtil::Marshal(parcel, action) ? + new (std::nothrow)Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, &parcel) : nullptr; }); if (!ITypesUtil::Marshal(reply, ret)) { IMSA_HILOGE("failed to write reply"); @@ -205,8 +205,8 @@ int32_t InputDataChannelStub::GetText(int32_t msgId, MessageParcel &data, Messag int32_t number = -1; auto resultHandler = std::make_shared>(MAX_TIMEOUT, u""); auto ret = SendMessage([&msgId, &data, &number, &resultHandler](MessageParcel &parcel) { - return ITypesUtil::Unmarshal(data, number) && ITypesUtil::Marshal(parcel, number) ? new (std::nothrow) - Message(msgId, &parcel, resultHandler) : nullptr; + return ITypesUtil::Unmarshal(data, number) && ITypesUtil::Marshal(parcel, number) ? + new (std::nothrow)Message(msgId, &parcel, resultHandler) : nullptr; }); if (ret != ErrorCode::NO_ERROR) { return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE; diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 697af7f8..40ed5705 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -774,6 +774,10 @@ void InputMethodController::GetText(const Message *msg) return; } auto number = msg->msgContent_->ReadInt32(); + if (number < 0) { + resultHandler->SetValue(text); + return; + } if (msg->msgId_ == MSG_ID_GET_TEXT_BEFORE_CURSOR) { text = textListener_->GetLeftTextOfCursor(number); } else { diff --git a/services/include/message.h b/services/include/message.h index b261cdd3..324390f8 100644 --- a/services/include/message.h +++ b/services/include/message.h @@ -30,7 +30,8 @@ public: MessageParcel *msgContent_ = nullptr; // message content std::shared_ptr> textResultHandler_{ nullptr }; std::shared_ptr> indexResultHandler_{ nullptr }; - Message(int32_t msgId, MessageParcel *msgContent, const std::shared_ptr> &textResultHandler); + Message( + int32_t msgId, MessageParcel *msgContent, const std::shared_ptr> &textResultHandler); Message(int32_t msgId, MessageParcel *msgContent, const std::shared_ptr> &indexResultHandler); Message(int32_t msgId, MessageParcel *msgContent); explicit Message(const Message &msg);