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