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 default:
68 SCLOCK_HILOGE("Default value received, check needed.");
69 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
70 }
71 return ERR_NONE;
72 }
73
OnIsLocked(Parcel & data,Parcel & reply)74 int32_t ScreenLockManagerStub::OnIsLocked(Parcel &data, Parcel &reply)
75 {
76 bool isLocked = false;
77 int32_t ret = IsLocked(isLocked);
78 reply.WriteInt32(ret);
79 if (ret == E_SCREENLOCK_OK) {
80 reply.WriteBool(isLocked);
81 }
82 return ERR_NONE;
83 }
84
OnIsScreenLocked(Parcel & data,Parcel & reply)85 int32_t ScreenLockManagerStub::OnIsScreenLocked(Parcel &data, Parcel &reply)
86 {
87 bool isScreenLocked = IsScreenLocked();
88 reply.WriteBool(isScreenLocked);
89 return ERR_NONE;
90 }
91
OnGetSecure(Parcel & data,Parcel & reply)92 int32_t ScreenLockManagerStub::OnGetSecure(Parcel &data, Parcel &reply)
93 {
94 bool result = GetSecure();
95 reply.WriteBool(result);
96 SCLOCK_HILOGD("GetSecure result = %{public}d", result);
97 return ERR_NONE;
98 }
99
OnUnlock(MessageParcel & data,MessageParcel & reply)100 int32_t ScreenLockManagerStub::OnUnlock(MessageParcel &data, MessageParcel &reply)
101 {
102 sptr<IRemoteObject> remote = data.ReadRemoteObject();
103 if (remote == nullptr) {
104 SCLOCK_HILOGE("remote is nullptr");
105 return ERR_INVALID_DATA;
106 }
107 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
108 if (listener.GetRefPtr() == nullptr) {
109 SCLOCK_HILOGE("listener is null");
110 return ERR_INVALID_DATA;
111 }
112 int32_t ret = Unlock(listener);
113 reply.WriteInt32(ret);
114 return ERR_NONE;
115 }
116
OnUnlockScreen(MessageParcel & data,MessageParcel & reply)117 int32_t ScreenLockManagerStub::OnUnlockScreen(MessageParcel &data, MessageParcel &reply)
118 {
119 sptr<IRemoteObject> remote = data.ReadRemoteObject();
120 if (remote == nullptr) {
121 SCLOCK_HILOGE("remote is nullptr");
122 return ERR_INVALID_DATA;
123 }
124 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
125 if (listener.GetRefPtr() == nullptr) {
126 SCLOCK_HILOGE("listener is null");
127 return ERR_INVALID_DATA;
128 }
129 int32_t ret = UnlockScreen(listener);
130 reply.WriteInt32(ret);
131 return ERR_NONE;
132 }
133
OnLock(MessageParcel & data,MessageParcel & reply)134 int32_t ScreenLockManagerStub::OnLock(MessageParcel &data, MessageParcel &reply)
135 {
136 sptr<IRemoteObject> remote = data.ReadRemoteObject();
137 if (remote == nullptr) {
138 SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
139 return ERR_INVALID_DATA;
140 }
141 sptr<ScreenLockCallbackInterface> listener = iface_cast<ScreenLockCallbackInterface>(remote);
142 if (listener.GetRefPtr() == nullptr) {
143 SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
144 return ERR_INVALID_DATA;
145 }
146 int32_t status = Lock(listener);
147 reply.WriteInt32(status);
148 return ERR_NONE;
149 }
150
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)151 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
152 {
153 sptr<IRemoteObject> remote = data.ReadRemoteObject();
154 if (remote == nullptr) {
155 SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
156 return ERR_INVALID_DATA;
157 }
158 sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
159 if (listener.GetRefPtr() == nullptr) {
160 SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
161 return ERR_INVALID_DATA;
162 }
163 int32_t ret = OnSystemEvent(listener);
164 reply.WriteInt32(ret);
165 return ERR_NONE;
166 }
167
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)168 int32_t ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
169 {
170 std::string event = data.ReadString();
171 int param = data.ReadInt32();
172 SCLOCK_HILOGD("event=%{public}s ", event.c_str());
173 SCLOCK_HILOGD("param=%{public}d ", param);
174 int32_t retCode = SendScreenLockEvent(event, param);
175 reply.WriteInt32(retCode);
176 return ERR_NONE;
177 }
178 } // namespace ScreenLock
179 } // namespace OHOS