1 /*
2 * Copyright (c) 2025 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 #include "imf_hisysevent_util.h"
17
18 #include <algorithm>
19
20 #include "accesstoken_kit.h"
21 #include "app_mgr_client.h"
22 #include "hisysevent.h"
23 #include "ipc_skeleton.h"
24 #include "running_process_info.h"
25 namespace OHOS {
26 namespace MiscServices {
27 using namespace OHOS::AppExecFwk;
28 using HiSysEvent = OHOS::HiviewDFX::HiSysEvent;
29 using namespace Security::AccessToken;
ReportClientAttachFault(const std::string & selfName,int64_t faultNum,const HiSysOriginalInfo & info)30 void ImfHiSysEventUtil::ReportClientAttachFault(
31 const std::string &selfName, int64_t faultNum, const HiSysOriginalInfo &info)
32 {
33 IMSA_HILOGD("run in.");
34 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, "CLIENT_ATTACH_FAILED", HiSysEvent::EventType::FAULT,
35 "SELF_NAME", selfName, "PEER_NAME", info.peerName, "PEER_PID", info.peerPid, "PEER_USERID", info.peerUserId,
36 "CLIENT_TYPE", info.clientType, "INPUT_PATTERN", info.inputPattern, "ISSHOWKEYBOARD", info.isShowKeyboard,
37 "IME_NAME", info.imeName, "ERR_CODE", info.errCode, "FAULT_COUNT", faultNum);
38 if (ret != HiviewDFX::SUCCESS) {
39 IMSA_HILOGE("report failed! ret: %{public}d", ret);
40 }
41 }
42
ReportClientShowFault(const std::string & selfName,int64_t faultNum,const HiSysOriginalInfo & info)43 void ImfHiSysEventUtil::ReportClientShowFault(
44 const std::string &selfName, int64_t faultNum, const HiSysOriginalInfo &info)
45 {
46 IMSA_HILOGD("run in.");
47 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, "CLIENT_SHOW_FAILED", HiSysEvent::EventType::FAULT,
48 "SELF_NAME", selfName, "PEER_NAME", info.peerName, "PEER_PID", info.peerPid, "PEER_USERID", info.peerUserId,
49 "CLIENT_TYPE", info.clientType, "INPUT_PATTERN", info.inputPattern, "IME_NAME", info.imeName, "EVENT_CODE",
50 info.eventCode, "ERR_CODE", info.errCode, "FAULT_COUNT", faultNum);
51 if (ret != HiviewDFX::SUCCESS) {
52 IMSA_HILOGE("report failed! ret: %{public}d", ret);
53 }
54 }
55
ReportImeStartInputFault(const std::string & selfName,int64_t faultNum,const HiSysOriginalInfo & info)56 void ImfHiSysEventUtil::ReportImeStartInputFault(
57 const std::string &selfName, int64_t faultNum, const HiSysOriginalInfo &info)
58 {
59 IMSA_HILOGD("run in.");
60 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, "IME_START_INPUT_FAILED", HiSysEvent::EventType::FAULT,
61 "SELF_NAME", selfName, "PEER_NAME", info.peerName, "PEER_PID", info.peerPid, "ISSHOWKEYBOARD",
62 info.isShowKeyboard, "EVENT_CODE", info.eventCode, "ERR_CODE", info.errCode, "FAULT_COUNT", faultNum);
63 if (ret != HiviewDFX::SUCCESS) {
64 IMSA_HILOGE("report failed! ret: %{public}d", ret);
65 }
66 }
67
ReportBaseTextOperationFault(const std::string & selfName,int64_t faultNum,const HiSysOriginalInfo & info)68 void ImfHiSysEventUtil::ReportBaseTextOperationFault(
69 const std::string &selfName, int64_t faultNum, const HiSysOriginalInfo &info)
70 {
71 IMSA_HILOGD("run in.");
72 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, "BASE_TEXT_OPERATION_FAILED",
73 HiSysEvent::EventType::FAULT, "SELF_NAME", selfName, "PEER_NAME", info.peerName, "PEER_PID", info.peerPid,
74 "CLIENT_TYPE", info.clientType, "EVENT_CODE", info.eventCode, "ERR_CODE", info.errCode, "FAULT_COUNT",
75 faultNum);
76 if (ret != HiviewDFX::SUCCESS) {
77 IMSA_HILOGE("report failed! ret: %{public}d", ret);
78 }
79 }
80
ReportStatisticsEvent(const std::string & eventName,const std::vector<std::string> & imeNames,const std::vector<std::string> & appNames,const std::vector<std::string> & statistics)81 void ImfHiSysEventUtil::ReportStatisticsEvent(const std::string &eventName, const std::vector<std::string> &imeNames,
82 const std::vector<std::string> &appNames, const std::vector<std::string> &statistics)
83 {
84 std::string infoStr;
85 if (!statistics.empty()) {
86 infoStr = statistics[0];
87 }
88 IMSA_HILOGD("run in, [%{public}s, %{public}s].", eventName.c_str(), infoStr.c_str());
89 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, eventName, HiSysEvent::EventType::STATISTIC,
90 "IME_NAME", imeNames, "APP_NAME", appNames, "INFOS", statistics);
91 if (ret != HiviewDFX::SUCCESS) {
92 IMSA_HILOGE("report failed! ret: %{public}d", ret);
93 }
94 }
95
ReportStatisticsEvent(const std::string & eventName,const std::string & imeName,const std::vector<std::string> & appNames,const std::vector<std::string> & statistics)96 void ImfHiSysEventUtil::ReportStatisticsEvent(const std::string &eventName, const std::string &imeName,
97 const std::vector<std::string> &appNames, const std::vector<std::string> &statistics)
98 {
99 std::string infoStr;
100 if (!statistics.empty()) {
101 infoStr = statistics[0];
102 }
103 IMSA_HILOGD("run in, [%{public}s, %{public}s].", eventName.c_str(), infoStr.c_str());
104 auto ret = HiSysEventWrite(HiSysEvent::Domain::INPUTMETHOD, eventName, HiSysEvent::EventType::STATISTIC,
105 "SELF_NAME", imeName, "APP_NAME", appNames, "INFOS", statistics);
106 if (ret != HiviewDFX::SUCCESS) {
107 IMSA_HILOGE("report failed! ret: %{public}d", ret);
108 }
109 }
110
GetAppName(uint64_t fullTokenId)111 std::string ImfHiSysEventUtil::GetAppName(uint64_t fullTokenId)
112 {
113 std::string name;
114 uint32_t tokenId = static_cast<uint32_t>(fullTokenId);
115 auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
116 switch (tokenType) {
117 case ATokenTypeEnum::TOKEN_HAP: {
118 if (fullTokenId == IPCSkeleton::GetSelfTokenID()) {
119 RunningProcessInfo info;
120 AppMgrClient client;
121 if (client.GetProcessRunningInformation(info) == 0) {
122 name = info.processName_;
123 }
124 break;
125 }
126 HapTokenInfo hapInfo;
127 if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) == 0) {
128 name = hapInfo.bundleName;
129 }
130 break;
131 }
132 case ATokenTypeEnum::TOKEN_NATIVE:
133 case ATokenTypeEnum::TOKEN_SHELL: {
134 NativeTokenInfo tokenInfo;
135 if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) == 0) {
136 name = tokenInfo.processName;
137 }
138 break;
139 }
140 default: {
141 break;
142 }
143 }
144 return name;
145 }
146
AddIfAbsent(const std::string & bundleName,std::vector<std::string> & bundleNames)147 std::string ImfHiSysEventUtil::AddIfAbsent(const std::string &bundleName, std::vector<std::string> &bundleNames)
148 {
149 auto it = std::find_if(bundleNames.begin(), bundleNames.end(),
150 [&bundleName](const std::string &bundleNameTmp) { return bundleName == bundleNameTmp; });
151 if (it == bundleNames.end()) {
152 bundleNames.push_back(bundleName);
153 return std::to_string(bundleNames.size() - 1);
154 }
155 return std::to_string(it - bundleNames.begin());
156 }
157 } // namespace MiscServices
158 } // namespace OHOS