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