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 #ifndef ANR_HANDLER_H
17 #define ANR_HANDLER_H
18 
19 #include <atomic>
20 #include <mutex>
21 
22 #include "singleton.h"
23 
24 #include "proto.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 class ANRHandler {
29     DECLARE_DELAYED_SINGLETON(ANRHandler);
30 
31 public:
32     DISALLOW_COPY_AND_MOVE(ANRHandler);
33 
34     void SetLastProcessedEventId(int32_t eventType, int32_t eventId, int64_t actionTime);
35     void MarkProcessed(int32_t eventType, int32_t eventId);
36     void ResetAnrArray();
37     std::mutex anrMtx_;
38 
39 private:
40     struct ANREvent {
41         bool sendStatus { false };
42         int32_t lastEventId { -1 };
43         int32_t lastReportId { -1 };
44     };
45     ANREvent event_[ANR_EVENT_TYPE_NUM];
46     int32_t lastEventId_ { 0 };
47     int32_t processedCount_ { 0 };
48     std::vector<int32_t> idList_;
49     void SendEvent(int32_t eventType, int32_t eventId);
50     std::mutex mutex_;
51 };
52 
53 #define ANRHDL ::OHOS::DelayedSingleton<ANRHandler>::GetInstance()
54 } // namespace MMI
55 } // namespace OHOS
56 #endif // ANR_HANDLER_H