1 /*
2 * Copyright (c) 2023 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 "event_stage.h"
17
18 #include <algorithm>
19
20 #include "entrance_log.h"
21 #include "proto.h"
22 #include "util.h"
23 #include "window_manager_hilog.h"
24
25 namespace OHOS {
26 namespace Rosen {
27
28 namespace {
29 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "EventStage" };
30 } // namespace
31
SetAnrStatus(int32_t persistentId,bool status)32 void EventStage::SetAnrStatus(int32_t persistentId, bool status)
33 {
34 isAnrProcess_[persistentId] = status;
35 }
36
CheckAnrStatus(int32_t persistentId)37 bool EventStage::CheckAnrStatus(int32_t persistentId)
38 {
39 if (isAnrProcess_.find(persistentId) != isAnrProcess_.end()) {
40 return isAnrProcess_[persistentId];
41 }
42 WLOGFD("Current persistentId:%{public}d is not in event stage", persistentId);
43 return false;
44 }
45
SaveANREvent(int32_t persistentId,int32_t eventId,int32_t timerId)46 void EventStage::SaveANREvent(int32_t persistentId, int32_t eventId, int32_t timerId)
47 {
48 EventTime eventTime { eventId, timerId };
49 events_[persistentId].push_back(eventTime);
50 }
51
GetTimerIds(int32_t persistentId)52 std::vector<int32_t> EventStage::GetTimerIds(int32_t persistentId)
53 {
54 if (events_.find(persistentId) == events_.end()) {
55 WLOGFD("Current events have no event for persistentId:%{public}d", persistentId);
56 return {};
57 }
58 std::vector<int32_t> timers;
59 for (auto &item : events_[persistentId]) {
60 timers.push_back(item.timerId);
61 item.timerId = -1;
62 }
63 return timers;
64 }
65
DelEvents(int32_t persistentId,int32_t eventId)66 std::list<int32_t> EventStage::DelEvents(int32_t persistentId, int32_t eventId)
67 {
68 WLOGFD("Delete events, persistentId:%{public}d, eventId:%{public}d", persistentId, eventId);
69 if (events_.find(persistentId) == events_.end()) {
70 WLOGFD("Current events have no event persistentId:%{public}d", persistentId);
71 return {};
72 }
73 auto &events = events_[persistentId];
74 auto fistMatchIter = find_if(events.begin(), events.end(), [eventId](const auto &item) {
75 return item.eventId > eventId;
76 });
77 std::list<int32_t> timerIds;
78 for (auto iter = events.begin(); iter != fistMatchIter; iter++) {
79 timerIds.push_back(iter->timerId);
80 }
81 events.erase(events.begin(), fistMatchIter);
82 SetAnrStatus(persistentId, false);
83 return timerIds;
84 }
85
OnSessionLost(int32_t persistentId)86 void EventStage::OnSessionLost(int32_t persistentId)
87 {
88 CALL_DEBUG_ENTER;
89 events_.erase(persistentId);
90 isAnrProcess_.erase(persistentId);
91 }
92
93 } // namespace Rosen
94 } // namespace OHOS
95