• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 <sstream>
16 #include "sclock_log.h"
17 #include "innerlistenermanager.h"
18 #include "screenlock_common.h"
19 #include "common_helper.h"
20 
21 namespace OHOS {
22 namespace ScreenLock {
23 using DeathRecipient = IRemoteObject::DeathRecipient;
24 const std::int64_t SUCCESS = 0;
25 std::mutex InnerListenerManager::instanceLock_;
26 sptr<InnerListenerManager> InnerListenerManager::instance_;
27 
GetInstance()28 sptr<InnerListenerManager> InnerListenerManager::GetInstance()
29 {
30     if (instance_ == nullptr) {
31         std::lock_guard<std::mutex> lock(instanceLock_);
32         if (instance_ == nullptr) {
33             SCLOCK_HILOGI("InnerListenerManager create instance.");
34             instance_ = new (std::nothrow) InnerListenerManager;
35         }
36     }
37     return instance_;
38 }
39 
RegisterInnerListener(int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)40 int32_t InnerListenerManager::RegisterInnerListener(int32_t userId, const ListenType listenType,
41                                                     const sptr<InnerListenerIf>& listener)
42 {
43     std::lock_guard<std::recursive_mutex> lock(mutex_);
44     if (listener == nullptr) {
45         SCLOCK_HILOGE("listener is nullptr");
46         return E_SCREENLOCK_NULLPTR;
47     }
48 
49     int32_t result = AddDeathRecipient(listenType, listener);
50     if (result != SUCCESS) {
51         SCLOCK_HILOGE("AddDeathRecipient fail");
52         return result;
53     }
54 
55     if (userId == static_cast<int32_t>(SpecialUserId::USER_CURRENT)) {
56         userId = GetUserIdFromCallingUid();
57     }
58 
59     result = AddInnerListener(userId, listenType, listener);
60     if (result != SUCCESS) {
61         SCLOCK_HILOGE("RegisterInnerListener fail");
62         return result;
63     }
64 
65     SCLOCK_HILOGI("RegisterInnerListener success");
66     return SUCCESS;
67 }
68 
UnRegisterInnerListener(const ListenType listenType,const sptr<InnerListenerIf> & listener)69 int32_t InnerListenerManager::UnRegisterInnerListener(const ListenType listenType,
70                                                       const sptr<InnerListenerIf>& listener)
71 {
72     std::lock_guard<std::recursive_mutex> lock(mutex_);
73     if (listener == nullptr) {
74         SCLOCK_HILOGE("listener is nullptr");
75         return E_SCREENLOCK_NULLPTR;
76     }
77     int32_t result = RemoveDeathRecipient(listener);
78     if (result != SUCCESS) {
79         SCLOCK_HILOGE("RemoveDeathRecipient fail");
80         return result;
81     }
82     // Remove the listener from the map
83     result = RemoveInnerListener(listenType, listener);
84     if (result != SUCCESS) {
85         SCLOCK_HILOGE("RemoveDeathRecipient fail");
86         return result;
87     }
88 
89     SCLOCK_HILOGI("UnRegisterInnerListener success");
90     return SUCCESS;
91 }
92 
AddInnerListener(int32_t userId,const ListenType listenType,const sptr<InnerListenerIf> & listener)93 int32_t InnerListenerManager::AddInnerListener(int32_t userId, const ListenType listenType,
94                                                const sptr<InnerListenerIf>& listener)
95 {
96     std::lock_guard<std::recursive_mutex> lock(mutex_);
97     SCLOCK_HILOGI("AddInnerListener, userId:%{public}d, listenType:%{public}d",
98                   userId, static_cast<int32_t>(listenType));
99     if (innerListenMap_.find(listenType) == innerListenMap_.end()) {
100         std::map<int32_t, std::set<sptr<InnerListenerIf>>> eventListenerMap;
101         innerListenMap_.emplace(listenType, eventListenerMap);
102     }
103 
104     if (innerListenMap_[listenType].find(userId) == innerListenMap_[listenType].end()) {
105         std::set<sptr<InnerListenerIf>> listenSet;
106         innerListenMap_[listenType].emplace(userId, listenSet);
107     }
108 
109     auto iter = std::find_if(innerListenMap_[listenType][userId].begin(), innerListenMap_[listenType][userId].end(),
110                              FinderSet(listener->AsObject()));
111     if (iter != innerListenMap_[listenType][userId].end()) {
112         SCLOCK_HILOGE("listener is already registed");
113         return SUCCESS;
114     }
115     innerListenMap_[listenType][userId].insert(listener);
116     return SUCCESS;
117 }
118 
RemoveInnerListener(const ListenType listenType,const sptr<InnerListenerIf> & listener)119 int32_t InnerListenerManager::RemoveInnerListener(const ListenType listenType,
120                                                   const sptr<InnerListenerIf>& listener)
121 {
122     std::lock_guard<std::recursive_mutex> lock(mutex_);
123     auto listenMapIter = innerListenMap_.find(listenType);
124     if (listenMapIter == innerListenMap_.end()) {
125         SCLOCK_HILOGE("RemoveInnerListener listenType not exit in innerListenMap_");
126         return SUCCESS;
127     }
128 
129     for (auto& pair : innerListenMap_[listenType]) {
130         int32_t userId = pair.first;
131         auto iter = std::find_if(innerListenMap_[listenType][userId].begin(), innerListenMap_[listenType][userId].end(),
132                                  FinderSet(listener->AsObject()));
133         if (iter != innerListenMap_[listenType][userId].end()) {
134             innerListenMap_[listenType][userId].erase(iter);
135             auto length = static_cast<int>(innerListenMap_[listenType][userId].size());
136             SCLOCK_HILOGI("Remove userId:%{public}d, length=%{public}d", static_cast<int32_t>(userId), length);
137             return SUCCESS;
138         }
139     }
140 
141     SCLOCK_HILOGI("listener not exit");
142     return SUCCESS;
143 }
144 
HasListenerSet(int32_t userId,ListenType listenType)145 bool InnerListenerManager::HasListenerSet(int32_t userId, ListenType listenType)
146 {
147     std::lock_guard<std::recursive_mutex> lock(mutex_);
148     if (innerListenMap_.find(listenType) == innerListenMap_.end()) {
149         return false;
150     }
151 
152     if (innerListenMap_[listenType].find(userId) == innerListenMap_[listenType].end()) {
153         return false;
154     }
155     return true;
156 }
157 
OnStrongAuthChanged(int32_t userId,int32_t strongAuth)158 void InnerListenerManager::OnStrongAuthChanged(int32_t userId, int32_t strongAuth)
159 {
160     SCLOCK_HILOGI("OnStrongAuthChanged enter.");
161     OnStateChanged(userId, strongAuth, ListenType::STRONG_AUTH);
162 }
163 
OnDeviceLockStateChanged(int32_t userId,int32_t lockState)164 void InnerListenerManager::OnDeviceLockStateChanged(int32_t userId, int32_t lockState)
165 {
166     SCLOCK_HILOGI("OnDeviceLockStateChanged enter.");
167     OnStateChanged(userId, lockState, ListenType::DEVICE_LOCK);
168 }
169 
OnStateChanged(int32_t userId,int32_t state,ListenType listenType)170 void InnerListenerManager::OnStateChanged(int32_t userId, int32_t state, ListenType listenType)
171 {
172     std::lock_guard<std::recursive_mutex> lock(mutex_);
173     if (HasListenerSet(userId, listenType)) {
174         std::set<sptr<InnerListenerIf>> listenerSetTemp = innerListenMap_[listenType][userId];
175         SCLOCK_HILOGI("OnStateChanged, userId=%{public}d, listenType=%{public}d, length=%{public}d, State=%{public}d",
176             userId, static_cast<int>(listenType), static_cast<int>(listenerSetTemp.size()), state);
177         for (auto& iter : listenerSetTemp) {
178             if (iter != nullptr) {
179                 iter->OnStateChanged(userId, state);
180             }
181         }
182     }
183 
184     int32_t allUser = static_cast<int32_t>(SpecialUserId::USER_ALL);
185     if (HasListenerSet(allUser, listenType)) {
186         std::set<sptr<InnerListenerIf>> listenerSetTemp = innerListenMap_[listenType][allUser];
187         SCLOCK_HILOGI("OnStateChanged allUser, listenType=%{public}d, length=%{public}d, State=%{public}d",
188             static_cast<int>(listenType), static_cast<int>(listenerSetTemp.size()), state);
189         for (auto& iter : listenerSetTemp) {
190             if (iter != nullptr) {
191                 iter->OnStateChanged(userId, state);
192             }
193         }
194     }
195 }
196 
AddDeathRecipient(const ListenType listenType,const sptr<InnerListenerIf> & listener)197 int32_t InnerListenerManager::AddDeathRecipient(const ListenType listenType,
198                                                 const sptr<InnerListenerIf>& listener)
199 {
200     std::lock_guard<std::recursive_mutex> lock(mutex_);
201     if (listener == nullptr) {
202         SCLOCK_HILOGE("listener is nullptr");
203         return E_SCREENLOCK_NULLPTR;
204     }
205 
206     auto obj = listener->AsObject();
207     if (obj == nullptr) {
208         SCLOCK_HILOGE("remote object is nullptr");
209         return E_SCREENLOCK_NULLPTR;
210     }
211 
212     auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(), FinderMap(listener->AsObject()));
213     if (iter != deathRecipientMap_.end()) {
214         SCLOCK_HILOGE("deathRecipient is already registed");
215         return SUCCESS;
216     }
217 
218     sptr<DeathRecipient> dr(new (std::nothrow) InnerListenerDeathRecipient());
219     if ((dr == nullptr) || (!obj->AddDeathRecipient(dr))) {
220         SCLOCK_HILOGE("AddDeathRecipient failed");
221         return E_SCREENLOCK_NULLPTR;
222     }
223 
224     deathRecipientMap_.emplace(listener, std::make_pair(listenType, dr));
225     SCLOCK_HILOGI("AddDeathRecipient success length=%{public}d", static_cast<int>(deathRecipientMap_.size()));
226     return SUCCESS;
227 }
228 
RemoveDeathRecipient(const sptr<InnerListenerIf> & listener)229 int32_t InnerListenerManager::RemoveDeathRecipient(const sptr<InnerListenerIf>& listener)
230 {
231     std::lock_guard<std::recursive_mutex> lock(mutex_);
232     if (listener == nullptr) {
233         SCLOCK_HILOGE("listener is nullptr");
234         return E_SCREENLOCK_NULLPTR;
235     }
236 
237     auto obj = listener->AsObject();
238     if (obj == nullptr) {
239         SCLOCK_HILOGE("remote object is nullptr");
240         return E_SCREENLOCK_NULLPTR;
241     }
242 
243     auto iter = std::find_if(deathRecipientMap_.begin(), deathRecipientMap_.end(), FinderMap(listener->AsObject()));
244     if (iter == deathRecipientMap_.end()) {
245         SCLOCK_HILOGE("deathRecipient is not registed");
246         return SUCCESS;
247     }
248 
249     sptr<DeathRecipient> deathRecipient = iter->second.second;
250     if (deathRecipient == nullptr) {
251         SCLOCK_HILOGE("deathRecipient is nullptr");
252         return E_SCREENLOCK_NULLPTR;
253     }
254 
255     obj->RemoveDeathRecipient(deathRecipient);
256     deathRecipientMap_.erase(iter);
257     SCLOCK_HILOGE("RemoveDeathRecipient success length=%{public}d", static_cast<int>(deathRecipientMap_.size()));
258     return SUCCESS;
259 }
260 
GetDeathRecipient()261 std::map<sptr<InnerListenerIf>, std::pair<ListenType, sptr<DeathRecipient>>> InnerListenerManager::GetDeathRecipient()
262 {
263     std::lock_guard<std::recursive_mutex> lock(mutex_);
264     return deathRecipientMap_;
265 }
266 
OnRemoteDied(const wptr<IRemoteObject> & remote)267 void InnerListenerManager::InnerListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
268 {
269     SCLOCK_HILOGI("start");
270     if (remote == nullptr) {
271         SCLOCK_HILOGE("remote is nullptr");
272         return;
273     }
274 
275     std::map<sptr<InnerListenerIf>, std::pair<ListenType, sptr<DeathRecipient>>> deathRecipientMap =
276         InnerListenerManager::GetInstance()->GetDeathRecipient();
277     for (auto& iter : deathRecipientMap) {
278         if (iter.first != nullptr && remote == iter.first->AsObject()) {
279             SCLOCK_HILOGD("OnRemoteDied success");
280             auto result = InnerListenerManager::GetInstance()->UnRegisterInnerListener(iter.second.first, iter.first);
281             if (result != SUCCESS) {
282                 SCLOCK_HILOGE("UnRegisterInnerListener fail");
283                 return;
284             }
285         }
286     }
287 }
288 
289 }  // namespace ScreenLock
290 }  // namespace OHOS