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 }
55 
~AccessibilityElementOperatorStub()56 AccessibilityElementOperatorStub::~AccessibilityElementOperatorStub()
57 {
58 }
59 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)60 int AccessibilityElementOperatorStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
61     MessageParcel &reply, MessageOption &option)
62 {
63     HILOG_DEBUG("cmd = %{public}u, flags = %{public}d", code, option.GetFlags());
64     std::u16string descriptor = AccessibilityElementOperatorStub::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     ErrCode result_code = ERR_NONE;
72     SWITCH_BEGIN(code)
73     ACCESSIBILITY_ELEMENT_OPERATOR_STUB_CASES()
74     SWITCH_END()
75 
76     if (result_code != ERR_CODE_DEFAULT) {
77         return result_code;
78     }
79 
80     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 }
82 
HandleSearchElementInfoByAccessibilityId(MessageParcel & data,MessageParcel & reply)83 ErrCode AccessibilityElementOperatorStub::HandleSearchElementInfoByAccessibilityId(MessageParcel &data,
84     MessageParcel &reply)
85 {
86     HILOG_DEBUG();
87 
88     int64_t elementId = data.ReadInt64();
89     int32_t requestId = data.ReadInt32();
90     sptr<IRemoteObject> remote = data.ReadRemoteObject();
91     if (remote == nullptr) {
92         HILOG_ERROR("remote is nullptr.");
93         return ERR_INVALID_VALUE;
94     }
95 
96     sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
97     if (callback == nullptr) {
98         HILOG_ERROR("callback is nullptr");
99         return ERR_INVALID_VALUE;
100     }
101     int32_t mode = data.ReadInt32();
102     bool isFilter = data.ReadBool();
103     SearchElementInfoByAccessibilityId(elementId, requestId, callback, mode, isFilter);
104     return NO_ERROR;
105 }
106 
HandleSearchElementInfosByText(MessageParcel & data,MessageParcel & reply)107 ErrCode AccessibilityElementOperatorStub::HandleSearchElementInfosByText(MessageParcel &data,
108     MessageParcel &reply)
109 {
110     HILOG_DEBUG();
111 
112     int64_t elementId = data.ReadInt64();
113     std::string text = data.ReadString();
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     SearchElementInfosByText(elementId, text, requestId, callback);
127     return NO_ERROR;
128 }
129 
HandleFindFocusedElementInfo(MessageParcel & data,MessageParcel & reply)130 ErrCode AccessibilityElementOperatorStub::HandleFindFocusedElementInfo(MessageParcel &data,
131     MessageParcel &reply)
132 {
133     HILOG_DEBUG();
134 
135     int64_t elementId = data.ReadInt64();
136     int32_t focusType = data.ReadInt32();
137     int32_t requestId = data.ReadInt32();
138     sptr<IRemoteObject> remote = data.ReadRemoteObject();
139     if (remote == nullptr) {
140         HILOG_ERROR("remote is nullptr.");
141         return ERR_INVALID_VALUE;
142     }
143 
144     sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
145     if (callback == nullptr) {
146         HILOG_ERROR("callback is nullptr");
147         return ERR_INVALID_VALUE;
148     }
149     FindFocusedElementInfo(elementId, focusType, requestId, callback);
150     return NO_ERROR;
151 }
152 
HandleFocusFind(MessageParcel & data,MessageParcel & reply)153 ErrCode AccessibilityElementOperatorStub::HandleFocusFind(MessageParcel &data, MessageParcel &reply)
154 {
155     HILOG_DEBUG();
156 
157     int64_t elementId = data.ReadInt64();
158     int32_t direction = data.ReadInt32();
159     int32_t requestId = data.ReadInt32();
160     sptr<IRemoteObject> remote = data.ReadRemoteObject();
161     if (remote == nullptr) {
162         HILOG_ERROR("remote is nullptr.");
163         return ERR_INVALID_VALUE;
164     }
165 
166     sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
167     if (callback == nullptr) {
168         HILOG_ERROR("callback is nullptr");
169         return ERR_INVALID_VALUE;
170     }
171     FocusMoveSearch(elementId, direction, requestId, callback);
172     return NO_ERROR;
173 }
174 
HandleExecuteAction(MessageParcel & data,MessageParcel & reply)175 ErrCode AccessibilityElementOperatorStub::HandleExecuteAction(MessageParcel &data, MessageParcel &reply)
176 {
177     HILOG_DEBUG();
178     std::vector<std::string> argumentKey;
179     std::vector<std::string> argumentValue;
180     int64_t elementId = data.ReadInt64();
181     int32_t action = data.ReadInt32();
182     if (!data.ReadStringVector(&argumentKey)) {
183         return ERR_TRANSACTION_FAILED;
184     }
185     if (!data.ReadStringVector(&argumentValue)) {
186         return ERR_TRANSACTION_FAILED;
187     }
188     if (argumentKey.size() != argumentValue.size()) {
189         return ERR_TRANSACTION_FAILED;
190     }
191     std::map<std::string, std::string> arguments;
192     for (size_t i = 0;i < argumentKey.size(); i++) {
193         arguments.insert(std::pair<std::string, std::string>(argumentKey[i], argumentValue[i]));
194     }
195     int32_t requestId = data.ReadInt32();
196     sptr<IRemoteObject> remote = data.ReadRemoteObject();
197     if (remote == nullptr) {
198         HILOG_ERROR("remote is nullptr.");
199         return ERR_INVALID_VALUE;
200     }
201 
202     sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
203     if (callback == nullptr) {
204         HILOG_ERROR("callback is nullptr");
205         return ERR_INVALID_VALUE;
206     }
207     ExecuteAction(elementId, action, arguments, requestId, callback);
208     return NO_ERROR;
209 }
210 
HandleGetCursorPosition(MessageParcel & data,MessageParcel & reply)211 ErrCode AccessibilityElementOperatorStub::HandleGetCursorPosition(MessageParcel &data, MessageParcel &reply)
212 {
213     HILOG_DEBUG();
214     int64_t elementId = data.ReadInt64();
215     int32_t requestId = data.ReadInt32();
216     sptr<IRemoteObject> remote = data.ReadRemoteObject();
217     if (remote == nullptr) {
218         HILOG_ERROR("remote is nullptr.");
219         return ERR_INVALID_VALUE;
220     }
221 
222     sptr<IAccessibilityElementOperatorCallback> callback = iface_cast<IAccessibilityElementOperatorCallback>(remote);
223     if (callback == nullptr) {
224         HILOG_ERROR("callback is nullptr");
225         return ERR_INVALID_VALUE;
226     }
227     GetCursorPosition(elementId, requestId, callback);
228     return NO_ERROR;
229 }
230 
HandleClearFocus(MessageParcel & data,MessageParcel & reply)231 ErrCode AccessibilityElementOperatorStub::HandleClearFocus(MessageParcel &data, MessageParcel &reply)
232 {
233     HILOG_DEBUG();
234 
235     ClearFocus();
236 
237     return NO_ERROR;
238 }
239 
HandleOutsideTouch(MessageParcel & data,MessageParcel & reply)240 ErrCode AccessibilityElementOperatorStub::HandleOutsideTouch(MessageParcel &data, MessageParcel &reply)
241 {
242     HILOG_DEBUG();
243 
244     OutsideTouch();
245 
246     return NO_ERROR;
247 }
248 
HandleSetChildTreeIdAndWinId(MessageParcel & data,MessageParcel & reply)249 ErrCode AccessibilityElementOperatorStub::HandleSetChildTreeIdAndWinId(MessageParcel &data, MessageParcel &reply)
250 {
251     HILOG_DEBUG();
252     int64_t elementId = data.ReadInt64();
253     int32_t treeId = data.ReadInt32();
254     int32_t childWindowId = data.ReadInt32();
255     SetChildTreeIdAndWinId(elementId, treeId, childWindowId);
256     return NO_ERROR;
257 }
258 
HandleSetBelongTreeId(MessageParcel & data,MessageParcel & reply)259 ErrCode AccessibilityElementOperatorStub::HandleSetBelongTreeId(MessageParcel &data, MessageParcel &reply)
260 {
261     HILOG_DEBUG();
262     int32_t treeId = data.ReadInt32();
263 
264     SetBelongTreeId(treeId);
265     return NO_ERROR;
266 }
267 
HandleSetParentWindowId(MessageParcel & data,MessageParcel & reply)268 ErrCode AccessibilityElementOperatorStub::HandleSetParentWindowId(MessageParcel &data, MessageParcel &reply)
269 {
270     HILOG_DEBUG();
271     int32_t iParentWindowId = data.ReadInt32();
272 
273     SetParentWindowId(iParentWindowId);
274     return NO_ERROR;
275 }
276 } // namespace Accessibility
277 } // namespace OHOS