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 #include "screenlock_manager_proxy.h"
16 
17 #include "hilog/log_cpp.h"
18 #include "iremote_broker.h"
19 #include "sclock_log.h"
20 #include "screenlock_server_ipc_interface_code.h"
21 
22 namespace OHOS {
23 namespace ScreenLock {
24 using namespace OHOS::HiviewDFX;
25 
ScreenLockManagerProxy(const sptr<IRemoteObject> & object)26 ScreenLockManagerProxy::ScreenLockManagerProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<ScreenLockManagerInterface>(object)
28 {
29 }
30 
IsScreenLockedInner(MessageParcel & reply,uint32_t command)31 int32_t ScreenLockManagerProxy::IsScreenLockedInner(MessageParcel &reply, uint32_t command)
32 {
33     MessageParcel data;
34     MessageOption option;
35     if (!data.WriteInterfaceToken(GetDescriptor())) {
36         SCLOCK_HILOGE(" Failed to write parcelable ");
37         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
38     }
39     int32_t ret = Remote()->SendRequest(command, data, reply, option);
40     if (ret != ERR_NONE) {
41         SCLOCK_HILOGE("IsScreenLocked, ret = %{public}d", ret);
42         return E_SCREENLOCK_SENDREQUEST_FAILED;
43     }
44     return E_SCREENLOCK_OK;
45 }
46 
IsLocked(bool & isLocked)47 int32_t ScreenLockManagerProxy::IsLocked(bool &isLocked)
48 {
49     MessageParcel reply;
50     int32_t ret = IsScreenLockedInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_LOCKED));
51     if (ret != E_SCREENLOCK_OK) {
52         SCLOCK_HILOGE("IsLocked, ret = %{public}d", ret);
53         return ret;
54     }
55     int errCode = reply.ReadInt32();
56     if (errCode != E_SCREENLOCK_OK) {
57         SCLOCK_HILOGE("IsLocked, errCode = %{public}d", errCode);
58         return errCode;
59     }
60     isLocked = reply.ReadBool();
61     return E_SCREENLOCK_OK;
62 }
63 
IsScreenLocked()64 bool ScreenLockManagerProxy::IsScreenLocked()
65 {
66     MessageParcel reply;
67     int32_t ret = IsScreenLockedInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREEN_LOCKED));
68     if (ret != E_SCREENLOCK_OK) {
69         return false;
70     }
71     return reply.ReadBool();
72 }
73 
GetSecure()74 bool ScreenLockManagerProxy::GetSecure()
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option;
79     data.WriteInterfaceToken(ScreenLockManagerProxy::GetDescriptor());
80     SCLOCK_HILOGD("ScreenLockManagerProxy GetSecure started.");
81     bool ret = Remote()->SendRequest(
82         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE), data, reply, option);
83     if (ret != ERR_NONE) {
84         SCLOCK_HILOGE("GetSecure, ret = %{public}d", ret);
85         return false;
86     }
87     return reply.ReadBool();
88 }
89 
UnlockInner(MessageParcel & reply,int32_t command,const sptr<ScreenLockCallbackInterface> & listener)90 int32_t ScreenLockManagerProxy::UnlockInner(
91     MessageParcel &reply, int32_t command, const sptr<ScreenLockCallbackInterface> &listener)
92 {
93     MessageParcel data;
94     MessageOption option;
95     data.WriteInterfaceToken(GetDescriptor());
96     SCLOCK_HILOGD("started.");
97     if (listener == nullptr) {
98         SCLOCK_HILOGE("listener is nullptr");
99         return E_SCREENLOCK_NULLPTR;
100     }
101     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
102         SCLOCK_HILOGE("write parcel failed.");
103         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
104     }
105     int32_t ret = Remote()->SendRequest(command, data, reply, option);
106     if (ret != ERR_NONE) {
107         SCLOCK_HILOGE("RequestUnlock, ret = %{public}d", ret);
108         return E_SCREENLOCK_SENDREQUEST_FAILED;
109     }
110     return E_SCREENLOCK_OK;
111 }
112 
Unlock(const sptr<ScreenLockCallbackInterface> & listener)113 int32_t ScreenLockManagerProxy::Unlock(const sptr<ScreenLockCallbackInterface> &listener)
114 {
115     MessageParcel reply;
116     int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK), listener);
117     if (ret != E_SCREENLOCK_OK) {
118         SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
119         return ret;
120     }
121     return reply.ReadInt32();
122 }
123 
UnlockScreen(const sptr<ScreenLockCallbackInterface> & listener)124 int32_t ScreenLockManagerProxy::UnlockScreen(const sptr<ScreenLockCallbackInterface> &listener)
125 {
126     MessageParcel reply;
127     int ret = UnlockInner(reply, static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN), listener);
128     if (ret != E_SCREENLOCK_OK) {
129         SCLOCK_HILOGE("Unlock, ret = %{public}d", ret);
130         return ret;
131     }
132     return reply.ReadInt32();
133 }
134 
Lock(const sptr<ScreenLockCallbackInterface> & listener)135 int32_t ScreenLockManagerProxy::Lock(const sptr<ScreenLockCallbackInterface> &listener)
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option;
140     if (!data.WriteInterfaceToken(GetDescriptor())) {
141         SCLOCK_HILOGE(" Failed to write parcelable ");
142         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
143     }
144     SCLOCK_HILOGD("ScreenLockManagerProxy RequestLock started.");
145     if (listener == nullptr) {
146         SCLOCK_HILOGE("listener is nullptr");
147         return E_SCREENLOCK_NULLPTR;
148     }
149     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
150         SCLOCK_HILOGE("write parcel failed.");
151         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
152     }
153     int32_t ret =
154         Remote()->SendRequest(static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK), data, reply, option);
155     if (ret != ERR_NONE) {
156         SCLOCK_HILOGE("RequestLock, ret = %{public}d", ret);
157         return E_SCREENLOCK_SENDREQUEST_FAILED;
158     }
159     int32_t retCode = reply.ReadInt32();
160     SCLOCK_HILOGD("Lock retCode = %{public}d", retCode);
161     return retCode;
162 }
163 
Lock(int32_t userId)164 int32_t ScreenLockManagerProxy::Lock(int32_t userId)
165 {
166     MessageParcel data;
167     MessageParcel reply;
168     MessageOption option;
169     if (!data.WriteInterfaceToken(GetDescriptor())) {
170         SCLOCK_HILOGE(" Failed to write parcelable ");
171         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
172     }
173     if (!data.WriteInt32(userId)) {
174         SCLOCK_HILOGE(" Failed to write userId");
175         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
176     }
177     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK_SCREEN), data,
178         reply, option);
179     if (ret != ERR_NONE) {
180         SCLOCK_HILOGE("RequestLock failed, ret = %{public}d", ret);
181         return E_SCREENLOCK_SENDREQUEST_FAILED;
182     }
183     int32_t retCode = reply.ReadInt32();
184     SCLOCK_HILOGD("Lock retCode = %{public}d", retCode);
185     return retCode;
186 }
187 
OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> & listener)188 int32_t ScreenLockManagerProxy::OnSystemEvent(const sptr<ScreenLockSystemAbilityInterface> &listener)
189 {
190     SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent");
191     MessageParcel data;
192     MessageParcel reply;
193     MessageOption option;
194     if (!data.WriteInterfaceToken(GetDescriptor())) {
195         SCLOCK_HILOGE(" Failed to write parcelable ");
196         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
197     }
198     if (listener == nullptr) {
199         SCLOCK_HILOGE("listener is nullptr");
200         return E_SCREENLOCK_NULLPTR;
201     }
202     if (!data.WriteRemoteObject(listener->AsObject().GetRefPtr())) {
203         SCLOCK_HILOGE("write parcel failed.");
204         return E_SCREENLOCK_WRITE_PARCEL_ERROR;
205     }
206     int32_t result = Remote()->SendRequest(
207         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT), data, reply, option);
208     if (result != ERR_NONE) {
209         SCLOCK_HILOGE(" ScreenLockManagerProxy::OnSystemEvent fail, result = %{public}d ", result);
210         return E_SCREENLOCK_SENDREQUEST_FAILED;
211     }
212     int32_t status = reply.ReadInt32();
213     SCLOCK_HILOGD("ScreenLockManagerProxy::OnSystemEvent out status is :%{public}d", status);
214     return status;
215 }
216 
SendScreenLockEvent(const std::string & event,int param)217 int32_t ScreenLockManagerProxy::SendScreenLockEvent(const std::string &event, int param)
218 {
219     MessageParcel data;
220     MessageParcel reply;
221     MessageOption option;
222     data.WriteInterfaceToken(GetDescriptor());
223     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent started.");
224     data.WriteString(event);
225     data.WriteInt32(param);
226     int32_t ret = Remote()->SendRequest(
227         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT), data, reply, option);
228     if (ret != ERR_NONE) {
229         SCLOCK_HILOGE("ScreenLockManagerProxy SendScreenLockEvent, ret = %{public}d", ret);
230         return E_SCREENLOCK_SENDREQUEST_FAILED;
231     }
232     int32_t retCode = reply.ReadInt32();
233     SCLOCK_HILOGD("ScreenLockManagerProxy SendScreenLockEvent end retCode is %{public}d.", retCode);
234     return retCode;
235 }
236 
IsScreenLockDisabled(int userId,bool & isDisabled)237 int32_t ScreenLockManagerProxy::IsScreenLockDisabled(int userId, bool &isDisabled)
238 {
239     MessageParcel data;
240     MessageParcel reply;
241     MessageOption option;
242     data.WriteInterfaceToken(GetDescriptor());
243     data.WriteInt32(userId);
244     SCLOCK_HILOGD("ScreenLockManagerProxy IsScreenLockDisabled started.");
245     int32_t ret = Remote()->SendRequest(
246         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREENLOCK_DISABLED), data, reply, option);
247     if (ret != ERR_NONE) {
248         SCLOCK_HILOGE("IsScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
249         return E_SCREENLOCK_SENDREQUEST_FAILED;
250     }
251     int32_t retCode = reply.ReadInt32();
252     if (retCode != E_SCREENLOCK_OK) {
253         return retCode;
254     }
255     isDisabled = reply.ReadBool();
256     SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, isDisabled);
257     return retCode;
258 }
259 
SetScreenLockDisabled(bool disable,int userId)260 int32_t ScreenLockManagerProxy::SetScreenLockDisabled(bool disable, int userId)
261 {
262     MessageParcel data;
263     MessageParcel reply;
264     MessageOption option;
265     data.WriteInterfaceToken(GetDescriptor());
266     data.WriteBool(disable);
267     data.WriteInt32(userId);
268     SCLOCK_HILOGD("ScreenLockManagerProxy SetScreenLockDisabled started.");
269     int32_t ret = Remote()->SendRequest(
270         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_DISABLED), data, reply, option);
271     if (ret != ERR_NONE) {
272         SCLOCK_HILOGE("SetScreenLockDisabled SendRequest failed, ret = %{public}d", ret);
273         return E_SCREENLOCK_SENDREQUEST_FAILED;
274     }
275     int32_t retCode = reply.ReadInt32();
276     SCLOCK_HILOGD("IsScreenLockDisabled end retCode is %{public}d, %{public}d.", retCode, disable);
277     return retCode;
278 }
279 
SetScreenLockAuthState(int authState,int32_t userId,std::string & authToken)280 int32_t ScreenLockManagerProxy::SetScreenLockAuthState(int authState, int32_t userId, std::string &authToken)
281 {
282     MessageParcel data;
283     MessageParcel reply;
284     MessageOption option;
285     data.WriteInterfaceToken(GetDescriptor());
286     data.WriteInt32(authState);
287     data.WriteInt32(userId);
288     data.WriteString(authToken);
289 
290     int32_t ret = Remote()->SendRequest(
291         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_AUTHSTATE), data, reply, option);
292     if (ret != ERR_NONE) {
293         SCLOCK_HILOGE("ScreenLockManagerProxy SetScreenLockAuthState, ret = %{public}d", ret);
294         return E_SCREENLOCK_SENDREQUEST_FAILED;
295     }
296     int32_t retCode = reply.ReadInt32();
297     SCLOCK_HILOGD("ScreenLockManagerProxy SetScreenLockAuthState end retCode is %{public}d.", retCode);
298     return retCode;
299 }
300 
GetScreenLockAuthState(int userId,int32_t & authState)301 int32_t ScreenLockManagerProxy::GetScreenLockAuthState(int userId, int32_t &authState)
302 {
303     MessageParcel data;
304     MessageParcel reply;
305     MessageOption option;
306     data.WriteInterfaceToken(GetDescriptor());
307     data.WriteInt32(userId);
308 
309     int32_t ret = Remote()->SendRequest(
310         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_SCREENLOCK_AUTHSTATE), data, reply, option);
311     if (ret != ERR_NONE) {
312         SCLOCK_HILOGE("ScreenLockManagerProxy GetScreenLockAuthState, ret = %{public}d", ret);
313         return E_SCREENLOCK_SENDREQUEST_FAILED;
314     }
315     int32_t retCode = reply.ReadInt32();
316     authState = reply.ReadInt32();
317     SCLOCK_HILOGD("GetScreenLockAuthState end retCode is %{public}d, %{public}d.", retCode, authState);
318     return retCode;
319 }
320 
RequestStrongAuth(int reasonFlag,int32_t userId)321 int32_t ScreenLockManagerProxy::RequestStrongAuth(int reasonFlag, int32_t userId)
322 {
323     MessageParcel data;
324     MessageParcel reply;
325     MessageOption option;
326     data.WriteInterfaceToken(GetDescriptor());
327     data.WriteInt32(reasonFlag);
328     data.WriteInt32(userId);
329 
330     int32_t ret = Remote()->SendRequest(
331         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REQUEST_STRONG_AUTHSTATE), data, reply, option);
332     if (ret != ERR_NONE) {
333         SCLOCK_HILOGE("ScreenLockManagerProxy RequestStrongAuth, ret = %{public}d", ret);
334         return E_SCREENLOCK_SENDREQUEST_FAILED;
335     }
336     int32_t retCode = reply.ReadInt32();
337     SCLOCK_HILOGD("ScreenLockManagerProxy RequestStrongAuth end retCode is %{public}d.", retCode);
338     return retCode;
339     return 0;
340 }
341 
GetStrongAuth(int userId,int32_t & reasonFlag)342 int32_t ScreenLockManagerProxy::GetStrongAuth(int userId, int32_t &reasonFlag)
343 {
344     MessageParcel data;
345     MessageParcel reply;
346     MessageOption option;
347     data.WriteInterfaceToken(GetDescriptor());
348     data.WriteInt32(userId);
349 
350     int32_t ret = Remote()->SendRequest(
351         static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_STRONG_AUTHSTATE), data, reply, option);
352     if (ret != ERR_NONE) {
353         SCLOCK_HILOGE("ScreenLockManagerProxy GetStrongAuth, ret = %{public}d", ret);
354         return E_SCREENLOCK_SENDREQUEST_FAILED;
355     }
356     int32_t retCode = reply.ReadInt32();
357     reasonFlag = reply.ReadInt32();
358     SCLOCK_HILOGD("GetStrongAuth end retCode is %{public}d, %{public}d.", retCode, reasonFlag);
359     return retCode;
360 }
361 } // namespace ScreenLock
362 } // namespace OHOS