• 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 "accessible_ability_channel_proxy.h"
17 
18 #include <cinttypes>
19 
20 #include "accessibility_element_info_parcel.h"
21 #include "accessibility_gesture_inject_path_parcel.h"
22 #include "accessibility_window_info_parcel.h"
23 #include "hilog_wrapper.h"
24 
25 namespace OHOS {
26 namespace Accessibility {
AccessibleAbilityChannelProxy(const sptr<IRemoteObject> & object)27 AccessibleAbilityChannelProxy::AccessibleAbilityChannelProxy(
28     const sptr<IRemoteObject> &object): IRemoteProxy<IAccessibleAbilityChannel>(object)
29 {
30 }
31 
WriteInterfaceToken(MessageParcel & data)32 bool AccessibleAbilityChannelProxy::WriteInterfaceToken(MessageParcel &data)
33 {
34     HILOG_DEBUG();
35 
36     if (!data.WriteInterfaceToken(AccessibleAbilityChannelProxy::GetDescriptor())) {
37         HILOG_ERROR("write interface token failed");
38         return false;
39     }
40     return true;
41 }
42 
SendTransactCmd(IAccessibleAbilityChannel::Message code,MessageParcel & data,MessageParcel & reply,MessageOption & option)43 bool AccessibleAbilityChannelProxy::SendTransactCmd(IAccessibleAbilityChannel::Message code,
44     MessageParcel &data, MessageParcel &reply, MessageOption &option)
45 {
46     HILOG_DEBUG();
47 
48     sptr<IRemoteObject> remote = Remote();
49     if (!remote) {
50         HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
51         return false;
52     }
53     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
54     if (result != NO_ERROR) {
55         HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
56         return false;
57     }
58     return true;
59 }
60 
SearchElementInfoByAccessibilityId(const int32_t accessibilityWindowId,const int32_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode)61 RetError AccessibleAbilityChannelProxy::SearchElementInfoByAccessibilityId(const int32_t accessibilityWindowId,
62     const int32_t elementId, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback,
63     const int32_t mode)
64 {
65     HILOG_DEBUG();
66     if (!callback) {
67         HILOG_ERROR("callback is nullptr.");
68         return RET_ERR_INVALID_PARAM;
69     }
70 
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option;
74 
75     if (!WriteInterfaceToken(data)) {
76         return RET_ERR_IPC_FAILED;
77     }
78     if (!data.WriteInt32(accessibilityWindowId)) {
79         HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
80         return RET_ERR_IPC_FAILED;
81     }
82     if (!data.WriteInt32(elementId)) {
83         HILOG_ERROR("elementId write error: %{public}d, ", elementId);
84         return RET_ERR_IPC_FAILED;
85     }
86     if (!data.WriteInt32(requestId)) {
87         HILOG_ERROR("requestId write error: %{public}d, ", requestId);
88         return RET_ERR_IPC_FAILED;
89     }
90     if (!data.WriteRemoteObject(callback->AsObject())) {
91         HILOG_ERROR("callback write error");
92         return RET_ERR_IPC_FAILED;
93     }
94     if (!data.WriteInt32(mode)) {
95         HILOG_ERROR("mode write error: %{public}d, ", mode);
96         return RET_ERR_IPC_FAILED;
97     }
98 
99     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFO_BY_ACCESSIBILITY_ID,
100         data, reply, option)) {
101         HILOG_ERROR("fail to find elementInfo by elementId");
102         return RET_ERR_IPC_FAILED;
103     }
104     return static_cast<RetError>(reply.ReadInt32());
105 }
106 
SearchElementInfosByText(const int32_t accessibilityWindowId,const int32_t elementId,const std::string & text,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)107 RetError AccessibleAbilityChannelProxy::SearchElementInfosByText(const int32_t accessibilityWindowId,
108     const int32_t elementId, const std::string &text, const int32_t requestId,
109     const sptr<IAccessibilityElementOperatorCallback> &callback)
110 {
111     HILOG_DEBUG();
112 
113     if (!callback) {
114         HILOG_ERROR("callback is nullptr.");
115         return RET_ERR_INVALID_PARAM;
116     }
117 
118     MessageParcel data;
119     MessageParcel reply;
120     MessageOption option;
121 
122     if (!WriteInterfaceToken(data)) {
123         return RET_ERR_IPC_FAILED;
124     }
125     if (!data.WriteInt32(accessibilityWindowId)) {
126         HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
127         return RET_ERR_IPC_FAILED;
128     }
129     if (!data.WriteInt32(elementId)) {
130         HILOG_ERROR("elementId write error: %{public}d, ", elementId);
131         return RET_ERR_IPC_FAILED;
132     }
133     if (!data.WriteString(text)) {
134         HILOG_ERROR("text write error: %{public}s, ", text.c_str());
135         return RET_ERR_IPC_FAILED;
136     }
137     if (!data.WriteInt32(requestId)) {
138         HILOG_ERROR("requestId write error: %{public}d, ", requestId);
139         return RET_ERR_IPC_FAILED;
140     }
141     if (!data.WriteRemoteObject(callback->AsObject())) {
142         HILOG_ERROR("callback write error");
143         return RET_ERR_IPC_FAILED;
144     }
145 
146     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFOS_BY_TEXT,
147         data, reply, option)) {
148         HILOG_ERROR("fail to find elementInfo by text");
149         return RET_ERR_IPC_FAILED;
150     }
151     return static_cast<RetError>(reply.ReadInt32());
152 }
153 
FindFocusedElementInfo(const int32_t accessibilityWindowId,const int32_t elementId,const int32_t focusType,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)154 RetError AccessibleAbilityChannelProxy::FindFocusedElementInfo(const int32_t accessibilityWindowId,
155     const int32_t elementId, const int32_t focusType, const int32_t requestId,
156     const sptr<IAccessibilityElementOperatorCallback> &callback)
157 {
158     HILOG_DEBUG();
159     if (!callback) {
160         HILOG_ERROR("callback is nullptr.");
161         return RET_ERR_INVALID_PARAM;
162     }
163 
164     MessageParcel data;
165     MessageParcel reply;
166     MessageOption option;
167 
168     if (!WriteInterfaceToken(data)) {
169         return RET_ERR_IPC_FAILED;
170     }
171     if (!data.WriteInt32(accessibilityWindowId)) {
172         HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
173         return RET_ERR_IPC_FAILED;
174     }
175     if (!data.WriteInt32(elementId)) {
176         HILOG_ERROR("elementId write error: %{public}d, ", elementId);
177         return RET_ERR_IPC_FAILED;
178     }
179     if (!data.WriteInt32(focusType)) {
180         HILOG_ERROR("focusType write error: %{public}d, ", focusType);
181         return RET_ERR_IPC_FAILED;
182     }
183     if (!data.WriteInt32(requestId)) {
184         HILOG_ERROR("requestId write error: %{public}d, ", requestId);
185         return RET_ERR_IPC_FAILED;
186     }
187     if (!data.WriteRemoteObject(callback->AsObject())) {
188         HILOG_ERROR("callback write error");
189         return RET_ERR_IPC_FAILED;
190     }
191 
192     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::FIND_FOCUSED_ELEMENTINFO, data, reply, option)) {
193         HILOG_ERROR("fail to gain focus");
194         return RET_ERR_IPC_FAILED;
195     }
196     return static_cast<RetError>(reply.ReadInt32());
197 }
198 
FocusMoveSearch(const int32_t accessibilityWindowId,const int32_t elementId,const int32_t direction,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)199 RetError AccessibleAbilityChannelProxy::FocusMoveSearch(const int32_t accessibilityWindowId, const int32_t elementId,
200     const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
201 {
202     HILOG_DEBUG();
203     if (!callback) {
204         HILOG_ERROR("callback is nullptr.");
205         return RET_ERR_INVALID_PARAM;
206     }
207 
208     MessageParcel data;
209     MessageParcel reply;
210     MessageOption option;
211 
212     if (!WriteInterfaceToken(data)) {
213         return RET_ERR_IPC_FAILED;
214     }
215     if (!data.WriteInt32(accessibilityWindowId)) {
216         HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
217         return RET_ERR_IPC_FAILED;
218     }
219     if (!data.WriteInt32(elementId)) {
220         HILOG_ERROR("elementId write error: %{public}d, ", elementId);
221         return RET_ERR_IPC_FAILED;
222     }
223     if (!data.WriteInt32(direction)) {
224         HILOG_ERROR("direction write error: %{public}d, ", direction);
225         return RET_ERR_IPC_FAILED;
226     }
227     if (!data.WriteInt32(requestId)) {
228         HILOG_ERROR("requestId write error: %{public}d, ", requestId);
229         return RET_ERR_IPC_FAILED;
230     }
231     if (!data.WriteRemoteObject(callback->AsObject())) {
232         HILOG_ERROR("callback write error");
233         return RET_ERR_IPC_FAILED;
234     }
235 
236     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::FOCUS_MOVE_SEARCH, data, reply, option)) {
237         HILOG_ERROR("fail to search focus");
238         return RET_ERR_IPC_FAILED;
239     }
240     return static_cast<RetError>(reply.ReadInt32());
241 }
242 
ExecuteAction(const int32_t accessibilityWindowId,const int32_t elementId,const int32_t action,const std::map<std::string,std::string> & actionArguments,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)243 RetError AccessibleAbilityChannelProxy::ExecuteAction(const int32_t accessibilityWindowId, const int32_t elementId,
244     const int32_t action, const std::map<std::string, std::string> &actionArguments, const int32_t requestId,
245     const sptr<IAccessibilityElementOperatorCallback> &callback)
246 {
247     HILOG_DEBUG();
248 
249     MessageParcel data;
250     MessageParcel reply;
251     MessageOption option;
252 
253     if (!WriteInterfaceToken(data)) {
254         return RET_ERR_IPC_FAILED;
255     }
256     if (!data.WriteInt32(accessibilityWindowId)) {
257         HILOG_ERROR("accessibilityWindowId write error: %{public}d, ", accessibilityWindowId);
258         return RET_ERR_IPC_FAILED;
259     }
260     if (!data.WriteInt32(elementId)) {
261         HILOG_ERROR("elementId write error: %{public}d, ", elementId);
262         return RET_ERR_IPC_FAILED;
263     }
264     if (!data.WriteInt32(action)) {
265         HILOG_ERROR("action write error: %{public}d, ", action);
266         return RET_ERR_IPC_FAILED;
267     }
268 
269     std::vector<std::string> actionArgumentsKey {};
270     std::vector<std::string> actionArgumentsValue {};
271     for (auto iter = actionArguments.begin(); iter != actionArguments.end(); iter++) {
272         actionArgumentsKey.push_back(iter->first);
273         actionArgumentsValue.push_back(iter->second);
274     }
275     if (!data.WriteStringVector(actionArgumentsKey)) {
276         HILOG_ERROR("actionArgumentsKey write error");
277         return RET_ERR_IPC_FAILED;
278     }
279     if (!data.WriteStringVector(actionArgumentsValue)) {
280         HILOG_ERROR("actionArgumentsValue write error");
281         return RET_ERR_IPC_FAILED;
282     }
283 
284     if (!data.WriteInt32(requestId)) {
285         HILOG_ERROR("requestId write error: %{public}d, ", requestId);
286         return RET_ERR_IPC_FAILED;
287     }
288     if (!callback || !data.WriteRemoteObject(callback->AsObject())) {
289         HILOG_ERROR("callback write error");
290         return RET_ERR_IPC_FAILED;
291     }
292 
293     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::PERFORM_ACTION,
294         data, reply, option)) {
295         HILOG_ERROR("fail to perform accessibility action");
296         return RET_ERR_IPC_FAILED;
297     }
298     return static_cast<RetError>(reply.ReadInt32());
299 }
300 
GetWindow(const int32_t windowId,AccessibilityWindowInfo & windowInfo)301 RetError AccessibleAbilityChannelProxy::GetWindow(const int32_t windowId, AccessibilityWindowInfo &windowInfo)
302 {
303     HILOG_DEBUG();
304 
305     MessageParcel data;
306     MessageParcel reply;
307     MessageOption option;
308 
309     if (!WriteInterfaceToken(data)) {
310         return RET_ERR_IPC_FAILED;
311     }
312 
313     if (!data.WriteInt32(windowId)) {
314         HILOG_ERROR("windowId write error: %{public}d, ", windowId);
315         return RET_ERR_IPC_FAILED;
316     }
317 
318     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_WINDOW, data, reply, option)) {
319         HILOG_ERROR("fail to get window");
320         return RET_ERR_IPC_FAILED;
321     }
322 
323     sptr<AccessibilityWindowInfoParcel> windowInfoParcel = reply.ReadStrongParcelable<AccessibilityWindowInfoParcel>();
324     if (!windowInfoParcel) {
325         HILOG_ERROR("ReadStrongParcelable<AccessibilityWindowInfoParcel> failed");
326         return RET_ERR_IPC_FAILED;
327     }
328     windowInfo = *windowInfoParcel;
329 
330     return static_cast<RetError>(reply.ReadInt32());
331 }
332 
GetWindows(std::vector<AccessibilityWindowInfo> & windows)333 RetError AccessibleAbilityChannelProxy::GetWindows(std::vector<AccessibilityWindowInfo> &windows)
334 {
335     HILOG_DEBUG();
336     MessageParcel data;
337     MessageParcel reply;
338     MessageOption option;
339 
340     if (!WriteInterfaceToken(data)) {
341         return RET_ERR_IPC_FAILED;
342     }
343 
344     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_WINDOWS, data, reply, option)) {
345         HILOG_ERROR("fail to get windows");
346         return RET_ERR_IPC_FAILED;
347     }
348 
349     int32_t windowsSize = reply.ReadInt32();
350     for (int32_t i = 0; i < windowsSize; i++) {
351         sptr<AccessibilityWindowInfoParcel> window = reply.ReadStrongParcelable<AccessibilityWindowInfoParcel>();
352         if (!window) {
353             HILOG_ERROR("ReadStrongParcelable<AccessibilityWindowInfoParcel> failed");
354             return RET_ERR_IPC_FAILED;
355         }
356         windows.emplace_back(*window);
357     }
358 
359     return static_cast<RetError>(reply.ReadInt32());
360 }
361 
GetWindowsByDisplayId(const uint64_t displayId,std::vector<AccessibilityWindowInfo> & windows)362 RetError AccessibleAbilityChannelProxy::GetWindowsByDisplayId(const uint64_t displayId,
363     std::vector<AccessibilityWindowInfo> &windows)
364 {
365     HILOG_DEBUG();
366 
367     MessageParcel data;
368     MessageParcel reply;
369     MessageOption option;
370 
371     if (!WriteInterfaceToken(data)) {
372         return RET_ERR_IPC_FAILED;
373     }
374 
375     if (!data.WriteUint64(displayId)) {
376         HILOG_ERROR("displayId write error: %{public}" PRIu64 ", ", displayId);
377         return RET_ERR_IPC_FAILED;
378     }
379 
380     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::GET_WINDOWS_BY_DISPLAY_ID, data, reply, option)) {
381         HILOG_ERROR("fail to get windows");
382         return RET_ERR_IPC_FAILED;
383     }
384 
385     int32_t windowsSize = reply.ReadInt32();
386     for (int32_t i = 0; i < windowsSize; i++) {
387         sptr<AccessibilityWindowInfoParcel> window = reply.ReadStrongParcelable<AccessibilityWindowInfoParcel>();
388         if (!window) {
389             HILOG_ERROR("ReadStrongParcelable<AccessibilityWindowInfoParcel> failed");
390             return RET_ERR_IPC_FAILED;
391         }
392         windows.emplace_back(*window);
393     }
394 
395     return static_cast<RetError>(reply.ReadInt32());
396 }
397 
SetOnKeyPressEventResult(const bool handled,const int32_t sequence)398 void AccessibleAbilityChannelProxy::SetOnKeyPressEventResult(const bool handled, const int32_t sequence)
399 {
400     HILOG_DEBUG();
401 
402     MessageParcel data;
403     MessageParcel reply;
404     MessageOption option(MessageOption::TF_ASYNC);
405 
406     if (!WriteInterfaceToken(data)) {
407         return;
408     }
409     if (!data.WriteBool(handled)) {
410         HILOG_ERROR("handled write error: %{public}d, ", handled);
411         return;
412     }
413     if (!data.WriteInt32(sequence)) {
414         HILOG_ERROR("sequence write error: %{public}d, ", sequence);
415         return;
416     }
417     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SET_ON_KEY_PRESS_EVENT_RESULT,
418         data, reply, option)) {
419         HILOG_ERROR("fail to set onKeyPressEvent result");
420     }
421 }
422 
SendSimulateGesture(const std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)423 RetError AccessibleAbilityChannelProxy::SendSimulateGesture(
424     const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
425 {
426     HILOG_DEBUG();
427     sptr<AccessibilityGestureInjectPathParcel> path =
428         new(std::nothrow) AccessibilityGestureInjectPathParcel(*gesturePath);
429     if (!path) {
430         HILOG_ERROR("Failed to create path.");
431         return RET_ERR_NULLPTR;
432     }
433 
434     MessageParcel data;
435     MessageParcel reply;
436     MessageOption option(MessageOption::TF_SYNC);
437 
438     if (!WriteInterfaceToken(data)) {
439         return RET_ERR_IPC_FAILED;
440     }
441 
442     if (!data.WriteStrongParcelable(path)) {
443         HILOG_ERROR("WriteStrongParcelable<AccessibilityGestureInjectPathParcel> failed");
444         return RET_ERR_IPC_FAILED;
445     }
446 
447     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SEND_SIMULATE_GESTURE_PATH, data, reply, option)) {
448         HILOG_ERROR("fail to send simulation gesture path");
449         return RET_ERR_IPC_FAILED;
450     }
451     return static_cast<RetError>(reply.ReadInt32());
452 }
453 
SetTargetBundleName(const std::vector<std::string> & targetBundleNames)454 RetError AccessibleAbilityChannelProxy::SetTargetBundleName(const std::vector<std::string> &targetBundleNames)
455 {
456     HILOG_DEBUG();
457 
458     MessageParcel data;
459     MessageParcel reply;
460     MessageOption option(MessageOption::TF_SYNC);
461 
462     if (!WriteInterfaceToken(data)) {
463         return RET_ERR_IPC_FAILED;
464     }
465     if (!data.WriteInt32(targetBundleNames.size())) {
466         HILOG_ERROR("targetBundleNames.size() write error: %{public}zu, ", targetBundleNames.size());
467         return RET_ERR_IPC_FAILED;
468     }
469     for (auto &targetBundleName : targetBundleNames) {
470         if (!data.WriteString(targetBundleName)) {
471             HILOG_ERROR("targetBundleName write error: %{public}s, ", targetBundleName.c_str());
472             return RET_ERR_IPC_FAILED;
473         }
474     }
475     if (!SendTransactCmd(IAccessibleAbilityChannel::Message::SET_TARGET_BUNDLE_NAME, data, reply, option)) {
476         HILOG_ERROR("fail to set target bundle name filter");
477         return RET_ERR_IPC_FAILED;
478     }
479     return static_cast<RetError>(reply.ReadInt32());
480 }
481 } // namespace Accessibility
482 } // namespace OHOS