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