• 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 <mutex>
22 #include <memory>
23 #include <stdint.h>
24 
25 #include "single_instance.h"
26 #include "timer_info.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 class TimerProxy {
31     DECLARE_SINGLE_INSTANCE(TimerProxy)
32 public:
33     void RemoveProxy(uint64_t timerNumber, int32_t uid);
34     void CallbackAlarmIfNeed(const std::shared_ptr<TimerInfo> &alarm);
35     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger,
36         const std::chrono::steady_clock::time_point &now,
37         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
38     bool ResetAllProxy(const std::chrono::steady_clock::time_point &now,
39         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
40     void EraseTimerFromProxyUidMap(const uint64_t id, const uint32_t uid);
41     void RecordUidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched);
42     void RemoveUidTimerMap(const std::shared_ptr<TimerInfo> &alarm);
43     void RemoveUidTimerMap(const uint64_t id);
44     bool IsUidProxy(const int32_t uid);
45     bool ShowProxyTimerInfo(int fd, const int64_t now);
46     bool ShowUidTimerMapInfo(int fd, const int64_t now);
47     bool SetProxyDelayTime(int fd, const int64_t proxyDelayTime);
48     bool ShowProxyDelayTime(int fd);
49     int64_t GetProxyDelayTime() const;
50 
51 private:
52     void ResetProxyMaps();
53     void EraseAlarmItem(
54         const uint64_t id, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>> &idAlarmsMap);
55     void UpdateProxyWhenElapsedForProxyUidMap(const int32_t uid,
56         const std::chrono::steady_clock::time_point &now,
57         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
58     bool RestoreProxyWhenElapsedByUid(const int32_t uid,
59         const std::chrono::steady_clock::time_point &now,
60         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
61     bool RestoreProxyWhenElapsedForProxyUidMap(const int32_t uid,
62         const std::chrono::steady_clock::time_point &now,
63         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
64     void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now,
65         std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback);
66 
67     std::mutex uidTimersMutex_;
68     /* <uid, <id, alarm ptr>> */
69     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> uidTimersMap_ {};
70     std::mutex proxyMutex_;
71     /* <uid, <id, trigger time>> */
72     std::unordered_map<int32_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyUids_ {};
73     std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_ {};
74     /* ms for 3 days */
75     int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000;
76 }; // timer_proxy
77 } // MiscServices
78 } // OHOS
79 
80 #endif // TIMER_PROXY_H