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_appinfo.h"
24 #include "screenlock_callback_interface.h"
25 #include "screenlock_common.h"
26 #include "screenlock_server_ipc_interface_code.h"
27 #include "screenlock_system_ability_interface.h"
28 
29 namespace OHOS {
30 namespace ScreenLock {
31 using namespace OHOS::HiviewDFX;
ScreenLockManagerStub()32 ScreenLockManagerStub::ScreenLockManagerStub()
33 {
34     InitHandleMap();
35 }
36 
InitHandleMap()37 void ScreenLockManagerStub::InitHandleMap()
38 {
39     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_LOCKED)] =
40         &ScreenLockManagerStub::OnIsLocked;
41     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREEN_LOCKED)] =
42         &ScreenLockManagerStub::OnIsScreenLocked;
43     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE)] =
44         &ScreenLockManagerStub::OnGetSecure;
45     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK)] = &ScreenLockManagerStub::OnUnlock;
46     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN)] =
47         &ScreenLockManagerStub::OnUnlockScreen;
48     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK)] = &ScreenLockManagerStub::OnLock;
49     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT)] =
50         &ScreenLockManagerStub::OnSendScreenLockEvent;
51     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT)] =
52         &ScreenLockManagerStub::OnScreenLockOn;
53     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK_SCREEN)] =
54         &ScreenLockManagerStub::OnLockScreen;
55     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREENLOCK_DISABLED)] =
56         &ScreenLockManagerStub::OnIsScreenLockDisabled;
57     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_DISABLED)] =
58         &ScreenLockManagerStub::OnSetScreenLockDisabled;
59     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SET_SCREENLOCK_AUTHSTATE)] =
60         &ScreenLockManagerStub::OnSetScreenLockAuthState;
61     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_SCREENLOCK_AUTHSTATE)] =
62         &ScreenLockManagerStub::OnGetScreenLockAuthState;
63     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::REQUEST_STRONG_AUTHSTATE)] =
64         &ScreenLockManagerStub::OnRequestStrongAuth;
65     handleFuncMap[static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::GET_STRONG_AUTHSTATE)] =
66         &ScreenLockManagerStub::OnGetStrongAuth;
67 }
68 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)69 int32_t ScreenLockManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
70     MessageOption &option) __attribute__((no_sanitize("cfi")))
71 {
72     SCLOCK_HILOGD("OnRemoteRequest started, code = %{public}d", code);
73     auto descriptorToken = data.ReadInterfaceToken();
74     if (descriptorToken != GetDescriptor()) {
75         SCLOCK_HILOGE("Remote descriptor not the same as local descriptor.");
76         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
77     }
78 
79     auto itFunc = handleFuncMap.find(code);
80     if (itFunc != handleFuncMap.end()) {
81         auto requestFunc = itFunc->second;
82         if (requestFunc != nullptr) {
83             (this->*requestFunc)(data, reply);
84             return ERR_NONE;
85         }
86     }
87 
88     SCLOCK_HILOGI("Default value received, check needed.");
89     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
90 }
91 
OnIsLocked(MessageParcel & data,MessageParcel & reply)92 int32_t ScreenLockManagerStub::OnIsLocked(MessageParcel &data, MessageParcel &reply)
93 {
94     bool isLocked = false;
95     int32_t ret = IsLocked(isLocked);
96     reply.WriteInt32(ret);
97     if (ret == E_SCREENLOCK_OK) {
98         reply.WriteBool(isLocked);
99     }
100     return ERR_NONE;
101 }
102 
OnIsScreenLocked(MessageParcel & data,MessageParcel & reply)103 int32_t ScreenLockManagerStub::OnIsScreenLocked(MessageParcel &data, MessageParcel &reply)
104 {
105     bool isScreenLocked = IsScreenLocked();
106     reply.WriteBool(isScreenLocked);
107     return ERR_NONE;
108 }
109 
OnGetSecure(MessageParcel & data,MessageParcel & reply)110 int32_t ScreenLockManagerStub::OnGetSecure(MessageParcel &data, MessageParcel &reply)
111 {
112     bool result = GetSecure();
113     reply.WriteBool(result);
114     SCLOCK_HILOGD("GetSecure result = %{public}d", result);
115     return ERR_NONE;
116 }
117 
OnUnlock(MessageParcel & data,MessageParcel & reply)118 int32_t ScreenLockManagerStub::OnUnlock(MessageParcel &data, MessageParcel &reply)
119 {
120     sptr<IRemoteObject> remote = data.ReadRemoteObject();
121     if (remote == nullptr) {
122         SCLOCK_HILOGE("remote is nullptr");
123         return ERR_INVALID_DATA;
124     }
125     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
126     if (listener.GetRefPtr() == nullptr) {
127         SCLOCK_HILOGE("listener is null");
128         return ERR_INVALID_DATA;
129     }
130     int32_t ret = Unlock(listener);
131     reply.WriteInt32(ret);
132     return ERR_NONE;
133 }
134 
OnUnlockScreen(MessageParcel & data,MessageParcel & reply)135 int32_t ScreenLockManagerStub::OnUnlockScreen(MessageParcel &data, MessageParcel &reply)
136 {
137     sptr<IRemoteObject> remote = data.ReadRemoteObject();
138     if (remote == nullptr) {
139         SCLOCK_HILOGE("remote is nullptr");
140         return ERR_INVALID_DATA;
141     }
142     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
143     if (listener.GetRefPtr() == nullptr) {
144         SCLOCK_HILOGE("listener is null");
145         return ERR_INVALID_DATA;
146     }
147     int32_t ret = UnlockScreen(listener);
148     reply.WriteInt32(ret);
149     return ERR_NONE;
150 }
151 
OnLock(MessageParcel & data,MessageParcel & reply)152 int32_t ScreenLockManagerStub::OnLock(MessageParcel &data, MessageParcel &reply)
153 {
154     sptr<IRemoteObject> remote = data.ReadRemoteObject();
155     if (remote == nullptr) {
156         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
157         return ERR_INVALID_DATA;
158     }
159     sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
160     if (listener.GetRefPtr() == nullptr) {
161         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
162         return ERR_INVALID_DATA;
163     }
164     int32_t status = Lock(listener);
165     reply.WriteInt32(status);
166     return ERR_NONE;
167 }
168 
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)169 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
170 {
171     sptr<IRemoteObject> remote = data.ReadRemoteObject();
172     if (remote == nullptr) {
173         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
174         return ERR_INVALID_DATA;
175     }
176     sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
177     if (listener.GetRefPtr() == nullptr) {
178         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
179         return ERR_INVALID_DATA;
180     }
181     int32_t ret = OnSystemEvent(listener);
182     reply.WriteInt32(ret);
183     return ERR_NONE;
184 }
185 
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)186 int32_t ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
187 {
188     std::string event = data.ReadString();
189     int param = data.ReadInt32();
190     SCLOCK_HILOGD("event=%{public}s, param=%{public}d", event.c_str(), param);
191     int32_t retCode = SendScreenLockEvent(event, param);
192     reply.WriteInt32(retCode);
193     return ERR_NONE;
194 }
195 
OnIsScreenLockDisabled(MessageParcel & data,MessageParcel & reply)196 int32_t ScreenLockManagerStub::OnIsScreenLockDisabled(MessageParcel &data, MessageParcel &reply)
197 {
198     bool isDisabled = false;
199     int userId = data.ReadInt32();
200     SCLOCK_HILOGD("userId=%{public}d", userId);
201     int32_t retCode = IsScreenLockDisabled(userId, isDisabled);
202     reply.WriteInt32(retCode);
203     if (retCode == E_SCREENLOCK_OK) {
204         reply.WriteBool(isDisabled);
205     }
206     return ERR_NONE;
207 }
208 
OnSetScreenLockDisabled(MessageParcel & data,MessageParcel & reply)209 int32_t ScreenLockManagerStub::OnSetScreenLockDisabled(MessageParcel &data, MessageParcel &reply)
210 {
211     bool disable = data.ReadBool();
212     int userId = data.ReadInt32();
213     SCLOCK_HILOGD("disable=%{public}d, userId=%{public}d", disable, userId);
214     int32_t retCode = SetScreenLockDisabled(disable, userId);
215     reply.WriteInt32(retCode);
216     return ERR_NONE;
217 }
218 
OnSetScreenLockAuthState(MessageParcel & data,MessageParcel & reply)219 int32_t ScreenLockManagerStub::OnSetScreenLockAuthState(MessageParcel &data, MessageParcel &reply)
220 {
221     int32_t authState = data.ReadInt32();
222     int32_t userId = data.ReadInt32();
223     std::string authToken = data.ReadString();
224     int32_t retCode = SetScreenLockAuthState(authState, userId, authToken);
225     reply.WriteInt32(retCode);
226     return ERR_NONE;
227 }
228 
OnGetScreenLockAuthState(MessageParcel & data,MessageParcel & reply)229 int32_t ScreenLockManagerStub::OnGetScreenLockAuthState(MessageParcel &data, MessageParcel &reply)
230 {
231     int32_t authState = -1;
232     int32_t userId = data.ReadInt32();
233     SCLOCK_HILOGD("userId=%{public}d", userId);
234     int32_t retCode = GetScreenLockAuthState(userId, authState);
235     reply.WriteInt32(retCode);
236     if (retCode == E_SCREENLOCK_OK) {
237         reply.WriteInt32(authState);
238     }
239     return ERR_NONE;
240 }
241 
OnRequestStrongAuth(MessageParcel & data,MessageParcel & reply)242 int32_t ScreenLockManagerStub::OnRequestStrongAuth(MessageParcel &data, MessageParcel &reply)
243 {
244     int32_t reasonFlag = data.ReadInt32();
245     int32_t userId = data.ReadInt32();
246     SCLOCK_HILOGD("OnRequestStrongAuth. reasonFlag=%{public}d", reasonFlag);
247     int32_t retCode = RequestStrongAuth(reasonFlag, userId);
248     reply.WriteInt32(retCode);
249     return ERR_NONE;
250 }
251 
OnGetStrongAuth(MessageParcel & data,MessageParcel & reply)252 int32_t ScreenLockManagerStub::OnGetStrongAuth(MessageParcel &data, MessageParcel &reply)
253 {
254     int32_t reasonFlag = -1;
255     int32_t userId = data.ReadInt32();
256     int32_t retCode = GetStrongAuth(userId, reasonFlag);
257     SCLOCK_HILOGI("userId=%{public}d, reasonFlag=%{public}d", userId, reasonFlag);
258     reply.WriteInt32(retCode);
259     if (retCode == E_SCREENLOCK_OK) {
260         reply.WriteInt32(reasonFlag);
261     }
262     return ERR_NONE;
263 }
264 
OnLockScreen(MessageParcel & data,MessageParcel & reply)265 int32_t ScreenLockManagerStub::OnLockScreen(MessageParcel &data, MessageParcel &reply)
266 {
267     int32_t useId = data.ReadInt32();
268     int32_t retCode = Lock(useId);
269     reply.WriteInt32(retCode);
270     return ERR_NONE;
271 }
272 } // namespace ScreenLock
273 } // namespace OHOS