blob: 68ef62342a52db563cd394326faa20c59cea56f4 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2014 The Chromium Authors
dtsengad8ae0f2014-11-04 19:56:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Erik Chen2ccece52021-03-18 01:59:155#ifndef UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_
6#define UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_
dtsengad8ae0f2014-11-04 19:56:247
Erik Chen280765ec2021-03-18 23:24:028#include <cstdint>
dtsengad8ae0f2014-11-04 19:56:249#include <map>
thestigcf9519fa2016-08-30 05:50:5410#include <utility>
dtsengad8ae0f2014-11-04 19:56:2411
Ali Hijazi60a72b0a2024-09-30 17:58:5312#include "base/memory/raw_ptr.h"
Erik Chen280765ec2021-03-18 23:24:0213#include "base/observer_list.h"
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3014#include "ui/accessibility/ax_action_handler.h"
Dominic Mazzonic39830f2021-02-02 05:58:2615#include "ui/accessibility/ax_base_export.h"
Dominic Mazzoni336bc0062018-09-23 16:46:4316#include "ui/accessibility/ax_tree_id.h"
avib7348942015-12-25 20:57:1017
olli.raula36aa8be2015-09-10 11:14:2218namespace base {
Daniel Cheng117c6a92022-10-10 23:47:2819template <typename T>
David Tsengd35161162021-11-06 15:34:4320class NoDestructor;
olli.raula36aa8be2015-09-10 11:14:2221} // namespace base
dtsengad8ae0f2014-11-04 19:56:2422
dtsengbe423432017-02-22 14:05:4323namespace ui {
dmazzoni1efe8792015-08-07 01:02:1524
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3025class AXActionHandlerBase;
dtseng32ea17362017-02-25 00:52:2726
Erik Chen280765ec2021-03-18 23:24:0227// An observer is informed of all automation actions.
28class 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 Newmana4149e852024-08-28 23:35:4234 virtual void PerformAction(const AXActionData& action_data) {}
David Tseng559a04c2022-10-18 18:15:2035
36 // Informs the observer that a tree has been removed.
37 virtual void TreeRemoved(AXTreeID tree_id) {}
Erik Chen280765ec2021-03-18 23:24:0238};
39
dtseng32ea17362017-02-25 00:52:2740// 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 Prada1b559ffe2020-07-14 12:58:3043// - from a backing |AXActionHandlerBase| object
dtseng32ea17362017-02-25 00:52:2744//
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 Tsengd35161162021-11-06 15:34:4348class AX_BASE_EXPORT AXActionHandlerRegistry final {
dtsengad8ae0f2014-11-04 19:56:2449 public:
thestigcf9519fa2016-08-30 05:50:5450 using FrameID = std::pair<int, int>;
dtsengad8ae0f2014-11-04 19:56:2451
52 // Get the single instance of this class.
Erik Chen2ccece52021-03-18 01:59:1553 static AXActionHandlerRegistry* GetInstance();
dtsengad8ae0f2014-11-04 19:56:2454
Greg Thompsond2908422025-03-12 21:52:5455 ~AXActionHandlerRegistry();
Peter Boström03d27022021-09-27 19:45:3956 AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete;
57 AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete;
58
David Tsengefd9d9c2018-12-12 16:23:0059 // Gets the frame id based on an ax tree id.
60 FrameID GetFrameID(const AXTreeID& ax_tree_id);
dtsengad8ae0f2014-11-04 19:56:2461
David Tsengefd9d9c2018-12-12 16:23:0062 // Gets an ax tree id from a frame id.
63 AXTreeID GetAXTreeID(FrameID frame_id);
64
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3065 // Retrieve an |AXActionHandlerBase| based on an ax tree id.
66 AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id);
dtseng32ea17362017-02-25 00:52:2767
Nektarios Paisios6ba339a62024-05-07 23:37:0268 // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly.
69 void SetAXTreeID(const AXTreeID& ax_tree_id,
70 AXActionHandlerBase* action_handler);
71
David Tsengefd9d9c2018-12-12 16:23:0072 // Removes an ax tree id, and its associated delegate and frame id (if it
73 // exists).
dtseng32ea17362017-02-25 00:52:2774 void RemoveAXTreeID(AXTreeID ax_tree_id);
dtsengbe423432017-02-22 14:05:4375
David Tsengefd9d9c2018-12-12 16:23:0076 // Associate a frame id with an ax tree id.
77 void SetFrameIDForAXTreeID(const FrameID& frame_id,
78 const AXTreeID& ax_tree_id);
79
Erik Chen280765ec2021-03-18 23:24:0280 void AddObserver(AXActionHandlerObserver* observer);
81 void RemoveObserver(AXActionHandlerObserver* observer);
82
83 // Calls PerformAction on all observers.
Jacques Newmana4149e852024-08-28 23:35:4284 void PerformAction(const AXActionData& action_data);
Erik Chen280765ec2021-03-18 23:24:0285
dtsengad8ae0f2014-11-04 19:56:2486 private:
Daniel Cheng117c6a92022-10-10 23:47:2887 friend base::NoDestructor<AXActionHandlerRegistry>;
David Tsengd35161162021-11-06 15:34:4388
89 // Allows registration of tree ids meant to be internally by AXActionHandler*.
90 // These typically involve the creation of a new tree id.
James Cookae086262018-12-15 00:28:4891 friend AXActionHandler;
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3092 friend AXActionHandlerBase;
dtseng32ea17362017-02-25 00:52:2793
David Tsengd35161162021-11-06 15:34:4394 AXActionHandlerRegistry();
95
James Cookae086262018-12-15 00:28:4896 // Get or create a ax tree id keyed on |handler|.
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3097 AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler);
98
dtsengad8ae0f2014-11-04 19:56:2499 // 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 Cookae086262018-12-15 00:28:48105 // Maps an id to its handler.
Ali Hijazi60a72b0a2024-09-30 17:58:53106 std::map<AXTreeID, raw_ptr<AXActionHandlerBase, CtnExperimental>>
107 id_to_action_handler_;
dtseng32ea17362017-02-25 00:52:27108
Erik Chen280765ec2021-03-18 23:24:02109 // Tracks all observers.
110 base::ObserverList<AXActionHandlerObserver> observers_;
dtsengad8ae0f2014-11-04 19:56:24111};
112
dtsengbe423432017-02-22 14:05:43113} // namespace ui
dmazzoni1efe8792015-08-07 01:02:15114
Erik Chen2ccece52021-03-18 01:59:15115#endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_