1 /*
2 * Copyright (c) 2022 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 "anr_manager.h"
17
18 #include "ability_manager_client.h"
19
20 #include "dfx_hisysevent.h"
21 #include "input_event_handler.h"
22 #include "input_windows_manager.h"
23 #include "mmi_log.h"
24 #include "napi_constants.h"
25 #include "proto.h"
26 #include "timer_manager.h"
27 #include "window_manager.h"
28
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "ANRManager" };
33 const std::string FOUNDATION = "foundation";
34 constexpr int32_t MAX_ANR_TIMER_COUNT = 50;
35 constexpr int32_t TIME_CONVERT_RATIO = 1000;
36 } // namespace
37
ANRManager()38 ANRManager::ANRManager() {}
~ANRManager()39 ANRManager::~ANRManager() {}
40
Init(UDSServer & udsServer)41 void ANRManager::Init(UDSServer &udsServer)
42 {
43 CALL_DEBUG_ENTER;
44 udsServer_ = &udsServer;
45 CHKPV(udsServer_);
46 udsServer_->AddSessionDeletedCallback(std::bind(&ANRManager::OnSessionLost, this, std::placeholders::_1));
47 }
48
MarkProcessed(int32_t pid,int32_t eventType,int32_t eventId)49 int32_t ANRManager::MarkProcessed(int32_t pid, int32_t eventType, int32_t eventId)
50 {
51 CALL_DEBUG_ENTER;
52 MMI_HILOGD("pid:%{public}d, eventType:%{public}d, eventId:%{public}d", pid, eventType, eventId);
53 SessionPtr sess = udsServer_->GetSessionByPid(pid);
54 CHKPR(sess, RET_ERR);
55 std::list<int32_t> timerIds = sess->DelEvents(eventType, eventId);
56 for (int32_t item : timerIds) {
57 if (item != -1) {
58 TimerMgr->RemoveTimer(item);
59 anrTimerCount_--;
60 MMI_HILOGD("Remove anr timer, anr type:%{public}d, eventId:%{public}d, timer id:%{public}d,"
61 "count:%{public}d", eventType, eventId, item, anrTimerCount_);
62 }
63 }
64 return RET_OK;
65 }
66
RemoveTimers(SessionPtr sess)67 void ANRManager::RemoveTimers(SessionPtr sess)
68 {
69 CHKPV(sess);
70 std::vector<int32_t> DispatchTimerIds = sess->GetTimerIds(ANR_DISPATCH);
71 for (int32_t item : DispatchTimerIds) {
72 if (item != -1) {
73 TimerMgr->RemoveTimer(item);
74 anrTimerCount_--;
75 }
76 }
77 std::vector<int32_t> MonitorTimerIds = sess->GetTimerIds(ANR_MONITOR);
78 for (int32_t item : MonitorTimerIds) {
79 if (item != -1) {
80 TimerMgr->RemoveTimer(item);
81 anrTimerCount_--;
82 }
83 }
84 }
85
AddTimer(int32_t type,int32_t id,int64_t currentTime,SessionPtr sess)86 void ANRManager::AddTimer(int32_t type, int32_t id, int64_t currentTime, SessionPtr sess)
87 {
88 CHKPV(sess);
89 if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) {
90 MMI_HILOGD("Not application event, skip. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
91 return;
92 }
93 if (anrTimerCount_ >= MAX_ANR_TIMER_COUNT) {
94 MMI_HILOGD("Add anr timer failed, anrtimer count reached the maximum number:%{public}d", MAX_ANR_TIMER_COUNT);
95 return;
96 }
97 int32_t timerId = TimerMgr->AddTimer(INPUT_UI_TIMEOUT_TIME / TIME_CONVERT_RATIO, 1, [this, id, type, sess]() {
98 CHKPV(sess);
99 if (type == ANR_MONITOR || WinMgr->IsWindowVisible(sess->GetPid())) {
100 sess->SetAnrStatus(type, true);
101 DfxHisysevent::ApplicationBlockInput(sess);
102 MMI_HILOGE("Application not responding. pid:%{public}d, anr type:%{public}d, eventId:%{public}d",
103 sess->GetPid(), type, id);
104 CHK_INVALID_RV(anrNoticedPid_, "Add anr timer failed, timer count reached the maximum number");
105 NetPacket pkt(MmiMessageId::NOTICE_ANR);
106 pkt << sess->GetPid();
107 if (pkt.ChkRWError()) {
108 MMI_HILOGE("Packet write failed");
109 return;
110 }
111 auto fd = udsServer_->GetClientFd(anrNoticedPid_);
112 if (!udsServer_->SendMsg(fd, pkt)) {
113 MMI_HILOGE("Send message failed, errCode:%{public}d", MSG_SEND_FAIL);
114 return;
115 }
116 }
117 std::vector<int32_t> timerIds = sess->GetTimerIds(type);
118 for (int32_t item : timerIds) {
119 if (item != -1) {
120 TimerMgr->RemoveTimer(item);
121 anrTimerCount_--;
122 MMI_HILOGD("Clear anr timer, type:%{public}d, timer id:%{public}d, count:%{public}d",
123 type, item, anrTimerCount_);
124 }
125 }
126 });
127 CHK_INVALID_RV(timerId, "Add anr timer failed, timer count reached the maximum number");
128 anrTimerCount_++;
129 MMI_HILOGD("Add anr timer success, anr type:%{public}d, eventId:%{public}d, timer id:%{public}d, count:%{public}d",
130 type, id, timerId, anrTimerCount_);
131 sess->SaveANREvent(type, id, currentTime, timerId);
132 }
133
TriggerANR(int32_t type,int64_t time,SessionPtr sess)134 bool ANRManager::TriggerANR(int32_t type, int64_t time, SessionPtr sess)
135 {
136 CALL_DEBUG_ENTER;
137 CHKPF(udsServer_);
138 CHKPF(sess);
139 if (sess->GetTokenType() != TokenType::TOKEN_HAP || sess->GetProgramName() == FOUNDATION) {
140 MMI_HILOGD("Not application event, skip. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
141 return false;
142 }
143 if (sess->CheckAnrStatus(type)) {
144 MMI_HILOGD("Application not responding. pid:%{public}d, anr type:%{public}d", sess->GetPid(), type);
145 return true;
146 }
147 MMI_HILOGD("Event dispatch normal");
148 return false;
149 }
150
OnSessionLost(SessionPtr session)151 void ANRManager::OnSessionLost(SessionPtr session)
152 {
153 CALL_DEBUG_ENTER;
154 CHKPV(session);
155 if (anrNoticedPid_ == session->GetPid()) {
156 MMI_HILOGD("The anrNoticedPid_ is invalid");
157 anrNoticedPid_ = -1;
158 }
159 MMI_HILOGI("SessionLost remove all Timers");
160 RemoveTimers(session);
161 }
162
SetANRNoticedPid(int32_t pid)163 int32_t ANRManager::SetANRNoticedPid(int32_t pid)
164 {
165 CALL_DEBUG_ENTER;
166 anrNoticedPid_ = pid;
167 return RET_OK;
168 }
169 } // namespace MMI
170 } // namespace OHOS
171