• 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 <unordered_set>
20 
21 #include "block_queue.h"
22 #include "client_group.h"
23 #include "event_status_manager.h"
24 #include "iinput_method_core.h"
25 #include "ime_cfg_manager.h"
26 #include "ime_connection.h"
27 #include "input_method_types.h"
28 #include "input_type_manager.h"
29 #include "inputmethod_message_handler.h"
30 #include "inputmethod_sysevent.h"
31 #include "want.h"
32 #include "ime_state_manager.h"
33 #include "input_method_client_types.h"
34 
35 namespace OHOS {
36 namespace Rosen {
37 struct CallingWindowInfo;
38 }
39 }
40 namespace OHOS {
41 namespace MiscServices {
42 const std::string IME_MIRROR_NAME = "proxyIme_IME_MIRROR";
43 enum class ImeStatus : uint32_t { STARTING, READY, EXITING };
44 enum class ImeEvent : uint32_t {
45     START_IME,
46     START_IME_TIMEOUT,
47     STOP_IME,
48     SET_CORE_AND_AGENT,
49 };
50 enum LargeMemoryState : int32_t {
51     LARGE_MEMORY_NEED = 2,
52     LARGE_MEMORY_NOT_NEED = 3
53 };
54 enum class ImeAction : uint32_t {
55     DO_NOTHING,
56     HANDLE_STARTING_IME,
57     FORCE_STOP_IME,
58     STOP_READY_IME,
59     START_AFTER_FORCE_STOP,
60     DO_SET_CORE_AND_AGENT,
61     DO_ACTION_IN_NULL_IME_DATA,
62     DO_ACTION_IN_IME_EVENT_CONVERT_FAILED,
63 };
64 struct ImeData {
65     static constexpr int64_t START_TIME_OUT = 8000;
66     sptr<IInputMethodCore> core{ nullptr };
67     sptr<IRemoteObject> agent{ nullptr };
68     sptr<InputDeathRecipient> deathRecipient{ nullptr };
69     pid_t pid;
70     std::shared_ptr<ImeStateManager> imeStateManager;
71     ImeStatus imeStatus{ ImeStatus::STARTING };
72     std::pair<std::string, std::string> ime; // first: bundleName  second:extName
73     int64_t startTime{ 0 };
ImeDataImeData74     ImeData(sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, sptr<InputDeathRecipient> deathRecipient,
75         pid_t imePid)
76         : core(std::move(core)), agent(std::move(agent)), deathRecipient(std::move(deathRecipient)), pid(imePid)
77     {
78     }
79 
IsImeMirrorImeData80     bool IsImeMirror() const
81     {
82         return ime.first == IME_MIRROR_NAME;
83     }
84     ImeExtendInfo imeExtendInfo;
85 };
86 
87 enum class StartPreDefaultImeStatus : uint32_t { NO_NEED, HAS_STARTED, TO_START };
88 /**@class PerUserSession
89  *
90  * @brief The class provides session management in input method management service
91  *
92  * This class manages the sessions between input clients and input method engines for each unlocked user.
93  */
94 class PerUserSession {
95 public:
96     explicit PerUserSession(int userId);
97     PerUserSession(int32_t userId, const std::shared_ptr<AppExecFwk::EventHandler> &eventHandler);
98     ~PerUserSession();
99 
100     int32_t OnPrepareInput(const InputClientInfo &clientInfo);
101     int32_t OnStartInput(const InputClientInfo &inputClientInfo, std::vector<sptr<IRemoteObject>> &agents,
102         std::vector<BindImeInfo> &imeInfos);
103     int32_t OnReleaseInput(const sptr<IInputClient> &client, uint32_t sessionId);
104     int32_t OnSetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent);
105     int32_t OnHideCurrentInput(uint64_t displayId);
106     int32_t OnShowCurrentInput(uint64_t displayId);
107     int32_t OnShowInput(sptr<IInputClient> client, int32_t requestKeyboardReason = 0);
108     int32_t OnHideInput(sptr<IInputClient> client);
109     int32_t OnRequestShowInput(uint64_t displayId);
110     int32_t OnRequestHideInput(int32_t callingPid, uint64_t displayId);
111     void OnSecurityChange(int32_t security);
112     void OnHideSoftKeyBoardSelf();
113     void NotifyImeChangeToClients(const Property &property, const SubProperty &subProperty);
114     int32_t SwitchSubtype(const SubProperty &subProperty);
115     int32_t SwitchSubtypeWithoutStartIme(const SubProperty &subProperty);
116     void OnFocused(uint64_t displayId, int32_t pid, int32_t uid);
117     void OnUnfocused(uint64_t displayId, int32_t pid, int32_t uid);
118     void OnScreenUnlock();
119     void OnScreenLock();
120     int64_t GetCurrentClientPid(uint64_t displayId);
121     int64_t GetInactiveClientPid(uint64_t displayId);
122     int32_t OnPanelStatusChange(const InputWindowStatus &status, const ImeWindowInfo &info, uint64_t displayId);
123     int32_t OnUpdateListenEventFlag(const InputClientInfo &clientInfo);
124     int32_t OnRegisterProxyIme(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent, pid_t pid);
125     int32_t OnUnRegisteredProxyIme(UnRegisteredType type, const sptr<IInputMethodCore> &core, pid_t pid);
126     int32_t OnBindImeMirror(const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent);
127     int32_t OnUnbindImeMirror();
128     int32_t UpdateLargeMemorySceneState(const int32_t memoryState);
129     int32_t OnRegisterProxyIme(
130         uint64_t displayId, const sptr<IInputMethodCore> &core, const sptr<IRemoteObject> &agent);
131     int32_t OnUnregisterProxyIme(uint64_t displayId);
132     int32_t InitConnect(pid_t pid);
133 
134     int32_t StartCurrentIme(bool isStopCurrentIme = false);
135     int32_t StartIme(const std::shared_ptr<ImeNativeCfg> &ime, bool isStopCurrentIme = false);
136     int32_t StopCurrentIme();
137     bool RestartIme();
138     void AddRestartIme();
139 
140     bool IsProxyImeEnable();
141     bool IsEnable(const std::shared_ptr<ImeData> &data);
142     bool IsBoundToClient(uint64_t displayId);
143     bool IsCurrentImeByPid(int32_t pid);
144     int32_t RestoreCurrentImeSubType(uint64_t callingDisplayId);
145     int32_t IsPanelShown(const PanelInfo &panelInfo, bool &isShown);
146     bool CheckSecurityMode();
147     int32_t OnConnectSystemCmd(const sptr<IRemoteObject> &channel, sptr<IRemoteObject> &agent);
148     int32_t RemoveAllCurrentClient();
149     std::shared_ptr<ImeData> GetReadyImeData(ImeType type);
150     std::shared_ptr<ImeData> GetImeData(ImeType type);
151     std::shared_ptr<ImeData> GetImeData(pid_t pid);
152     BlockQueue<SwitchInfo>& GetSwitchQueue();
153     bool IsWmsReady();
154     bool CheckPwdInputPatternConv(InputClientInfo &clientInfo, uint64_t displayId);
155     int32_t StartUserSpecifiedIme(uint64_t callingDisplayId);
156     int32_t SetInputType();
157     std::shared_ptr<ImeNativeCfg> GetImeNativeCfg(int32_t userId, const std::string &bundleName,
158         const std::string &subName);
159     int32_t OnSetCallingWindow(uint32_t callingWindowId, uint64_t callingDisplayId, sptr<IInputClient> client);
160     int32_t GetInputStartInfo(
161         uint64_t displayId, bool &isInputStart, uint32_t &callingWndId, int32_t &requestKeyboardReason);
162     bool IsSaReady(int32_t saId);
163     void TryUnloadSystemAbility();
164     void OnCallingDisplayIdChanged(const int32_t windowId, const int32_t callingPid, const uint64_t displayId);
165     ImfCallingWindowInfo GetCallingWindowInfo(const InputClientInfo &clientInfo);
166     bool SpecialScenarioCheck();
167     bool IsScreenLockOrSecurityFlag();
168     int32_t SpecialSendPrivateData(const std::unordered_map<std::string, PrivateDataValue> &privateCommand);
169     uint64_t GetDisplayGroupId(uint64_t displayId);
170     bool IsDefaultDisplayGroup(uint64_t displayId);
171     bool IsNumkeyAutoInputApp(const std::string &bundleName);
172     std::pair<int32_t, int32_t> GetCurrentInputPattern();
173     void NotifyOnInputStopFinished();
174     bool IsPreconfiguredDefaultImeSpecified(const InputClientInfo &inputClientInfo);
175     bool IsImeSwitchForbidden();
176     std::pair<int32_t, StartPreDefaultImeStatus> StartPreconfiguredDefaultIme(
177         uint64_t callingDisplayId, const ImeExtendInfo &imeExtendInfo = {}, bool isStopCurrentIme = false);
178     void IncreaseAttachCount();
179     void DecreaseAttachCount();
180     uint32_t GetAttachCount();
181     void IncreaseScbStartCount();
182     int32_t TryStartIme();
183     int32_t TryDisconnectIme();
184 
185 private:
186     struct ResetManager {
187         uint32_t num{ 0 };
188         time_t last{};
189     };
190     using CoreMethod = std::function<int32_t(const sptr<IInputMethodCore> &)>;
191     enum TimeLimitType : uint32_t {
192         IME_LIMIT,
193         PROXY_IME_LIMIT,
194     };
195 
196     int32_t userId_; // the id of the user to whom the object is linking
197 #ifdef IMF_ON_DEMAND_START_STOP_SA_ENABLE
198     static const int MAX_IME_START_TIME = 2000;
199 #else
200     static const int MAX_IME_START_TIME = 1500;
201 #endif
202     static const int MAX_NOTIFY_TIME = 20;
203     std::mutex resetLock;
204     std::map<TimeLimitType, ResetManager> managers_;
205     using IpcExec = std::function<int32_t()>;
206 
207     PerUserSession(const PerUserSession &);
208     PerUserSession &operator=(const PerUserSession &);
209     PerUserSession(const PerUserSession &&);
210     PerUserSession &operator=(const PerUserSession &&);
211 
212     static constexpr int32_t MAX_WAIT_TIME = 5000;
213     BlockQueue<SwitchInfo> switchQueue_{ MAX_WAIT_TIME };
214 
215     void OnClientDied(sptr<IInputClient> remote);
216     void OnImeDied(const sptr<IInputMethodCore> &remote, ImeType type, pid_t pid);
217 
218     int AddClientInfo(sptr<IRemoteObject> inputClient, const InputClientInfo &clientInfo, ClientAddEvent event);
219     int32_t RemoveClient(const sptr<IInputClient> &client, const std::shared_ptr<ClientGroup> &clientGroup,
220         const DetachOptions &options);
221     void DeactivateClient(const sptr<IInputClient> &client);
222     std::shared_ptr<InputClientInfo> GetCurrentClientInfo(uint64_t displayId = DEFAULT_DISPLAY_ID);
223     std::shared_ptr<ClientGroup> GetClientGroup(uint64_t displayGroupId);
224     std::shared_ptr<ClientGroup> GetClientGroup(sptr<IRemoteObject> client);
225     std::shared_ptr<ClientGroup> GetClientGroup(ImeType type);
226     ImeType GetImeType(uint64_t displayId);
227     std::shared_ptr<ImeData> GetValidIme(ImeType type);
228 
229     int32_t InitImeData(const std::pair<std::string, std::string> &ime,
230         const std::shared_ptr<ImeNativeCfg> &imeNativeCfg = nullptr);
231     int32_t UpdateImeData(sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, pid_t pid);
232     int32_t AddImeData(ImeType type, sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, pid_t pid);
233     void AddImeData(std::vector<std::shared_ptr<ImeData>> &imeDataList, const std::shared_ptr<ImeData> &imeData);
234     void RemoveImeData(ImeType type);
235     void RemoveImeData(pid_t pid);
236     int32_t RemoveIme(ImeType type, pid_t pid);
237 
238     int32_t BindClientWithIme(const std::shared_ptr<InputClientInfo> &clientInfo, ImeType type,
239         bool isBindFromClient = false, uint64_t displayId = DEFAULT_DISPLAY_ID);
240     void UnBindClientWithIme(const std::shared_ptr<InputClientInfo> &currentClientInfo, const DetachOptions &options);
241     void StopClientInput(
242         const std::shared_ptr<InputClientInfo> &clientInfo, bool isStopInactiveClient = false, bool isAsync = false);
243     void StopImeInput(ImeType currentType, const sptr<IRemoteObject> &currentChannel, uint32_t sessionId);
244 
245     int32_t HideKeyboard(const sptr<IInputClient> &currentClient, const std::shared_ptr<ClientGroup> &clientGroup);
246     int32_t ShowKeyboard(const sptr<IInputClient> &currentClient, const std::shared_ptr<ClientGroup> &clientGroup,
247         int32_t requestKeyboardReason = 0);
248 
249     int32_t InitInputControlChannel();
250     void StartImeInImeDied();
251     void StartImeIfInstalled();
252     void ReplaceCurrentClient(const sptr<IInputClient> &client, const std::shared_ptr<ClientGroup> &clientGroup);
253     bool IsSameClient(sptr<IInputClient> source, sptr<IInputClient> dest);
254 
255     bool IsImeStartInBind(ImeType bindImeType, ImeType startImeType);
256     bool IsProxyImeStartInBind(ImeType bindImeType, ImeType startImeType);
257     bool IsProxyImeStartInImeBind(ImeType bindImeType, ImeType startImeType);
258     bool IsImeBindTypeChanged(ImeType bindImeType);
259     int32_t RequestIme(const std::shared_ptr<ImeData> &data, RequestType type, const IpcExec &exec);
260     int32_t RequestAllIme(const std::shared_ptr<ImeData> data, RequestType reqType, const CoreMethod &method);
261     std::vector<std::shared_ptr<ImeData>> GetAllReadyImeData(ImeType type);
262 
263     bool WaitForCurrentImeStop();
264     void NotifyImeStopFinished();
265     bool CanStartIme();
266     int32_t ChangeToDefaultImeIfNeed(
267         const std::shared_ptr<ImeNativeCfg> &ime, std::shared_ptr<ImeNativeCfg> &imeToStart);
268     AAFwk::Want GetWant(const std::shared_ptr<ImeNativeCfg> &ime);
269     int32_t StartCurrentIme(const std::shared_ptr<ImeNativeCfg> &ime);
270     int32_t StartNewIme(const std::shared_ptr<ImeNativeCfg> &ime);
271     int32_t StartInputService(const std::shared_ptr<ImeNativeCfg> &ime);
272     int32_t ForceStopCurrentIme(bool isNeedWait = true);
273     int32_t StopReadyCurrentIme();
274     int32_t HandleFirstStart(const std::shared_ptr<ImeNativeCfg> &ime, bool isStopCurrentIme);
275     int32_t HandleStartImeTimeout(const std::shared_ptr<ImeNativeCfg> &ime);
276     bool GetInputTypeToStart(std::shared_ptr<ImeNativeCfg> &imeToStart);
277     void HandleBindImeChanged(InputClientInfo &newClientInfo, const std::shared_ptr<ClientGroup> &clientGroup);
278     int32_t NotifyCallingDisplayChanged(uint64_t displayId);
279     bool GetCallingWindowInfo(const InputClientInfo &clientInfo, Rosen::CallingWindowInfo &callingWindowInfo);
280     int32_t SendPrivateData(const std::unordered_map<std::string, PrivateDataValue> &privateCommand);
281     void ClearRequestKeyboardReason(std::shared_ptr<InputClientInfo> &clientInfo);
282     std::pair<std::string, std::string> GetImeUsedBeforeScreenLocked();
283     void SetImeUsedBeforeScreenLocked(const std::pair<std::string, std::string> &ime);
284     std::shared_ptr<ImeNativeCfg> GetRealCurrentIme(bool needMinGuarantee);
285     int32_t NotifyImeChangedToClients();
286     int32_t NotifySubTypeChangedToIme(const std::string &bundleName, const std::string &subName);
287     bool CompareExchange(const int32_t value);
288     bool IsLargeMemoryStateNeed();
289     bool IsAttachFinished();
290     uint32_t GetScbStartCount();
291     void ResetRestartTasks();
292     int32_t SendAllReadyImeToClient(
293         std::shared_ptr<ImeData> data, const std::shared_ptr<InputClientInfo> &clientInfo);
294     void SetImeConnection(const sptr<AAFwk::IAbilityConnection> &connection);
295     sptr<AAFwk::IAbilityConnection> GetImeConnection();
296     void ClearImeConnection(const sptr<AAFwk::IAbilityConnection> &connection);
297     int32_t IsRequestOverLimit(TimeLimitType timeLimit, int32_t resetTimeOut, int32_t restartNum);
298     int32_t FillBindImeInfo(ImeType imeType, std::vector<sptr<IRemoteObject>> &agents,
299         std::vector<BindImeInfo> &imeInfos);
300 
301     std::mutex imeStartLock_;
302 
303     BlockData<bool> isImeStarted_{ MAX_IME_START_TIME, false };
304     std::mutex imeDataLock_;
305     std::unordered_map<ImeType, std::vector<std::shared_ptr<ImeData>>> imeData_;
306     std::mutex focusedClientLock_;
307 
308     std::atomic<bool> isSwitching_ = false;
309     std::mutex imeStopMutex_;
310     std::condition_variable imeStopCv_;
311 
312     std::mutex restartMutex_;
313     int32_t restartTasks_ = 0;
314     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_{ nullptr };
315     ImeAction GetImeAction(ImeEvent action);
316     static inline const std::map<std::pair<ImeStatus, ImeEvent>, std::pair<ImeStatus, ImeAction>> imeEventConverter_ = {
317         { { ImeStatus::READY, ImeEvent::START_IME }, { ImeStatus::READY, ImeAction::DO_NOTHING } },
318         { { ImeStatus::STARTING, ImeEvent::START_IME }, { ImeStatus::STARTING, ImeAction::HANDLE_STARTING_IME } },
319         { { ImeStatus::EXITING, ImeEvent::START_IME }, { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } },
320         { { ImeStatus::READY, ImeEvent::START_IME_TIMEOUT }, { ImeStatus::READY, ImeAction::DO_NOTHING } },
321         { { ImeStatus::STARTING, ImeEvent::START_IME_TIMEOUT },
322             { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } },
323         { { ImeStatus::EXITING, ImeEvent::START_IME_TIMEOUT },
324             { ImeStatus::EXITING, ImeAction::START_AFTER_FORCE_STOP } },
325         { { ImeStatus::READY, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::STOP_READY_IME } },
326         { { ImeStatus::STARTING, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::FORCE_STOP_IME } },
327         { { ImeStatus::EXITING, ImeEvent::STOP_IME }, { ImeStatus::EXITING, ImeAction::FORCE_STOP_IME } },
328         { { ImeStatus::READY, ImeEvent::SET_CORE_AND_AGENT }, { ImeStatus::READY, ImeAction::DO_NOTHING } },
329         { { ImeStatus::STARTING, ImeEvent::SET_CORE_AND_AGENT },
330             { ImeStatus::READY, ImeAction::DO_SET_CORE_AND_AGENT } },
331         { { ImeStatus::EXITING, ImeEvent::SET_CORE_AND_AGENT }, { ImeStatus::EXITING, ImeAction::DO_NOTHING } }
332     };
333     std::string runningIme_;
334     std::atomic<bool> isUserUnlocked_{ false };
335     std::mutex imeUsedLock_;
336     std::pair<std::string, std::string> imeUsedBeforeScreenLocked_;
337     std::mutex virtualDisplayLock_{};
338     std::unordered_set<uint64_t> virtualScreenDisplayId_;
339     std::atomic<uint64_t> agentDisplayId_{ DEFAULT_DISPLAY_ID };
340     std::mutex largeMemoryStateMutex_{};
341     int32_t largeMemoryState_ = LargeMemoryState::LARGE_MEMORY_NOT_NEED;
342     std::mutex clientGroupLock_{};
343     std::unordered_map<uint64_t, std::shared_ptr<ClientGroup>> clientGroupMap_;
344     std::mutex isNotifyFinishedLock_{};
345     BlockData<bool> isNotifyFinished_{ MAX_NOTIFY_TIME, false };
346     std::mutex attachCountMtx_{};
347     uint32_t attachingCount_ { 0 };
348     std::mutex scbStartCountMtx_{};
349     uint32_t scbStartCount_ { 0 };
350     std::mutex connectionLock_{};
351     sptr<AAFwk::IAbilityConnection> connection_ = nullptr;
352     std::atomic<bool> isBlockStartedByLowMem_ = false;
353     bool isFirstPreemption_ = false;
354 };
355 } // namespace MiscServices
356 } // namespace OHOS
357 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H
358