Repository navigation
Expand file tree
/
Copy pathax_platform_node_base.cc
More file actions
2672 lines (2310 loc) · 93.4 KB
/
Copy pathax_platform_node_base.cc
File metadata and controls
2672 lines (2310 loc) · 93.4 KB
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 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/platform/ax_platform_node_base.h"
#include <iomanip>
#include <limits>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include "base/no_destructor.h"
#include "base/numerics/checked_math.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_dump_provider.h"
#include "base/trace_event/process_memory_dump.h"
#include "build/build_config.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/ax_role_properties.h"
#include "ui/accessibility/ax_selection.h"
#include "ui/accessibility/ax_tree_data.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
#include "ui/accessibility/platform/compute_attributes.h"
#include "ui/gfx/geometry/rect_conversions.h"
namespace ui {
namespace {
using OnNotifyEventCallbackMap =
std::map<ax::mojom::Event,
// A function to call when focus changes, for testing only.
base::RepeatingClosure>;
OnNotifyEventCallbackMap& GetOnNotifyEventCallbackMap() {
static base::NoDestructor<OnNotifyEventCallbackMap>
on_notify_event_for_testing;
return *on_notify_event_for_testing;
}
// Check for descendant comment, using limited depth first search.
bool FindDescendantRoleWithMaxDepth(const AXPlatformNodeBase* node,
ax::mojom::Role descendant_role,
size_t max_depth,
size_t max_children_to_check) {
if (node->GetRole() == descendant_role)
return true;
if (max_depth <= 1)
return false;
size_t num_children_to_check =
std::min(node->GetChildCount(), max_children_to_check);
for (size_t index = 0; index < num_children_to_check; index++) {
auto* child = static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(node->ChildAtIndex(index)));
if (child &&
FindDescendantRoleWithMaxDepth(child, descendant_role, max_depth - 1,
max_children_to_check)) {
return true;
}
}
return false;
}
// Map from each AXPlatformNode's unique id to its instance.
using UniqueIdMap = std::unordered_map<int32_t, AXPlatformNode*>;
base::LazyInstance<UniqueIdMap>::Leaky g_unique_id_map =
LAZY_INSTANCE_INITIALIZER;
// Adds process-wide statistics about accessibility objects to traces.
class AXPlatformNodeMemoryDumpProvider
: public base::trace_event::MemoryDumpProvider {
public:
AXPlatformNodeMemoryDumpProvider(const AXPlatformNodeMemoryDumpProvider&) =
delete;
AXPlatformNodeMemoryDumpProvider& operator=(
const AXPlatformNodeMemoryDumpProvider&) = delete;
// base::trace_event::MemoryDumpProvider:
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
private:
friend class base::NoDestructor<AXPlatformNodeMemoryDumpProvider>;
explicit AXPlatformNodeMemoryDumpProvider(const UniqueIdMap& id_to_node);
~AXPlatformNodeMemoryDumpProvider() override = default;
const raw_ref<const UniqueIdMap> id_to_node_;
};
bool AXPlatformNodeMemoryDumpProvider::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) {
auto* const dump = pmd->CreateAllocatorDump("accessibility/ax_platform_node");
dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount,
base::trace_event::MemoryAllocatorDump::kUnitsObjects,
id_to_node_->size());
return true;
}
AXPlatformNodeMemoryDumpProvider::AXPlatformNodeMemoryDumpProvider(
const UniqueIdMap& id_to_node)
: id_to_node_(id_to_node) {
// Skip this in tests that don't set up a task runner on the main thread.
if (base::SingleThreadTaskRunner::HasCurrentDefault()) {
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
this, "AXPlatformNode",
base::SingleThreadTaskRunner::GetCurrentDefault());
}
}
} // namespace
const char16_t AXPlatformNodeBase::kEmbeddedCharacter = u'\xfffc';
// TODO(fxbug.dev/91030): Remove the !BUILDFLAG(IS_FUCHSIA) condition once
// fuchsia has native accessibility.
#if !BUILDFLAG_INTERNAL_HAS_NATIVE_ACCESSIBILITY() && !BUILDFLAG(IS_FUCHSIA)
// static
AXPlatformNode* AXPlatformNode::Create(AXPlatformNodeDelegate* delegate) {
AXPlatformNodeBase* node = new AXPlatformNodeBase();
node->Init(delegate);
return node;
}
#endif
// static
AXPlatformNode* AXPlatformNodeBase::GetFromUniqueId(int32_t unique_id) {
UniqueIdMap* unique_ids = g_unique_id_map.Pointer();
auto iter = unique_ids->find(unique_id);
if (iter != unique_ids->end())
return iter->second;
return nullptr;
}
// static
size_t AXPlatformNodeBase::GetInstanceCountForTesting() {
return g_unique_id_map.Get().size();
}
// static
void AXPlatformNodeBase::SetOnNotifyEventCallbackForTesting(
ax::mojom::Event event_type,
base::RepeatingClosure callback) {
OnNotifyEventCallbackMap& callback_map = GetOnNotifyEventCallbackMap();
callback_map[event_type] = std::move(callback);
}
AXPlatformNodeBase::AXPlatformNodeBase() = default;
AXPlatformNodeBase::~AXPlatformNodeBase() = default;
void AXPlatformNodeBase::Init(AXPlatformNodeDelegate* delegate) {
delegate_ = delegate;
// This must be called after assigning our delegate.
g_unique_id_map.Get()[GetUniqueId()] = this;
static base::NoDestructor<AXPlatformNodeMemoryDumpProvider> dump_provider(
g_unique_id_map.Get());
}
const AXNodeData& AXPlatformNodeBase::GetData() const {
static const base::NoDestructor<AXNodeData> empty_data;
if (delegate_)
return delegate_->GetData();
return *empty_data;
}
gfx::NativeViewAccessible AXPlatformNodeBase::GetFocus() const {
if (delegate_)
return delegate_->GetFocus();
return nullptr;
}
gfx::NativeViewAccessible AXPlatformNodeBase::GetParent() const {
if (delegate_)
return delegate_->GetParent();
return nullptr;
}
AXPlatformNodeBase* AXPlatformNodeBase::GetPlatformParent() const {
if (delegate_)
return FromNativeViewAccessible(delegate_->GetParent());
return nullptr;
}
AXPlatformNodeBase* AXPlatformNodeBase::GetPlatformTextFieldAncestor() const {
if (delegate_)
return FromNativeViewAccessible(delegate_->GetTextFieldAncestor());
return nullptr;
}
size_t AXPlatformNodeBase::GetChildCount() const {
if (delegate_)
return delegate_->GetChildCount();
return 0;
}
gfx::NativeViewAccessible AXPlatformNodeBase::ChildAtIndex(size_t index) const {
if (delegate_)
return delegate_->ChildAtIndex(index);
return nullptr;
}
std::string AXPlatformNodeBase::GetName() const {
if (delegate_) {
std::string name = delegate_->GetName();
// Compute extra name based on the image annotation (generated alt text)
// results.
std::string extra_text;
ax::mojom::ImageAnnotationStatus status =
GetData().GetImageAnnotationStatus();
switch (status) {
case ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kAnnotationPending:
case ax::mojom::ImageAnnotationStatus::kAnnotationEmpty:
case ax::mojom::ImageAnnotationStatus::kAnnotationAdult:
case ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed:
extra_text = base::UTF16ToUTF8(
delegate_->GetLocalizedStringForImageAnnotationStatus(status));
break;
case ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded:
extra_text =
GetStringAttribute(ax::mojom::StringAttribute::kImageAnnotation);
break;
case ax::mojom::ImageAnnotationStatus::kNone:
case ax::mojom::ImageAnnotationStatus::kWillNotAnnotateDueToScheme:
case ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kSilentlyEligibleForAnnotation:
break;
}
if (!extra_text.empty()) {
if (!name.empty())
name += ". ";
name += extra_text;
}
DCHECK(base::IsStringUTF8AllowingNoncharacters(name)) << "Invalid UTF8";
return name;
}
return std::string();
}
absl::optional<size_t> AXPlatformNodeBase::GetIndexInParent() {
AXPlatformNodeBase* parent = FromNativeViewAccessible(GetParent());
if (!parent)
return absl::nullopt;
// If this is the webview, it is not in the child in the list of its parent's
// child.
// TODO(jkim): Check if we could remove this after making WebView ignored.
if (delegate_ &&
delegate_->GetNativeViewAccessible() != GetNativeViewAccessible()) {
return absl::nullopt;
}
size_t child_count = parent->GetChildCount();
if (child_count == 0) {
// |child_count| could be 0 if the parent is IsLeaf.
DCHECK(parent->IsLeaf());
return absl::nullopt;
}
// Ask the delegate for the index in parent, and return it if it's plausible.
//
// Delegates are allowed to not implement this (ViewsAXPlatformNodeDelegate
// returns -1). Also, delegates may not know the correct answer if this
// node is the root of a tree that's embedded in another tree, in which
// case the delegate should return -1 and we'll compute it.
auto index = delegate_ ? delegate_->GetIndexInParent() : absl::nullopt;
if (index.has_value() && index.value() < child_count)
return index;
// Otherwise, search the parent's children.
gfx::NativeViewAccessible current = GetNativeViewAccessible();
for (size_t i = 0; i < child_count; i++) {
if (parent->ChildAtIndex(i) == current)
return i;
}
// If the parent has a modal dialog, it doesn't count other children.
if (parent->delegate_ && parent->delegate_->HasModalDialog())
return absl::nullopt;
DCHECK(false)
<< "Unable to find the child in the list of its parent's children.";
return absl::nullopt;
}
base::stack<gfx::NativeViewAccessible> AXPlatformNodeBase::GetAncestors() {
base::stack<gfx::NativeViewAccessible> ancestors;
gfx::NativeViewAccessible current_node = GetNativeViewAccessible();
while (current_node) {
ancestors.push(current_node);
current_node = FromNativeViewAccessible(current_node)->GetParent();
}
return ancestors;
}
absl::optional<int> AXPlatformNodeBase::CompareTo(AXPlatformNodeBase& other) {
// We define two node's relative positions in the following way:
// 1. this->CompareTo(other) == 0:
// - |this| and |other| are the same node.
// 2. this->CompareTo(other) < 0:
// - |this| is an ancestor of |other|.
// - |this|'s first uncommon ancestor comes before |other|'s first uncommon
// ancestor. The first uncommon ancestor is defined as the immediate child
// of the lowest common anestor of the two nodes. The first uncommon
// ancestor of |this| and |other| share the same parent (i.e. lowest common
// ancestor), so we can just compare the first uncommon ancestors' child
// indices to determine their relative positions.
// 3. this->CompareTo(other) == nullopt:
// - |this| and |other| are not comparable. E.g. they do not have a common
// ancestor.
//
// Another way to look at the nodes' relative positions/logical orders is that
// they are equivalent to pre-order traversal of the tree. If we pre-order
// traverse from the root, the node that we visited earlier is always going to
// be before (logically less) the node we visit later.
if (this == &other)
return absl::optional<int>(0);
// Compute the ancestor stacks of both positions and traverse them from the
// top most ancestor down, so we can discover the first uncommon ancestors.
// The first uncommon ancestor is the immediate child of the lowest common
// ancestor.
gfx::NativeViewAccessible common_ancestor = nullptr;
base::stack<gfx::NativeViewAccessible> our_ancestors = GetAncestors();
base::stack<gfx::NativeViewAccessible> other_ancestors = other.GetAncestors();
// Start at the root and traverse down. Keep going until the |this|'s ancestor
// chain and |other|'s ancestor chain disagree. The last node before they
// disagree is the lowest common ancestor.
while (!our_ancestors.empty() && !other_ancestors.empty() &&
our_ancestors.top() == other_ancestors.top()) {
common_ancestor = our_ancestors.top();
our_ancestors.pop();
other_ancestors.pop();
}
// Nodes do not have a common ancestor, they are not comparable.
if (!common_ancestor)
return absl::nullopt;
// Compute the logical order when the common ancestor is |this| or |other|.
auto* common_ancestor_platform_node =
FromNativeViewAccessible(common_ancestor);
if (common_ancestor_platform_node == this)
return absl::optional<int>(-1);
if (common_ancestor_platform_node == &other)
return absl::optional<int>(1);
// Compute the logical order of |this| and |other| by using their first
// uncommon ancestors.
if (!our_ancestors.empty() && !other_ancestors.empty()) {
absl::optional<int> this_index_in_parent =
FromNativeViewAccessible(our_ancestors.top())->GetIndexInParent();
absl::optional<int> other_index_in_parent =
FromNativeViewAccessible(other_ancestors.top())->GetIndexInParent();
if (!this_index_in_parent || !other_index_in_parent)
return absl::nullopt;
int this_uncommon_ancestor_index = this_index_in_parent.value();
int other_uncommon_ancestor_index = other_index_in_parent.value();
DCHECK_NE(this_uncommon_ancestor_index, other_uncommon_ancestor_index)
<< "Deepest uncommon ancestors should truly be uncommon, i.e. not "
"the same.";
return absl::optional<int>(this_uncommon_ancestor_index -
other_uncommon_ancestor_index);
}
return absl::nullopt;
}
AXNodeID AXPlatformNodeBase::GetNodeId() const {
if (!delegate_)
return kInvalidAXNodeID;
return delegate_->GetData().id;
}
AXPlatformNodeBase* AXPlatformNodeBase::GetActiveDescendant() const {
if (!delegate_)
return nullptr;
AXNodeID active_descendant_id;
AXPlatformNodeBase* active_descendant = nullptr;
if (GetIntAttribute(ax::mojom::IntAttribute::kActivedescendantId,
&active_descendant_id)) {
active_descendant = static_cast<AXPlatformNodeBase*>(
delegate_->GetFromNodeID(active_descendant_id));
}
if (GetRole() == ax::mojom::Role::kComboBoxSelect) {
AXPlatformNodeBase* child = GetFirstChild();
if (child && child->GetRole() == ax::mojom::Role::kMenuListPopup &&
!child->IsInvisibleOrIgnored()) {
// The active descendant is found on the menu list popup, i.e. on the
// actual list and not on the button that opens it.
// If there is no active descendant, focus should stay on the button so
// that Windows screen readers would enable their virtual cursor.
// Do not expose an activedescendant in a hidden/collapsed list, as
// screen readers expect the focus event to go to the button itself.
// Note that the AX hierarchy in this case is strange -- the active
// option is the only visible option, and is inside an invisible list.
if (child->GetIntAttribute(ax::mojom::IntAttribute::kActivedescendantId,
&active_descendant_id)) {
active_descendant = static_cast<AXPlatformNodeBase*>(
child->delegate_->GetFromNodeID(active_descendant_id));
}
}
}
if (active_descendant && !active_descendant->IsInvisibleOrIgnored())
return active_descendant;
return nullptr;
}
// AXPlatformNode overrides.
void AXPlatformNodeBase::Destroy() {
g_unique_id_map.Get().erase(GetUniqueId());
AXPlatformNode::Destroy();
delegate_ = nullptr;
Dispose();
}
void AXPlatformNodeBase::Dispose() {
delete this;
}
gfx::NativeViewAccessible AXPlatformNodeBase::GetNativeViewAccessible() {
return nullptr;
}
void AXPlatformNodeBase::NotifyAccessibilityEvent(ax::mojom::Event event_type) {
OnNotifyEventCallbackMap& callback_map = GetOnNotifyEventCallbackMap();
if (callback_map.find(event_type) != callback_map.end() &&
callback_map[event_type]) {
callback_map[event_type].Run();
}
}
#if BUILDFLAG(IS_APPLE)
void AXPlatformNodeBase::AnnounceText(const std::u16string& text) {}
#endif
AXPlatformNodeDelegate* AXPlatformNodeBase::GetDelegate() const {
return delegate_;
}
bool AXPlatformNodeBase::IsDescendantOf(AXPlatformNode* ancestor) const {
if (!ancestor)
return false;
if (this == ancestor)
return true;
AXPlatformNodeBase* parent = FromNativeViewAccessible(GetParent());
if (!parent)
return false;
return parent->IsDescendantOf(ancestor);
}
AXPlatformNodeBase::AXPlatformNodeChildIterator
AXPlatformNodeBase::AXPlatformNodeChildrenBegin() const {
return AXPlatformNodeChildIterator(this, GetFirstChild());
}
AXPlatformNodeBase::AXPlatformNodeChildIterator
AXPlatformNodeBase::AXPlatformNodeChildrenEnd() const {
return AXPlatformNodeChildIterator(this, nullptr);
}
// Helpers.
AXPlatformNodeBase* AXPlatformNodeBase::GetPreviousSibling() const {
if (!delegate_)
return nullptr;
return FromNativeViewAccessible(delegate_->GetPreviousSibling());
}
AXPlatformNodeBase* AXPlatformNodeBase::GetNextSibling() const {
if (!delegate_)
return nullptr;
return FromNativeViewAccessible(delegate_->GetNextSibling());
}
AXPlatformNodeBase* AXPlatformNodeBase::GetFirstChild() const {
if (!delegate_)
return nullptr;
return FromNativeViewAccessible(delegate_->GetFirstChild());
}
AXPlatformNodeBase* AXPlatformNodeBase::GetLastChild() const {
if (!delegate_)
return nullptr;
return FromNativeViewAccessible(delegate_->GetLastChild());
}
bool AXPlatformNodeBase::IsDescendant(AXPlatformNodeBase* node) {
if (!delegate_)
return false;
if (!node)
return false;
if (node == this)
return true;
gfx::NativeViewAccessible native_parent = node->GetParent();
if (!native_parent)
return false;
AXPlatformNodeBase* parent = FromNativeViewAccessible(native_parent);
return IsDescendant(parent);
}
ax::mojom::Role AXPlatformNodeBase::GetRole() const {
if (!delegate_)
return ax::mojom::Role::kUnknown;
return delegate_->GetRole();
}
bool AXPlatformNodeBase::HasBoolAttribute(
ax::mojom::BoolAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasBoolAttribute(attribute);
}
bool AXPlatformNodeBase::GetBoolAttribute(
ax::mojom::BoolAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->GetBoolAttribute(attribute);
}
bool AXPlatformNodeBase::GetBoolAttribute(ax::mojom::BoolAttribute attribute,
bool* value) const {
if (!delegate_)
return false;
return delegate_->GetBoolAttribute(attribute, value);
}
bool AXPlatformNodeBase::HasFloatAttribute(
ax::mojom::FloatAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasFloatAttribute(attribute);
}
float AXPlatformNodeBase::GetFloatAttribute(
ax::mojom::FloatAttribute attribute) const {
if (!delegate_)
return 0.0f;
return delegate_->GetFloatAttribute(attribute);
}
bool AXPlatformNodeBase::GetFloatAttribute(ax::mojom::FloatAttribute attribute,
float* value) const {
if (!delegate_)
return false;
return delegate_->GetFloatAttribute(attribute, value);
}
const std::vector<std::pair<ax::mojom::IntAttribute, int32_t>>&
AXPlatformNodeBase::GetIntAttributes() const {
static const base::NoDestructor<
const std::vector<std::pair<ax::mojom::IntAttribute, int32_t>>>
empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetIntAttributes();
}
bool AXPlatformNodeBase::HasIntAttribute(
ax::mojom::IntAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasIntAttribute(attribute);
}
int AXPlatformNodeBase::GetIntAttribute(
ax::mojom::IntAttribute attribute) const {
if (!delegate_)
return 0;
return delegate_->GetIntAttribute(attribute);
}
bool AXPlatformNodeBase::GetIntAttribute(ax::mojom::IntAttribute attribute,
int* value) const {
if (!delegate_)
return false;
return delegate_->GetIntAttribute(attribute, value);
}
const std::vector<std::pair<ax::mojom::StringAttribute, std::string>>&
AXPlatformNodeBase::GetStringAttributes() const {
static const base::NoDestructor<
const std::vector<std::pair<ax::mojom::StringAttribute, std::string>>>
empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetStringAttributes();
}
bool AXPlatformNodeBase::HasStringAttribute(
ax::mojom::StringAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasStringAttribute(attribute);
}
const std::string& AXPlatformNodeBase::GetStringAttribute(
ax::mojom::StringAttribute attribute) const {
if (!delegate_)
return base::EmptyString();
return delegate_->GetStringAttribute(attribute);
}
bool AXPlatformNodeBase::GetStringAttribute(
ax::mojom::StringAttribute attribute,
std::string* value) const {
if (!delegate_)
return false;
return delegate_->GetStringAttribute(attribute, value);
}
std::u16string AXPlatformNodeBase::GetString16Attribute(
ax::mojom::StringAttribute attribute) const {
if (!delegate_)
return std::u16string();
return delegate_->GetString16Attribute(attribute);
}
bool AXPlatformNodeBase::GetString16Attribute(
ax::mojom::StringAttribute attribute,
std::u16string* value) const {
if (!delegate_)
return false;
return delegate_->GetString16Attribute(attribute, value);
}
bool AXPlatformNodeBase::HasInheritedStringAttribute(
ax::mojom::StringAttribute attribute) const {
const AXPlatformNodeBase* current_node = this;
do {
if (!current_node->delegate_) {
return false;
}
if (current_node->HasStringAttribute(attribute)) {
return true;
}
current_node = FromNativeViewAccessible(current_node->GetParent());
} while (current_node);
return false;
}
const std::string& AXPlatformNodeBase::GetInheritedStringAttribute(
ax::mojom::StringAttribute attribute) const {
// TODO(nektar): Switch to using `AXNode::GetInheritedStringAttribute` after
// it has been modified to cross tree boundaries.
const AXPlatformNodeBase* current_node = this;
do {
if (!current_node->delegate_) {
return base::EmptyString();
}
if (current_node->HasStringAttribute(attribute)) {
return current_node->GetStringAttribute(attribute);
}
current_node = FromNativeViewAccessible(current_node->GetParent());
} while (current_node);
return base::EmptyString();
}
bool AXPlatformNodeBase::GetInheritedStringAttribute(
ax::mojom::StringAttribute attribute,
std::string* value) const {
// TODO(nektar): Switch to using `AXNode::GetInheritedStringAttribute` after
// it has been modified to cross tree boundaries.
const AXPlatformNodeBase* current_node = this;
do {
if (!current_node->delegate_) {
return false;
}
if (current_node->GetStringAttribute(attribute, value)) {
return true;
}
current_node = FromNativeViewAccessible(current_node->GetParent());
} while (current_node);
return false;
}
std::u16string AXPlatformNodeBase::GetInheritedString16Attribute(
ax::mojom::StringAttribute attribute) const {
// TODO(nektar): Switch to using `AXNode::GetInheritedString16Attribute` after
// it has been modified to cross tree boundaries.
return base::UTF8ToUTF16(GetInheritedStringAttribute(attribute));
}
bool AXPlatformNodeBase::GetInheritedString16Attribute(
ax::mojom::StringAttribute attribute,
std::u16string* value) const {
// TODO(nektar): Switch to using `AXNode::GetInheritedString16Attribute` after
// it has been modified to cross tree boundaries.
std::string value_utf8;
if (!GetInheritedStringAttribute(attribute, &value_utf8))
return false;
*value = base::UTF8ToUTF16(value_utf8);
return true;
}
const std::vector<std::pair<ax::mojom::IntListAttribute, std::vector<int32_t>>>&
AXPlatformNodeBase::GetIntListAttributes() const {
static const base::NoDestructor<const std::vector<
std::pair<ax::mojom::IntListAttribute, std::vector<int32_t>>>>
empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetIntListAttributes();
}
bool AXPlatformNodeBase::HasIntListAttribute(
ax::mojom::IntListAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasIntListAttribute(attribute);
}
const std::vector<int32_t>& AXPlatformNodeBase::GetIntListAttribute(
ax::mojom::IntListAttribute attribute) const {
static const base::NoDestructor<std::vector<int32_t>> empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetIntListAttribute(attribute);
}
bool AXPlatformNodeBase::GetIntListAttribute(
ax::mojom::IntListAttribute attribute,
std::vector<int32_t>* value) const {
if (!delegate_)
return false;
return delegate_->GetIntListAttribute(attribute, value);
}
bool AXPlatformNodeBase::HasStringListAttribute(
ax::mojom::StringListAttribute attribute) const {
if (!delegate_)
return false;
return delegate_->HasStringListAttribute(attribute);
}
const std::vector<std::string>& AXPlatformNodeBase::GetStringListAttribute(
ax::mojom::StringListAttribute attribute) const {
static const base::NoDestructor<std::vector<std::string>> empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetStringListAttribute(attribute);
}
bool AXPlatformNodeBase::GetStringListAttribute(
ax::mojom::StringListAttribute attribute,
std::vector<std::string>* value) const {
if (!delegate_)
return false;
return delegate_->GetStringListAttribute(attribute, value);
}
bool AXPlatformNodeBase::HasHtmlAttribute(const char* attribute) const {
if (!delegate_)
return false;
return delegate_->HasHtmlAttribute(attribute);
}
const base::StringPairs& AXPlatformNodeBase::GetHtmlAttributes() const {
static const base::NoDestructor<base::StringPairs> empty_data;
if (!delegate_)
return *empty_data;
return delegate_->GetHtmlAttributes();
}
bool AXPlatformNodeBase::GetHtmlAttribute(const char* attribute,
std::string* value) const {
if (!delegate_)
return false;
return delegate_->GetHtmlAttribute(attribute, value);
}
bool AXPlatformNodeBase::GetHtmlAttribute(const char* attribute,
std::u16string* value) const {
if (!delegate_)
return false;
return delegate_->GetHtmlAttribute(attribute, value);
}
AXTextAttributes AXPlatformNodeBase::GetTextAttributes() const {
if (!delegate_)
return AXTextAttributes();
return delegate_->GetTextAttributes();
}
bool AXPlatformNodeBase::HasState(ax::mojom::State state) const {
if (!delegate_)
return false;
return delegate_->HasState(state);
}
ax::mojom::State AXPlatformNodeBase::GetState() const {
if (!delegate_)
return ax::mojom::State::kNone;
return delegate_->GetState();
}
bool AXPlatformNodeBase::HasAction(ax::mojom::Action action) const {
if (!delegate_)
return false;
return delegate_->HasAction(action);
}
bool AXPlatformNodeBase::HasTextStyle(ax::mojom::TextStyle text_style) const {
if (!delegate_)
return false;
return delegate_->HasTextStyle(text_style);
}
ax::mojom::NameFrom AXPlatformNodeBase::GetNameFrom() const {
if (!delegate_)
return ax::mojom::NameFrom::kNone;
return delegate_->GetNameFrom();
}
bool AXPlatformNodeBase::HasNameFromOtherElement() const {
ax::mojom::NameFrom nameFrom = GetNameFrom();
return nameFrom == ax::mojom::NameFrom::kCaption ||
nameFrom == ax::mojom::NameFrom::kRelatedElement;
}
// static
AXPlatformNodeBase* AXPlatformNodeBase::FromNativeViewAccessible(
gfx::NativeViewAccessible accessible) {
return static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(accessible));
}
bool AXPlatformNodeBase::SetHypertextSelection(int start_offset,
int end_offset) {
if (!delegate_)
return false;
return delegate_->SetHypertextSelection(start_offset, end_offset);
}
bool AXPlatformNodeBase::IsPlatformDocument() const {
return delegate_ && delegate_->IsPlatformDocument();
}
bool AXPlatformNodeBase::IsStructuredAnnotation() const {
// The node represents a structured annotation if it can trace back to a
// target node that is being annotated.
std::vector<AXPlatformNode*> reverse_relations =
GetDelegate()->GetSourceNodesForReverseRelations(
ax::mojom::IntListAttribute::kDetailsIds);
return !reverse_relations.empty();
}
// TODO(accessibility): This is only used in AXPlatformNodeWin and therefore
// should be moved there.
bool AXPlatformNodeBase::IsSelectionItemSupported() const {
switch (GetRole()) {
// An ARIA 1.1+ role of "cell", or a role of "row" inside
// an ARIA 1.1 role of "table", should not be selectable.
// ARIA "table" is not interactable, ARIA "grid" is.
case ax::mojom::Role::kCell:
case ax::mojom::Role::kColumnHeader:
case ax::mojom::Role::kRow:
case ax::mojom::Role::kRowHeader: {
// An ARIA grid subwidget is only selectable if explicitly marked as
// selected (or not) with the aria-selected property.
if (!HasBoolAttribute(ax::mojom::BoolAttribute::kSelected))
return false;
AXPlatformNodeBase* table = GetTable();
if (!table)
return false;
return table->GetRole() == ax::mojom::Role::kGrid ||
table->GetRole() == ax::mojom::Role::kTreeGrid;
}
// https://www.w3.org/TR/core-aam-1.1/#mapping_state-property_table
// SelectionItem.IsSelected is exposed when aria-checked is True or False,
// for 'radio' and 'menuitemradio' roles.
case ax::mojom::Role::kRadioButton:
case ax::mojom::Role::kMenuItemRadio: {
if (GetData().GetCheckedState() == ax::mojom::CheckedState::kTrue ||
GetData().GetCheckedState() == ax::mojom::CheckedState::kFalse)
return true;
return false;
}
// https://www.w3.org/TR/wai-aria-1.1/#aria-selected
// SelectionItem.IsSelected is exposed when aria-select is True or False.
case ax::mojom::Role::kListBoxOption:
case ax::mojom::Role::kListItem:
case ax::mojom::Role::kMenuListOption:
case ax::mojom::Role::kTreeItem:
return HasBoolAttribute(ax::mojom::BoolAttribute::kSelected);
case ax::mojom::Role::kTab:
// According to the UIA documentation, this role should always support the
// SelectionItem control pattern:
// https://learn.microsoft.com/en-us/windows/win32/winauto/uiauto-supporttabitemcontroltype#required-control-patterns.
return true;
default:
return false;
}
}
bool AXPlatformNodeBase::IsTextField() const {
return GetData().IsTextField();
}
bool AXPlatformNodeBase::IsAtomicTextField() const {
return GetData().IsAtomicTextField();
}
bool AXPlatformNodeBase::IsNonAtomicTextField() const {
return GetData().IsNonAtomicTextField();
}
bool AXPlatformNodeBase::IsText() const {
return delegate_ && delegate_->IsText();
}
std::u16string AXPlatformNodeBase::GetHypertext() const {
if (!delegate_)
return std::u16string();
// Hypertext of platform leaves, which internally are composite objects, are
// represented with the text content of the internal composite object. These
// don't exist on non-web content.
if (IsChildOfLeaf())
return GetTextContentUTF16();
if (hypertext_.needs_update)
UpdateComputedHypertext();
return hypertext_.hypertext;
}
std::u16string AXPlatformNodeBase::GetTextContentUTF16() const {
if (!delegate_)
return std::u16string();
return delegate_->GetTextContentUTF16();
}
std::u16string
AXPlatformNodeBase::GetRoleDescriptionFromImageAnnotationStatusOrFromAttribute()
const {
if (GetRole() == ax::mojom::Role::kImage &&
(GetData().GetImageAnnotationStatus() ==
ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation ||
GetData().GetImageAnnotationStatus() ==
ax::mojom::ImageAnnotationStatus::kSilentlyEligibleForAnnotation)) {
return GetDelegate()->GetLocalizedRoleDescriptionForUnlabeledImage();
}
return GetString16Attribute(ax::mojom::StringAttribute::kRoleDescription);
}