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 child node information by accessibility ID and filtered by text and set the result by callback.
66      * @param elementId: The unique id of the component 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 text  Filter for the child components to matched with the text
70      */
71     virtual void SearchElementInfosByText(const int64_t elementId, const std::string &text,
72         const int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
73 
74     /**
75      * @brief Make the node information of the component focused by the focus type specified.
76      * @param elementId: The unique id of the component ID.
77      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
78      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
79      * @param focusType FOCUS_TYPE_ACCESSIBILITY: accessibility focus
80      *                  FOCUS_TYPE_INPUT: text input focus
81      */
82     virtual void FindFocusedElementInfo(const int64_t elementId, const int32_t focusType, const int32_t requestId,
83         const sptr<IAccessibilityElementOperatorCallback> &callback) override;
84 
85     /**
86      * @brief Make the node info by current focus move direction.
87      * @param elementId: The unique id of the component ID.
88      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
89      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
90      * @param direction Refer to AccessibilityElementInfo.FocusMoveDirection(UP/DOWN/LEFT/RIGHT/FORWARD/BACKWARD)
91      */
92     virtual void FocusMoveSearch(const int64_t elementId, const int32_t direction, const int32_t requestId,
93         const sptr<IAccessibilityElementOperatorCallback> &callback) override;
94 
95     /**
96      * @brief To return the result of perform action.
97      * @param elementId: The unique id of the component ID.
98      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
99      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
100      * @param action Refer to [AccessibilityElementInfo.ActionType]
101      * @param actionArguments The parameter for action type. such as:
102      *      action: ACCESSIBILITY_ACTION_NEXT_HTML_ITEM,
103      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
104      *      action: ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM,
105      *                  actionArguments(ACTION_ARGU_HTML_ELEMENT,HtmlItemType)
106      *      action: ACCESSIBILITY_ACTION_NEXT_TEXT,
107      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
108      *      action: ACCESSIBILITY_ACTION_PREVIOUS_TEXT,
109      *                  actionArguments(ACTION_ARGU_MOVE_UNIT,MOVE_UNIT_XXX)
110      *      action: ACCESSIBILITY_ACTION_SET_SELECTION,
111      *                  actionArguments({ACTION_ARGU_SELECT_TEXT_START,"1"(start location)},
112      *                                  {ACTION_ARGU_SELECT_TEXT_END,"10"(end location)})
113      *      action: ACCESSIBILITY_ACTION_SET_TEXT,
114      *                  actionArguments(ACTION_ARGU_SET_TEXT,"the text of setted")
115      */
116     virtual void ExecuteAction(const int64_t elementId, const int32_t action,
117         const std::map<std::string, std::string> &actionArguments,
118         int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback) override;
119 
120     /**
121      * @brief To return the result of cursor position.
122      * @param elementId: The unique id of the component ID.
123      * @param requestId Matched the request and response. It needn't cared by ACE, transfer it by callback only.
124      * @param callback  To transfer the node info to ASAC and it defined by ASAC.
125      */
126     virtual void GetCursorPosition(const int64_t elementId, const int32_t requestId,
127         const sptr<IAccessibilityElementOperatorCallback> &callback) override;
128 
129     /**
130      * @brief The function is called while accessibility System check the id of window is not equal
131      * to the id of active window when sendAccessibility.
132      */
133 
134     virtual void ClearFocus() override;
135 
136     /**
137      * @brief the low layer is notified by the function called while accessibility system execute
138      * the function of executeAction from AS to check the all low windows cared the outside event.
139      * Example: PopupWindow receive the OUTSIDE_EVENT to close itself.
140      */
141     virtual void OutsideTouch() override;
142 
143     /**
144      * @brief Get the window id related with operator object
145      * @return The unique id of the window related with operator object.
146      */
147     int32_t GetWindowId();
148 
149     /**
150      * @brief Set the elementId, childWindowId, childtreeId to AA
151      * @param elementId The element ID.
152      * @param childTreeId The childTree ID.
153      * @param childWindowId The childwindow ID.
154      */
155     virtual void SetChildTreeIdAndWinId(const int64_t elementId, const int32_t childTreeId,
156         const int32_t childWindowId) override;
157 
158     /**
159      * @brief Set the belongTree id to AA
160      * @param  treeId The tree ID.
161      */
162     virtual void SetBelongTreeId(const int32_t treeId) override;
163 
164     /**
165      * @brief Set the parent window id to AA
166      * @param  parentWindowId The parentWindowId ID.
167      */
168     virtual void SetParentWindowId(const int32_t parentWindowId) override;
169 
170     /**
171      * @brief Set the element information by accessibility id to AA.
172      * @param infos The element info searched by accessibility id.
173      * @param requestId The request id from AA, it is used to match with request and response.
174      */
175     void SetSearchElementInfoByAccessibilityIdResult(const std::list<AccessibilityElementInfo> &infos,
176         const int32_t requestId);
177 
178     /**
179      * @brief Set whether to perform filtering.
180      */
181     static void SetFiltering(std::vector<AccessibilityElementInfo> &filterInfos);
182 
183     /**
184      * @brief Set the element information matched with text to AA.
185      * @param infos The element information searched matched with text.
186      * @param requestId The request id from AA, it is used to match with request and response.
187      */
188     void SetSearchElementInfoByTextResult(const std::list<AccessibilityElementInfo> &infos,
189         const int32_t requestId);
190 
191     /**
192      * @brief Set the element information matched with focus type to AA.
193      * @param info The element information searched matched with focus type.
194      * @param requestId The request id from AA, it is used to match with request and response.
195      */
196     void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, const int32_t requestId);
197 
198     /**
199      * @brief Set the element information by focus direction to AA.
200      * @param info The element information searched by focus direction.
201      * @param requestId The request id from AA, it is used to match with request and response.
202      */
203     void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId);
204 
205     /**
206      * @brief Set the result of action executed to AA.
207      * @param succeeded True: The action is executed successfully; otherwise is false.
208      * @param requestId The request id from AA, it is used to match with request and response.
209      */
210     void SetExecuteActionResult(const bool succeeded, const int32_t requestId);
211 
212     /**
213      * @brief Set the result of cursor position to AA.
214      * @param cursorPosition The cursorPosition to be returned.
215      * @param requestId The request id from AA, it is used to match with request and response.
216      */
217     void SetCursorPositionResult(const int32_t cursorPosition, const int32_t requestId);
218 
219     static sptr<IAccessibilityElementOperatorCallback> GetCallbackByRequestId(const int32_t requestId);
220     static void EraseCallback(const int32_t requestId);
221 
222 private:
223     int32_t AddRequest(int32_t requestId, const sptr<IAccessibilityElementOperatorCallback> &callback);
224 
225     static ffrt::mutex requestsMutex_;
226     int32_t windowId_ = 0;
227     AccessibilityElementOperatorCallback &operatorCallback_;
228     std::shared_ptr<AccessibilityElementOperator> operator_ = nullptr;
229     static std::unordered_map<int32_t, sptr<IAccessibilityElementOperatorCallback>> requests_;
230     DISALLOW_COPY_AND_MOVE(AccessibilityElementOperatorImpl);
231 };
232 
233 template<class T>
TranslateListToVector(const std::list<T> & originList)234 std::vector<T> TranslateListToVector(const std::list<T> &originList)
235 {
236     std::size_t len = originList.size();
237     std::vector<T> destVector(len);
238     std::copy(originList.begin(), originList.end(), destVector.begin());
239     return destVector;
240 }
241 
242 template<class T>
TranslateVectorToList(const std::vector<T> & originVector)243 std::list<T> TranslateVectorToList(const std::vector<T> &originVector)
244 {
245     size_t len = originVector.length();
246     std::list<T> destList(len);
247     std::copy(originVector.begin(), originVector.end(), destList.begin());
248     return destList;
249 }
250 } // namespace Accessibility
251 } // namespace OHOS
252 #endif // ACCESSIBILITY_ELEMENT_OPERATOR_IMPL_H