diff --git a/frameworks/inputmethod_ability/include/input_method_ability.h b/frameworks/inputmethod_ability/include/input_method_ability.h index 16c4792fcef69687fe8a45f5aacb5cbf164b5360..ce688d3f9ef684ef5c7b3b26c35531dc181f72c0 100644 --- a/frameworks/inputmethod_ability/include/input_method_ability.h +++ b/frameworks/inputmethod_ability/include/input_method_ability.h @@ -113,6 +113,8 @@ namespace MiscServices { void InitialInputWindow(); void ShowInputWindow(bool isShowKeyboard); void DissmissInputWindow(); + + void BindServiceAndClient(); }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/inputmethod_ability/src/input_method_ability.cpp b/frameworks/inputmethod_ability/src/input_method_ability.cpp index 4b380a1b92ffdf0df8e5f3d4897c66cd4e4ad148..5f6b636f63b4449fbe423425ebb6cd25d80580b4 100644 --- a/frameworks/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/inputmethod_ability/src/input_method_ability.cpp @@ -105,7 +105,6 @@ namespace MiscServices { } sptr stub = new InputMethodCoreStub(0); stub->SetMessageHandler(msgHandler); - sptr stub2 = stub; sptr inputMethodAgentStub(new InputMethodAgentStub()); inputMethodAgentStub->SetMessageHandler(msgHandler); @@ -113,11 +112,11 @@ namespace MiscServices { MessageParcel data; if (!(data.WriteInterfaceToken(mImms->GetDescriptor()) - && data.WriteRemoteObject(stub2->AsObject()) + && data.WriteRemoteObject(stub->AsObject()) && data.WriteRemoteObject(inputMethodAgent->AsObject()))) { return; } - mImms->SetCoreAndAgent(data); + mImms->SetCoreAndAgentDeprecated(data); } void InputMethodAbility::Initialize() @@ -543,5 +542,29 @@ namespace MiscServices { listener->OnInputStop(ParaHandle::GetDefaultIme(Utils::ToUserId(IPCSkeleton::GetCallingUid()))); } } + + void InputMethodAbility::BindServiceAndClient() + { + IMSA_HILOGI("InputMethodAbility::BindServiceAndClient"); + mImms = GetImsaProxy(); + if (mImms == nullptr) { + IMSA_HILOGI("mImms is nullptr"); + return; + } + sptr stub = new InputMethodCoreStub(0); + stub->SetMessageHandler(msgHandler); + + sptr inputMethodAgentStub(new InputMethodAgentStub()); + inputMethodAgentStub->SetMessageHandler(msgHandler); + sptr inputMethodAgent = sptr(new InputMethodAgentProxy(inputMethodAgentStub)); + + MessageParcel data; + if (!(data.WriteInterfaceToken(mImms->GetDescriptor()) + && data.WriteRemoteObject(stub->AsObject()) + && data.WriteRemoteObject(inputMethodAgent->AsObject()))) { + return; + } + mImms->SetCoreAndAgent(data); + } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/inputmethod_controller/include/input_method_controller.h b/frameworks/inputmethod_controller/include/input_method_controller.h index 88144eb3e9e232b25cb93995d22cace7d9a0ebb3..bea56d2786f0cd6776a7a75ce641b7ff399d5bc1 100644 --- a/frameworks/inputmethod_controller/include/input_method_controller.h +++ b/frameworks/inputmethod_controller/include/input_method_controller.h @@ -73,17 +73,22 @@ namespace MiscServices { void OnSelectionChange(std::u16string text, int start, int end); void OnConfigurationChange(Configuration info); bool dispatchKeyEvent(std::shared_ptr keyEvent); - int32_t DisplayOptionalInputMethod(); std::vector ListInputMethodCommon(InputMethodStatus status); std::vector ListInputMethod(); std::vector ListInputMethod(bool enable); int32_t GetEnterKeyType(); int32_t GetInputPattern(); std::shared_ptr GetCurrentInputMethod(); - int32_t HideCurrentInput(); - int32_t ShowCurrentInput(); void SetCallingWindow(uint32_t windowId); int32_t SwitchInputMethod(const Property &target); + int32_t ShowSoftKeyboard(); + int32_t HideSoftKeyboard(); + int32_t ShowOptionalInputMethod(); + + // Deprecated innerkits with no permission check, kept for compatibility + int32_t ShowCurrentInput(); + int32_t HideCurrentInput(); + int32_t DisplayOptionalInputMethod(); private: InputMethodController(); @@ -99,6 +104,7 @@ namespace MiscServices { void SetInputMethodAgent(sptr &object); std::shared_ptr GetInputMethodAgent(); void WorkThread(); + int32_t SendDataByProxy(std::function &, MessageParcel &)> callback); sptr mInputDataChannel; sptr mClient; diff --git a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h index fd637f44d156f24a3424132e76d2e9c3a2950db7..a07315cadfccae41c526135eee2089c47a562d54 100644 --- a/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h +++ b/frameworks/inputmethod_controller/include/input_method_system_ability_proxy.h @@ -67,8 +67,15 @@ namespace MiscServices { int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) override; int32_t SwitchInputMethod(const InputMethodProperty &target) override; + // Deprecated because of no permission check, kept for compatibility + int32_t DisplayOptionalInputMethodDeprecated(MessageParcel &data) override; + int32_t HideCurrentInputDeprecated(MessageParcel &data) override; + int32_t ShowCurrentInputDeprecated(MessageParcel &data) override; + void SetCoreAndAgentDeprecated(MessageParcel &data) override; + private: static inline BrokerDelegator delegator_; + int32_t SendRequestToService(MessageParcel &data, uint32_t code); }; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/inputmethod_controller/src/input_method_controller.cpp b/frameworks/inputmethod_controller/src/input_method_controller.cpp index e455a32fe9a2151f0c5e21926fb718c51c7343a9..6d95cbd5a384092fe26fb44546ab33895e05a1fc 100644 --- a/frameworks/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/inputmethod_controller/src/input_method_controller.cpp @@ -246,27 +246,17 @@ using namespace MessageID; int32_t InputMethodController::HideCurrentInput() { IMSA_HILOGI("InputMethodController::HideCurrentInput"); - if (!mImms) { - return ErrorCode::ERROR_KBD_HIDE_FAILED; - } - MessageParcel data; - if (!(data.WriteInterfaceToken(mImms->GetDescriptor()))) { - return ErrorCode::ERROR_KBD_HIDE_FAILED; - } - return mImms->HideCurrentInput(data); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->HideCurrentInputDeprecated(data); + }); } int32_t InputMethodController::ShowCurrentInput() { IMSA_HILOGI("InputMethodController::ShowCurrentInput"); - if (!mImms) { - return ErrorCode::ERROR_KBD_SHOW_FAILED; - } - MessageParcel data; - if (!(data.WriteInterfaceToken(mImms->GetDescriptor()))) { - return ErrorCode::ERROR_KBD_SHOW_FAILED; - } - return mImms->ShowCurrentInput(data); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->ShowCurrentInputDeprecated(data); + }); } void InputMethodController::Close() @@ -298,14 +288,9 @@ using namespace MessageID; int32_t InputMethodController::DisplayOptionalInputMethod() { IMSA_HILOGI("InputMethodController::DisplayOptionalInputMethod"); - if (!mImms) { - return ErrorCode::ERROR_STATUS_BAD_VALUE; - } - MessageParcel data; - if (!(data.WriteInterfaceToken(mImms->GetDescriptor()))) { - return ErrorCode::ERROR_STATUS_BAD_VALUE; - } - return mImms->displayOptionalInputMethod(data); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->DisplayOptionalInputMethodDeprecated(data); + }); } std::vector InputMethodController::ListInputMethodCommon(InputMethodStatus status) @@ -566,5 +551,46 @@ using namespace MessageID; std::lock_guard lock(agentLock_); return mAgent; } + + int32_t InputMethodController::ShowSoftKeyboard() + { + IMSA_HILOGI("InputMethodController ShowSoftKeyboard"); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->ShowCurrentInput(data); + }); + } + + int32_t InputMethodController::HideSoftKeyboard() + { + IMSA_HILOGI("InputMethodController HideSoftKeyboard"); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->HideCurrentInput(data); + }); + } + + int32_t InputMethodController::ShowOptionalInputMethod() + { + IMSA_HILOGI("InputMethodController::ShowOptionalInputMethod"); + return SendDataByProxy([](sptr &proxy, MessageParcel &data) -> int32_t { + return proxy->displayOptionalInputMethod(data); + }); + } + + int32_t InputMethodController::SendDataByProxy( + std::function &, MessageParcel &)> callback) + { + IMSA_HILOGI("InputMethodController::SendDataToByProxy"); + sptr proxy = mImms; + if (proxy == nullptr) { + IMSA_HILOGE("proxy is nullptr"); + return ErrorCode::ERROR_NULL_POINTER; + } + MessageParcel data; + if (!(data.WriteInterfaceToken(proxy->GetDescriptor()))) { + IMSA_HILOGE("write descriptor failed"); + return ErrorCode::ERROR_STATUS_FAILED_TRANSACTION; + } + return callback(proxy, data); + } } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp index de12f03a5d48850d9d603c3c1bf157e1f3357fe0..21a1f3bc0dfa56ceac59bef333017fa55dcfbc46 100644 --- a/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp +++ b/frameworks/inputmethod_controller/src/input_method_system_ability_proxy.cpp @@ -46,21 +46,8 @@ namespace MiscServices { int32_t InputMethodSystemAbilityProxy::displayOptionalInputMethod(MessageParcel& data) { - MessageParcel reply; - MessageOption option; - - auto ret = Remote()->SendRequest(DISPLAY_OPTIONAL_INPUT_METHOD, data, reply, option); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::displayOptionalInputMethod SendRequest failed"); - return ret; - } - - ret = reply.ReadInt32(); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::displayOptionalInputMethod reply failed"); - return ret; - } - return ErrorCode::NO_ERROR; + IMSA_HILOGI("InputMethodSystemAbilityProxy::displayOptionalInputMethod"); + return SendRequestToService(data, DISPLAY_OPTIONAL_INPUT_METHOD); } void InputMethodSystemAbilityProxy::releaseInput(MessageParcel& data) @@ -119,60 +106,26 @@ namespace MiscServices { } } - void InputMethodSystemAbilityProxy::SetCoreAndAgent(MessageParcel& data) + void InputMethodSystemAbilityProxy::SetCoreAndAgent(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityProxy::SetCoreAndAgent"); - - auto remote = Remote(); - if (!remote) { - return; - } MessageParcel reply; - MessageOption option { - MessageOption::TF_SYNC - }; - - Remote()->SendRequest(SET_CORE_AND_AGENT, data, reply, option); + MessageOption option{ MessageOption::TF_SYNC }; + if (Remote()->SendRequest(SET_CORE_AND_AGENT, data, reply, option) != NO_ERROR) { + IMSA_HILOGE("SendRequest failed"); + } } int32_t InputMethodSystemAbilityProxy::HideCurrentInput(MessageParcel& data) { IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput"); - MessageParcel reply; - MessageOption option; - - auto ret = Remote()->SendRequest(HIDE_CURRENT_INPUT, data, reply, option); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput SendRequest failed"); - return ErrorCode::ERROR_KBD_HIDE_FAILED; - } - - ret = reply.ReadInt32(); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInput reply failed"); - return ErrorCode::ERROR_KBD_HIDE_FAILED; - } - return ErrorCode::NO_ERROR; + return SendRequestToService(data, HIDE_CURRENT_INPUT); } int32_t InputMethodSystemAbilityProxy::ShowCurrentInput(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput"); - MessageParcel reply; - MessageOption option; - - auto ret = Remote()->SendRequest(SHOW_CURRENT_INPUT, data, reply, option); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput SendRequest failed"); - return ErrorCode::ERROR_KBD_SHOW_FAILED; - } - - ret = reply.ReadInt32(); - if (ret != NO_ERROR) { - IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInput reply failed"); - return ErrorCode::ERROR_KBD_SHOW_FAILED; - } - return ErrorCode::NO_ERROR; + return SendRequestToService(data, SHOW_CURRENT_INPUT); } int32_t InputMethodSystemAbilityProxy::Prepare(int32_t displayId, sptr &client, @@ -474,5 +427,50 @@ namespace MiscServices { ret = reply.ReadInt32(); return ret; } + + int32_t InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::ShowCurrentInputDeprecated"); + return SendRequestToService(data, SHOW_CURRENT_INPUT_DEPRECATED); + } + + int32_t InputMethodSystemAbilityProxy::HideCurrentInputDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::HideCurrentInputDeprecated"); + return SendRequestToService(data, HIDE_CURRENT_INPUT_DEPRECATED); + } + + int32_t InputMethodSystemAbilityProxy::DisplayOptionalInputMethodDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::DisplayOptionalInputMethodDeprecated"); + return SendRequestToService(data, DISPLAY_OPTIONAL_INPUT_METHOD_DEPRECATED); + } + + void InputMethodSystemAbilityProxy::SetCoreAndAgentDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::SetCoreAndAgentDeprecated"); + MessageParcel reply; + MessageOption option{ MessageOption::TF_SYNC }; + if (Remote()->SendRequest(SET_CORE_AND_AGENT_DEPRECATED, data, reply, option) != NO_ERROR) { + IMSA_HILOGE("SendRequest failed"); + } + } + + int32_t InputMethodSystemAbilityProxy::SendRequestToService(MessageParcel &data, uint32_t code) + { + IMSA_HILOGI("InputMethodSystemAbilityProxy::SendRequestToService"); + MessageParcel reply; + MessageOption option{ MessageOption::TF_SYNC }; + auto ret = Remote()->SendRequest(code, data, reply, option); + if (ret != NO_ERROR) { + IMSA_HILOGE("SendRequest failed"); + return ret; + } + ret = reply.ReadInt32(); + if (ret != NO_ERROR) { + IMSA_HILOGE("reply error"); + } + return ret; + } } // namespace MiscServices } // namespace OHOS diff --git a/services/BUILD.gn b/services/BUILD.gn index 2cf6af9505aa7fe9662dc1603ac692b81ad2fb45..a876cc0a49fdf37548aa302ef2d04fe547c0d2e6 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -73,6 +73,7 @@ ohos_shared_library("inputmethod_service") { [ "//base/inputmethod/imf/services/dialog/js:dialog_ime_js_files_etc" ] external_deps = [ "ability_base:want", + "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/services/include/i_input_method_system_ability.h b/services/include/i_input_method_system_ability.h index e7a386787449c7cbd81bbf973a2d8cfa507d2f64..395fc587183f589db23cd8d20c46cf3e0be990c4 100644 --- a/services/include/i_input_method_system_ability.h +++ b/services/include/i_input_method_system_ability.h @@ -53,6 +53,10 @@ namespace MiscServices { SET_CORE_AND_AGENT, DISPLAY_OPTIONAL_INPUT_METHOD, SWITCH_INPUT_METHOD, + SHOW_CURRENT_INPUT_DEPRECATED, + HIDE_CURRENT_INPUT_DEPRECATED, + DISPLAY_OPTIONAL_INPUT_METHOD_DEPRECATED, + SET_CORE_AND_AGENT_DEPRECATED, }; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.miscservices.inputmethod.IInputMethodSystemAbility"); @@ -73,6 +77,12 @@ namespace MiscServices { virtual std::vector ListInputMethod(InputMethodStatus stauts) = 0; virtual int32_t listKeyboardType(const std::u16string& imeId, std::vector *types) = 0; virtual int32_t SwitchInputMethod(const InputMethodProperty &target) = 0; + + // Deprecated because of no permission check, kept for compatibility + virtual void SetCoreAndAgentDeprecated(MessageParcel &data) = 0; + virtual int32_t HideCurrentInputDeprecated(MessageParcel &data) = 0; + virtual int32_t ShowCurrentInputDeprecated(MessageParcel &data) = 0; + virtual int32_t DisplayOptionalInputMethodDeprecated(MessageParcel &data) = 0; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/include/input_method_system_ability_stub.h b/services/include/input_method_system_ability_stub.h index 2f3a01ef66cc0d091db288ec39012a30e5a5a80c..c4db0a6f9fea6cf18f31ed94499611f3444dd809 100644 --- a/services/include/input_method_system_ability_stub.h +++ b/services/include/input_method_system_ability_stub.h @@ -42,7 +42,15 @@ namespace MiscServices { virtual std::vector ListInputMethodByUserId(int32_t userId, InputMethodStatus status) = 0; int32_t OnSwitchInputMethod(MessageParcel &data); + // Deprecated because of no permission check, kept for compatibility + int32_t DisplayOptionalInputMethodDeprecated(MessageParcel &data) override; + int32_t HideCurrentInputDeprecated(MessageParcel &data) override; + int32_t ShowCurrentInputDeprecated(MessageParcel &data) override; + void SetCoreAndAgentDeprecated(MessageParcel &data) override; + protected: + bool CheckPermission(const std::string &permission); + int32_t SendMessageToService(int32_t code, std::function callback); int32_t getUserId(int32_t uid); int USER_ID_CHANGE_VALUE = 200000; // user range }; diff --git a/services/src/input_method_system_ability_stub.cpp b/services/src/input_method_system_ability_stub.cpp index 0e11cded61737f53131f0aeda7e5845dd91316e9..212b28856f0ce0275337144a790c77a2b79aee42 100644 --- a/services/src/input_method_system_ability_stub.cpp +++ b/services/src/input_method_system_ability_stub.cpp @@ -17,12 +17,16 @@ #include +#include "access_token.h" +#include "accesstoken_kit.h" #include "ipc_skeleton.h" #include "message_handler.h" namespace OHOS { namespace MiscServices { using namespace MessageID; + using namespace Security::AccessToken; + static const std::string PERMISSION_CONNECT_IME_ABILITY = "ohos.permission.CONNECT_IME_ABILITY"; /*! Handle the transaction from the remote binder \n Run in binder thread \param code transaction code number @@ -65,9 +69,7 @@ namespace MiscServices { break; } case SET_CORE_AND_AGENT: { - MessageParcel *msgParcel = (MessageParcel*) &data; - SetCoreAndAgent(*msgParcel); - reply.WriteInt32(NO_ERROR); + SetCoreAndAgent(data); break; } case GET_DISPLAY_MODE: { @@ -132,13 +134,13 @@ namespace MiscServices { break; } case DISPLAY_OPTIONAL_INPUT_METHOD: { - displayOptionalInputMethod(data); - reply.WriteInt32(NO_ERROR); + int32_t ret = displayOptionalInputMethod(data); + reply.WriteInt32(ret); break; } case HIDE_CURRENT_INPUT: { - HideCurrentInput(data); - reply.WriteInt32(NO_ERROR); + int32_t ret = HideCurrentInput(data); + reply.WriteInt32(ret); break; } case SHOW_CURRENT_INPUT: { @@ -155,6 +157,25 @@ namespace MiscServices { OnGetCurrentInputMethod(reply); break; } + case SHOW_CURRENT_INPUT_DEPRECATED: { + int32_t ret = ShowCurrentInputDeprecated(data); + reply.WriteInt32(ret); + break; + } + case HIDE_CURRENT_INPUT_DEPRECATED: { + int32_t ret = HideCurrentInputDeprecated(data); + reply.WriteInt32(ret); + break; + } + case DISPLAY_OPTIONAL_INPUT_METHOD_DEPRECATED: { + int ret = DisplayOptionalInputMethodDeprecated(data); + reply.WriteInt32(ret); + break; + } + case SET_CORE_AND_AGENT_DEPRECATED: { + SetCoreAndAgentDeprecated(data); + break; + } default: { return BRemoteObject::OnRemoteRequest(code, data, reply, option); } @@ -190,17 +211,17 @@ namespace MiscServices { int32_t InputMethodSystemAbilityStub::displayOptionalInputMethod(MessageParcel& data) { IMSA_HILOGI("InputMethodSystemAbilityStub::displayOptionalInputMethod"); + if (!CheckPermission(PERMISSION_CONNECT_IME_ABILITY)) { + IMSA_HILOGE("Permission denied"); + return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; + } int32_t pid = IPCSkeleton::GetCallingPid(); int32_t uid = IPCSkeleton::GetCallingUid(); int32_t userId = getUserId(uid); - MessageParcel *parcel = new MessageParcel(); - parcel->WriteInt32(userId); - parcel->WriteInt32(pid); - parcel->WriteInt32(uid); - - Message *msg = new Message(MSG_ID_DISPLAY_OPTIONAL_INPUT_METHOD, parcel); - MessageHandler::Instance()->SendMessage(msg); - return ErrorCode::NO_ERROR; + return SendMessageToService( + MSG_ID_DISPLAY_OPTIONAL_INPUT_METHOD, [pid, uid, userId](MessageParcel &parcel) -> bool { + return parcel.WriteInt32(userId) && parcel.WriteInt32(pid) && parcel.WriteInt32(uid); + }); } /*! Release input @@ -268,6 +289,10 @@ namespace MiscServices { void InputMethodSystemAbilityStub::SetCoreAndAgent(MessageParcel& data) { IMSA_HILOGI("InputMethodSystemAbilityStub::SetCoreAndAgent"); + if (!CheckPermission(PERMISSION_CONNECT_IME_ABILITY)) { + IMSA_HILOGE("Permission denied"); + return; + } int32_t uid = IPCSkeleton::GetCallingUid(); int32_t userId = getUserId(uid); MessageParcel *parcel = new MessageParcel(); @@ -279,37 +304,28 @@ namespace MiscServices { MessageHandler::Instance()->SendMessage(msg); } - int32_t InputMethodSystemAbilityStub::HideCurrentInput(MessageParcel& data) + int32_t InputMethodSystemAbilityStub::HideCurrentInput(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityStub::HideCurrentInput"); - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = getUserId(uid); - MessageParcel *parcel = new MessageParcel(); - parcel->WriteInt32(userId); - - Message *msg = new Message(MSG_HIDE_CURRENT_INPUT, parcel); - MessageHandler::Instance()->SendMessage(msg); - return ErrorCode::NO_ERROR; + if (!CheckPermission(PERMISSION_CONNECT_IME_ABILITY)) { + IMSA_HILOGE("Permission denied"); + return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; + } + int userId = getUserId(IPCSkeleton::GetCallingUid()); + return SendMessageToService( + MSG_HIDE_CURRENT_INPUT, [userId](MessageParcel &parcel) -> bool { return parcel.WriteInt32(userId); }); } int32_t InputMethodSystemAbilityStub::ShowCurrentInput(MessageParcel &data) { IMSA_HILOGI("InputMethodSystemAbilityStub::ShowCurrentInput"); - int32_t uid = IPCSkeleton::GetCallingUid(); - int32_t userId = getUserId(uid); - - auto *parcel = new (std::nothrow) MessageParcel(); - if (parcel == nullptr) { - return ErrorCode::ERROR_EX_NULL_POINTER; - } - parcel->WriteInt32(userId); - - auto *msg = new (std::nothrow) Message(MSG_SHOW_CURRENT_INPUT, parcel); - if (msg == nullptr) { - return ErrorCode::ERROR_EX_NULL_POINTER; + if (!CheckPermission(PERMISSION_CONNECT_IME_ABILITY)) { + IMSA_HILOGE("Permission denied"); + return ErrorCode::ERROR_STATUS_PERMISSION_DENIED; } - MessageHandler::Instance()->SendMessage(msg); - return ErrorCode::NO_ERROR; + int userId = getUserId(IPCSkeleton::GetCallingUid()); + return SendMessageToService( + MSG_SHOW_CURRENT_INPUT, [userId](MessageParcel &parcel) -> bool { return parcel.WriteInt32(userId); }); } int32_t InputMethodSystemAbilityStub::OnSwitchInputMethod(MessageParcel &data) @@ -329,6 +345,7 @@ namespace MiscServices { auto *msg = new (std::nothrow) Message(MSG_ID_SWITCH_INPUT_METHOD, parcel); if (msg == nullptr) { + delete parcel; return ErrorCode::ERROR_EX_NULL_POINTER; } MessageHandler::Instance()->SendMessage(msg); @@ -362,6 +379,85 @@ namespace MiscServices { } } + int32_t InputMethodSystemAbilityStub::ShowCurrentInputDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::ShowCurrentInputDeprecated"); + int userId = getUserId(IPCSkeleton::GetCallingUid()); + return SendMessageToService( + MSG_SHOW_CURRENT_INPUT, [userId](MessageParcel &parcel) -> bool { return parcel.WriteInt32(userId); }); + } + + int32_t InputMethodSystemAbilityStub::HideCurrentInputDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::HideCurrentInputDeprecated"); + int userId = getUserId(IPCSkeleton::GetCallingUid()); + return SendMessageToService( + MSG_HIDE_CURRENT_INPUT, [userId](MessageParcel &parcel) -> bool { return parcel.WriteInt32(userId); }); + } + + int32_t InputMethodSystemAbilityStub::DisplayOptionalInputMethodDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::DisplayOptionalInputMethodDeprecated"); + int32_t pid = IPCSkeleton::GetCallingPid(); + int32_t uid = IPCSkeleton::GetCallingUid(); + int32_t userId = getUserId(uid); + return SendMessageToService( + MSG_ID_DISPLAY_OPTIONAL_INPUT_METHOD, [pid, uid, userId](MessageParcel &parcel) -> bool { + return parcel.WriteInt32(userId) && parcel.WriteInt32(pid) && parcel.WriteInt32(uid); + }); + } + + void InputMethodSystemAbilityStub::SetCoreAndAgentDeprecated(MessageParcel &data) + { + IMSA_HILOGI("InputMethodSystemAbilityStub::SetCoreAndAgentDeprecated"); + int32_t userId = getUserId(IPCSkeleton::GetCallingUid()); + int32_t ret = SendMessageToService(MSG_ID_SET_CORE_AND_AGENT, [userId, &data](MessageParcel &parcel) -> bool { + return parcel.WriteInt32(userId) && parcel.WriteRemoteObject(data.ReadRemoteObject()) + && parcel.WriteRemoteObject(data.ReadRemoteObject()); + }); + if (ret != NO_ERROR) { + IMSA_HILOGE("send message to service failed: %{public}s", ErrorCode::ToString(ret)); + } + } + + bool InputMethodSystemAbilityStub::CheckPermission(const std::string &permission) + { + IMSA_HILOGI("Check Permission"); + AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); + TypeATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); + if (tokenType == TOKEN_INVALID) { + IMSA_HILOGE("invalid token id %{public}d", tokenId); + return false; + } + int result = AccessTokenKit::VerifyAccessToken(tokenId, permission); + IMSA_HILOGI("CheckPermission result: %{public}s", result == PERMISSION_GRANTED ? "success" : "failed"); + return result == PERMISSION_GRANTED; + } + + int32_t InputMethodSystemAbilityStub::SendMessageToService( + int32_t code, std::function callback) + { + auto parcel = new (std::nothrow) MessageParcel(); + if (parcel == nullptr) { + IMSA_HILOGE("parcel is nullptr"); + return ErrorCode::ERROR_NULL_POINTER; + } + bool ret = callback(*parcel); + if (!ret) { + IMSA_HILOGE("failed to write parcel"); + delete parcel; + return ErrorCode::ERROR_STATUS_FAILED_TRANSACTION; + } + auto msg = new Message(code, parcel); + if (msg == nullptr) { + IMSA_HILOGE("msg is nullptr"); + delete parcel; + return ErrorCode::ERROR_NULL_POINTER; + } + MessageHandler::Instance()->SendMessage(msg); + return ErrorCode::NO_ERROR; + } + /*! Get user id from uid \param uid the uid from which the remote call is \return return user id of the remote caller diff --git a/unitest/src/input_method_controller_test.cpp b/unitest/src/input_method_controller_test.cpp index 8e019fe103200180a39a38fd17dc4bae3820c78e..2e164eaf6198e524397682c583360d9fc9059976 100644 --- a/unitest/src/input_method_controller_test.cpp +++ b/unitest/src/input_method_controller_test.cpp @@ -274,6 +274,48 @@ namespace MiscServices { IMSA_HILOGI("IMC TEST OVER"); } + /** + * @tc.name: testShowSoftKeyboard + * @tc.desc: IMC ShowSoftKeyboard + * @tc.type: FUNC + */ + HWTEST_F(InputMethodControllerTest, testShowSoftKeyboard, TestSize.Level0) + { + IMSA_HILOGI("IMC ShowSoftKeyboard Test START"); + sptr imc = InputMethodController::GetInstance(); + EXPECT_NE(imc, nullptr); + int32_t ret = imc->ShowSoftKeyboard(); + EXPECT_NE(ret, 0); + } + + /** + * @tc.name: testHideSoftKeyboard + * @tc.desc: IMC HideSoftKeyboard + * @tc.type: FUNC + */ + HWTEST_F(InputMethodControllerTest, testHideSoftKeyboard, TestSize.Level0) + { + IMSA_HILOGI("IMC HideSoftKeyboard Test START"); + sptr imc = InputMethodController::GetInstance(); + EXPECT_NE(imc, nullptr); + int32_t ret = imc->HideSoftKeyboard(); + EXPECT_NE(ret, 0); + } + + /** + * @tc.name: testShowOptionalInputMethod + * @tc.desc: IMC ShowOptionalInputMethod + * @tc.type: FUNC + */ + HWTEST_F(InputMethodControllerTest, testShowOptionalInputMethod, TestSize.Level2) + { + IMSA_HILOGI("IMC ShowOptionalInputMethod Test START"); + sptr imc = InputMethodController::GetInstance(); + EXPECT_NE(imc, nullptr); + int32_t ret = imc->ShowOptionalInputMethod(); + EXPECT_NE(ret, 0); + } + /** * @tc.name: testIMCGetCurrentInputMethod * @tc.desc: IMC GetCurrentInputMethod @@ -325,28 +367,6 @@ namespace MiscServices { EXPECT_TRUE(!properties.empty()); } - /** - * @tc.name: testIMSAProxyShowCurrentInput - * @tc.desc: IMSAProxy ShowCurrentInput. - * @tc.type: FUNC - * @tc.require: issueI5NXHK - */ - HWTEST_F(InputMethodControllerTest, testIMSAProxyShowCurrentInput, TestSize.Level0) - { - IMSA_HILOGI("IMSAProxy ShowCurrentInput Test START"); - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - - auto systemAbility = systemAbilityManager->GetSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID, ""); - - sptr imsaProxy = new InputMethodSystemAbilityProxy(systemAbility); - - MessageParcel data; - data.WriteInterfaceToken(imsaProxy->GetDescriptor()); - int32_t ret = imsaProxy->ShowCurrentInput(data); - EXPECT_TRUE(ret == 0); - } - /** * @tc.name: testIMCHideCurrentInput * @tc.desc: IMC HideCurrentInput.