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