• 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 
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();
92         int OnShowKeyboardSelf();
93 
94         void StopInputService(std::string imeId);
95         void OnInputMethodSwitched(const Property &property, const SubProperty &subProperty);
96 
97         void SetCurrentSubProperty(const SubProperty &subProperty);
98         SubProperty GetCurrentSubProperty();
99         void UpdateCurrentUserId(int32_t userId);
100 
101     private:
102         int userId_; // the id of the user to whom the object is linking
103         int userState = UserState::USER_STATE_STARTED; // the state of the user to whom the object is linking
104         int displayId; // the id of the display screen on which the user is
105         std::map<sptr<IRemoteObject>, std::shared_ptr<ClientInfo>> mapClients;
106         static const int MAX_RESTART_NUM = 3;
107         static const int IME_RESET_TIME_OUT = 300;
108         static const int MAX_RESET_WAIT_TIME = 1600000;
109 
110         sptr<IInputControlChannel> inputControlChannel[MAX_IME];
111         std::mutex imsCoreLock_;
112         sptr<IInputMethodCore> imsCore[MAX_IME]; // the remote handlers of input method service
113 
114         sptr<IInputMethodAgent> imsAgent;
115         std::mutex clientLock_;
116         sptr<IInputClient> currentClient_; // the current input client
117 
118         sptr<RemoteObjectDeathRecipient> imsDeathRecipient = nullptr;
119         MessageHandler *msgHandler = nullptr; // message handler working with Work Thread
120         std::recursive_mutex mtx; // mutex to lock the operations among multi work threads
121         std::mutex resetLock;
122         ResetManager manager[MAX_IME];
123 
124         PerUserSession(const PerUserSession&);
125         PerUserSession& operator =(const PerUserSession&);
126         PerUserSession(const PerUserSession&&);
127         PerUserSession& operator =(const PerUserSession&&);
128         std::shared_ptr<ClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient);
129 
130         void OnClientDied(sptr<IInputClient> remote);
131         void OnImsDied(sptr<IInputMethodCore> remote);
132 
133         int AddClient(const sptr<IRemoteObject> &inputClient, const ClientInfo &clientInfo);
134         void UpdateClient(sptr<IRemoteObject> inputClient, bool isShowKeyboard);
135         void RemoveClient(const sptr<IRemoteObject> &inputClient);
136         int ShowKeyboard(const sptr<IInputClient>& inputClient, bool isShowKeyboard);
137         int HideKeyboard(const sptr<IInputClient>& inputClient);
138         int GetImeIndex(const sptr<IInputClient>& inputClient);
139         void SendAgentToSingleClient(const ClientInfo &clientInfo);
140         void InitInputControlChannel();
141         void SendAgentToAllClients();
142         bool IsRestartIme(uint32_t index);
143         void ClearImeData(uint32_t index);
144         void SetCurrentClient(sptr<IInputClient> client);
145         sptr<IInputClient> GetCurrentClient();
146         void SetImsCore(int32_t index, sptr<IInputMethodCore> core);
147         sptr<IInputMethodCore> GetImsCore(int32_t index);
IsValid(int32_t index)148         static inline bool IsValid(int32_t index)
149         {
150             return index >= CURRENT_IME && index <= SECURITY_IME;
151         }
152 
153         std::mutex propertyLock_;
154         SubProperty currentSubProperty;
155     };
156 } // namespace MiscServices
157 } // namespace OHOS
158 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H