1 /*
2 * Copyright (c) 2023 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 "session/container/include/zidl/window_event_channel_stub.h"
17
18 #include <axis_event.h>
19 #include <ipc_types.h>
20 #include <key_event.h>
21 #include <pointer_event.h>
22
23 #include "window_manager_hilog.h"
24
25 namespace OHOS::Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowEventChannelStub"};
28 }
29
30 const std::map<uint32_t, WindowEventChannelStubFunc> WindowEventChannelStub::stubFuncMap_{
31 std::make_pair(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_KEY_EVENT),
32 &WindowEventChannelStub::HandleTransferKeyEvent),
33 std::make_pair(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_POINTER_EVENT),
34 &WindowEventChannelStub::HandleTransferPointerEvent),
35 std::make_pair(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_FOCUS_ACTIVE_EVENT),
36 &WindowEventChannelStub::HandleTransferFocusActiveEvent),
37 std::make_pair(static_cast<uint32_t>(WindowEventChannelMessage::TRANS_ID_TRANSFER_FOCUS_STATE_EVENT),
38 &WindowEventChannelStub::HandleTransferFocusStateEvent)
39 };
40
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int WindowEventChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
42 MessageParcel &reply, MessageOption &option)
43 {
44 WLOGFD("Window event channel on remote request!, code: %{public}u", code);
45 if (data.ReadInterfaceToken() != GetDescriptor()) {
46 WLOGFE("Failed to check interface token!");
47 return ERR_INVALID_STATE;
48 }
49
50 const auto func = stubFuncMap_.find(code);
51 if (func == stubFuncMap_.end()) {
52 WLOGFE("Failed to find function handler!");
53 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54 }
55
56 return (this->*(func->second))(data, reply);
57 }
58
HandleTransferKeyEvent(MessageParcel & data,MessageParcel & reply)59 int WindowEventChannelStub::HandleTransferKeyEvent(MessageParcel& data, MessageParcel& reply)
60 {
61 WLOGFD("TransferKeyEvent!");
62 auto keyEvent = MMI::KeyEvent::Create();
63 if (keyEvent == nullptr) {
64 WLOGFE("Failed to create key event!");
65 return ERR_INVALID_DATA;
66 }
67 if (!keyEvent->ReadFromParcel(data)) {
68 WLOGFE("Read Key Event failed");
69 return ERR_INVALID_DATA;
70 }
71 bool isConsumed = false;
72 WSError errCode = TransferKeyEventForConsumed(keyEvent, isConsumed);
73
74 reply.WriteBool(isConsumed);
75 reply.WriteUint32(static_cast<uint32_t>(errCode));
76 return ERR_NONE;
77 }
78
HandleTransferPointerEvent(MessageParcel & data,MessageParcel & reply)79 int WindowEventChannelStub::HandleTransferPointerEvent(MessageParcel& data, MessageParcel& reply)
80 {
81 WLOGFD("TransferPointerEvent!");
82 auto pointerEvent = MMI::PointerEvent::Create();
83 if (pointerEvent == nullptr) {
84 WLOGFE("Failed to create pointer event!");
85 return ERR_INVALID_DATA;
86 }
87 if (!pointerEvent->ReadFromParcel(data)) {
88 WLOGFE("Read Pointer Event failed");
89 return ERR_INVALID_DATA;
90 }
91 WSError errCode = TransferPointerEvent(pointerEvent);
92 reply.WriteUint32(static_cast<uint32_t>(errCode));
93 return ERR_NONE;
94 }
95
HandleTransferFocusActiveEvent(MessageParcel & data,MessageParcel & reply)96 int WindowEventChannelStub::HandleTransferFocusActiveEvent(MessageParcel& data, MessageParcel& reply)
97 {
98 bool isFocusActive = data.ReadBool();
99 WSError errCode = TransferFocusActiveEvent(isFocusActive);
100 reply.WriteUint32(static_cast<uint32_t>(errCode));
101 return ERR_NONE;
102 }
103
HandleTransferFocusStateEvent(MessageParcel & data,MessageParcel & reply)104 int WindowEventChannelStub::HandleTransferFocusStateEvent(MessageParcel& data, MessageParcel& reply)
105 {
106 bool focusState = data.ReadBool();
107 WSError errCode = TransferFocusState(focusState);
108 reply.WriteUint32(static_cast<uint32_t>(errCode));
109 return ERR_NONE;
110 }
111 }
112