• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "parcel.h"
18 
19 using namespace std;
20 
21 namespace OHOS {
22 namespace Accessibility {
AccessibleAbilityChannelStub()23 AccessibleAbilityChannelStub::AccessibleAbilityChannelStub()
24 {
25     HILOG_DEBUG("start.");
26 
27     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFO_BY_ACCESSIBILITYID)] =
28         &AccessibleAbilityChannelStub::HandleSearchElementInfoByAccessibilityId;
29     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEARCH_ELEMENTINFOS_BY_TEXT)] =
30         &AccessibleAbilityChannelStub::HandleSearchElementInfosByText;
31     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::FIND_FOCUSED_ELEMENTINFO)] =
32         &AccessibleAbilityChannelStub::HandleFindFocusedElementInfo;
33     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::FOCUS_MOVE_SEARCH)] =
34         &AccessibleAbilityChannelStub::HandleFocusMoveSearch;
35     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::PERFORM_ACTION)] =
36         &AccessibleAbilityChannelStub::HandleExecuteAction;
37     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_WINDOWS)] =
38         &AccessibleAbilityChannelStub::HandleGetWindows;
39     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::EXECUTE_COMMON_ACTION)] =
40         &AccessibleAbilityChannelStub::HandleExecuteCommonAction;
41     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SET_ON_KEY_PRESS_EVENT_RESULT)] =
42         &AccessibleAbilityChannelStub::HandleSetOnKeyPressEventResult;
43     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_DISPALYRESIZE_SCALE)] =
44         &AccessibleAbilityChannelStub::HandleGetDisplayResizeScale;
45     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_DISPALYRESIZE_CETER_X)] =
46         &AccessibleAbilityChannelStub::HandleGetDisplayResizeCenterX;
47     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_DISPLAYRESIZE_CETER_Y)] =
48         &AccessibleAbilityChannelStub::HandleGetDisplayResizeCenterY;
49     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::GET_DISPLAYRESIZE_RECT)] =
50         &AccessibleAbilityChannelStub::HandleGetDisplayResizeRect;
51     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::RESET_DISPALYRESIZE)] =
52         &AccessibleAbilityChannelStub::HandleResetDisplayResize;
53     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SET_DISPLAYRESIZE_SCALE_AND_CENTER)] =
54         &AccessibleAbilityChannelStub::HandleSetDisplayResizeScaleAndCenter;
55     memberFuncMap_[static_cast<uint32_t>(IAccessibleAbilityChannel::Message::SEND_SIMULATE_GESTURE)] =
56         &AccessibleAbilityChannelStub::HandleSendSimulateGesture;
57 }
58 
~AccessibleAbilityChannelStub()59 AccessibleAbilityChannelStub::~AccessibleAbilityChannelStub()
60 {
61     HILOG_DEBUG("start.");
62     memberFuncMap_.clear();
63 }
64 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int32_t AccessibleAbilityChannelStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
66     MessageOption &option)
67 {
68     HILOG_DEBUG("AccessibleAbilityChannelStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
69         code, option.GetFlags());
70     std::u16string descriptor = AccessibleAbilityChannelStub::GetDescriptor();
71     std::u16string remoteDescriptor = data.ReadInterfaceToken();
72     if (descriptor != remoteDescriptor) {
73         HILOG_INFO("local descriptor is not equal to remote");
74         return ERR_INVALID_STATE;
75     }
76 
77     auto itFunc = memberFuncMap_.find(code);
78     if (itFunc != memberFuncMap_.end()) {
79         auto requestFunc = itFunc->second;
80         if (requestFunc != nullptr) {
81             return (this->*requestFunc)(data, reply);
82         }
83     }
84     HILOG_WARN("AbilityManagerStub::OnRemoteRequest, default case, need check.");
85     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
86 }
87 
HandleSearchElementInfoByAccessibilityId(MessageParcel & data,MessageParcel & reply)88 ErrCode AccessibleAbilityChannelStub::HandleSearchElementInfoByAccessibilityId(MessageParcel &data,
89     MessageParcel &reply)
90 {
91     HILOG_DEBUG("start.");
92 
93     int accessibilityWindowId = data.ReadInt32();
94     int elementId = data.ReadInt64();
95     int requestId = data.ReadInt32();
96 
97     sptr<IRemoteObject> object = data.ReadRemoteObject();
98     if (!object) {
99         HILOG_ERROR("object is nullptr.");
100         return ERR_INVALID_VALUE;
101     }
102     sptr<IAccessibilityElementOperatorCallback> callback =
103         iface_cast<IAccessibilityElementOperatorCallback>(object);
104     if (!callback) {
105         HILOG_ERROR("callback is nullptr.");
106         return ERR_INVALID_VALUE;
107     }
108 
109     int mode = data.ReadInt32();
110     bool result = SearchElementInfoByAccessibilityId(accessibilityWindowId, elementId, requestId, callback, mode);
111     HILOG_DEBUG("SearchElementInfoByAccessibilityId ret = %{public}d", result);
112     reply.WriteBool(result);
113 
114     return NO_ERROR;
115 }
116 
HandleSearchElementInfosByText(MessageParcel & data,MessageParcel & reply)117 ErrCode AccessibleAbilityChannelStub::HandleSearchElementInfosByText(MessageParcel &data,
118     MessageParcel &reply)
119 {
120     HILOG_DEBUG("start.");
121 
122     int accessibilityWindowId = data.ReadInt32();
123     long elementId = data.ReadInt32();
124     string text = data.ReadString();
125     int requestId = data.ReadInt32();
126 
127     sptr<IRemoteObject> object = data.ReadRemoteObject();
128     sptr<IAccessibilityElementOperatorCallback> callback =
129         iface_cast<IAccessibilityElementOperatorCallback>(object);
130 
131     bool result = SearchElementInfosByText(accessibilityWindowId, elementId, text, requestId, callback);
132     HILOG_DEBUG("SearchElementInfosByText ret = %{public}d", result);
133     reply.WriteBool(result);
134 
135     return NO_ERROR;
136 }
137 
HandleFindFocusedElementInfo(MessageParcel & data,MessageParcel & reply)138 ErrCode AccessibleAbilityChannelStub::HandleFindFocusedElementInfo(MessageParcel &data, MessageParcel &reply)
139 {
140     HILOG_DEBUG("start.");
141 
142     int accessibilityWindowId = data.ReadInt32();
143     long elementId = data.ReadInt32();
144     int focusType = data.ReadInt32();
145     int requestId = data.ReadInt32();
146 
147     sptr<IRemoteObject> object = data.ReadRemoteObject();
148     sptr<IAccessibilityElementOperatorCallback> callback =
149         iface_cast<IAccessibilityElementOperatorCallback>(object);
150 
151     bool result = FindFocusedElementInfo(accessibilityWindowId, elementId, focusType, requestId, callback);
152     HILOG_DEBUG("FindFocusedElementInfo ret = %{public}d", result);
153     reply.WriteBool(result);
154 
155     return NO_ERROR;
156 }
157 
HandleFocusMoveSearch(MessageParcel & data,MessageParcel & reply)158 ErrCode AccessibleAbilityChannelStub::HandleFocusMoveSearch(MessageParcel &data, MessageParcel &reply)
159 {
160     HILOG_DEBUG("start.");
161 
162     int accessibilityWindowId = data.ReadInt32();
163     long elementId = data.ReadInt32();
164     int direction = data.ReadInt32();
165     int requestId = data.ReadInt32();
166 
167     sptr<IRemoteObject> object = data.ReadRemoteObject();
168     sptr<IAccessibilityElementOperatorCallback> callback =
169         iface_cast<IAccessibilityElementOperatorCallback>(object);
170 
171     bool result = FocusMoveSearch(accessibilityWindowId, elementId, direction, requestId, callback);
172     HILOG_DEBUG("FocusMoveSearch ret = %{public}d", result);
173     reply.WriteBool(result);
174 
175     return NO_ERROR;
176 }
177 
HandleExecuteAction(MessageParcel & data,MessageParcel & reply)178 ErrCode AccessibleAbilityChannelStub::HandleExecuteAction(MessageParcel &data, MessageParcel &reply)
179 {
180     HILOG_DEBUG("start.");
181 
182     int accessibilityWindowId = data.ReadInt32();
183     long elementId = data.ReadInt32();
184     int action = data.ReadInt32();
185 
186     vector<string> actionArgumentsKey;
187     vector<string> actionArgumentsValue;
188     map<string, string> actionArguments;
189 
190     if (!data.ReadStringVector(&actionArgumentsKey)) {
191         HILOG_ERROR("ReadStringVector actionArgumentsKey failed");
192         return ERR_INVALID_VALUE;
193     }
194     if (!data.ReadStringVector(&actionArgumentsValue)) {
195         HILOG_ERROR("ReadStringVector actionArgumentsValue failed");
196         return ERR_INVALID_VALUE;
197     }
198     if (actionArgumentsKey.size() != actionArgumentsValue.size()) {
199         HILOG_ERROR("Read actionArguments failed.");
200         return ERR_INVALID_VALUE;
201     }
202     for (unsigned int i = 0; i < actionArgumentsKey.size(); i++) {
203         actionArguments.insert(make_pair(actionArgumentsKey[i], actionArgumentsValue[i]));
204     }
205 
206     int requestId = data.ReadInt32();
207 
208     auto callback = iface_cast<IAccessibilityElementOperatorCallback>(data.ReadRemoteObject());
209     if (!callback) {
210         HILOG_ERROR("callback is nullptr");
211         return ERR_INVALID_VALUE;
212     }
213 
214     bool result = ExecuteAction(accessibilityWindowId, elementId, action, actionArguments, requestId, callback);
215     HILOG_DEBUG("ExecuteAction ret = %{public}d", result);
216     reply.WriteBool(result);
217     return NO_ERROR;
218 }
219 
HandleGetWindows(MessageParcel & data,MessageParcel & reply)220 ErrCode AccessibleAbilityChannelStub::HandleGetWindows(MessageParcel &data, MessageParcel &reply)
221 {
222     HILOG_DEBUG("start.");
223 
224     vector<AccessibilityWindowInfo> windows = GetWindows();
225     if (!reply.WriteInt32((int32_t)windows.size())) {
226         HILOG_ERROR("windows.size() write error: %{public}d, ", windows.size());
227         return ERR_INVALID_VALUE;
228     }
229     for (auto &window : windows) {
230         if (!reply.WriteParcelable(&window)) {
231             HILOG_ERROR("write windows failed");
232             return ERR_INVALID_VALUE;
233         }
234     }
235     return NO_ERROR;
236 }
237 
HandleExecuteCommonAction(MessageParcel & data,MessageParcel & reply)238 ErrCode AccessibleAbilityChannelStub::HandleExecuteCommonAction(MessageParcel &data, MessageParcel &reply)
239 {
240     HILOG_DEBUG("start.");
241 
242     int action = data.ReadInt32();
243     bool result = ExecuteCommonAction(action);
244 
245     HILOG_DEBUG("ExecuteCommonAction ret = %{public}d", result);
246     reply.WriteBool(result);
247     return NO_ERROR;
248 }
249 
HandleSetOnKeyPressEventResult(MessageParcel & data,MessageParcel & reply)250 ErrCode AccessibleAbilityChannelStub::HandleSetOnKeyPressEventResult(MessageParcel &data, MessageParcel &reply)
251 {
252     HILOG_DEBUG("start.");
253 
254     bool handled = data.ReadBool();
255     int sequence = data.ReadInt32();
256     SetOnKeyPressEventResult(handled, sequence);
257 
258     return NO_ERROR;
259 }
260 
HandleGetDisplayResizeScale(MessageParcel & data,MessageParcel & reply)261 ErrCode AccessibleAbilityChannelStub::HandleGetDisplayResizeScale(MessageParcel &data, MessageParcel &reply)
262 {
263     HILOG_DEBUG("start.");
264 
265     int displayId = data.ReadInt32();
266     float result = GetDisplayResizeScale(displayId);
267 
268     HILOG_DEBUG("GetDisplayResizeScale ret = %{public}f", result);
269     reply.WriteFloat(result);
270     return NO_ERROR;
271 }
272 
HandleGetDisplayResizeCenterX(MessageParcel & data,MessageParcel & reply)273 ErrCode AccessibleAbilityChannelStub::HandleGetDisplayResizeCenterX(MessageParcel &data, MessageParcel &reply)
274 {
275     HILOG_DEBUG("start.");
276 
277     int displayId = data.ReadInt32();
278     float result = GetDisplayResizeCenterX(displayId);
279 
280     HILOG_DEBUG("GetDisplayResizeCenterX ret = %{public}f", result);
281     reply.WriteFloat(result);
282     return NO_ERROR;
283 }
284 
HandleGetDisplayResizeCenterY(MessageParcel & data,MessageParcel & reply)285 ErrCode AccessibleAbilityChannelStub::HandleGetDisplayResizeCenterY(MessageParcel &data, MessageParcel &reply)
286 {
287     HILOG_DEBUG("start.");
288 
289     int displayId = data.ReadInt32();
290     float result = GetDisplayResizeCenterY(displayId);
291 
292     HILOG_DEBUG("GetDisplayResizeCenterY ret = %{public}f", result);
293     reply.WriteFloat(result);
294     return NO_ERROR;
295 }
296 
HandleGetDisplayResizeRect(MessageParcel & data,MessageParcel & reply)297 ErrCode AccessibleAbilityChannelStub::HandleGetDisplayResizeRect(MessageParcel &data, MessageParcel &reply)
298 {
299     HILOG_DEBUG("start.");
300 
301     int displayId = data.ReadInt32();
302     Rect rect = GetDisplayResizeRect(displayId);
303 
304     HILOG_DEBUG("GetDisplayResizeRect");
305     reply.WriteParcelable(&rect);
306     return NO_ERROR;
307 }
308 
HandleResetDisplayResize(MessageParcel & data,MessageParcel & reply)309 ErrCode AccessibleAbilityChannelStub::HandleResetDisplayResize(MessageParcel &data, MessageParcel &reply)
310 {
311     HILOG_DEBUG("start.");
312 
313     int displayId = data.ReadInt32();
314     bool animate = data.ReadBool();
315     bool result = ResetDisplayResize(displayId, animate);
316 
317     HILOG_DEBUG("ResetDisplayResize ret = %{public}d", result);
318     reply.WriteBool(result);
319     return NO_ERROR;
320 }
321 
HandleSetDisplayResizeScaleAndCenter(MessageParcel & data,MessageParcel & reply)322 ErrCode AccessibleAbilityChannelStub::HandleSetDisplayResizeScaleAndCenter(MessageParcel &data,
323     MessageParcel &reply)
324 {
325     HILOG_DEBUG("start.");
326 
327     int displayId = data.ReadInt32();
328     float scale = data.ReadFloat();
329     float centerX = data.ReadFloat();
330     float centerY = data.ReadFloat();
331     bool animate = data.ReadBool();
332     bool result = SetDisplayResizeScaleAndCenter(displayId, scale, centerX, centerY, animate);
333 
334     HILOG_DEBUG("SetDisplayResizeScaleAndCenter ret = %{public}d", result);
335     reply.WriteBool(result);
336     return NO_ERROR;
337 }
338 
HandleSendSimulateGesture(MessageParcel & data,MessageParcel & reply)339 ErrCode AccessibleAbilityChannelStub::HandleSendSimulateGesture(MessageParcel &data, MessageParcel &reply)
340 {
341     HILOG_DEBUG("start.");
342 
343     int32_t requestId = data.ReadInt32();
344 
345     vector<GesturePathDefine> gestureSteps;
346     int32_t stepSize = data.ReadInt32();
347     if (!stepSize) {
348         HILOG_ERROR("stepSize is 0");
349         return ERROR;
350     }
351     for (int32_t i = 0; i < stepSize; i++) {
352         std::shared_ptr<GesturePathDefine> gestureStep(data.ReadParcelable<GesturePathDefine>());
353         if (!gestureStep) {
354             HILOG_ERROR("ReadParcelable<GesturePathDefine> failed");
355             return ERR_INVALID_VALUE;
356         }
357         gestureSteps.emplace_back(*gestureStep);
358     }
359     SendSimulateGesture(requestId, gestureSteps);
360     return NO_ERROR;
361 }
362 } // namespace Accessibility
363 } // namespace OHOS