diff --git a/frameworks/cj/src/cj_input_method_textchanged_listener.cpp b/frameworks/cj/src/cj_input_method_textchanged_listener.cpp index 0e7d605ec3e39a45440ced825a00239ff557ccf1..2afe9f86786b726d58b58910eafab85d749594f6 100644 --- a/frameworks/cj/src/cj_input_method_textchanged_listener.cpp +++ b/frameworks/cj/src/cj_input_method_textchanged_listener.cpp @@ -32,52 +32,102 @@ sptr CjInputMethodTextChangedListener::GetInst void CjInputMethodTextChangedListener::InsertText(const std::u16string &text) { - CjInputMethodController::GetInstance()->InsertText(text); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->InsertText(text); } void CjInputMethodTextChangedListener::DeleteForward(int32_t length) { - CjInputMethodController::GetInstance()->DeleteRight(length); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteRight(length); } void CjInputMethodTextChangedListener::DeleteBackward(int32_t length) { - CjInputMethodController::GetInstance()->DeleteLeft(length); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteLeft(length); } void CjInputMethodTextChangedListener::SendKeyboardStatus(const KeyboardStatus &status) { - CjInputMethodController::GetInstance()->SendKeyboardStatus(status); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendKeyboardStatus(status); } void CjInputMethodTextChangedListener::SendFunctionKey(const FunctionKey &functionKey) { - CjInputMethodController::GetInstance()->SendFunctionKey(functionKey); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendFunctionKey(functionKey); } void CjInputMethodTextChangedListener::MoveCursor(const Direction direction) { - CjInputMethodController::GetInstance()->MoveCursor(direction); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->MoveCursor(direction); } void CjInputMethodTextChangedListener::HandleExtendAction(int32_t action) { - CjInputMethodController::GetInstance()->HandleExtendAction(action); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->HandleExtendAction(action); } std::u16string CjInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number) { - return CjInputMethodController::GetInstance()->GetLeftText(number); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetLeftText(number); } std::u16string CjInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number) { - return CjInputMethodController::GetInstance()->GetRightText(number); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetRightText(number); } int32_t CjInputMethodTextChangedListener::GetTextIndexAtCursor() { - return CjInputMethodController::GetInstance()->GetTextIndexAtCursor(); + auto controller = CjInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return controller->GetTextIndexAtCursor(); } int32_t CjInputMethodTextChangedListener::ReceivePrivateCommand( diff --git a/frameworks/cj/src/input_method_ffi.cpp b/frameworks/cj/src/input_method_ffi.cpp index 9016c2878cb1e9217e625a616d3b4a87dd660f9f..46cfe7a8ee3aa2e6d67fc3a77fd7cc3c08567b76 100644 --- a/frameworks/cj/src/input_method_ffi.cpp +++ b/frameworks/cj/src/input_method_ffi.cpp @@ -125,7 +125,7 @@ int32_t FfiInputMethodGetSystemInputMethodConfigAbility(CElementName *elem) return ERR_NO_MEMORY; } int32_t ret = ctrl->GetInputMethodConfig(inputMethodConfig); - if (ret == ErrorCode::NO_ERROR) { + if (ret == ErrorCode::NO_ERROR && elem != nullptr) { elem->deviceId = Utils::MallocCString(inputMethodConfig.GetDeviceID()); elem->bundleName = Utils::MallocCString(inputMethodConfig.GetBundleName()); elem->abilityName = Utils::MallocCString(inputMethodConfig.GetAbilityName()); diff --git a/frameworks/cj/src/utils.cpp b/frameworks/cj/src/utils.cpp index 7eccb76a09e11196c5cb8f8051cbd96d1d26ef2e..76dd91e43f903fe2ea045c8646a35bdc7acf3491 100644 --- a/frameworks/cj/src/utils.cpp +++ b/frameworks/cj/src/utils.cpp @@ -103,6 +103,10 @@ char* Utils::MallocCString(const std::string &origin) void Utils::InputMethodProperty2C(CInputMethodProperty *props, const Property &property) { + if (props == nullptr) { + IMSA_HILOGE("props is nullptr."); + return; + } props->name = Utils::MallocCString(property.name); props->id = Utils::MallocCString(property.id); props->label = Utils::MallocCString(property.label); @@ -125,6 +129,10 @@ Property Utils::C2InputMethodProperty(CInputMethodProperty props) void Utils::InputMethodSubProperty2C(CInputMethodSubtype *props, const SubProperty &property) { + if (props == nullptr) { + IMSA_HILOGE("props is nullptr."); + return; + } props->name = Utils::MallocCString(property.name); props->id = Utils::MallocCString(property.id); props->label = Utils::MallocCString(property.label); diff --git a/frameworks/js/napi/common/js_callback_handler.h b/frameworks/js/napi/common/js_callback_handler.h index 95a245a8ec332b9bb5f406c38cf888521c14763e..241e2d911eac54598c4cf1431ce66ea573e8cd64 100644 --- a/frameworks/js/napi/common/js_callback_handler.h +++ b/frameworks/js/napi/common/js_callback_handler.h @@ -51,6 +51,9 @@ public: { InputMethodSyncTrace tracer("Traverse callback with output"); for (const auto &object : objects) { + if (object == nullptr) { + continue; + } JsUtil::ScopeGuard scopeGuard(object->env_); napi_value jsOutput = nullptr; Execute(object, argContainer, jsOutput); diff --git a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp index 49224328d7c97f0a19bd9c1e482cd095ab3a53f2..b17dbe5efc0359f5cf1946be5e42b575c0e305ff 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -375,6 +375,9 @@ void JsInputMethodEngineSetting::RegisterListener(napi_value callback, std::stri } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 6a5da5bcf3d23790e5238c1416e5dc88ae749d48..8417230c82ca58ee4bfb9037c0e159bdcb2bb543 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -177,6 +177,9 @@ void JsKeyboardDelegateSetting::RegisterListener(napi_value callback, std::strin } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp index f10a65a73c665fe961ba507c45af54ae77f480bb..4ee924146aeb398eb47775f9c712ce6b1f08d9ad 100644 --- a/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp +++ b/frameworks/js/napi/inputmethodability/js_text_input_client_engine.cpp @@ -1062,6 +1062,9 @@ void JsTextInputClientEngine::RegisterListener(napi_value callback, std::string } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { 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 abb5dba04e0af0fe942542b1c81be6fed0b67099..572ef30776e8158159ce442afabbdd88cc7113b8 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_controller.cpp @@ -383,6 +383,9 @@ void JsGetInputMethodController::RegisterListener(napi_value callback, std::stri auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index 4b6701856d5e3d48dbc48b3bcdd30fd5ca09504e..fa0cee0128e728e313573ae050a27fa58cbe9a3a 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -574,6 +574,9 @@ int32_t JsGetInputMethodSetting::RegisterListener(napi_value callback, std::stri auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { 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 b55beb6d77a49c366f33036910a8461872be6f96..0fe58a8c0e3ccc76c4fb1a5a600d7fbf0e6dd532 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 @@ -39,52 +39,102 @@ sptr JsGetInputMethodTextChangedListener::G void JsGetInputMethodTextChangedListener::InsertText(const std::u16string &text) { - JsGetInputMethodController::GetInstance()->InsertText(text); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->InsertText(text); } void JsGetInputMethodTextChangedListener::DeleteForward(int32_t length) { - JsGetInputMethodController::GetInstance()->DeleteRight(length); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteRight(length); } void JsGetInputMethodTextChangedListener::DeleteBackward(int32_t length) { - JsGetInputMethodController::GetInstance()->DeleteLeft(length); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->DeleteLeft(length); } void JsGetInputMethodTextChangedListener::SendKeyboardStatus(const KeyboardStatus &status) { - JsGetInputMethodController::GetInstance()->SendKeyboardStatus(status); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendKeyboardStatus(status); } void JsGetInputMethodTextChangedListener::SendFunctionKey(const FunctionKey &functionKey) { - JsGetInputMethodController::GetInstance()->SendFunctionKey(functionKey); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->SendFunctionKey(functionKey); } void JsGetInputMethodTextChangedListener::MoveCursor(const Direction direction) { - JsGetInputMethodController::GetInstance()->MoveCursor(direction); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->MoveCursor(direction); } void JsGetInputMethodTextChangedListener::HandleExtendAction(int32_t action) { - JsGetInputMethodController::GetInstance()->HandleExtendAction(action); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + controller->HandleExtendAction(action); } std::u16string JsGetInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number) { - return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor", number); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetText("getLeftTextOfCursor", number); } std::u16string JsGetInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number) { - return JsGetInputMethodController::GetInstance()->GetText("getRightTextOfCursor", number); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return std::u16string(); + } + return controller->GetText("getRightTextOfCursor", number); } int32_t JsGetInputMethodTextChangedListener::GetTextIndexAtCursor() { - return JsGetInputMethodController::GetInstance()->GetTextIndexAtCursor(); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return controller->GetTextIndexAtCursor(); } int32_t JsGetInputMethodTextChangedListener::ReceivePrivateCommand( @@ -100,12 +150,22 @@ bool JsGetInputMethodTextChangedListener::IsFromTs() int32_t JsGetInputMethodTextChangedListener::SetPreviewText(const std::u16string &text, const Range &range) { - return JsGetInputMethodController::GetInstance()->SetPreviewText(text, range); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return ErrorCode::ERROR_NULL_POINTER; + } + return controller->SetPreviewText(text, range); } void JsGetInputMethodTextChangedListener::FinishTextPreview() { - return JsGetInputMethodController::GetInstance()->FinishTextPreview(); + auto controller = JsGetInputMethodController::GetInstance(); + if (controller == nullptr) { + IMSA_HILOGE("controller is nullptr!"); + return; + } + return controller->FinishTextPreview(); } std::shared_ptr JsGetInputMethodTextChangedListener::GetEventHandler() diff --git a/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp index 1cb5c03bacf41a089aae2d4d544e1f001b674a60..c2f06211bcf7de0fac0b130bfd8d402a52fdd34b 100644 --- a/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp +++ b/frameworks/js/napi/keyboardpanelmanager/js_keyboard_panel_manager.cpp @@ -77,7 +77,13 @@ napi_value JsKeyboardPanelManager::ConnectSystemCmd(napi_env env, napi_callback_ auto ctxt = std::make_shared(); auto manager = JsKeyboardPanelManager::GetInstance(); auto exec = [ctxt, env, manager](AsyncCall::Context *ctx) { - auto ret = ImeSystemCmdChannel::GetInstance()->ConnectSystemCmd(manager); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetErrorCode(ErrorCode::ERROR_NULL_POINTER); + ctxt->SetState(napi_generic_failure); + return; + } + auto ret = channel->ConnectSystemCmd(manager); ctxt->SetErrorCode(ret); CHECK_RETURN_VOID(ret == ErrorCode::NO_ERROR, "ConnectSystemCmd return error!"); ctxt->SetState(napi_ok); @@ -149,6 +155,9 @@ void JsKeyboardPanelManager::RegisterListener(napi_value callback, std::string t } auto callbacks = jsCbMap_[type]; bool ret = std::any_of(callbacks.begin(), callbacks.end(), [&callback](std::shared_ptr cb) { + if (cb == nullptr) { + return false; + }; return JsUtils::Equals(cb->env_, callback, cb->callback_, cb->threadId_); }); if (ret) { @@ -195,7 +204,12 @@ napi_value JsKeyboardPanelManager::GetSmartMenuCfg(napi_env env, napi_callback_i return napi_ok; }; auto exec = [ctxt](AsyncCall::Context *ctx) { - ctxt->smartMenu = ImeSystemCmdChannel::GetInstance()->GetSmartMenuCfg(); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetState(napi_generic_failure); + return; + } + ctxt->smartMenu = channel->GetSmartMenuCfg(); ctxt->SetState(napi_ok); }; ctxt->SetAction(nullptr, std::move(output)); @@ -222,7 +236,12 @@ napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callbac auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; }; auto exec = [ctxt](AsyncCall::Context *ctx) { privateCommandQueue_.Wait(ctxt->info); - int32_t code = ImeSystemCmdChannel::GetInstance()->SendPrivateCommand(ctxt->privateCommand); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + ctxt->SetState(napi_generic_failure); + return; + } + int32_t code = channel->SendPrivateCommand(ctxt->privateCommand); privateCommandQueue_.Pop(); if (code == ErrorCode::NO_ERROR) { ctxt->SetState(napi_ok); @@ -239,7 +258,11 @@ napi_value JsKeyboardPanelManager::SendPrivateCommand(napi_env env, napi_callbac napi_value JsKeyboardPanelManager::GetDefaultInputMethod(napi_env env, napi_callback_info info) { std::shared_ptr property; - int32_t ret = ImeSystemCmdChannel::GetInstance()->GetDefaultImeCfg(property); + auto channel = ImeSystemCmdChannel::GetInstance(); + if (channel == nullptr) { + return nullptr; + } + int32_t ret = channel->GetDefaultImeCfg(property); if (ret != ErrorCode::NO_ERROR || property == nullptr) { IMSA_HILOGE("GetDefaultImeCfg failed or property is nullptr ret: %{public}d!", ret); return nullptr; diff --git a/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp b/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp index 74ac1deb1ebdd19362cbb9e2cc795a591d12697f..3df25760bd5fae7f7e7c388b2894950bb3b98ddb 100644 --- a/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp +++ b/frameworks/native/inputmethod_controller/src/system_cmd_channel_service_impl.cpp @@ -33,12 +33,16 @@ ErrCode SystemCmdChannelServiceImpl::SendPrivateCommand(const Value &value) { std::unordered_map privateCommand; privateCommand = value.valueMap; - return ImeSystemCmdChannel::GetInstance()->ReceivePrivateCommand(privateCommand); + auto channel = ImeSystemCmdChannel::GetInstance(); + return (channel == nullptr) ? + ErrorCode::ERROR_NULL_POINTER : channel->ReceivePrivateCommand(privateCommand); } ErrCode SystemCmdChannelServiceImpl::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus) { - return ImeSystemCmdChannel::GetInstance()->NotifyPanelStatus(sysPanelStatus); + auto channel = ImeSystemCmdChannel::GetInstance(); + return (channel == nullptr) ? + ErrorCode::ERROR_NULL_POINTER : channel->NotifyPanelStatus(sysPanelStatus); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file