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_stub.h"
17 #include "accessibility_element_operator_callback_proxy.h"
18 #include "hilog_wrapper.h"
19 #include <cinttypes>
20
21 #define SWITCH_BEGIN(code) switch (code) {
22 #define SWITCH_CASE(case_code, func) \
23 case case_code: { \
24 result_code = func(data, reply); \
25 break; \
26 }
27
28 #define SWITCH_END() \
29 default: { \
30 result_code = ERR_CODE_DEFAULT; \
31 HILOG_WARN("AccessibilityElementOperatorStub::OnRemoteRequest, default case, need check."); \
32 break; \
33 } \
34 }
35
36 #define ACCESSIBILITY_ELEMENT_OPERATOR_STUB_CASES() \
37 SWITCH_CASE(AccessibilityInterfaceCode::SEARCH_BY_ACCESSIBILITY_ID, HandleSearchElementInfoByAccessibilityId) \
38 SWITCH_CASE(AccessibilityInterfaceCode::SEARCH_BY_TEXT, HandleSearchElementInfosByText) \
39 SWITCH_CASE(AccessibilityInterfaceCode::FIND_FOCUSED_INFO, HandleFindFocusedElementInfo) \
40 SWITCH_CASE(AccessibilityInterfaceCode::FOCUS_FIND, HandleFocusFind) \
41 SWITCH_CASE(AccessibilityInterfaceCode::PERFORM_ACTION_ELEMENT, HandleExecuteAction) \
42 SWITCH_CASE(AccessibilityInterfaceCode::CURSOR_POSITION, HandleGetCursorPosition) \
43 SWITCH_CASE(AccessibilityInterfaceCode::CLEAR_FOCUS, HandleClearFocus) \
44 SWITCH_CASE(AccessibilityInterfaceCode::OUTSIDE_TOUCH, HandleOutsideTouch) \
45 SWITCH_CASE(AccessibilityInterfaceCode::SET_CHILDTREEID, HandleSetChildTreeIdAndWinId) \
46 SWITCH_CASE(AccessibilityInterfaceCode::SET_BELONGTREEID, HandleSetBelongTreeId) \
47 SWITCH_CASE(AccessibilityInterfaceCode::SET_PARENTWINDOWID, HandleSetParentWindowId) \
48 SWITCH_CASE(AccessibilityInterfaceCode::SEARCH_BY_WINDOW_ID, HandleSearchDefaultFocusedByWindowId) \
49 SWITCH_CASE(AccessibilityInterfaceCode::SEARCH_BY_SPECIFIC_PROPERTY, HandleSearchElementInfoBySpecificProperty)
50
51 namespace OHOS {
52 namespace Accessibility {
53 constexpr int32_t ERR_CODE_DEFAULT = -1000;
54
AccessibilityElementOperatorStub()55 AccessibilityElementOperatorStub::AccessibilityElementOperatorStub()
56 {
57 }
58
~AccessibilityElementOperatorStub()59 AccessibilityElementOperatorStub::~AccessibilityElementOperatorStub()
60 {
61 }
62
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)63 int AccessibilityElementOperatorStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
64 MessageParcel &reply, MessageOption &option)
65 {
66 HILOG_DEBUG("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
67 std::u16string descriptor = AccessibilityElementOperatorStub::GetDescriptor();
68 std::u16string remoteDescriptor = data.ReadInterfaceToken();
69 if (descriptor != remoteDescriptor) {
70 HILOG_INFO("local descriptor is not equal to remote");
71 return ERR_INVALID_STATE;
72 }
73
74 ErrCode result_code = ERR_NONE;
75 SWITCH_BEGIN(code)
76 ACCESSIBILITY_ELEMENT_OPERATOR_STUB_CASES()
77 SWITCH_END()
78
79 if (result_code != ERR_CODE_DEFAULT) {
80 return result_code;
81 }
82
83 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
84 }
85
HandleSearchElementInfoByAccessibilityId(MessageParcel & data,MessageParcel & reply)86 ErrCode AccessibilityElementOperatorStub::HandleSearchElementInfoByAccessibilityId(MessageParcel &data,
87 MessageParcel &reply)
88 {
89 HILOG_DEBUG();
90
91 int64_t elementId = data.ReadInt64();
92 int32_t requestId = data.ReadInt32();
93 sptr<IRemoteObject> remote = data.ReadRemoteObject();
94 if (remote == nullptr) {
95 HILOG_ERROR("remote is nullptr.");
96 return ERR_INVALID_VALUE;
97 }
98
99 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
100 if (callback == nullptr) {
101 HILOG_ERROR("callback is nullptr");
102 return ERR_INVALID_VALUE;
103 }
104 int32_t mode = data.ReadInt32();
105 bool isFilter = data.ReadBool();
106 SearchElementInfoByAccessibilityId(elementId, requestId, callback, mode, isFilter);
107 return NO_ERROR;
108 }
109
HandleSearchDefaultFocusedByWindowId(MessageParcel & data,MessageParcel & reply)110 ErrCode AccessibilityElementOperatorStub::HandleSearchDefaultFocusedByWindowId(MessageParcel &data,
111 MessageParcel &reply)
112 {
113 HILOG_DEBUG();
114
115 int32_t windowId = data.ReadInt32();
116 int32_t requestId = data.ReadInt32();
117 sptr<IRemoteObject> remote = data.ReadRemoteObject();
118 if (remote == nullptr) {
119 HILOG_ERROR("remote is nullptr.");
120 return ERR_INVALID_VALUE;
121 }
122
123 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
124 if (callback == nullptr) {
125 HILOG_ERROR("callback is nullptr");
126 return ERR_INVALID_VALUE;
127 }
128 int32_t mode = data.ReadInt32();
129 bool isFilter = data.ReadBool();
130 SearchDefaultFocusedByWindowId(windowId, requestId, callback, mode, isFilter);
131 return NO_ERROR;
132 }
133
HandleSearchElementInfosByText(MessageParcel & data,MessageParcel & reply)134 ErrCode AccessibilityElementOperatorStub::HandleSearchElementInfosByText(MessageParcel &data,
135 MessageParcel &reply)
136 {
137 HILOG_DEBUG();
138
139 int64_t elementId = data.ReadInt64();
140 std::string text = data.ReadString();
141 int32_t requestId = data.ReadInt32();
142 sptr<IRemoteObject> remote = data.ReadRemoteObject();
143 if (remote == nullptr) {
144 HILOG_ERROR("remote is nullptr.");
145 return ERR_INVALID_VALUE;
146 }
147
148 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
149 if (callback == nullptr) {
150 HILOG_ERROR("callback is nullptr");
151 return ERR_INVALID_VALUE;
152 }
153 SearchElementInfosByText(elementId, text, requestId, callback);
154 return NO_ERROR;
155 }
156
HandleFindFocusedElementInfo(MessageParcel & data,MessageParcel & reply)157 ErrCode AccessibilityElementOperatorStub::HandleFindFocusedElementInfo(MessageParcel &data,
158 MessageParcel &reply)
159 {
160 HILOG_DEBUG();
161
162 int64_t elementId = data.ReadInt64();
163 int32_t focusType = data.ReadInt32();
164 int32_t requestId = data.ReadInt32();
165 sptr<IRemoteObject> remote = data.ReadRemoteObject();
166 if (remote == nullptr) {
167 HILOG_ERROR("remote is nullptr.");
168 return ERR_INVALID_VALUE;
169 }
170
171 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
172 if (callback == nullptr) {
173 HILOG_ERROR("callback is nullptr");
174 return ERR_INVALID_VALUE;
175 }
176 FindFocusedElementInfo(elementId, focusType, requestId, callback);
177 return NO_ERROR;
178 }
179
HandleFocusFind(MessageParcel & data,MessageParcel & reply)180 ErrCode AccessibilityElementOperatorStub::HandleFocusFind(MessageParcel &data, MessageParcel &reply)
181 {
182 HILOG_DEBUG();
183
184 int64_t elementId = data.ReadInt64();
185 int32_t direction = data.ReadInt32();
186 int32_t requestId = data.ReadInt32();
187 sptr<IRemoteObject> remote = data.ReadRemoteObject();
188 if (remote == nullptr) {
189 HILOG_ERROR("remote is nullptr.");
190 return ERR_INVALID_VALUE;
191 }
192
193 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
194 if (callback == nullptr) {
195 HILOG_ERROR("callback is nullptr");
196 return ERR_INVALID_VALUE;
197 }
198 FocusMoveSearch(elementId, direction, requestId, callback);
199 return NO_ERROR;
200 }
201
HandleExecuteAction(MessageParcel & data,MessageParcel & reply)202 ErrCode AccessibilityElementOperatorStub::HandleExecuteAction(MessageParcel &data, MessageParcel &reply)
203 {
204 HILOG_DEBUG();
205 std::vector<std::string> argumentKey;
206 std::vector<std::string> argumentValue;
207 int64_t elementId = data.ReadInt64();
208 int32_t action = data.ReadInt32();
209 if (!data.ReadStringVector(&argumentKey)) {
210 return ERR_TRANSACTION_FAILED;
211 }
212 if (!data.ReadStringVector(&argumentValue)) {
213 return ERR_TRANSACTION_FAILED;
214 }
215 if (argumentKey.size() != argumentValue.size()) {
216 return ERR_TRANSACTION_FAILED;
217 }
218 std::map<std::string, std::string> arguments;
219 for (size_t i = 0;i < argumentKey.size(); i++) {
220 arguments.insert(std::pair<std::string, std::string>(argumentKey[i], argumentValue[i]));
221 }
222 int32_t requestId = data.ReadInt32();
223 sptr<IRemoteObject> remote = data.ReadRemoteObject();
224 if (remote == nullptr) {
225 HILOG_ERROR("remote is nullptr.");
226 return ERR_INVALID_VALUE;
227 }
228
229 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
230 if (callback == nullptr) {
231 HILOG_ERROR("callback is nullptr");
232 return ERR_INVALID_VALUE;
233 }
234 ExecuteAction(elementId, action, arguments, requestId, callback);
235 return NO_ERROR;
236 }
237
HandleGetCursorPosition(MessageParcel & data,MessageParcel & reply)238 ErrCode AccessibilityElementOperatorStub::HandleGetCursorPosition(MessageParcel &data, MessageParcel &reply)
239 {
240 HILOG_DEBUG();
241 int64_t elementId = data.ReadInt64();
242 int32_t requestId = data.ReadInt32();
243 sptr<IRemoteObject> remote = data.ReadRemoteObject();
244 if (remote == nullptr) {
245 HILOG_ERROR("remote is nullptr.");
246 return ERR_INVALID_VALUE;
247 }
248
249 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
250 if (callback == nullptr) {
251 HILOG_ERROR("callback is nullptr");
252 return ERR_INVALID_VALUE;
253 }
254 GetCursorPosition(elementId, requestId, callback);
255 return NO_ERROR;
256 }
257
HandleClearFocus(MessageParcel & data,MessageParcel & reply)258 ErrCode AccessibilityElementOperatorStub::HandleClearFocus(MessageParcel &data, MessageParcel &reply)
259 {
260 HILOG_DEBUG();
261
262 ClearFocus();
263
264 return NO_ERROR;
265 }
266
HandleOutsideTouch(MessageParcel & data,MessageParcel & reply)267 ErrCode AccessibilityElementOperatorStub::HandleOutsideTouch(MessageParcel &data, MessageParcel &reply)
268 {
269 HILOG_DEBUG();
270
271 OutsideTouch();
272
273 return NO_ERROR;
274 }
275
HandleSetChildTreeIdAndWinId(MessageParcel & data,MessageParcel & reply)276 ErrCode AccessibilityElementOperatorStub::HandleSetChildTreeIdAndWinId(MessageParcel &data, MessageParcel &reply)
277 {
278 HILOG_DEBUG();
279 int64_t elementId = data.ReadInt64();
280 int32_t treeId = data.ReadInt32();
281 int32_t childWindowId = data.ReadInt32();
282 SetChildTreeIdAndWinId(elementId, treeId, childWindowId);
283 return NO_ERROR;
284 }
285
HandleSetBelongTreeId(MessageParcel & data,MessageParcel & reply)286 ErrCode AccessibilityElementOperatorStub::HandleSetBelongTreeId(MessageParcel &data, MessageParcel &reply)
287 {
288 HILOG_DEBUG();
289 int32_t treeId = data.ReadInt32();
290
291 SetBelongTreeId(treeId);
292 return NO_ERROR;
293 }
294
HandleSetParentWindowId(MessageParcel & data,MessageParcel & reply)295 ErrCode AccessibilityElementOperatorStub::HandleSetParentWindowId(MessageParcel &data, MessageParcel &reply)
296 {
297 HILOG_DEBUG();
298 int32_t iParentWindowId = data.ReadInt32();
299
300 SetParentWindowId(iParentWindowId);
301 return NO_ERROR;
302 }
303
HandleSearchElementInfoBySpecificProperty(MessageParcel & data,MessageParcel & reply)304 ErrCode AccessibilityElementOperatorStub::HandleSearchElementInfoBySpecificProperty(MessageParcel &data,
305 MessageParcel &reply)
306 {
307 int64_t elementId = data.ReadInt64();
308
309 SpecificPropertyParam param;
310 param.propertyTarget = data.ReadString();
311 param.propertyType = static_cast<SEARCH_TYPE>(data.ReadUint32());
312
313 int32_t requestId = data.ReadInt32();
314
315 HILOG_DEBUG("elementId:%{public}" PRId64 ", propertyTarget:%{public}s, propertyType:%{public}u, "
316 "requestId:%{public}d", elementId, param.propertyTarget.c_str(),
317 static_cast<uint32_t>(param.propertyType), requestId);
318
319 sptr<IRemoteObject> remote = data.ReadRemoteObject();
320 if (remote == nullptr) {
321 HILOG_ERROR("remote is nullptr.");
322 return ERR_INVALID_VALUE;
323 }
324
325 sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
326 if (callback == nullptr) {
327 HILOG_ERROR("callback is nullptr");
328 return ERR_INVALID_VALUE;
329 }
330
331 SearchElementInfoBySpecificProperty(elementId, param, requestId, callback);
332 return NO_ERROR;
333 }
334 } // namespace Accessibility
335 } // namespace OHOS