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 "single_instance.h" 19 #include "timer_info.h" 20 21 namespace OHOS { 22 namespace MiscServices { 23 using AdjustTimerCallback = std::function<bool(std::shared_ptr<TimerInfo> timer)>; 24 class TimerProxy { 25 DECLARE_SINGLE_INSTANCE(TimerProxy) 26 public: 27 int32_t CallbackAlarmIfNeed(const std::shared_ptr<TimerInfo> &alarm); 28 bool ProxyTimer(int32_t uid, int pid, bool isProxy, bool needRetrigger, 29 const std::chrono::steady_clock::time_point &now, 30 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback); 31 bool AdjustTimer(bool isAdjust, uint32_t interval, 32 const std::chrono::steady_clock::time_point &now, uint32_t delta, 33 std::function<void(AdjustTimerCallback adjustTimer)> updateTimerDeliveries); 34 bool SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption); 35 bool IsTimerExemption(std::shared_ptr<TimerInfo> time); 36 bool ResetAllProxy(const std::chrono::steady_clock::time_point &now, 37 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback); 38 void EraseTimerFromProxyTimerMap(const uint64_t id, const int uid, const int pid); 39 void RecordUidTimerMap(const std::shared_ptr<TimerInfo> &alarm, const bool isRebatched); 40 void RecordProxyTimerMap(const std::shared_ptr<TimerInfo> &alarm, bool isPid); 41 void RemoveUidTimerMap(const std::shared_ptr<TimerInfo> &alarm); 42 void RemoveUidTimerMapLocked(const std::shared_ptr<TimerInfo> &alarm); 43 void RemoveUidTimerMap(const uint64_t id); 44 int32_t CountUidTimerMapByUid(int32_t uid); 45 bool IsProxy(const int32_t uid, const int32_t pid); 46 bool IsProxyLocked(const int32_t uid, const int32_t pid); 47 #ifdef HIDUMPER_ENABLE 48 bool ShowProxyTimerInfo(int fd, const int64_t now); 49 bool ShowUidTimerMapInfo(int fd, const int64_t now); 50 bool ShowProxyDelayTime(int fd); 51 void ShowAdjustTimerInfo(int fd); 52 #endif 53 int64_t GetProxyDelayTime() const; 54 55 private: 56 void EraseAlarmItem( 57 const uint64_t id, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>> &idAlarmsMap); 58 void UpdateProxyWhenElapsedForProxyTimers(const int32_t uid, const int32_t pid, 59 const std::chrono::steady_clock::time_point &now, 60 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback, 61 bool needRetrigger); 62 bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now, uint32_t delta, 63 uint32_t interval, std::shared_ptr<TimerInfo> &timer); 64 bool RestoreAdjustWhenElapsed(std::shared_ptr<TimerInfo> &timer); 65 bool RestoreProxyWhenElapsed(const int32_t uid, const int32_t pid, 66 const std::chrono::steady_clock::time_point &now, 67 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback, 68 bool needRetrigger); 69 bool RestoreProxyWhenElapsedForProxyTimers(const int32_t uid, const int32_t pid, 70 const std::chrono::steady_clock::time_point &now, 71 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback, 72 bool needRetrigger); 73 void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now, 74 std::function<void(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger)> insertAlarmCallback); 75 76 std::mutex uidTimersMutex_; 77 /* <uid, <id, alarm ptr>> */ 78 std::unordered_map<int32_t, std::unordered_map<uint64_t, std::shared_ptr<TimerInfo>>> uidTimersMap_ {}; 79 std::mutex proxyMutex_; 80 /* <(uid << 32) | pid, [timerid]> */ 81 std::unordered_map<uint64_t, std::vector<uint64_t>> proxyTimers_ {}; 82 std::mutex adjustMutex_; 83 std::unordered_set<std::string> adjustExemptionList_ { "time_service" }; 84 std::unordered_set<uint64_t> adjustTimers_ {}; 85 /* ms for 3 days */ 86 int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000; 87 }; // timer_proxy 88 } // MiscServices 89 } // OHOS 90 91 #endif // TIMER_PROXY_H