Implementation for AXIsolatedObject::nextSibling and previousSibling.
[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     NextSibling,
251     Orientation,
252     Path,
253     PlaceholderValue,
254     PressedIsPresent,
255     PreviousSibling,
256     PopupValue,
257     PosInSet,
258     PreventKeyboardDOMEventDispatch,
259     ReadOnlyValue,
260     RoleValue,
261     RolePlatformString,
262     RoleDescription,
263     Rows,
264     RowCount,
265     RowHeaders,
266     RowIndex,
267     RowIndexRange,
268     SelectedChildren,
269     SelectedRadioButton,
270     SelectedTabItem,
271     SessionID,
272     SetSize,
273     SortDirection,
274     SpeakAs,
275     SpeechHint,
276     StringValue,
277     SupportsRowCountChange,
278     SupportsDragging,
279     SupportsDropping,
280     SupportsARIAOwns,
281     SupportsCurrent,
282     SupportsDatetimeAttribute,
283     SupportsExpanded,
284     SupportsExpandedTextValue,
285     SupportsLiveRegion,
286     SupportsPath,
287     SupportsPosInSet,
288     SupportsPressAction,
289     SupportsRangeValue,
290     SupportsRequiredAttribute,
291     SupportsSelectedRows,
292     SupportsSetSize,
293     TabChildren,
294     TableLevel,
295     TagName,
296     TextLength,
297     Title,
298     TitleAttributeValue,
299     TitleUIElement,
300     URL,
301     ValueAutofillButtonType,
302     ValueDescription,
303     ValueForRange,
304     ValidationMessage,
305     VerticalScrollBar,
306     VisibleChildren,
307     VisibleRows,
308     WebArea,
309 };
310
311 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>;
312 using AXPropertyMap = HashMap<AXPropertyName, AXPropertyValueVariant, WTF::IntHash<AXPropertyName>, WTF::StrongEnumHashTraits<AXPropertyName>>;
313
314 struct AXPropertyChange {
315     AXID axID { InvalidAXID }; // ID of the object whose properties changed.
316     AXPropertyMap properties; // Changed properties.
317 };
318
319 class AXIsolatedTree : public ThreadSafeRefCounted<AXIsolatedTree> {
320     WTF_MAKE_NONCOPYABLE(AXIsolatedTree); WTF_MAKE_FAST_ALLOCATED;
321     friend WTF::TextStream& operator<<(WTF::TextStream&, AXIsolatedTree&);
322 public:
323     static Ref<AXIsolatedTree> create();
324     virtual ~AXIsolatedTree();
325
326     static Ref<AXIsolatedTree> createTreeForPageID(PageIdentifier);
327     static void removeTreeForPageID(PageIdentifier);
328
329     static RefPtr<AXIsolatedTree> treeForPageID(PageIdentifier);
330     static RefPtr<AXIsolatedTree> treeForID(AXIsolatedTreeID);
331     AXObjectCache* axObjectCache() const { return m_axObjectCache; }
332     void setAXObjectCache(AXObjectCache* axObjectCache) { m_axObjectCache = axObjectCache; }
333
334     RefPtr<AXIsolatedObject> rootNode();
335     RefPtr<AXIsolatedObject> focusedNode();
336     RefPtr<AXIsolatedObject> nodeForID(AXID) const;
337     Vector<RefPtr<AXCoreObject>> objectsForIDs(Vector<AXID>) const;
338
339     struct NodeChange {
340         Ref<AXIsolatedObject> m_isolatedObject;
341         RetainPtr<AccessibilityObjectWrapper> m_wrapper;
342         NodeChange(AXIsolatedObject&, AccessibilityObjectWrapper*);
343         NodeChange(const NodeChange&);
344     };
345
346     void generateSubtree(AXCoreObject&, AXCoreObject*, bool attachWrapper);
347     void updateNode(AXCoreObject&);
348     void updateNodeCheckedState(const AXCoreObject&);
349     void updateSubtree(AXCoreObject&);
350     void updateChildren(AXCoreObject&);
351
352     // Removes the given node leaving all descendants alone.
353     void removeNode(AXID);
354     // Removes the given node and all its descendants.
355     void removeSubtree(AXID);
356
357     // Both setRootNodeID and setFocusedNodeID are called during the generation
358     // of the IsolatedTree.
359     // Focused node updates in AXObjectCache use setFocusNodeID.
360     void setRootNode(AXIsolatedObject*);
361     void setFocusedNodeID(AXID);
362
363     // Called on AX thread from WebAccessibilityObjectWrapper methods.
364     // During layout tests, it is called on the main thread.
365     void applyPendingChanges();
366
367     AXIsolatedTreeID treeID() const { return m_treeID; }
368
369 private:
370     AXIsolatedTree();
371     void clear();
372
373     static HashMap<AXIsolatedTreeID, Ref<AXIsolatedTree>>& treeIDCache();
374     static HashMap<PageIdentifier, Ref<AXIsolatedTree>>& treePageCache();
375
376     // Call on main thread
377     Ref<AXIsolatedObject> createSubtree(AXCoreObject&, AXID parentID, bool attachWrapper, Vector<NodeChange>&);
378     // Queues all pending additions to the tree as the result of a subtree generation.
379     void appendNodeChanges(Vector<NodeChange>&&);
380     // Called on main thread to update both m_nodeMap and m_pendingChildrenUpdates.
381     void updateChildrenIDs(AXID parentID, Vector<AXID>&& childrenIDs);
382
383     AXIsolatedTreeID m_treeID;
384     AXObjectCache* m_axObjectCache { nullptr };
385
386     // Only accessed on main thread.
387     HashMap<AXID, Vector<AXID>> m_nodeMap;
388     // Only accessed on AX thread requesting data.
389     HashMap<AXID, Ref<AXIsolatedObject>> m_readerThreadNodeMap;
390
391     // Written to by main thread under lock, accessed and applied by AX thread.
392     RefPtr<AXIsolatedObject> m_rootNode;
393     Vector<NodeChange> m_pendingAppends; // Nodes to be added to the tree and platform-wrapped.
394     Vector<AXPropertyChange> m_pendingPropertyChanges;
395     Vector<AXID> m_pendingNodeRemovals; // Nodes to be removed from the tree.
396     Vector<AXID> m_pendingSubtreeRemovals; // Nodes whose subtrees are to be removed from the tree.
397     Vector<std::pair<AXID, Vector<AXID>>> m_pendingChildrenUpdates;
398     AXID m_pendingFocusedNodeID { InvalidAXID };
399     AXID m_focusedNodeID { InvalidAXID };
400     Lock m_changeLogLock;
401 };
402
403 } // namespace WebCore
404
405 #endif