!1159 增加hisysevent打点

Merge pull request !1159 from wangdongqi/master
This commit is contained in:
openharmony_ci 2024-04-30 13:34:29 +00:00 committed by Gitee
commit 2e3d98a96d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 145 additions and 6 deletions

View File

@ -43,6 +43,8 @@ enum class IMEBehaviour : int32_t {
CHANGE_IME,
};
enum class ImeState : int32_t { UNBIND = 0, BIND };
class InputMethodSysEvent {
public:
static InputMethodSysEvent &GetInstance();
@ -50,6 +52,7 @@ public:
void InputmethodFaultReporter(int32_t errCode, const std::string &name, const std::string &info);
void RecordEvent(IMEBehaviour behaviour);
void OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCode);
void ReportImeState(ImeState state, pid_t pid, const std::string &bundleName);
bool StartTimerForReport();
void SetUserId(int32_t userId);

View File

@ -118,6 +118,18 @@ void InputMethodSysEvent::OperateSoftkeyboardBehaviour(OperateIMEInfoCode infoCo
}
}
void InputMethodSysEvent::ReportImeState(ImeState state, pid_t pid, const std::string &bundleName)
{
IMSA_HILOGD("run in.");
int32_t ret = HiSysEventWrite(HiSysEventNameSpace::Domain::INPUTMETHOD, "IME_STATE_CHANGED",
HiSysEventNameSpace::EventType::BEHAVIOR, "STATE", static_cast<int32_t>(state), "PID", pid, "BUNDLE_NAME",
bundleName);
if (ret != HiviewDFX::SUCCESS) {
IMSA_HILOGE("ime: %{public}s state: %{public}d report failed! ret: %{public}d", bundleName.c_str(),
static_cast<int32_t>(state), ret);
}
}
const std::string InputMethodSysEvent::GetOperateInfo(int32_t infoCode)
{
auto iter = operateInfo_.find(static_cast<int32_t>(infoCode));

View File

@ -34,4 +34,10 @@ IME_USAGE:
OPERATE_SOFTKEYBOARD:
__BASE: {type: BEHAVIOR, level: MINOR, tag: IMEOperate, desc: Operate the soft keyboard report}
OPERATING: {type: STRING, desc: 'Show, hide or unbind soft keyboard'}
OPERATE_INFO: {type: STRING, desc: 'Manipulate to show, hide, or unbind soft keyboard information'}
OPERATE_INFO: {type: STRING, desc: 'Manipulate to show, hide, or unbind soft keyboard information'}
IME_STATE_CHANGED:
__BASE: {type: BEHAVIOR, level: MINOR, tag: IMEState, desc: InputMethodExtension state changed}
STATE: {type: INT32, desc: InputMethodExtension STATE}
PID: {type: INT32, desc: The process id of current InputMethodExtension}
BUNDLE_NAME: {type: STRING, desc: The bundle name of current InputMethodExtension}

View File

@ -54,10 +54,11 @@ struct ImeData {
sptr<IInputMethodCore> core{ nullptr };
sptr<IRemoteObject> agent{ nullptr };
sptr<InputDeathRecipient> deathRecipient{ nullptr };
pid_t pid;
std::shared_ptr<FreezeManager> freezeMgr;
ImeData(sptr<IInputMethodCore> core, sptr<IRemoteObject> agent, sptr<InputDeathRecipient> deathRecipient,
pid_t imePid)
: core(std::move(core)), agent(std::move(agent)), deathRecipient(std::move(deathRecipient)),
: core(std::move(core)), agent(std::move(agent)), deathRecipient(std::move(deathRecipient)), pid(imePid),
freezeMgr(std::make_shared<FreezeManager>(imePid))
{
}

View File

@ -326,6 +326,8 @@ int32_t PerUserSession::OnRequestShowInput()
IMSA_HILOGE("failed to show keyboard, ret: %{public}d", ret);
return ErrorCode::ERROR_KBD_SHOW_FAILED;
}
InputMethodSysEvent::GetInstance().ReportImeState(
ImeState::BIND, data->pid, ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_)->bundleName);
auto currentClient = GetCurrentClient();
if (currentClient != nullptr) {
UpdateClientInfo(currentClient->AsObject(), { { UpdateFlag::ISSHOWKEYBOARD, true } });
@ -452,6 +454,8 @@ void PerUserSession::DeactivateClient(const sptr<IInputClient> &client)
data->core->OnClientInactive(clientInfo->channel);
return ErrorCode::NO_ERROR;
});
InputMethodSysEvent::GetInstance().ReportImeState(
ImeState::UNBIND, data->pid, ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_)->bundleName);
}
bool PerUserSession::IsProxyImeEnable()
@ -512,6 +516,10 @@ int32_t PerUserSession::BindClientWithIme(
IMSA_HILOGE("start input failed, ret: %{public}d", ret);
return ErrorCode::ERROR_IME_START_INPUT_FAILED;
}
if (type == ImeType::IME) {
InputMethodSysEvent::GetInstance().ReportImeState(
ImeState::BIND, data->pid, ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_)->bundleName);
}
if (!isBindFromClient && clientInfo->client->OnInputReady(data->agent) != ErrorCode::NO_ERROR) {
IMSA_HILOGE("start client input failed, ret: %{public}d", ret);
return ErrorCode::ERROR_EX_PARCELABLE;
@ -554,6 +562,10 @@ void PerUserSession::StopImeInput(ImeType currentType, const sptr<IRemoteObject>
auto ret = RequestIme(
data, RequestType::STOP_INPUT, [&data, &currentChannel]() { return data->core->StopInput(currentChannel); });
IMSA_HILOGI("stop ime input, ret: %{public}d", ret);
if (ret == ErrorCode::NO_ERROR && currentType == ImeType::IME) {
InputMethodSysEvent::GetInstance().ReportImeState(
ImeState::UNBIND, data->pid, ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_)->bundleName);
}
}
void PerUserSession::OnSecurityChange(int32_t security)

View File

@ -46,8 +46,12 @@ constexpr const char *CMD1 = "hidumper -s 3703 -a -a";
constexpr const char *CMD2 = "hidumper -s 3703 -a -h";
constexpr const char *CMD3 = "hidumper -s 3703 -a -test";
constexpr const char *PARAM_KEY = "OPERATE_INFO";
constexpr const char *STATE = "STATE";
constexpr const char *PID = "PID";
constexpr const char *BUNDLE_NAME = "BUNDLE_NAME";
constexpr const char *DOMAIN = "INPUTMETHOD";
constexpr const char *EVENT_NAME = "OPERATE_SOFTKEYBOARD";
constexpr const char *OPERATE_SOFTKEYBOARD_EVENT_NAME = "OPERATE_SOFTKEYBOARD";
constexpr const char *IME_STATE_CHANGED_EVENT_NAME = "IME_STATE_CHANGED";
class Watcher : public HiSysEventListener {
public:
@ -84,12 +88,57 @@ private:
std::string operateInfo_;
};
class WatcherImeChange : public HiSysEventListener {
public:
explicit WatcherImeChange(const std::string &state, const std::string &pid, const std::string &bundleName)
: state_(state), pid_(pid), bundleName_(bundleName)
{
}
virtual ~WatcherImeChange()
{
}
void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
{
if (sysEvent == nullptr) {
IMSA_HILOGE("sysEvent is nullptr!");
return;
}
std::string pid;
std::string state;
std::string bundleName;
sysEvent->GetParamValue(STATE, state);
sysEvent->GetParamValue(PID, pid);
sysEvent->GetParamValue(BUNDLE_NAME, bundleName);
IMSA_HILOGD("bundleName: %{public}s, state: %{public}s, pid: %{public}s", bundleName.c_str(), state.c_str(),
pid.c_str());
if (state != state_ || pid != pid_ || bundleName != bundleName_) {
IMSA_HILOGE("string is not matched.");
return;
}
std::unique_lock<std::mutex> lock(cvMutexImeChange_);
watcherCvImeChange_.notify_all();
}
void OnServiceDied() final
{
IMSA_HILOGE("WatcherImeChange::OnServiceDied");
}
std::mutex cvMutexImeChange_;
std::condition_variable watcherCvImeChange_;
private:
std::string state_;
std::string pid_;
std::string bundleName_;
};
class InputMethodDfxTest : public testing::Test {
public:
using ExecFunc = std::function<void()>;
static void SetUpTestCase(void);
static void TearDownTestCase(void);
static bool WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec);
static bool WriteAndWatch(const std::shared_ptr<Watcher> &watcher, const InputMethodDfxTest::ExecFunc &exec);
static bool WriteAndWatchImeChange(
const std::shared_ptr<WatcherImeChange> &watcher, const InputMethodDfxTest::ExecFunc &exec);
void SetUp();
void TearDown();
static sptr<InputMethodController> inputMethodController_;
@ -102,9 +151,11 @@ sptr<OnTextChangedListener> InputMethodDfxTest::textListener_;
sptr<InputMethodAbility> InputMethodDfxTest::inputMethodAbility_;
std::shared_ptr<InputMethodEngineListenerImpl> InputMethodDfxTest::imeListener_;
bool InputMethodDfxTest::WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMethodDfxTest::ExecFunc exec)
bool InputMethodDfxTest::WriteAndWatch(
const std::shared_ptr<Watcher> &watcher, const InputMethodDfxTest::ExecFunc &exec)
{
OHOS::HiviewDFX::ListenerRule listenerRule(DOMAIN, EVENT_NAME, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
OHOS::HiviewDFX::ListenerRule listenerRule(
DOMAIN, OPERATE_SOFTKEYBOARD_EVENT_NAME, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
sysRules.emplace_back(listenerRule);
auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
@ -123,6 +174,29 @@ bool InputMethodDfxTest::WriteAndWatch(std::shared_ptr<Watcher> watcher, InputMe
return true;
}
bool InputMethodDfxTest::WriteAndWatchImeChange(
const std::shared_ptr<WatcherImeChange> &watcher, const InputMethodDfxTest::ExecFunc &exec)
{
OHOS::HiviewDFX::ListenerRule listenerRule(
DOMAIN, IME_STATE_CHANGED_EVENT_NAME, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
std::vector<OHOS::HiviewDFX::ListenerRule> sysRules;
sysRules.emplace_back(listenerRule);
auto ret = OHOS::HiviewDFX::HiSysEventManager::AddListener(watcher, sysRules);
if (ret != SUCCESS) {
IMSA_HILOGE("AddListener failed! ret = %{public}d", ret);
return false;
}
std::unique_lock<std::mutex> lock(watcher->cvMutexImeChange_);
exec();
bool result = watcher->watcherCvImeChange_.wait_for(lock, std::chrono::seconds(1)) != std::cv_status::timeout;
ret = OHOS::HiviewDFX::HiSysEventManager::RemoveListener(watcher);
if (ret != SUCCESS || !result) {
IMSA_HILOGE("RemoveListener ret = %{public}d, wait_for result = %{public}s", ret, result ? "true" : "false");
return false;
}
return true;
}
void InputMethodDfxTest::SetUpTestCase(void)
{
IMSA_HILOGI("InputMethodDfxTest::SetUpTestCase");
@ -325,5 +399,36 @@ HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Close, TestSize.Level
auto close = []() { inputMethodController_->Close(); };
EXPECT_TRUE(InputMethodDfxTest::WriteAndWatch(watcher, close));
}
/**
* @tc.name: InputMethodDfxTest_Hisysevent_UnBind
* @tc.desc: Hisysevent UnBind.
* @tc.type: FUNC
*/
HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_UnBind, TestSize.Level0)
{
std::string bundleName = "com.example.kikakeyboard";
auto watcherImeChange = std::make_shared<WatcherImeChange>(std::to_string(static_cast<int32_t>(ImeState::UNBIND)),
std::to_string(static_cast<int32_t>(getpid())), bundleName);
auto imeStateUnBind = []() {
inputMethodController_->Attach(textListener_, true);
inputMethodController_->Close();
};
EXPECT_TRUE(InputMethodDfxTest::WriteAndWatchImeChange(watcherImeChange, imeStateUnBind));
}
/**
* @tc.name: InputMethodDfxTest_Hisysevent_Bind
* @tc.desc: Hisysevent Bind.
* @tc.type: FUNC
*/
HWTEST_F(InputMethodDfxTest, InputMethodDfxTest_Hisysevent_Bind, TestSize.Level0)
{
std::string bundleName = "com.example.kikakeyboard";
auto watcherImeChange = std::make_shared<WatcherImeChange>(std::to_string(static_cast<int32_t>(ImeState::BIND)),
std::to_string(static_cast<int32_t>(getpid())), bundleName);
auto imeStateBind = []() { inputMethodController_->RequestShowInput(); };
EXPECT_TRUE(InputMethodDfxTest::WriteAndWatchImeChange(watcherImeChange, imeStateBind));
}
} // namespace MiscServices
} // namespace OHOS