1 /*
2  * Copyright (C) 2023-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 #ifndef TIMER_PROXY_H
16 #define TIMER_PROXY_H
17 
18 #include <chrono>
19 #include <functional>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <mutex>
23 #include <memory>
24 #include <stdint.h>
25 
26 #include "single_instance.h"
27 #include "timer_info.h"
28 
29 namespace OHOS {
30 namespace MiscServices {
31 using AdjustTimerCallback = std::function<bool(std::shared_ptr<TimerInfo> timer)>;
32 class TimerProxy {
33     DECLARE_SINGLE_INSTANCE(TimerProxy)
34 public:
35     void RemoveProxy(uint64_t timerNumber, int32_t uid);
36     void RemovePidProxy(uint64_t timerNumber, int32_t pid);
37     int32_t CallbackAlarmIfNeed(const std::shared_ptr<TimerInfo> &alarm);
38     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger,
39         const std::chrono::steady_clock::time_point &now,
40         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
41     bool PidProxyTimer(int32_t uid, int pid, bool isProxy, bool needRetrigger,
42         const std::chrono::steady_clock::time_point &now,
43         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
44     bool AdjustTimer(bool isAdjust, uint32_t interval,
45         const std::chrono::steady_clock::time_point &now,
46         std::function<void(AdjustTimerCallback adjustTimer)> updateTimerDeliveries);
47     bool SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption);
48     bool IsTimerExemption(std::shared_ptr<TimerInfo> time);
49     bool ResetAllProxy(const std::chrono::steady_clock::time_point &now,
50         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
51     void EraseTimerFromProxyUidMap(const uint64_t id, const uint32_t uid);
52     void EraseTimerFromProxyPidMap(const uint64_t id, const int uid, const int pid);
53     void RecordUidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched);
54     void RecordPidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched);
55     void RecordProxyUidTimerMap(const std::shared_ptr<TimerInfo> &alarm);
56     void RecordProxyPidTimerMap(const std::shared_ptr<TimerInfo> &alarm);
57     void RemoveUidTimerMap(const std::shared_ptr<TimerInfo> &alarm);
58     void RemovePidTimerMap(const std::shared_ptr<TimerInfo> &alarm);
59     void RemoveUidTimerMap(const uint64_t id);
60     void RemovePidTimerMap(const uint64_t id);
61     bool IsUidProxy(const int32_t uid);
62     bool IsPidProxy(const int32_t uid, const int32_t pid);
63     bool ShowProxyTimerInfo(int fd, const int64_t now);
64     bool ShowPidProxyTimerInfo(int fd, const int64_t now);
65     bool ShowUidTimerMapInfo(int fd, const int64_t now);
66     bool ShowPidTimerMapInfo(int fd, const int64_t now);
67     bool ShowProxyDelayTime(int fd);
68     void ShowAdjustTimerInfo(int fd);
69     int64_t GetProxyDelayTime() const;
70 
71 private:
72     void ResetProxyMaps();
73     void ResetProxyPidMaps();
74     void EraseAlarmItem(
75         const uint64_t id, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>> &idAlarmsMap);
76     void UpdateProxyWhenElapsedForProxyUidMap(const int32_t uid,
77         const std::chrono::steady_clock::time_point &now,
78         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
79     void UpdateProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid,
80         const std::chrono::steady_clock::time_point &now,
81         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
82     bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now,
83         uint32_t interval, std::shared_ptr<TimerInfo> &timer);
84     bool RestoreAdjustWhenElapsed(std::shared_ptr<TimerInfo> &timer);
85     bool RestoreProxyWhenElapsedByUid(const int32_t uid,
86         const std::chrono::steady_clock::time_point &now,
87         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
88     bool RestoreProxyWhenElapsedByPid(const int32_t uid, const int32_t pid,
89         const std::chrono::steady_clock::time_point &now,
90         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
91     bool RestoreProxyWhenElapsedForProxyUidMap(const int32_t uid,
92         const std::chrono::steady_clock::time_point &now,
93         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
94     bool RestoreProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid,
95         const std::chrono::steady_clock::time_point &now,
96         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
97     void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now,
98         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
99     void ResetAllPidProxyWhenElapsed(const std::chrono::steady_clock::time_point &now,
100         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
101 
102     std::mutex uidTimersMutex_;
103     std::mutex pidTimersMutex_;
104     /* <uid, <id, alarm ptr>> */
105     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> uidTimersMap_ {};
106     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> pidTimersMap_ {};
107     std::mutex proxyMutex_;
108     std::mutex proxyPidMutex_;
109     /* <uid, <id, trigger time>> */
110     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyUids_ {};
111     /* <(uid << 32) | pid, <id, trigger time> */
112     std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyPids_ {};
113     std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_ {};
114     std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyPidMap_ {};
115     std::mutex adjustMutex_;
116     std::unordered_set<std::string> adjustExemptionList_ { "time_service" };
117     std::vector<std::shared_ptr<TimerInfo>> adjustTimers_ {};
118     /* ms for 3 days */
119     int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000;
120 }; // timer_proxy
121 } // MiscServices
122 } // OHOS
123 
124 #endif // TIMER_PROXY_H