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_stub.h"
17 #include "accessibility_element_info_parcel.h"
18 #include "accessibility_gesture_inject_path_parcel.h"
19 #include "accessibility_window_info_parcel.h"
20 #include "hilog_wrapper.h"
21
22 namespace OHOS {
23 namespace Accessibility {
AccessibleAbilityChannelStub()24 AccessibleAbilityChannelStub::AccessibleAbilityChannelStub()
25 {
26 HILOG_DEBUG();
27
28 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFO_BY_ACCESSIBILITY_ID)] =
29 &AccessibleAbilityChannelStub::HandleSearchElementInfoByAccessibilityId;
30 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFOS_BY_TEXT)] =
31 &AccessibleAbilityChannelStub::HandleSearchElementInfosByText;
32 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::FIND_FOCUSED_ELEMENTINFO)] =
33 &AccessibleAbilityChannelStub::HandleFindFocusedElementInfo;
34 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::FOCUS_MOVE_SEARCH)] =
35 &AccessibleAbilityChannelStub::HandleFocusMoveSearch;
36 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::PERFORM_ACTION)] =
37 &AccessibleAbilityChannelStub::HandleExecuteAction;
38 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_WINDOW)] =
39 &AccessibleAbilityChannelStub::HandleGetWindow;
40 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_WINDOWS)] =
41 &AccessibleAbilityChannelStub::HandleGetWindows;
42 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_WINDOWS_BY_DISPLAY_ID)] =
43 &AccessibleAbilityChannelStub::HandleGetWindowsByDisplayId;
44 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SET_ON_KEY_PRESS_EVENT_RESULT)] =
45 &AccessibleAbilityChannelStub::HandleSetOnKeyPressEventResult;
46 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEND_SIMULATE_GESTURE_PATH)] =
47 &AccessibleAbilityChannelStub::HandleSendSimulateGesturePath;
48 memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SET_TARGET_BUNDLE_NAME)] =
49 &AccessibleAbilityChannelStub::HandleSetTargetBundleName;
50 }
51
~AccessibleAbilityChannelStub()52 AccessibleAbilityChannelStub::~AccessibleAbilityChannelStub()
53 {
54 HILOG_DEBUG();
55 memberFuncMap_.clear();
56 }
57
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int AccessibleAbilityChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
59 MessageOption &option)
60 {
61 HILOG_DEBUG("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
62 std::u16string descriptor = AccessibleAbilityChannelStub::GetDescriptor();
63 std::u16string remoteDescriptor = data.ReadInterfaceToken();
64 if (descriptor != remoteDescriptor) {
65 HILOG_INFO("local descriptor is not equal to remote");
66 return ERR_INVALID_STATE;
67 }
68
69 auto itFunc = memberFuncMap_.find(code);
70 if (itFunc != memberFuncMap_.end()) {
71 auto requestFunc = itFunc->second;
72 if (requestFunc != nullptr) {
73 return (this->*requestFunc)(data, reply);
74 }
75 }
76 HILOG_WARN("AbilityManagerStub::OnRemoteRequest, default case, need check.");
77 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 }
79
HandleSearchElementInfoByAccessibilityId(MessageParcel & data,MessageParcel & reply)80 ErrCode AccessibleAbilityChannelStub::HandleSearchElementInfoByAccessibilityId(MessageParcel &data,
81 MessageParcel &reply)
82 {
83 HILOG_DEBUG();
84
85 int32_t accessibilityWindowId = data.ReadInt32();
86 int32_t elementId = data.ReadInt32();
87 int32_t requestId = data.ReadInt32();
88
89 sptr<IRemoteObject> remote = data.ReadRemoteObject();
90 if (!remote) {
91 HILOG_ERROR("remote is nullptr.");
92 return ERR_INVALID_VALUE;
93 }
94 sptr<IAccessibilityElementOperatorCallback> callback =
95 iface_cast<IAccessibilityElementOperatorCallback>(remote);
96 if (!callback) {
97 HILOG_ERROR("callback is nullptr.");
98 return ERR_INVALID_VALUE;
99 }
100
101 int32_t mode = data.ReadInt32();
102 RetError result = SearchElementInfoByAccessibilityId(accessibilityWindowId, elementId, requestId, callback, mode);
103 HILOG_DEBUG("SearchElementInfoByAccessibilityId ret = %{public}d", result);
104 reply.WriteInt32(result);
105
106 return NO_ERROR;
107 }
108
HandleSearchElementInfosByText(MessageParcel & data,MessageParcel & reply)109 ErrCode AccessibleAbilityChannelStub::HandleSearchElementInfosByText(MessageParcel &data,
110 MessageParcel &reply)
111 {
112 HILOG_DEBUG();
113
114 int32_t accessibilityWindowId = data.ReadInt32();
115 int32_t elementId = data.ReadInt32();
116 std::string text = data.ReadString();
117 int32_t requestId = data.ReadInt32();
118
119 sptr<IRemoteObject> remote = data.ReadRemoteObject();
120 sptr<IAccessibilityElementOperatorCallback> callback =
121 iface_cast<IAccessibilityElementOperatorCallback>(remote);
122
123 RetError result = SearchElementInfosByText(accessibilityWindowId, elementId, text, requestId, callback);
124 HILOG_DEBUG("SearchElementInfosByText ret = %{public}d", result);
125 reply.WriteInt32(result);
126
127 return NO_ERROR;
128 }
129
HandleFindFocusedElementInfo(MessageParcel & data,MessageParcel & reply)130 ErrCode AccessibleAbilityChannelStub::HandleFindFocusedElementInfo(MessageParcel &data, MessageParcel &reply)
131 {
132 HILOG_DEBUG();
133
134 int32_t accessibilityWindowId = data.ReadInt32();
135 int32_t elementId = data.ReadInt32();
136 int32_t focusType = data.ReadInt32();
137 int32_t requestId = data.ReadInt32();
138
139 sptr<IRemoteObject> remote = data.ReadRemoteObject();
140 sptr<IAccessibilityElementOperatorCallback> callback =
141 iface_cast<IAccessibilityElementOperatorCallback>(remote);
142
143 RetError result = FindFocusedElementInfo(accessibilityWindowId, elementId, focusType, requestId, callback);
144 HILOG_DEBUG("FindFocusedElementInfo ret = %{public}d", result);
145 reply.WriteInt32(result);
146
147 return NO_ERROR;
148 }
149
HandleFocusMoveSearch(MessageParcel & data,MessageParcel & reply)150 ErrCode AccessibleAbilityChannelStub::HandleFocusMoveSearch(MessageParcel &data, MessageParcel &reply)
151 {
152 HILOG_DEBUG();
153
154 int32_t accessibilityWindowId = data.ReadInt32();
155 int32_t elementId = data.ReadInt32();
156 int32_t direction = data.ReadInt32();
157 int32_t requestId = data.ReadInt32();
158
159 sptr<IRemoteObject> remote = data.ReadRemoteObject();
160 sptr<IAccessibilityElementOperatorCallback> callback =
161 iface_cast<IAccessibilityElementOperatorCallback>(remote);
162
163 RetError result = FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, callback);
164 HILOG_DEBUG("FocusMoveSearch ret = %{public}d", result);
165 reply.WriteInt32(result);
166
167 return NO_ERROR;
168 }
169
HandleExecuteAction(MessageParcel & data,MessageParcel & reply)170 ErrCode AccessibleAbilityChannelStub::HandleExecuteAction(MessageParcel &data, MessageParcel &reply)
171 {
172 HILOG_DEBUG();
173
174 int32_t accessibilityWindowId = data.ReadInt32();
175 int32_t elementId = data.ReadInt32();
176 int32_t action = data.ReadInt32();
177
178 std::vector<std::string> actionArgumentsKey;
179 std::vector<std::string> actionArgumentsValue;
180 std::map<std::string, std::string> actionArguments;
181
182 if (!data.ReadStringVector(&actionArgumentsKey)) {
183 HILOG_ERROR("ReadStringVector actionArgumentsKey failed");
184 return ERR_INVALID_VALUE;
185 }
186 if (!data.ReadStringVector(&actionArgumentsValue)) {
187 HILOG_ERROR("ReadStringVector actionArgumentsValue failed");
188 return ERR_INVALID_VALUE;
189 }
190 if (actionArgumentsKey.size() != actionArgumentsValue.size()) {
191 HILOG_ERROR("Read actionArguments failed.");
192 return ERR_INVALID_VALUE;
193 }
194 for (size_t i = 0; i < actionArgumentsKey.size(); i++) {
195 actionArguments.insert(make_pair(actionArgumentsKey[i], actionArgumentsValue[i]));
196 }
197
198 int32_t requestId = data.ReadInt32();
199
200 auto callback = iface_cast<IAccessibilityElementOperatorCallback>(data.ReadRemoteObject());
201 if (!callback) {
202 HILOG_ERROR("callback is nullptr");
203 return ERR_INVALID_VALUE;
204 }
205
206 RetError result = ExecuteAction(accessibilityWindowId, elementId, action, actionArguments, requestId, callback);
207 HILOG_DEBUG("ExecuteAction ret = %{public}d", result);
208 reply.WriteInt32(result);
209 return NO_ERROR;
210 }
211
HandleGetWindow(MessageParcel & data,MessageParcel & reply)212 ErrCode AccessibleAbilityChannelStub::HandleGetWindow(MessageParcel &data, MessageParcel &reply)
213 {
214 HILOG_DEBUG();
215
216 int32_t windowId = data.ReadInt32();
217 sptr<AccessibilityWindowInfoParcel> windowInfoParcel = new(std::nothrow) AccessibilityWindowInfoParcel();
218 if (!windowInfoParcel) {
219 HILOG_ERROR("Failed to create windowInfoParcel.");
220 return ERR_NULL_OBJECT;
221 }
222
223 RetError result = GetWindow(windowId, *windowInfoParcel);
224 if (!reply.WriteStrongParcelable(windowInfoParcel)) {
225 HILOG_ERROR("WriteStrongParcelable windows failed");
226 return ERR_INVALID_VALUE;
227 }
228
229 reply.WriteInt32(result);
230 return NO_ERROR;
231 }
232
HandleGetWindows(MessageParcel & data,MessageParcel & reply)233 ErrCode AccessibleAbilityChannelStub::HandleGetWindows(MessageParcel &data, MessageParcel &reply)
234 {
235 HILOG_DEBUG();
236 std::vector<AccessibilityWindowInfo> windows;
237 RetError result = GetWindows(windows);
238 if (!reply.WriteInt32(static_cast<int32_t>(windows.size()))) {
239 HILOG_ERROR("windows.size() write error: %{public}zu, ", windows.size());
240 return ERR_INVALID_VALUE;
241 }
242 for (auto &window : windows) {
243 sptr<AccessibilityWindowInfoParcel> windowInfo = new(std::nothrow) AccessibilityWindowInfoParcel(window);
244 if (!windowInfo) {
245 HILOG_ERROR("Failed to create windowInfo.");
246 return ERR_NULL_OBJECT;
247 }
248 if (!reply.WriteStrongParcelable(windowInfo)) {
249 HILOG_ERROR("WriteStrongParcelable windows failed");
250 return ERR_INVALID_VALUE;
251 }
252 }
253 reply.WriteInt32(result);
254 return NO_ERROR;
255 }
256
HandleGetWindowsByDisplayId(MessageParcel & data,MessageParcel & reply)257 ErrCode AccessibleAbilityChannelStub::HandleGetWindowsByDisplayId(MessageParcel &data, MessageParcel &reply)
258 {
259 HILOG_DEBUG();
260
261 uint64_t displayId = data.ReadUint64();
262 std::vector<AccessibilityWindowInfo> windows;
263 RetError result = GetWindowsByDisplayId(displayId, windows);
264 if (!reply.WriteInt32(static_cast<int32_t>(windows.size()))) {
265 HILOG_ERROR("windows.size() write error: %{public}zu, ", windows.size());
266 return ERR_INVALID_VALUE;
267 }
268 for (auto &window : windows) {
269 sptr<AccessibilityWindowInfoParcel> windowInfo = new(std::nothrow) AccessibilityWindowInfoParcel(window);
270 if (!windowInfo) {
271 HILOG_ERROR("Failed to create windowInfo.");
272 return ERR_NULL_OBJECT;
273 }
274 if (!reply.WriteStrongParcelable(windowInfo)) {
275 HILOG_ERROR("WriteStrongParcelable windows failed");
276 return ERR_INVALID_VALUE;
277 }
278 }
279 reply.WriteInt32(result);
280 return NO_ERROR;
281 }
282
HandleSetOnKeyPressEventResult(MessageParcel & data,MessageParcel & reply)283 ErrCode AccessibleAbilityChannelStub::HandleSetOnKeyPressEventResult(MessageParcel &data, MessageParcel &reply)
284 {
285 HILOG_DEBUG();
286
287 bool handled = data.ReadBool();
288 int32_t sequence = data.ReadInt32();
289 SetOnKeyPressEventResult(handled, sequence);
290
291 return NO_ERROR;
292 }
293
HandleSendSimulateGesturePath(MessageParcel & data,MessageParcel & reply)294 ErrCode AccessibleAbilityChannelStub::HandleSendSimulateGesturePath(MessageParcel &data, MessageParcel &reply)
295 {
296 HILOG_DEBUG();
297
298 sptr<AccessibilityGestureInjectPathParcel> positions =
299 data.ReadStrongParcelable<AccessibilityGestureInjectPathParcel>();
300 if (!positions) {
301 HILOG_ERROR("ReadStrongParcelable<AccessibilityGestureInjectPathParcel> failed");
302 return ERR_INVALID_VALUE;
303 }
304
305 std::shared_ptr<AccessibilityGestureInjectPath> gesturePath =
306 std::make_shared<AccessibilityGestureInjectPath>(*positions);
307 RetError result = SendSimulateGesture(gesturePath);
308 reply.WriteInt32(result);
309 return NO_ERROR;
310 }
311
HandleSetTargetBundleName(MessageParcel & data,MessageParcel & reply)312 ErrCode AccessibleAbilityChannelStub::HandleSetTargetBundleName(MessageParcel &data, MessageParcel &reply)
313 {
314 HILOG_DEBUG();
315 std::vector<std::string> targetBundleNames;
316 int32_t size = data.ReadInt32();
317 for (int32_t i = 0; i < size; i++) {
318 std::string temp = data.ReadString();
319 targetBundleNames.emplace_back(temp);
320 }
321 RetError result = SetTargetBundleName(targetBundleNames);
322 reply.WriteInt32(result);
323 return NO_ERROR;
324 }
325 } // namespace Accessibility
326 } // namespace OHOS