-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AXObjectCache.cpp
3247 lines (2675 loc) · 117 KB
/
AXObjectCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008-2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(ACCESSIBILITY)
#include "AXObjectCache.h"
#include "AXIsolatedObject.h"
#include "AXIsolatedTree.h"
#include "AccessibilityARIAGrid.h"
#include "AccessibilityARIAGridCell.h"
#include "AccessibilityARIAGridRow.h"
#include "AccessibilityAttachment.h"
#include "AccessibilityImageMapLink.h"
#include "AccessibilityLabel.h"
#include "AccessibilityList.h"
#include "AccessibilityListBox.h"
#include "AccessibilityListBoxOption.h"
#include "AccessibilityMathMLElement.h"
#include "AccessibilityMediaControls.h"
#include "AccessibilityMediaObject.h"
#include "AccessibilityMenuList.h"
#include "AccessibilityMenuListOption.h"
#include "AccessibilityMenuListPopup.h"
#include "AccessibilityProgressIndicator.h"
#include "AccessibilityRenderObject.h"
#include "AccessibilitySVGElement.h"
#include "AccessibilitySVGRoot.h"
#include "AccessibilityScrollView.h"
#include "AccessibilityScrollbar.h"
#include "AccessibilitySlider.h"
#include "AccessibilitySpinButton.h"
#include "AccessibilityTable.h"
#include "AccessibilityTableCell.h"
#include "AccessibilityTableColumn.h"
#include "AccessibilityTableHeaderContainer.h"
#include "AccessibilityTableRow.h"
#include "AccessibilityTree.h"
#include "AccessibilityTreeItem.h"
#include "Document.h"
#include "Editing.h"
#include "Editor.h"
#include "ElementIterator.h"
#include "FocusController.h"
#include "Frame.h"
#include "HTMLAreaElement.h"
#include "HTMLCanvasElement.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLabelElement.h"
#include "HTMLMeterElement.h"
#include "HTMLNames.h"
#include "HTMLParserIdioms.h"
#include "HTMLTextFormControlElement.h"
#include "InlineElementBox.h"
#include "MathMLElement.h"
#include "Page.h"
#include "RenderAttachment.h"
#include "RenderLineBreak.h"
#include "RenderListBox.h"
#include "RenderMathMLOperator.h"
#include "RenderMenuList.h"
#include "RenderMeter.h"
#include "RenderProgress.h"
#include "RenderSVGRoot.h"
#include "RenderSlider.h"
#include "RenderTable.h"
#include "RenderTableCell.h"
#include "RenderTableRow.h"
#include "RenderView.h"
#include "RuntimeEnabledFeatures.h"
#include "SVGElement.h"
#include "ScriptDisallowedScope.h"
#include "ScrollView.h"
#include "TextBoundaries.h"
#include "TextControlInnerElements.h"
#include "TextIterator.h"
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE) && PLATFORM(MAC)
#include <pal/spi/mac/HIServicesSPI.h>
#endif
#include <wtf/DataLog.h>
#include <wtf/SetForScope.h>
#if ENABLE(VIDEO)
#include "MediaControlElements.h"
#endif
#if COMPILER(MSVC)
// See https://msdn.microsoft.com/en-us/library/1wea5zwe.aspx
#pragma warning(disable: 4701)
#endif
namespace WebCore {
using namespace HTMLNames;
const AXID InvalidAXID = 0;
// Post value change notifications for password fields or elements contained in password fields at a 40hz interval to thwart analysis of typing cadence
static const Seconds accessibilityPasswordValueChangeNotificationInterval { 25_ms };
static const Seconds accessibilityLiveRegionChangedNotificationInterval { 20_ms };
static const Seconds accessibilityFocusModalNodeNotificationInterval { 50_ms };
static bool rendererNeedsDeferredUpdate(const RenderObject& renderer)
{
ASSERT(!renderer.beingDestroyed());
auto& document = renderer.document();
return renderer.needsLayout() || document.needsStyleRecalc() || document.inRenderTreeUpdate() || (document.view() && document.view()->layoutContext().isInRenderTreeLayout());
}
static bool nodeAndRendererAreValid(Node* node)
{
if (!node)
return false;
auto* renderer = node->renderer();
return renderer && !renderer->beingDestroyed();
}
AccessibilityObjectInclusion AXComputedObjectAttributeCache::getIgnored(AXID id) const
{
auto it = m_idMapping.find(id);
return it != m_idMapping.end() ? it->value.ignored : AccessibilityObjectInclusion::DefaultBehavior;
}
void AXComputedObjectAttributeCache::setIgnored(AXID id, AccessibilityObjectInclusion inclusion)
{
HashMap<AXID, CachedAXObjectAttributes>::iterator it = m_idMapping.find(id);
if (it != m_idMapping.end())
it->value.ignored = inclusion;
else {
CachedAXObjectAttributes attributes;
attributes.ignored = inclusion;
m_idMapping.set(id, attributes);
}
}
AccessibilityReplacedText::AccessibilityReplacedText(const VisibleSelection& selection)
{
if (AXObjectCache::accessibilityEnabled()) {
m_replacedRange.startIndex.value = indexForVisiblePosition(selection.start(), m_replacedRange.startIndex.scope);
if (selection.isRange()) {
m_replacedText = AccessibilityObject::stringForVisiblePositionRange(selection);
m_replacedRange.endIndex.value = indexForVisiblePosition(selection.end(), m_replacedRange.endIndex.scope);
} else
m_replacedRange.endIndex = m_replacedRange.startIndex;
}
}
void AccessibilityReplacedText::postTextStateChangeNotification(AXObjectCache* cache, AXTextEditType type, const String& text, const VisibleSelection& selection)
{
if (!cache)
return;
if (!AXObjectCache::accessibilityEnabled())
return;
VisiblePosition position = selection.start();
auto* node = highestEditableRoot(position.deepEquivalent(), HasEditableAXRole);
if (m_replacedText.length())
cache->postTextReplacementNotification(node, AXTextEditTypeDelete, m_replacedText, type, text, position);
else
cache->postTextStateChangeNotification(node, type, text, position);
}
bool AXObjectCache::gAccessibilityEnabled = false;
bool AXObjectCache::gAccessibilityEnhancedUserInterfaceEnabled = false;
void AXObjectCache::enableAccessibility()
{
gAccessibilityEnabled = true;
}
void AXObjectCache::disableAccessibility()
{
gAccessibilityEnabled = false;
}
void AXObjectCache::setEnhancedUserInterfaceAccessibility(bool flag)
{
gAccessibilityEnhancedUserInterfaceEnabled = flag;
#if PLATFORM(MAC)
if (flag)
enableAccessibility();
#endif
}
AXObjectCache::AXObjectCache(Document& document)
: m_document(document)
, m_pageID(document.pageID())
, m_notificationPostTimer(*this, &AXObjectCache::notificationPostTimerFired)
, m_passwordNotificationPostTimer(*this, &AXObjectCache::passwordNotificationPostTimerFired)
, m_liveRegionChangedPostTimer(*this, &AXObjectCache::liveRegionChangedNotificationPostTimerFired)
, m_focusModalNodeTimer(*this, &AXObjectCache::focusModalNodeTimerFired)
, m_currentModalNode(nullptr)
, m_performCacheUpdateTimer(*this, &AXObjectCache::performCacheUpdateTimerFired)
{
findModalNodes();
}
AXObjectCache::~AXObjectCache()
{
m_notificationPostTimer.stop();
m_liveRegionChangedPostTimer.stop();
m_focusModalNodeTimer.stop();
m_performCacheUpdateTimer.stop();
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
// Destroy the IsolatedTree before destroying the live tree.
if (m_pageID)
AXIsolatedTree::removeTreeForPageID(*m_pageID);
#endif
for (const auto& object : m_objects.values())
object->detach(AccessibilityDetachmentType::CacheDestroyed);
}
void AXObjectCache::findModalNodes()
{
// Traverse the DOM tree to look for the aria-modal=true nodes.
for (Element* element = ElementTraversal::firstWithin(document().rootNode()); element; element = ElementTraversal::nextIncludingPseudo(*element)) {
// Must have dialog or alertdialog role
if (!nodeHasRole(element, "dialog") && !nodeHasRole(element, "alertdialog"))
continue;
if (!equalLettersIgnoringASCIICase(element->attributeWithoutSynchronization(aria_modalAttr), "true"))
continue;
m_modalNodesSet.add(element);
}
// Set the current valid aria-modal node if possible.
updateCurrentModalNode();
}
void AXObjectCache::updateCurrentModalNode()
{
// There might be multiple nodes with aria-modal=true set.
// We use this function to pick the one we want.
m_currentModalNode = nullptr;
if (m_modalNodesSet.isEmpty())
return;
// We only care about the nodes which are visible.
ListHashSet<RefPtr<Node>> visibleNodes;
for (auto& object : m_modalNodesSet) {
if (isNodeVisible(object))
visibleNodes.add(object);
}
if (visibleNodes.isEmpty())
return;
// If any of the node are keyboard focused, we want to pick that.
Node* focusedNode = document().focusedElement();
for (auto& object : visibleNodes) {
if (focusedNode != nullptr && focusedNode->isDescendantOf(object.get())) {
m_currentModalNode = object.get();
break;
}
}
// If none of the nodes are focused, we want to pick the last dialog in the DOM.
if (!m_currentModalNode)
m_currentModalNode = visibleNodes.last().get();
}
bool AXObjectCache::isNodeVisible(Node* node) const
{
if (!is<Element>(node))
return false;
RenderObject* renderer = node->renderer();
if (!renderer)
return false;
const RenderStyle& style = renderer->style();
if (style.display() == DisplayType::None || style.visibility() != Visibility::Visible)
return false;
// We also need to consider aria hidden status.
if (!isNodeAriaVisible(node))
return false;
return true;
}
Node* AXObjectCache::modalNode()
{
// This function returns the valid aria modal node.
if (m_modalNodesSet.isEmpty())
return nullptr;
// Check the current valid aria modal node first.
// Usually when one dialog sets aria-modal=true, that dialog is the one we want.
if (isNodeVisible(m_currentModalNode))
return m_currentModalNode;
// Recompute the valid aria modal node when m_currentModalNode is null or hidden.
updateCurrentModalNode();
return isNodeVisible(m_currentModalNode) ? m_currentModalNode : nullptr;
}
AccessibilityObject* AXObjectCache::focusedImageMapUIElement(HTMLAreaElement* areaElement)
{
// Find the corresponding accessibility object for the HTMLAreaElement. This should be
// in the list of children for its corresponding image.
if (!areaElement)
return nullptr;
HTMLImageElement* imageElement = areaElement->imageElement();
if (!imageElement)
return nullptr;
AccessibilityObject* axRenderImage = areaElement->document().axObjectCache()->getOrCreate(imageElement);
if (!axRenderImage)
return nullptr;
for (const auto& child : axRenderImage->children()) {
if (!is<AccessibilityImageMapLink>(*child))
continue;
if (downcast<AccessibilityImageMapLink>(*child).areaElement() == areaElement)
return downcast<AccessibilityImageMapLink>(child.get());
}
return nullptr;
}
AXCoreObject* AXObjectCache::focusedObject(Document& document)
{
Element* focusedElement = document.focusedElement();
if (is<HTMLAreaElement>(focusedElement))
return focusedImageMapUIElement(downcast<HTMLAreaElement>(focusedElement));
auto* axObjectCache = document.axObjectCache();
if (!axObjectCache)
return nullptr;
AXCoreObject* focus = axObjectCache->getOrCreate(focusedElement ? focusedElement : static_cast<Node*>(&document));
if (!focus)
return nullptr;
if (focus->shouldFocusActiveDescendant()) {
if (auto* descendant = focus->activeDescendant())
focus = descendant;
}
// the HTML element, for example, is focusable but has an AX object that is ignored
if (focus->accessibilityIsIgnored())
focus = focus->parentObjectUnignored();
return focus;
}
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
AXCoreObject* AXObjectCache::isolatedTreeFocusedObject(Document& document)
{
auto pageID = document.pageID();
if (!pageID)
return nullptr;
auto tree = AXIsolatedTree::treeForPageID(*pageID);
if (!tree) {
tree = generateIsolatedTree(*pageID, document);
// Now that we have created our tree, initialize the secondary thread,
// so future requests come in on the other thread.
_AXUIElementUseSecondaryAXThread(true);
}
if (tree)
return tree->focusedUIElement().get();
// Should not get here, couldn't create the IsolatedTree.
ASSERT_NOT_REACHED();
return nullptr;
}
void AXObjectCache::setIsolatedTreeFocusedObject(Node* focusedNode)
{
ASSERT(isMainThread());
if (!m_pageID)
return;
auto* focus = getOrCreate(focusedNode);
if (auto tree = AXIsolatedTree::treeForPageID(*m_pageID))
tree->setFocusedNodeID(focus ? focus->objectID() : InvalidAXID);
}
#endif
AXCoreObject* AXObjectCache::focusedUIElementForPage(const Page* page)
{
ASSERT(isMainThread());
if (!gAccessibilityEnabled)
return nullptr;
// get the focused node in the page
Document* focusedDocument = page->focusController().focusedOrMainFrame().document();
if (!focusedDocument)
return nullptr;
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (clientSupportsIsolatedTree())
return isolatedTreeFocusedObject(*focusedDocument);
#endif
return focusedObject(*focusedDocument);
}
AccessibilityObject* AXObjectCache::get(Widget* widget)
{
if (!widget)
return nullptr;
AXID axID = m_widgetObjectMapping.get(widget);
ASSERT(!HashTraits<AXID>::isDeletedValue(axID));
if (!axID)
return nullptr;
return m_objects.get(axID);
}
AccessibilityObject* AXObjectCache::get(RenderObject* renderer)
{
if (!renderer)
return nullptr;
AXID axID = m_renderObjectMapping.get(renderer);
ASSERT(!HashTraits<AXID>::isDeletedValue(axID));
if (!axID)
return nullptr;
return m_objects.get(axID);
}
AccessibilityObject* AXObjectCache::get(Node* node)
{
if (!node)
return nullptr;
AXID renderID = node->renderer() ? m_renderObjectMapping.get(node->renderer()) : 0;
ASSERT(!HashTraits<AXID>::isDeletedValue(renderID));
AXID nodeID = m_nodeObjectMapping.get(node);
ASSERT(!HashTraits<AXID>::isDeletedValue(nodeID));
if (node->renderer() && nodeID && !renderID) {
// This can happen if an AccessibilityNodeObject is created for a node that's not
// rendered, but later something changes and it gets a renderer (like if it's
// reparented).
remove(nodeID);
return nullptr;
}
if (renderID)
return m_objects.get(renderID);
if (!nodeID)
return nullptr;
return m_objects.get(nodeID);
}
// FIXME: This probably belongs on Node.
// FIXME: This should take a const char*, but one caller passes nullAtom().
bool nodeHasRole(Node* node, const String& role)
{
if (!node || !is<Element>(node))
return false;
auto& roleValue = downcast<Element>(*node).attributeWithoutSynchronization(roleAttr);
if (role.isNull())
return roleValue.isEmpty();
if (roleValue.isEmpty())
return false;
return SpaceSplitString(roleValue, true).contains(role);
}
static Ref<AccessibilityObject> createFromRenderer(RenderObject* renderer)
{
// FIXME: How could renderer->node() ever not be an Element?
Node* node = renderer->node();
// If the node is aria role="list" or the aria role is empty and its a
// ul/ol/dl type (it shouldn't be a list if aria says otherwise).
if (node && ((nodeHasRole(node, "list") || nodeHasRole(node, "directory"))
|| (nodeHasRole(node, nullAtom()) && (node->hasTagName(ulTag) || node->hasTagName(olTag) || node->hasTagName(dlTag)))))
return AccessibilityList::create(renderer);
// aria tables
if (nodeHasRole(node, "grid") || nodeHasRole(node, "treegrid") || nodeHasRole(node, "table"))
return AccessibilityARIAGrid::create(renderer);
if (nodeHasRole(node, "row"))
return AccessibilityARIAGridRow::create(renderer);
if (nodeHasRole(node, "gridcell") || nodeHasRole(node, "cell") || nodeHasRole(node, "columnheader") || nodeHasRole(node, "rowheader"))
return AccessibilityARIAGridCell::create(renderer);
// aria tree
if (nodeHasRole(node, "tree"))
return AccessibilityTree::create(renderer);
if (nodeHasRole(node, "treeitem"))
return AccessibilityTreeItem::create(renderer);
if (node && is<HTMLLabelElement>(node) && nodeHasRole(node, nullAtom()))
return AccessibilityLabel::create(renderer);
#if PLATFORM(IOS_FAMILY)
if (is<HTMLMediaElement>(node) && nodeHasRole(node, nullAtom()))
return AccessibilityMediaObject::create(renderer);
#endif
#if ENABLE(VIDEO)
// media controls
if (node && node->isMediaControlElement())
return AccessibilityMediaControl::create(renderer);
#endif
if (is<RenderSVGRoot>(*renderer))
return AccessibilitySVGRoot::create(renderer);
if (is<SVGElement>(node))
return AccessibilitySVGElement::create(renderer);
#if ENABLE(MATHML)
// The mfenced element creates anonymous RenderMathMLOperators which should be treated
// as MathML elements and assigned the MathElementRole so that platform logic regarding
// inclusion and role mapping is not bypassed.
bool isAnonymousOperator = renderer->isAnonymous() && is<RenderMathMLOperator>(*renderer);
if (isAnonymousOperator || is<MathMLElement>(node))
return AccessibilityMathMLElement::create(renderer, isAnonymousOperator);
#endif
if (is<RenderBoxModelObject>(*renderer)) {
RenderBoxModelObject& cssBox = downcast<RenderBoxModelObject>(*renderer);
if (is<RenderListBox>(cssBox))
return AccessibilityListBox::create(&downcast<RenderListBox>(cssBox));
if (is<RenderMenuList>(cssBox))
return AccessibilityMenuList::create(&downcast<RenderMenuList>(cssBox));
// standard tables
if (is<RenderTable>(cssBox))
return AccessibilityTable::create(&downcast<RenderTable>(cssBox));
if (is<RenderTableRow>(cssBox))
return AccessibilityTableRow::create(&downcast<RenderTableRow>(cssBox));
if (is<RenderTableCell>(cssBox))
return AccessibilityTableCell::create(&downcast<RenderTableCell>(cssBox));
// progress bar
if (is<RenderProgress>(cssBox))
return AccessibilityProgressIndicator::create(&downcast<RenderProgress>(cssBox));
#if ENABLE(ATTACHMENT_ELEMENT)
if (is<RenderAttachment>(cssBox))
return AccessibilityAttachment::create(&downcast<RenderAttachment>(cssBox));
#endif
#if ENABLE(METER_ELEMENT)
if (is<RenderMeter>(cssBox))
return AccessibilityProgressIndicator::create(&downcast<RenderMeter>(cssBox));
#endif
// input type=range
if (is<RenderSlider>(cssBox))
return AccessibilitySlider::create(&downcast<RenderSlider>(cssBox));
}
return AccessibilityRenderObject::create(renderer);
}
static Ref<AccessibilityObject> createFromNode(Node* node)
{
return AccessibilityNodeObject::create(node);
}
void AXObjectCache::cacheAndInitializeWrapper(AccessibilityObject* newObject, DOMObjectVariant domObject)
{
ASSERT(newObject);
AXID axID = getAXID(newObject);
WTF::switchOn(domObject,
[&axID, this] (RenderObject* typedValue) { m_renderObjectMapping.set(typedValue, axID); },
[&axID, this] (Node* typedValue) { m_nodeObjectMapping.set(typedValue, axID); },
[&axID, this] (Widget* typedValue) { m_widgetObjectMapping.set(typedValue, axID); },
[] (auto&) { }
);
m_objects.set(axID, newObject);
newObject->init();
attachWrapper(newObject);
}
AccessibilityObject* AXObjectCache::getOrCreate(Widget* widget)
{
if (!widget)
return nullptr;
if (AccessibilityObject* obj = get(widget))
return obj;
RefPtr<AccessibilityObject> newObj;
if (is<ScrollView>(*widget))
newObj = AccessibilityScrollView::create(downcast<ScrollView>(widget));
else if (is<Scrollbar>(*widget))
newObj = AccessibilityScrollbar::create(downcast<Scrollbar>(widget));
// Will crash later if we have two objects for the same widget.
ASSERT(!get(widget));
// Catch the case if an (unsupported) widget type is used. Only FrameView and ScrollBar are supported now.
ASSERT(newObj);
if (!newObj)
return nullptr;
cacheAndInitializeWrapper(newObj.get(), widget);
return newObj.get();
}
AccessibilityObject* AXObjectCache::getOrCreate(Node* node)
{
if (!node)
return nullptr;
if (AccessibilityObject* obj = get(node))
return obj;
if (node->renderer())
return getOrCreate(node->renderer());
if (!node->parentElement())
return nullptr;
// It's only allowed to create an AccessibilityObject from a Node if it's in a canvas subtree.
// Or if it's a hidden element, but we still want to expose it because of other ARIA attributes.
bool inCanvasSubtree = lineageOfType<HTMLCanvasElement>(*node->parentElement()).first();
bool isHidden = isNodeAriaVisible(node);
bool insideMeterElement = false;
#if ENABLE(METER_ELEMENT)
insideMeterElement = is<HTMLMeterElement>(*node->parentElement());
#endif
if (!inCanvasSubtree && !isHidden && !insideMeterElement)
return nullptr;
auto protectedNode = makeRef(*node);
// Fallback content is only focusable as long as the canvas is displayed and visible.
// Update the style before Element::isFocusable() gets called.
if (inCanvasSubtree)
node->document().updateStyleIfNeeded();
RefPtr<AccessibilityObject> newObj = createFromNode(node);
// Will crash later if we have two objects for the same node.
ASSERT(!get(node));
cacheAndInitializeWrapper(newObj.get(), node);
newObj->setLastKnownIsIgnoredValue(newObj->accessibilityIsIgnored());
// Sometimes asking accessibilityIsIgnored() will cause the newObject to be deallocated, and then
// it will disappear when this function is finished, leading to a use-after-free.
if (newObj->isDetached())
return nullptr;
return newObj.get();
}
AccessibilityObject* AXObjectCache::getOrCreate(RenderObject* renderer)
{
if (!renderer)
return nullptr;
if (AccessibilityObject* obj = get(renderer))
return obj;
RefPtr<AccessibilityObject> newObj = createFromRenderer(renderer);
// Will crash later if we have two objects for the same renderer.
ASSERT(!get(renderer));
cacheAndInitializeWrapper(newObj.get(), renderer);
newObj->setLastKnownIsIgnoredValue(newObj->accessibilityIsIgnored());
// Sometimes asking accessibilityIsIgnored() will cause the newObject to be deallocated, and then
// it will disappear when this function is finished, leading to a use-after-free.
if (newObj->isDetached())
return nullptr;
return newObj.get();
}
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
bool AXObjectCache::clientSupportsIsolatedTree()
{
if (!RuntimeEnabledFeatures::sharedFeatures().isAccessibilityIsolatedTreeEnabled())
return false;
AXClientType type = _AXGetClientForCurrentRequestUntrusted();
// FIXME: Remove unknown client before setting isAccessibilityIsolatedTreeEnabled initial value = true.
return type == kAXClientTypeVoiceOver
|| type == kAXClientTypeUnknown;
}
#endif
AXCoreObject* AXObjectCache::rootObject()
{
if (!gAccessibilityEnabled)
return nullptr;
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (clientSupportsIsolatedTree())
return isolatedTreeRootObject();
#endif
return getOrCreate(m_document.view());
}
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
AXCoreObject* AXObjectCache::isolatedTreeRootObject()
{
if (!m_pageID)
return nullptr;
auto tree = AXIsolatedTree::treeForPageID(*m_pageID);
if (!tree) {
tree = Accessibility::retrieveValueFromMainThread<RefPtr<AXIsolatedTree>>([this] () -> RefPtr<AXIsolatedTree> {
return generateIsolatedTree(*m_pageID, m_document);
});
// Now that we have created our tree, initialize the secondary thread,
// so future requests come in on the other thread.
_AXUIElementUseSecondaryAXThread(true);
}
if (tree)
return tree->rootNode().get();
// Should not get here, couldn't create the IsolatedTree.
ASSERT_NOT_REACHED();
return nullptr;
}
#endif
bool AXObjectCache::canUseSecondaryAXThread()
{
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE) && PLATFORM(MAC)
if (_AXUIElementRequestServicedBySecondaryAXThread())
return true;
// _AXUIElementRequestServicedBySecondaryAXThread returns false for
// LayoutTests, but we still want to run LayoutTests using isolated tree on
// a secondary thread to simulate the actual execution.
return clientSupportsIsolatedTree();
#else
return false;
#endif
}
AccessibilityObject* AXObjectCache::rootObjectForFrame(Frame* frame)
{
if (!gAccessibilityEnabled)
return nullptr;
if (!frame)
return nullptr;
return getOrCreate(frame->view());
}
AccessibilityObject* AXObjectCache::getOrCreate(AccessibilityRole role)
{
RefPtr<AccessibilityObject> obj;
// will be filled in...
switch (role) {
case AccessibilityRole::ListBoxOption:
obj = AccessibilityListBoxOption::create();
break;
case AccessibilityRole::ImageMapLink:
obj = AccessibilityImageMapLink::create();
break;
case AccessibilityRole::Column:
obj = AccessibilityTableColumn::create();
break;
case AccessibilityRole::TableHeaderContainer:
obj = AccessibilityTableHeaderContainer::create();
break;
case AccessibilityRole::SliderThumb:
obj = AccessibilitySliderThumb::create();
break;
case AccessibilityRole::MenuListPopup:
obj = AccessibilityMenuListPopup::create();
break;
case AccessibilityRole::MenuListOption:
obj = AccessibilityMenuListOption::create();
break;
case AccessibilityRole::SpinButton:
obj = AccessibilitySpinButton::create();
break;
case AccessibilityRole::SpinButtonPart:
obj = AccessibilitySpinButtonPart::create();
break;
default:
obj = nullptr;
}
if (!obj)
return nullptr;
cacheAndInitializeWrapper(obj.get());
return obj.get();
}
void AXObjectCache::remove(AXID axID)
{
if (!axID)
return;
#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
if (m_pageID) {
if (auto tree = AXIsolatedTree::treeForPageID(*m_pageID))
tree->removeNode(axID);
}
#endif
auto object = m_objects.take(axID);
if (!object)
return;
object->detach(AccessibilityDetachmentType::ElementDestroyed);
m_idsInUse.remove(axID);
ASSERT(m_objects.size() >= m_idsInUse.size());
}
void AXObjectCache::remove(RenderObject* renderer)
{
if (!renderer)
return;
remove(m_renderObjectMapping.take(renderer));
}
void AXObjectCache::remove(Node& node)
{
if (is<Element>(node)) {
m_deferredRecomputeIsIgnoredList.remove(downcast<Element>(&node));
m_deferredSelectedChildredChangedList.remove(downcast<Element>(&node));
m_deferredTextFormControlValue.remove(downcast<Element>(&node));
m_deferredAttributeChange.remove(downcast<Element>(&node));
}
m_deferredChildrenChangedNodeList.remove(&node);
m_deferredTextChangedList.remove(&node);
// Remove the entry if the new focused node is being removed.
m_deferredFocusedNodeChange.removeAllMatching([&node](auto& entry) -> bool {
return entry.second == &node;
});
removeNodeForUse(node);
remove(m_nodeObjectMapping.take(&node));
if (m_currentModalNode == &node)
m_currentModalNode = nullptr;
m_modalNodesSet.remove(&node);
remove(node.renderer());
}
void AXObjectCache::remove(Widget* view)
{
if (!view)
return;
remove(m_widgetObjectMapping.take(view));
}
#if !PLATFORM(WIN)
AXID AXObjectCache::platformGenerateAXID() const
{
static AXID lastUsedID = 0;
// Generate a new ID.
AXID objID = lastUsedID;
do {
++objID;
} while (!objID || HashTraits<AXID>::isDeletedValue(objID) || m_idsInUse.contains(objID));
lastUsedID = objID;
return objID;
}
#endif
AXID AXObjectCache::getAXID(AccessibilityObject* obj)
{
// check for already-assigned ID
AXID objID = obj->objectID();
if (objID) {
ASSERT(m_idsInUse.contains(objID));
return objID;
}
objID = platformGenerateAXID();
m_idsInUse.add(objID);
obj->setObjectID(objID);
return objID;
}
void AXObjectCache::textChanged(Node* node)
{
textChanged(getOrCreate(node));
}
void AXObjectCache::textChanged(AccessibilityObject* obj)
{
if (!obj)
return;
bool parentAlreadyExists = obj->parentObjectIfExists();
obj->textChanged();
postNotification(obj, obj->document(), AXObjectCache::AXTextChanged);
if (parentAlreadyExists)
obj->notifyIfIgnoredValueChanged();
}
void AXObjectCache::updateCacheAfterNodeIsAttached(Node* node)
{
// Calling get() will update the AX object if we had an AccessibilityNodeObject but now we need
// an AccessibilityRenderObject, because it was reparented to a location outside of a canvas.
get(node);
}
void AXObjectCache::handleMenuOpened(Node* node)
{
if (!node || !node->renderer() || !nodeHasRole(node, "menu"))
return;
postNotification(getOrCreate(node), &document(), AXMenuOpened);
}
void AXObjectCache::handleLiveRegionCreated(Node* node)
{
if (!is<Element>(node) || !node->renderer())
return;
Element* element = downcast<Element>(node);
String liveRegionStatus = element->attributeWithoutSynchronization(aria_liveAttr);
if (liveRegionStatus.isEmpty()) {
const AtomString& ariaRole = element->attributeWithoutSynchronization(roleAttr);
if (!ariaRole.isEmpty())
liveRegionStatus = AccessibilityObject::defaultLiveRegionStatusForRole(AccessibilityObject::ariaRoleToWebCoreRole(ariaRole));
}
if (AccessibilityObject::liveRegionStatusIsEnabled(liveRegionStatus))
postNotification(getOrCreate(node), &document(), AXLiveRegionCreated);
}
void AXObjectCache::childrenChanged(Node* node, Node* newChild)
{
if (newChild)
m_deferredChildrenChangedNodeList.add(newChild);
childrenChanged(get(node));
}
void AXObjectCache::childrenChanged(RenderObject* renderer, RenderObject* newChild)
{
if (!renderer)
return;
if (newChild && newChild->node())
m_deferredChildrenChangedNodeList.add(newChild->node());
childrenChanged(get(renderer));
}