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
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 "scene_board_judgement.h"
26 #include "session_manager.h"
27
28 namespace OHOS {
29 namespace ScreenLock {
30 std::mutex ScreenLockManager::instanceLock_;
31 sptr<ScreenLockManager> ScreenLockManager::instance_;
32 sptr<ScreenLockSaDeathRecipient> ScreenLockManager::deathRecipient_;
33
ScreenLockManager()34 ScreenLockManager::ScreenLockManager()
35 {
36 }
37
~ScreenLockManager()38 ScreenLockManager::~ScreenLockManager()
39 {
40 }
41
GetInstance()42 sptr<ScreenLockManager> ScreenLockManager::GetInstance()
43 {
44 if (instance_ == nullptr) {
45 std::lock_guard<std::mutex> autoLock(instanceLock_);
46 if (instance_ == nullptr) {
47 instance_ = new ScreenLockManager;
48 std::lock_guard<std::mutex> guard(instance_->managerProxyLock_);
49 instance_->screenlockManagerProxy_ = GetScreenLockManagerProxy();
50 }
51 }
52 return instance_;
53 }
54
IsLocked(bool & isLocked)55 int32_t ScreenLockManager::IsLocked(bool &isLocked)
56 {
57 auto proxy = GetProxy();
58 if (proxy == nullptr) {
59 SCLOCK_HILOGE("IsLocked quit because GetScreenLockManagerProxy failed.");
60 return E_SCREENLOCK_SENDREQUEST_FAILED;
61 }
62 return proxy->IsLocked(isLocked);
63 }
64
IsScreenLocked()65 bool ScreenLockManager::IsScreenLocked()
66 {
67 auto proxy = GetProxy();
68 if (proxy == nullptr) {
69 SCLOCK_HILOGE("IsScreenLocked quit because GetScreenLockManagerProxy failed.");
70 return false;
71 }
72 return proxy->IsScreenLocked();
73 }
74
GetSecure()75 bool ScreenLockManager::GetSecure()
76 {
77 auto proxy = GetProxy();
78 if (proxy == nullptr) {
79 SCLOCK_HILOGE("GetSecure quit because redoing GetScreenLockManagerProxy failed.");
80 return false;
81 }
82 return proxy->GetSecure();
83 }
84
Unlock(Action action,const sptr<ScreenLockCallbackInterface> & listener)85 int32_t ScreenLockManager::Unlock(Action action, const sptr<ScreenLockCallbackInterface> &listener)
86 {
87 auto proxy = GetProxy();
88 if (proxy == nullptr) {
89 SCLOCK_HILOGE("RequestUnlock quit because redoing GetScreenLockManagerProxy failed.");
90 return E_SCREENLOCK_NULLPTR;
91 }
92 if (listener == nullptr) {
93 SCLOCK_HILOGE("listener is nullptr.");
94 return E_SCREENLOCK_NULLPTR;
95 }
96 StartAsyncTrace(HITRACE_TAG_MISC, "ScreenLockManager RequestUnlock start", HITRACE_UNLOCKSCREEN);
97 if (action == Action::UNLOCKSCREEN) {
98 return proxy->UnlockScreen(listener);
99 }
100 return proxy->Unlock(listener);
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
GetScreenLockManagerProxy()119 sptr<ScreenLockManagerInterface> ScreenLockManager::GetScreenLockManagerProxy()
120 {
121 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
122 return Rosen::SessionManager::GetInstance().GetScreenLockManagerProxy();
123 }
124 sptr<ISystemAbilityManager> systemAbilityManager =
125 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
126 if (systemAbilityManager == nullptr) {
127 SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
128 return nullptr;
129 }
130 auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
131 if (systemAbility == nullptr) {
132 SCLOCK_HILOGE("Get SystemAbility failed.");
133 return nullptr;
134 }
135 deathRecipient_ = new ScreenLockSaDeathRecipient();
136 systemAbility->AddDeathRecipient(deathRecipient_);
137 sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
138 if (screenlockServiceProxy == nullptr) {
139 SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
140 return nullptr;
141 }
142 SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
143 return screenlockServiceProxy;
144 }
145
OnRemoteSaDied(const wptr<IRemoteObject> & remote)146 void ScreenLockManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
147 {
148 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
149 screenlockManagerProxy_ = GetScreenLockManagerProxy();
150 }
151
GetProxy()152 sptr<ScreenLockManagerInterface> ScreenLockManager::GetProxy()
153 {
154 if (screenlockManagerProxy_ != nullptr) {
155 return screenlockManagerProxy_;
156 }
157 std::lock_guard<std::mutex> autoLock(managerProxyLock_);
158 if (screenlockManagerProxy_ == nullptr) {
159 SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
160 screenlockManagerProxy_ = GetScreenLockManagerProxy();
161 }
162 return screenlockManagerProxy_;
163 }
164
ScreenLockSaDeathRecipient()165 ScreenLockSaDeathRecipient::ScreenLockSaDeathRecipient()
166 {
167 }
168
~ScreenLockSaDeathRecipient()169 ScreenLockSaDeathRecipient::~ScreenLockSaDeathRecipient()
170 {
171 }
172
OnRemoteDied(const wptr<IRemoteObject> & object)173 void ScreenLockSaDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
174 {
175 SCLOCK_HILOGE("ScreenLockSaDeathRecipient on remote systemAbility died.");
176 ScreenLockManager::GetInstance()->OnRemoteSaDied(object);
177 }
178 } // namespace ScreenLock
179 } // namespace OHOS
180