• 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 "block_data.h"
26 #include "event_handler.h"
27 #include "event_status_manager.h"
28 #include "global.h"
29 #include "i_input_client.h"
30 #include "i_input_control_channel.h"
31 #include "i_input_data_channel.h"
32 #include "i_input_method_agent.h"
33 #include "i_input_method_core.h"
34 #include "input_attribute.h"
35 #include "input_client_info.h"
36 #include "input_control_channel_stub.h"
37 #include "input_death_recipient.h"
38 #include "input_method_info.h"
39 #include "input_method_property.h"
40 #include "input_window_info.h"
41 #include "inputmethod_sysevent.h"
42 #include "iremote_object.h"
43 #include "message.h"
44 #include "message_handler.h"
45 
46 namespace OHOS {
47 namespace MiscServices {
48 struct ResetManager {
49     uint32_t num{ 0 };
50     time_t last{};
51 };
52 
53 /**@class PerUserSession
54  *
55  * @brief The class provides session management in input method management service
56  *
57  * This class manages the sessions between input clients and input method engines for each unlocked user.
58  */
59 class PerUserSession {
60     enum : int32_t {
61         CURRENT_IME = 0, // index for current ime
62         SECURITY_IME,    // index for security ime
63         MAX_IME          // the maximum count of ime started for a user
64     };
65 
66     enum ClientAddEvent : int32_t {
67         PREPARE_INPUT = 0,
68         START_LISTENING,
69     };
70 
71 public:
72     explicit PerUserSession(int userId);
73     ~PerUserSession();
74 
75     int32_t OnPrepareInput(const InputClientInfo &clientInfo);
76     int32_t OnStartInput(const sptr<IInputClient> &client, bool isShowKeyboard, bool attachFlag);
77     int32_t OnStopInput(sptr<IInputClient> client);
78     int32_t OnReleaseInput(const sptr<IInputClient> &client);
79     int32_t OnSetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent);
80     int OnHideKeyboardSelf();
81     int OnShowKeyboardSelf();
82     void StopInputService(std::string imeId);
83     int32_t OnSwitchIme(const Property &property, const SubProperty &subProperty, bool isSubtypeSwitch);
84     void UpdateCurrentUserId(int32_t userId);
85     void OnFocused(int32_t pid, int32_t uid);
86     void OnUnfocused(int32_t pid, int32_t uid);
87     bool IsFocused(int64_t callingPid, uint32_t callingTokenId);
88     int32_t OnPanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo);
89     int32_t OnUpdateListenEventFlag(const InputClientInfo &clientInfo);
90     bool StartInputService(const std::string &imeName, bool isRetry);
91 
92 private:
93     int32_t userId_; // the id of the user to whom the object is linking
94     std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> mapClients_;
95     static const int MAX_RESTART_NUM = 3;
96     static const int IME_RESET_TIME_OUT = 3;
97     static const int MAX_IME_START_TIME = 1000;
98 
99     std::mutex imsCoreLock_;
100     sptr<IInputMethodCore> imsCore[MAX_IME];       // the remote handlers of input method service
101 
102     std::mutex agentLock_;
103     sptr<IInputMethodAgent> agent_;
104     std::mutex clientLock_;
105     sptr<IInputClient> currentClient_;              // the current input client
106 
107     sptr<InputDeathRecipient> imsDeathRecipient_ = nullptr;
108     std::recursive_mutex mtx;             // mutex to lock the operations among multi work threads
109     std::mutex resetLock;
110     ResetManager manager[MAX_IME];
111 
112     PerUserSession(const PerUserSession &);
113     PerUserSession &operator=(const PerUserSession &);
114     PerUserSession(const PerUserSession &&);
115     PerUserSession &operator=(const PerUserSession &&);
116     std::shared_ptr<InputClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient);
117 
118     void OnClientDied(sptr<IInputClient> remote);
119     void OnImsDied(const sptr<IInputMethodCore> &remote);
120 
121     int AddClient(sptr<IRemoteObject> inputClient, const InputClientInfo &clientInfo, ClientAddEvent event);
122     void UpdateClient(sptr<IRemoteObject> inputClient, bool isShowKeyboard);
123     int32_t RemoveClient(const sptr<IRemoteObject> &client, bool isClientDied);
124     int32_t ShowKeyboard(const sptr<IInputDataChannel> &channel, const sptr<IInputClient> &inputClient,
125         bool isShowKeyboard, bool attachFlag);
126     int32_t HideKeyboard(const sptr<IInputClient> &inputClient);
127     int32_t ClearDataChannel(const sptr<IInputDataChannel> &channel);
128     int32_t SendAgentToSingleClient(const sptr<IInputClient> &client);
129     int32_t InitInputControlChannel();
130     bool IsReadyToStartIme();
131     bool IsRestartIme(uint32_t index);
132     void ClearImeData(uint32_t index);
133     void SetCurrentClient(sptr<IInputClient> client);
134     sptr<IInputClient> GetCurrentClient();
135     void SetImsCore(int32_t index, sptr<IInputMethodCore> core);
136     sptr<IInputMethodCore> GetImsCore(int32_t index);
137     void SetAgent(sptr<IInputMethodAgent> agent);
138     sptr<IInputMethodAgent> GetAgent();
139     bool IsCurrentClient(int32_t pid, int32_t uid);
140     void UnbindClient(const sptr<IInputClient> &client);
141 
IsValid(int32_t index)142     static inline bool IsValid(int32_t index)
143     {
144         return index >= CURRENT_IME && index <= SECURITY_IME;
145     }
146 
147     BlockData<bool> isImeStarted_{ MAX_IME_START_TIME, false };
148 };
149 } // namespace MiscServices
150 } // namespace OHOS
151 #endif // SERVICES_INCLUDE_PERUSER_SESSION_H
152