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 25 #include "ability_connect_callback_proxy.h" 26 #include "ability_manager_interface.h" 27 #include "global.h" 28 #include "i_input_client.h" 29 #include "i_input_control_channel.h" 30 #include "i_input_data_channel.h" 31 #include "i_input_method_agent.h" 32 #include "i_input_method_core.h" 33 #include "input_attribute.h" 34 #include "input_control_channel_stub.h" 35 #include "input_method_info.h" 36 #include "input_method_property.h" 37 #include "inputmethod_sysevent.h" 38 #include "iremote_object.h" 39 #include "message.h" 40 #include "message_handler.h" 41 42 namespace OHOS { 43 namespace MiscServices { 44 class RemoteObjectDeathRecipient : public IRemoteObject::DeathRecipient { 45 public: 46 using RemoteDiedHandler = std::function<void(const wptr<IRemoteObject> &)>; 47 void SetDeathRecipient(RemoteDiedHandler handler); 48 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 49 private: 50 RemoteDiedHandler handler_; 51 }; 52 53 struct ClientInfo { 54 int pid; // the process id of the process in which the input client is running 55 int uid; // the uid of the process in which the input client is running 56 int userId; // the user if of the user under which the input client is running 57 int displayId; // the display id on which the input client is showing 58 sptr<IInputClient> client; // the remote object handler for the service to callback to the input client 59 sptr<IInputDataChannel> channel; // the remote object handler for IMSA callback to input client 60 sptr<RemoteObjectDeathRecipient> deathRecipient; 61 InputAttribute attribute; // the input attribute of the input client 62 bool isShowKeyBoard{ false }; 63 }; 64 65 struct ResetManager { 66 uint32_t num {0}; 67 time_t last {}; 68 }; 69 70 /*! \class PerUserSession 71 \brief The class provides session management in input method management service 72 73 This class manages the sessions between input clients and input method engines for each unlocked user. 74 */ 75 class PerUserSession { 76 enum : int32_t { 77 CURRENT_IME = 0, // index for current ime 78 SECURITY_IME, // index for security ime 79 MAX_IME // the maximum count of ime started for a user 80 }; 81 82 public: 83 explicit PerUserSession(int userId); 84 ~PerUserSession(); 85 86 int32_t OnPrepareInput(const ClientInfo &clientInfo); 87 int32_t OnStartInput(sptr<IInputClient> client, bool isShowKeyboard); 88 int32_t OnStopInput(sptr<IInputClient> client); 89 int32_t OnReleaseInput(sptr<IInputClient> client); 90 int32_t OnSetCoreAndAgent(sptr<IInputMethodCore> core, sptr<IInputMethodAgent> agent); 91 int OnHideKeyboardSelf(int flags); 92 int OnShowKeyboardSelf(); 93 94 void CreateWorkThread(MessageHandler& handler); 95 void JoinWorkThread(); 96 void StopInputService(std::string imeId); 97 void OnInputMethodSwitched(const Property &property, const SubProperty &subProperty); 98 99 void SetCurrentSubProperty(const SubProperty &subProperty); 100 SubProperty GetCurrentSubProperty(); 101 void UpdateCurrentUserId(int32_t userId); 102 103 private: 104 int userId_; // the id of the user to whom the object is linking 105 int userState = UserState::USER_STATE_STARTED; // 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 std::map<sptr<IRemoteObject>, std::shared_ptr<ClientInfo>> mapClients; 108 static const int MAX_RESTART_NUM = 3; 109 static const int IME_RESET_TIME_OUT = 300; 110 static const int MAX_RESET_WAIT_TIME = 1600000; 111 112 sptr<IInputControlChannel> inputControlChannel[MAX_IME]; 113 std::mutex imsCoreLock_; 114 sptr<IInputMethodCore> imsCore[MAX_IME]; // the remote handlers of input method service 115 116 sptr<IInputMethodAgent> imsAgent; 117 std::mutex clientLock_; 118 sptr<IInputClient> currentClient; // the current input client 119 120 sptr<RemoteObjectDeathRecipient> imsDeathRecipient = nullptr; 121 MessageHandler *msgHandler = nullptr; // message handler working with Work Thread 122 std::thread workThreadHandler; // work thread handler 123 std::recursive_mutex mtx; // mutex to lock the operations among multi work threads 124 std::mutex resetLock; 125 ResetManager manager[MAX_IME]; 126 127 PerUserSession(const PerUserSession&); 128 PerUserSession& operator =(const PerUserSession&); 129 PerUserSession(const PerUserSession&&); 130 PerUserSession& operator =(const PerUserSession&&); 131 std::shared_ptr<ClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient); 132 void WorkThread(); 133 134 void OnClientDied(sptr<IInputClient> remote); 135 void OnImsDied(sptr<IInputMethodCore> remote); 136 137 int AddClient(sptr<IRemoteObject> inputClient, const ClientInfo &clientInfo); 138 void UpdateClient(sptr<IRemoteObject> inputClient, bool isShowKeyboard); 139 void RemoveClient(sptr<IRemoteObject> inputClient); 140 int ShowKeyboard(const sptr<IInputClient>& inputClient, bool isShowKeyboard); 141 int HideKeyboard(const sptr<IInputClient>& inputClient); 142 int GetImeIndex(const sptr<IInputClient>& inputClient); 143 void SendAgentToSingleClient(const ClientInfo &clientInfo); 144 void InitInputControlChannel(); 145 void SendAgentToAllClients(); 146 bool IsRestartIme(uint32_t index); 147 void ClearImeData(uint32_t index); 148 void SetCurrentClient(sptr<IInputClient> client); 149 sptr<IInputClient> GetCurrentClient(); 150 void SetImsCore(int32_t index, sptr<IInputMethodCore> core); 151 sptr<IInputMethodCore> GetImsCore(int32_t index); IsValid(int32_t index)152 static inline bool IsValid(int32_t index) 153 { 154 return index >= CURRENT_IME && index <= SECURITY_IME; 155 } 156 157 std::mutex propertyLock_; 158 SubProperty currentSubProperty; 159 }; 160 } // namespace MiscServices 161 } // namespace OHOS 162 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H