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 7d95e9c7fae82079aabe60014648733b9149accf..178ffeb328668615635053db2c89bb7c54d0e60c 100644 --- a/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_input_method_engine_setting.cpp @@ -190,7 +190,7 @@ std::shared_ptr JsInputMethodEngineSetting::GetInput bool JsInputMethodEngineSetting::InitInputMethodSetting() { - if (InputMethodAbility::GetInstance()->SetCoreAndAgent() != ErrorCode::NO_ERROR) { + if (!InputMethodAbility::GetInstance()->IsCurrentIme()) { return false; } auto engine = GetInputMethodEngineSetting(); diff --git a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp index 4b937dd9962890ef26a28737b1cc3d8b36724259..8aeaf27a33bc40c7748468166527bc82eafb52eb 100644 --- a/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp +++ b/frameworks/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp @@ -101,7 +101,7 @@ std::shared_ptr JsKeyboardDelegateSetting::GetKeyboar bool JsKeyboardDelegateSetting::InitKeyboardDelegate() { - if (InputMethodAbility::GetInstance()->SetCoreAndAgent() != ErrorCode::NO_ERROR) { + if (!InputMethodAbility::GetInstance()->IsCurrentIme()) { return false; } auto delegate = GetKeyboardDelegateSetting(); diff --git a/frameworks/js/napi/inputmethodclient/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index fa117546b000adf1c5034bd23a4cc391bf6e8672..af4876744d639671071156a621f92d644253559c 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -48,7 +48,6 @@ const std::map JsUtils::ERROR_CODE_MAP = { { ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT, EXCEPTION_IMMS }, { ErrorCode::ERROR_EX_ILLEGAL_STATE, EXCEPTION_IMMS }, { ErrorCode::ERROR_NOT_IME, EXCEPTION_IME }, - { ErrorCode::ERROR_IME_NOT_READY, EXCEPTION_IMENGINE }, { ErrorCode::ERROR_IME, EXCEPTION_IMENGINE }, }; diff --git a/frameworks/kits/extension/src/js_inputmethod_extension.cpp b/frameworks/kits/extension/src/js_inputmethod_extension.cpp index 6dbe9b939f290c9e42adbe160063209d8b77ce95..7e7dfd1b0f035399e2581366c46a003e3e8260e0 100644 --- a/frameworks/kits/extension/src/js_inputmethod_extension.cpp +++ b/frameworks/kits/extension/src/js_inputmethod_extension.cpp @@ -166,7 +166,6 @@ void JsInputMethodExtension::OnStart(const AAFwk::Want &want) StartAsync("onCreate", static_cast(TraceTaskId::ONCREATE_EXTENSION)); CallObjectMethod("onCreate", argv, ARGC_ONE); FinishAsync("onCreate", static_cast(TraceTaskId::ONCREATE_EXTENSION)); - InputMethodAbility::GetInstance()->OnImeReady(); auto ret = InputMethodAbility::GetInstance()->SetCoreAndAgent(); IMSA_HILOGI("ime bind imf ret: %{public}d", ret); FinishAsync("OnStart", static_cast(TraceTaskId::ONSTART_EXTENSION)); diff --git a/frameworks/native/inputmethod_ability/BUILD.gn b/frameworks/native/inputmethod_ability/BUILD.gn index 53ad6b3c01b23b10ce46c42b2c2cc131bc97c17d..b41590f178a6af5a2155ac61c429dbfcdbb42013 100644 --- a/frameworks/native/inputmethod_ability/BUILD.gn +++ b/frameworks/native/inputmethod_ability/BUILD.gn @@ -41,8 +41,10 @@ ohos_shared_library("inputmethod_ability") { sources = [ "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_client_proxy.cpp", "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_data_channel_proxy.cpp", + "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_death_recipient.cpp", "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp", "${inputmethod_path}/frameworks/native/inputmethod_controller/src/itypes_util.cpp", + "${inputmethod_path}/services/src/global.cpp", "${inputmethod_path}/services/src/input_channel.cpp", "${inputmethod_path}/services/src/input_control_channel_proxy.cpp", "${inputmethod_path}/services/src/message.cpp", diff --git a/frameworks/native/inputmethod_ability/include/input_method_ability.h b/frameworks/native/inputmethod_ability/include/input_method_ability.h index b929b68f51264ea9f9be4931b095a8a7d5eb289b..da18441562ff89f722d2bfd53e8fbfcad46210b6 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/native/inputmethod_ability/include/input_method_ability.h @@ -72,14 +72,14 @@ public: int32_t GetInputPattern(int32_t &inputPattern); int32_t GetTextIndexAtCursor(int32_t &index); int32_t GetTextConfig(TextTotalConfig &textConfig); - void OnImeReady(); int32_t CreatePanel(const std::shared_ptr &context, const PanelInfo &panelInfo, std::shared_ptr &inputMethodPanel); int32_t DestroyPanel(const std::shared_ptr &inputMethodPanel); + bool IsCurrentIme(); private: std::thread workThreadHandler; - MessageHandler *msgHandler; + MessageHandler *msgHandler_; bool stop_ = false; int32_t KEYBOARD_HIDE = 1; int32_t KEYBOARD_SHOW = 2; @@ -95,14 +95,12 @@ private: static std::mutex instanceLock_; static sptr instance_; - sptr mImms; - struct ServiceDeathRecipient : public IRemoteObject::DeathRecipient { - std::shared_ptr listener{ nullptr }; - void OnRemoteDied(const wptr &object) override; - std::string currentIme_; - }; - sptr deathRecipientPtr_{ nullptr }; - sptr GetImsaProxy(); + std::mutex abilityLock_; + sptr abilityManager_{ nullptr }; + sptr deathRecipient_{ nullptr }; + std::string currentIme_; + sptr GetImsaProxy(); + void OnRemoteSaDied(const wptr &object); void SetInputDataChannel(const sptr &object); std::shared_ptr GetInputDataChannelProxy(); @@ -121,9 +119,9 @@ private: void OnConfigurationChange(Message *msg); int32_t ShowInputWindow(bool isShowKeyboard); void OnTextConfigChange(const TextTotalConfig &textConfig); - bool isImeReady_{ false }; - InputStartNotifier notifier_; + int32_t ShowPanelKeyboard(); ConcurrentMap> panels_{}; + std::atomic_bool isPanelKeyboard_{ false }; std::atomic_bool isBound_{ false }; }; } // namespace MiscServices diff --git a/frameworks/native/inputmethod_ability/include/input_method_core_stub.h b/frameworks/native/inputmethod_ability/include/input_method_core_stub.h index 2a5e25e87d766da2ae5124a3946d8cabc8286144..dac6c73a2477317f265a7f83995d4f33912e2e46 100644 --- a/frameworks/native/inputmethod_ability/include/input_method_core_stub.h +++ b/frameworks/native/inputmethod_ability/include/input_method_core_stub.h @@ -36,7 +36,7 @@ namespace MiscServices { class InputMethodCoreStub : public IRemoteStub { public: DISALLOW_COPY_AND_MOVE(InputMethodCoreStub); - explicit InputMethodCoreStub(int userId); + InputMethodCoreStub(); virtual ~InputMethodCoreStub(); int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; int32_t ShowKeyboard( @@ -49,7 +49,6 @@ public: void SetMessageHandler(MessageHandler *msgHandler); private: - int32_t userId_ = -1; MessageHandler *msgHandler_; void InitInputControlChannelOnRemote(MessageParcel &data, MessageParcel &reply); void SetSubtypeOnRemote(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index 7759ba0b9ac87615485b6e9bf044b4e7d95bc918..ca8077dcdae413c86ead0f5afd508478c2ac47c0 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -39,15 +39,19 @@ sptr InputMethodAbility::instance_; std::mutex InputMethodAbility::instanceLock_; constexpr double INVALID_CURSOR_VALUE = -1.0; constexpr int32_t INVALID_SELECTION_VALUE = -1; -InputMethodAbility::InputMethodAbility() : msgHandler(nullptr), stop_(false) {} +constexpr uint32_t FIND_PANEL_RETRY_INTERVAL = 10; +constexpr uint32_t MAX_RETRY_TIMES = 100; +InputMethodAbility::InputMethodAbility() : msgHandler_(nullptr), stop_(false) +{ +} InputMethodAbility::~InputMethodAbility() { IMSA_HILOGI("InputMethodAbility::~InputMethodAbility"); QuitWorkThread(); - if (msgHandler != nullptr) { - delete msgHandler; - msgHandler = nullptr; + if (msgHandler_ != nullptr) { + delete msgHandler_; + msgHandler_ = nullptr; } } @@ -68,34 +72,38 @@ sptr InputMethodAbility::GetInstance() return instance_; } -sptr InputMethodAbility::GetImsaProxy() +sptr InputMethodAbility::GetImsaProxy() { - IMSA_HILOGI("InputMethodAbility::GetImsaProxy"); + std::lock_guard lock(abilityLock_); + if (abilityManager_ != nullptr) { + return abilityManager_; + } + IMSA_HILOGD("InputMethodAbility get imsa proxy"); sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemAbilityManager == nullptr) { - IMSA_HILOGI("InputMethodAbility::GetImsaProxy systemAbilityManager is nullptr"); + IMSA_HILOGE("systemAbilityManager is nullptr"); return nullptr; } - auto systemAbility = systemAbilityManager->GetSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID, ""); if (systemAbility == nullptr) { - IMSA_HILOGI("InputMethodAbility::GetImsaProxy systemAbility is nullptr"); + IMSA_HILOGE("systemAbility is nullptr"); return nullptr; } - if (deathRecipientPtr_ == nullptr) { - deathRecipientPtr_ = new (std::nothrow) ServiceDeathRecipient(); - if (deathRecipientPtr_ == nullptr) { - IMSA_HILOGE("new ServiceDeathRecipient failed"); + if (deathRecipient_ == nullptr) { + deathRecipient_ = new (std::nothrow) InputDeathRecipient(); + if (deathRecipient_ == nullptr) { + IMSA_HILOGE("failed to new death recipient"); return nullptr; } } - if ((systemAbility->IsProxyObject()) && (!systemAbility->AddDeathRecipient(deathRecipientPtr_))) { + deathRecipient_->SetDeathRecipient([this](const wptr &remote) { OnRemoteSaDied(remote); }); + if ((systemAbility->IsProxyObject()) && (!systemAbility->AddDeathRecipient(deathRecipient_))) { IMSA_HILOGE("failed to add death recipient."); + return nullptr; } - - sptr iface = new InputMethodSystemAbilityProxy(systemAbility); - return iface; + abilityManager_ = iface_cast(systemAbility); + return abilityManager_; } int32_t InputMethodAbility::SetCoreAndAgent() @@ -105,18 +113,25 @@ int32_t InputMethodAbility::SetCoreAndAgent() IMSA_HILOGD("already bound"); return ErrorCode::NO_ERROR; } - mImms = GetImsaProxy(); - if (mImms == nullptr) { - IMSA_HILOGI("InputMethodAbility mImms is nullptr"); + auto proxy = GetImsaProxy(); + if (proxy == nullptr) { + IMSA_HILOGE("imsa proxy is nullptr"); return ErrorCode::ERROR_NULL_POINTER; } - sptr stub = new InputMethodCoreStub(0); - stub->SetMessageHandler(msgHandler); - - sptr inputMethodAgentStub(new InputMethodAgentStub()); - inputMethodAgentStub->SetMessageHandler(msgHandler); - sptr inputMethodAgent = sptr(new InputMethodAgentProxy(inputMethodAgentStub)); - int32_t ret = mImms->SetCoreAndAgent(stub, inputMethodAgent); + auto coreStub = new (std::nothrow) InputMethodCoreStub(); + if (coreStub == nullptr) { + IMSA_HILOGE("failed to create core"); + return ErrorCode::ERROR_NULL_POINTER; + } + auto agentStub = new (std::nothrow) InputMethodAgentStub(); + if (agentStub == nullptr) { + IMSA_HILOGE("failed to create agent"); + delete coreStub; + return ErrorCode::ERROR_NULL_POINTER; + } + coreStub->SetMessageHandler(msgHandler_); + agentStub->SetMessageHandler(msgHandler_); + int32_t ret = proxy->SetCoreAndAgent(coreStub, agentStub); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("set failed, ret: %{public}d", ret); return ret; @@ -129,7 +144,11 @@ int32_t InputMethodAbility::SetCoreAndAgent() void InputMethodAbility::Initialize() { IMSA_HILOGI("InputMethodAbility::Initialize"); - msgHandler = new MessageHandler(); + msgHandler_ = new (std::nothrow) MessageHandler(); + if (msgHandler_ == nullptr) { + IMSA_HILOGE("failed to create message handler"); + return; + } workThreadHandler = std::thread([this] { WorkThread(); }); } @@ -139,24 +158,6 @@ void InputMethodAbility::SetImeListener(std::shared_ptrlistener == nullptr) { - deathRecipientPtr_->listener = imeListener_; - } -} - -void InputMethodAbility::OnImeReady() -{ - isImeReady_ = true; - if (!notifier_.isNotify) { - IMSA_HILOGI("InputMethodAbility::Ime Ready, don't need to notify"); - return; - } - IMSA_HILOGI("InputMethodAbility::Ime Ready, notify InputStart"); - TextTotalConfig textConfig{}; - int32_t ret = GetTextConfig(textConfig); - IMSA_HILOGI("InputMethodAbility, get text config failed, ret is %{public}d", ret); - OnTextConfigChange(textConfig); - ShowInputWindow(notifier_.isShowKeyboard); } void InputMethodAbility::SetKdListener(std::shared_ptr kdListener) @@ -171,7 +172,7 @@ void InputMethodAbility::WorkThread() { prctl(PR_SET_NAME, "IMAWorkThread"); while (!stop_) { - Message *msg = msgHandler->GetMessage(); + Message *msg = msgHandler_->GetMessage(); switch (msg->msgId_) { case MSG_ID_INIT_INPUT_CONTROL_CHANNEL: { OnInitInputControlChannel(msg); @@ -221,9 +222,7 @@ void InputMethodAbility::OnInitInputControlChannel(Message *msg) IMSA_HILOGI("InputMethodAbility::OnInitInputControlChannel channelObject is nullptr"); return; } - if (deathRecipientPtr_ != nullptr) { - deathRecipientPtr_->currentIme_ = data->ReadString(); - } + currentIme_ = data->ReadString(); SetInputControlChannel(channelObject); } @@ -357,13 +356,7 @@ void InputMethodAbility::OnConfigurationChange(Message *msg) int32_t InputMethodAbility::ShowInputWindow(bool isShowKeyboard) { - IMSA_HILOGI("InputMethodAbility::ShowInputWindow"); - if (!isImeReady_) { - IMSA_HILOGE("InputMethodAbility::ime is unready, store notifier_"); - notifier_.isNotify = true; - notifier_.isShowKeyboard = isShowKeyboard; - return ErrorCode::ERROR_IME_NOT_READY; - } + IMSA_HILOGI("run in, isShowKeyboard: %{public}d", isShowKeyboard); if (imeListener_ == nullptr) { IMSA_HILOGE("InputMethodAbility, imeListener is nullptr"); return ErrorCode::ERROR_IME; @@ -374,27 +367,44 @@ int32_t InputMethodAbility::ShowInputWindow(bool isShowKeyboard) return ErrorCode::NO_ERROR; } imeListener_->OnKeyboardStatus(true); + if (isPanelKeyboard_.load()) { + auto ret = ShowPanelKeyboard(); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + } auto channel = GetInputDataChannelProxy(); if (channel == nullptr) { - IMSA_HILOGE("InputMethodAbility::ShowInputWindow channel is nullptr"); + IMSA_HILOGE("channel is nullptr"); return ErrorCode::ERROR_CLIENT_NULL_POINTER; } channel->SendKeyboardStatus(KEYBOARD_SHOW); + return ErrorCode::NO_ERROR; +} + +int32_t InputMethodAbility::ShowPanelKeyboard() +{ + if (!BlockRetry(FIND_PANEL_RETRY_INTERVAL, MAX_RETRY_TIMES, + [this]() -> bool { return panels_.Find(SOFT_KEYBOARD).first; })) { + IMSA_HILOGE("SOFT_KEYBOARD panel not found"); + return ErrorCode::ERROR_OPERATE_PANEL; + } auto result = panels_.Find(SOFT_KEYBOARD); - if (result.first) { - IMSA_HILOGI("find SOFT_KEYBOARD panel."); - auto panel = result.second; - if (panel->GetPanelFlag() == PanelFlag::FLG_CANDIDATE_COLUMN) { - IMSA_HILOGD("panel flag is candidate, not need to show."); - return ErrorCode::NO_ERROR; - } - auto ret = panel->ShowPanel(); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); - } - return ret; + if (!result.first) { + IMSA_HILOGE("SOFT_KEYBOARD panel not found"); + return ErrorCode::ERROR_OPERATE_PANEL; } - return ErrorCode::NO_ERROR; + IMSA_HILOGI("find SOFT_KEYBOARD panel."); + auto panel = result.second; + if (panel->GetPanelFlag() == PanelFlag::FLG_CANDIDATE_COLUMN) { + IMSA_HILOGD("panel flag is candidate, not need to show."); + return ErrorCode::NO_ERROR; + } + auto ret = panel->ShowPanel(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("Show panel failed, ret = %{public}d.", ret); + } + return ret; } void InputMethodAbility::OnTextConfigChange(const TextTotalConfig &textConfig) @@ -660,11 +670,15 @@ std::shared_ptr InputMethodAbility::GetInputControlCha return controlChannel_; } -void InputMethodAbility::ServiceDeathRecipient::OnRemoteDied(const wptr &object) +void InputMethodAbility::OnRemoteSaDied(const wptr &object) { - IMSA_HILOGI("ServiceDeathRecipient::OnRemoteDied"); - if (listener != nullptr) { - listener->OnInputStop(currentIme_); + IMSA_HILOGI("input method service died"); + { + std::lock_guard lock(abilityLock_); + abilityManager_ = nullptr; + } + if (imeListener_ != nullptr) { + imeListener_->OnInputStop(currentIme_); } } @@ -672,7 +686,7 @@ void InputMethodAbility::QuitWorkThread() { stop_ = true; Message *msg = new Message(MessageID::MSG_ID_QUIT_WORKER_THREAD, nullptr); - msgHandler->SendMessage(msg); + msgHandler_->SendMessage(msg); if (workThreadHandler.joinable()) { workThreadHandler.join(); } @@ -682,6 +696,10 @@ int32_t InputMethodAbility::CreatePanel(const std::shared_ptr &inputMethodPanel) { IMSA_HILOGI("InputMethodAbility::CreatePanel start."); + bool isSoftKeyboard = panelInfo.panelType == PanelType::SOFT_KEYBOARD; + if (isSoftKeyboard) { + isPanelKeyboard_.store(true); + } auto flag = panels_.ComputeIfAbsent(panelInfo.panelType, [&panelInfo, &context, &inputMethodPanel](const PanelType &panelType, std::shared_ptr &panel) { @@ -694,7 +712,11 @@ int32_t InputMethodAbility::CreatePanel(const std::shared_ptr &inputMethodPanel) @@ -704,11 +726,25 @@ int32_t InputMethodAbility::DestroyPanel(const std::shared_ptr return ErrorCode::ERROR_BAD_PARAMETERS; } PanelType panelType = inputMethodPanel->GetPanelType(); + if (panelType == PanelType::SOFT_KEYBOARD) { + isPanelKeyboard_.store(false); + } auto ret = inputMethodPanel->DestroyPanel(); if (ret == ErrorCode::NO_ERROR) { panels_.Erase(panelType); } return ret; } + +bool InputMethodAbility::IsCurrentIme() +{ + IMSA_HILOGD("InputMethodAbility, in"); + auto proxy = GetImsaProxy(); + if (proxy == nullptr) { + IMSA_HILOGE("failed to get imsa proxy"); + return false; + } + return proxy->IsCurrentIme(); +} } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp b/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp index 481d4af3a9b367ce7d636a9a39d45270dc2e0b3b..2d67e91f8248223ab4bfbc07bd89d9826cb337a3 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_core_stub.cpp @@ -28,9 +28,8 @@ namespace OHOS { namespace MiscServices { using namespace MessageID; -InputMethodCoreStub::InputMethodCoreStub(int userId) +InputMethodCoreStub::InputMethodCoreStub() { - userId_ = userId; msgHandler_ = nullptr; } diff --git a/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h b/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h index dc324d6f7144a050a1091f12fc8e94644ea170da..f72156718cbb384357e00cc36650f28a6de19cad 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/native/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -55,11 +55,12 @@ public: int32_t ListInputMethod(InputMethodStatus status, std::vector &props) override; int32_t SwitchInputMethod(const std::string &name, const std::string &subName) override; int32_t DisplayOptionalInputMethod() override; - int32_t SetCoreAndAgent(sptr core, sptr agent) override; + int32_t SetCoreAndAgent(const sptr &core, const sptr &agent) override; int32_t ListCurrentInputMethodSubtype(std::vector &subProps) override; int32_t ListInputMethodSubtype(const std::string &name, std::vector &subProps) override; int32_t PanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo) override; int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType) override; + bool IsCurrentIme() override; // Deprecated because of no permission check, kept for compatibility int32_t HideCurrentInputDeprecated() override; diff --git a/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp b/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp index 24557273a7fbf0ffe3fa3ec9812f6624adaa44ad..2837e13d8af931a9a7566006938d825545b5331c 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -86,7 +86,8 @@ int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethod() return SendRequest(static_cast(InputMethodInterfaceCode::DISPLAY_OPTIONAL_INPUT_METHOD)); } -int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent(sptr core, sptr agent) +int32_t InputMethodSystemAbilityProxy::SetCoreAndAgent( + const sptr &core, const sptr &agent) { IMSA_HILOGD("%{public}s in", __func__); return SendRequest( @@ -201,6 +202,14 @@ int32_t InputMethodSystemAbilityProxy::UpdateListenEventFlag(InputClientInfo &cl }); } +bool InputMethodSystemAbilityProxy::IsCurrentIme() +{ + bool isCurrentIme = false; + SendRequest(static_cast(InputMethodInterfaceCode::IS_CURRENT_IME), nullptr, + [&isCurrentIme](MessageParcel &reply) { return ITypesUtil::Unmarshal(reply, isCurrentIme); }); + return isCurrentIme; +} + int32_t InputMethodSystemAbilityProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output) { IMSA_HILOGD("InputMethodSystemAbilityProxy run in, code = %{public}d", code); diff --git a/services/include/global.h b/services/include/global.h index 1d12a192becf85494010c0ac40aacc91b0772765..5cd946b64a40878ab5d79f710e6b89d3e544c725 100644 --- a/services/include/global.h +++ b/services/include/global.h @@ -96,8 +96,7 @@ enum { ERROR_NOT_IME = 22, ERROR_ADD_DEATH_RECIPIENT_FAILED = 23, ERROR_STATUS_SYSTEM_PERMISSION = 24, // not system application - ERROR_IME_NOT_READY = 25, - ERROR_IME = 26, + ERROR_IME = 25, }; }; // namespace ErrorCode diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index e2edd18e948e2e816a0af0098e3fe9e9cdd3f8ba..0c6f72d58232a6ac1e23ed72bd0611df4ab81ce1 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -51,12 +51,13 @@ public: virtual std::shared_ptr GetCurrentInputMethodSubtype() = 0; virtual int32_t ListInputMethod(InputMethodStatus status, std::vector &props) = 0; virtual int32_t DisplayOptionalInputMethod() = 0; - virtual int32_t SetCoreAndAgent(sptr core, sptr agent) = 0; + virtual int32_t SetCoreAndAgent(const sptr &core, const sptr &agent) = 0; virtual int32_t ListCurrentInputMethodSubtype(std::vector &subProps) = 0; virtual int32_t ListInputMethodSubtype(const std::string &name, std::vector &subProps) = 0; virtual int32_t SwitchInputMethod(const std::string &bundleName, const std::string &name) = 0; virtual int32_t PanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo) = 0; virtual int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType) = 0; + virtual bool IsCurrentIme() = 0; // Deprecated because of no permission check, and keep for compatibility virtual int32_t HideCurrentInputDeprecated() = 0; diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index c646667adba0d106490c7f7002fcfb3ee4b71e2d..ecf0e0dd86725d6209a1fb9170a13c29c7d055b2 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -73,9 +73,10 @@ public: int32_t ListInputMethodSubtype(const std::string &bundleName, std::vector &subProps) override; int32_t SwitchInputMethod(const std::string &bundleName, const std::string &subName) override; int32_t DisplayOptionalInputMethod() override; - int32_t SetCoreAndAgent(sptr core, sptr agent) override; + int32_t SetCoreAndAgent(const sptr &core, const sptr &agent) override; int32_t PanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo) override; int32_t UpdateListenEventFlag(InputClientInfo &clientInfo, EventType eventType) override; + bool IsCurrentIme() override; // Deprecated because of no permission check, kept for compatibility int32_t HideCurrentInputDeprecated() override; diff --git a/services/include/input_method_system_ability_stub.h b/services/include/input_method_system_ability_stub.h index 517876ce04b6e33937a3b752c6abb05536fd6460..8a49c8e87b79ee55836363ee88ad6f82001ebc9b 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -65,6 +65,8 @@ private: int32_t UpdateListenEventFlagOnRemote(MessageParcel &data, MessageParcel &reply); + int32_t IsCurrentImeOnRemote(MessageParcel &data, MessageParcel &reply); + // Deprecated because of no permission check, kept for compatibility int32_t DisplayInputOnRemoteDeprecated(MessageParcel &data, MessageParcel &reply); @@ -114,6 +116,8 @@ private: &InputMethodSystemAbilityStub::PanelStatusChangeOnRemote, [static_cast(InputMethodInterfaceCode::UPDATE_LISTEN_EVENT_FLAG)] = &InputMethodSystemAbilityStub::UpdateListenEventFlagOnRemote, + [static_cast(InputMethodInterfaceCode::IS_CURRENT_IME)] = + &InputMethodSystemAbilityStub::IsCurrentImeOnRemote, }; }; } // namespace OHOS::MiscServices diff --git a/services/include/inputmethod_service_ipc_interface_code.h b/services/include/inputmethod_service_ipc_interface_code.h index b2cae2557d19a92543f089e0e6ae10ef3e47537c..514df79ec4745b2d343c113387d7013f367441dd 100644 --- a/services/include/inputmethod_service_ipc_interface_code.h +++ b/services/include/inputmethod_service_ipc_interface_code.h @@ -40,6 +40,7 @@ enum class InputMethodInterfaceCode { DISPLAY_OPTIONAL_INPUT_DEPRECATED, PANEL_STATUS_CHANGE, UPDATE_LISTEN_EVENT_FLAG, + IS_CURRENT_IME, IMS_CMD_LAST }; } // namespace MiscServices diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index fe3c45bd1a1890fe50a555fb7d6bf440438e909d..7be4c143ac4ecc24532c66320a951ac393150007 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -293,11 +293,11 @@ int32_t InputMethodSystemAbility::StopInputSession() return userSession_->OnHideKeyboardSelf(); } -int32_t InputMethodSystemAbility::SetCoreAndAgent(sptr core, sptr agent) +int32_t InputMethodSystemAbility::SetCoreAndAgent( + const sptr &core, const sptr &agent) { IMSA_HILOGD("InputMethodSystemAbility run in"); - auto currentImeCfg = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_); - if (!identityChecker_->IsCurrentIme(IPCSkeleton::GetCallingTokenID(), currentImeCfg->bundleName)) { + if (!IsCurrentIme()) { return ErrorCode::ERROR_NOT_CURRENT_IME; } if (core == nullptr || agent == nullptr) { @@ -305,7 +305,7 @@ int32_t InputMethodSystemAbility::SetCoreAndAgent(sptr core, s return ErrorCode::ERROR_NULL_POINTER; } return userSession_->OnSetCoreAndAgent(core, agent); -}; +} int32_t InputMethodSystemAbility::HideCurrentInput() { @@ -365,6 +365,12 @@ int32_t InputMethodSystemAbility::UpdateListenEventFlag(InputClientInfo &clientI return userSession_->OnUpdateListenEventFlag(clientInfo); } +bool InputMethodSystemAbility::IsCurrentIme() +{ + auto currentImeCfg = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_); + return identityChecker_->IsCurrentIme(IPCSkeleton::GetCallingTokenID(), currentImeCfg->bundleName); +} + int32_t InputMethodSystemAbility::DisplayOptionalInputMethod() { IMSA_HILOGD("InputMethodSystemAbility run in"); diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 5db90007a2c593ec43aebf91c3512f2c41126388..6dae50704fd398692cbcfb369f52dc0db3f8ef31 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -255,5 +255,11 @@ int32_t InputMethodSystemAbilityStub::DisplayInputOnRemoteDeprecated(MessageParc int32_t ret = DisplayOptionalInputMethodDeprecated(); return reply.WriteInt32(ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE; } + +int32_t InputMethodSystemAbilityStub::IsCurrentImeOnRemote(MessageParcel &data, MessageParcel &reply) +{ + bool ret = IsCurrentIme(); + return ITypesUtil::Marshal(reply, ErrorCode::NO_ERROR, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE; +} } // namespace MiscServices } // namespace OHOS diff --git a/test/fuzztest/corestub_fuzzer/corestub_fuzzer.cpp b/test/fuzztest/corestub_fuzzer/corestub_fuzzer.cpp index 346379759081f7243bbc0717e5895a27525f2a7e..870cf161f345692ab9c6c7e5d5e710ed3f1977a7 100644 --- a/test/fuzztest/corestub_fuzzer/corestub_fuzzer.cpp +++ b/test/fuzztest/corestub_fuzzer/corestub_fuzzer.cpp @@ -38,7 +38,6 @@ uint32_t ConvertToUint32(const uint8_t *ptr) } bool FuzzCoreStub(const uint8_t *rawData, size_t size) { - constexpr int32_t MAIN_USER_ID = 0; uint32_t code = ConvertToUint32(rawData); rawData = rawData + OFFSET; size = size - OFFSET; @@ -50,7 +49,7 @@ bool FuzzCoreStub(const uint8_t *rawData, size_t size) MessageParcel reply; MessageOption option; - sptr stub = new InputMethodCoreStub(MAIN_USER_ID); + sptr stub = new InputMethodCoreStub(); MessageHandler *handler = MessageHandler::Instance(); stub->SetMessageHandler(handler); stub->OnRemoteRequest(code, data, reply, option); diff --git a/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp b/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp index 26cbedf6f185b902c6f0a570b34e265390019fab..17b2353965d27601368327acc3398613581b3a1c 100644 --- a/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp +++ b/test/fuzztest/perusersession_fuzzer/perusersession_fuzzer.cpp @@ -80,7 +80,7 @@ bool FuzzPerUserSession(const uint8_t *rawData, size_t size) return false; } auto client = iface_cast(clientInfo.client->AsObject()); - sptr coreStub = new (std::nothrow) InputMethodCoreStub(MAIN_USER_ID); + sptr coreStub = new (std::nothrow) InputMethodCoreStub(); if (coreStub == nullptr) { return false; } diff --git a/test/unittest/cpp_test/src/identity_checker_test.cpp b/test/unittest/cpp_test/src/identity_checker_test.cpp index 42ed4b5acd1e041dd50a0a591077c68390eb683c..0127b8e62bf34cbd04d238bdbb7c93e72e52eda0 100644 --- a/test/unittest/cpp_test/src/identity_checker_test.cpp +++ b/test/unittest/cpp_test/src/identity_checker_test.cpp @@ -388,6 +388,36 @@ HWTEST_F(IdentityCheckerTest, testSetCoreAndAgent_002, TestSize.Level0) EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); } +/** + * @tc.name: testIsCurrentIme_001 + * @tc.desc: not current ime + * @tc.type: FUNC + * @tc.require: + * @tc.author: +*/ +HWTEST_F(IdentityCheckerTest, testIsCurrentIme_001, TestSize.Level0) +{ + IMSA_HILOGI("IdentityCheckerTest testIsCurrentIme_001 start"); + service_->identityChecker_ = identityCheckerImpl_; + bool ret = IdentityCheckerTest::service_->IsCurrentIme(); + EXPECT_FALSE(ret); +} + +/** + * @tc.name: testIsCurrentIme_002 + * @tc.desc: current ime + * @tc.type: FUNC + * @tc.require: + * @tc.author: +*/ +HWTEST_F(IdentityCheckerTest, testIsCurrentIme_002, TestSize.Level0) +{ + IMSA_HILOGI("IdentityCheckerTest testIsCurrentIme_002 start"); + EXPECT_CALL(*IdentityCheckerTest::identityCheckerMock_, IsCurrentIme(_, _)).Times(1).WillRepeatedly(Return(true)); + bool ret = IdentityCheckerTest::service_->IsCurrentIme(); + EXPECT_TRUE(ret); +} + /** * @tc.name: testHideCurrentInput_001 * @tc.desc: is broker diff --git a/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp b/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp index 3f9d36266ca9c78693b29dcf8352e74c67d45b7e..97303a47c34b56218a948e2c437a16f050bef087 100644 --- a/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_exception_test.cpp @@ -44,7 +44,6 @@ public: } static void ResetMemberVar() { - inputMethodAbility_->isImeReady_ = false; inputMethodAbility_->dataChannelProxy_ = nullptr; inputMethodAbility_->dataChannelObject_ = nullptr; inputMethodAbility_->imeListener_ = nullptr; @@ -263,11 +262,6 @@ HWTEST_F(InputMethodAbilityExceptionTest, testShowKeyboard_001, TestSize.Level0) auto ret = inputMethodAbility_->ShowKeyboard(nullptr, false, true); EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER); - // GetTextConfig failed - sptr channelObject = new InputDataChannelStub(); - ret = inputMethodAbility_->ShowKeyboard(channelObject->AsObject(), false, true); - EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_READY); - ResetMemberVar(); } @@ -281,13 +275,8 @@ HWTEST_F(InputMethodAbilityExceptionTest, testShowKeyboard_001, TestSize.Level0) HWTEST_F(InputMethodAbilityExceptionTest, testShowInputWindow_001, TestSize.Level0) { IMSA_HILOGI("InputMethodAbilityExceptionTest testShowInputWindow_001 START"); - // isImeReady_ is false - auto ret = inputMethodAbility_->ShowInputWindow(true); - EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_READY); - // imeListener_ == nullptr - inputMethodAbility_->isImeReady_ = true; - ret = inputMethodAbility_->ShowInputWindow(true); + auto ret = inputMethodAbility_->ShowInputWindow(true); EXPECT_EQ(ret, ErrorCode::ERROR_IME); // channel == nullptr @@ -305,13 +294,6 @@ HWTEST_F(InputMethodAbilityExceptionTest, testShowInputWindow_001, TestSize.Leve ret = inputMethodAbility_->ShowInputWindow(true); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - // ShowPanel failed - inputMethodAbility_->panels_.Clear(); - panel->panelFlag_ = FLG_FIXED; - inputMethodAbility_->panels_.Insert(SOFT_KEYBOARD, panel); - ret = inputMethodAbility_->ShowInputWindow(true); - EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); - ResetMemberVar(); } diff --git a/test/unittest/cpp_test/src/input_method_ability_test.cpp b/test/unittest/cpp_test/src/input_method_ability_test.cpp index 96e3836816829973844d9ae0f9bb85f3d2e9c4b8..76ec7fcf3f4bd04794cd9318a57ee913d64bbcbf 100644 --- a/test/unittest/cpp_test/src/input_method_ability_test.cpp +++ b/test/unittest/cpp_test/src/input_method_ability_test.cpp @@ -98,7 +98,6 @@ public: std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest"; TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); - inputMethodAbility_->OnImeReady(); inputMethodAbility_->SetCoreAndAgent(); TddUtil::RestoreSelfTokenID(); TextListener::ResetParam(); @@ -155,7 +154,7 @@ HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0) HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0) { IMSA_HILOGI("testShowKeyboardInputMethodCoreProxy start."); - sptr coreStub = new InputMethodCoreStub(0); + sptr coreStub = new InputMethodCoreStub(); sptr core = coreStub; auto msgHandler = new (std::nothrow) MessageHandler(); coreStub->SetMessageHandler(msgHandler); diff --git a/test/unittest/cpp_test/src/input_method_attach_test.cpp b/test/unittest/cpp_test/src/input_method_attach_test.cpp index f24b7d176157020fe3156f698308e0a420c0ef61..99d21b65317a93717ad4bb8a5655e457db496e9c 100644 --- a/test/unittest/cpp_test/src/input_method_attach_test.cpp +++ b/test/unittest/cpp_test/src/input_method_attach_test.cpp @@ -42,7 +42,6 @@ public: std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest"; TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); - inputMethodAbility_->OnImeReady(); inputMethodAbility_->SetCoreAndAgent(); inputMethodAbility_->SetImeListener(std::make_shared()); TddUtil::RestoreSelfTokenID(); diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index 3a2a70fe2f5d66cdfaf0c2d1468530723ae00668..06eb3fccbf748da4c485cc475dcadcb05f118f75 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -213,7 +213,6 @@ void InputMethodControllerTest::SetUpTestCase(void) TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); inputMethodAbility_->SetCoreAndAgent(); - inputMethodAbility_->OnImeReady(); imeListener_ = std::make_shared(); controllerListener_ = std::make_shared(); textListener_ = new TextListener(); diff --git a/test/unittest/cpp_test/src/input_method_dfx_test.cpp b/test/unittest/cpp_test/src/input_method_dfx_test.cpp index 5173906a8c1dd4b6ae8a68ccc41955dedd8ef08a..4bf5eaa4d89c6812aa3b8ba6b48c7ef8b0fdde8e 100644 --- a/test/unittest/cpp_test/src/input_method_dfx_test.cpp +++ b/test/unittest/cpp_test/src/input_method_dfx_test.cpp @@ -131,7 +131,6 @@ void InputMethodDfxTest::SetUpTestCase(void) TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); imeListener_ = std::make_shared(); - inputMethodAbility_->OnImeReady(); inputMethodAbility_->SetCoreAndAgent(); inputMethodAbility_->SetImeListener(imeListener_); diff --git a/test/unittest/cpp_test/src/input_method_editor_test.cpp b/test/unittest/cpp_test/src/input_method_editor_test.cpp index c8235e081159d522e202361ac60789266f62a08c..6ab2075cbeac3ab3db9eda8e899ad1e068d01035 100644 --- a/test/unittest/cpp_test/src/input_method_editor_test.cpp +++ b/test/unittest/cpp_test/src/input_method_editor_test.cpp @@ -115,7 +115,6 @@ void InputMethodEditorTest::SetUpTestCase(void) std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest"; TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName)); inputMethodAbility_ = InputMethodAbility::GetInstance(); - inputMethodAbility_->OnImeReady(); inputMethodAbility_->SetCoreAndAgent(); kbListener_ = std::make_shared(); imeListener_ = std::make_shared(); diff --git a/test/unittest/cpp_test/src/input_method_private_member_test.cpp b/test/unittest/cpp_test/src/input_method_private_member_test.cpp index feabba0e4e2bb14bdd002d19b5ac09ec44168058..98e764a5ba1acd197ab0da137f9504ac7e24943d 100644 --- a/test/unittest/cpp_test/src/input_method_private_member_test.cpp +++ b/test/unittest/cpp_test/src/input_method_private_member_test.cpp @@ -292,7 +292,7 @@ HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionClientError, TestSize.Level IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionClientError TEST START"); auto userSession = std::make_shared(MAIN_USER_ID); auto imc = InputMethodController::GetInstance(); - sptr core = new InputMethodCoreStub(0); + sptr core = new InputMethodCoreStub(); auto clientInfo = userSession->GetClientInfo(imc->clientInfo_.client->AsObject()); EXPECT_EQ(clientInfo, nullptr); @@ -345,7 +345,7 @@ HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr002, TestSi { IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr002 TEST START"); auto userSession = std::make_shared(MAIN_USER_ID); - sptr core = new InputMethodCoreStub(0); + sptr core = new InputMethodCoreStub(); sptr inputMethodAgentStub(new InputMethodAgentStub()); sptr agent = sptr(new InputMethodAgentProxy(inputMethodAgentStub)); int32_t ret = userSession->OnSetCoreAndAgent(nullptr, nullptr); @@ -367,7 +367,7 @@ HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr003, TestSi { IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr003 TEST START"); auto userSession = std::make_shared(MAIN_USER_ID); - sptr core = new InputMethodCoreStub(0); + sptr core = new InputMethodCoreStub(); userSession->OnClientDied(nullptr); userSession->OnImsDied(nullptr); userSession->UpdateClient(nullptr, true);