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