• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t CountUidTimerMapByUid(int32_t uid);
62     bool IsUidProxy(const int32_t uid);
63     bool IsPidProxy(const int32_t uid, const int32_t pid);
64     bool ShowProxyTimerInfo(int fd, const int64_t now);
65     bool ShowPidProxyTimerInfo(int fd, const int64_t now);
66     bool ShowUidTimerMapInfo(int fd, const int64_t now);
67     bool ShowPidTimerMapInfo(int fd, const int64_t now);
68     bool ShowProxyDelayTime(int fd);
69     void ShowAdjustTimerInfo(int fd);
70     int64_t GetProxyDelayTime() const;
71 
72 private:
73     void ResetProxyMaps();
74     void ResetProxyPidMaps();
75     void EraseAlarmItem(
76         const uint64_t id, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>> &idAlarmsMap);
77     void UpdateProxyWhenElapsedForProxyUidMap(const int32_t uid,
78         const std::chrono::steady_clock::time_point &now,
79         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
80     void UpdateProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid,
81         const std::chrono::steady_clock::time_point &now,
82         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
83     bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now,
84         uint32_t interval, std::shared_ptr<TimerInfo> &timer);
85     bool RestoreAdjustWhenElapsed(std::shared_ptr<TimerInfo> &timer);
86     bool RestoreProxyWhenElapsedByUid(const int32_t uid,
87         const std::chrono::steady_clock::time_point &now,
88         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
89     bool RestoreProxyWhenElapsedByPid(const int32_t uid, const int32_t pid,
90         const std::chrono::steady_clock::time_point &now,
91         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
92     bool RestoreProxyWhenElapsedForProxyUidMap(const int32_t uid,
93         const std::chrono::steady_clock::time_point &now,
94         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
95     bool RestoreProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid,
96         const std::chrono::steady_clock::time_point &now,
97         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
98     void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now,
99         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
100     void ResetAllPidProxyWhenElapsed(const std::chrono::steady_clock::time_point &now,
101         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
102 
103     std::mutex uidTimersMutex_;
104     std::mutex pidTimersMutex_;
105     /* <uid, <id, alarm ptr>> */
106     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> uidTimersMap_ {};
107     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> pidTimersMap_ {};
108     std::mutex proxyMutex_;
109     std::mutex proxyPidMutex_;
110     /* <uid, <id, trigger time>> */
111     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyUids_ {};
112     /* <(uid << 32) | pid, <id, trigger time> */
113     std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyPids_ {};
114     std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_ {};
115     std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyPidMap_ {};
116     std::mutex adjustMutex_;
117     std::unordered_set<std::string> adjustExemptionList_ { "time_service" };
118     std::vector<std::shared_ptr<TimerInfo>> adjustTimers_ {};
119     /* ms for 3 days */
120     int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000;
121 }; // timer_proxy
122 } // MiscServices
123 } // OHOS
124 
125 #endif // TIMER_PROXY_H