/
browser_accessibility.cc
2499 lines (2167 loc) · 91.9 KB
/
browser_accessibility.cc
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) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/accessibility/browser_accessibility.h"
#include <cstddef>
#include <algorithm>
#include <iterator>
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/accessibility/browser_accessibility_manager.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/common/ax_serialization_utils.h"
#include "content/public/common/content_client.h"
#include "third_party/blink/public/strings/grit/blink_accessibility_strings.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/accessibility/platform/ax_unique_id.h"
#include "ui/base/buildflags.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/strings/grit/ax_strings.h"
namespace content {
#if !BUILDFLAG(HAS_PLATFORM_ACCESSIBILITY_SUPPORT)
// static
std::unique_ptr<BrowserAccessibility> BrowserAccessibility::Create(
BrowserAccessibilityManager* manager,
ui::AXNode* node) {
return std::unique_ptr<BrowserAccessibility>(
new BrowserAccessibility(manager, node));
}
#endif // !BUILDFLAG(HAS_PLATFORM_ACCESSIBILITY_SUPPORT)
// static
BrowserAccessibility* BrowserAccessibility::FromAXPlatformNodeDelegate(
ui::AXPlatformNodeDelegate* delegate) {
if (!delegate || !delegate->IsWebContent())
return nullptr;
return static_cast<BrowserAccessibility*>(delegate);
}
BrowserAccessibility::BrowserAccessibility(BrowserAccessibilityManager* manager,
ui::AXNode* node)
: AXPlatformNodeDelegate(node), manager_(manager) {
DCHECK(manager);
}
BrowserAccessibility::~BrowserAccessibility() = default;
namespace {
// Get the native text field's deepest container; the lowest descendant that
// contains all its text. Returns nullptr if the text field is empty, or if it
// is not a native text field (input or textarea).
BrowserAccessibility* GetTextFieldInnerEditorElement(
const BrowserAccessibility& text_field) {
ui::AXNode* text_container =
text_field.node()->GetTextFieldInnerEditorElement();
return text_field.manager()->GetFromAXNode(text_container);
}
} // namespace
bool BrowserAccessibility::IsValid() const {
// Currently we only perform validity checks on non-empty, atomic text fields.
// An atomic text field does not expose its internal implementation to
// assistive software, appearing as a single leaf node in the accessibility
// tree. It includes <input>, <textarea> and Views-based text fields.
if (!IsAtomicTextField())
return true;
// If the input type is not plain or text it may be a complex field, such as
// a datetime input. We don't try to enforce a special structure for those.
const std::string& input_type =
GetStringAttribute(ax::mojom::StringAttribute::kInputType);
DCHECK(IsIgnored() ||
GetStringAttribute(ax::mojom::StringAttribute::kHtmlTag) != "input" ||
!input_type.empty())
<< "By design, all non-hidden <input> elements in the accessibility "
"tree, should have an input type: "
<< *this;
if (input_type != "text" && input_type != "search")
return true; // Not a plain text field, just consider it valid.
if (InternalChildCount()) {
// If the atomic text field is aria-hidden then all its descendants are
// ignored.
// See the dump tree test AccessibilityAriaHiddenFocusedInput.
//
// TODO(accessibility): We need to fix this by pruning the tree and removing
// the native text field if it is aria-hidden.
return IsInvisibleOrIgnored() || GetTextFieldInnerEditorElement(*this);
}
return true;
}
void BrowserAccessibility::OnDataChanged() {
DCHECK(IsValid()) << "Invalid node: " << *this;
}
bool BrowserAccessibility::CanFireEvents() const {
return node()->CanFireEvents();
}
ui::AXPlatformNode* BrowserAccessibility::GetAXPlatformNode() const {
// Not all BrowserAccessibility subclasses can return an AXPlatformNode yet.
// So, here we just return nullptr.
return nullptr;
}
size_t BrowserAccessibility::PlatformChildCount() const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
return IsLeaf() ? 0 : node()->GetUnignoredChildCountCrossingTreeBoundary();
}
BrowserAccessibility* BrowserAccessibility::PlatformGetParent() const {
ui::AXNode* parent = node()->GetUnignoredParentCrossingTreeBoundary();
return manager()->GetFromAXNode(parent);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetFirstChild() const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
if (IsLeaf())
return nullptr;
ui::AXNode* first_child =
node()->GetFirstUnignoredChildCrossingTreeBoundary();
return manager()->GetFromAXNode(first_child);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetLastChild() const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
if (IsLeaf())
return nullptr;
ui::AXNode* last_child = node()->GetLastUnignoredChildCrossingTreeBoundary();
return manager()->GetFromAXNode(last_child);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetNextSibling() const {
return InternalGetNextSibling();
}
BrowserAccessibility* BrowserAccessibility::PlatformGetPreviousSibling() const {
return InternalGetPreviousSibling();
}
BrowserAccessibility::PlatformChildIterator
BrowserAccessibility::PlatformChildrenBegin() const {
return PlatformChildIterator(this, PlatformGetFirstChild());
}
BrowserAccessibility::PlatformChildIterator
BrowserAccessibility::PlatformChildrenEnd() const {
return PlatformChildIterator(this, nullptr);
}
bool BrowserAccessibility::IsDescendantOf(
const BrowserAccessibility* ancestor) const {
if (!ancestor)
return false;
DCHECK(ancestor->node());
return node()->IsDescendantOfCrossingTreeBoundary(ancestor->node());
}
bool BrowserAccessibility::IsIgnored() const {
return node()->IsIgnored();
}
bool BrowserAccessibility::IsIgnoredForTextNavigation() const {
return node()->IsIgnoredForTextNavigation();
}
bool BrowserAccessibility::IsLineBreakObject() const {
return node()->IsLineBreak();
}
BrowserAccessibility* BrowserAccessibility::PlatformGetChild(
size_t child_index) const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
if (IsLeaf())
return nullptr;
ui::AXNode* child =
node()->GetUnignoredChildAtIndexCrossingTreeBoundary(child_index);
return manager()->GetFromAXNode(child);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetLowestPlatformAncestor()
const {
ui::AXNode* lowest_platform_ancestor = node()->GetLowestPlatformAncestor();
return manager()->GetFromAXNode(lowest_platform_ancestor);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetTextFieldAncestor()
const {
ui::AXNode* text_field_ancestor = node()->GetTextFieldAncestor();
return manager()->GetFromAXNode(text_field_ancestor);
}
BrowserAccessibility* BrowserAccessibility::PlatformGetSelectionContainer()
const {
ui::AXNode* selection_container_ancestor = node()->GetSelectionContainer();
return manager()->GetFromAXNode(selection_container_ancestor);
}
BrowserAccessibility* BrowserAccessibility::PlatformDeepestFirstChild() const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
if (IsLeaf())
return nullptr;
BrowserAccessibility* deepest_child = PlatformGetFirstChild();
while (!deepest_child->IsLeaf())
deepest_child = deepest_child->PlatformGetFirstChild();
return deepest_child;
}
BrowserAccessibility* BrowserAccessibility::PlatformDeepestLastChild() const {
// We need to explicitly check for leafiness here instead of relying on
// `AXNode::IsLeaf()` because Android has a different notion of this concept.
if (IsLeaf())
return nullptr;
BrowserAccessibility* deepest_child = PlatformGetLastChild();
while (!deepest_child->IsLeaf())
deepest_child = deepest_child->PlatformGetLastChild();
return deepest_child;
}
BrowserAccessibility* BrowserAccessibility::InternalDeepestFirstChild() const {
// By design, this method should be able to traverse platform leaves, hence we
// don't check for leafiness.
ui::AXNode* deepest_child = node()->GetDeepestFirstUnignoredChild();
return manager()->GetFromAXNode(deepest_child);
}
BrowserAccessibility* BrowserAccessibility::InternalDeepestLastChild() const {
// By design, this method should be able to traverse platform leaves, hence we
// don't check for leafiness. We need to explicitly check for leafiness here
// instead of relying on `AXNode::IsLeaf()` because Android has a different
// notion of this concept.
ui::AXNode* deepest_child = node()->GetDeepestLastUnignoredChild();
return manager()->GetFromAXNode(deepest_child);
}
size_t BrowserAccessibility::InternalChildCount() const {
return node()->GetUnignoredChildCount();
}
BrowserAccessibility* BrowserAccessibility::InternalGetChild(
size_t child_index) const {
// By design, this method should be able to traverse platform leaves, hence we
// don't check for leafiness.
ui::AXNode* child_node = node()->GetUnignoredChildAtIndex(child_index);
return manager_->GetFromAXNode(child_node);
}
BrowserAccessibility* BrowserAccessibility::InternalGetParent() const {
ui::AXNode* parent_node = node()->GetUnignoredParent();
return manager_->GetFromAXNode(parent_node);
}
BrowserAccessibility* BrowserAccessibility::InternalGetFirstChild() const {
// By design, this method should be able to traverse platform leaves, hence we
// don't check for leafiness.
ui::AXNode* child_node = node()->GetFirstUnignoredChild();
return manager_->GetFromAXNode(child_node);
}
BrowserAccessibility* BrowserAccessibility::InternalGetLastChild() const {
// By design, this method should be able to traverse platform leaves, hence we
// don't check for leafiness.
ui::AXNode* child_node = node()->GetLastUnignoredChild();
return manager_->GetFromAXNode(child_node);
}
BrowserAccessibility* BrowserAccessibility::InternalGetNextSibling() const {
ui::AXNode* sibling_node = node()->GetNextUnignoredSibling();
return manager_->GetFromAXNode(sibling_node);
}
BrowserAccessibility* BrowserAccessibility::InternalGetPreviousSibling() const {
ui::AXNode* sibling_node = node()->GetPreviousUnignoredSibling();
return manager_->GetFromAXNode(sibling_node);
}
BrowserAccessibility::InternalChildIterator
BrowserAccessibility::InternalChildrenBegin() const {
return InternalChildIterator(this, InternalGetFirstChild());
}
BrowserAccessibility::InternalChildIterator
BrowserAccessibility::InternalChildrenEnd() const {
return InternalChildIterator(this, nullptr);
}
const BrowserAccessibility*
BrowserAccessibility::AllChildrenRange::Iterator::operator*() {
if (child_tree_root_)
return index_ == 0 ? child_tree_root_ : nullptr;
// TODO(nektar): Consider using
// `AXNode::GetChildAtIndexCrossingTreeBoundary()`.
ui::AXNode* child = parent_->node()->GetChildAtIndex(index_);
return parent_->manager()->GetFromAXNode(child);
}
ui::AXNodeID BrowserAccessibility::GetId() const {
return node()->id();
}
gfx::RectF BrowserAccessibility::GetLocation() const {
return GetData().relative_bounds.bounds;
}
gfx::Rect BrowserAccessibility::GetUnclippedRootFrameHypertextRangeBoundsRect(
const int start_offset,
const int end_offset,
ui::AXOffscreenResult* offscreen_result) const {
return GetHypertextRangeBoundsRect(
start_offset, end_offset, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped, offscreen_result);
}
gfx::Rect BrowserAccessibility::GetUnclippedScreenInnerTextRangeBoundsRect(
const int start_offset,
const int end_offset,
ui::AXOffscreenResult* offscreen_result) const {
return GetInnerTextRangeBoundsRect(
start_offset, end_offset, ui::AXCoordinateSystem::kScreenDIPs,
ui::AXClippingBehavior::kUnclipped, offscreen_result);
}
gfx::Rect BrowserAccessibility::GetUnclippedRootFrameInnerTextRangeBoundsRect(
const int start_offset,
const int end_offset,
ui::AXOffscreenResult* offscreen_result) const {
return GetInnerTextRangeBoundsRect(
start_offset, end_offset, ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kUnclipped, offscreen_result);
}
gfx::Rect BrowserAccessibility::GetBoundsRect(
const ui::AXCoordinateSystem coordinate_system,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
return RelativeToAbsoluteBounds(gfx::RectF(), coordinate_system,
clipping_behavior, offscreen_result);
}
gfx::Rect BrowserAccessibility::GetHypertextRangeBoundsRect(
const int start_offset,
const int end_offset,
const ui::AXCoordinateSystem coordinate_system,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
int effective_start_offset = start_offset;
int effective_end_offset = end_offset;
if (effective_start_offset == effective_end_offset)
return gfx::Rect();
if (effective_start_offset > effective_end_offset)
std::swap(effective_start_offset, effective_end_offset);
const std::u16string& text_str = GetHypertext();
if (effective_start_offset < 0 ||
effective_start_offset >= static_cast<int>(text_str.size())) {
return gfx::Rect();
}
if (effective_end_offset < 0 ||
effective_end_offset > static_cast<int>(text_str.size())) {
return gfx::Rect();
}
if (coordinate_system == ui::AXCoordinateSystem::kFrame) {
NOTIMPLEMENTED();
return gfx::Rect();
}
// Obtain bounds in root frame coordinates.
gfx::Rect bounds = GetRootFrameHypertextRangeBoundsRect(
effective_start_offset, effective_end_offset - effective_start_offset,
clipping_behavior, offscreen_result);
if (coordinate_system == ui::AXCoordinateSystem::kScreenDIPs ||
coordinate_system == ui::AXCoordinateSystem::kScreenPhysicalPixels) {
// Convert to screen coordinates.
bounds.Offset(
manager()->GetViewBoundsInScreenCoordinates().OffsetFromOrigin());
}
return bounds;
}
gfx::Rect BrowserAccessibility::GetRootFrameHypertextRangeBoundsRect(
int start,
int len,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
// TODO(nektar): Move to `AXNode` as soon as hypertext computation is fully
// migrated to that class.
DCHECK_GE(start, 0);
DCHECK_GE(len, 0);
// Atomic text fields such as textarea have a text container node inside them
// that holds all the text and do not expose any IA2 hypertext. We need to get
// to the flattened representation of the text in the field in order that
// `start` and `len` would be applicable. Non-native text fields, including
// ARIA-based ones expose their actual subtree and do use IA2 hypertext, so
// `start` and `len` would apply in those cases.
if (const BrowserAccessibility* text_container =
GetTextFieldInnerEditorElement(*this)) {
return text_container->GetRootFrameHypertextRangeBoundsRect(
start, len, clipping_behavior, offscreen_result);
}
if (GetRole() != ax::mojom::Role::kStaticText) {
gfx::Rect bounds;
for (InternalChildIterator it = InternalChildrenBegin();
it != InternalChildrenEnd() && len > 0; ++it) {
const BrowserAccessibility* child = it.get();
// Child objects are of length one, since they are represented by a single
// embedded object character. The exception is text-only objects.
int child_length_in_parent = 1;
if (child->IsText())
child_length_in_parent = static_cast<int>(child->GetHypertext().size());
if (start < child_length_in_parent) {
gfx::Rect child_rect;
if (child->IsText()) {
child_rect = child->GetRootFrameHypertextRangeBoundsRect(
start, len, clipping_behavior, offscreen_result);
} else {
child_rect = child->GetRootFrameHypertextRangeBoundsRect(
0, static_cast<int>(child->GetHypertext().size()),
clipping_behavior, offscreen_result);
}
bounds.Union(child_rect);
len -= (child_length_in_parent - start);
}
if (start > child_length_in_parent)
start -= child_length_in_parent;
else
start = 0;
}
// When past the end of text, the area will be 0.
// In this case, use bounds provided for the caret.
return bounds.IsEmpty() ? GetRootFrameHypertextBoundsPastEndOfText(
clipping_behavior, offscreen_result)
: bounds;
}
int end = start + len;
int child_start = 0;
int child_end = 0;
gfx::Rect bounds;
for (InternalChildIterator it = InternalChildrenBegin();
it != InternalChildrenEnd() && child_end < start + len; ++it) {
const BrowserAccessibility* child = it.get();
if (child->GetRole() != ax::mojom::Role::kInlineTextBox) {
DLOG(WARNING) << "BrowserAccessibility objects with role STATIC_TEXT "
<< "should have children of role INLINE_TEXT_BOX.\n";
continue;
}
int child_length = static_cast<int>(child->GetHypertext().size());
child_start = child_end;
child_end += child_length;
if (child_end < start)
continue;
int overlap_start = std::max(start, child_start);
int overlap_end = std::min(end, child_end);
int local_start = overlap_start - child_start;
int local_end = overlap_end - child_start;
// |local_end| and |local_start| may equal |child_length| when the caret is
// at the end of a text field.
DCHECK_GE(local_start, 0);
DCHECK_LE(local_start, child_length);
DCHECK_GE(local_end, 0);
DCHECK_LE(local_end, child_length);
// Don't clip bounds. Some screen magnifiers (e.g. ZoomText) prefer to
// get unclipped bounds so that they can make smooth scrolling calculations.
gfx::Rect absolute_child_rect = child->RelativeToAbsoluteBounds(
child->GetTextContentRangeBoundsUTF16(local_start, local_end),
ui::AXCoordinateSystem::kRootFrame, clipping_behavior,
offscreen_result);
if (bounds.width() == 0 && bounds.height() == 0) {
bounds = absolute_child_rect;
} else {
bounds.Union(absolute_child_rect);
}
}
return bounds;
}
gfx::Rect BrowserAccessibility::GetScreenHypertextRangeBoundsRect(
int start,
int len,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
gfx::Rect bounds = GetRootFrameHypertextRangeBoundsRect(
start, len, clipping_behavior, offscreen_result);
// Adjust the bounds by the top left corner of the containing view's bounds
// in screen coordinates.
bounds.Offset(
manager_->GetViewBoundsInScreenCoordinates().OffsetFromOrigin());
return bounds;
}
gfx::Rect BrowserAccessibility::GetRootFrameHypertextBoundsPastEndOfText(
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
// TODO(nektar): Move to `AXNode` as soon as hypertext computation is fully
// migrated to that class.
// Step 1: get approximate caret bounds. The thickness may not yet be correct.
gfx::Rect bounds;
if (InternalChildCount() > 0) {
// When past the end of text, use bounds provided by a last child if
// available, and then correct for thickness of caret.
BrowserAccessibility* child = InternalGetLastChild();
int child_text_len = child->GetHypertext().size();
bounds = child->GetRootFrameHypertextRangeBoundsRect(
child_text_len, child_text_len, clipping_behavior, offscreen_result);
if (bounds.width() == 0 && bounds.height() == 0)
return bounds; // Inline text boxes info not yet available.
} else {
// Compute bounds of where caret would be, based on bounds of object.
bounds = GetBoundsRect(ui::AXCoordinateSystem::kRootFrame,
clipping_behavior, offscreen_result);
}
// Step 2: correct for the thickness of the caret.
auto text_direction = static_cast<ax::mojom::WritingDirection>(
GetIntAttribute(ax::mojom::IntAttribute::kTextDirection));
constexpr int kCaretThickness = 1;
switch (text_direction) {
case ax::mojom::WritingDirection::kNone:
case ax::mojom::WritingDirection::kLtr: {
bounds.set_width(kCaretThickness);
break;
}
case ax::mojom::WritingDirection::kRtl: {
bounds.set_x(bounds.right() - kCaretThickness);
bounds.set_width(kCaretThickness);
break;
}
case ax::mojom::WritingDirection::kTtb: {
bounds.set_height(kCaretThickness);
break;
}
case ax::mojom::WritingDirection::kBtt: {
bounds.set_y(bounds.bottom() - kCaretThickness);
bounds.set_height(kCaretThickness);
break;
}
}
return bounds;
}
gfx::Rect BrowserAccessibility::GetInnerTextRangeBoundsRect(
const int start_offset,
const int end_offset,
const ui::AXCoordinateSystem coordinate_system,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
const int text_length = GetTextContentUTF16().length();
if (start_offset < 0 || end_offset > text_length || start_offset > end_offset)
return gfx::Rect();
return GetInnerTextRangeBoundsRectInSubtree(
start_offset, end_offset, coordinate_system, clipping_behavior,
offscreen_result);
}
gfx::Rect BrowserAccessibility::GetInnerTextRangeBoundsRectInSubtree(
const int start_offset,
const int end_offset,
const ui::AXCoordinateSystem coordinate_system,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
// TODO(nektar): Move to `AXNode` as soon as hypertext computation is fully
// migrated to that class.
if (GetRole() == ax::mojom::Role::kInlineTextBox) {
return RelativeToAbsoluteBounds(
GetTextContentRangeBoundsUTF16(start_offset, end_offset),
coordinate_system, clipping_behavior, offscreen_result);
}
gfx::Rect bounds;
int child_offset_in_parent = 0;
for (InternalChildIterator it = InternalChildrenBegin();
it != InternalChildrenEnd(); ++it) {
const BrowserAccessibility* browser_accessibility_child = it.get();
const int child_text_length =
browser_accessibility_child->GetTextContentUTF16().length();
// The text bounds queried are not in this subtree; skip it and continue.
const int child_start_offset =
std::max(start_offset - child_offset_in_parent, 0);
if (child_start_offset > child_text_length) {
child_offset_in_parent += child_text_length;
continue;
}
// The text bounds queried have already been gathered; short circuit.
const int child_end_offset =
std::min(end_offset - child_offset_in_parent, child_text_length);
if (child_end_offset < 0)
return bounds;
// Increase the text bounds by the subtree text bounds.
const gfx::Rect child_bounds =
browser_accessibility_child->GetInnerTextRangeBoundsRectInSubtree(
child_start_offset, child_end_offset, coordinate_system,
clipping_behavior, offscreen_result);
if (bounds.IsEmpty())
bounds = child_bounds;
else
bounds.Union(child_bounds);
child_offset_in_parent += child_text_length;
}
return bounds;
}
gfx::RectF BrowserAccessibility::GetTextContentRangeBoundsUTF16(
int start_offset,
int end_offset) const {
return node()->GetTextContentRangeBoundsUTF16(start_offset, end_offset);
}
BrowserAccessibility* BrowserAccessibility::ApproximateHitTest(
const gfx::Point& blink_screen_point) {
// TODO(crbug.com/1049261): This is one of the few methods that won't be moved
// to `AXNode` in the foreseeable future because the functionality it provides
// is not immediately needed in Views.
// The best result found that's a child of this object.
BrowserAccessibility* child_result = nullptr;
// The best result that's an indirect descendant like grandchild, etc.
BrowserAccessibility* descendant_result = nullptr;
// Walk the children recursively looking for the BrowserAccessibility that
// most tightly encloses the specified point. Walk backwards so that in
// the absence of any other information, we assume the object that occurs
// later in the tree is on top of one that comes before it.
for (BrowserAccessibility* child = PlatformGetLastChild(); child != nullptr;
child = child->PlatformGetPreviousSibling()) {
// Skip table columns because cells are only contained in rows,
// not columns.
if (child->GetRole() == ax::mojom::Role::kColumn)
continue;
if (child->GetClippedScreenBoundsRect().Contains(blink_screen_point)) {
BrowserAccessibility* result =
child->ApproximateHitTest(blink_screen_point);
if (result == child && !child_result)
child_result = result;
if (result != child && !descendant_result)
descendant_result = result;
}
if (child_result && descendant_result)
break;
}
// Explanation of logic: it's possible that this point overlaps more than
// one child of this object. If so, as a heuristic we prefer if the point
// overlaps a descendant of one of the two children and not the other.
// As an example, suppose you have two rows of buttons - the buttons don't
// overlap, but the rows do. Without this heuristic, we'd greedily only
// consider one of the containers.
if (descendant_result)
return descendant_result;
if (child_result)
return child_result;
return this;
}
bool BrowserAccessibility::IsRootWebAreaForPresentationalIframe() const {
return node()->IsRootWebAreaForPresentationalIframe();
}
bool BrowserAccessibility::IsClickable() const {
return GetData().IsClickable();
}
bool BrowserAccessibility::IsTextField() const {
return GetData().IsTextField();
}
bool BrowserAccessibility::IsPasswordField() const {
return GetData().IsPasswordField();
}
bool BrowserAccessibility::IsAtomicTextField() const {
return GetData().IsAtomicTextField();
}
bool BrowserAccessibility::IsNonAtomicTextField() const {
return GetData().IsNonAtomicTextField();
}
bool BrowserAccessibility::HasExplicitlyEmptyName() const {
return GetNameFrom() == ax::mojom::NameFrom::kAttributeExplicitlyEmpty;
}
BrowserAccessibility::AXPosition BrowserAccessibility::CreatePositionAt(
int offset,
ax::mojom::TextAffinity affinity) const {
return ui::AXNodePosition::CreatePosition(
*node(), /* child_index_or_text_offset */ offset);
}
// |offset| could either be a text character or a child index in case of
// non-text objects.
// Currently, to be safe, we convert to text leaf equivalents and we don't use
// tree positions.
// TODO(nektar): Remove this function once selection fixes in Blink are
// thoroughly tested and convert to tree positions.
BrowserAccessibility::AXPosition
BrowserAccessibility::CreatePositionForSelectionAt(int offset) const {
return CreateTextPositionAt(offset)->AsDomSelectionPosition();
}
const std::string& BrowserAccessibility::GetName() const {
return node()->GetNameUTF8();
}
std::u16string BrowserAccessibility::GetNameAsString16() const {
return node()->GetNameUTF16();
}
const std::string& BrowserAccessibility::GetDescription() const {
return GetStringAttribute(ax::mojom::StringAttribute::kDescription);
}
std::u16string BrowserAccessibility::GetHypertext() const {
// Overloaded by platforms which require a hypertext accessibility text
// implementation.
return std::u16string();
}
const std::map<int, int>&
BrowserAccessibility::GetHypertextOffsetToHyperlinkChildIndex() const {
return node()->GetHypertextOffsetToHyperlinkChildIndex();
}
std::u16string BrowserAccessibility::GetTextContentUTF16() const {
return node()->GetTextContentUTF16();
}
std::u16string BrowserAccessibility::GetValueForControl() const {
return base::UTF8ToUTF16(node()->GetValueForControl());
}
gfx::Rect BrowserAccessibility::RelativeToAbsoluteBounds(
gfx::RectF bounds,
const ui::AXCoordinateSystem coordinate_system,
const ui::AXClippingBehavior clipping_behavior,
ui::AXOffscreenResult* offscreen_result) const {
// TODO(crbug.com/1049261): This is one of the few methods that won't be moved
// to `AXNode` in the foreseeable future because the functionality it provides
// is not immediately needed in Views.
const bool clip_bounds =
clipping_behavior == ui::AXClippingBehavior::kClipped;
bool offscreen = false;
const BrowserAccessibility* node = this;
while (node) {
BrowserAccessibilityManager* manager = node->manager();
bounds = manager->ax_tree()->RelativeToTreeBounds(node->node(), bounds,
&offscreen, clip_bounds);
// On some platforms we need to unapply root scroll offsets.
if (!manager->UseRootScrollOffsetsWhenComputingBounds()) {
// Get the node that's the "root scroller", which isn't necessarily
// the root of the tree.
ui::AXNodeID root_scroller_id = manager->GetTreeData().root_scroller_id;
BrowserAccessibility* root_scroller =
manager->GetFromID(root_scroller_id);
if (root_scroller) {
int sx = 0;
int sy = 0;
if (root_scroller->GetIntAttribute(ax::mojom::IntAttribute::kScrollX,
&sx) &&
root_scroller->GetIntAttribute(ax::mojom::IntAttribute::kScrollY,
&sy)) {
bounds.Offset(sx, sy);
}
}
}
if (coordinate_system == ui::AXCoordinateSystem::kFrame)
break;
const BrowserAccessibility* root = manager->GetRoot();
node = root->PlatformGetParent();
}
if (coordinate_system == ui::AXCoordinateSystem::kScreenDIPs ||
coordinate_system == ui::AXCoordinateSystem::kScreenPhysicalPixels) {
// Most platforms include page scale factor in the transform on the root
// node of the AXTree. That transform gets applied by the call to
// RelativeToTreeBounds() in the loop above. However, if the root transform
// did not include page scale factor, we need to apply it now.
// TODO(crbug.com/1074116): this should probably apply visual viewport
// offset as well.
if (!content::AXShouldIncludePageScaleFactorInRoot()) {
BrowserAccessibilityManager* root_manager = manager()->GetRootManager();
if (root_manager)
bounds.Scale(root_manager->GetPageScaleFactor());
}
bounds.Offset(
manager()->GetViewBoundsInScreenCoordinates().OffsetFromOrigin());
}
if (offscreen_result) {
*offscreen_result = offscreen ? ui::AXOffscreenResult::kOffscreen
: ui::AXOffscreenResult::kOnscreen;
}
return gfx::ToEnclosingRect(bounds);
}
bool BrowserAccessibility::IsOffscreen() const {
ui::AXOffscreenResult offscreen_result = ui::AXOffscreenResult::kOnscreen;
RelativeToAbsoluteBounds(gfx::RectF(), ui::AXCoordinateSystem::kRootFrame,
ui::AXClippingBehavior::kClipped, &offscreen_result);
return offscreen_result == ui::AXOffscreenResult::kOffscreen;
}
bool BrowserAccessibility::IsMinimized() const {
return false;
}
bool BrowserAccessibility::IsText() const {
return node()->IsText();
}
bool BrowserAccessibility::IsWebContent() const {
return true;
}
bool BrowserAccessibility::IsReadOnlySupported() const {
return node()->IsReadOnlySupported();
}
bool BrowserAccessibility::IsReadOnlyOrDisabled() const {
return node()->IsReadOnlyOrDisabled();
}
bool BrowserAccessibility::HasVisibleCaretOrSelection() const {
// The caret should be visible if Caret Browsing is enabled.
//
// TODO(crbug.com/1052091): Caret Browsing should be looking at leaf text
// nodes so it might not return expected results in this method.
if (BrowserAccessibilityStateImpl::GetInstance()->IsCaretBrowsingEnabled())
return true;
return node()->HasVisibleCaretOrSelection();
}
std::set<ui::AXPlatformNode*> BrowserAccessibility::GetNodesForNodeIdSet(
const std::set<int32_t>& ids) {
std::set<ui::AXPlatformNode*> nodes;
for (int32_t node_id : ids) {
if (ui::AXPlatformNode* node = GetFromNodeID(node_id)) {
nodes.insert(node);
}
}
return nodes;
}
ui::AXPlatformNode* BrowserAccessibility::GetTargetNodeForRelation(
ax::mojom::IntAttribute attr) {
DCHECK(ui::IsNodeIdIntAttribute(attr));
int target_id;
if (!node()->GetIntAttribute(attr, &target_id))
return nullptr;
return GetFromNodeID(target_id);
}
std::vector<ui::AXPlatformNode*>
BrowserAccessibility::GetTargetNodesForRelation(
ax::mojom::IntListAttribute attr) {
DCHECK(ui::IsNodeIdIntListAttribute(attr));
std::vector<int32_t> target_ids;
if (!GetIntListAttribute(attr, &target_ids))
return std::vector<ui::AXPlatformNode*>();
// If we use std::set to eliminate duplicates, the resulting set will be
// sorted by the id and we will lose the original order provided by the
// author which may be of interest to ATs. The number of ids should be small.
std::vector<ui::AXPlatformNode*> nodes;
for (int32_t target_id : target_ids) {
if (ui::AXPlatformNode* node = GetFromNodeID(target_id)) {
if (std::find(nodes.begin(), nodes.end(), node) == nodes.end())
nodes.push_back(node);
}
}
return nodes;
}
std::set<ui::AXPlatformNode*> BrowserAccessibility::GetReverseRelations(
ax::mojom::IntAttribute attr) {
DCHECK(manager_);
DCHECK(ui::IsNodeIdIntAttribute(attr));
return GetNodesForNodeIdSet(
manager_->ax_tree()->GetReverseRelations(attr, GetData().id));
}
std::set<ui::AXPlatformNode*> BrowserAccessibility::GetReverseRelations(
ax::mojom::IntListAttribute attr) {
DCHECK(manager_);
DCHECK(ui::IsNodeIdIntListAttribute(attr));
return GetNodesForNodeIdSet(
manager_->ax_tree()->GetReverseRelations(attr, GetData().id));
}
std::u16string BrowserAccessibility::GetAuthorUniqueId() const {
return node()->GetHtmlAttribute("id");
}
const ui::AXUniqueId& BrowserAccessibility::GetUniqueId() const {
// This is not the same as GetData().id which comes from Blink, because
// those ids are only unique within the Blink process. We need one that is
// unique for the browser process.
return unique_id_;
}
std::string BrowserAccessibility::SubtreeToStringHelper(size_t level) {
std::string result(level * 2, '+');
result += ToString();
result += '\n';
for (InternalChildIterator it = InternalChildrenBegin();
it != InternalChildrenEnd(); ++it) {
BrowserAccessibility* child = it.get();
DCHECK(child);
result += child->SubtreeToStringHelper(level + 1);
}
return result;
}
const std::vector<gfx::NativeViewAccessible>
BrowserAccessibility::GetUIADirectChildrenInRange(
ui::AXPlatformNodeDelegate* start,
ui::AXPlatformNodeDelegate* end) {
// This method is only called on Windows. Other platforms should not call it.
// The BrowserAccessibilityWin subclass overrides this method.
NOTREACHED();
return {};
}
std::string BrowserAccessibility::GetLanguage() const {
return node()->GetLanguage();
}
gfx::NativeViewAccessible BrowserAccessibility::GetNativeViewAccessible() {
// TODO(703369) On Windows, where we have started to migrate to an
// AXPlatformNode implementation, the BrowserAccessibilityWin subclass has
// overridden this method. On all other platforms, this method should not be
// called yet. In the future, when all subclasses have moved over to be
// implemented by AXPlatformNode, we may make this method completely virtual.
NOTREACHED();
return nullptr;
}
//
// AXPlatformNodeDelegate.
//
const ui::AXNodeData& BrowserAccessibility::GetData() const {
return node()->data();
}
const ui::AXTreeData& BrowserAccessibility::GetTreeData() const {