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