/* * Copyright (C) 2023-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef TIMER_PROXY_H #define TIMER_PROXY_H #include #include #include #include #include #include #include #include "single_instance.h" #include "timer_info.h" namespace OHOS { namespace MiscServices { using AdjustTimerCallback = std::function timer)>; class TimerProxy { DECLARE_SINGLE_INSTANCE(TimerProxy) public: void RemoveProxy(uint64_t timerNumber, int32_t uid); void RemovePidProxy(uint64_t timerNumber, int32_t pid); int32_t CallbackAlarmIfNeed(const std::shared_ptr &alarm); bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool PidProxyTimer(int32_t uid, int pid, bool isProxy, bool needRetrigger, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool AdjustTimer(bool isAdjust, uint32_t interval, const std::chrono::steady_clock::time_point &now, std::function updateTimerDeliveries); bool SetTimerExemption(const std::unordered_set &nameArr, bool isExemption); bool IsTimerExemption(std::shared_ptr time); bool ResetAllProxy(const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); void EraseTimerFromProxyUidMap(const uint64_t id, const uint32_t uid); void EraseTimerFromProxyPidMap(const uint64_t id, const int uid, const int pid); void RecordUidTimerMap(const std::shared_ptr &alarm, const bool isRebatched); void RecordPidTimerMap(const std::shared_ptr &alarm, const bool isRebatched); void RecordProxyUidTimerMap(const std::shared_ptr &alarm); void RecordProxyPidTimerMap(const std::shared_ptr &alarm); void RemoveUidTimerMap(const std::shared_ptr &alarm); void RemovePidTimerMap(const std::shared_ptr &alarm); void RemoveUidTimerMap(const uint64_t id); void RemovePidTimerMap(const uint64_t id); bool IsUidProxy(const int32_t uid); bool IsPidProxy(const int32_t uid, const int32_t pid); bool ShowProxyTimerInfo(int fd, const int64_t now); bool ShowPidProxyTimerInfo(int fd, const int64_t now); bool ShowUidTimerMapInfo(int fd, const int64_t now); bool ShowPidTimerMapInfo(int fd, const int64_t now); bool ShowProxyDelayTime(int fd); void ShowAdjustTimerInfo(int fd); int64_t GetProxyDelayTime() const; private: void ResetProxyMaps(); void ResetProxyPidMaps(); void EraseAlarmItem( const uint64_t id, std::unordered_map> &idAlarmsMap); void UpdateProxyWhenElapsedForProxyUidMap(const int32_t uid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); void UpdateProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool UpdateAdjustWhenElapsed(const std::chrono::steady_clock::time_point &now, uint32_t interval, std::shared_ptr &timer); bool RestoreAdjustWhenElapsed(std::shared_ptr &timer); bool RestoreProxyWhenElapsedByUid(const int32_t uid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool RestoreProxyWhenElapsedByPid(const int32_t uid, const int32_t pid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool RestoreProxyWhenElapsedForProxyUidMap(const int32_t uid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); bool RestoreProxyWhenElapsedForProxyPidMap(const int32_t uid, const int32_t pid, const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); void ResetAllProxyWhenElapsed(const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); void ResetAllPidProxyWhenElapsed(const std::chrono::steady_clock::time_point &now, std::function &alarm)> insertAlarmCallback); std::mutex uidTimersMutex_; std::mutex pidTimersMutex_; /* > */ std::unordered_map>> uidTimersMap_ {}; std::unordered_map>> pidTimersMap_ {}; std::mutex proxyMutex_; std::mutex proxyPidMutex_; /* > */ std::unordered_map> proxyUids_ {}; /* <(uid << 32) | pid, */ std::unordered_map> proxyPids_ {}; std::map>> proxyMap_ {}; std::map>> proxyPidMap_ {}; std::mutex adjustMutex_; std::unordered_set adjustExemptionList_ { "time_service" }; std::vector> adjustTimers_ {}; /* ms for 3 days */ int64_t proxyDelayTime_ = 3 * 24 * 60 * 60 * 1000; }; // timer_proxy } // MiscServices } // OHOS #endif // TIMER_PROXY_H