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