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