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