• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         // RunningLockTimer exist, unregister timerId
41         POWER_HILOGW(FEATURE_RUNNING_LOCK, "RLT exist,unregistId=%{public}d", lastTimerId);
42         UnregisterTimer(lastTimerId);
43     }
44     bool once = true;
45     uint32_t curTimerId = handlerTimer_->Register(callback, timeoutMs, once);
46     if (curTimerId == OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
47         POWER_HILOGW(FEATURE_RUNNING_LOCK, "RegisterRunningLockTimer failed");
48         if (lastTimerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
49             RemoveRunningLockTimerMap(token);
50         }
51         return false;
52     }
53     // AddRunningLockTimer:timerid timeoutMs
54     POWER_HILOGI(FEATURE_RUNNING_LOCK, "Timerid=%{public}u,Ms=%{public}d", curTimerId, timeoutMs);
55     AddRunningLockTimerMap(token, curTimerId);
56     return true;
57 }
58 
UnregisterRunningLockTimer(const sptr<IRemoteObject> & token)59 bool RunningLockTimerHandler::UnregisterRunningLockTimer(const sptr<IRemoteObject> &token)
60 {
61     std::lock_guard<std::mutex> lock(mutex_);
62     uint32_t timerId = GetRunningLockTimerId(token);
63     if (timerId != OHOS::Utils::TIMER_ERR_DEAL_FAILED) {
64         // Running lock timer is exist, unregister timerId
65         POWER_HILOGI(FEATURE_RUNNING_LOCK, "timer exist,unregistId=%{public}d", timerId);
66         UnregisterTimer(timerId);
67         RemoveRunningLockTimerMap(token);
68     }
69     return true;
70 }
71 
GetRunningLockTimerId(const sptr<IRemoteObject> & token)72 uint32_t RunningLockTimerHandler::GetRunningLockTimerId(const sptr<IRemoteObject> &token)
73 {
74     uint32_t timerId = OHOS::Utils::TIMER_ERR_DEAL_FAILED;
75     auto iter = runninglockTimerMap_.find(token);
76     if (iter != runninglockTimerMap_.end()) {
77         timerId = iter->second;
78     }
79     return timerId;
80 }
81 
AddRunningLockTimerMap(const sptr<IRemoteObject> & token,uint32_t timerId)82 void RunningLockTimerHandler::AddRunningLockTimerMap(const sptr<IRemoteObject> &token, uint32_t timerId)
83 {
84     auto iter = runninglockTimerMap_.find(token);
85     if (iter == runninglockTimerMap_.end()) {
86         runninglockTimerMap_.emplace(token, timerId);
87         return;
88     }
89     iter->second = timerId;
90 }
91 
RemoveRunningLockTimerMap(const sptr<IRemoteObject> & token)92 void RunningLockTimerHandler::RemoveRunningLockTimerMap(const sptr<IRemoteObject> &token)
93 {
94     auto iter = runninglockTimerMap_.find(token);
95     if (iter != runninglockTimerMap_.end()) {
96         runninglockTimerMap_.erase(token);
97     }
98 }
99 
UnregisterTimer(uint32_t timerId)100 void RunningLockTimerHandler::UnregisterTimer(uint32_t timerId)
101 {
102     if (handlerTimer_ != nullptr) {
103         handlerTimer_->Unregister(timerId);
104     }
105 }
106 
CleanTimer()107 void RunningLockTimerHandler::CleanTimer()
108 {
109     if (handlerTimer_ != nullptr) {
110         for (auto& iter : runninglockTimerMap_) {
111             UnregisterTimer(iter.second);
112         }
113         runninglockTimerMap_.clear();
114         handlerTimer_->Shutdown();
115     }
116 }
117 } // namespace PowerMgr
118 } // namespace OHOS
119