2e3ba3a966d42bcd6dc3dfdd88ab7481a9c37443
[WebKit-https.git] / Source / WebCore / accessibility / isolatedtree / AXIsolatedTree.h
1 /*
2  * Copyright (C) 2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
29
30 #include "AccessibilityObjectInterface.h"
31 #include "PageIdentifier.h"
32 #include <wtf/HashMap.h>
33 #include <wtf/RefPtr.h>
34 #include <wtf/ThreadSafeRefCounted.h>
35
36 namespace WTF {
37 class TextStream;
38 }
39
40 namespace WebCore {
41
42 class AXIsolatedObject;
43 class AXObjectCache;
44 class Page;
45
46 using AXIsolatedTreeID = unsigned;
47
48 enum class AXPropertyName : uint16_t {
49     ARIAControlsElements,
50     ARIADetailsElements,
51     DropEffects,
52     ARIAErrorMessageElements,
53     ARIAIsMultiline,
54     ARIAFlowToElements,
55     ARIALandmarkRoleDescription,
56     ARIATreeItemContent,
57     ARIATreeRows,
58     ARIARoleAttribute,
59     ARIAOwnsElements,
60     AXColumnCount,
61     AXColumnIndex,
62     AXRowCount,
63     AXRowIndex,
64     AccessKey,
65     AccessibilityButtonState,
66     AccessibilityDescription,
67     AccessibilityText,
68     ActionVerb,
69     AutoCompleteValue,
70     BlockquoteLevel,
71     BoundingBoxRect,
72     CanHaveSelectedChildren,
73     CanSetExpandedAttribute,
74     CanSetFocusAttribute,
75     CanSetNumericValue,
76     CanSetSelectedAttribute,
77     CanSetSelectedChildrenAttribute,
78     CanSetTextRangeAttributes,
79     CanSetValueAttribute,
80     CanvasHasFallbackContent,
81 #if PLATFORM(MAC)
82     CaretBrowsingEnabled,
83 #endif
84     Cells,
85     ClassList,
86     ClickPoint,
87     ColorValue,
88     Columns,
89     ColumnCount,
90     ColumnHeader,
91     ColumnHeaders,
92     ColumnIndex,
93     ColumnIndexRange,
94     ComputedLabel,
95     ComputedRoleString,
96     CurrentState,
97     CurrentValue,
98     DatetimeAttributeValue,
99     DecrementButton,
100     Description,
101     DisclosedByRow,
102     DisclosedRows,
103     DocumentEncoding,
104     DocumentLinks,
105     DocumentURI,
106     EditableAncestor,
107     ElementRect,
108     EstimatedLoadingProgress,
109     ExpandedTextValue,
110     ExposesTitleUIElement,
111     FileUploadButtonReturnsValueInTitle,
112     FocusableAncestor,
113     HasARIAValueNow,
114     HasApplePDFAnnotationAttribute,
115     HasBoldFont,
116     HasChildren,
117     HasHighlighting,
118     HasItalicFont,
119     HasPlainText,
120     HasPopup,
121     HasUnderline,
122     HeaderContainer,
123     HeadingLevel,
124     HelpText,
125     HierarchicalLevel,
126     HighestEditableAncestor,
127     HorizontalScrollBar,
128     IdentifierAttribute,
129     InvalidStatus,
130     IncrementButton,
131     IsAccessibilityIgnored,
132     IsActiveDescendantOfFocusedContainer,
133     IsAnonymousMathOperator,
134     IsGrabbed,
135     IsARIATreeGridRow,
136     IsAttachment,
137     IsButton,
138     IsBusy,
139     IsChecked,
140     IsCollapsed,
141     IsColumnHeaderCell,
142     IsControl,
143     IsDataTable,
144     IsDescriptionList,
145     IsEnabled,
146     IsExpanded,
147     IsExposable,
148     IsFieldset,
149     IsFileUploadButton,
150     IsFocused,
151     IsGroup,
152     IsImageMapLink,
153     IsIncrementor,
154     IsIndeterminate,
155     IsInlineText,
156     IsInputImage,
157     IsInsideLiveRegion,
158     IsHeading,
159     IsHovered,
160     IsKeyboardFocusable,
161     IsLandmark,
162     IsLink,
163     IsLinked,
164     IsList,
165     IsListBox,
166     IsLoaded,
167     IsMathElement,
168     IsMathFraction,
169     IsMathFenced,
170     IsMathSubscriptSuperscript,
171     IsMathRow,
172     IsMathUnderOver,
173     IsMathRoot,
174     IsMathSquareRoot,
175     IsMathText,
176     IsMathNumber,
177     IsMathOperator,
178     IsMathFenceOperator,
179     IsMathSeparatorOperator,
180     IsMathIdentifier,
181     IsMathTable,
182     IsMathTableRow,
183     IsMathTableCell,
184     IsMathMultiscript,
185     IsMathToken,
186     IsMathScriptObject,
187     IsMediaTimeline,
188     IsMenu,
189     IsMenuBar,
190     IsMenuButton,
191     IsMenuItem,
192     IsMenuList,
193     IsMenuListOption,
194     IsMenuListPopup,
195     IsMenuRelated,
196     IsMeter,
197     IsMultiSelectable,
198     IsOrderedList,
199     IsOutput,
200     IsPasswordField,
201     IsPressed,
202     IsProgressIndicator,
203     IsRangeControl,
204     IsRequired,
205     IsRowHeaderCell,
206     IsScrollbar,
207     IsSearchField,
208     IsSelected,
209     IsSelectedOptionActive,
210     IsShowingValidationMessage,
211     IsSlider,
212     IsStyleFormatGroup,
213     IsTable,
214     IsTableCell,
215     IsTableColumn,
216     IsTableRow,
217     IsTextControl,
218     IsTree,
219     IsTreeItem,
220     IsUnorderedList,
221     IsUnvisited,
222     IsValueAutofilled,
223     IsValueAutofillAvailable,
224     IsVisible,
225     IsVisited,
226     KeyShortcutsValue,
227     Language,
228     LayoutCount,
229     LinkRelValue,
230     LinkedUIElements,
231     LiveRegionAtomic,
232     LiveRegionRelevant,
233     LiveRegionStatus,
234     MathFencedOpenString,
235     MathFencedCloseString,
236     MathLineThickness,
237     MathPrescripts,
238     MathPostscripts,
239     MathRadicandObject,
240     MathRootIndexObject,
241     MathUnderObject,
242     MathOverObject,
243     MathNumeratorObject,
244     MathDenominatorObject,
245     MathBaseObject,
246     MathSubscriptObject,
247     MathSuperscriptObject,
248     MaxValueForRange,
249     MinValueForRange,
250     Orientation,
251     Path,
252     PlaceholderValue,
253     PressedIsPresent,
254     PopupValue,
255     PosInSet,
256     PreventKeyboardDOMEventDispatch,
257     ReadOnlyValue,
258     RoleValue,
259     RolePlatformString,
260     RoleDescription,
261     Rows,
262     RowCount,
263     RowHeaders,
264     RowIndex,
265     RowIndexRange,
266     SelectedChildren,
267     SelectedRadioButton,
268     SelectedTabItem,
269     SessionID,
270     SetSize,
271     SortDirection,
272     SpeakAs,
273     SpeechHint,
274     StringValue,
275     SupportsRowCountChange,
276     SupportsDragging,
277     SupportsDropping,
278     SupportsARIAOwns,
279     SupportsCurrent,
280     SupportsDatetimeAttribute,
281     SupportsExpanded,
282     SupportsExpandedTextValue,
283     SupportsLiveRegion,
284     SupportsPath,
285     SupportsPosInSet,
286     SupportsPressAction,
287     SupportsRangeValue,
288     SupportsRequiredAttribute,
289     SupportsSelectedRows,
290     SupportsSetSize,
291     TabChildren,
292     TableLevel,
293     TagName,
294     TextLength,
295     Title,
296     TitleAttributeValue,
297     TitleUIElement,
298     URL,
299     ValueAutofillButtonType,
300     ValueDescription,
301     ValueForRange,
302     ValidationMessage,
303     VerticalScrollBar,
304     VisibleChildren,
305     VisibleRows,
306     WebArea,
307 };
308
309 using AXPropertyValueVariant = Variant<std::nullptr_t, String, bool, int, unsigned, double, float, uint64_t, Color, URL, LayoutRect, FloatRect, AXID, IntPoint, OptionSet<SpeakAs>, std::pair<unsigned, unsigned>, Vector<AccessibilityText>, Vector<AXID>, Vector<std::pair<AXID, AXID>>, Vector<String>, Path>;
310 using AXPropertyMap = HashMap<AXPropertyName, AXPropertyValueVariant, WTF::IntHash<AXPropertyName>, WTF::StrongEnumHashTraits<AXPropertyName>>;
311
312 struct AXPropertyChange {
313     AXID axID { InvalidAXID }; // ID of the object whose properties changed.
314     AXPropertyMap properties; // Changed properties.
315 };
316
317 class AXIsolatedTree : public ThreadSafeRefCounted<AXIsolatedTree> {
318     WTF_MAKE_NONCOPYABLE(AXIsolatedTree); WTF_MAKE_FAST_ALLOCATED;
319     friend WTF::TextStream& operator<<(WTF::TextStream&, AXIsolatedTree&);
320 public:
321     static Ref<AXIsolatedTree> create();
322     virtual ~AXIsolatedTree();
323
324     static Ref<AXIsolatedTree> createTreeForPageID(PageIdentifier);
325     static void removeTreeForPageID(PageIdentifier);
326
327     static RefPtr<AXIsolatedTree> treeForPageID(PageIdentifier);
328     static RefPtr<AXIsolatedTree> treeForID(AXIsolatedTreeID);
329     AXObjectCache* axObjectCache() const { return m_axObjectCache; }
330     void setAXObjectCache(AXObjectCache* axObjectCache) { m_axObjectCache = axObjectCache; }
331
332     RefPtr<AXIsolatedObject> rootNode();
333     RefPtr<AXIsolatedObject> focusedNode();
334     RefPtr<AXIsolatedObject> nodeForID(AXID) const;
335     Vector<RefPtr<AXCoreObject>> objectsForIDs(Vector<AXID>) const;
336
337     struct NodeChange {
338         Ref<AXIsolatedObject> m_isolatedObject;
339         RetainPtr<AccessibilityObjectWrapper> m_wrapper;
340         NodeChange(AXIsolatedObject&, AccessibilityObjectWrapper*);
341         NodeChange(const NodeChange&);
342     };
343
344     void generateSubtree(AXCoreObject&, AXCoreObject*, bool attachWrapper);
345     void updateNode(AXCoreObject&);
346     void updateNodeCheckedState(const AXCoreObject&);
347     void updateSubtree(AXCoreObject&);
348     void updateChildren(AXCoreObject&);
349
350     // Removes the given node leaving all descendants alone.
351     void removeNode(AXID);
352     // Removes the given node and all its descendants.
353     void removeSubtree(AXID);
354
355     // Both setRootNodeID and setFocusedNodeID are called during the generation
356     // of the IsolatedTree.
357     // Focused node updates in AXObjectCache use setFocusNodeID.
358     void setRootNode(AXIsolatedObject*);
359     void setFocusedNodeID(AXID);
360
361     // Called on AX thread from WebAccessibilityObjectWrapper methods.
362     // During layout tests, it is called on the main thread.
363     void applyPendingChanges();
364
365     AXIsolatedTreeID treeID() const { return m_treeID; }
366
367 private:
368     AXIsolatedTree();
369     void clear();
370
371     static HashMap<AXIsolatedTreeID, Ref<AXIsolatedTree>>& treeIDCache();
372     static HashMap<PageIdentifier, Ref<AXIsolatedTree>>& treePageCache();
373
374     // Call on main thread
375     Ref<AXIsolatedObject> createSubtree(AXCoreObject&, AXID parentID, bool attachWrapper, Vector<NodeChange>&);
376     // Queues all pending additions to the tree as the result of a subtree generation.
377     void appendNodeChanges(Vector<NodeChange>&&);
378     // Called on main thread to update both m_nodeMap and m_pendingChildrenUpdates.
379     void updateChildrenIDs(AXID parentID, Vector<AXID>&& childrenIDs);
380
381     AXIsolatedTreeID m_treeID;
382     AXObjectCache* m_axObjectCache { nullptr };
383
384     // Only accessed on main thread.
385     HashMap<AXID, Vector<AXID>> m_nodeMap;
386     // Only accessed on AX thread requesting data.
387     HashMap<AXID, Ref<AXIsolatedObject>> m_readerThreadNodeMap;
388
389     // Written to by main thread under lock, accessed and applied by AX thread.
390     RefPtr<AXIsolatedObject> m_rootNode;
391     Vector<NodeChange> m_pendingAppends; // Nodes to be added to the tree and platform-wrapped.
392     Vector<AXPropertyChange> m_pendingPropertyChanges;
393     Vector<AXID> m_pendingNodeRemovals; // Nodes to be removed from the tree.
394     Vector<AXID> m_pendingSubtreeRemovals; // Nodes whose subtrees are to be removed from the tree.
395     Vector<std::pair<AXID, Vector<AXID>>> m_pendingChildrenUpdates;
396     AXID m_pendingFocusedNodeID { InvalidAXID };
397     AXID m_focusedNodeID { InvalidAXID };
398     Lock m_changeLogLock;
399 };
400
401 } // namespace WebCore
402
403 #endif