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 "accessibility_element_operator_proxy.h"
17 #include "hilog_wrapper.h"
18 #include <cinttypes>
19
20 namespace OHOS {
21 namespace Accessibility {
AccessibilityElementOperatorProxy(const sptr<IRemoteObject> & impl)22 AccessibilityElementOperatorProxy::AccessibilityElementOperatorProxy(
23 const sptr<IRemoteObject> &impl) : IRemoteProxy<IAccessibilityElementOperator>(impl)
24 {}
25
WriteInterfaceToken(MessageParcel & data)26 bool AccessibilityElementOperatorProxy::WriteInterfaceToken(MessageParcel &data)
27 {
28 HILOG_DEBUG();
29
30 if (!data.WriteInterfaceToken(AccessibilityElementOperatorProxy::GetDescriptor())) {
31 HILOG_ERROR("write interface token failed");
32 return false;
33 }
34 return true;
35 }
36
SendTransactCmd(AccessibilityInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 bool AccessibilityElementOperatorProxy::SendTransactCmd(AccessibilityInterfaceCode code,
38 MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40 HILOG_DEBUG();
41 sptr<IRemoteObject> remoteObj = Remote();
42 if (remoteObj == nullptr) {
43 HILOG_ERROR("fail to send transact cmd %{public}d due to remote object", code);
44 return false;
45 }
46 int32_t result = remoteObj->SendRequest(static_cast<uint32_t>(code), data, reply, option);
47 if (result != NO_ERROR) {
48 HILOG_ERROR("receive error transact code %{public}d in transact cmd %{public}d", result, code);
49 return false;
50 }
51 return true;
52 }
53
SearchElementInfoByAccessibilityId(const int64_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode,bool isFilter)54 RetError AccessibilityElementOperatorProxy::SearchElementInfoByAccessibilityId(const int64_t elementId,
55 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
56 bool isFilter)
57 {
58 HILOG_DEBUG();
59 MessageParcel data;
60 MessageParcel reply;
61 MessageOption option(MessageOption::TF_SYNC);
62
63 if (!WriteInterfaceToken(data)) {
64 HILOG_ERROR("connection write token failed");
65 return RET_ERR_FAILED;
66 }
67
68 if (!data.WriteInt64(elementId)) {
69 HILOG_ERROR("connection write parcelable element id failed");
70 return RET_ERR_FAILED;
71 }
72
73 if (!data.WriteInt32(requestId)) {
74 HILOG_ERROR("connection write parcelable request id failed");
75 return RET_ERR_FAILED;
76 }
77
78 if (callback == nullptr) {
79 HILOG_ERROR("callback is nullptr");
80 return RET_ERR_NULLPTR;
81 }
82 if (!data.WriteRemoteObject(callback->AsObject())) {
83 HILOG_ERROR("connection write parcelable callback failed");
84 return RET_ERR_FAILED;
85 }
86
87 if (!data.WriteInt32(mode)) {
88 HILOG_ERROR("connection write parcelable mode failed");
89 return RET_ERR_FAILED;
90 }
91
92 if (!data.WriteBool(isFilter)) {
93 HILOG_ERROR("connection write parcelable isFilter failed");
94 return RET_ERR_FAILED;
95 }
96
97 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_ACCESSIBILITY_ID,
98 data, reply, option)) {
99 HILOG_ERROR("search element info by accessibility id failed");
100 return RET_ERR_FAILED;
101 }
102 return RET_OK;
103 }
104
SearchDefaultFocusedByWindowId(const int32_t windowId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback,const int32_t mode,bool isFilter)105 void AccessibilityElementOperatorProxy::SearchDefaultFocusedByWindowId(const int32_t windowId,
106 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
107 bool isFilter)
108 {
109 HILOG_DEBUG();
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
113
114 if (!WriteInterfaceToken(data)) {
115 HILOG_ERROR("connection write token failed");
116 return;
117 }
118
119 if (!data.WriteInt32(windowId)) {
120 HILOG_ERROR("connection write parcelable element id failed");
121 return;
122 }
123
124 if (!data.WriteInt32(requestId)) {
125 HILOG_ERROR("connection write parcelable request id failed");
126 return;
127 }
128
129 if (callback == nullptr) {
130 HILOG_ERROR("callback is nullptr");
131 return;
132 }
133 if (!data.WriteRemoteObject(callback->AsObject())) {
134 HILOG_ERROR("connection write parcelable callback failed");
135 return;
136 }
137
138 if (!data.WriteInt32(mode)) {
139 HILOG_ERROR("connection write parcelable mode failed");
140 return;
141 }
142
143 if (!data.WriteBool(isFilter)) {
144 HILOG_ERROR("connection write parcelable isFilter failed");
145 return;
146 }
147
148 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_WINDOW_ID,
149 data, reply, option)) {
150 HILOG_ERROR("search element info by accessibility id failed");
151 return;
152 }
153 }
154
SearchElementInfosByText(const int64_t elementId,const std::string & text,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)155 void AccessibilityElementOperatorProxy::SearchElementInfosByText(const int64_t elementId,
156 const std::string &text,
157 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
158 {
159 HILOG_DEBUG();
160 MessageParcel data;
161 MessageParcel reply;
162 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
163
164 if (!WriteInterfaceToken(data)) {
165 HILOG_ERROR("connection write token failed");
166 return;
167 }
168
169 if (!data.WriteInt64(elementId)) {
170 HILOG_ERROR("connection write parcelable elementId failed");
171 return;
172 }
173 if (!data.WriteString(text)) {
174 HILOG_ERROR("connection write text failed");
175 return;
176 }
177
178 if (!data.WriteInt32(requestId)) {
179 HILOG_ERROR("connection write request id failed");
180 return;
181 }
182
183 if (callback == nullptr) {
184 HILOG_ERROR("callback is nullptr");
185 return;
186 }
187 if (!data.WriteRemoteObject(callback->AsObject())) {
188 HILOG_ERROR("connection write callback failed");
189 return;
190 }
191
192 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_TEXT, data, reply, option)) {
193 HILOG_ERROR("search element infos by text failed");
194 return;
195 }
196 }
197
FindFocusedElementInfo(const int64_t elementId,const int32_t focusType,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)198 void AccessibilityElementOperatorProxy::FindFocusedElementInfo(const int64_t elementId,
199 const int32_t focusType, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
200 {
201 HILOG_DEBUG();
202 MessageParcel data;
203 MessageParcel reply;
204 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
205
206 if (!WriteInterfaceToken(data)) {
207 HILOG_ERROR("connection write token failed");
208 return;
209 }
210
211 if (!data.WriteInt64(elementId)) {
212 HILOG_ERROR("connection write elementId failed");
213 return;
214 }
215
216 if (!data.WriteInt32(focusType)) {
217 HILOG_ERROR("connection write focusType failed");
218 return;
219 }
220
221 if (!data.WriteInt32(requestId)) {
222 HILOG_ERROR("connection write requestId failed");
223 return;
224 }
225
226 if (callback == nullptr) {
227 HILOG_ERROR("callback is nullptr");
228 return;
229 }
230 if (!data.WriteRemoteObject(callback->AsObject())) {
231 HILOG_ERROR("connection write callback failed");
232 return;
233 }
234
235 if (!SendTransactCmd(AccessibilityInterfaceCode::FIND_FOCUSED_INFO, data, reply, option)) {
236 HILOG_ERROR("find focused element info failed");
237 return;
238 }
239 }
240
FocusMoveSearch(const int64_t elementId,const int32_t direction,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)241 void AccessibilityElementOperatorProxy::FocusMoveSearch(const int64_t elementId,
242 const int32_t direction, const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback)
243 {
244 HILOG_DEBUG();
245 MessageParcel data;
246 MessageParcel reply;
247 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
248
249 if (!WriteInterfaceToken(data)) {
250 HILOG_ERROR("fail, connection write Token");
251 return;
252 }
253
254 if (!data.WriteInt64(elementId)) {
255 HILOG_ERROR("fail, connection write elementId error");
256 return;
257 }
258
259 if (!data.WriteInt32(direction)) {
260 HILOG_ERROR("fail, connection write focusType error");
261 return;
262 }
263
264 if (!data.WriteInt32(requestId)) {
265 HILOG_ERROR("fail, connection write requestId error");
266 return;
267 }
268
269 if (callback == nullptr) {
270 HILOG_ERROR("callback is nullptr.");
271 return;
272 }
273 if (!data.WriteRemoteObject(callback->AsObject())) {
274 HILOG_ERROR("fail, connection write callback error");
275 return;
276 }
277
278 if (!SendTransactCmd(AccessibilityInterfaceCode::FOCUS_FIND, data, reply, option)) {
279 HILOG_ERROR("focus move search failed");
280 return;
281 }
282 }
283
ExecuteAction(const int64_t elementId,const int32_t action,const std::map<std::string,std::string> & arguments,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)284 void AccessibilityElementOperatorProxy::ExecuteAction(const int64_t elementId, const int32_t action,
285 const std::map<std::string, std::string> &arguments, const int32_t requestId,
286 const sptr<IAccessibilityElementOperatorCallback> &callback)
287 {
288 HILOG_DEBUG();
289 MessageParcel data;
290 MessageParcel reply;
291 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
292
293 if (!WriteInterfaceToken(data)) {
294 HILOG_ERROR("connection write token failed");
295 return;
296 }
297
298 if (!data.WriteInt64(elementId)) {
299 HILOG_ERROR("connection write element id failed");
300 return;
301 }
302
303 if (!data.WriteInt32(action)) {
304 HILOG_ERROR("connection write focus type failed");
305 return;
306 }
307
308 auto iter = arguments.begin();
309 std::vector<std::string> keys;
310 std::vector<std::string> values;
311 while (iter != arguments.end()) {
312 keys.push_back(iter->first);
313 values.push_back(iter->second);
314 iter++;
315 }
316
317 if (!data.WriteStringVector(keys)) {
318 HILOG_ERROR("connection write argument keys failed");
319 return;
320 }
321
322 if (!data.WriteStringVector(values)) {
323 HILOG_ERROR("connection write argument values failed");
324 return;
325 }
326
327 if (!data.WriteInt32(requestId)) {
328 HILOG_ERROR("connection write request id failed");
329 return;
330 }
331
332 if (callback == nullptr) {
333 HILOG_ERROR("callback is nullptr");
334 return;
335 }
336 if (!data.WriteRemoteObject(callback->AsObject())) {
337 HILOG_ERROR("connection write callback failed");
338 return;
339 }
340
341 if (!SendTransactCmd(AccessibilityInterfaceCode::PERFORM_ACTION_ELEMENT, data, reply, option)) {
342 HILOG_ERROR("execute action failed");
343 return;
344 }
345 }
346
347
GetCursorPosition(const int64_t elementId,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)348 void AccessibilityElementOperatorProxy::GetCursorPosition(const int64_t elementId, const int32_t requestId,
349 const sptr<IAccessibilityElementOperatorCallback> &callback)
350 {
351 HILOG_DEBUG();
352 MessageParcel data;
353 MessageParcel reply;
354 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
355
356 if (!WriteInterfaceToken(data)) {
357 HILOG_ERROR("connection write token failed");
358 return;
359 }
360
361 if (!data.WriteInt64(elementId)) {
362 HILOG_ERROR("connection write elementId failed");
363 return;
364 }
365
366 if (!data.WriteInt32(requestId)) {
367 HILOG_ERROR("connection write requestId failed");
368 return;
369 }
370
371 if (callback == nullptr) {
372 HILOG_ERROR("callback is nullptr");
373 return;
374 }
375 if (!data.WriteRemoteObject(callback->AsObject())) {
376 HILOG_ERROR("connection write callback failed");
377 return;
378 }
379
380 if (!SendTransactCmd(AccessibilityInterfaceCode::CURSOR_POSITION, data, reply, option)) {
381 HILOG_ERROR("find cursor position info failed");
382 return;
383 }
384 }
385
ClearFocus()386 void AccessibilityElementOperatorProxy::ClearFocus()
387 {
388 HILOG_DEBUG();
389 MessageParcel data;
390 MessageParcel reply;
391 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
392 if (!WriteInterfaceToken(data)) {
393 HILOG_ERROR("connection write token failed");
394 return;
395 }
396
397 if (!SendTransactCmd(AccessibilityInterfaceCode::CLEAR_FOCUS, data, reply, option)) {
398 HILOG_ERROR("clear focus failed");
399 return;
400 }
401 }
402
OutsideTouch()403 void AccessibilityElementOperatorProxy::OutsideTouch()
404 {
405 HILOG_DEBUG();
406 MessageParcel data;
407 MessageParcel reply;
408 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
409 if (!WriteInterfaceToken(data)) {
410 HILOG_ERROR("connection write token failed");
411 return;
412 }
413
414 if (!SendTransactCmd(AccessibilityInterfaceCode::OUTSIDE_TOUCH, data, reply, option)) {
415 HILOG_ERROR("outside touch failed");
416 return;
417 }
418 }
419
SetChildTreeIdAndWinId(const int64_t elementId,const int32_t treeId,const int32_t childWindowId)420 void AccessibilityElementOperatorProxy::SetChildTreeIdAndWinId(const int64_t elementId, const int32_t treeId,
421 const int32_t childWindowId)
422 {
423 HILOG_DEBUG();
424 MessageParcel data;
425 MessageParcel reply;
426 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
427
428 if (!WriteInterfaceToken(data)) {
429 HILOG_ERROR("connection write token failed");
430 return;
431 }
432
433 if (!data.WriteInt64(elementId)) {
434 HILOG_ERROR("connection write elementId failed");
435 return;
436 }
437
438 if (!data.WriteInt32(treeId)) {
439 HILOG_ERROR("connection write treeId failed");
440 return;
441 }
442
443 if (!data.WriteInt32(childWindowId)) {
444 HILOG_ERROR("connection write childWindowId failed");
445 return;
446 }
447
448 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_CHILDTREEID, data, reply, option)) {
449 HILOG_ERROR("clear focus failed");
450 return;
451 }
452 }
453
SetBelongTreeId(const int32_t treeId)454 void AccessibilityElementOperatorProxy::SetBelongTreeId(const int32_t treeId)
455 {
456 HILOG_DEBUG();
457 MessageParcel data;
458 MessageParcel reply;
459 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
460 if (!WriteInterfaceToken(data)) {
461 HILOG_ERROR("connection write token failed");
462 return;
463 }
464
465 if (!data.WriteInt32(treeId)) {
466 HILOG_ERROR("connection write elementId failed");
467 return;
468 }
469
470 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_BELONGTREEID, data, reply, option)) {
471 HILOG_ERROR("clear focus failed");
472 return;
473 }
474 }
475
SetParentWindowId(const int32_t iParentWindowId)476 void AccessibilityElementOperatorProxy::SetParentWindowId(const int32_t iParentWindowId)
477 {
478 HILOG_DEBUG();
479 MessageParcel data;
480 MessageParcel reply;
481 MessageOption option(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
482 if (!WriteInterfaceToken(data)) {
483 HILOG_ERROR("connection write token failed");
484 return;
485 }
486
487 if (!data.WriteInt32(iParentWindowId)) {
488 HILOG_ERROR("connection write iParentWindowId failed");
489 return;
490 }
491
492 if (!SendTransactCmd(AccessibilityInterfaceCode::SET_PARENTWINDOWID, data, reply, option)) {
493 HILOG_ERROR("clear focus failed");
494 return;
495 }
496 }
497
SearchElementInfoBySpecificProperty(const int64_t elementId,const SpecificPropertyParam & param,const int32_t requestId,const sptr<IAccessibilityElementOperatorCallback> & callback)498 void AccessibilityElementOperatorProxy::SearchElementInfoBySpecificProperty(const int64_t elementId,
499 const SpecificPropertyParam& param, const int32_t requestId,
500 const sptr<IAccessibilityElementOperatorCallback> &callback)
501 {
502 HILOG_DEBUG("elementId:%{public}" PRId64 ", propertyTarget:%{public}s, propertyType:%{public}u, "
503 "requestId:%{public}d", elementId, param.propertyTarget.c_str(),
504 static_cast<uint32_t>(param.propertyType), requestId);
505 MessageParcel data;
506 MessageParcel reply;
507 MessageOption option(MessageOption::TF_SYNC);
508
509 if (!WriteInterfaceToken(data)) {
510 HILOG_ERROR("connection write token failed");
511 return;
512 }
513
514 if (!data.WriteInt64(elementId)) {
515 HILOG_ERROR("connection write parcelable element id failed");
516 return;
517 }
518
519 if (!data.WriteString(param.propertyTarget)) {
520 HILOG_ERROR("connection write parcelable property target failed");
521 return;
522 }
523
524 if (!data.WriteUint32(static_cast<uint32_t>(param.propertyType))) {
525 HILOG_ERROR("connection write parcelable property type failed");
526 return;
527 }
528
529 if (!data.WriteInt32(requestId)) {
530 HILOG_ERROR("connection write parcelable request id failed");
531 return;
532 }
533
534 if (callback == nullptr) {
535 HILOG_ERROR("callback is nullptr");
536 return;
537 }
538
539 if (!data.WriteRemoteObject(callback->AsObject())) {
540 HILOG_ERROR("connection write parcelable callback failed");
541 return;
542 }
543
544 if (!SendTransactCmd(AccessibilityInterfaceCode::SEARCH_BY_SPECIFIC_PROPERTY,
545 data, reply, option)) {
546 HILOG_ERROR("search element info by specific property failed");
547 return;
548 }
549 }
550 } // namespace Accessibility
551 } // namespace OHOS