1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ui/accessibility/platform/compute_attributes.h"
6 
7 #include <cstddef>
8 
9 #include "base/optional.h"
10 #include "ui/accessibility/ax_enums.mojom.h"
11 #include "ui/accessibility/ax_node_data.h"
12 #include "ui/accessibility/platform/ax_platform_node_delegate.h"
13 
14 namespace ui {
15 namespace {
16 
GetCellAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)17 base::Optional<int32_t> GetCellAttribute(
18     const ui::AXPlatformNodeDelegate* delegate,
19     ax::mojom::IntAttribute attribute) {
20   switch (attribute) {
21     case ax::mojom::IntAttribute::kAriaCellColumnIndex:
22       return delegate->GetTableCellAriaColIndex();
23     case ax::mojom::IntAttribute::kAriaCellRowIndex:
24       return delegate->GetTableCellAriaRowIndex();
25     case ax::mojom::IntAttribute::kTableCellColumnIndex:
26       return delegate->GetTableCellColIndex();
27     case ax::mojom::IntAttribute::kTableCellRowIndex:
28       return delegate->GetTableCellRowIndex();
29     case ax::mojom::IntAttribute::kTableCellColumnSpan:
30       return delegate->GetTableCellColSpan();
31     case ax::mojom::IntAttribute::kTableCellRowSpan:
32       return delegate->GetTableCellRowSpan();
33     default:
34       return base::nullopt;
35   }
36 }
37 
GetRowAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)38 base::Optional<int32_t> GetRowAttribute(
39     const ui::AXPlatformNodeDelegate* delegate,
40     ax::mojom::IntAttribute attribute) {
41   if (attribute == ax::mojom::IntAttribute::kTableRowIndex) {
42     return delegate->GetTableRowRowIndex();
43   }
44   return base::nullopt;
45 }
46 
GetTableAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)47 base::Optional<int32_t> GetTableAttribute(
48     const ui::AXPlatformNodeDelegate* delegate,
49     ax::mojom::IntAttribute attribute) {
50   switch (attribute) {
51     case ax::mojom::IntAttribute::kTableColumnCount:
52       return delegate->GetTableColCount();
53     case ax::mojom::IntAttribute::kTableRowCount:
54       return delegate->GetTableRowCount();
55     case ax::mojom::IntAttribute::kAriaColumnCount:
56       return delegate->GetTableAriaColCount();
57     case ax::mojom::IntAttribute::kAriaRowCount:
58       return delegate->GetTableAriaRowCount();
59     default:
60       return base::nullopt;
61   }
62 }
63 
GetOrderedSetItemAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)64 base::Optional<int> GetOrderedSetItemAttribute(
65     const ui::AXPlatformNodeDelegate* delegate,
66     ax::mojom::IntAttribute attribute) {
67   switch (attribute) {
68     case ax::mojom::IntAttribute::kPosInSet:
69       return delegate->GetPosInSet();
70     case ax::mojom::IntAttribute::kSetSize:
71       return delegate->GetSetSize();
72     default:
73       return base::nullopt;
74   }
75 }
76 
GetOrderedSetAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)77 base::Optional<int> GetOrderedSetAttribute(
78     const ui::AXPlatformNodeDelegate* delegate,
79     ax::mojom::IntAttribute attribute) {
80   switch (attribute) {
81     case ax::mojom::IntAttribute::kSetSize:
82       return delegate->GetSetSize();
83     default:
84       return base::nullopt;
85   }
86 }
87 
GetFromData(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)88 base::Optional<int32_t> GetFromData(const ui::AXPlatformNodeDelegate* delegate,
89                                     ax::mojom::IntAttribute attribute) {
90   int32_t value;
91   if (delegate->GetData().GetIntAttribute(attribute, &value)) {
92     return value;
93   }
94   return base::nullopt;
95 }
96 
97 }  // namespace
98 
ComputeAttribute(const ui::AXPlatformNodeDelegate * delegate,ax::mojom::IntAttribute attribute)99 base::Optional<int32_t> ComputeAttribute(
100     const ui::AXPlatformNodeDelegate* delegate,
101     ax::mojom::IntAttribute attribute) {
102   base::Optional<int32_t> maybe_value = base::nullopt;
103   // Table-related nodes.
104   if (delegate->IsTableCellOrHeader())
105     maybe_value = GetCellAttribute(delegate, attribute);
106   else if (delegate->IsTableRow())
107     maybe_value = GetRowAttribute(delegate, attribute);
108   else if (delegate->IsTable())
109     maybe_value = GetTableAttribute(delegate, attribute);
110   // Ordered-set-related nodes.
111   else if (delegate->IsOrderedSetItem())
112     maybe_value = GetOrderedSetItemAttribute(delegate, attribute);
113   else if (delegate->IsOrderedSet())
114     maybe_value = GetOrderedSetAttribute(delegate, attribute);
115 
116   if (!maybe_value.has_value()) {
117     return GetFromData(delegate, attribute);
118   }
119   return maybe_value;
120 }
121 
122 }  // namespace ui
123