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 #ifndef ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
17 #define ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H
18
19 #include <memory>
20 #include <unordered_map>
21 #include "accessibility_element_operator_callback.h"
22 #include "accessibility_element_operator_stub.h"
23 #include "accessibility_element_operator.h"
24 #include "nocopyable.h"
25 #include "ffrt.h"
26
27 namespace OHOS {
28 namespace Accessibility {
29 /*
30 * The class define the interface for UI to implement.
31 * It is triggered by ABMS when AA to request the accessibility information.
32 */
33 class AccessibilityElementOperatorImpl : public AccessibilityElementOperatorStub {
34 public:
35 /**
36 * @brief construct function
37 * @param windowId The window id.
38 * @param operation The object implemented by ACE.
39 * @param callback The callback which is to transfer the result from ACE.
40 */
41 explicit AccessibilityElementOperatorImpl(int32_t windowId,
42 const std::shared_ptr<AccessibilityElementOperator> &operation,
43 AccessibilityElementOperatorCallback &callback);
44
45 /**
46 * @brief destruct function
47 */
48 ~AccessibilityElementOperatorImpl();
49
50 /**
51 * @brief Make the node information by accessibility ID and set the result by callback.
52 * @param elementId: The unique id of the component ID.
53 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
54 * @param callback To transfer the node info to ASAC and it defined by ASAC.
55 * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also.
56 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also.
57 * PREFETCH_CHILDREN: Need to make the child node info also.
58 * otherwise: Make the node information by elementId only.
59 */
60 virtual void SearchElementInfoByAccessibilityId(const int64_t elementId, const int32_t requestId,
61 const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
62 bool isFilter = false) override;
63
64 /**
65 * @brief Make the node information by window ID and set the result by callback.
66 * @param windowId: The window id.
67 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
68 * @param callback To transfer the node info to ASAC and it defined by ASAC.
69 * @param mode PREFETCH_PREDECESSORS: Need to make the parent node info also.
70 * PREFETCH_SIBLINGS: Need to make the sister/brothers node info also.
71 * PREFETCH_CHILDREN: Need to make the child node info also.
72 * otherwise: Make the node information by elementId only.
73 */
74 virtual void SearchDefaultFocusedByWindowId(const int32_t windowId, const int32_t requestId,
75 const sptr<IAccessibilityElementOperatorCallback> &callback, const int32_t mode,
76 bool isFilter = false) override;
77
78 /**
79 * @brief Make the child node information by accessibility ID and filtered by text and set the result by callback.
80 * @param elementId: The unique id of the component ID.
81 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
82 * @param callback To transfer the node info to ASAC and it defined by ASAC.
83 * @param text Filter for the child components to matched with the text
84 */
85 virtual void SearchElementInfosByText(const int64_t elementId, const std::string &text,
86 const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
87
88 /**
89 * @brief Make the node information of the component focused by the focus type specified.
90 * @param elementId: The unique id of the component ID.
91 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
92 * @param callback To transfer the node info to ASAC and it defined by ASAC.
93 * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
94 * FOCUS_TYPE_INPUT: text input focus
95 */
96 virtual void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
97 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
98
99 /**
100 * @brief Make the node info by current focus move direction.
101 * @param elementId: The unique id of the component ID.
102 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
103 * @param callback To transfer the node info to ASAC and it defined by ASAC.
104 * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
105 */
106 virtual void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
107 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
108
109 /**
110 * @brief To return the result of perform action.
111 * @param elementId: The unique id of the component ID.
112 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
113 * @param callback To transfer the node info to ASAC and it defined by ASAC.
114 * @param action Refer to [AccessibilityElementInfo.ActionType]
115 * @param actionArguments The parameter for action type. such as:
116 * action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
117 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
118 * action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
119 * actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
120 * action: ACCESSIBILITY_ACTION_NEXT_TEXT,
121 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
122 * action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
123 * actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
124 * action: ACCESSIBILITY_ACTION_SET_SELECTION,
125 * actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
126 * {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
127 * action: ACCESSIBILITY_ACTION_SET_TEXT,
128 * actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
129 */
130 virtual void ExecuteAction(const int64_t elementId, const int32_t action,
131 const std::map<std::string, std::string> &actionArguments,
132 int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
133
134 /**
135 * @brief To return the result of cursor position.
136 * @param elementId: The unique id of the component ID.
137 * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
138 * @param callback To transfer the node info to ASAC and it defined by ASAC.
139 */
140 virtual void GetCursorPosition(const int64_t elementId, const int32_t requestId,
141 const sptr<IAccessibilityElementOperatorCallback> &callback) override;
142
143 /**
144 * @brief The function is called while accessibility System check the id of window is not equal
145 * to the id of active window when sendAccessibility.
146 */
147
148 virtual void ClearFocus() override;
149
150 /**
151 * @brief the low layer is notified by the function called while accessibility system execute
152 * the function of executeAction from AS to check the all low windows cared the outside event.
153 * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
154 */
155 virtual void OutsideTouch() override;
156
157 /**
158 * @brief Get the window id related with operator object
159 * @return The unique id of the window related with operator object.
160 */
161 int32_t GetWindowId();
162
163 /**
164 * @brief Set the elementId, childWindowId, childtreeId to AA
165 * @param elementId The element ID.
166 * @param childTreeId The childTree ID.
167 * @param childWindowId The childwindow ID.
168 */
169 virtual void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t childTreeId,
170 const int32_t childWindowId) override;
171
172 /**
173 * @brief Set the belongTree id to AA
174 * @param treeId The tree ID.
175 */
176 virtual void SetBelongTreeId(const int32_t treeId) override;
177
178 /**
179 * @brief Set the parent window id to AA
180 * @param parentWindowId The parentWindowId ID.
181 */
182 virtual void SetParentWindowId(const int32_t parentWindowId) override;
183
184 /**
185 * @brief Set the element information by accessibility id to AA.
186 * @param infos The element info searched by accessibility id.
187 * @param requestId The request id from AA, it is used to match with request and response.
188 */
189 void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
190 const int32_t requestId);
191
192 /**
193 * @brief Set the element information by window id to AA.
194 * @param infos The element info searched by window id.
195 * @param requestId The request id from AA, it is used to match with request and response.
196 */
197 void SetSearchDefaultFocusByWindowIdResult(const std::list<AccessibilityElementInfo> &infos,
198 const int32_t requestId);
199
200 /**
201 * @brief Set whether to perform filtering.
202 */
203 static void SetFiltering(std::vector<AccessibilityElementInfo> &filterInfos);
204
205 /**
206 * @brief Set the element information matched with text to AA.
207 * @param infos The element information searched matched with text.
208 * @param requestId The request id from AA, it is used to match with request and response.
209 */
210 void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
211 const int32_t requestId);
212
213 /**
214 * @brief Set the element information matched with focus type to AA.
215 * @param info The element information searched matched with focus type.
216 * @param requestId The request id from AA, it is used to match with request and response.
217 */
218 void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
219
220 /**
221 * @brief Set the element information by focus direction to AA.
222 * @param info The element information searched by focus direction.
223 * @param requestId The request id from AA, it is used to match with request and response.
224 */
225 void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
226
227 /**
228 * @brief Set the result of action executed to AA.
229 * @param succeeded True: The action is executed successfully; otherwise is false.
230 * @param requestId The request id from AA, it is used to match with request and response.
231 */
232 void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
233
234 /**
235 * @brief Set the result of cursor position to AA.
236 * @param cursorPosition The cursorPosition to be returned.
237 * @param requestId The request id from AA, it is used to match with request and response.
238 */
239 void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId);
240
241 static sptr<IAccessibilityElementOperatorCallback> GetCallbackByRequestId(const int32_t requestId);
242 static void EraseCallback(const int32_t requestId);
243
244 private:
245 int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
246
247 static ffrt::mutex requestsMutex_;
248 int32_t windowId_ = 0;
249 AccessibilityElementOperatorCallback &operatorCallback_;
250 std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
251 static std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
252 DISALLOW_COPY_AND_MOVE(AccessibilityElementOperatorImpl);
253 };
254
255 template<class T>
TranslateListToVector(const std::list<T> & originList)256 std::vector<T> TranslateListToVector(const std::list<T> &originList)
257 {
258 std::size_t len = originList.size();
259 std::vector<T> destVector(len);
260 std::copy(originList.begin(), originList.end(), destVector.begin());
261 return destVector;
262 }
263
264 template<class T>
TranslateVectorToList(const std::vector<T> & originVector)265 std::list<T> TranslateVectorToList(const std::vector<T> &originVector)
266 {
267 size_t len = originVector.length();
268 std::list<T> destList(len);
269 std::copy(originVector.begin(), originVector.end(), destList.begin());
270 return destList;
271 }
272 } // namespace Accessibility
273 } // namespace OHOS
274 #endif // ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H