1 // Copyright 2014 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_ID_REGISTRY_H_
6 #define UI_ACCESSIBILITY_AX_TREE_ID_REGISTRY_H_
7 
8 #include <map>
9 #include <string>
10 #include <utility>
11 
12 #include "base/macros.h"
13 #include "ui/accessibility/ax_export.h"
14 #include "ui/accessibility/ax_tree_id.h"
15 
16 namespace base {
17 template <typename T>
18 struct DefaultSingletonTraits;
19 }  // namespace base
20 
21 namespace ui {
22 
23 class AXActionHandler;
24 
25 // This class generates and saves a runtime id for an accessibility tree.
26 // It provides a few distinct forms of generating an id:
27 //     - from a frame id (which consists of a process and routing id)
28 //     - from a backing |AXActionHandler| object
29 //
30 // The first form allows underlying instances to change but refer to the same
31 // frame.
32 // The second form allows this registry to track the object for later retrieval.
33 class AX_EXPORT AXTreeIDRegistry {
34  public:
35   using FrameID = std::pair<int, int>;
36 
37   // Get the single instance of this class.
38   static AXTreeIDRegistry* GetInstance();
39 
40   // Gets the frame id based on an ax tree id.
41   FrameID GetFrameID(const AXTreeID& ax_tree_id);
42 
43   // Gets an ax tree id from a frame id.
44   AXTreeID GetAXTreeID(FrameID frame_id);
45 
46   // Retrieve an |AXActionHandler| based on an ax tree id.
47   AXActionHandler* GetActionHandler(AXTreeID ax_tree_id);
48 
49   // Removes an ax tree id, and its associated delegate and frame id (if it
50   // exists).
51   void RemoveAXTreeID(AXTreeID ax_tree_id);
52 
53   // Associate a frame id with an ax tree id.
54   void SetFrameIDForAXTreeID(const FrameID& frame_id,
55                              const AXTreeID& ax_tree_id);
56 
57  private:
58   friend struct base::DefaultSingletonTraits<AXTreeIDRegistry>;
59   friend AXActionHandler;
60 
61   // Get or create a ax tree id keyed on |handler|.
62   AXTreeID GetOrCreateAXTreeID(AXActionHandler* handler);
63 
64   AXTreeIDRegistry();
65   virtual ~AXTreeIDRegistry();
66 
67   // Maps an accessibility tree to its frame via ids.
68   std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_;
69 
70   // Maps frames to an accessibility tree via ids.
71   std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_;
72 
73   // Maps an id to its handler.
74   std::map<AXTreeID, AXActionHandler*> id_to_action_handler_;
75 
76   DISALLOW_COPY_AND_ASSIGN(AXTreeIDRegistry);
77 };
78 
79 }  // namespace ui
80 
81 #endif  // UI_ACCESSIBILITY_AX_TREE_ID_REGISTRY_H_
82