1 /*
2  * Copyright (C) 2021 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 "keyevent_consumer_stub.h"
17 
18 #include "global.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_skeleton.h"
21 #include "ipc_types.h"
22 #include "itypes_util.h"
23 #include "message.h"
24 
25 namespace OHOS {
26 namespace MiscServices {
KeyEventConsumerStub(KeyEventCallback callback,std::shared_ptr<MMI::KeyEvent> keyEvent)27 KeyEventConsumerStub::KeyEventConsumerStub(KeyEventCallback callback, std::shared_ptr<MMI::KeyEvent> keyEvent)
28     : callback_(callback), keyEvent_(keyEvent)
29 {
30 }
31 
~KeyEventConsumerStub()32 KeyEventConsumerStub::~KeyEventConsumerStub() {}
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int32_t KeyEventConsumerStub::OnRemoteRequest(
35     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
36 {
37     IMSA_HILOGD("KeyEventConsumerStub, code: %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
38         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
39     auto descriptorToken = data.ReadInterfaceToken();
40     if (descriptorToken != IKeyEventConsumer::GetDescriptor()) {
41         IMSA_HILOGE("KeyEventConsumerStub descriptor error!");
42         return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
43     }
44     if (code < KEY_EVENT_CONSUMER_BEGIN || code >= KEY_EVENT_CONSUMER_CMD_END) {
45         IMSA_HILOGE("code error, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
46             IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
47         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
48     }
49     return (this->*HANDLERS[code])(data, reply);
50 }
51 
OnKeyEventResultOnRemote(MessageParcel & data,MessageParcel & reply)52 int32_t KeyEventConsumerStub::OnKeyEventResultOnRemote(MessageParcel &data, MessageParcel &reply)
53 {
54     bool isConsumed = false;
55     if (!ITypesUtil::Unmarshal(data, isConsumed)) {
56         IMSA_HILOGE("failed to read message parcel!");
57         return ErrorCode::ERROR_EX_PARCELABLE;
58     }
59     if (callback_ != nullptr) {
60         callback_(keyEvent_, isConsumed);
61     } else {
62         IMSA_HILOGE("callback is nullptr, isConsumed: %{public}d!", isConsumed);
63     }
64     return reply.WriteInt32(ErrorCode::NO_ERROR) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
65 }
66 
OnKeyEventResult(bool isConsumed)67 int32_t KeyEventConsumerStub::OnKeyEventResult(bool isConsumed)
68 {
69     return ErrorCode::NO_ERROR;
70 }
71 } // namespace MiscServices
72 } // namespace OHOS
73