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 <cinttypes>
19
20 #include "ipc_skeleton.h"
21 #include "parcel.h"
22
23 #include "sclock_log.h"
24 #include "screenlock_common.h"
25 #include "screenlock_system_ability_interface.h"
26
27 namespace OHOS {
28 namespace ScreenLock {
29 using namespace OHOS::HiviewDFX;
30
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int32_t ScreenLockManagerStub::OnRemoteRequest(
32 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
33 {
34 SCLOCK_HILOGD("OnRemoteRequest started, code = %{public}d", code);
35 int32_t result = -1;
36 auto descriptorToken = data.ReadInterfaceToken();
37 if (descriptorToken != GetDescriptor()) {
38 SCLOCK_HILOGE("Remote descriptor not the same as local descriptor.");
39 return E_SCREENLOCK_TRANSACT_ERROR;
40 }
41 switch (code) {
42 case IS_SCREEN_LOCKED:
43 return OnIsScreenLocked(data, reply);
44 case IS_SECURE_MODE:
45 return OnGetSecure(data, reply);
46 case REQUEST_UNLOCK:
47 OnRequestUnlock(data, reply);
48 return 0;
49 case SEND_SCREENLOCK_EVENT:
50 result = OnSendScreenLockEvent(data, reply);
51 break;
52 case ON:
53 result = OnScreenLockOn(data, reply);
54 break;
55 case OFF:
56 result = OnScreenLockOff(data, reply);
57 break;
58 case TEST_SET_SCREENLOCKED:
59 result = OnTest_SetScreenLocked(data, reply);
60 break;
61 case TEST_RUNTIME_NOTIFY:
62 result = OnTest_RuntimeNotify(data, reply);
63 break;
64 case TEST_GET_RUNTIME_STATE:
65 result = OnTest_GetRuntimeState(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 result;
72 }
73
OnIsScreenLocked(Parcel & data,Parcel & reply)74 bool ScreenLockManagerStub::OnIsScreenLocked(Parcel &data, Parcel &reply)
75 {
76 bool result = IsScreenLocked();
77 if (!reply.WriteBool(result)) {
78 SCLOCK_HILOGE("WriteBool failed");
79 return false;
80 }
81 return true;
82 }
83
OnGetSecure(Parcel & data,Parcel & reply)84 bool ScreenLockManagerStub::OnGetSecure(Parcel &data, Parcel &reply)
85 {
86 bool result = GetSecure();
87 if (!reply.WriteBool(result)) {
88 SCLOCK_HILOGE("WriteBool failed");
89 return false;
90 }
91 return true;
92 }
93
OnRequestUnlock(MessageParcel & data,MessageParcel & reply)94 void ScreenLockManagerStub::OnRequestUnlock(MessageParcel &data, MessageParcel &reply)
95 {
96 sptr<IRemoteObject> remote = data.ReadRemoteObject();
97 SCLOCK_HILOGD("ScreenLockManagerStub::OnRequestUnlock addr=%{public}p", remote.GetRefPtr());
98 if (remote == nullptr) {
99 SCLOCK_HILOGD("ScreenLockManagerStub::OnRequestUnlock remote is nullptr");
100 if (!reply.WriteInt32(ERR_NONE)) {
101 return;
102 }
103 return;
104 }
105 sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
106 SCLOCK_HILOGD("ScreenLockManagerStub::OnRequestUnlock addr=%{public}p", listener.GetRefPtr());
107 if (listener.GetRefPtr() == nullptr) {
108 SCLOCK_HILOGD("ScreenLockManagerStub::OnRequestUnlock listener is null");
109 return;
110 }
111 RequestUnlock(listener);
112 return;
113 }
114
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)115 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
116 {
117 std::string type = data.ReadString();
118 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOn type=%{public}s ", type.c_str());
119 if (type.empty()) {
120 SCLOCK_HILOGE("ScreenLockManagerStub::OnScreenLockOn type is null.");
121 return false;
122 }
123 sptr<IRemoteObject> remote = data.ReadRemoteObject();
124 if (remote == nullptr) {
125 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOn remote is nullptr");
126 if (!reply.WriteInt32(ERR_NONE)) {
127 return -1;
128 }
129 return 0;
130 }
131 sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
132 if (listener.GetRefPtr() == nullptr) {
133 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOn listener is null");
134 return -1;
135 }
136 bool status = On(listener, type);
137 int32_t ret = (status == true) ? 0 : -1;
138 if (!reply.WriteInt32(ret)) {
139 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOn 4444");
140 return -1;
141 }
142 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOn out");
143 return 0;
144 }
145
OnScreenLockOff(MessageParcel & data,MessageParcel & reply)146 int32_t ScreenLockManagerStub::OnScreenLockOff(MessageParcel &data, MessageParcel &reply)
147 {
148 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOff in");
149 std::string type = data.ReadString();
150 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOff type=%{public}s ", type.c_str());
151 bool status = Off(type);
152 int32_t ret = (status == true) ? 0 : -1;
153 if (!reply.WriteInt32(ret)) {
154 return -1;
155 }
156 SCLOCK_HILOGD("ScreenLockManagerStub::OnScreenLockOff out");
157 return 0;
158 }
159
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)160 bool ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
161 {
162 std::string event = data.ReadString();
163 int param = data.ReadInt32();
164 SCLOCK_HILOGD("event=%{public}s ", event.c_str());
165 SCLOCK_HILOGD("param=%{public}d ", param);
166 bool flag = SendScreenLockEvent(event, param);
167 reply.WriteBool(flag);
168 return flag;
169 }
170
OnTest_SetScreenLocked(MessageParcel & data,MessageParcel & reply)171 bool ScreenLockManagerStub::OnTest_SetScreenLocked(MessageParcel &data, MessageParcel &reply)
172 {
173 SCLOCK_HILOGD("ScreenLockManagerStub Test_SetScreenLocked started.");
174 bool isScreenlocked = data.ReadBool();
175 bool flag = Test_SetScreenLocked(isScreenlocked);
176 reply.WriteBool(flag);
177 return flag;
178 }
OnTest_RuntimeNotify(MessageParcel & data,MessageParcel & reply)179 bool ScreenLockManagerStub::OnTest_RuntimeNotify(MessageParcel &data, MessageParcel &reply)
180 {
181 std::string event = data.ReadString();
182 int param = data.ReadInt32();
183 SCLOCK_HILOGD("ScreenLockManagerStub OnTest_RuntimeNotify started. event=%{public}s", event.c_str());
184 SCLOCK_HILOGD("ScreenLockManagerStub OnTest_RuntimeNotify started. param=%{public}d", param);
185 bool flag = Test_RuntimeNotify(event, param);
186 reply.WriteBool(flag);
187 return flag;
188 }
OnTest_GetRuntimeState(MessageParcel & data,MessageParcel & reply)189 int32_t ScreenLockManagerStub::OnTest_GetRuntimeState(MessageParcel &data, MessageParcel &reply)
190 {
191 std::string event = data.ReadString();
192 int flag = Test_GetRuntimeState(event);
193 reply.WriteInt32(flag);
194 return flag;
195 }
196 } // namespace ScreenLock
197 } // namespace OHOS