• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
17 
18 #include <string>
19 
20 #include "ipc_skeleton.h"
21 #include "parcel.h"
22 #include "sclock_log.h"
23 #include "screenlock_callback_interface.h"
24 #include "screenlock_common.h"
25 #include "screenlock_server_ipc_interface_code.h"
26 #include "screenlock_system_ability_interface.h"
27 
28 namespace OHOS {
29 namespace ScreenLock {
30 using namespace OHOS::HiviewDFX;
ScreenLockManagerStub()31 ScreenLockManagerStub::ScreenLockManagerStub()
32 {
33     InitHandleMap();
34 }
35 
InitHandleMap()36 void ScreenLockManagerStub::InitHandleMap()
37 {
38     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_LOCKED)] =
39         &ScreenLockManagerStub::OnIsLocked;
40     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREEN_LOCKED)] =
41         &ScreenLockManagerStub::OnIsScreenLocked;
42     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE)] =
43         &ScreenLockManagerStub::OnGetSecure;
44     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK)] = &ScreenLockManagerStub::OnUnlock;
45     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN)] =
46         &ScreenLockManagerStub::OnUnlockScreen;
47     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK)] = &ScreenLockManagerStub::OnLock;
48     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT)] =
49         &ScreenLockManagerStub::OnSendScreenLockEvent;
50     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT)] =
51         &ScreenLockManagerStub::OnScreenLockOn;
52     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK_SCREEN)] =
53         &ScreenLockManagerStub::OnLockScreen;
54     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREENLOCK_DISABLED)] =
55         &ScreenLockManagerStub::OnIsScreenLockDisabled;
56     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_DISABLED)] =
57         &ScreenLockManagerStub::OnSetScreenLockDisabled;
58     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_AUTHSTATE)] =
59         &ScreenLockManagerStub::OnSetScreenLockAuthState;
60     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_SCREENLOCK_AUTHSTATE)] =
61         &ScreenLockManagerStub::OnGetScreenLockAuthState;
62     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REQUEST_STRONG_AUTHSTATE)] =
63         &ScreenLockManagerStub::OnRequestStrongAuth;
64     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_STRONG_AUTHSTATE)] =
65         &ScreenLockManagerStub::OnGetStrongAuth;
66     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_DEVICE_LOCKED)] =
67         &ScreenLockManagerStub::OnIsDeviceLocked;
68     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REGISTER_INNER_LISTENER)] =
69         &ScreenLockManagerStub::OnRegistInnerListener;
70     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNREGISTER_INNER_LISTENER)] =
71         &ScreenLockManagerStub::OnUnRegistInnerListener;
72     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_USER_SCREEN_LOCKED)] =
73         &ScreenLockManagerStub::OnIsLockedWithUserId;
74 }
75 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int32_t ScreenLockManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
77     MessageOption &option) __attribute__((no_sanitize("cfi")))
78 {
79     SCLOCK_HILOGD("OnRemoteRequest started, code = %{public}d", code);
80     auto descriptorToken = data.ReadInterfaceToken();
81     if (descriptorToken != GetDescriptor()) {
82         SCLOCK_HILOGE("Remote descriptor not the same as local descriptor.");
83         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
84     }
85 
86     auto itFunc = handleFuncMap.find(code);
87     if (itFunc != handleFuncMap.end()) {
88         auto requestFunc = itFunc->second;
89         if (requestFunc != nullptr) {
90             (this->*requestFunc)(data, reply);
91             return ERR_NONE;
92         }
93     }
94 
95     SCLOCK_HILOGI("Default value received, check needed.");
96     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
97 }
98 
OnIsLocked(MessageParcel & data,MessageParcel & reply)99 int32_t ScreenLockManagerStub::OnIsLocked(MessageParcel &data, MessageParcel &reply)
100 {
101     bool isLocked = false;
102     int32_t ret = IsLocked(isLocked);
103     reply.WriteInt32(ret);
104     if (ret == E_SCREENLOCK_OK) {
105         reply.WriteBool(isLocked);
106     }
107     return ERR_NONE;
108 }
109 
OnIsScreenLocked(MessageParcel & data,MessageParcel & reply)110 int32_t ScreenLockManagerStub::OnIsScreenLocked(MessageParcel &data, MessageParcel &reply)
111 {
112     bool isScreenLocked = IsScreenLocked();
113     reply.WriteBool(isScreenLocked);
114     return ERR_NONE;
115 }
116 
OnGetSecure(MessageParcel & data,MessageParcel & reply)117 int32_t ScreenLockManagerStub::OnGetSecure(MessageParcel &data, MessageParcel &reply)
118 {
119     bool result = GetSecure();
120     reply.WriteBool(result);
121     SCLOCK_HILOGD("GetSecure result = %{public}d", result);
122     return ERR_NONE;
123 }
124 
OnUnlock(MessageParcel & data,MessageParcel & reply)125 int32_t ScreenLockManagerStub::OnUnlock(MessageParcel &data, MessageParcel &reply)
126 {
127     sptr<IRemoteObject> remote = data.ReadRemoteObject();
128     if (remote == nullptr) {
129         SCLOCK_HILOGE("remote is nullptr");
130         return ERR_INVALID_DATA;
131     }
132     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
133     if (listener.GetRefPtr() == nullptr) {
134         SCLOCK_HILOGE("listener is null");
135         return ERR_INVALID_DATA;
136     }
137     int32_t ret = Unlock(listener);
138     reply.WriteInt32(ret);
139     return ERR_NONE;
140 }
141 
OnUnlockScreen(MessageParcel & data,MessageParcel & reply)142 int32_t ScreenLockManagerStub::OnUnlockScreen(MessageParcel &data, MessageParcel &reply)
143 {
144     sptr<IRemoteObject> remote = data.ReadRemoteObject();
145     if (remote == nullptr) {
146         SCLOCK_HILOGE("remote is nullptr");
147         return ERR_INVALID_DATA;
148     }
149     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
150     if (listener.GetRefPtr() == nullptr) {
151         SCLOCK_HILOGE("listener is null");
152         return ERR_INVALID_DATA;
153     }
154     int32_t ret = UnlockScreen(listener);
155     reply.WriteInt32(ret);
156     return ERR_NONE;
157 }
158 
OnLock(MessageParcel & data,MessageParcel & reply)159 int32_t ScreenLockManagerStub::OnLock(MessageParcel &data, MessageParcel &reply)
160 {
161     sptr<IRemoteObject> remote = data.ReadRemoteObject();
162     if (remote == nullptr) {
163         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
164         return ERR_INVALID_DATA;
165     }
166     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
167     if (listener.GetRefPtr() == nullptr) {
168         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
169         return ERR_INVALID_DATA;
170     }
171     int32_t status = Lock(listener);
172     reply.WriteInt32(status);
173     return ERR_NONE;
174 }
175 
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)176 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
177 {
178     sptr<IRemoteObject> remote = data.ReadRemoteObject();
179     if (remote == nullptr) {
180         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
181         return ERR_INVALID_DATA;
182     }
183     sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
184     if (listener.GetRefPtr() == nullptr) {
185         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
186         return ERR_INVALID_DATA;
187     }
188     int32_t ret = OnSystemEvent(listener);
189     reply.WriteInt32(ret);
190     return ERR_NONE;
191 }
192 
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)193 int32_t ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
194 {
195     std::string event = data.ReadString();
196     int param = data.ReadInt32();
197     SCLOCK_HILOGD("event=%{public}s, param=%{public}d", event.c_str(), param);
198     int32_t retCode = SendScreenLockEvent(event, param);
199     reply.WriteInt32(retCode);
200     return ERR_NONE;
201 }
202 
OnIsScreenLockDisabled(MessageParcel & data,MessageParcel & reply)203 int32_t ScreenLockManagerStub::OnIsScreenLockDisabled(MessageParcel &data, MessageParcel &reply)
204 {
205     bool isDisabled = false;
206     int userId = data.ReadInt32();
207     SCLOCK_HILOGD("userId=%{public}d", userId);
208     int32_t retCode = IsScreenLockDisabled(userId, isDisabled);
209     reply.WriteInt32(retCode);
210     if (retCode == E_SCREENLOCK_OK) {
211         reply.WriteBool(isDisabled);
212     }
213     return ERR_NONE;
214 }
215 
OnSetScreenLockDisabled(MessageParcel & data,MessageParcel & reply)216 int32_t ScreenLockManagerStub::OnSetScreenLockDisabled(MessageParcel &data, MessageParcel &reply)
217 {
218     bool disable = data.ReadBool();
219     int userId = data.ReadInt32();
220     SCLOCK_HILOGD("disable=%{public}d, userId=%{public}d", disable, userId);
221     int32_t retCode = SetScreenLockDisabled(disable, userId);
222     reply.WriteInt32(retCode);
223     return ERR_NONE;
224 }
225 
OnSetScreenLockAuthState(MessageParcel & data,MessageParcel & reply)226 int32_t ScreenLockManagerStub::OnSetScreenLockAuthState(MessageParcel &data, MessageParcel &reply)
227 {
228     int32_t authState = data.ReadInt32();
229     int32_t userId = data.ReadInt32();
230     std::string authToken = data.ReadString();
231     int32_t retCode = SetScreenLockAuthState(authState, userId, authToken);
232     reply.WriteInt32(retCode);
233     return ERR_NONE;
234 }
235 
OnGetScreenLockAuthState(MessageParcel & data,MessageParcel & reply)236 int32_t ScreenLockManagerStub::OnGetScreenLockAuthState(MessageParcel &data, MessageParcel &reply)
237 {
238     int32_t authState = -1;
239     int32_t userId = data.ReadInt32();
240     SCLOCK_HILOGD("userId=%{public}d", userId);
241     int32_t retCode = GetScreenLockAuthState(userId, authState);
242     reply.WriteInt32(retCode);
243     if (retCode == E_SCREENLOCK_OK) {
244         reply.WriteInt32(authState);
245     }
246     return ERR_NONE;
247 }
248 
OnRequestStrongAuth(MessageParcel & data,MessageParcel & reply)249 int32_t ScreenLockManagerStub::OnRequestStrongAuth(MessageParcel &data, MessageParcel &reply)
250 {
251     int32_t reasonFlag = data.ReadInt32();
252     int32_t userId = data.ReadInt32();
253     SCLOCK_HILOGD("OnRequestStrongAuth. reasonFlag=%{public}d", reasonFlag);
254     int32_t retCode = RequestStrongAuth(reasonFlag, userId);
255     reply.WriteInt32(retCode);
256     return ERR_NONE;
257 }
258 
OnGetStrongAuth(MessageParcel & data,MessageParcel & reply)259 int32_t ScreenLockManagerStub::OnGetStrongAuth(MessageParcel &data, MessageParcel &reply)
260 {
261     int32_t reasonFlag = -1;
262     int32_t userId = data.ReadInt32();
263     int32_t retCode = GetStrongAuth(userId, reasonFlag);
264     SCLOCK_HILOGI("userId=%{public}d, reasonFlag=%{public}d", userId, reasonFlag);
265     reply.WriteInt32(retCode);
266     if (retCode == E_SCREENLOCK_OK) {
267         reply.WriteInt32(reasonFlag);
268     }
269     return ERR_NONE;
270 }
271 
OnRegistInnerListener(MessageParcel & data,MessageParcel & reply)272 int32_t ScreenLockManagerStub::OnRegistInnerListener(MessageParcel &data, MessageParcel &reply)
273 {
274     int32_t userId = data.ReadInt32();
275     int32_t listenType = data.ReadInt32();
276 
277     sptr<IRemoteObject> remote = data.ReadRemoteObject();
278     if (remote == nullptr) {
279         SCLOCK_HILOGE("remote is nullptr");
280         return ERR_INVALID_DATA;
281     }
282     sptr<InnerListenerIf> listener = iface_cast<InnerListenerIf>(remote);
283     if (listener.GetRefPtr() == nullptr) {
284         SCLOCK_HILOGE("listener is null");
285         return ERR_INVALID_DATA;
286     }
287     int32_t retCode = RegisterInnerListener(userId, static_cast<ListenType>(listenType), listener);
288     reply.WriteInt32(retCode);
289     return ERR_NONE;
290 }
291 
OnUnRegistInnerListener(MessageParcel & data,MessageParcel & reply)292 int32_t ScreenLockManagerStub::OnUnRegistInnerListener(MessageParcel &data, MessageParcel &reply)
293 {
294     int32_t userId = data.ReadInt32();
295     ListenType listenType = static_cast<ListenType>(data.ReadInt32());
296     sptr<IRemoteObject> remote = data.ReadRemoteObject();
297     if (remote == nullptr) {
298         SCLOCK_HILOGE("remote is nullptr");
299         return ERR_INVALID_DATA;
300     }
301     sptr<InnerListenerIf> listener = iface_cast<InnerListenerIf>(remote);
302     if (listener.GetRefPtr() == nullptr) {
303         SCLOCK_HILOGE("listener is null");
304         return ERR_INVALID_DATA;
305     }
306     int32_t retCode = UnRegisterInnerListener(userId, listenType, listener);
307     reply.WriteInt32(retCode);
308     return ERR_NONE;
309 }
310 
OnLockScreen(MessageParcel & data,MessageParcel & reply)311 int32_t ScreenLockManagerStub::OnLockScreen(MessageParcel &data, MessageParcel &reply)
312 {
313     int32_t useId = data.ReadInt32();
314     int32_t retCode = Lock(useId);
315     reply.WriteInt32(retCode);
316     return ERR_NONE;
317 }
318 
OnIsDeviceLocked(MessageParcel & data,MessageParcel & reply)319 int32_t ScreenLockManagerStub::OnIsDeviceLocked(MessageParcel &data, MessageParcel &reply)
320 {
321     bool isDeviceLocked = false;
322     int32_t userId = data.ReadInt32();
323     SCLOCK_HILOGD("userId=%{public}d", userId);
324     int32_t retCode = IsDeviceLocked(userId, isDeviceLocked);
325     reply.WriteInt32(retCode);
326     if (retCode == E_SCREENLOCK_OK) {
327         reply.WriteBool(isDeviceLocked);
328     }
329     return ERR_NONE;
330 }
331 
OnIsLockedWithUserId(MessageParcel & data,MessageParcel & reply)332 int32_t ScreenLockManagerStub::OnIsLockedWithUserId(MessageParcel &data, MessageParcel &reply)
333 {
334     bool isLocked = true;
335     int32_t userId = data.ReadInt32();
336     SCLOCK_HILOGD("userId=%{public}d", userId);
337     int32_t retCode = IsLockedWithUserId(userId, isLocked);
338     reply.WriteInt32(retCode);
339     if (retCode == E_SCREENLOCK_OK) {
340         reply.WriteBool(isLocked);
341     }
342     return ERR_NONE;
343 }
344 } // namespace ScreenLock
345 } // namespace OHOS