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 
EventStage()32 EventStage::EventStage() {}
~EventStage()33 EventStage::~EventStage() {}
34 
SetAnrStatus(int32_t persistentId,bool status)35 void EventStage::SetAnrStatus(int32_t persistentId, bool status)
36 {
37     std::lock_guard<std::mutex> lock(mutex_);
38     isAnrProcess_[persistentId] = status;
39 }
40 
CheckAnrStatus(int32_t persistentId)41 bool EventStage::CheckAnrStatus(int32_t persistentId)
42 {
43     std::lock_guard<std::mutex> lock(mutex_);
44     if (isAnrProcess_.find(persistentId) != isAnrProcess_.end()) {
45         return isAnrProcess_[persistentId];
46     }
47     WLOGFD("Current persistentId:%{public}d is not in event stage", persistentId);
48     return false;
49 }
50 
SaveANREvent(int32_t persistentId,int32_t eventId,int32_t timerId)51 void EventStage::SaveANREvent(int32_t persistentId, int32_t eventId, int32_t timerId)
52 {
53     std::lock_guard<std::mutex> lock(mutex_);
54     EventTime eventTime { eventId, timerId };
55     events_[persistentId].push_back(eventTime);
56 }
57 
GetTimerIds(int32_t persistentId)58 std::vector<int32_t> EventStage::GetTimerIds(int32_t persistentId)
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     if (events_.find(persistentId) == events_.end()) {
62         WLOGFD("Current events have no event for persistentId:%{public}d", persistentId);
63         return {};
64     }
65     std::vector<int32_t> timers;
66     for (const auto &item : events_[persistentId]) {
67         timers.push_back(item.timerId);
68     }
69     return timers;
70 }
71 
DelEvents(int32_t persistentId,int32_t eventId)72 std::vector<int32_t> EventStage::DelEvents(int32_t persistentId, int32_t eventId)
73 {
74     std::lock_guard<std::mutex> lock(mutex_);
75     WLOGFD("Delete events, persistentId:%{public}d, eventId:%{public}d", persistentId, eventId);
76     if (events_.find(persistentId) == events_.end()) {
77         WLOGFD("Current events have no event persistentId:%{public}d", persistentId);
78         return {};
79     }
80     auto &events = events_[persistentId];
81     auto fistMatchIter = find_if(events.begin(), events.end(), [eventId](const auto& item) {
82         return item.eventId > eventId;
83     });
84     std::vector<int32_t> timerIds;
85     for (auto iter = events.begin(); iter != fistMatchIter; iter++) {
86         timerIds.push_back(iter->timerId);
87     }
88     events.erase(events.begin(), fistMatchIter);
89     isAnrProcess_[persistentId] = false;
90     return timerIds;
91 }
92 
OnSessionLost(int32_t persistentId)93 void EventStage::OnSessionLost(int32_t persistentId)
94 {
95     CALL_DEBUG_ENTER;
96     std::lock_guard<std::mutex> guard(mutex_);
97     events_.erase(persistentId);
98     isAnrProcess_.erase(persistentId);
99 }
100 } // namespace Rosen
101 } // namespace OHOS
102