Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / ui / accessibility / ax_table_info.h
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef UI_ACCESSIBILITY_AX_TABLE_INFO_H_
6 #define UI_ACCESSIBILITY_AX_TABLE_INFO_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
12 #include "base/memory/raw_ptr.h"
13 #include "build/build_config.h"
14 #include "ui/accessibility/ax_export.h"
15 #include "ui/accessibility/ax_node_data.h"
16
17 namespace ui {
18
19 class AXTree;
20 class AXNode;
21
22 #if BUILDFLAG(IS_MAC)
23 #define AX_EXTRA_MAC_NODES 1
24 #endif
25
26 // This helper class computes info about tables and grids in AXTrees.
27 class AX_EXPORT AXTableInfo {
28  public:
29   struct CellData {
30     raw_ptr<AXNode, DanglingUntriaged> cell;
31     AXNodeID cell_id;
32     size_t col_index;
33     size_t row_index;
34     size_t col_span;
35     size_t row_span;
36     size_t aria_col_index;
37     size_t aria_row_index;
38   };
39
40   // Returns nullptr if the node is not a valid table or grid node.
41   static AXTableInfo* Create(AXTree* tree, AXNode* table_node);
42
43   ~AXTableInfo();
44
45   // Called automatically on Create(), but must be called again any time
46   // the table is invalidated. Returns true if this is still a table.
47   bool Update();
48
49   // Whether the data is valid. Whenever the tree is updated in any way,
50   // every AXTableInfo is invalidated and needs to be recomputed, just
51   // to be safe.
52   bool valid() const { return valid_; }
53   void Invalidate();
54
55   const AXNode* GetFirstCellInRow(const AXNode*) const;
56
57   // The real row count, guaranteed to be at least as large as the
58   // maximum row index of any cell.
59   size_t row_count = 0;
60
61   // The real column count, guaranteed to be at least as large as the
62   // maximum column index of any cell.
63   size_t col_count = 0;
64
65   // List of column header nodes IDs for each column index.
66   std::vector<std::vector<AXNodeID>> col_headers;
67
68   // All column header cells in a one dimensional array.
69   std::vector<AXNodeID> all_col_headers;
70
71   // List of row header node IDs for each row index.
72   std::vector<std::vector<AXNodeID>> row_headers;
73
74   // The id of the element with the caption tag or ARIA role.
75   AXNodeID caption_id;
76
77   // 2-D array of [row][column] -> cell node ID.
78   // This may contain duplicates if there is a rowspan or
79   // colspan. The entry is empty (zero) only if the cell
80   // really is missing from the table.
81   std::vector<std::vector<AXNodeID>> cell_ids;
82
83   // Array of cell data for every unique cell in the table.
84   std::vector<CellData> cell_data_vector;
85
86   // Set of all unique cell node IDs in the table.
87   std::vector<AXNodeID> unique_cell_ids;
88
89   // Extra computed nodes for the accessibility tree for macOS:
90   // one column node for each table column, followed by one
91   // table header container node.
92   std::vector<AXNode*> extra_mac_nodes;
93
94   // Map from each cell's node ID to its index in unique_cell_ids.
95   std::map<AXNodeID, size_t> cell_id_to_index;
96
97   // Map from each row's node ID to its row index.
98   std::map<AXNodeID, size_t> row_id_to_index;
99
100   // List of ax nodes that represent the rows of the table.
101   std::vector<AXNode*> row_nodes;
102
103   // The ARIA row count and column count, if any ARIA table or grid
104   // attributes are used in the table at all.
105   int aria_row_count = 0;
106   int aria_col_count = 0;
107
108   std::string ToString() const;
109
110  private:
111   struct CellBuildState {
112    public:
113     size_t cell_index;
114     size_t current_col_index;
115     size_t current_row_index;
116     size_t spanned_col_index;
117     size_t current_aria_row_index;
118     size_t current_aria_col_index;
119     bool is_first_cell_in_row;
120   };
121
122   AXTableInfo(AXTree* tree, AXNode* table_node);
123
124   void ClearVectors();
125   void BuildCellDataVectorFromRowAndCellNodes(
126       const std::vector<AXNode*>& row_node_list,
127       const std::vector<std::vector<AXNode*>>& cell_nodes_per_row);
128   void BuildCellDataVectorFromCellNodes(
129       const std::vector<std::vector<AXNode*>>& cell_nodes_per_row);
130   void BuildCellData(AXNode* cell,
131                      AXNode* row_or_first_cell,
132                      CellBuildState& state);
133   void BuildCellAndHeaderVectorsFromCellData();
134   void UpdateExtraMacNodes();
135   void ClearExtraMacNodes();
136
137   AXNode* CreateExtraMacColumnNode(size_t col_index);
138   AXNode* CreateExtraMacTableHeaderNode();
139   void UpdateExtraMacColumnNodeAttributes(size_t col_index);
140
141   raw_ptr<AXTree> tree_ = nullptr;
142   raw_ptr<AXNode> table_node_ = nullptr;
143   bool valid_ = false;
144   std::map<int, std::map<int, CellData>> incremental_row_col_map_;
145 };
146
147 }  // namespace ui
148
149 #endif  // UI_ACCESSIBILITY_AX_TABLE_INFO_H_