1 // Copyright 2016 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 #ifndef UI_ACCESSIBILITY_AX_TREE_COMBINER_H_
6 #define UI_ACCESSIBILITY_AX_TREE_COMBINER_H_
7 
8 #include <vector>
9 
10 #include "ui/accessibility/ax_export.h"
11 #include "ui/accessibility/ax_tree_id_registry.h"
12 #include "ui/accessibility/ax_tree_update.h"
13 
14 namespace ui {
15 
16 // This helper class takes multiple accessibility trees that reference each
17 // other via tree IDs, and combines them into a single accessibility tree
18 // that spans all of them.
19 //
20 // Since node IDs are relative to each ID, it has to renumber all of the IDs
21 // and update all of the attributes that reference IDs of other nodes to
22 // ensure they point to the right node.
23 //
24 // It also makes sure the final combined tree points to the correct focused
25 // node across all of the trees based on the focused tree ID of the root tree.
26 class AX_EXPORT AXTreeCombiner {
27  public:
28   AXTreeCombiner();
29   ~AXTreeCombiner();
30 
31   void AddTree(const AXTreeUpdate& tree, bool is_root);
32   bool Combine();
33 
combined()34   const AXTreeUpdate& combined() { return combined_; }
35 
36  private:
37   int32_t MapId(AXTreeID tree_id, int32_t node_id);
38 
39   void ProcessTree(const AXTreeUpdate* tree);
40 
41   std::vector<ui::AXTreeUpdate> trees_;
42   AXTreeID root_tree_id_;
43   int32_t next_id_ = 1;
44   std::map<AXTreeID, const AXTreeUpdate*> tree_id_map_;
45   std::map<std::pair<AXTreeID, int32_t>, int32_t> tree_id_node_id_map_;
46   AXTreeUpdate combined_;
47 };
48 
49 
50 }  // namespace ui
51 
52 #endif  // UI_ACCESSIBILITY_AX_TREE_COMBINER_H_
53