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 "block_queue.h" 20 #include "enable_ime_data_parser.h" 21 #include "event_status_manager.h" 22 #include "freeze_manager.h" 23 #include "i_input_method_core.h" 24 #include "ime_cfg_manager.h" 25 #include "input_type_manager.h" 26 #include "inputmethod_sysevent.h" 27 #include "inputmethod_message_handler.h" 28 #include "input_method_types.h" 29 #include "want.h" 30 31 namespace OHOS { 32 namespace Rosen { 33 struct CallingWindowInfo; 34 } 35 } 36 namespace OHOS { 37 namespace MiscServices { 38 enum class ImeStatus : uint32_t { STARTING, READY, EXITING }; 39 enum class ImeEvent : uint32_t { 40 START_IME, 41 START_IME_TIMEOUT, 42 STOP_IME, 43 SET_CORE_AND_AGENT, 44 }; 45 enum class ImeAction : uint32_t { 46 DO_NOTHING, 47 HANDLE_STARTING_IME, 48 FORCE_STOP_IME, 49 STOP_READY_IME, 50 START_AFTER_FORCE_STOP, 51 DO_SET_CORE_AND_AGENT, 52 DO_ACTION_IN_NULL_IME_DATA, 53 DO_ACTION_IN_IME_EVENT_CONVERT_FAILED, 54 }; 55 struct ImeData { 56 static constexpr int64_t START_TIME_OUT = 8000; 57 sptr<IInputMethodCore> core{ nullptr }; 58 sptr<IRemoteObject> agent{ nullptr }; 59 sptr<InputDeathRecipient> deathRecipient{ nullptr }; 60 pid_t pid; 61 std::shared_ptr<FreezeManager> freezeMgr; 62 ImeStatus imeStatus{ ImeStatus::STARTING }; 63 std::pair<std::string, std::string> ime; // first: bundleName second:extName 64 int64_t startTime{ 0 }; ImeDataImeData65 ImeData(sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, sptr<InputDeathRecipient> deathRecipient, 66 pid_t imePid) 67 : core(std::move(core)), agent(std::move(agent)), deathRecipient(std::move(deathRecipient)), pid(imePid), 68 freezeMgr(std::make_shared<FreezeManager>(imePid)) 69 { 70 } 71 }; 72 /**@class PerUserSession 73 * 74 * @brief The class provides session management in input method management service 75 * 76 * This class manages the sessions between input clients and input method engines for each unlocked user. 77 */ 78 class PerUserSession { 79 public: 80 explicit PerUserSession(int userId); 81 PerUserSession(int32_t userId, const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler); 82 ~PerUserSession(); 83 84 int32_t OnPrepareInput(const InputClientInfo &clientInfo); 85 int32_t OnStartInput( 86 const InputClientInfo &inputClientInfo, sptr<IRemoteObject> &agent, std::pair<int64_t, std::string> &imeInfo); 87 int32_t OnReleaseInput(const sptr<IInputClient> &client); 88 int32_t OnSetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent); 89 int32_t OnHideCurrentInput(); 90 int32_t OnShowCurrentInput(); 91 int32_t OnShowInput(sptr<IInputClient> client, int32_t requestKeyboardReason = 0); 92 int32_t OnHideInput(sptr<IInputClient> client); 93 int32_t OnRequestShowInput(); 94 int32_t OnRequestHideInput(int32_t callingPid); 95 void OnSecurityChange(int32_t security); 96 void OnHideSoftKeyBoardSelf(); 97 void NotifyImeChangeToClients(const Property &property, const SubProperty &subProperty); 98 int32_t SwitchSubtype(const SubProperty &subProperty); 99 int32_t SwitchSubtypeWithoutStartIme(const SubProperty &subProperty); 100 void OnFocused(int32_t pid, int32_t uid); 101 void OnUnfocused(int32_t pid, int32_t uid); 102 void OnUserUnlocked(); 103 int64_t GetCurrentClientPid(); 104 int64_t GetInactiveClientPid(); 105 int32_t OnPanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info); 106 int32_t OnUpdateListenEventFlag(const InputClientInfo &clientInfo); 107 int32_t OnRegisterProxyIme(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent); 108 int32_t OnUnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core); 109 int32_t InitConnect(pid_t pid); 110 111 int32_t StartCurrentIme(bool isStopCurrentIme = false); 112 int32_t StartIme(const std::shared_ptr<ImeNativeCfg> &ime, bool isStopCurrentIme = false); 113 int32_t StopCurrentIme(); 114 bool RestartIme(); 115 void AddRestartIme(); 116 117 bool IsProxyImeEnable(); 118 bool IsBoundToClient(); 119 bool IsCurrentImeByPid(int32_t pid); 120 int32_t RestoreCurrentImeSubType(); 121 int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown); 122 bool CheckSecurityMode(); 123 int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent); 124 int32_t RemoveCurrentClient(); 125 std::shared_ptr<ImeData> GetReadyImeData(ImeType type); 126 std::shared_ptr<ImeData> GetImeData(ImeType type); 127 BlockQueue<SwitchInfo>& GetSwitchQueue(); 128 bool IsWmsReady(); 129 bool CheckPwdInputPatternConv(InputClientInfo &clientInfo); 130 int32_t RestoreCurrentIme(); 131 int32_t SetInputType(); 132 std::shared_ptr<ImeNativeCfg> GetImeNativeCfg(int32_t userId, const std::string &bundleName, 133 const std::string &subName); 134 int32_t OnSetCallingWindow(uint32_t callingWindowId, sptr<IInputClient> client); 135 int32_t GetInputStartInfo(bool& isInputStart, uint32_t& callingWndId, int32_t& requestKeyboardReason); 136 bool IsSaReady(int32_t saId); 137 void UpdateUserLockState(); 138 void TryUnloadSystemAbility(); 139 void OnCallingDisplayIdChanged(const int32_t windowId, const int32_t callingPid, const uint64_t displayId); 140 ImfCallingWindowInfo GetCallingWindowInfo(const InputClientInfo &clientInfo); 141 private: 142 struct ResetManager { 143 uint32_t num{ 0 }; 144 time_t last{}; 145 }; 146 enum ClientAddEvent : int32_t { 147 PREPARE_INPUT = 0, 148 START_LISTENING, 149 }; 150 int32_t userId_; // the id of the user to whom the object is linking 151 std::recursive_mutex mtx; 152 std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> mapClients_; 153 #ifdef IMF_ON_DEMAND_START_STOP_SA_ENABLE 154 static const int MAX_IME_START_TIME = 2000; 155 #else 156 static const int MAX_IME_START_TIME = 1500; 157 #endif 158 std::mutex clientLock_; 159 sptr<IInputClient> currentClient_; // the current input client 160 std::mutex resetLock; 161 ResetManager manager; 162 using IpcExec = std::function<int32_t()>; 163 164 PerUserSession(const PerUserSession &); 165 PerUserSession &operator=(const PerUserSession &); 166 PerUserSession(const PerUserSession &&); 167 PerUserSession &operator=(const PerUserSession &&); 168 169 static constexpr int32_t MAX_WAIT_TIME = 5000; 170 BlockQueue<SwitchInfo> switchQueue_{ MAX_WAIT_TIME }; 171 172 void OnClientDied(sptr<IInputClient> remote); 173 void OnImeDied(const sptr<IInputMethodCore> &remote, ImeType type); 174 175 int AddClientInfo(sptr<IRemoteObject> inputClient, const InputClientInfo &clientInfo, ClientAddEvent event); 176 void RemoveClientInfo(const sptr<IRemoteObject> &client, bool isClientDied = false); 177 int32_t RemoveClient(const sptr<IInputClient> &client, bool isUnbindFromClient = false, 178 bool isInactiveClient = false, bool isNotifyClientAsync = false); 179 void DeactivateClient(const sptr<IInputClient> &client); 180 std::shared_ptr<InputClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient); 181 std::shared_ptr<InputClientInfo> GetClientInfo(pid_t pid); 182 std::shared_ptr<InputClientInfo> GetCurClientInfo(); 183 void UpdateClientInfo(const sptr<IRemoteObject> &client, 184 const std::unordered_map<UpdateFlag, 185 std::variant<bool, uint32_t, ImeType, ClientState, TextTotalConfig, ClientType>> &updateInfos); 186 187 int32_t InitImeData(const std::pair<std::string, std::string> &ime); 188 int32_t UpdateImeData(sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, pid_t pid); 189 int32_t AddImeData(ImeType type, sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, pid_t pid); 190 void RemoveImeData(ImeType type, bool isImeDied); 191 int32_t RemoveIme(const sptr<IInputMethodCore> &core, ImeType type); 192 std::shared_ptr<ImeData> GetValidIme(ImeType type); 193 194 int32_t BindClientWithIme(const std::shared_ptr<InputClientInfo> &clientInfo, ImeType type, 195 bool isBindFromClient = false); 196 void UnBindClientWithIme(const std::shared_ptr<InputClientInfo> ¤tClientInfo, 197 bool isUnbindFromClient = false, bool isNotifyClientAsync = false); 198 void StopClientInput( 199 const std::shared_ptr<InputClientInfo> &clientInfo, bool isStopInactiveClient = false, bool isAsync = false); 200 void StopImeInput(ImeType currentType, const sptr<IRemoteObject> ¤tChannel); 201 202 int32_t HideKeyboard(const sptr<IInputClient> ¤tClient); 203 int32_t ShowKeyboard(const sptr<IInputClient> ¤tClient, int32_t requestKeyboardReason = 0); 204 205 int32_t InitInputControlChannel(); 206 void StartImeInImeDied(); 207 void StartImeIfInstalled(); 208 void SetCurrentClient(sptr<IInputClient> client); 209 sptr<IInputClient> GetCurrentClient(); 210 void ReplaceCurrentClient(const sptr<IInputClient> &client); 211 void SetInactiveClient(sptr<IInputClient> client); 212 sptr<IInputClient> GetInactiveClient(); 213 bool IsCurClientFocused(int32_t pid, int32_t uid); 214 bool IsCurClientUnFocused(int32_t pid, int32_t uid); 215 bool IsSameClient(sptr<IInputClient> source, sptr<IInputClient> dest); 216 217 bool IsImeStartInBind(ImeType bindImeType, ImeType startImeType); 218 bool IsProxyImeStartInBind(ImeType bindImeType, ImeType startImeType); 219 bool IsProxyImeStartInImeBind(ImeType bindImeType, ImeType startImeType); 220 bool IsImeBindTypeChanged(ImeType bindImeType); 221 std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> GetClientMap(); 222 int32_t RequestIme(const std::shared_ptr<ImeData> &data, RequestType type, const IpcExec &exec); 223 224 bool WaitForCurrentImeStop(); 225 void NotifyImeStopFinished(); 226 bool GetCurrentUsingImeId(ImeIdentification &imeId); 227 bool CanStartIme(); 228 int32_t ChangeToDefaultImeIfNeed( 229 const std::shared_ptr<ImeNativeCfg> &ime, std::shared_ptr<ImeNativeCfg> &imeToStart); 230 AAFwk::Want GetWant(const std::shared_ptr<ImeNativeCfg> &ime); 231 int32_t StartCurrentIme(const std::shared_ptr<ImeNativeCfg> &ime); 232 int32_t StartNewIme(const std::shared_ptr<ImeNativeCfg> &ime); 233 int32_t StartInputService(const std::shared_ptr<ImeNativeCfg> &ime); 234 int32_t ForceStopCurrentIme(bool isNeedWait = true); 235 int32_t StopReadyCurrentIme(); 236 int32_t HandleFirstStart(const std::shared_ptr<ImeNativeCfg> &ime, bool isStopCurrentIme); 237 int32_t HandleStartImeTimeout(const std::shared_ptr<ImeNativeCfg> &ime); 238 bool GetInputTypeToStart(std::shared_ptr<ImeNativeCfg> &imeToStart); 239 // from service notify clients input start and stop 240 int32_t NotifyInputStartToClients(uint32_t callingWndId, int32_t requestKeyboardReason = 0); 241 int32_t NotifyInputStopToClients(); 242 bool IsNotifyInputStop(const sptr<IInputClient> &client); 243 void HandleImeBindTypeChanged(InputClientInfo &newClientInfo); 244 int32_t NotifyCallingDisplayChanged(uint64_t displayId); 245 bool GetCallingWindowInfo(const InputClientInfo &clientInfo, Rosen::CallingWindowInfo &callingWindowInfo); 246 std::mutex imeStartLock_; 247 248 BlockData<bool> isImeStarted_{ MAX_IME_START_TIME, false }; 249 std::mutex imeDataLock_; 250 std::unordered_map<ImeType, std::shared_ptr<ImeData>> imeData_; 251 std::mutex inactiveClientLock_; 252 sptr<IInputClient> inactiveClient_; // the inactive input client 253 std::mutex focusedClientLock_; 254 255 std::atomic<bool> isSwitching_ = false; 256 std::mutex imeStopMutex_; 257 std::condition_variable imeStopCv_; 258 259 std::mutex restartMutex_; 260 int32_t restartTasks_ = 0; 261 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_{ nullptr }; 262 ImeAction GetImeAction(ImeEvent action); 263 static inline const std::map<std::pair<ImeStatus, ImeEvent>, std::pair<ImeStatus, ImeAction>> imeEventConverter_ = { 264 { { ImeStatus::READY, ImeEvent::START_IME }, { ImeStatus::READY, ImeAction::DO_NOTHING } }, 265 { { ImeStatus::STARTING, ImeEvent::START_IME }, { ImeStatus::STARTING, ImeAction::HANDLE_STARTING_IME } }, 266 { { ImeStatus::EXITING, ImeEvent::START_IME }, { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } }, 267 { { ImeStatus::READY, ImeEvent::START_IME_TIMEOUT }, { ImeStatus::READY, ImeAction::DO_NOTHING } }, 268 { { ImeStatus::STARTING, ImeEvent::START_IME_TIMEOUT }, 269 { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } }, 270 { { ImeStatus::EXITING, ImeEvent::START_IME_TIMEOUT }, 271 { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } }, 272 { { ImeStatus::READY, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::STOP_READY_IME } }, 273 { { ImeStatus::STARTING, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::FORCE_STOP_IME } }, 274 { { ImeStatus::EXITING, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::FORCE_STOP_IME } }, 275 { { ImeStatus::READY, ImeEvent::SET_CORE_AND_AGENT }, { ImeStatus::READY, ImeAction::DO_NOTHING } }, 276 { { ImeStatus::STARTING, ImeEvent::SET_CORE_AND_AGENT }, 277 { ImeStatus::READY, ImeAction::DO_SET_CORE_AND_AGENT } }, 278 { { ImeStatus::EXITING, ImeEvent::SET_CORE_AND_AGENT }, { ImeStatus::EXITING, ImeAction::DO_NOTHING } } 279 }; 280 std::string runningIme_; 281 std::atomic<bool> isUserUnlocked_{ false }; 282 }; 283 } // namespace MiscServices 284 } // namespace OHOS 285 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H