From 4dc5f6a37e8376f0542d0e089e7a45f7556fd62b Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Thu, 15 Aug 2024 17:44:28 +0800 Subject: [PATCH] modify service code Signed-off-by: zhaolinglan --- .../js/napi/inputmethodclient/async_call.h | 2 +- services/include/caller_process_info.h | 32 + .../include/input_method_system_ability.h | 113 ++- services/include/peruser_session.h | 2 + services/src/input_method_system_ability.cpp | 725 +++++++++++------- services/src/peruser_session.cpp | 64 +- 6 files changed, 601 insertions(+), 337 deletions(-) create mode 100644 services/include/caller_process_info.h diff --git a/frameworks/js/napi/inputmethodclient/async_call.h b/frameworks/js/napi/inputmethodclient/async_call.h index d4a6387ef..38ebf3137 100644 --- a/frameworks/js/napi/inputmethodclient/async_call.h +++ b/frameworks/js/napi/inputmethodclient/async_call.h @@ -16,7 +16,7 @@ #define ASYN_CALL_H #include "global.h" -#include "input_method_info.h" +#include "input_method_info.h"// #include "js_utils.h" #include "napi/native_api.h" #include "napi/native_common.h" diff --git a/services/include/caller_process_info.h b/services/include/caller_process_info.h new file mode 100644 index 000000000..8acc1f55d --- /dev/null +++ b/services/include/caller_process_info.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INPUTMETHOD_IMF_CALLER_PROCESS_INFO_H +#define INPUTMETHOD_IMF_CALLER_PROCESS_INFO_H + +#include + +namespace OHOS { +namespace MiscServices { +struct CallerProcessInfo { + int32_t pid = 0; + int32_t uid = 0; + uint32_t tokenId = 0; + uint64_t fullTokenId = 0; +}; +} // namespace MiscServices +} // namespace OHOS + +#endif //INPUTMETHOD_IMF_CALLER_PROCESS_INFO_H diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index 8c0d786e2..059363ee6 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -23,6 +23,7 @@ #include "application_info.h" #include "block_queue.h" #include "bundle_mgr_proxy.h" +#include "caller_process_info.h" #include "element_name.h" #include "enable_ime_data_parser.h" #include "event_handler.h" @@ -92,34 +93,88 @@ protected: void OnStop() override; private: + static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; + static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; + static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; + static std::shared_ptr taskHandler_; + static std::shared_ptr mainHandler_; int32_t Init(); void Initialize(); - - std::thread workThreadHandler; /*!< thread handler of the WorkThread */ + void InitServiceHandler(); + void InitTaskHandler(); + void SubscribeCommonEvent(); int32_t GetUserId(int32_t uid); int32_t GetCallingUserId(); - std::shared_ptr identityChecker_ = nullptr; + CallerProcessInfo GetCallerInfo(); + + // handler tasks + int32_t RunStartInput( + const CallerProcessInfo &callerInfo, InputClientInfo &inputClientInfo, sptr &agent); + int32_t RunShowCurrentInput(const CallerProcessInfo &callerInfo); + int32_t RunHideCurrentInput(const CallerProcessInfo &callerInfo); + int32_t RunShowInput(const CallerProcessInfo &callerInfo, sptr client); + int32_t RunHideInput(const CallerProcessInfo &callerInfo, sptr client); + int32_t RunStopInputSession(const CallerProcessInfo &callerInfo); + int32_t RunReleaseInput(const CallerProcessInfo &callerInfo, sptr client); + int32_t RunRequestShowInput(const CallerProcessInfo &callerInfo); + int32_t RunRequestHideInput(const CallerProcessInfo &callerInfo); + int32_t RunSwitchInputMethod(const CallerProcessInfo &callerInfo, const std::string &bundleName, + const std::string &subName, SwitchTrigger trigger); + int32_t RunHideCurrentInputDeprecated(const CallerProcessInfo &callerInfo); + int32_t RunShowCurrentInputDeprecated(const CallerProcessInfo &callerInfo); + int32_t RunExitCurrentInputType(const CallerProcessInfo &callerInfo); + int32_t RunIsPanelShown(const CallerProcessInfo &callerInfo, const PanelInfo &panelInfo, bool &isShown); + int32_t RunUnRegisteredProxyIme( + const CallerProcessInfo &callerInfo, UnRegisteredType type, const sptr &core); + int32_t OnDisplayOptionalInputMethod(); int32_t PrepareInput(int32_t userId, InputClientInfo &clientInfo); + int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); + void CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo, const CallerProcessInfo &callerInfo); + + // common events callbacks void WorkThread(); int32_t OnUserStarted(const Message *msg); int32_t OnUserRemoved(const Message *msg); int32_t OnUserStop(const Message *msg); int32_t OnHideKeyboardSelf(const Message *msg); - bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); - int32_t CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); - bool IsStartInputTypePermitted(int32_t userId); - int32_t OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger); - int32_t OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission); int32_t HandlePackageEvent(const Message *msg); int32_t OnPackageRemoved(int32_t userId, const std::string &packageName); - int32_t OnDisplayOptionalInputMethod(); - void SubscribeCommonEvent(); + + // permission check + bool IsNeedSwitch(int32_t userId, const std::string &bundleName, const std::string &subName); + bool IsStartInputTypePermitted(int32_t userId, const CallerProcessInfo &callerInfo); + bool IsCurrentIme(int32_t userId); + int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); + int32_t CheckSwitchPermission( + int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger, const CallerProcessInfo &callerInfo); + + // switch + int32_t OnSwitchInputMethod( + int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger, const CallerProcessInfo &callerInfo); int32_t Switch(int32_t userId, const std::string &bundleName, const std::shared_ptr &info); int32_t SwitchExtension(int32_t userId, const std::shared_ptr &info); int32_t SwitchSubType(int32_t userId, const std::shared_ptr &info); int32_t SwitchInputType(int32_t userId, const SwitchInfo &switchInfo); - ServiceRunningState state_; - void InitServiceHandler(); + int32_t StartInputType(int32_t userId, InputType type, const CallerProcessInfo &callerInfo); + int32_t OnStartInputType( + int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission, const CallerProcessInfo &callerInfo); + void StopImeInBackground(); + + // physical keyboard switch + int32_t SwitchByCombinationKey(uint32_t state); + int32_t SwitchMode(); + int32_t SwitchLanguage(); + int32_t SwitchType(); + + // monitors for external modules + void InitMonitors(); + int32_t InitKeyEventMonitor(); + bool InitWmsMonitor(); + void InitSystemLanguageMonitor(); + bool InitMemMgrMonitor(); + void InitWmsConnectionMonitor(); + void InitFocusChangedMonitor(); + int32_t InitAccountMonitor(); void UpdateUserInfo(int32_t userId); void HandleWmsConnected(int32_t userId, int32_t screenId); void HandleWmsDisconnected(int32_t userId, int32_t screenId); @@ -129,39 +184,23 @@ private: void HandleMemStarted(); void HandleOsAccountStarted(); void HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid); - void StopImeInBackground(); - int32_t InitAccountMonitor(); + + // data observers int32_t RegisterDataShareObserver(); - static std::shared_ptr serviceHandler_; - int32_t userId_; - static constexpr const char *SELECT_DIALOG_ACTION = "action.system.inputmethodchoose"; - static constexpr const char *SELECT_DIALOG_HAP = "com.ohos.inputmethodchoosedialog"; - static constexpr const char *SELECT_DIALOG_ABILITY = "InputMethod"; - bool stop_ = false; - void InitMonitors(); - int32_t InitKeyEventMonitor(); - bool InitWmsMonitor(); - void InitSystemLanguageMonitor(); - bool InitMemMgrMonitor(); - void InitWmsConnectionMonitor(); - void InitFocusChangedMonitor(); - int32_t SwitchByCombinationKey(uint32_t state); - int32_t SwitchMode(); - int32_t SwitchLanguage(); - int32_t SwitchType(); - int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); void RegisterEnableImeObserver(); void RegisterSecurityModeObserver(); - void CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); - int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); void DealSwitchRequest(); void DealSecurityChange(); void OnSecurityModeChange(); - bool IsCurrentIme(int32_t userId); - int32_t StartInputType(int32_t userId, InputType type); + void DatashareCallback(const std::string &key); + + std::shared_ptr identityChecker_ = nullptr; + int32_t userId_; + std::thread workThreadHandler_; /*!< thread handler of the WorkThread */ + bool stop_ = false; + ServiceRunningState state_; std::mutex checkMutex_; - void DatashareCallback(const std::string &key); bool enableImeOn_ = false; bool enableSecurityMode_ = false; diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index ce1b09dcc..3e6a312c0 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -78,6 +78,7 @@ public: explicit PerUserSession(int userId); PerUserSession(int32_t userId, const std::shared_ptr &eventHandler); ~PerUserSession(); + static void SetServiceTaskHandler(std::shared_ptr handler); int32_t OnPrepareInput(const InputClientInfo &clientInfo); int32_t OnStartInput(const InputClientInfo &inputClientInfo, sptr &agent); @@ -219,6 +220,7 @@ private: std::mutex restartMutex_; int32_t restartTasks_ = 0; std::shared_ptr eventHandler_{ nullptr }; + static std::shared_ptr serviceTaskHandler_; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index dd8a7aa96..189549d56 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -52,7 +52,8 @@ using namespace Security::AccessToken; REGISTER_SYSTEM_ABILITY_BY_ID(InputMethodSystemAbility, INPUT_METHOD_SYSTEM_ABILITY_ID, true); constexpr std::int32_t INIT_INTERVAL = 10000L; static const std::string PERMISSION_CONNECT_IME_ABILITY = "ohos.permission.CONNECT_IME_ABILITY"; -std::shared_ptr InputMethodSystemAbility::serviceHandler_; +std::shared_ptr InputMethodSystemAbility::taskHandler_; +std::shared_ptr InputMethodSystemAbility::mainHandler_; InputMethodSystemAbility::InputMethodSystemAbility(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(systemAbilityId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START) @@ -68,8 +69,8 @@ InputMethodSystemAbility::~InputMethodSystemAbility() stop_ = true; Message *msg = new Message(MessageID::MSG_ID_QUIT_WORKER_THREAD, nullptr); MessageHandler::Instance()->SendMessage(msg); - if (workThreadHandler.joinable()) { - workThreadHandler.join(); + if (workThreadHandler_.joinable()) { + workThreadHandler_.join(); } } @@ -84,19 +85,19 @@ void InputMethodSystemAbility::OnStart() return; } InitServiceHandler(); + InitTaskHandler(); Initialize(); int32_t ret = Init(); if (ret != ErrorCode::NO_ERROR) { InputMethodSysEvent::GetInstance().ServiceFaultReporter("imf", ret); auto callback = [=]() { Init(); }; - serviceHandler_->PostTask(callback, INIT_INTERVAL); + taskHandler_->PostTask(callback, INIT_INTERVAL); IMSA_HILOGE("init failed. try again 10s later!"); } InitHiTrace(); InputMethodSyncTrace tracer("InputMethodController Attach trace."); InputmethodDump::GetInstance().AddDumpAllMethod([this](int fd) { this->DumpAllMethod(fd); }); IMSA_HILOGI("start imsa service success."); - return; } int InputMethodSystemAbility::Dump(int fd, const std::vector &args) @@ -162,7 +163,9 @@ void InputMethodSystemAbility::OnStop() { IMSA_HILOGI("OnStop start."); FreezeManager::SetEventHandler(nullptr); - serviceHandler_ = nullptr; + PerUserSession::SetServiceTaskHandler(nullptr); + taskHandler_ = nullptr; + mainHandler_ = nullptr; state_ = ServiceRunningState::STATE_NOT_START; Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(), 1, 0, INPUT_METHOD_SYSTEM_ABILITY_ID); } @@ -170,17 +173,37 @@ void InputMethodSystemAbility::OnStop() void InputMethodSystemAbility::InitServiceHandler() { IMSA_HILOGI("InitServiceHandler start."); - if (serviceHandler_ != nullptr) { + if (taskHandler_ != nullptr) { IMSA_HILOGE("InputMethodSystemAbility already init."); return; } std::shared_ptr runner = AppExecFwk::EventRunner::Create("OS_InputMethodSystemAbility"); - serviceHandler_ = std::make_shared(runner); - FreezeManager::SetEventHandler(serviceHandler_); + taskHandler_ = std::make_shared(runner); + FreezeManager::SetEventHandler(taskHandler_); IMSA_HILOGI("InitServiceHandler succeeded."); } +void InputMethodSystemAbility::InitTaskHandler() +{ + if (mainHandler_ != nullptr) { + IMSA_HILOGI("already init"); + return; + } + std::shared_ptr runner = AppExecFwk::EventRunner::Create("OS_InputMethodServiceTask"); + mainHandler_ = std::make_shared(runner); + if (mainHandler_ == nullptr) { + IMSA_HILOGE("handler is nullptr"); + return; + } + // timout default 30s + auto ret = HiviewDFX::Watchdog::GetInstance().AddThread("OS_InputMethodServiceTask", mainHandler_); + if (ret != 0) { + IMSA_HILOGW("failed to add watch dog ret: %{public}d", ret); + } + IMSA_HILOGI("success"); +} + /** * Initialization of Input method management service * \n It's called after the service starts, before any transaction. @@ -189,10 +212,11 @@ void InputMethodSystemAbility::Initialize() { IMSA_HILOGI("InputMethodSystemAbility::Initialize."); // init work thread to handle the messages - workThreadHandler = std::thread([this] { this->WorkThread(); }); + workThreadHandler_ = std::thread([this] { this->WorkThread(); }); identityChecker_ = std::make_shared(); userId_ = OsAccountAdapter::MAIN_USER_ID; - UserSessionManager::GetInstance().SetEventHandler(serviceHandler_); + PerUserSession::SetServiceTaskHandler(mainHandler_); + UserSessionManager::GetInstance().SetEventHandler(taskHandler_); UserSessionManager::GetInstance().AddUserSession(userId_); InputMethodSysEvent::GetInstance().SetUserId(userId_); isScbEnable_ = Rosen::SceneBoardJudgement::IsSceneBoardEnabled(); @@ -209,7 +233,147 @@ void InputMethodSystemAbility::SubscribeCommonEvent() IMSA_HILOGE("failed, try again 10s later"); auto callback = [this]() { SubscribeCommonEvent(); }; - serviceHandler_->PostTask(callback, INIT_INTERVAL); + taskHandler_->PostTask(callback, INIT_INTERVAL); +} + +int32_t InputMethodSystemAbility::StartInput(InputClientInfo &inputClientInfo, sptr &agent) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &inputClientInfo, &agent, &ret]() { + ret = RunStartInput(callerInfo, inputClientInfo, agent); + }; + mainHandler_->PostSyncTask(task, "StartInput"); + return ret; +} + +int32_t InputMethodSystemAbility::ShowCurrentInput() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunShowCurrentInput(callerInfo); }; + mainHandler_->PostSyncTask(task, "ShowCurrentInput"); + return ret; +} + +int32_t InputMethodSystemAbility::HideCurrentInput() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunHideCurrentInput(callerInfo); }; + mainHandler_->PostSyncTask(task, "HideCurrentInput"); + return ret; +} + +int32_t InputMethodSystemAbility::ShowInput(sptr client) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, client, &ret]() { ret = RunShowInput(callerInfo, client); }; + mainHandler_->PostSyncTask(task, "ShowInput"); + return ret; +} + +int32_t InputMethodSystemAbility::HideInput(sptr client) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, client, &ret]() { ret = RunHideInput(callerInfo, client); }; + mainHandler_->PostSyncTask(task, "HideInput"); + return ret; +} + +int32_t InputMethodSystemAbility::StopInputSession() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunStopInputSession(callerInfo); }; + mainHandler_->PostSyncTask(task, "StopInputSession"); + return ret; +} + +int32_t InputMethodSystemAbility::ReleaseInput(sptr client) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, client, &ret]() { ret = RunReleaseInput(callerInfo, client); }; + mainHandler_->PostSyncTask(task, "ReleaseInput"); + return ret; +} + +int32_t InputMethodSystemAbility::RequestShowInput() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunRequestShowInput(callerInfo); }; + mainHandler_->PostSyncTask(task, "RequestShowInput"); + return ret; +} + +int32_t InputMethodSystemAbility::RequestHideInput() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunRequestHideInput(callerInfo); }; + mainHandler_->PostSyncTask(task, "RequestHideInput"); + return ret; +} + +int32_t InputMethodSystemAbility::SwitchInputMethod( + const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &bundleName, &subName, trigger, &ret]() { + ret = RunSwitchInputMethod(callerInfo, bundleName, subName, trigger); + }; + mainHandler_->PostSyncTask(task, "SwitchInputMethod"); + return ret; +} + +int32_t InputMethodSystemAbility::ShowCurrentInputDeprecated() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunShowCurrentInputDeprecated(callerInfo); }; + mainHandler_->PostSyncTask(task, "ShowCurrentInputDeprecated"); + return ret; +} + +int32_t InputMethodSystemAbility::HideCurrentInputDeprecated() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunHideCurrentInputDeprecated(callerInfo); }; + mainHandler_->PostSyncTask(task, "HideCurrentInputDeprecated"); + return ret; +} + +int32_t InputMethodSystemAbility::ExitCurrentInputType() +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &ret]() { ret = RunExitCurrentInputType(callerInfo); }; + mainHandler_->PostSyncTask(task, "ExitCurrentInputType"); + return ret; +} + +int32_t InputMethodSystemAbility::IsPanelShown(const PanelInfo &panelInfo, bool &isShown) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, &panelInfo, &isShown, &ret]() { ret = RunIsPanelShown(callerInfo, panelInfo, isShown); }; + mainHandler_->PostSyncTask(task, "IsPanelShown"); + return ret; +} + +int32_t InputMethodSystemAbility::UnRegisteredProxyIme(UnRegisteredType type, const sptr &core) +{ + auto callerInfo = GetCallerInfo(); + int32_t ret = 0; + auto task = [this, &callerInfo, type, core, &ret]() { ret = RunUnRegisteredProxyIme(callerInfo, type, core); }; + mainHandler_->PostSyncTask(task, "UnRegisteredProxyIme"); + return ret; } int32_t InputMethodSystemAbility::PrepareInput(int32_t userId, InputClientInfo &clientInfo) @@ -248,39 +412,36 @@ int32_t InputMethodSystemAbility::GenerateClientInfo(int32_t userId, InputClient return ErrorCode::NO_ERROR; } -int32_t InputMethodSystemAbility::ReleaseInput(sptr client) +int32_t InputMethodSystemAbility::RunReleaseInput(const CallerProcessInfo &callerInfo, sptr client) { if (client == nullptr) { IMSA_HILOGE("client is nullptr!"); return ErrorCode::ERROR_CLIENT_NULL_POINTER; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } return session->OnReleaseInput(client); -}; +} -int32_t InputMethodSystemAbility::StartInput(InputClientInfo &inputClientInfo, sptr &agent) +int32_t InputMethodSystemAbility::RunStartInput( + const CallerProcessInfo &callerInfo, InputClientInfo &inputClientInfo, sptr &agent) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId)) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } - } else { + if (identityChecker_->IsBroker(callerInfo.tokenId)) { inputClientInfo.config.windowId = ANCO_INVALID_WINDOW_ID; + } else if (!identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId)) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (session->GetCurrentClientPid() != IPCSkeleton::GetCallingPid() - && session->GetInactiveClientPid() != IPCSkeleton::GetCallingPid()) { + if (session->GetCurrentClientPid() != callerInfo.pid && session->GetInactiveClientPid() != callerInfo.pid) { // notify inputStart when caller pid different from both current client and inactive client inputClientInfo.isNotifyInputStart = true; } @@ -288,7 +449,7 @@ int32_t InputMethodSystemAbility::StartInput(InputClientInfo &inputClientInfo, s inputClientInfo.needHide = session->CheckPwdInputPatternConv(inputClientInfo); } if (!session->IsProxyImeEnable()) { - CheckInputTypeOption(userId, inputClientInfo); + CheckInputTypeOption(userId, inputClientInfo, callerInfo); } int32_t ret = PrepareInput(userId, inputClientInfo); if (ret != ErrorCode::NO_ERROR) { @@ -296,9 +457,10 @@ int32_t InputMethodSystemAbility::StartInput(InputClientInfo &inputClientInfo, s return ret; } return session->OnStartInput(inputClientInfo, agent); -}; +} -void InputMethodSystemAbility::CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo) +void InputMethodSystemAbility::CheckInputTypeOption( + int32_t userId, InputClientInfo &inputClientInfo, const CallerProcessInfo &callerInfo) { IMSA_HILOGI("SecurityFlag: %{public}d, IsSameTextInput: %{public}d, IsStarted: %{public}d, " "IsSecurityImeStarted: %{public}d.", @@ -306,7 +468,7 @@ void InputMethodSystemAbility::CheckInputTypeOption(int32_t userId, InputClientI InputTypeManager::GetInstance().IsStarted(), InputTypeManager::GetInstance().IsSecurityImeStarted()); if (inputClientInfo.config.inputAttribute.GetSecurityFlag()) { if (!InputTypeManager::GetInstance().IsStarted()) { - StartInputType(userId, InputType::SECURITY_INPUT); + StartInputType(userId, InputType::SECURITY_INPUT, callerInfo); IMSA_HILOGI("SecurityFlag, input type is not started."); return; } @@ -315,7 +477,7 @@ void InputMethodSystemAbility::CheckInputTypeOption(int32_t userId, InputClientI return; } if (!InputTypeManager::GetInstance().IsSecurityImeStarted()) { - StartInputType(userId, InputType::SECURITY_INPUT); + StartInputType(userId, InputType::SECURITY_INPUT, callerInfo); IMSA_HILOGI("SecurityFlag, input type is started, but not security."); return; } @@ -324,25 +486,23 @@ void InputMethodSystemAbility::CheckInputTypeOption(int32_t userId, InputClientI } if (inputClientInfo.isNotifyInputStart && InputTypeManager::GetInstance().IsStarted()) { IMSA_HILOGI("NormalFlag diff text input, input type started."); - StartInputType(userId, InputType::NONE); + StartInputType(userId, InputType::NONE, callerInfo); return; } IMSA_HILOGI("NormalFlag success."); } -int32_t InputMethodSystemAbility::ShowInput(sptr client) +int32_t InputMethodSystemAbility::RunShowInput(const CallerProcessInfo &callerInfo, sptr client) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId, session->GetCurrentClientPid())) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } + if (!identityChecker_->IsBroker(callerInfo.tokenId) + && !identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId, session->GetCurrentClientPid())) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } if (client == nullptr) { IMSA_HILOGE("client is nullptr"); @@ -351,19 +511,17 @@ int32_t InputMethodSystemAbility::ShowInput(sptr client) return session->OnShowInput(client); } -int32_t InputMethodSystemAbility::HideInput(sptr client) +int32_t InputMethodSystemAbility::RunHideInput(const CallerProcessInfo &callerInfo, sptr client) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId, session->GetCurrentClientPid())) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } + if (!identityChecker_->IsBroker(callerInfo.tokenId) + && !identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId, session->GetCurrentClientPid())) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } if (client == nullptr) { IMSA_HILOGE("client is nullptr"); @@ -372,31 +530,28 @@ int32_t InputMethodSystemAbility::HideInput(sptr client) return session->OnHideInput(client); }; -int32_t InputMethodSystemAbility::StopInputSession() +int32_t InputMethodSystemAbility::RunStopInputSession(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId, session->GetCurrentClientPid())) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } + if (!identityChecker_->IsBroker(callerInfo.tokenId) + && !identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId, session->GetCurrentClientPid())) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } return session->OnHideCurrentInput(); } -int32_t InputMethodSystemAbility::RequestShowInput() +int32_t InputMethodSystemAbility::RunRequestShowInput(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId) && - !identityChecker_->HasPermission(tokenId, PERMISSION_CONNECT_IME_ABILITY)) { + if (!identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId) + && !identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); @@ -405,14 +560,13 @@ int32_t InputMethodSystemAbility::RequestShowInput() return session->OnRequestShowInput(); } -int32_t InputMethodSystemAbility::RequestHideInput() +int32_t InputMethodSystemAbility::RunRequestHideInput(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId) && - !identityChecker_->HasPermission(tokenId, PERMISSION_CONNECT_IME_ABILITY)) { + if (!identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId) + && !identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); @@ -439,37 +593,35 @@ int32_t InputMethodSystemAbility::SetCoreAndAgent(const sptr & return ErrorCode::ERROR_NOT_CURRENT_IME; } -int32_t InputMethodSystemAbility::HideCurrentInput() +int32_t InputMethodSystemAbility::RunHideCurrentInput(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (identityChecker_->IsBroker(tokenId)) { + if (identityChecker_->IsBroker(callerInfo.tokenId)) { return session->OnHideCurrentInput(); } - if (!identityChecker_->HasPermission(tokenId, PERMISSION_CONNECT_IME_ABILITY)) { + if (!identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } return session->OnHideCurrentInput(); -}; +} -int32_t InputMethodSystemAbility::ShowCurrentInput() +int32_t InputMethodSystemAbility::RunShowCurrentInput(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (identityChecker_->IsBroker(tokenId)) { + if (identityChecker_->IsBroker(callerInfo.tokenId)) { return session->OnShowCurrentInput(); } - if (!identityChecker_->HasPermission(tokenId, PERMISSION_CONNECT_IME_ABILITY)) { + if (!identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } return session->OnShowCurrentInput(); @@ -531,12 +683,13 @@ bool InputMethodSystemAbility::IsInputTypeSupported(InputType type) int32_t InputMethodSystemAbility::StartInputType(InputType type) { - return StartInputType(GetCallingUserId(), type); + auto callerInfo = GetCallerInfo(); + return StartInputType(GetCallingUserId(), type, callerInfo); } -int32_t InputMethodSystemAbility::ExitCurrentInputType() +int32_t InputMethodSystemAbility::RunExitCurrentInputType(const CallerProcessInfo &callerInfo) { - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto ret = IsDefaultImeFromTokenId(userId, IPCSkeleton::GetCallingTokenID()); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("not default ime."); @@ -548,9 +701,9 @@ int32_t InputMethodSystemAbility::ExitCurrentInputType() return ErrorCode::ERROR_NULL_POINTER; } if (session->CheckSecurityMode()) { - return StartInputType(userId, InputType::SECURITY_INPUT); + return StartInputType(userId, InputType::SECURITY_INPUT, callerInfo); } - return StartInputType(userId, InputType::NONE); + return StartInputType(userId, InputType::NONE, callerInfo); } int32_t InputMethodSystemAbility::IsDefaultIme() @@ -572,13 +725,14 @@ int32_t InputMethodSystemAbility::IsDefaultImeFromTokenId(int32_t userId, uint32 return ErrorCode::NO_ERROR; } -int32_t InputMethodSystemAbility::IsPanelShown(const PanelInfo &panelInfo, bool &isShown) +int32_t InputMethodSystemAbility::RunIsPanelShown( + const CallerProcessInfo &callerInfo, const PanelInfo &panelInfo, bool &isShown) { - if (!identityChecker_->IsSystemApp(IPCSkeleton::GetCallingFullTokenID())) { + if (!identityChecker_->IsSystemApp(callerInfo.fullTokenId)) { IMSA_HILOGE("not system application!"); return ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); @@ -593,8 +747,8 @@ int32_t InputMethodSystemAbility::DisplayOptionalInputMethod() return OnDisplayOptionalInputMethod(); } -int32_t InputMethodSystemAbility::SwitchInputMethod(const std::string &bundleName, const std::string &subName, - SwitchTrigger trigger) +int32_t InputMethodSystemAbility::RunSwitchInputMethod(const CallerProcessInfo &callerInfo, + const std::string &bundleName, const std::string &subName, SwitchTrigger trigger) { // IMSA not check permission, add this verify for prevent counterfeit if (trigger == SwitchTrigger::IMSA) { @@ -602,7 +756,7 @@ int32_t InputMethodSystemAbility::SwitchInputMethod(const std::string &bundleNam return ErrorCode::ERROR_BAD_PARAMETERS; } SwitchInfo switchInfo = { std::chrono::system_clock::now(), bundleName, subName }; - int32_t userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); @@ -615,12 +769,12 @@ int32_t InputMethodSystemAbility::SwitchInputMethod(const std::string &bundleNam switchInfo.timestamp = std::chrono::system_clock::now(); session->GetSwitchQueue().Push(switchInfo); return InputTypeManager::GetInstance().IsInputType({ bundleName, subName }) - ? OnStartInputType(userId, switchInfo, true) - : OnSwitchInputMethod(userId, switchInfo, trigger); + ? OnStartInputType(userId, switchInfo, true, callerInfo) + : OnSwitchInputMethod(userId, switchInfo, trigger, callerInfo); } -int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, - SwitchTrigger trigger) +int32_t InputMethodSystemAbility::OnSwitchInputMethod( + int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger, const CallerProcessInfo &callerInfo) { IMSA_HILOGD("start, switchInfo: %{public}s|%{public}s", switchInfo.bundleName.c_str(), switchInfo.subName.c_str()); InputMethodSysEvent::GetInstance().RecordEvent(IMEBehaviour::CHANGE_IME); @@ -629,27 +783,19 @@ int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const Swit IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!session->GetSwitchQueue().IsReady(switchInfo)) { - IMSA_HILOGD("start wait."); - session->GetSwitchQueue().Wait(switchInfo); - } IMSA_HILOGI("start switch %{public}s|%{public}s.", switchInfo.bundleName.c_str(), switchInfo.subName.c_str()); - int32_t ret = CheckSwitchPermission(userId, switchInfo, trigger); + int32_t ret = CheckSwitchPermission(userId, switchInfo, trigger, callerInfo); if (ret != ErrorCode::NO_ERROR) { InputMethodSysEvent::GetInstance().InputmethodFaultReporter(ErrorCode::ERROR_STATUS_PERMISSION_DENIED, switchInfo.bundleName, "switch input method failed!"); - session->GetSwitchQueue().Pop(); return ret; } - if (!InputTypeManager::GetInstance().IsStarted() && !IsNeedSwitch(userId, switchInfo.bundleName, switchInfo.subName)) { - session->GetSwitchQueue().Pop(); return ErrorCode::NO_ERROR; } auto info = ImeInfoInquirer::GetInstance().GetImeInfo(userId, switchInfo.bundleName, switchInfo.subName); if (info == nullptr) { - session->GetSwitchQueue().Pop(); return ErrorCode::ERROR_BAD_PARAMETERS; } { @@ -659,7 +805,6 @@ int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const Swit if (InputTypeManager::GetInstance().IsStarted()) { InputTypeManager::GetInstance().Set(false); } - session->GetSwitchQueue().Pop(); if (ret != ErrorCode::NO_ERROR) { InputMethodSysEvent::GetInstance().InputmethodFaultReporter(ret, switchInfo.bundleName, "switch input method failed!"); @@ -667,38 +812,30 @@ int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const Swit return ret; } -int32_t InputMethodSystemAbility::OnStartInputType(int32_t userId, const SwitchInfo &switchInfo, - bool isCheckPermission) +int32_t InputMethodSystemAbility::OnStartInputType( + int32_t userId, const SwitchInfo &switchInfo, bool isCheckPermission, const CallerProcessInfo &callerInfo) { auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!session->GetSwitchQueue().IsReady(switchInfo)) { - IMSA_HILOGD("start wait."); - session->GetSwitchQueue().Wait(switchInfo); - } auto cfgIme = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); if (switchInfo.bundleName == cfgIme->bundleName && switchInfo.subName == cfgIme->subName) { IMSA_HILOGD("input type is current ime, exit input type."); int32_t ret = session->ExitCurrentInputType(); - session->GetSwitchQueue().Pop(); return ret; } IMSA_HILOGD("start switch %{public}s|%{public}s.", switchInfo.bundleName.c_str(), switchInfo.subName.c_str()); - if (isCheckPermission && !IsStartInputTypePermitted(userId)) { + if (isCheckPermission && !IsStartInputTypePermitted(userId, callerInfo)) { IMSA_HILOGE("not permitted to start input type!"); - session->GetSwitchQueue().Pop(); return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } if (!IsNeedSwitch(userId, switchInfo.bundleName, switchInfo.subName)) { IMSA_HILOGI("no need to switch."); - session->GetSwitchQueue().Pop(); return ErrorCode::NO_ERROR; } int32_t ret = SwitchInputType(userId, switchInfo); - session->GetSwitchQueue().Pop(); return ret; } @@ -729,8 +866,7 @@ int32_t InputMethodSystemAbility::Switch(int32_t userId, const std::string &bund return SwitchExtension(userId, info); } auto currentInputType = InputTypeManager::GetInstance().GetCurrentIme(); - auto isInputTypeStarted = InputTypeManager::GetInstance().IsStarted(); - if (isInputTypeStarted && bundleName != currentInputType.bundleName) { + if (InputTypeManager::GetInstance().IsStarted() && bundleName != currentInputType.bundleName) { IMSA_HILOGI("right click on state, switch input method to: %{public}s", bundleName.c_str()); return SwitchExtension(userId, info); } @@ -819,39 +955,35 @@ int32_t InputMethodSystemAbility::SwitchInputType(int32_t userId, const SwitchIn } // Deprecated because of no permission check, kept for compatibility -int32_t InputMethodSystemAbility::HideCurrentInputDeprecated() +int32_t InputMethodSystemAbility::RunHideCurrentInputDeprecated(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - int32_t userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId, session->GetCurrentClientPid())) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } + if (!identityChecker_->IsBroker(callerInfo.tokenId) + && !identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId, session->GetCurrentClientPid())) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } return session->OnHideCurrentInput(); -}; +} -int32_t InputMethodSystemAbility::ShowCurrentInputDeprecated() +int32_t InputMethodSystemAbility::RunShowCurrentInputDeprecated(const CallerProcessInfo &callerInfo) { - AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); - int32_t userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (!identityChecker_->IsBroker(tokenId)) { - if (!identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId, session->GetCurrentClientPid())) { - return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; - } + if (!identityChecker_->IsBroker(callerInfo.tokenId) + && !identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId, session->GetCurrentClientPid())) { + return ErrorCode::ERROR_CLIENT_NOT_FOCUSED; } return session->OnShowCurrentInput(); -}; +} std::shared_ptr InputMethodSystemAbility::GetCurrentInputMethod() { @@ -952,20 +1084,24 @@ void InputMethodSystemAbility::WorkThread() */ int32_t InputMethodSystemAbility::OnUserStarted(const Message *msg) { - if (msg->msgContent_ == nullptr) { + if (msg == nullptr || msg->msgContent_ == nullptr) { IMSA_HILOGE("msgContent_ is nullptr."); return ErrorCode::ERROR_NULL_POINTER; } auto newUserId = msg->msgContent_->ReadInt32(); - FullImeInfoManager::GetInstance().Add(newUserId); - // if scb enable, deal when receive wmsConnected. - if (isScbEnable_) { - return ErrorCode::NO_ERROR; - } - if (newUserId == userId_) { - return ErrorCode::NO_ERROR; - } - HandleUserSwitched(newUserId); + auto task = [this, newUserId]() { + IMSA_HILOGI("OnUserStarted: %{public}d", newUserId); + FullImeInfoManager::GetInstance().Add(newUserId); + // if scb enable, deal when receive wmsConnected. + if (isScbEnable_) { + return; + } + if (newUserId == userId_) { + return; + } + HandleUserSwitched(newUserId); + }; + mainHandler_->PostTask(task, "OnUserStarted"); return ErrorCode::NO_ERROR; } @@ -976,14 +1112,17 @@ int32_t InputMethodSystemAbility::OnUserRemoved(const Message *msg) return ErrorCode::ERROR_NULL_POINTER; } auto userId = msg->msgContent_->ReadInt32(); - IMSA_HILOGI("start: %{public}d", userId); - auto session = UserSessionManager::GetInstance().GetUserSession(userId); - if (session != nullptr) { - session->StopCurrentIme(); - UserSessionManager::GetInstance().RemoveUserSession(userId); - } - ImeCfgManager::GetInstance().DeleteImeCfg(userId); - FullImeInfoManager::GetInstance().Delete(userId); + auto task = [userId]() { + IMSA_HILOGI("OnUserRemoved: %{public}d", userId); + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session != nullptr) { + session->StopCurrentIme(); + UserSessionManager::GetInstance().RemoveUserSession(userId); + } + ImeCfgManager::GetInstance().DeleteImeCfg(userId); + FullImeInfoManager::GetInstance().Delete(userId); + }; + mainHandler_->PostTask(task, "OnUserRemoved"); return ErrorCode::NO_ERROR; } @@ -994,12 +1133,15 @@ int32_t InputMethodSystemAbility::OnUserStop(const Message *msg) return ErrorCode::ERROR_NULL_POINTER; } auto userId = msg->msgContent_->ReadInt32(); - auto session = UserSessionManager::GetInstance().GetUserSession(userId); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr", userId); - return ErrorCode::ERROR_NULL_POINTER; - } - session->StopCurrentIme(); + auto task = [userId](){ + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr", userId); + return; + } + session->StopCurrentIme(); + }; + mainHandler_->PostTask(task, "OnUserStop"); return ErrorCode::NO_ERROR; } @@ -1010,22 +1152,25 @@ int32_t InputMethodSystemAbility::OnHideKeyboardSelf(const Message *msg) return ErrorCode::ERROR_NULL_POINTER; } auto userId = msg->msgContent_->ReadInt32(); - auto session = UserSessionManager::GetInstance().GetUserSession(userId); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr", userId); - return ErrorCode::ERROR_NULL_POINTER; - } - session->OnHideSoftKeyBoardSelf(); + auto task = [userId](){ + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr", userId); + return; + } + session->OnHideSoftKeyBoardSelf(); + }; + mainHandler_->PostTask(task, "OnHideKeyboardSelf"); return ErrorCode::NO_ERROR; } int32_t InputMethodSystemAbility::HandlePackageEvent(const Message *msg) { - MessageParcel *data = msg->msgContent_; - if (data == nullptr) { - IMSA_HILOGD("data is nullptr"); + if (msg == nullptr || msg->msgContent_ == nullptr) { + IMSA_HILOGE("Aborted! Message is nullptr."); return ErrorCode::ERROR_NULL_POINTER; } + MessageParcel *data = msg->msgContent_; int32_t userId = 0; std::string packageName; if (!ITypesUtil::Unmarshal(*data, userId, packageName)) { @@ -1039,7 +1184,8 @@ int32_t InputMethodSystemAbility::HandlePackageEvent(const Message *msg) return FullImeInfoManager::GetInstance().Add(userId, packageName); } if (msg->msgId_ == MSG_ID_PACKAGE_REMOVED) { - return OnPackageRemoved(userId, packageName); + auto task = [this, userId, packageName]() { OnPackageRemoved(userId, packageName); }; + mainHandler_->PostTask(task, "OnPackageRemoved"); } return ErrorCode::NO_ERROR; } @@ -1092,30 +1238,35 @@ int32_t InputMethodSystemAbility::OnDisplayOptionalInputMethod() int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) { IMSA_HILOGD("InputMethodSystemAbility::SwitchByCombinationKey start."); - auto session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr", userId_); - return ErrorCode::ERROR_NULL_POINTER; - } - if (session->IsProxyImeEnable()) { - IMSA_HILOGI("proxy enable, not switch."); - return ErrorCode::NO_ERROR; - } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state)) { - IMSA_HILOGI("switch mode."); - return SwitchMode(); - } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state)) { - IMSA_HILOGI("switch language."); - return SwitchLanguage(); - } - if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state)) { - IMSA_HILOGI("switch ime."); - DealSwitchRequest(); - return ErrorCode::NO_ERROR; - } - IMSA_HILOGE("keycode is undefined"); - return ErrorCode::ERROR_EX_UNSUPPORTED_OPERATION; + auto task = [this, state]() { + auto session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr", userId_); + return; + } + if (session->IsProxyImeEnable()) { + IMSA_HILOGI("proxy enable, not switch."); + return; + } + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_MODE, state)) { + IMSA_HILOGI("switch mode."); + SwitchMode(); + return; + } + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_LANGUAGE, state)) { + IMSA_HILOGI("switch language."); + SwitchLanguage(); + return; + } + if (CombinationKey::IsMatch(CombinationKeyFunction::SWITCH_IME, state)) { + IMSA_HILOGI("switch ime."); + DealSwitchRequest(); + return; + } + IMSA_HILOGE("keycode is undefined"); + }; + mainHandler_->PostTask(task, "SwitchByCombinationKey"); + return ErrorCode::NO_ERROR; } void InputMethodSystemAbility::DealSecurityChange() @@ -1176,7 +1327,7 @@ void InputMethodSystemAbility::DealSwitchRequest() } while (checkSwitchCount()); }; // 0 means delay time is 0. - serviceHandler_->PostTask(switchTask, "SwitchImeTask", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + taskHandler_->PostTask(switchTask, "SwitchImeTask", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); } int32_t InputMethodSystemAbility::SwitchMode() @@ -1205,7 +1356,7 @@ int32_t InputMethodSystemAbility::SwitchMode() return ErrorCode::ERROR_NULL_POINTER; } session->GetSwitchQueue().Push(switchInfo); - return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA); + return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA, {}); } int32_t InputMethodSystemAbility::SwitchLanguage() @@ -1237,7 +1388,7 @@ int32_t InputMethodSystemAbility::SwitchLanguage() return ErrorCode::ERROR_NULL_POINTER; } session->GetSwitchQueue().Push(switchInfo); - return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA); + return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA, {}); } int32_t InputMethodSystemAbility::SwitchType() @@ -1262,7 +1413,7 @@ int32_t InputMethodSystemAbility::SwitchType() return ErrorCode::ERROR_NULL_POINTER; } session->GetSwitchQueue().Push(switchInfo); - return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA); + return OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA, {}); } void InputMethodSystemAbility::InitMonitors() @@ -1362,12 +1513,17 @@ void InputMethodSystemAbility::RegisterSecurityModeObserver() void InputMethodSystemAbility::DatashareCallback(const std::string &key) { - IMSA_HILOGI("start."); - if (key == EnableImeDataParser::ENABLE_KEYBOARD || key == EnableImeDataParser::ENABLE_IME) { - EnableImeDataParser::GetInstance()->OnConfigChanged(userId_, key); - std::lock_guard autoLock(checkMutex_); - SwitchInfo switchInfo; - if (EnableImeDataParser::GetInstance()->CheckNeedSwitch(key, switchInfo, userId_)) { + IMSA_HILOGD("in, key: %{public}s", key.c_str()); + auto task = [this, key]() { + IMSA_HILOGI("DatashareCallback start, key: %{public}s", key.c_str()); + if (key == EnableImeDataParser::ENABLE_KEYBOARD || key == EnableImeDataParser::ENABLE_IME) { + EnableImeDataParser::GetInstance()->OnConfigChanged(userId_, key); + std::lock_guard autoLock(checkMutex_); + SwitchInfo switchInfo; + if (!EnableImeDataParser::GetInstance()->CheckNeedSwitch(key, switchInfo, userId_)) { + IMSA_HILOGD("DatashareCallback no need switch"); + return; + } auto session = UserSessionManager::GetInstance().GetUserSession(userId_); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId_); @@ -1375,12 +1531,13 @@ void InputMethodSystemAbility::DatashareCallback(const std::string &key) } switchInfo.timestamp = std::chrono::system_clock::now(); session->GetSwitchQueue().Push(switchInfo); - OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA); + OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA, {}); } - } - if (key == SecurityModeParser::SECURITY_MODE) { - DealSecurityChange(); - } + if (key == SecurityModeParser::SECURITY_MODE) { + DealSecurityChange(); + } + }; + mainHandler_->PostTask(task, "DatashareCallback"); } void InputMethodSystemAbility::OnSecurityModeChange() @@ -1426,13 +1583,14 @@ int32_t InputMethodSystemAbility::GetSecurityMode(int32_t &security) return ErrorCode::NO_ERROR; } -int32_t InputMethodSystemAbility::UnRegisteredProxyIme(UnRegisteredType type, const sptr &core) +int32_t InputMethodSystemAbility::RunUnRegisteredProxyIme( + const CallerProcessInfo &callerInfo, UnRegisteredType type, const sptr &core) { - if (!identityChecker_->IsNativeSa(IPCSkeleton::GetCallingTokenID())) { + if (!identityChecker_->IsNativeSa(callerInfo.tokenId)) { IMSA_HILOGE("not native sa!"); return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } - auto userId = GetCallingUserId(); + auto userId = GetUserId(callerInfo.uid); auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { IMSA_HILOGE("%{public}d session is nullptr", userId); @@ -1441,19 +1599,19 @@ int32_t InputMethodSystemAbility::UnRegisteredProxyIme(UnRegisteredType type, co return session->OnUnRegisteredProxyIme(type, core); } -int32_t InputMethodSystemAbility::CheckSwitchPermission(int32_t userId, const SwitchInfo &switchInfo, - SwitchTrigger trigger) +int32_t InputMethodSystemAbility::CheckSwitchPermission( + int32_t userId, const SwitchInfo &switchInfo, SwitchTrigger trigger, const CallerProcessInfo &callerInfo) { IMSA_HILOGD("trigger: %{public}d.", static_cast(trigger)); if (trigger == SwitchTrigger::IMSA) { return ErrorCode::NO_ERROR; } if (trigger == SwitchTrigger::SYSTEM_APP) { - if (!identityChecker_->IsSystemApp(IPCSkeleton::GetCallingFullTokenID())) { + if (!identityChecker_->IsSystemApp(callerInfo.fullTokenId)) { IMSA_HILOGE("not system app!"); return ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION; } - if (!identityChecker_->HasPermission(IPCSkeleton::GetCallingTokenID(), PERMISSION_CONNECT_IME_ABILITY)) { + if (!identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { IMSA_HILOGE("have not PERMISSION_CONNECT_IME_ABILITY"); return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } @@ -1461,14 +1619,13 @@ int32_t InputMethodSystemAbility::CheckSwitchPermission(int32_t userId, const Sw } if (trigger == SwitchTrigger::CURRENT_IME) { // PERMISSION_CONNECT_IME_ABILITY check temporarily reserved for application adaptation, will be deleted soon - if (identityChecker_->HasPermission(IPCSkeleton::GetCallingTokenID(), PERMISSION_CONNECT_IME_ABILITY)) { + if (identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return ErrorCode::NO_ERROR; } IMSA_HILOGE("have not PERMISSION_CONNECT_IME_ABILITY"); // switchInfo.subName.empty() check temporarily reserved for application adaptation, will be deleted soon auto currentBundleName = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId)->bundleName; - if (identityChecker_->IsBundleNameValid(IPCSkeleton::GetCallingTokenID(), currentBundleName) && - !switchInfo.subName.empty()) { + if (identityChecker_->IsBundleNameValid(callerInfo.tokenId, currentBundleName) && !switchInfo.subName.empty()) { return ErrorCode::NO_ERROR; } IMSA_HILOGE("not current ime!"); @@ -1479,18 +1636,17 @@ int32_t InputMethodSystemAbility::CheckSwitchPermission(int32_t userId, const Sw return ErrorCode::ERROR_BAD_PARAMETERS; } -bool InputMethodSystemAbility::IsStartInputTypePermitted(int32_t userId) +bool InputMethodSystemAbility::IsStartInputTypePermitted(int32_t userId, const CallerProcessInfo &callerInfo) { auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultImeInfo(userId); if (defaultIme == nullptr) { IMSA_HILOGE("failed to get default ime!"); return false; } - auto tokenId = IPCSkeleton::GetCallingTokenID(); - if (identityChecker_->IsBundleNameValid(tokenId, defaultIme->prop.name)) { + if (identityChecker_->IsBundleNameValid(callerInfo.tokenId, defaultIme->prop.name)) { return true; } - if (identityChecker_->HasPermission(tokenId, PERMISSION_CONNECT_IME_ABILITY)) { + if (identityChecker_->HasPermission(callerInfo.tokenId, PERMISSION_CONNECT_IME_ABILITY)) { return true; } auto session = UserSessionManager::GetInstance().GetUserSession(userId); @@ -1498,7 +1654,7 @@ bool InputMethodSystemAbility::IsStartInputTypePermitted(int32_t userId) IMSA_HILOGE("%{public}d session is nullptr", userId); return false; } - return identityChecker_->IsFocused(IPCSkeleton::GetCallingPid(), tokenId) && session->IsBoundToClient(); + return identityChecker_->IsFocused(callerInfo.pid, callerInfo.tokenId) && session->IsBoundToClient(); } int32_t InputMethodSystemAbility::ConnectSystemCmd(const sptr &channel, sptr &agent) @@ -1519,13 +1675,17 @@ int32_t InputMethodSystemAbility::ConnectSystemCmd(const sptr &ch void InputMethodSystemAbility::HandleWmsConnected(int32_t userId, int32_t screenId) { - if (userId == userId_) { - // device boot or scb in foreground reboot - HandleScbStarted(userId, screenId); - return; - } - // user switched - HandleUserSwitched(userId); + IMSA_HILOGD("in, key: %{public}s", key.c_str()); + auto task = [this, userId, screenId]() { + if (userId == userId_) { + // device boot or scb in foreground reboot + HandleScbStarted(userId, screenId); + return; + } + // user switched + HandleUserSwitched(userId); + }; + mainHandler_->PostTask(task, "HandleWmsConnected"); } void InputMethodSystemAbility::HandleScbStarted(int32_t userId, int32_t screenId) @@ -1562,21 +1722,23 @@ void InputMethodSystemAbility::HandleUserSwitched(int32_t userId) void InputMethodSystemAbility::HandleWmsDisconnected(int32_t userId, int32_t screenId) { - // clear client - auto session = UserSessionManager::GetInstance().GetUserSession(userId); - if (session != nullptr) { - session->RemoveCurrentClient(); - } - - if (userId == userId_) { - // user switched or scb in foreground died, not deal - return; - } - // scb in background died, stop ime - if (session == nullptr) { - return; - } - session->StopCurrentIme(); + auto task = [this, userId, screenId]() { + // clear client + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session != nullptr) { + session->RemoveCurrentClient(); + } + if (userId == userId_) { + // user switched or scb in foreground died, not deal + return; + } + // scb in background died, stop ime + if (session == nullptr) { + return; + } + session->StopCurrentIme(); + }; + mainHandler_->PostTask(task, "HandleWmsDisconnected"); } void InputMethodSystemAbility::HandleWmsStarted() @@ -1589,32 +1751,38 @@ void InputMethodSystemAbility::HandleWmsStarted() InitWmsConnectionMonitor(); return; } - // clear client - auto session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session != nullptr) { - session->RemoveCurrentClient(); - } - if (session == nullptr) { - UserSessionManager::GetInstance().AddUserSession(userId_); - } - session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr", userId_); - return; - } - session->AddRestartIme(); - StopImeInBackground(); + auto task = [this]() { + // clear client + auto session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session != nullptr) { + session->RemoveCurrentClient(); + } + if (session == nullptr) { + UserSessionManager::GetInstance().AddUserSession(userId_); + } + session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr", userId_); + return; + } + session->AddRestartIme(); + StopImeInBackground(); + }; + mainHandler_->PostTask(task, "HandleWmsStarted"); } void InputMethodSystemAbility::HandleFocusChanged(bool isFocused, int32_t pid, int32_t uid) { - int32_t userId = GetUserId(uid); - auto session = UserSessionManager::GetInstance().GetUserSession(userId); - if (session == nullptr) { - IMSA_HILOGE("[%{public}d, %{public}d] session is nullptr", uid, userId); - return; - } - isFocused ? session->OnFocused(pid, uid) : session->OnUnfocused(pid, uid); + auto task = [this, isFocused, pid, uid]() { + int32_t userId = GetUserId(uid); + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + IMSA_HILOGE("[%{public}d, %{public}d] session is nullptr", uid, userId); + return; + } + isFocused ? session->OnFocused(pid, uid) : session->OnUnfocused(pid, uid); + }; + mainHandler_->PostTask(task, "HandleFocusChanged"); } void InputMethodSystemAbility::HandleMemStarted() @@ -1622,17 +1790,20 @@ void InputMethodSystemAbility::HandleMemStarted() // singleton IMSA_HILOGI("MemMgr start."); Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(), 1, 1, INPUT_METHOD_SYSTEM_ABILITY_ID); - auto session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - UserSessionManager::GetInstance().AddUserSession(userId_); - } - session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr", userId_); - return; - } - session->AddRestartIme(); - StopImeInBackground(); + auto task = [this]() { + auto session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session == nullptr) { + UserSessionManager::GetInstance().AddUserSession(userId_); + } + session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr", userId_); + return; + } + session->AddRestartIme(); + StopImeInBackground(); + }; + mainHandler_->PostTask(task, "HandleMemStarted"); } void InputMethodSystemAbility::HandleOsAccountStarted() @@ -1658,7 +1829,7 @@ void InputMethodSystemAbility::StopImeInBackground() } } }; - serviceHandler_->PostTask(task, "StopImeInBackground", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); + taskHandler_->PostTask(task, "StopImeInBackground", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); } int32_t InputMethodSystemAbility::GetUserId(int32_t uid) @@ -1679,6 +1850,14 @@ int32_t InputMethodSystemAbility::GetCallingUserId() return GetUserId(uid); } +CallerProcessInfo InputMethodSystemAbility::GetCallerInfo() +{ + return { .pid = IPCSkeleton::GetCallingPid(), + .uid = IPCSkeleton::GetCallingUid(), + .tokenId = IPCSkeleton::GetCallingTokenID(), + .fullTokenId = IPCSkeleton::GetCallingFullTokenID() }; +} + bool InputMethodSystemAbility::IsCurrentIme(int32_t userId) { if (InputTypeManager::GetInstance().IsStarted()) { @@ -1689,7 +1868,7 @@ bool InputMethodSystemAbility::IsCurrentIme(int32_t userId) return identityChecker_->IsBundleNameValid(IPCSkeleton::GetCallingTokenID(), currentImeCfg->bundleName); } -int32_t InputMethodSystemAbility::StartInputType(int32_t userId, InputType type) +int32_t InputMethodSystemAbility::StartInputType(int32_t userId, InputType type, const CallerProcessInfo &callerInfo) { auto session = UserSessionManager::GetInstance().GetUserSession(userId); if (session == nullptr) { @@ -1711,7 +1890,7 @@ int32_t InputMethodSystemAbility::StartInputType(int32_t userId, InputType type) auto cfgIme = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); SwitchInfo switchInfo = { std::chrono::system_clock::now(), cfgIme->bundleName, cfgIme->subName }; session->GetSwitchQueue().Push(switchInfo); - return OnStartInputType(userId, switchInfo, false); + return OnStartInputType(userId, switchInfo, false, callerInfo); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 9fca44558..3e7b58ae6 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -44,6 +44,7 @@ constexpr int64_t INVALID_PID = -1; constexpr uint32_t STOP_IME_TIME = 600; constexpr const char *STRICT_MODE = "strictMode"; constexpr const char *ISOLATED_SANDBOX = "isolatedSandbox"; +std::shared_ptr PerUserSession::serviceTaskHandler_; PerUserSession::PerUserSession(int userId) : userId_(userId) { } @@ -57,6 +58,11 @@ PerUserSession::~PerUserSession() { } +void PerUserSession::SetServiceTaskHandler(std::shared_ptr handler) +{ + serviceTaskHandler_ = std::move(handler); +} + int PerUserSession::AddClientInfo(sptr inputClient, const InputClientInfo &clientInfo, ClientAddEvent event) { @@ -218,14 +224,17 @@ void PerUserSession::OnClientDied(sptr remote) if (remote == nullptr) { return; } - IMSA_HILOGI("userId: %{public}d.", userId_); - if (IsSameClient(remote, GetCurrentClient())) { - auto clientInfo = GetClientInfo(remote->AsObject()); - StopImeInput(clientInfo->bindImeType, clientInfo->channel); - SetCurrentClient(nullptr); - ExitCurrentInputType(); - } - RemoveClientInfo(remote->AsObject(), true); + auto task = [this, remote]() { + IMSA_HILOGI("OnClientDied userId: %{public}d.", userId_); + if (IsSameClient(remote, GetCurrentClient())) { + auto clientInfo = GetClientInfo(remote->AsObject()); + StopImeInput(clientInfo->bindImeType, clientInfo->channel); + SetCurrentClient(nullptr); + ExitCurrentInputType(); + } + RemoveClientInfo(remote->AsObject(), true); + }; + serviceTaskHandler_->PostTask(task, "OnClientDied"); } /** Handle the situation that an ime died @@ -256,26 +265,29 @@ void PerUserSession::OnImeDied(const sptr &remote, ImeType typ NotifyImeStopFinished(); return; } - if (clientInfo != nullptr && clientInfo->bindImeType == type) { - StopClientInput(clientInfo); - if (type == ImeType::IME) { + auto task = [this, type, clientInfo]() { + if (clientInfo != nullptr && clientInfo->bindImeType == type) { + StopClientInput(clientInfo); + if (type == ImeType::IME) { + StartImeInImeDied(); + } + return; + } + auto currentImeInfo = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_); + if (currentImeInfo == nullptr) { + IMSA_HILOGE("OnImeDied currentImeInfo is nullptr!"); + return; + } + auto defaultImeInfo = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp(); + if (defaultImeInfo == nullptr) { + IMSA_HILOGE("OnImeDied defaultImeInfo is nullptr!"); + return; + } + if (type == ImeType::IME && currentImeInfo->bundleName == defaultImeInfo->name) { StartImeInImeDied(); } - return; - } - auto currentImeInfo = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_); - if (currentImeInfo == nullptr) { - IMSA_HILOGE("currentImeInfo is nullptr!"); - return; - } - auto defaultImeInfo = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp(); - if (defaultImeInfo == nullptr) { - IMSA_HILOGE("defaultImeInfo is nullptr!"); - return; - } - if (type == ImeType::IME && currentImeInfo->bundleName == defaultImeInfo->name) { - StartImeInImeDied(); - } + }; + serviceTaskHandler_->PostTask(task, "OnImeDied"); } int32_t PerUserSession::RemoveIme(const sptr &core, ImeType type) -- Gitee