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> |
Dominic Mazzoni | 336bc006 | 2018-09-23 16:46:43 | [diff] [blame] | 10 | #include <string> |
thestig | cf9519fa | 2016-08-30 05:50:54 | [diff] [blame] | 11 | #include <utility> |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 12 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 13 | #include "base/macros.h" |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 14 | #include "base/observer_list.h" |
| 15 | #include "base/values.h" |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 16 | #include "ui/accessibility/ax_action_handler.h" |
Dominic Mazzoni | c39830f | 2021-02-02 05:58:26 | [diff] [blame] | 17 | #include "ui/accessibility/ax_base_export.h" |
Dominic Mazzoni | 336bc006 | 2018-09-23 16:46:43 | [diff] [blame] | 18 | #include "ui/accessibility/ax_tree_id.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 19 | |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 20 | namespace base { |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 21 | template <typename T> |
| 22 | struct DefaultSingletonTraits; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 23 | } // namespace base |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 24 | |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 25 | namespace ui { |
dmazzoni | 1efe879 | 2015-08-07 01:02:15 | [diff] [blame] | 26 | |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 27 | class AXActionHandlerBase; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 28 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 29 | // An observer is informed of all automation actions. |
| 30 | class AXActionHandlerObserver : public base::CheckedObserver { |
| 31 | public: |
| 32 | // This method is intended to route actions to their final destinations. The |
| 33 | // routing is asynchronous and we do not know which observers intend to |
| 34 | // respond to which actions -- so we forward all actions to all observers. |
| 35 | // Only the observer that owns the unique |tree_id| will perform the action. |
David Tseng | 6d5814b8 | 2021-05-22 17:10:44 | [diff] [blame^] | 36 | virtual void PerformAction(const ui::AXActionData& action_data) = 0; |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 37 | }; |
| 38 | |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 39 | // This class generates and saves a runtime id for an accessibility tree. |
| 40 | // It provides a few distinct forms of generating an id: |
| 41 | // - from a frame id (which consists of a process and routing id) |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 42 | // - from a backing |AXActionHandlerBase| object |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 43 | // |
| 44 | // The first form allows underlying instances to change but refer to the same |
| 45 | // frame. |
| 46 | // The second form allows this registry to track the object for later retrieval. |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 47 | class AX_BASE_EXPORT AXActionHandlerRegistry { |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 48 | public: |
thestig | cf9519fa | 2016-08-30 05:50:54 | [diff] [blame] | 49 | using FrameID = std::pair<int, int>; |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 50 | |
| 51 | // Get the single instance of this class. |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 52 | static AXActionHandlerRegistry* GetInstance(); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 53 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 54 | // Gets the frame id based on an ax tree id. |
| 55 | FrameID GetFrameID(const AXTreeID& ax_tree_id); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 56 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 57 | // Gets an ax tree id from a frame id. |
| 58 | AXTreeID GetAXTreeID(FrameID frame_id); |
| 59 | |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 60 | // Retrieve an |AXActionHandlerBase| based on an ax tree id. |
| 61 | AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id); |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 62 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 63 | // Removes an ax tree id, and its associated delegate and frame id (if it |
| 64 | // exists). |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 65 | void RemoveAXTreeID(AXTreeID ax_tree_id); |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 66 | |
David Tseng | efd9d9c | 2018-12-12 16:23:00 | [diff] [blame] | 67 | // Associate a frame id with an ax tree id. |
| 68 | void SetFrameIDForAXTreeID(const FrameID& frame_id, |
| 69 | const AXTreeID& ax_tree_id); |
| 70 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 71 | void AddObserver(AXActionHandlerObserver* observer); |
| 72 | void RemoveObserver(AXActionHandlerObserver* observer); |
| 73 | |
| 74 | // Calls PerformAction on all observers. |
David Tseng | 6d5814b8 | 2021-05-22 17:10:44 | [diff] [blame^] | 75 | void PerformAction(const ui::AXActionData& action_data); |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 76 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 77 | private: |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 78 | friend struct base::DefaultSingletonTraits<AXActionHandlerRegistry>; |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 79 | friend AXActionHandler; |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 80 | friend AXActionHandlerBase; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 81 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 82 | // Get or create a ax tree id keyed on |handler|. |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 83 | AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler); |
| 84 | |
| 85 | // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly. |
| 86 | void SetAXTreeID(const AXTreeID& ax_tree_id, |
| 87 | AXActionHandlerBase* action_handler); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 88 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 89 | AXActionHandlerRegistry(); |
| 90 | virtual ~AXActionHandlerRegistry(); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 91 | |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 92 | // Maps an accessibility tree to its frame via ids. |
| 93 | std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_; |
| 94 | |
| 95 | // Maps frames to an accessibility tree via ids. |
| 96 | std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_; |
| 97 | |
James Cook | ae08626 | 2018-12-15 00:28:48 | [diff] [blame] | 98 | // Maps an id to its handler. |
Mario Sanchez Prada | 1b559ffe | 2020-07-14 12:58:30 | [diff] [blame] | 99 | std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_; |
dtseng | 32ea1736 | 2017-02-25 00:52:27 | [diff] [blame] | 100 | |
Erik Chen | 280765ec | 2021-03-18 23:24:02 | [diff] [blame] | 101 | // Tracks all observers. |
| 102 | base::ObserverList<AXActionHandlerObserver> observers_; |
| 103 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(AXActionHandlerRegistry); |
dtseng | ad8ae0f | 2014-11-04 19:56:24 | [diff] [blame] | 105 | }; |
| 106 | |
dtseng | be42343 | 2017-02-22 14:05:43 | [diff] [blame] | 107 | } // namespace ui |
dmazzoni | 1efe879 | 2015-08-07 01:02:15 | [diff] [blame] | 108 | |
Erik Chen | 2ccece5 | 2021-03-18 01:59:15 | [diff] [blame] | 109 | #endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_ |