Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 5 | #ifndef UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_ |
| 6 | #define UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_ |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 7 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 8 | #include <cstdint> |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 9 | #include <map> |
thestig | cf9519fa | 2016-08-30 05:50:54 | [diff] [blame] | 10 | #include <utility> |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 11 | |
Ali Hijazi | 60a72b0a | 2024-09-30 17:58:53 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 13 | #include "base/observer_list.h" |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 14 | #include "ui/accessibility/ax_action_handler.h" |
Dominic Mazzoni | c39830f | 2021-02-02 05:58:26 | [diff] [blame] | 15 | #include "ui/accessibility/ax_base_export.h" |
Dominic Mazzoni | 336bc006 | 2018-09-23 16:46:43 | [diff] [blame] | 16 | #include "ui/accessibility/ax_tree_id.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 17 | |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 18 | namespace base { |
Daniel Cheng | 117c6a9 | 2022-10-10 23:47:28 | [diff] [blame] | 19 | template <typename T> |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame] | 20 | class NoDestructor; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 21 | } // namespace base |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 22 | |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 23 | namespace ui { |
dmazzoni | 1efe879 | 2015-08-07 01:02:15 | [diff] [blame] | 24 | |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 25 | class AXActionHandlerBase; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 26 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 27 | // An observer is informed of all automation actions. |
| 28 | class AXActionHandlerObserver : public base::CheckedObserver { |
| 29 | public: |
| 30 | // This method is intended to route actions to their final destinations. The |
| 31 | // routing is asynchronous and we do not know which observers intend to |
| 32 | // respond to which actions -- so we forward all actions to all observers. |
| 33 | // Only the observer that owns the unique |tree_id| will perform the action. |
Jacques Newman | a4149e85 | 2024-08-28 23:35:42 | [diff] [blame] | 34 | virtual void PerformAction(const AXActionData& action_data) {} |
David Tseng | 559a04c | 2022-10-18 18:15:20 | [diff] [blame] | 35 | |
| 36 | // Informs the observer that a tree has been removed. |
| 37 | virtual void TreeRemoved(AXTreeID tree_id) {} |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 38 | }; |
| 39 | |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 40 | // This class generates and saves a runtime id for an accessibility tree. |
| 41 | // It provides a few distinct forms of generating an id: |
| 42 | // - from a frame id (which consists of a process and routing id) |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 43 | // - from a backing |AXActionHandlerBase| object |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 44 | // |
| 45 | // The first form allows underlying instances to change but refer to the same |
| 46 | // frame. |
| 47 | // The second form allows this registry to track the object for later retrieval. |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame] | 48 | class AX_BASE_EXPORT AXActionHandlerRegistry final { |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 49 | public: |
thestig | cf9519fa | 2016-08-30 05:50:54 | [diff] [blame] | 50 | using FrameID = std::pair<int, int>; |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 51 | |
| 52 | // Get the single instance of this class. |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 53 | static AXActionHandlerRegistry* GetInstance(); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 54 | |
Greg Thompson | d290842 | 2025-03-12 21:52:54 | [diff] [blame] | 55 | ~AXActionHandlerRegistry(); |
Peter Boström | 03d2702 | 2021-09-27 19:45:39 | [diff] [blame] | 56 | AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete; |
| 57 | AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete; |
| 58 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 59 | // Gets the frame id based on an ax tree id. |
| 60 | FrameID GetFrameID(const AXTreeID& ax_tree_id); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 61 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 62 | // Gets an ax tree id from a frame id. |
| 63 | AXTreeID GetAXTreeID(FrameID frame_id); |
| 64 | |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 65 | // Retrieve an |AXActionHandlerBase| based on an ax tree id. |
| 66 | AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id); |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 67 | |
Nektarios Paisios | 6ba339a6 | 2024-05-07 23:37:02 | [diff] [blame] | 68 | // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly. |
| 69 | void SetAXTreeID(const AXTreeID& ax_tree_id, |
| 70 | AXActionHandlerBase* action_handler); |
| 71 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 72 | // Removes an ax tree id, and its associated delegate and frame id (if it |
| 73 | // exists). |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 74 | void RemoveAXTreeID(AXTreeID ax_tree_id); |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 75 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 76 | // Associate a frame id with an ax tree id. |
| 77 | void SetFrameIDForAXTreeID(const FrameID& frame_id, |
| 78 | const AXTreeID& ax_tree_id); |
| 79 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 80 | void AddObserver(AXActionHandlerObserver* observer); |
| 81 | void RemoveObserver(AXActionHandlerObserver* observer); |
| 82 | |
| 83 | // Calls PerformAction on all observers. |
Jacques Newman | a4149e85 | 2024-08-28 23:35:42 | [diff] [blame] | 84 | void PerformAction(const AXActionData& action_data); |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 85 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 86 | private: |
Daniel Cheng | 117c6a9 | 2022-10-10 23:47:28 | [diff] [blame] | 87 | friend base::NoDestructor<AXActionHandlerRegistry>; |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame] | 88 | |
| 89 | // Allows registration of tree ids meant to be internally by AXActionHandler*. |
| 90 | // These typically involve the creation of a new tree id. |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 91 | friend AXActionHandler; |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 92 | friend AXActionHandlerBase; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 93 | |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame] | 94 | AXActionHandlerRegistry(); |
| 95 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 96 | // Get or create a ax tree id keyed on |handler|. |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 97 | AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler); |
| 98 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 99 | // Maps an accessibility tree to its frame via ids. |
| 100 | std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_; |
| 101 | |
| 102 | // Maps frames to an accessibility tree via ids. |
| 103 | std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_; |
| 104 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 105 | // Maps an id to its handler. |
Ali Hijazi | 60a72b0a | 2024-09-30 17:58:53 | [diff] [blame] | 106 | std::map<AXTreeID, raw_ptr<AXActionHandlerBase, CtnExperimental>> |
| 107 | id_to_action_handler_; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 108 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 109 | // Tracks all observers. |
| 110 | base::ObserverList<AXActionHandlerObserver> observers_; |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 111 | }; |
| 112 | |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 113 | } // namespace ui |
dmazzoni | 1efe879 | 2015-08-07 01:02:15 | [diff] [blame] | 114 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 115 | #endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_ |