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