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_app_manager.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "sclock_log.h"
21 #include "screenlock_common.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace ScreenLock {
26 std::mutex ScreenLockAppManager::instanceLock_;
27 sptr<ScreenLockAppManager> ScreenLockAppManager::instance_;
28 sptr<ScreenLockAppDeathRecipient> ScreenLockAppManager::deathRecipient_;
29 std::mutex ScreenLockAppManager::listenerLock_;
30 sptr<ScreenLockSystemAbilityInterface> ScreenLockAppManager::systemEventListener_;
31 
ScreenLockAppManager()32 ScreenLockAppManager::ScreenLockAppManager()
33 {
34 }
35 
~ScreenLockAppManager()36 ScreenLockAppManager::~ScreenLockAppManager()
37 {
38 }
39 
GetInstance()40 sptr<ScreenLockAppManager> ScreenLockAppManager::GetInstance()
41 {
42     if (instance_ == nullptr) {
43         std::lock_guard<std::mutex> autoLock(instanceLock_);
44         if (instance_ == nullptr) {
45             instance_ = new ScreenLockAppManager;
46             std::lock_guard<std::mutex> guard(instance_->managerProxyLock_);
47             instance_->screenlockManagerProxy_ = GetScreenLockManagerProxy();
48         }
49     }
50     return instance_;
51 }
52 
SendScreenLockEvent(const std::string & event,int param)53 int32_t ScreenLockAppManager::SendScreenLockEvent(const std::string &event, int param)
54 {
55     auto proxy = GetProxy();
56     if (proxy == nullptr) {
57         SCLOCK_HILOGE("ScreenLockAppManager::SendScreenLockEvent quit because redoing GetProxy failed.");
58         return E_SCREENLOCK_NULLPTR;
59     }
60     int ret = proxy->SendScreenLockEvent(event, param);
61     SCLOCK_HILOGD("SendScreenLockEvent result = %{public}d", ret);
62     return ret;
63 }
64 
IsScreenLockDisabled(int userId,bool & isDisabled)65 int32_t ScreenLockAppManager::IsScreenLockDisabled(int userId, bool &isDisabled)
66 {
67     SCLOCK_HILOGD("ScreenLockAppManager::IsScreenLockDisabled in");
68     auto proxy = GetProxy();
69     if (proxy == nullptr) {
70         SCLOCK_HILOGE("ScreenLockAppManager::IsScreenLockDisabled quit because redoing GetProxy failed.");
71         return E_SCREENLOCK_NULLPTR;
72     }
73     int32_t status = proxy->IsScreenLockDisabled(userId, isDisabled);
74     SCLOCK_HILOGD("ScreenLockAppManager::IsScreenLockDisabled out, status=%{public}d", status);
75     return status;
76 }
77 
SetScreenLockDisabled(bool disable,int userId)78 int32_t ScreenLockAppManager::SetScreenLockDisabled(bool disable, int userId)
79 {
80     SCLOCK_HILOGD("ScreenLockAppManager::SetScreenLockDisabled in");
81     auto proxy = GetProxy();
82     if (proxy == nullptr) {
83         SCLOCK_HILOGE("ScreenLockAppManager::SetScreenLockDisabled quit because redoing GetProxy failed.");
84         return E_SCREENLOCK_NULLPTR;
85     }
86     int32_t status = proxy->SetScreenLockDisabled(disable, userId);
87     SCLOCK_HILOGD("ScreenLockAppManager::SetScreenLockDisabled out, status=%{public}d", status);
88     return status;
89 }
90 
SetScreenLockAuthState(int authState,int32_t userId,std::string & authToken)91 int32_t ScreenLockAppManager::SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken)
92 {
93     SCLOCK_HILOGD("ScreenLockAppManager::SetScreenLockAuthState in");
94     auto proxy = GetProxy();
95     if (proxy == nullptr) {
96         SCLOCK_HILOGE("ScreenLockAppManager::SetScreenLockAuthState quit because redoing GetProxy failed.");
97         return E_SCREENLOCK_NULLPTR;
98     }
99     int32_t status = proxy->SetScreenLockAuthState(authState, userId, authToken);
100     SCLOCK_HILOGD("ScreenLockAppManager::SetScreenLockAuthState out, status=%{public}d", status);
101     return status;
102 }
103 
GetScreenLockAuthState(int userId,int32_t & authState)104 int32_t ScreenLockAppManager::GetScreenLockAuthState(int userId, int32_t &authState)
105 {
106     SCLOCK_HILOGD("ScreenLockAppManager::GetScreenLockAuthState in");
107     auto proxy = GetProxy();
108     if (proxy == nullptr) {
109         SCLOCK_HILOGE("ScreenLockAppManager::GetScreenLockAuthState quit because redoing GetProxy failed.");
110         return E_SCREENLOCK_NULLPTR;
111     }
112     int32_t status = proxy->GetScreenLockAuthState(userId, authState);
113     SCLOCK_HILOGD("ScreenLockAppManager::GetScreenLockAuthState out, status=%{public}d", status);
114     return status;
115 }
116 
RequestStrongAuth(int reasonFlag,int32_t userId)117 int32_t ScreenLockAppManager::RequestStrongAuth(int reasonFlag, int32_t userId)
118 {
119     SCLOCK_HILOGD("ScreenLockAppManager::RequestStrongAuth in");
120     auto proxy = GetProxy();
121     if (proxy == nullptr) {
122         SCLOCK_HILOGE("ScreenLockAppManager::RequestStrongAuth quit because redoing GetProxy failed.");
123         return E_SCREENLOCK_NULLPTR;
124     }
125     int32_t status = proxy->RequestStrongAuth(reasonFlag, userId);
126     SCLOCK_HILOGD("ScreenLockAppManager::RequestStrongAuth out, status=%{public}d", status);
127     return status;
128     return 0;
129 }
130 
GetStrongAuth(int userId,int32_t & reasonFlag)131 int32_t ScreenLockAppManager::GetStrongAuth(int userId, int32_t &reasonFlag)
132 {
133     SCLOCK_HILOGD("ScreenLockAppManager::GetStrongAuth in");
134     auto proxy = GetProxy();
135     if (proxy == nullptr) {
136         SCLOCK_HILOGE("ScreenLockAppManager::GetStrongAuth quit because redoing GetProxy failed.");
137         return E_SCREENLOCK_NULLPTR;
138     }
139     int32_t status = proxy->GetStrongAuth(userId, reasonFlag);
140     SCLOCK_HILOGD("ScreenLockAppManager::GetStrongAuth out, status=%{public}d", status);
141     return status;
142 }
143 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)144 int32_t ScreenLockAppManager::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
145 {
146     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent in");
147     auto proxy = GetProxy();
148     if (proxy == nullptr) {
149         SCLOCK_HILOGE("ScreenLockAppManager::OnSystemEvent quit because redoing GetScreenLockManagerProxy failed.");
150         return E_SCREENLOCK_NULLPTR;
151     }
152     if (listener == nullptr) {
153         SCLOCK_HILOGE("listener is nullptr.");
154         return E_SCREENLOCK_NULLPTR;
155     }
156     listenerLock_.lock();
157     systemEventListener_ = listener;
158     listenerLock_.unlock();
159     int32_t status = proxy->OnSystemEvent(listener);
160     SCLOCK_HILOGD("ScreenLockAppManager::OnSystemEvent out, status=%{public}d", status);
161     return status;
162 }
163 
GetScreenLockManagerProxy()164 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetScreenLockManagerProxy()
165 {
166     sptr<ISystemAbilityManager> systemAbilityManager =
167         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
168     if (systemAbilityManager == nullptr) {
169         SCLOCK_HILOGE("Getting SystemAbilityManager failed.");
170         return nullptr;
171     }
172     auto systemAbility = systemAbilityManager->GetSystemAbility(SCREENLOCK_SERVICE_ID, "");
173     if (systemAbility == nullptr) {
174         SCLOCK_HILOGE("Get SystemAbility failed.");
175         return nullptr;
176     }
177     deathRecipient_ = new ScreenLockAppDeathRecipient();
178     systemAbility->AddDeathRecipient(deathRecipient_);
179     sptr<ScreenLockManagerInterface> screenlockServiceProxy = iface_cast<ScreenLockManagerInterface>(systemAbility);
180     if (screenlockServiceProxy == nullptr) {
181         SCLOCK_HILOGE("Get ScreenLockManagerProxy from SA failed.");
182         return nullptr;
183     }
184     SCLOCK_HILOGD("Getting ScreenLockManagerProxy succeeded.");
185     return screenlockServiceProxy;
186 }
187 
OnRemoteSaDied(const wptr<IRemoteObject> & remote)188 void ScreenLockAppManager::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
189 {
190     {
191         std::lock_guard<std::mutex> autoLock(managerProxyLock_);
192         screenlockManagerProxy_ = GetScreenLockManagerProxy();
193     }
194     if (systemEventListener_ != nullptr) {
195         SystemEvent systemEvent(SERVICE_RESTART);
196         systemEventListener_->OnCallBack(systemEvent);
197     }
198 }
199 
GetProxy()200 sptr<ScreenLockManagerInterface> ScreenLockAppManager::GetProxy()
201 {
202     std::lock_guard<std::mutex> autoLock(managerProxyLock_);
203     if (screenlockManagerProxy_ == nullptr) {
204         SCLOCK_HILOGW("Redo GetScreenLockManagerProxy");
205         screenlockManagerProxy_ = GetScreenLockManagerProxy();
206     }
207     return screenlockManagerProxy_;
208 }
209 
ScreenLockAppDeathRecipient()210 ScreenLockAppDeathRecipient::ScreenLockAppDeathRecipient()
211 {
212 }
213 
~ScreenLockAppDeathRecipient()214 ScreenLockAppDeathRecipient::~ScreenLockAppDeathRecipient()
215 {
216 }
217 
OnRemoteDied(const wptr<IRemoteObject> & object)218 void ScreenLockAppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
219 {
220     SCLOCK_HILOGE("ScreenLockAppDeathRecipient on remote systemAbility died.");
221     ScreenLockAppManager::GetInstance()->OnRemoteSaDied(object);
222 }
223 } // namespace ScreenLock
224 } // namespace OHOS
225