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