1 /*
2  * Copyright (c) 2021-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 RENDER_SERVICE_BASE_COMMON_RS_EVENT_MANAGER_H
17 #define RENDER_SERVICE_BASE_COMMON_RS_EVENT_MANAGER_H
18 
19 #include <chrono>
20 #include "rs_event_detector.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 struct RSEventState final {
25     int eventIntervalMs = 60000; // the mini interval between two same event is 60000 ms
26     uint64_t prevEventTimeStampMs = 0;
27 };
28 
29 class RSEventManager final {
30 public:
31     RSEventManager() = default;
32     ~RSEventManager();
33     RSEventManager(const RSEventManager&) = delete;
34     RSEventManager(const RSEventManager&&) = delete;
35     RSEventManager& operator=(const RSEventManager&) = delete;
36     RSEventManager& operator=(const RSEventManager&&) = delete;
37     void UpdateParam(); // Update Param: timeout\Event Frequency...
38     void AddEvent(const std::shared_ptr<RSBaseEventDetector>& detectorPtr,
39     int eventIntervalMs = 60000); // 60000ms: 1min
40     void RemoveEvent(std::string stringId);
41     void DumpAllEventParam(std::string& dumpString);
42 private:
43     void UpdateDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr);
44     void UpdateEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr);
45     void EventReport(const RSSysEventMsg& eventMsg);
46     void Clear();
47     void DumpDetectorParam(std::shared_ptr<RSBaseEventDetector> detectorPtr, std::string& dumpString);
48     void DumpEventIntervalMs(std::shared_ptr<RSBaseEventDetector> detectorPtr, std::string& dumpString);
49     std::map<std::string, std::weak_ptr<RSBaseEventDetector>> eventDetectorList_;
50     std::map<std::string, RSEventState> eventStateList_;
51     std::mutex listMutex_;
52     int updateCount_ = 0;
53     int updateThreshold_ = 200; // every 200 times, update param
54 };
55 }
56 }
57 #endif // RENDER_SERVICE_BASE_COMMON_RS_EVENT_MANAGER_H