1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SERVICES_INCLUDE_PERUSER_SESSION_H 17 #define SERVICES_INCLUDE_PERUSER_SESSION_H 18 19 #include <functional> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <thread> 24 #include <variant> 25 26 #include "block_data.h" 27 #include "event_handler.h" 28 #include "event_status_manager.h" 29 #include "global.h" 30 #include "i_input_client.h" 31 #include "i_input_control_channel.h" 32 #include "i_input_data_channel.h" 33 #include "i_input_method_agent.h" 34 #include "i_input_method_core.h" 35 #include "ime_cfg_manager.h" 36 #include "input_attribute.h" 37 #include "input_client_info.h" 38 #include "input_control_channel_stub.h" 39 #include "input_death_recipient.h" 40 #include "input_method_info.h" 41 #include "input_method_property.h" 42 #include "input_window_info.h" 43 #include "inputmethod_sysevent.h" 44 #include "iremote_object.h" 45 #include "message.h" 46 #include "message_handler.h" 47 #include "panel_info.h" 48 #include "unRegistered_type.h" 49 50 namespace OHOS { 51 namespace MiscServices { 52 struct ImeData { 53 sptr<IInputMethodCore> core{ nullptr }; 54 sptr<IInputMethodAgent> agent{ nullptr }; 55 sptr<InputDeathRecipient> deathRecipient{ nullptr }; ImeDataImeData56 ImeData(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent, sptr<InputDeathRecipient> deathRecipient) 57 : core(std::move(core)), agent(std::move(agent)), deathRecipient(std::move(deathRecipient)) 58 { 59 } 60 }; 61 /**@class PerUserSession 62 * 63 * @brief The class provides session management in input method management service 64 * 65 * This class manages the sessions between input clients and input method engines for each unlocked user. 66 */ 67 class PerUserSession { 68 public: 69 explicit PerUserSession(int userId); 70 ~PerUserSession(); 71 72 int32_t OnPrepareInput(const InputClientInfo &clientInfo); 73 int32_t OnStartInput(const InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent); 74 int32_t OnReleaseInput(const sptr<IInputClient> &client); 75 int32_t OnSetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent); 76 int32_t OnHideCurrentInput(); 77 int32_t OnShowCurrentInput(); 78 int32_t OnShowInput(sptr<IInputClient> client); 79 int32_t OnHideInput(sptr<IInputClient> client); 80 int32_t OnRequestShowInput(); 81 int32_t OnRequestHideInput(); 82 void OnSecurityChange(int32_t &security); 83 void OnHideSoftKeyBoardSelf(); 84 void NotifyImeChangeToClients(const Property &property, const SubProperty &subProperty); 85 int32_t SwitchSubtype(const SubProperty &subProperty); 86 void UpdateCurrentUserId(int32_t userId); 87 void OnFocused(int32_t pid, int32_t uid); 88 void OnUnfocused(int32_t pid, int32_t uid); 89 int64_t GetCurrentClientPid(); 90 int32_t OnPanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo); 91 int32_t OnUpdateListenEventFlag(const InputClientInfo &clientInfo); 92 int32_t OnRegisterProxyIme(const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent); 93 int32_t OnUnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core); 94 bool StartCurrentIme(int32_t userId, bool isRetry); 95 void StopCurrentIme(); 96 bool StartInputService(const std::shared_ptr<ImeNativeCfg> &ime, bool isRetry); 97 bool ActivateIme(const std::shared_ptr<ImeNativeCfg> &ime, bool isRetry); 98 void DeactivateIme(const std::string &bundleName, const std::string &subName); 99 bool IsProxyImeEnable(); 100 bool IsBoundToClient(); 101 int32_t ExitCurrentInputType(); 102 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown); 103 bool CheckSecurityMode(); 104 bool IsWmsReady(); 105 106 private: 107 struct ResetManager { 108 uint32_t num{ 0 }; 109 time_t last{}; 110 }; 111 enum ClientAddEvent : int32_t { 112 PREPARE_INPUT = 0, 113 START_LISTENING, 114 }; 115 int32_t userId_; // the id of the user to whom the object is linking 116 std::recursive_mutex mtx; 117 std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> mapClients_; 118 static const int MAX_RESTART_NUM = 3; 119 static const int IME_RESET_TIME_OUT = 3; 120 static const int MAX_IME_START_TIME = 1000; 121 std::mutex clientLock_; 122 sptr<IInputClient> currentClient_; // the current input client 123 std::mutex resetLock; 124 ResetManager manager; 125 126 PerUserSession(const PerUserSession &); 127 PerUserSession &operator=(const PerUserSession &); 128 PerUserSession(const PerUserSession &&); 129 PerUserSession &operator=(const PerUserSession &&); 130 131 void OnClientDied(sptr<IInputClient> remote); 132 void OnImeDied(const sptr<IInputMethodCore> &remote, ImeType type); 133 134 int AddClientInfo(sptr<IRemoteObject> inputClient, const InputClientInfo &clientInfo, ClientAddEvent event); 135 void RemoveClientInfo(const sptr<IRemoteObject> &client, bool isClientDied = false); 136 int32_t RemoveClient(const sptr<IInputClient> &client, bool isUnbindFromClient = false); 137 void DeactivateClient(const sptr<IInputClient> &client); 138 std::shared_ptr<InputClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient); 139 std::shared_ptr<InputClientInfo> GetClientInfo(pid_t pid); 140 void UpdateClientInfo(const sptr<IRemoteObject> &client, 141 const std::unordered_map<UpdateFlag, std::variant<bool, uint32_t, ImeType, ClientState, TextTotalConfig>> 142 &updateInfos); 143 144 int32_t AddImeData(ImeType type, sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent); 145 void RemoveImeData(ImeType type, bool isImeDied); 146 int32_t RemoveIme(const sptr<IInputMethodCore> &core, ImeType type); 147 std::shared_ptr<ImeData> GetImeData(ImeType type); 148 std::shared_ptr<ImeData> GetValidIme(ImeType type); 149 150 int32_t BindClientWithIme( 151 const std::shared_ptr<InputClientInfo> &clientInfo, ImeType type, bool isBindFromClient = false); 152 void UnBindClientWithIme( 153 const std::shared_ptr<InputClientInfo> ¤tClientInfo, bool isUnbindFromClient = false); 154 void StopClientInput(const sptr<IInputClient> ¤tClient); 155 void StopImeInput(ImeType currentType, const sptr<IInputDataChannel> ¤tChannel); 156 157 int32_t HideKeyboard(const sptr<IInputClient> ¤tClient); 158 int32_t ShowKeyboard(const sptr<IInputClient> ¤tClient); 159 160 int32_t InitInputControlChannel(); 161 bool IsRestartIme(); 162 void RestartIme(); 163 void SetCurrentClient(sptr<IInputClient> client); 164 sptr<IInputClient> GetCurrentClient(); 165 void ReplaceCurrentClient(const sptr<IInputClient> &client); 166 void SetInactiveClient(sptr<IInputClient> client); 167 sptr<IInputClient> GetInactiveClient(); 168 bool IsCurrentClient(int32_t pid, int32_t uid); 169 bool IsSameClient(sptr<IInputClient> source, sptr<IInputClient> dest); 170 171 bool IsImeStartInBind(ImeType bindImeType, ImeType startImeType); 172 bool IsProxyImeStartInBind(ImeType bindImeType, ImeType startImeType); 173 bool IsProxyImeStartInImeBind(ImeType bindImeType, ImeType startImeType); 174 bool IsBindProxyImeInImeBind(ImeType bindImeType); 175 bool IsBindImeInProxyImeBind(ImeType bindImeType); 176 bool IsImeBindChanged(ImeType bindImeType); 177 std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> GetClientMap(); 178 179 BlockData<bool> isImeStarted_{ MAX_IME_START_TIME, false }; 180 std::mutex imeDataLock_; 181 std::unordered_map<ImeType, std::shared_ptr<ImeData>> imeData_; 182 std::mutex attachLock_; 183 std::condition_variable imeAttachCv_; 184 std::mutex inactiveClientLock_; 185 sptr<IInputClient> inactiveClient_; // the inactive input client 186 std::mutex focusedClientLock_; 187 }; 188 } // namespace MiscServices 189 } // namespace OHOS 190 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H 191