• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SCREEN_LOCKED:
41             return OnIsScreenLocked(data, reply);
42         case IS_SECURE_MODE:
43             return OnGetSecure(data, reply);
44         case REQUEST_UNLOCK:
45             OnRequestUnlock(data, reply);
46             return 0;
47         case REQUEST_LOCK:
48             OnRequestLock(data, reply);
49             return 0;
50         case SEND_SCREENLOCK_EVENT:
51             result = OnSendScreenLockEvent(data, reply);
52             break;
53         case ONSYSTEMEVENT:
54             result = OnScreenLockOn(data, reply);
55             break;
56         default:
57             SCLOCK_HILOGE("Default value received, check needed.");
58             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59     }
60     return result;
61 }
62 
OnIsScreenLocked(Parcel & data,Parcel & reply)63 bool ScreenLockManagerStub::OnIsScreenLocked(Parcel &data, Parcel &reply)
64 {
65     bool result = IsScreenLocked();
66     if (!reply.WriteBool(result)) {
67         SCLOCK_HILOGE("WriteBool failed");
68         return false;
69     }
70     return true;
71 }
72 
OnGetSecure(Parcel & data,Parcel & reply)73 bool ScreenLockManagerStub::OnGetSecure(Parcel &data, Parcel &reply)
74 {
75     bool result = GetSecure();
76     if (!reply.WriteBool(result)) {
77         SCLOCK_HILOGE("WriteBool failed");
78         return false;
79     }
80     return true;
81 }
82 
OnRequestUnlock(MessageParcel & data,MessageParcel & reply)83 void ScreenLockManagerStub::OnRequestUnlock(MessageParcel &data, MessageParcel &reply)
84 {
85     sptr<IRemoteObject> remote = data.ReadRemoteObject();
86     if (remote == nullptr) {
87         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
88         reply.WriteInt32(E_SCREENLOCK_NULLPTR);
89         return;
90     }
91     sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
92     if (listener.GetRefPtr() == nullptr) {
93         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
94         reply.WriteInt32(E_SCREENLOCK_NULLPTR);
95         return;
96     }
97     int32_t status = RequestUnlock(listener);
98     reply.WriteInt32(status);
99 }
100 
OnRequestLock(MessageParcel & data,MessageParcel & reply)101 void ScreenLockManagerStub::OnRequestLock(MessageParcel &data, MessageParcel &reply)
102 {
103     sptr<IRemoteObject> remote = data.ReadRemoteObject();
104     if (remote == nullptr) {
105         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
106         reply.WriteInt32(E_SCREENLOCK_NULLPTR);
107         return;
108     }
109     sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
110     if (listener.GetRefPtr() == nullptr) {
111         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
112         reply.WriteInt32(E_SCREENLOCK_NULLPTR);
113         return;
114     }
115     int32_t status = RequestLock(listener);
116     reply.WriteInt32(status);
117 }
118 
OnScreenLockOn(MessageParcel & data,MessageParcel & reply)119 int32_t ScreenLockManagerStub::OnScreenLockOn(MessageParcel &data, MessageParcel &reply)
120 {
121     sptr<IRemoteObject> remote = data.ReadRemoteObject();
122     if (remote == nullptr) {
123         SCLOCK_HILOGE("ScreenLockManagerStub remote is nullptr");
124         if (!reply.WriteInt32(E_SCREENLOCK_NULLPTR)) {
125             return -1;
126         }
127         return 0;
128     }
129     sptr<ScreenLockSystemAbilityInterface> listener = iface_cast<ScreenLockSystemAbilityInterface>(remote);
130     if (listener.GetRefPtr() == nullptr) {
131         SCLOCK_HILOGE("ScreenLockManagerStub listener is null");
132         if (!reply.WriteInt32(E_SCREENLOCK_NULLPTR)) {
133             return -1;
134         }
135         return 0;
136     }
137     int32_t ret = OnSystemEvent(listener);
138     if (!reply.WriteInt32(ret)) {
139         SCLOCK_HILOGE("ScreenLockManagerStub write int32 failed.");
140         return -1;
141     }
142     return ret;
143 }
144 
OnSendScreenLockEvent(MessageParcel & data,MessageParcel & reply)145 int32_t ScreenLockManagerStub::OnSendScreenLockEvent(MessageParcel &data, MessageParcel &reply)
146 {
147     std::string event = data.ReadString();
148     int param = data.ReadInt32();
149     SCLOCK_HILOGD("event=%{public}s ", event.c_str());
150     SCLOCK_HILOGD("param=%{public}d ", param);
151     int32_t retCode = SendScreenLockEvent(event, param);
152     reply.WriteInt32(retCode);
153     return retCode;
154 }
155 } // namespace ScreenLock
156 } // namespace OHOS