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
26 namespace OHOS {
27 namespace ScreenLock {
28 std::mutex ScreenLockManager::instanceLock_;
29 sptr<ScreenLockManager> ScreenLockManager::instance_;
ScreenLockManager()30 ScreenLockManager::ScreenLockManager()
31 {
32 }
33
~ScreenLockManager()34 ScreenLockManager::~ScreenLockManager()
35 {
36 SCLOCK_HILOGW("~ScreenLockManager");
37 RemoveDeathRecipient();
38 }
39
GetInstance()40 sptr<ScreenLockManager> ScreenLockManager::GetInstance()
41 {
42 if (instance_ == nullptr) {
43 std::lock_guard<std::mutex> autoLock(instanceLock_);
44 if (instance_ == nullptr) {
45 instance_ = new ScreenLockManager;
46 }
47 }
48 return instance_;
49 }
50
IsLocked(bool & isLocked)51 int32_t ScreenLockManager::IsLocked(bool &isLocked)
52 {
53 auto proxy = GetProxy();
54 if (proxy == nullptr) {
55 SCLOCK_HILOGE("IsLocked quit because GetScreenLockManagerProxy failed.");
56 return E_SCREENLOCK_SENDREQUEST_FAILED;
57 }
58 return proxy->IsLocked(isLocked);
59 }
60
IsScreenLocked()61 bool ScreenLockManager::IsScreenLocked()
62 {
63 auto proxy = GetProxy();
64 if (proxy == nullptr) {
65 SCLOCK_HILOGE("IsScreenLocked quit because GetScreenLockManagerProxy failed.");
66 return false;
67 }
68 return proxy->IsScreenLocked();
69 }
70
GetSecure()71 bool ScreenLockManager::GetSecure()
72 {
73 auto proxy = GetProxy();
74 if (proxy == nullptr) {
75 SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed.");
76 return false;
77 }
78 return proxy->GetSecure();
79 }
80
Unlock(Action action,const sptr<ScreenLockCallbackInterface> & listener)81 int32_t ScreenLockManager::Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener)
82 {
83 auto proxy = GetProxy();
84 if (proxy == nullptr) {
85 SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed.");
86 return E_SCREENLOCK_NULLPTR;
87 }
88 if (listener == nullptr) {
89 SCLOCK_HILOGE("listener is nullptr.");
90 return E_SCREENLOCK_NULLPTR;
91 }
92 StartAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock start", HITRACE_UNLOCKSCREEN);
93 int32_t ret = 0;
94 if (action == Action::UNLOCKSCREEN) {
95 ret = proxy->UnlockScreen(listener);
96 } else {
97 ret = proxy->Unlock(listener);
98 }
99 FinishAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager Unlock end", HITRACE_UNLOCKSCREEN);
100 return ret;
101 }
102
103
Lock(const sptr<ScreenLockCallbackInterface> & listener)104 int32_t ScreenLockManager::Lock(const sptr<ScreenLockCallbackInterface> &listener)
105 {
106 auto proxy = GetProxy();
107 if (proxy == nullptr) {
108 SCLOCK_HILOGE("RequestLock quit because redoing GetScreenLockManagerProxy failed.");
109 return E_SCREENLOCK_NULLPTR;
110 }
111 if (listener == nullptr) {
112 SCLOCK_HILOGE("listener is nullptr.");
113 return E_SCREENLOCK_NULLPTR;
114 }
115 SCLOCK_HILOGD("ScreenLockManager RequestLock succeeded.");
116 return proxy->Lock(listener);
117 }
118
Lock(int32_t userId)119 int32_t ScreenLockManager::Lock(int32_t userId)
120 {
121 auto proxy = GetProxy();
122 if (proxy == nullptr) {
123 SCLOCK_HILOGE("GetProxy failed.");
124 return E_SCREENLOCK_NULLPTR;
125 }
126 return proxy->Lock(userId);
127 }
128
RequestStrongAuth(int reasonFlag,int32_t userId)129 int32_t ScreenLockManager::RequestStrongAuth(int reasonFlag, int32_t userId)
130 {
131 SCLOCK_HILOGI("ScreenLockManager::RequestStrongAuth, reasonFlag=%{public}d, userId=%{public}d", reasonFlag, userId);
132 auto proxy = GetProxy();
133 if (proxy == nullptr) {
134 SCLOCK_HILOGE("RequestStrongAuth quit because GetProxy failed.");
135 return E_SCREENLOCK_NULLPTR;
136 }
137 return proxy->RequestStrongAuth(reasonFlag, userId);
138 }
139
GetScreenLockManagerProxy()140 sptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy()
141 {
142 sptr<ISystemAbilityManager> systemAbilityManager =
143 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
144 if (systemAbilityManager == nullptr) {
145 SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
146 return nullptr;
147 }
148 auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
149 if (systemAbility == nullptr) {
150 SCLOCK_HILOGE("Get SystemAbility failed.");
151 return nullptr;
152 }
153 deathRecipient_ = new ScreenLockSaDeathRecipient();
154 systemAbility->AddDeathRecipient(deathRecipient_);
155 sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
156 if (screenlockServiceProxy == nullptr) {
157 SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
158 return nullptr;
159 }
160 SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
161 return screenlockServiceProxy;
162 }
163
OnRemoteSaDied(const wptr<IRemoteObject> & remote)164 void ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
165 {
166 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
167 screenlockManagerProxy_ = GetScreenLockManagerProxy();
168 }
169
GetProxy()170 sptr<ScreenLockManagerInterface> ScreenLockManager::GetProxy()
171 {
172 if (screenlockManagerProxy_ != nullptr) {
173 return screenlockManagerProxy_;
174 }
175 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
176 if (screenlockManagerProxy_ == nullptr) {
177 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
178 screenlockManagerProxy_ = GetScreenLockManagerProxy();
179 }
180 return screenlockManagerProxy_;
181 }
182
RemoveDeathRecipient()183 void ScreenLockManager::RemoveDeathRecipient()
184 {
185 if (screenlockManagerProxy_ != nullptr && deathRecipient_ != nullptr) {
186 screenlockManagerProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
187 }
188 }
189 } // namespace ScreenLock
190 } // namespace OHOS
191