dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 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 | |
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 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 12 | #include "base/macros.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 { |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame^] | 19 | template <typename T, typename O> |
| 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. |
David Tseng | 6d5814b8 | 2021-05-22 17:10:44 | [diff] [blame] | 34 | virtual void PerformAction(const ui::AXActionData& action_data) = 0; |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 35 | }; |
| 36 | |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 37 | // This class generates and saves a runtime id for an accessibility tree. |
| 38 | // It provides a few distinct forms of generating an id: |
| 39 | // - from a frame id (which consists of a process and routing id) |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 40 | // - from a backing |AXActionHandlerBase| object |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 41 | // |
| 42 | // The first form allows underlying instances to change but refer to the same |
| 43 | // frame. |
| 44 | // The second form allows this registry to track the object for later retrieval. |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame^] | 45 | class AX_BASE_EXPORT AXActionHandlerRegistry final { |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 46 | public: |
thestig | cf9519fa | 2016-08-30 05:50:54 | [diff] [blame] | 47 | using FrameID = std::pair<int, int>; |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 48 | |
| 49 | // Get the single instance of this class. |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 50 | static AXActionHandlerRegistry* GetInstance(); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 51 | |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame^] | 52 | virtual ~AXActionHandlerRegistry(); |
Peter Boström | 03d2702 | 2021-09-27 19:45:39 | [diff] [blame] | 53 | AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete; |
| 54 | AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete; |
| 55 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 56 | // Gets the frame id based on an ax tree id. |
| 57 | FrameID GetFrameID(const AXTreeID& ax_tree_id); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 58 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 59 | // Gets an ax tree id from a frame id. |
| 60 | AXTreeID GetAXTreeID(FrameID frame_id); |
| 61 | |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 62 | // Retrieve an |AXActionHandlerBase| based on an ax tree id. |
| 63 | AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id); |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 64 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 65 | // Removes an ax tree id, and its associated delegate and frame id (if it |
| 66 | // exists). |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 67 | void RemoveAXTreeID(AXTreeID ax_tree_id); |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 68 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 69 | // Associate a frame id with an ax tree id. |
| 70 | void SetFrameIDForAXTreeID(const FrameID& frame_id, |
| 71 | const AXTreeID& ax_tree_id); |
| 72 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 73 | void AddObserver(AXActionHandlerObserver* observer); |
| 74 | void RemoveObserver(AXActionHandlerObserver* observer); |
| 75 | |
| 76 | // Calls PerformAction on all observers. |
David Tseng | 6d5814b8 | 2021-05-22 17:10:44 | [diff] [blame] | 77 | void PerformAction(const ui::AXActionData& action_data); |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 78 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 79 | private: |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame^] | 80 | friend base::NoDestructor<AXActionHandlerRegistry, std::nullptr_t>; |
| 81 | |
| 82 | // Allows registration of tree ids meant to be internally by AXActionHandler*. |
| 83 | // These typically involve the creation of a new tree id. |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 84 | friend AXActionHandler; |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 85 | friend AXActionHandlerBase; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 86 | |
David Tseng | d3516116 | 2021-11-06 15:34:43 | [diff] [blame^] | 87 | AXActionHandlerRegistry(); |
| 88 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 89 | // Get or create a ax tree id keyed on |handler|. |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 90 | AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler); |
| 91 | |
| 92 | // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly. |
| 93 | void SetAXTreeID(const AXTreeID& ax_tree_id, |
| 94 | AXActionHandlerBase* action_handler); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 95 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 96 | // Maps an accessibility tree to its frame via ids. |
| 97 | std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_; |
| 98 | |
| 99 | // Maps frames to an accessibility tree via ids. |
| 100 | std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_; |
| 101 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 102 | // Maps an id to its handler. |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 103 | std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 104 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 105 | // Tracks all observers. |
| 106 | base::ObserverList<AXActionHandlerObserver> observers_; |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 107 | }; |
| 108 | |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 109 | } // namespace ui |
dmazzoni | 1efe879 | 2015-08-07 01:02:15 | [diff] [blame] | 110 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 111 | #endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_ |