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 FM_IMMS_PROJECT_PERUSERSESSION_H 17 #define FM_IMMS_PROJECT_PERUSERSESSION_H 18 19 #include <thread> 20 #include <mutex> 21 #include <map> 22 #include <memory> 23 24 #include "iremote_object.h" 25 #include "i_input_control_channel.h" 26 #include "i_input_client.h" 27 #include "i_input_method_core.h" 28 #include "i_input_data_channel.h" 29 #include "i_input_method_agent.h" 30 #include "input_attribute.h" 31 #include "input_method_property.h" 32 #include "input_method_setting.h" 33 #include "input_control_channel_stub.h" 34 #include "message.h" 35 #include "message_handler.h" 36 #include "global.h" 37 #include "platform.h" 38 #include "keyboard_type.h" 39 #include "ability_manager_interface.h" 40 #include "ability_connect_callback_proxy.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 class RemoteObjectDeathRecipient : public IRemoteObject::DeathRecipient { 45 public: 46 RemoteObjectDeathRecipient(int userId, int msgId); 47 ~RemoteObjectDeathRecipient(); 48 void OnRemoteDied(const wptr<IRemoteObject>& who) override; 49 50 private: 51 int userId_; // the id of the user to whom the object is linking 52 int msgId_; // the message id can be MessageID::MSG_ID_CLIENT_DIED and MessageID::MSG_ID_IMS_DIED 53 }; 54 55 struct ClientInfo { 56 int pid; 57 int uid; 58 int userId; 59 int displayId; 60 sptr<IInputClient> client; 61 sptr<IInputDataChannel> channel; 62 InputAttribute attribute; 63 sptr<RemoteObjectDeathRecipient> deathRecipient; 64 }; 65 66 struct ResetManager { 67 uint32_t num {0}; 68 time_t last {}; 69 }; 70 71 /*! \class PerUserSession 72 \brief The class provides session management in input method management service 73 74 This class manages the sessions between input clients and input method engines for each unlocked user. 75 */ 76 class PerUserSession { 77 enum : int32_t { 78 DEFAULT_IME = 0, // index for default input method service 79 SECURITY_IME = 1, // index for security input method service 80 MAX_IME = 2, // the maximum count of ims started for a user 81 }; 82 83 public: 84 explicit PerUserSession(int userId); 85 ~PerUserSession(); 86 87 void SetCurrentIme(InputMethodProperty *ime); 88 void SetSecurityIme(InputMethodProperty *ime); 89 void SetInputMethodSetting(InputMethodSetting *setting); 90 void ResetIme(InputMethodProperty *defaultIme, InputMethodProperty *securityIme); 91 void OnPackageRemoved(const std::u16string& packageName); 92 93 int GetDisplayMode(); 94 int GetKeyboardWindowHeight(int retHeight); 95 KeyboardType *GetCurrentKeyboardType(); 96 97 int OnSettingChanged(const std::u16string& key, const std::u16string& value); 98 void CreateWorkThread(MessageHandler& handler); 99 void JoinWorkThread(); 100 void StopInputService(std::string imeId); 101 static bool StartInputService(); 102 103 private: 104 int userId_; // the id of the user to whom the object is linking 105 int userState; // the state of the user to whom the object is linking 106 int displayId; // the id of the display screen on which the user is 107 int currentIndex; 108 std::map<IRemoteObject *, ClientInfo *> mapClients; 109 static const int MIN_IME = 2; 110 static const int MAX_RESTART_NUM = 3; 111 static const int IME_RESET_TIME_OUT = 300; 112 static const int MAX_RESET_WAIT_TIME = 1600000; 113 114 InputMethodProperty *currentIme[MAX_IME]; // 0 - the default ime. 1 - security ime 115 116 InputControlChannelStub *localControlChannel[MAX_IME]; 117 sptr<IInputControlChannel> inputControlChannel[MAX_IME]; 118 sptr<IInputMethodCore> imsCore[MAX_IME]; // the remote handlers of input method service 119 sptr<IRemoteObject> inputMethodToken[MAX_IME]; // the window token of keyboard 120 int currentKbdIndex[MAX_IME]; // current keyboard index 121 int lastImeIndex; // The last ime which showed keyboard 122 InputMethodSetting *inputMethodSetting; // The pointer referred to the object in PerUserSetting 123 int currentDisplayMode; // the display mode of the current keyboard 124 125 sptr<IInputMethodAgent> imsAgent; 126 InputChannel *imsChannel; // the write channel created by input method service 127 std::mutex clientLock_; 128 sptr<IInputClient> currentClient; // the current input client 129 sptr<IInputClient> needReshowClient; // the input client for which keyboard need to re-show 130 131 sptr<RemoteObjectDeathRecipient> imsDeathRecipient; 132 MessageHandler *msgHandler = nullptr; // message handler working with Work Thread 133 std::thread workThreadHandler; // work thread handler 134 std::mutex mtx; // mutex to lock the operations among multi work threads 135 sptr<AAFwk::AbilityConnectionProxy> connCallback; 136 std::mutex resetLock; 137 ResetManager manager[MAX_IME]; 138 139 PerUserSession(const PerUserSession&); 140 PerUserSession& operator =(const PerUserSession&); 141 PerUserSession(const PerUserSession&&); 142 PerUserSession& operator =(const PerUserSession&&); 143 KeyboardType *GetKeyboardType(int imeIndex, int typeIndex); 144 void ResetCurrentKeyboardType(int imeIndex); 145 int OnCurrentKeyboardTypeChanged(int index, const std::u16string& value); 146 void CopyInputMethodService(int imeIndex); 147 ClientInfo *GetClientInfo(const sptr<IInputClient>& inputClient); 148 void WorkThread(); 149 void OnPrepareInput(Message *msg); 150 void OnReleaseInput(Message *msg); 151 void OnStartInput(Message *msg); 152 void OnStopInput(Message *msg); 153 void SetCoreAndAgent(Message *msg); 154 void OnClientDied(IRemoteObject *remoteObject); 155 void OnImsDied(IRemoteObject *remoteObject); 156 void OnHideKeyboardSelf(int flags); 157 void OnAdvanceToNext(); 158 void OnSetDisplayMode(int mode); 159 void OnRestartIms(int index, const std::u16string& imeId); 160 void OnUserLocked(); 161 int AddClient(const ClientInfo &clientInfo); 162 void RemoveClient(IRemoteObject *inputClient); 163 int StartInputMethod(int index); 164 int StopInputMethod(int index); 165 int ShowKeyboard(const sptr<IInputClient>& inputClient); 166 int HideKeyboard(const sptr<IInputClient>& inputClient); 167 void SetDisplayId(int displayId); 168 int GetImeIndex(const sptr<IInputClient>& inputClient); 169 static sptr<AAFwk::IAbilityManager> GetAbilityManagerService(); 170 void SendAgentToSingleClient(const sptr<IInputClient>& inputClient); 171 void InitInputControlChannel(); 172 void SendAgentToAllClients(); 173 void ResetImeError(uint32_t index); 174 bool IsRestartIme(uint32_t index); 175 void ClearImeData(uint32_t index); 176 void SetCurrentClient(sptr<IInputClient> client); 177 sptr<IInputClient> GetCurrentClient(); 178 }; 179 } 180 } 181 #endif // FM_IMMS_PROJECT_PERUSERSESSION_H 182