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 #include "running_lock_timer_handler.h"
16 
17 #include "common_timer_errors.h"
18 #include "power_log.h"
19 
20 namespace OHOS {
21 namespace PowerMgr {
22 namespace {
23 const std::string RUNNINGLOCK_TIMER_HANDLER_NAME = "RunningLockTimer";
24 }
~RunningLockTimerHandler()25 RunningLockTimerHandler::~RunningLockTimerHandler()
26 {
27     CleanTimer();
28 }
29 
RegisterRunningLockTimer(const sptr<IRemoteObject> & token,const std::function<void ()> & callback,int32_t timeoutMs)30 bool RunningLockTimerHandler::RegisterRunningLockTimer(
31     const sptr<IRemoteObject> &token, const std::function<void()> &callback, int32_t timeoutMs)
32 {
33     std::lock_guard<std::mutex> lock(mutex_);
34     if (handlerTimer_ == nullptr) {
35         handlerTimer_ = std::make_unique<OHOS::Utils::Timer>(RUNNINGLOCK_TIMER_HANDLER_NAME);
36         handlerTimer_->Setup();
37     }
38     uint32_t lastTimerId = GetRunningLockTimerId(token);
39     if (lastTimerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
40         POWER_HILOGW(FEATURE_RUNNING_LOCK, "RunningLockTimer exist, unregister timerId=%{public}d", lastTimerId);
41         UnregisterTimer(lastTimerId);
42     }
43     bool once = true;
44     uint32_t curTimerId = handlerTimer_->Register(callback, timeoutMs, once);
45     if (curTimerId == OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
46         POWER_HILOGW(FEATURE_RUNNING_LOCK, "RegisterRunningLockTimer failed");
47         if (lastTimerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
48             RemoveRunningLockTimerMap(token);
49         }
50         return false;
51     }
52     POWER_HILOGI(FEATURE_RUNNING_LOCK, "AddRunningLockTimer timerid=%{public}u, timeoutMs=%{public}d",
53         curTimerId, timeoutMs);
54     AddRunningLockTimerMap(token, curTimerId);
55     return true;
56 }
57 
UnregisterRunningLockTimer(const sptr<IRemoteObject> & token)58 bool RunningLockTimerHandler::UnregisterRunningLockTimer(const sptr<IRemoteObject> &token)
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     uint32_t timerId = GetRunningLockTimerId(token);
62     if (timerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
63         POWER_HILOGI(FEATURE_RUNNING_LOCK, "Running lock timer is exist, unregister timerId=%{public}d", timerId);
64         UnregisterTimer(timerId);
65         RemoveRunningLockTimerMap(token);
66     }
67     return true;
68 }
69 
GetRunningLockTimerId(const sptr<IRemoteObject> & token)70 uint32_t RunningLockTimerHandler::GetRunningLockTimerId(const sptr<IRemoteObject> &token)
71 {
72     uint32_t timerId = OHOS::Utils::TIMER_ERR_DEAL_FAILED;
73     auto iter = runninglockTimerMap_.find(token);
74     if (iter != runninglockTimerMap_.end()) {
75         timerId = iter->second;
76     }
77     return timerId;
78 }
79 
AddRunningLockTimerMap(const sptr<IRemoteObject> & token,uint32_t timerId)80 void RunningLockTimerHandler::AddRunningLockTimerMap(const sptr<IRemoteObject> &token, uint32_t timerId)
81 {
82     auto iter = runninglockTimerMap_.find(token);
83     if (iter == runninglockTimerMap_.end()) {
84         runninglockTimerMap_.emplace(token, timerId);
85         return;
86     }
87     iter->second = timerId;
88 }
89 
RemoveRunningLockTimerMap(const sptr<IRemoteObject> & token)90 void RunningLockTimerHandler::RemoveRunningLockTimerMap(const sptr<IRemoteObject> &token)
91 {
92     auto iter = runninglockTimerMap_.find(token);
93     if (iter != runninglockTimerMap_.end()) {
94         runninglockTimerMap_.erase(token);
95     }
96 }
97 
UnregisterTimer(uint32_t timerId)98 void RunningLockTimerHandler::UnregisterTimer(uint32_t timerId)
99 {
100     if (handlerTimer_ != nullptr) {
101         handlerTimer_->Unregister(timerId);
102     }
103 }
104 
CleanTimer()105 void RunningLockTimerHandler::CleanTimer()
106 {
107     if (handlerTimer_ != nullptr) {
108         for (auto& iter : runninglockTimerMap_) {
109             UnregisterTimer(iter.second);
110         }
111         runninglockTimerMap_.clear();
112         handlerTimer_->Shutdown();
113     }
114 }
115 } // namespace PowerMgr
116 } // namespace OHOS
117