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