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_proxy.h"
17 #include "session/container/include/zidl/window_event_ipc_interface_code.h"
18 
19 #include <axis_event.h>
20 #include <ipc_types.h>
21 #include <key_event.h>
22 #include <message_option.h>
23 #include <message_parcel.h>
24 #include <pointer_event.h>
25 #include <vector>
26 
27 #include "parcel/accessibility_element_info_parcel.h"
28 #include "window_manager_hilog.h"
29 
30 namespace OHOS::Rosen {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowEventChannelProxy"};
33 }
34 
TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)35 WSError WindowEventChannelProxy::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
36 {
37     MessageParcel data;
38     MessageParcel reply;
39     MessageOption option(MessageOption::TF_ASYNC);
40     if (!data.WriteInterfaceToken(GetDescriptor())) {
41         TLOGE(WmsLogTag::WMS_EVENT, "WriteInterfaceToken failed");
42         return WSError::WS_ERROR_IPC_FAILED;
43     }
44 
45     if (!keyEvent->WriteToParcel(data)) {
46         TLOGE(WmsLogTag::WMS_EVENT, "Failed to write key event");
47         return WSError::WS_ERROR_IPC_FAILED;
48     }
49     bool isPreImeEvent = false;
50     if (!data.WriteBool(isPreImeEvent)) {
51         TLOGE(WmsLogTag::WMS_EVENT, "Write bool failed");
52         return WSError::WS_ERROR_IPC_FAILED;
53     }
54     sptr<IRemoteObject> remote = Remote();
55     if (remote == nullptr) {
56         TLOGE(WmsLogTag::WMS_EVENT, "remote is null");
57         return WSError::WS_ERROR_IPC_FAILED;
58     }
59     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_KEY_EVENT),
60         data, reply, option) != ERR_NONE) {
61         TLOGE(WmsLogTag::WMS_EVENT, "SendRequest failed");
62         return WSError::WS_ERROR_IPC_FAILED;
63     }
64     reply.ReadBool();
65     int32_t ret = reply.ReadInt32();
66     return static_cast<WSError>(ret);
67 }
68 
TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)69 WSError WindowEventChannelProxy::TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
70 {
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option(MessageOption::TF_ASYNC);
74     if (!data.WriteInterfaceToken(GetDescriptor())) {
75         TLOGE(WmsLogTag::WMS_EVENT, "WriteInterfaceToken failed");
76         return WSError::WS_ERROR_IPC_FAILED;
77     }
78 
79     if (!pointerEvent->WriteToParcel(data)) {
80         TLOGE(WmsLogTag::WMS_EVENT, "Failed to write pointer event");
81         return WSError::WS_ERROR_IPC_FAILED;
82     }
83 
84     sptr<IRemoteObject> remote = Remote();
85     if (remote == nullptr) {
86         TLOGE(WmsLogTag::WMS_EVENT, "remote is null");
87         return WSError::WS_ERROR_IPC_FAILED;
88     }
89     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_POINTER_EVENT),
90         data, reply, option) != ERR_NONE) {
91         TLOGE(WmsLogTag::WMS_EVENT, "SendRequest failed");
92         return WSError::WS_ERROR_IPC_FAILED;
93     }
94     int32_t ret = reply.ReadInt32();
95     return static_cast<WSError>(ret);
96 }
97 
TransferBackpressedEventForConsumed(bool & isConsumed)98 WSError WindowEventChannelProxy::TransferBackpressedEventForConsumed(bool& isConsumed)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option(MessageOption::TF_SYNC);
103     if (!data.WriteInterfaceToken(GetDescriptor())) {
104         WLOGFE("WriteInterfaceToken failed");
105         return WSError::WS_ERROR_IPC_FAILED;
106     }
107 
108     sptr<IRemoteObject> remote = Remote();
109     if (remote == nullptr) {
110         WLOGFE("remote is null");
111         return WSError::WS_ERROR_IPC_FAILED;
112     }
113     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_BACKPRESSED_EVENT),
114         data, reply, option) != ERR_NONE) {
115         WLOGFE("SendRequest failed");
116         return WSError::WS_ERROR_IPC_FAILED;
117     }
118     isConsumed = reply.ReadBool();
119     int32_t ret = reply.ReadInt32();
120     return static_cast<WSError>(ret);
121 }
122 
TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed,bool isPreImeEvent)123 WSError WindowEventChannelProxy::TransferKeyEventForConsumed(
124     const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed, bool isPreImeEvent)
125 {
126     MessageParcel data;
127     MessageParcel reply;
128     MessageOption option(MessageOption::TF_SYNC);
129     if (!data.WriteInterfaceToken(GetDescriptor())) {
130         TLOGE(WmsLogTag::WMS_EVENT, "WriteInterfaceToken failed");
131         return WSError::WS_ERROR_IPC_FAILED;
132     }
133 
134     if (!keyEvent->WriteToParcel(data)) {
135         TLOGE(WmsLogTag::WMS_EVENT, "Failed to write key event");
136         return WSError::WS_ERROR_IPC_FAILED;
137     }
138     if (!data.WriteBool(isPreImeEvent)) {
139         TLOGE(WmsLogTag::WMS_EVENT, "Write bool failed");
140         return WSError::WS_ERROR_IPC_FAILED;
141     }
142     sptr<IRemoteObject> remote = Remote();
143     if (remote == nullptr) {
144         TLOGE(WmsLogTag::WMS_EVENT, "remote is null");
145         return WSError::WS_ERROR_IPC_FAILED;
146     }
147     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_KEY_EVENT),
148         data, reply, option) != ERR_NONE) {
149         TLOGE(WmsLogTag::WMS_EVENT, "SendRequest failed");
150         return WSError::WS_ERROR_IPC_FAILED;
151     }
152     isConsumed = reply.ReadBool();
153     int32_t ret = reply.ReadInt32();
154     return static_cast<WSError>(ret);
155 }
156 
TransferKeyEventForConsumedAsync(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool isPreImeEvent,const sptr<IRemoteObject> & listener)157 WSError WindowEventChannelProxy::TransferKeyEventForConsumedAsync(
158     const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool isPreImeEvent, const sptr<IRemoteObject>& listener)
159 {
160     MessageParcel data;
161     MessageParcel reply;
162     MessageOption option(MessageOption::TF_ASYNC);
163     if (!data.WriteInterfaceToken(GetDescriptor())) {
164         TLOGE(WmsLogTag::WMS_EVENT, "WriteInterfaceToken failed");
165         return WSError::WS_ERROR_IPC_FAILED;
166     }
167 
168     if (!keyEvent->WriteToParcel(data)) {
169         TLOGE(WmsLogTag::WMS_EVENT, "Failed to write key event");
170         return WSError::WS_ERROR_IPC_FAILED;
171     }
172 
173     if (!data.WriteBool(isPreImeEvent)) {
174         TLOGE(WmsLogTag::WMS_EVENT, "Write isPreImeEvent failed");
175         return WSError::WS_ERROR_IPC_FAILED;
176     }
177 
178     if (!data.WriteRemoteObject(listener)) {
179         TLOGE(WmsLogTag::WMS_EVENT, "WriteRemoteObject listener failed");
180         return WSError::WS_ERROR_IPC_FAILED;
181     }
182 
183     sptr<IRemoteObject> remote = Remote();
184     if (remote == nullptr) {
185         TLOGE(WmsLogTag::WMS_UIEXT, "remote is null");
186         return WSError::WS_ERROR_IPC_FAILED;
187     }
188     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_KEY_EVENT_ASYNC),
189         data, reply, option) != ERR_NONE) {
190         TLOGE(WmsLogTag::WMS_EVENT, "SendRequest failed");
191         return WSError::WS_ERROR_IPC_FAILED;
192     }
193     int32_t ret = reply.ReadInt32();
194     return static_cast<WSError>(ret);
195 }
196 
TransferFocusActiveEvent(bool isFocusActive)197 WSError WindowEventChannelProxy::TransferFocusActiveEvent(bool isFocusActive)
198 {
199     MessageParcel data;
200     MessageParcel reply;
201     MessageOption option(MessageOption::TF_ASYNC);
202     if (!data.WriteInterfaceToken(GetDescriptor())) {
203         WLOGFE("WriteInterfaceToken failed");
204         return WSError::WS_ERROR_IPC_FAILED;
205     }
206     if (!data.WriteBool(isFocusActive)) {
207         WLOGFE("Write bool failed");
208         return WSError::WS_ERROR_IPC_FAILED;
209     }
210     sptr<IRemoteObject> remote = Remote();
211     if (remote == nullptr) {
212         WLOGFE("remote is null");
213         return WSError::WS_ERROR_IPC_FAILED;
214     }
215     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_FOCUS_ACTIVE_EVENT),
216         data, reply, option) != ERR_NONE) {
217         WLOGFE("SendRequest failed");
218         return WSError::WS_ERROR_IPC_FAILED;
219     }
220     int32_t ret = reply.ReadInt32();
221     return static_cast<WSError>(ret);
222 }
223 
TransferFocusState(bool focusState)224 WSError WindowEventChannelProxy::TransferFocusState(bool focusState)
225 {
226     MessageParcel data;
227     MessageParcel reply;
228     MessageOption option(MessageOption::TF_ASYNC);
229     if (!data.WriteInterfaceToken(GetDescriptor())) {
230         WLOGFE("WriteInterfaceToken failed");
231         return WSError::WS_ERROR_IPC_FAILED;
232     }
233     if (!data.WriteBool(focusState)) {
234         WLOGFE("Write focusState failed");
235         return WSError::WS_ERROR_IPC_FAILED;
236     }
237     sptr<IRemoteObject> remote = Remote();
238     if (remote == nullptr) {
239         WLOGFE("remote is null");
240         return WSError::WS_ERROR_IPC_FAILED;
241     }
242     if (remote->SendRequest(static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_FOCUS_STATE_EVENT),
243         data, reply, option) != ERR_NONE) {
244         WLOGFE("SendRequest failed");
245         return WSError::WS_ERROR_IPC_FAILED;
246     }
247     int32_t ret = reply.ReadInt32();
248     return static_cast<WSError>(ret);
249 }
250 
TransferAccessibilityHoverEvent(float pointX,float pointY,int32_t sourceType,int32_t eventType,int64_t timeMs)251 WSError WindowEventChannelProxy::TransferAccessibilityHoverEvent(float pointX, float pointY, int32_t sourceType,
252     int32_t eventType, int64_t timeMs)
253 {
254     MessageParcel data;
255     MessageParcel reply;
256     MessageOption option(MessageOption::TF_ASYNC);
257     if (!data.WriteInterfaceToken(GetDescriptor())) {
258         TLOGE(WmsLogTag::WMS_UIEXT, "WriteInterfaceToken failed");
259         return WSError::WS_ERROR_IPC_FAILED;
260     }
261     if (!data.WriteFloat(pointX) ||
262         !data.WriteFloat(pointY) ||
263         !data.WriteInt32(sourceType) ||
264         !data.WriteInt32(eventType) ||
265         !data.WriteInt64(timeMs)) {
266         TLOGE(WmsLogTag::WMS_UIEXT, "Write TransferAccessibilityHoverEvent data failed");
267         return WSError::WS_ERROR_IPC_FAILED;
268     }
269     sptr<IRemoteObject> remote = Remote();
270     if (remote == nullptr) {
271         TLOGE(WmsLogTag::WMS_UIEXT, "remote is null");
272         return WSError::WS_ERROR_IPC_FAILED;
273     }
274     if (remote->SendRequest(
275         static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_ACCESSIBILITY_HOVER_EVENT),
276         data, reply, option) != ERR_NONE) {
277         TLOGE(WmsLogTag::WMS_UIEXT, "SendRequest failed");
278         return WSError::WS_ERROR_IPC_FAILED;
279     }
280     int32_t ret = reply.ReadInt32();
281     return static_cast<WSError>(ret);
282 }
283 
TransferAccessibilityChildTreeRegister(uint32_t windowId,int32_t treeId,int64_t accessibilityId)284 WSError WindowEventChannelProxy::TransferAccessibilityChildTreeRegister(
285     uint32_t windowId, int32_t treeId, int64_t accessibilityId)
286 {
287     MessageParcel data;
288     MessageParcel reply;
289     MessageOption option(MessageOption::TF_ASYNC);
290     if (!data.WriteInterfaceToken(GetDescriptor())) {
291         TLOGE(WmsLogTag::WMS_UIEXT, "WriteInterfaceToken failed");
292         return WSError::WS_ERROR_IPC_FAILED;
293     }
294     if (!data.WriteUint32(windowId)) {
295         TLOGE(WmsLogTag::WMS_UIEXT, "write windowId fail, action error");
296         return WSError::WS_ERROR_IPC_FAILED;
297     }
298     if (!data.WriteInt32(treeId)) {
299         TLOGE(WmsLogTag::WMS_UIEXT, "write treeId fail, action error");
300         return WSError::WS_ERROR_IPC_FAILED;
301     }
302     if (!data.WriteInt64(accessibilityId)) {
303         TLOGE(WmsLogTag::WMS_UIEXT, "write accessibilityId fail, action error");
304         return WSError::WS_ERROR_IPC_FAILED;
305     }
306 
307     sptr<IRemoteObject> remote = Remote();
308     if (remote == nullptr) {
309         TLOGE(WmsLogTag::WMS_UIEXT, "remote is null");
310         return WSError::WS_ERROR_IPC_FAILED;
311     }
312     int error = remote->SendRequest(
313         static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_ACCESSIBILITY_CHILD_TREE_REGISTER),
314         data, reply, option);
315     if (error != ERR_OK) {
316         TLOGE(WmsLogTag::WMS_UIEXT, "failed to SendRequest: %{public}d", error);
317         return WSError::WS_ERROR_IPC_FAILED;
318     }
319     int32_t ret = reply.ReadInt32();
320     return static_cast<WSError>(ret);
321 }
322 
TransferAccessibilityChildTreeUnregister()323 WSError WindowEventChannelProxy::TransferAccessibilityChildTreeUnregister()
324 {
325     MessageParcel data;
326     MessageParcel reply;
327     MessageOption option(MessageOption::TF_ASYNC);
328     if (!data.WriteInterfaceToken(GetDescriptor())) {
329         TLOGE(WmsLogTag::WMS_UIEXT, "WriteInterfaceToken failed");
330         return WSError::WS_ERROR_IPC_FAILED;
331     }
332     sptr<IRemoteObject> remote = Remote();
333     if (remote == nullptr) {
334         TLOGE(WmsLogTag::WMS_UIEXT, "remote is null");
335         return WSError::WS_ERROR_IPC_FAILED;
336     }
337     int error = remote->SendRequest(
338         static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_ACCESSIBILITY_CHILD_TREE_UNREGISTER),
339         data, reply, option);
340     if (error != ERR_OK) {
341         TLOGE(WmsLogTag::WMS_UIEXT, "failed to SendRequest: %{public}d", error);
342         return WSError::WS_ERROR_IPC_FAILED;
343     }
344     int32_t ret = reply.ReadInt32();
345     return static_cast<WSError>(ret);
346 }
347 
TransferAccessibilityDumpChildInfo(const std::vector<std::string> & params,std::vector<std::string> & info)348 WSError WindowEventChannelProxy::TransferAccessibilityDumpChildInfo(
349     const std::vector<std::string>& params, std::vector<std::string>& info)
350 {
351     MessageParcel data;
352     MessageParcel reply;
353     MessageOption option;
354     if (!data.WriteInterfaceToken(GetDescriptor())) {
355         TLOGE(WmsLogTag::WMS_UIEXT, "WriteInterfaceToken failed");
356         return WSError::WS_ERROR_IPC_FAILED;
357     }
358     if (!data.WriteStringVector(params)) {
359         TLOGE(WmsLogTag::WMS_UIEXT, "failed to write params");
360         return WSError::WS_ERROR_IPC_FAILED;
361     }
362     sptr<IRemoteObject> remote = Remote();
363     if (remote == nullptr) {
364         TLOGE(WmsLogTag::WMS_UIEXT, "remote is null");
365         return WSError::WS_ERROR_IPC_FAILED;
366     }
367     int error = remote->SendRequest(
368         static_cast<uint32_t>(WindowEventInterfaceCode::TRANS_ID_TRANSFER_ACCESSIBILITY_DUMP_CHILD_INFO),
369         data, reply, option);
370     if (error != ERR_OK) {
371         TLOGE(WmsLogTag::WMS_UIEXT, "failed to SendRequest: %{public}d", error);
372         return WSError::WS_ERROR_IPC_FAILED;
373     }
374     if (!reply.ReadStringVector(&info)) {
375         TLOGE(WmsLogTag::WMS_UIEXT, "Read reply info failed");
376         return WSError::WS_ERROR_IPC_FAILED;
377     }
378     int32_t ret = reply.ReadInt32();
379     return static_cast<WSError>(ret);
380 }
381 } // namespace OHOS::Rosen
382