inputmethod_imf/services/include/peruser_session.h
zhaolinglan f99a4563ce modify StartAbility
Signed-off-by: zhaolinglan <zhaolinglan@huawei.com>
2023-07-12 10:49:33 +08:00

151 lines
5.7 KiB
C++

/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SERVICES_INCLUDE_PERUSER_SESSION_H
#define SERVICES_INCLUDE_PERUSER_SESSION_H
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include "block_data.h"
#include "event_handler.h"
#include "event_status_manager.h"
#include "global.h"
#include "i_input_client.h"
#include "i_input_control_channel.h"
#include "i_input_data_channel.h"
#include "i_input_method_agent.h"
#include "i_input_method_core.h"
#include "input_attribute.h"
#include "input_client_info.h"
#include "input_control_channel_stub.h"
#include "input_death_recipient.h"
#include "input_method_info.h"
#include "input_method_property.h"
#include "input_window_info.h"
#include "inputmethod_sysevent.h"
#include "iremote_object.h"
#include "message.h"
#include "message_handler.h"
namespace OHOS {
namespace MiscServices {
struct ResetManager {
uint32_t num{ 0 };
time_t last{};
};
/**@class PerUserSession
*
* @brief The class provides session management in input method management service
*
* This class manages the sessions between input clients and input method engines for each unlocked user.
*/
class PerUserSession {
enum : int32_t {
CURRENT_IME = 0, // index for current ime
SECURITY_IME, // index for security ime
MAX_IME // the maximum count of ime started for a user
};
enum ClientAddEvent : int32_t {
PREPARE_INPUT = 0,
START_LISTENING,
};
public:
explicit PerUserSession(int userId);
~PerUserSession();
int32_t OnPrepareInput(const InputClientInfo &clientInfo);
int32_t OnStartInput(const sptr<IInputClient> &client, bool isShowKeyboard, bool attachFlag);
int32_t OnStopInput(sptr<IInputClient> client);
int32_t OnReleaseInput(const sptr<IInputClient> &client);
int32_t OnSetCoreAndAgent(const sptr<IInputMethodCore> &core, const sptr<IInputMethodAgent> &agent);
int OnHideKeyboardSelf();
int OnShowKeyboardSelf();
void StopInputService(std::string imeId);
int32_t OnSwitchIme(const Property &property, const SubProperty &subProperty, bool isSubtypeSwitch);
void UpdateCurrentUserId(int32_t userId);
void OnFocused(int32_t pid, int32_t uid);
void OnUnfocused(int32_t pid, int32_t uid);
bool CheckFocused(uint32_t tokenId);
int32_t OnPanelStatusChange(const InputWindowStatus &status, const InputWindowInfo &windowInfo);
int32_t OnUpdateListenEventFlag(const InputClientInfo &clientInfo);
bool StartInputService(const std::string &imeName, bool isRetry);
private:
int32_t userId_; // the id of the user to whom the object is linking
std::map<sptr<IRemoteObject>, std::shared_ptr<InputClientInfo>> mapClients_;
static const int MAX_RESTART_NUM = 3;
static const int IME_RESET_TIME_OUT = 3;
static const int MAX_IME_START_TIME = 1000;
std::mutex imsCoreLock_;
sptr<IInputMethodCore> imsCore[MAX_IME]; // the remote handlers of input method service
std::mutex agentLock_;
sptr<IInputMethodAgent> agent_;
std::mutex clientLock_;
sptr<IInputClient> currentClient_; // the current input client
sptr<InputDeathRecipient> imsDeathRecipient_ = nullptr;
std::recursive_mutex mtx; // mutex to lock the operations among multi work threads
std::mutex resetLock;
ResetManager manager[MAX_IME];
PerUserSession(const PerUserSession &);
PerUserSession &operator=(const PerUserSession &);
PerUserSession(const PerUserSession &&);
PerUserSession &operator=(const PerUserSession &&);
std::shared_ptr<InputClientInfo> GetClientInfo(sptr<IRemoteObject> inputClient);
void OnClientDied(sptr<IInputClient> remote);
void OnImsDied(const sptr<IInputMethodCore> &remote);
int AddClient(sptr<IRemoteObject> inputClient, const InputClientInfo &clientInfo, ClientAddEvent event);
void UpdateClient(sptr<IRemoteObject> inputClient, bool isShowKeyboard);
int32_t RemoveClient(const sptr<IRemoteObject> &client, bool isClientDied);
int32_t ShowKeyboard(const sptr<IInputDataChannel> &channel, const sptr<IInputClient> &inputClient,
bool isShowKeyboard, bool attachFlag);
int32_t HideKeyboard(const sptr<IInputClient> &inputClient);
int32_t ClearDataChannel(const sptr<IInputDataChannel> &channel);
int32_t SendAgentToSingleClient(const sptr<IInputClient> &client);
int32_t InitInputControlChannel();
bool IsRestartIme(uint32_t index);
void ClearImeData(uint32_t index);
void SetCurrentClient(sptr<IInputClient> client);
sptr<IInputClient> GetCurrentClient();
void SetImsCore(int32_t index, sptr<IInputMethodCore> core);
sptr<IInputMethodCore> GetImsCore(int32_t index);
void SetAgent(sptr<IInputMethodAgent> agent);
sptr<IInputMethodAgent> GetAgent();
bool IsCurrentClient(int32_t pid, int32_t uid);
void UnbindClient(const sptr<IInputClient> &client);
static inline bool IsValid(int32_t index)
{
return index >= CURRENT_IME && index <= SECURITY_IME;
}
BlockData<bool> isImeStarted_{ MAX_IME_START_TIME, false };
};
} // namespace MiscServices
} // namespace OHOS
#endif // SERVICES_INCLUDE_PERUSER_SESSION_H