• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #include "screenlock_manager.h"
17 #include "screenlock_manager_proxy.h"
18 #include <hitrace_meter.h>
19 
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "sclock_log.h"
23 #include "screenlock_common.h"
24 #include "system_ability_definition.h"
25 #include "screenlock_inner_listener.h"
26 
27 namespace OHOS {
28 namespace ScreenLock {
29 std::mutex ScreenLockManager::instanceLock_;
30 sptr<ScreenLockManager> ScreenLockManager::instance_;
31 std::mutex ScreenLockManager::listenerLock_;
32 sptr<ScreenLockSystemAbilityInterface> ScreenLockManager::systemEventListener_;
ScreenLockManager()33 ScreenLockManager::ScreenLockManager()
34 {
35 }
36 
~ScreenLockManager()37 ScreenLockManager::~ScreenLockManager()
38 {
39     SCLOCK_HILOGW("~ScreenLockManager");
40     RemoveDeathRecipient();
41 }
42 
GetInstance()43 sptr<ScreenLockManager> ScreenLockManager::GetInstance()
44 {
45     if (instance_ == nullptr) {
46         std::lock_guard<std::mutex> autoLock(instanceLock_);
47         if (instance_ == nullptr) {
48             instance_ = new ScreenLockManager;
49         }
50     }
51     return instance_;
52 }
53 
IsLocked(bool & isLocked)54 int32_t ScreenLockManager::IsLocked(bool &isLocked)
55 {
56     auto proxy = GetProxy();
57     if (proxy == nullptr) {
58         SCLOCK_HILOGE("IsLocked quit because GetScreenLockManagerProxy failed.");
59         return E_SCREENLOCK_SENDREQUEST_FAILED;
60     }
61     return proxy->IsLocked(isLocked);
62 }
63 
IsScreenLocked()64 bool ScreenLockManager::IsScreenLocked()
65 {
66     auto proxy = GetProxy();
67     if (proxy == nullptr) {
68         SCLOCK_HILOGE("IsScreenLocked quit because GetScreenLockManagerProxy failed.");
69         return false;
70     }
71     return proxy->IsScreenLocked();
72 }
73 
IsLockedWithUserId(int userId,bool & isLocked)74 int32_t ScreenLockManager::IsLockedWithUserId(int userId, bool &isLocked)
75 {
76     SCLOCK_HILOGD("ScreenLockManager::IsLockedWithUserId in");
77     auto proxy = GetProxy();
78     if (proxy == nullptr) {
79         SCLOCK_HILOGE("ScreenLockManager::IsLockedWithUserId quit because redoing GetProxy failed.");
80         return E_SCREENLOCK_NULLPTR;
81     }
82     int32_t status = proxy->IsLockedWithUserId(userId, isLocked);
83     SCLOCK_HILOGD("ScreenLockManager::IsLockedWithUserId out, status=%{public}d", status);
84     return status;
85 }
86 
GetSecure()87 bool ScreenLockManager::GetSecure()
88 {
89     auto proxy = GetProxy();
90     if (proxy == nullptr) {
91         SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed.");
92         return false;
93     }
94     return proxy->GetSecure();
95 }
96 
Unlock(Action action,const sptr<ScreenLockCallbackInterface> & listener)97 int32_t ScreenLockManager::Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener)
98 {
99     auto proxy = GetProxy();
100     if (proxy == nullptr) {
101         SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed.");
102         return E_SCREENLOCK_NULLPTR;
103     }
104     if (listener == nullptr) {
105         SCLOCK_HILOGE("listener is nullptr.");
106         return E_SCREENLOCK_NULLPTR;
107     }
108     StartAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock start", HITRACE_UNLOCKSCREEN);
109     int32_t ret = 0;
110     if (action == Action::UNLOCKSCREEN) {
111         ret = proxy->UnlockScreen(listener);
112     } else {
113         ret = proxy->Unlock(listener);
114     }
115     FinishAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock end", HITRACE_UNLOCKSCREEN);
116     return ret;
117 }
118 
119 
Lock(const sptr<ScreenLockCallbackInterface> & listener)120 int32_t ScreenLockManager::Lock(const sptr<ScreenLockCallbackInterface> &listener)
121 {
122     auto proxy = GetProxy();
123     if (proxy == nullptr) {
124         SCLOCK_HILOGE("RequestLock quit because redoing GetScreenLockManagerProxy failed.");
125         return E_SCREENLOCK_NULLPTR;
126     }
127     if (listener == nullptr) {
128         SCLOCK_HILOGE("listener is nullptr.");
129         return E_SCREENLOCK_NULLPTR;
130     }
131     SCLOCK_HILOGD("ScreenLockManager RequestLock succeeded.");
132     return proxy->Lock(listener);
133 }
134 
Lock(int32_t userId)135 int32_t ScreenLockManager::Lock(int32_t userId)
136 {
137     auto proxy = GetProxy();
138     if (proxy == nullptr) {
139         SCLOCK_HILOGE("GetProxy failed.");
140         return E_SCREENLOCK_NULLPTR;
141     }
142     return proxy->Lock(userId);
143 }
144 
SendScreenLockEvent(const std::string & event,int param)145 int32_t ScreenLockManager::SendScreenLockEvent(const std::string &event, int param)
146 {
147     auto proxy = GetProxy();
148     if (proxy == nullptr) {
149         SCLOCK_HILOGE("ScreenLockManager::SendScreenLockEvent quit because redoing GetProxy failed.");
150         return E_SCREENLOCK_NULLPTR;
151     }
152     int ret = proxy->SendScreenLockEvent(event, param);
153     SCLOCK_HILOGD("SendScreenLockEvent result = %{public}d", ret);
154     return ret;
155 }
156 
IsScreenLockDisabled(int userId,bool & isDisabled)157 int32_t ScreenLockManager::IsScreenLockDisabled(int userId, bool &isDisabled)
158 {
159     SCLOCK_HILOGD("ScreenLockManager::IsScreenLockDisabled in");
160     auto proxy = GetProxy();
161     if (proxy == nullptr) {
162         SCLOCK_HILOGE("ScreenLockManager::IsScreenLockDisabled quit because redoing GetProxy failed.");
163         return E_SCREENLOCK_NULLPTR;
164     }
165     int32_t status = proxy->IsScreenLockDisabled(userId, isDisabled);
166     SCLOCK_HILOGD("ScreenLockManager::IsScreenLockDisabled out, status=%{public}d", status);
167     return status;
168 }
169 
SetScreenLockDisabled(bool disable,int userId)170 int32_t ScreenLockManager::SetScreenLockDisabled(bool disable, int userId)
171 {
172     SCLOCK_HILOGD("ScreenLockManager::SetScreenLockDisabled in");
173     auto proxy = GetProxy();
174     if (proxy == nullptr) {
175         SCLOCK_HILOGE("ScreenLockManager::SetScreenLockDisabled quit because redoing GetProxy failed.");
176         return E_SCREENLOCK_NULLPTR;
177     }
178     int32_t status = proxy->SetScreenLockDisabled(disable, userId);
179     SCLOCK_HILOGD("ScreenLockManager::SetScreenLockDisabled out, status=%{public}d", status);
180     return status;
181 }
182 
SetScreenLockAuthState(int authState,int32_t userId,std::string & authToken)183 int32_t ScreenLockManager::SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken)
184 {
185     SCLOCK_HILOGD("ScreenLockManager::SetScreenLockAuthState in");
186     auto proxy = GetProxy();
187     if (proxy == nullptr) {
188         SCLOCK_HILOGE("ScreenLockManager::SetScreenLockAuthState quit because redoing GetProxy failed.");
189         return E_SCREENLOCK_NULLPTR;
190     }
191     int32_t status = proxy->SetScreenLockAuthState(authState, userId, authToken);
192     SCLOCK_HILOGD("ScreenLockManager::SetScreenLockAuthState out, status=%{public}d", status);
193     return status;
194 }
195 
GetScreenLockAuthState(int userId,int32_t & authState)196 int32_t ScreenLockManager::GetScreenLockAuthState(int userId, int32_t &authState)
197 {
198     SCLOCK_HILOGD("ScreenLockManager::GetScreenLockAuthState in");
199     auto proxy = GetProxy();
200     if (proxy == nullptr) {
201         SCLOCK_HILOGE("ScreenLockManager::GetScreenLockAuthState quit because redoing GetProxy failed.");
202         return E_SCREENLOCK_NULLPTR;
203     }
204     int32_t status = proxy->GetScreenLockAuthState(userId, authState);
205     SCLOCK_HILOGD("ScreenLockManager::GetScreenLockAuthState out, status=%{public}d", status);
206     return status;
207 }
208 
RequestStrongAuth(int reasonFlag,int32_t userId)209 int32_t ScreenLockManager::RequestStrongAuth(int reasonFlag, int32_t userId)
210 {
211     SCLOCK_HILOGD("ScreenLockManager::RequestStrongAuth in");
212     auto proxy = GetProxy();
213     if (proxy == nullptr) {
214         SCLOCK_HILOGE("ScreenLockManager::RequestStrongAuth quit because redoing GetProxy failed.");
215         return E_SCREENLOCK_NULLPTR;
216     }
217     int32_t status = proxy->RequestStrongAuth(reasonFlag, userId);
218     SCLOCK_HILOGD("ScreenLockManager::RequestStrongAuth out, status=%{public}d", status);
219     return status;
220     return 0;
221 }
222 
GetStrongAuth(int userId,int32_t & reasonFlag)223 int32_t ScreenLockManager::GetStrongAuth(int userId, int32_t &reasonFlag)
224 {
225     SCLOCK_HILOGD("ScreenLockManager::GetStrongAuth in");
226     auto proxy = GetProxy();
227     if (proxy == nullptr) {
228         SCLOCK_HILOGE("ScreenLockManager::GetStrongAuth quit because redoing GetProxy failed.");
229         return E_SCREENLOCK_NULLPTR;
230     }
231     int32_t status = proxy->GetStrongAuth(userId, reasonFlag);
232     SCLOCK_HILOGD("ScreenLockManager::GetStrongAuth out, status=%{public}d", status);
233     return status;
234 }
235 
IsDeviceLocked(int userId,bool & isDeviceLocked)236 int32_t ScreenLockManager::IsDeviceLocked(int userId, bool &isDeviceLocked)
237 {
238     SCLOCK_HILOGD("ScreenLockManager::IsDeviceLocked in");
239     auto proxy = GetProxy();
240     if (proxy == nullptr) {
241         SCLOCK_HILOGE("ScreenLockManager::IsDeviceLocked quit because redoing GetProxy failed.");
242         return E_SCREENLOCK_NULLPTR;
243     }
244     int32_t status = proxy->IsDeviceLocked(userId, isDeviceLocked);
245     SCLOCK_HILOGD("ScreenLockManager::IsDeviceLocked out, status=%{public}d", status);
246     return status;
247 }
248 
RegisterStrongAuthListener(const sptr<StrongAuthListener> & listener)249 int32_t ScreenLockManager::RegisterStrongAuthListener(const sptr<StrongAuthListener> &listener)
250 {
251     SCLOCK_HILOGD("RegisterStrongAuthListener in");
252     auto proxy = GetProxy();
253     if (proxy == nullptr) {
254         SCLOCK_HILOGE("RegisterStrongAuthListener quit because redoing GetProxy failed.");
255         return E_SCREENLOCK_NULLPTR;
256     }
257 
258     if (listener == nullptr) {
259         SCLOCK_HILOGE("RegisterStrongAuthListener quit because listener is null.");
260         return E_SCREENLOCK_NULLPTR;
261     }
262 
263     return RegisterListenerInner(ListenType::STRONG_AUTH, listener);
264 }
265 
UnRegisterStrongAuthListener(const sptr<StrongAuthListener> & listener)266 int32_t ScreenLockManager::UnRegisterStrongAuthListener(const sptr<StrongAuthListener> &listener)
267 {
268     SCLOCK_HILOGD("UnRegisterInnerListener in");
269     auto proxy = GetProxy();
270     if (proxy == nullptr) {
271         SCLOCK_HILOGE("UnRegisterInnerListener quit because redoing GetProxy failed.");
272         return E_SCREENLOCK_NULLPTR;
273     }
274 
275     if (listener == nullptr) {
276         SCLOCK_HILOGE("RegisterStrongAuthListener quit because listener is null.");
277         return E_SCREENLOCK_NULLPTR;
278     }
279 
280     return UnRegisterListenerInner(ListenType::STRONG_AUTH, listener);
281 }
282 
RegisterDeviceLockedListener(const sptr<DeviceLockedListener> & listener)283 int32_t ScreenLockManager::RegisterDeviceLockedListener(const sptr<DeviceLockedListener> &listener)
284 {
285     SCLOCK_HILOGD("RegisterDeviceLockedListener in");
286 
287     if (listener == nullptr) {
288         SCLOCK_HILOGE("RegisterDeviceLockedListener quit because listener is null.");
289         return E_SCREENLOCK_NULLPTR;
290     }
291 
292     return RegisterListenerInner(ListenType::DEVICE_LOCK, listener);
293 }
UnRegisterDeviceLockedListener(const sptr<DeviceLockedListener> & listener)294 int32_t ScreenLockManager::UnRegisterDeviceLockedListener(const sptr<DeviceLockedListener> &listener)
295 {
296     SCLOCK_HILOGD("UnRegisterDeviceLockedListener in");
297 
298     if (listener == nullptr) {
299         SCLOCK_HILOGE("UnRegisterDeviceLockedListener quit because listener is null.");
300         return E_SCREENLOCK_NULLPTR;
301     }
302 
303     return UnRegisterListenerInner(ListenType::DEVICE_LOCK, listener);
304 }
305 
RegisterListenerInner(const ListenType listenType,const sptr<InnerListener> & listener)306 int32_t ScreenLockManager::RegisterListenerInner(const ListenType listenType, const sptr<InnerListener>& listener)
307 {
308     auto proxy = GetProxy();
309     if (proxy == nullptr) {
310         SCLOCK_HILOGE("RegisterDeviceLockedListener quit because redoing GetProxy failed.");
311         return E_SCREENLOCK_NULLPTR;
312     }
313 
314     sptr<InnerListenerWrapper> wrapper = new (std::nothrow) InnerListenerWrapper(listener);
315     if (wrapper == nullptr) {
316         SCLOCK_HILOGE("Failed to create InnerListenerWrapper.");
317         return E_SCREENLOCK_NULLPTR;
318     }
319 
320     std::lock_guard<std::mutex> lock(ListenerWrapperMapMutex);
321     // 检查是否已经存在对应的Wrapper
322     if (InnerListenerWrapperMap.find(listener) != InnerListenerWrapperMap.end()) {
323         SCLOCK_HILOGW("Wrapper already exists for this listener.");
324         return E_SCREENLOCK_NULLPTR;
325     }
326 
327     InnerListenerWrapperMap[listener] = wrapper;
328     int32_t userId = listener->GetUserId();
329     int32_t status = proxy->RegisterInnerListener(userId, listenType, wrapper);
330     SCLOCK_HILOGI("RegisterInnerListener out, listenType=%{public}d, status=%{public}d", listenType, status);
331     return status;
332 }
333 
UnRegisterListenerInner(const ListenType listenType,const sptr<InnerListener> & listener)334 int32_t ScreenLockManager::UnRegisterListenerInner(const ListenType listenType,
335                                                    const sptr<InnerListener>& listener)
336 {
337     auto proxy = GetProxy();
338     if (proxy == nullptr) {
339         SCLOCK_HILOGE("UnRegisterDeviceLockedListener quit because redoing GetProxy failed.");
340         return E_SCREENLOCK_NULLPTR;
341     }
342 
343     std::lock_guard<std::mutex> lock(ListenerWrapperMapMutex);
344     auto it = InnerListenerWrapperMap.find(listener);
345     if (it == InnerListenerWrapperMap.end()) {
346         SCLOCK_HILOGW("No wrapper found for this listener.");
347         return E_SCREENLOCK_NULLPTR;
348     }
349     sptr<InnerListenerWrapper> wrapper = it->second;
350     int32_t userId = listener->GetUserId();
351     int32_t status = proxy->UnRegisterInnerListener(userId, listenType, wrapper);
352     SCLOCK_HILOGI("UnRegisterInnerListener out, listenType=%{public}d, status=%{public}d", listenType, status);
353     // 移除Wrapper对象
354     InnerListenerWrapperMap.erase(it);
355     return status;
356 }
357 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)358 int32_t ScreenLockManager::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
359 {
360     SCLOCK_HILOGD("ScreenLockManager::OnSystemEvent in");
361     auto proxy = GetProxy();
362     if (proxy == nullptr) {
363         SCLOCK_HILOGE("ScreenLockManager::OnSystemEvent quit because redoing GetScreenLockManagerProxy failed.");
364         return E_SCREENLOCK_NULLPTR;
365     }
366     if (listener == nullptr) {
367         SCLOCK_HILOGE("listener is nullptr.");
368         return E_SCREENLOCK_NULLPTR;
369     }
370     listenerLock_.lock();
371     systemEventListener_ = listener;
372     listenerLock_.unlock();
373     int32_t status = proxy->OnSystemEvent(listener);
374     SCLOCK_HILOGD("ScreenLockManager::OnSystemEvent out, status=%{public}d", status);
375     return status;
376 }
377 
GetScreenLockManagerProxy()378 sptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy()
379 {
380     sptr<ISystemAbilityManager> systemAbilityManager =
381         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
382     if (systemAbilityManager == nullptr) {
383         SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
384         return nullptr;
385     }
386     auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
387     if (systemAbility == nullptr) {
388         SCLOCK_HILOGE("Get SystemAbility failed.");
389         return nullptr;
390     }
391     deathRecipient_ = new ScreenLockSaDeathRecipient();
392     systemAbility->AddDeathRecipient(deathRecipient_);
393     sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
394     if (screenlockServiceProxy == nullptr) {
395         SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
396         return nullptr;
397     }
398     SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
399     return screenlockServiceProxy;
400 }
401 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)402 void ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
403 {
404     SCLOCK_HILOGE("ScreenLockDeathRecipient on remote systemAbility died.");
405     std::lock_guard<std::mutex> autoLock(managerProxyLock_);
406     screenlockManagerProxy_ = GetScreenLockManagerProxy();
407     if (systemEventListener_ != nullptr) {
408         SystemEvent systemEvent(SERVICE_RESTART);
409         systemEventListener_->OnCallBack(systemEvent);
410     }
411 }
412 
GetProxy()413 sptr<ScreenLockManagerInterface> ScreenLockManager::GetProxy()
414 {
415     std::lock_guard<std::mutex> autoLock(managerProxyLock_);
416     if (screenlockManagerProxy_ == nullptr) {
417         SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
418         screenlockManagerProxy_ = GetScreenLockManagerProxy();
419     }
420     return screenlockManagerProxy_;
421 }
422 
RemoveDeathRecipient()423 void ScreenLockManager::RemoveDeathRecipient()
424 {
425     if (screenlockManagerProxy_ != nullptr && deathRecipient_ != nullptr) {
426         screenlockManagerProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
427     }
428 }
429 } // namespace ScreenLock
430 } // namespace OHOS
431