1cf69771bSopenharmony_ci/* 2cf69771bSopenharmony_ci * Copyright (C) 2023-2023 Huawei Device Co., Ltd. 3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License. 5cf69771bSopenharmony_ci * You may obtain a copy of the License at 6cf69771bSopenharmony_ci * 7cf69771bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cf69771bSopenharmony_ci * 9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and 13cf69771bSopenharmony_ci * limitations under the License. 14cf69771bSopenharmony_ci */ 15cf69771bSopenharmony_ci#ifndef TIMER_PROXY_H 16cf69771bSopenharmony_ci#define TIMER_PROXY_H 17cf69771bSopenharmony_ci 18cf69771bSopenharmony_ci#include <chrono> 19cf69771bSopenharmony_ci#include <functional> 20cf69771bSopenharmony_ci#include <unordered_map> 21cf69771bSopenharmony_ci#include <unordered_set> 22cf69771bSopenharmony_ci#include <mutex> 23cf69771bSopenharmony_ci#include <memory> 24cf69771bSopenharmony_ci#include <stdint.h> 25cf69771bSopenharmony_ci 26cf69771bSopenharmony_ci#include "single_instance.h" 27cf69771bSopenharmony_ci#include "timer_info.h" 28cf69771bSopenharmony_ci 29cf69771bSopenharmony_cinamespace OHOS { 30cf69771bSopenharmony_cinamespace MiscServices { 31cf69771bSopenharmony_ciusing AdjustTimerCallback = std::function<bool(std::shared_ptr<TimerInfo> timer)>; 32cf69771bSopenharmony_ciclass TimerProxy { 33cf69771bSopenharmony_ci DECLARE_SINGLE_INSTANCE(TimerProxy) 34cf69771bSopenharmony_cipublic: 35cf69771bSopenharmony_ci void RemoveProxy(uint64_t timerNumber, int32_t uid); 36cf69771bSopenharmony_ci void RemovePidProxy(uint64_t timerNumber, int32_t uid); 37cf69771bSopenharmony_ci int32_t CallbackAlarmIfNeed(const std::shared_ptr<TimerInfo> &alarm); 38cf69771bSopenharmony_ci bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger, 39cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 40cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 41cf69771bSopenharmony_ci bool PidProxyTimer(int pidList, bool isProxy, bool needRetrigger, 42cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 43cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 44cf69771bSopenharmony_ci bool AdjustTimer(bool isAdjust, uint32_t interval, 45cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 46cf69771bSopenharmony_ci std::function<void(AdjustTimerCallback adjustTimer)> updateTimerDeliveries); 47cf69771bSopenharmony_ci bool SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption); 48cf69771bSopenharmony_ci bool IsTimerExemption(std::shared_ptr<TimerInfo> time); 49cf69771bSopenharmony_ci bool ResetAllProxy(const std::chrono::steady_clock::time_point &now, 50cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 51cf69771bSopenharmony_ci void EraseTimerFromProxyUidMap(const uint64_t id, const uint32_t uid); 52cf69771bSopenharmony_ci void EraseTimerFromProxyPidMap(const uint64_t id, const int pid); 53cf69771bSopenharmony_ci void RecordUidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched); 54cf69771bSopenharmony_ci void RecordPidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched); 55cf69771bSopenharmony_ci void RecordProxyUidTimerMap(const std::shared_ptr<TimerInfo> &alarm); 56cf69771bSopenharmony_ci void RecordProxyPidTimerMap(const std::shared_ptr<TimerInfo> &alarm); 57cf69771bSopenharmony_ci void RemoveUidTimerMap(const std::shared_ptr<TimerInfo> &alarm); 58cf69771bSopenharmony_ci void RemovePidTimerMap(const std::shared_ptr<TimerInfo> &alarm); 59cf69771bSopenharmony_ci void RemoveUidTimerMap(const uint64_t id); 60cf69771bSopenharmony_ci void RemovePidTimerMap(const uint64_t id); 61cf69771bSopenharmony_ci bool IsUidProxy(const int32_t uid); 62cf69771bSopenharmony_ci bool IsPidProxy(const int32_t pid); 63cf69771bSopenharmony_ci bool ShowProxyTimerInfo(int fd, const int64_t now); 64cf69771bSopenharmony_ci bool ShowPidProxyTimerInfo(int fd, const int64_t now); 65cf69771bSopenharmony_ci bool ShowUidTimerMapInfo(int fd, const int64_t now); 66cf69771bSopenharmony_ci bool ShowPidTimerMapInfo(int fd, const int64_t now); 67cf69771bSopenharmony_ci bool ShowProxyDelayTime(int fd); 68cf69771bSopenharmony_ci void ShowAdjustTimerInfo(int fd); 69cf69771bSopenharmony_ci int64_t GetProxyDelayTime() const; 70cf69771bSopenharmony_ci 71cf69771bSopenharmony_ciprivate: 72cf69771bSopenharmony_ci void ResetProxyMaps(); 73cf69771bSopenharmony_ci void ResetProxyPidMaps(); 74cf69771bSopenharmony_ci void EraseAlarmItem( 75cf69771bSopenharmony_ci const uint64_t id, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>> &idAlarmsMap); 76cf69771bSopenharmony_ci void UpdateProxyWhenElapsedForProxyUidMap(const int32_t uid, 77cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 78cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 79cf69771bSopenharmony_ci void UpdateProxyWhenElapsedForProxyPidMap(const int32_t uid, 80cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 81cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 82cf69771bSopenharmony_ci bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now, 83cf69771bSopenharmony_ci uint32_t interval, std::shared_ptr<TimerInfo> &timer); 84cf69771bSopenharmony_ci bool RestoreAdjustWhenElapsed(std::shared_ptr<TimerInfo> &timer); 85cf69771bSopenharmony_ci bool RestoreProxyWhenElapsedByUid(const int32_t uid, 86cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 87cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 88cf69771bSopenharmony_ci bool RestoreProxyWhenElapsedByPid(const int32_t uid, 89cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 90cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 91cf69771bSopenharmony_ci bool RestoreProxyWhenElapsedForProxyUidMap(const int32_t uid, 92cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 93cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 94cf69771bSopenharmony_ci bool RestoreProxyWhenElapsedForProxyPidMap(const int32_t uid, 95cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &now, 96cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 97cf69771bSopenharmony_ci void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now, 98cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 99cf69771bSopenharmony_ci void ResetAllPidProxyWhenElapsed(const std::chrono::steady_clock::time_point &now, 100cf69771bSopenharmony_ci std::function<void(std::shared_ptr<TimerInfo> &alarm)> insertAlarmCallback); 101cf69771bSopenharmony_ci 102cf69771bSopenharmony_ci std::mutex uidTimersMutex_; 103cf69771bSopenharmony_ci std::mutex pidTimersMutex_; 104cf69771bSopenharmony_ci /* <uid, <id, alarm ptr>> */ 105cf69771bSopenharmony_ci std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> uidTimersMap_ {}; 106cf69771bSopenharmony_ci std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> pidTimersMap_ {}; 107cf69771bSopenharmony_ci std::mutex proxyMutex_; 108cf69771bSopenharmony_ci std::mutex proxyPidMutex_; 109cf69771bSopenharmony_ci /* <uid, <id, trigger time>> */ 110cf69771bSopenharmony_ci std::unordered_map<int32_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyUids_ {}; 111cf69771bSopenharmony_ci std::unordered_map<int32_t, std::unordered_map<uint64_t, std::chrono::steady_clock::time_point>> proxyPids_ {}; 112cf69771bSopenharmony_ci std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_ {}; 113cf69771bSopenharmony_ci std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyPidMap_ {}; 114cf69771bSopenharmony_ci std::mutex adjustMutex_; 115cf69771bSopenharmony_ci std::unordered_set<std::string> adjustExemptionList_ {}; 116cf69771bSopenharmony_ci std::vector<std::shared_ptr<TimerInfo>> adjustTimers_ {}; 117cf69771bSopenharmony_ci /* ms for 3 days */ 118cf69771bSopenharmony_ci int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000; 119cf69771bSopenharmony_ci}; // timer_proxy 120cf69771bSopenharmony_ci} // MiscServices 121cf69771bSopenharmony_ci} // OHOS 122cf69771bSopenharmony_ci 123cf69771bSopenharmony_ci#endif // TIMER_PROXY_H