1 /*
2  * Copyright (c) 2023-2024 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 
16 #ifndef RUNNINGLOCK_TIMERHANDKER_H
17 #define RUNNINGLOCK_TIMERHANDKER_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <timer.h>
22 #include <mutex>
23 
24 #include <iremote_object.h>
25 
26 namespace OHOS {
27 namespace PowerMgr {
28 class RunningLockTimerHandler {
29 public:
30     RunningLockTimerHandler() = default;
31     ~RunningLockTimerHandler();
32 
GetInstance()33     static RunningLockTimerHandler& GetInstance()
34     {
35         static RunningLockTimerHandler timerHandler;
36         return timerHandler;
37     }
38 
39     bool RegisterRunningLockTimer(const sptr<IRemoteObject> &token, const std::function<void()> &callback,
40         int32_t timeoutMs);
41     bool UnregisterRunningLockTimer(const sptr<IRemoteObject> &token);
42 
43 private:
44     uint32_t GetRunningLockTimerId(const sptr<IRemoteObject> &token);
45     void AddRunningLockTimerMap(const sptr<IRemoteObject> &token, uint32_t timerId);
46     void RemoveRunningLockTimerMap(const sptr<IRemoteObject> &token);
47     void UnregisterTimer(uint32_t timerId);
48     void CleanTimer();
49     std::unique_ptr<OHOS::Utils::Timer> handlerTimer_;
50     std::mutex mutex_;
51     std::map<const sptr<IRemoteObject>, uint32_t> runninglockTimerMap_;
52 };
53 } // namespace PowerMgr
54 } // namespace OHOS
55 
56 #endif // RUNNINGLOCK_TIMERHANDKER_H
57