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;
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t ScreenLockManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34 MessageOption &option) __attribute__((no_sanitize("cfi")))
35 {
36 SCLOCK_HILOGD("OnRemoteRequest started, code = %{public}d", code);
37 auto descriptorToken = data.ReadInterfaceToken();
38 if (descriptorToken != GetDescriptor()) {
39 SCLOCK_HILOGE("Remote descriptor not the same as local descriptor.");
40 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
41 }
42 switch (code) {
43 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_LOCKED):
44 OnIsLocked(data, reply);
45 break;
46 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SCREEN_LOCKED):
47 OnIsScreenLocked(data, reply);
48 break;
49 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::IS_SECURE_MODE):
50 OnGetSecure(data, reply);
51 break;
52 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK):
53 OnUnlock(data, reply);
54 break;
55 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::UNLOCK_SCREEN):
56 OnUnlockScreen(data, reply);
57 break;
58 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK):
59 OnLock(data, reply);
60 break;
61 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::SEND_SCREENLOCK_EVENT):
62 OnSendScreenLockEvent(data, reply);
63 break;
64 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::ONSYSTEMEVENT):
65 OnScreenLockOn(data, reply);
66 break;
67 case static_cast<uint32_t>(ScreenLockServerIpcInterfaceCode::LOCK_SCREEN):
68 OnLockScreen(data, reply);
69 break;
70 default:
71 SCLOCK_HILOGE("Default value received, check needed.");
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74 return ERR_NONE;
75 }
76
OnIsLocked(Parcel & data,Parcel & reply)77 int32_t ScreenLockManagerStub::OnIsLocked(Parcel &data, Parcel &reply)
78 {
79 bool isLocked = false;
80 int32_t ret = IsLocked(isLocked);
81 reply.WriteInt32(ret);
82 if (ret == E_SCREENLOCK_OK) {
83 reply.WriteBool(isLocked);
84 }
85 return ERR_NONE;
86 }
87
OnIsScreenLocked(Parcel & data,Parcel & reply)88 int32_t ScreenLockManagerStub::OnIsScreenLocked(Parcel &data, Parcel &reply)
89 {
90 bool isScreenLocked = IsScreenLocked();
91 reply.WriteBool(isScreenLocked);
92 return ERR_NONE;
93 }
94
OnGetSecure(Parcel & data,Parcel & reply)95 int32_t ScreenLockManagerStub::OnGetSecure(Parcel &data, Parcel &reply)
96 {
97 bool result = GetSecure();
98 reply.WriteBool(result);
99 SCLOCK_HILOGD("GetSecure result = %{public}d", result);
100 return ERR_NONE;
101 }
102
OnUnlock(MessageParcel & data,MessageParcel & reply)103 int32_t ScreenLockManagerStub::OnUnlock(MessageParcel &data, MessageParcel &reply)
104 {
105 sptr<IRemoteObject> remote = data.ReadRemoteObject();
106 if (remote == nullptr) {
107 SCLOCK_HILOGE("remote is nullptr");
108 return ERR_INVALID_DATA;
109 }
110 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
111 if (listener.GetRefPtr() == nullptr) {
112 SCLOCK_HILOGE("listener is null");
113 return ERR_INVALID_DATA;
114 }
115 int32_t ret = Unlock(listener);
116 reply.WriteInt32(ret);
117 return ERR_NONE;
118 }
119
OnUnlockScreen(MessageParcel & data,MessageParcel & reply)120 int32_t ScreenLockManagerStub::OnUnlockScreen(MessageParcel &data, MessageParcel &reply)
121 {
122 sptr<IRemoteObject> remote = data.ReadRemoteObject();
123 if (remote == nullptr) {
124 SCLOCK_HILOGE("remote is nullptr");
125 return ERR_INVALID_DATA;
126 }
127 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
128 if (listener.GetRefPtr() == nullptr) {
129 SCLOCK_HILOGE("listener is null");
130 return ERR_INVALID_DATA;
131 }
132 int32_t ret = UnlockScreen(listener);
133 reply.WriteInt32(ret);
134 return ERR_NONE;
135 }
136
OnLock(MessageParcel & data,MessageParcel & reply)137 int32_t ScreenLockManagerStub::OnLock(MessageParcel &data, MessageParcel &reply)
138 {
139 sptr<IRemoteObject> remote = data.ReadRemoteObject();
140 if (remote == nullptr) {
141 SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
142 return ERR_INVALID_DATA;
143 }
144 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
145 if (listener.GetRefPtr() == nullptr) {
146 SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
147 return ERR_INVALID_DATA;
148 }
149 int32_t status = Lock(listener);
150 reply.WriteInt32(status);
151 return ERR_NONE;
152 }
153
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)154 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
155 {
156 sptr<IRemoteObject> remote = data.ReadRemoteObject();
157 if (remote == nullptr) {
158 SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
159 return ERR_INVALID_DATA;
160 }
161 sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
162 if (listener.GetRefPtr() == nullptr) {
163 SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
164 return ERR_INVALID_DATA;
165 }
166 int32_t ret = OnSystemEvent(listener);
167 reply.WriteInt32(ret);
168 return ERR_NONE;
169 }
170
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)171 int32_t ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
172 {
173 std::string event = data.ReadString();
174 int param = data.ReadInt32();
175 SCLOCK_HILOGD("event=%{public}s, param=%{public}d", event.c_str(), param);
176 int32_t retCode = SendScreenLockEvent(event, param);
177 reply.WriteInt32(retCode);
178 return ERR_NONE;
179 }
180
OnLockScreen(MessageParcel & data,MessageParcel & reply)181 int32_t ScreenLockManagerStub::OnLockScreen(MessageParcel &data, MessageParcel &reply)
182 {
183 int32_t useId = data.ReadInt32();
184 int32_t retCode = Lock(useId);
185 reply.WriteInt32(retCode);
186 return ERR_NONE;
187 }
188 } // namespace ScreenLock
189 } // namespace OHOS