blob: 05cc1b36f899054e12a91f0396f2e74e63b62127 [file] [log] [blame]
dtsengad8ae0f2014-11-04 19:56:241// 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 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
avib7348942015-12-25 20:57:1012#include "base/macros.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 {
David Tsengd35161162021-11-06 15:34:4319template <typename T, typename O>
20class 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.
David Tseng6d5814b82021-05-22 17:10:4434 virtual void PerformAction(const ui::AXActionData& action_data) = 0;
Erik Chen280765ec2021-03-18 23:24:0235};
36
dtseng32ea17362017-02-25 00:52:2737// 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 Prada1b559ffe2020-07-14 12:58:3040// - from a backing |AXActionHandlerBase| object
dtseng32ea17362017-02-25 00:52:2741//
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 Tsengd35161162021-11-06 15:34:4345class AX_BASE_EXPORT AXActionHandlerRegistry final {
dtsengad8ae0f2014-11-04 19:56:2446 public:
thestigcf9519fa2016-08-30 05:50:5447 using FrameID = std::pair<int, int>;
dtsengad8ae0f2014-11-04 19:56:2448
49 // Get the single instance of this class.
Erik Chen2ccece52021-03-18 01:59:1550 static AXActionHandlerRegistry* GetInstance();
dtsengad8ae0f2014-11-04 19:56:2451
David Tsengd35161162021-11-06 15:34:4352 virtual ~AXActionHandlerRegistry();
Peter Boström03d27022021-09-27 19:45:3953 AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete;
54 AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete;
55
David Tsengefd9d9c2018-12-12 16:23:0056 // Gets the frame id based on an ax tree id.
57 FrameID GetFrameID(const AXTreeID& ax_tree_id);
dtsengad8ae0f2014-11-04 19:56:2458
David Tsengefd9d9c2018-12-12 16:23:0059 // Gets an ax tree id from a frame id.
60 AXTreeID GetAXTreeID(FrameID frame_id);
61
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3062 // Retrieve an |AXActionHandlerBase| based on an ax tree id.
63 AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id);
dtseng32ea17362017-02-25 00:52:2764
David Tsengefd9d9c2018-12-12 16:23:0065 // Removes an ax tree id, and its associated delegate and frame id (if it
66 // exists).
dtseng32ea17362017-02-25 00:52:2767 void RemoveAXTreeID(AXTreeID ax_tree_id);
dtsengbe423432017-02-22 14:05:4368
David Tsengefd9d9c2018-12-12 16:23:0069 // Associate a frame id with an ax tree id.
70 void SetFrameIDForAXTreeID(const FrameID& frame_id,
71 const AXTreeID& ax_tree_id);
72
Erik Chen280765ec2021-03-18 23:24:0273 void AddObserver(AXActionHandlerObserver* observer);
74 void RemoveObserver(AXActionHandlerObserver* observer);
75
76 // Calls PerformAction on all observers.
David Tseng6d5814b82021-05-22 17:10:4477 void PerformAction(const ui::AXActionData& action_data);
Erik Chen280765ec2021-03-18 23:24:0278
dtsengad8ae0f2014-11-04 19:56:2479 private:
David Tsengd35161162021-11-06 15:34:4380 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 Cookae086262018-12-15 00:28:4884 friend AXActionHandler;
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3085 friend AXActionHandlerBase;
dtseng32ea17362017-02-25 00:52:2786
David Tsengd35161162021-11-06 15:34:4387 AXActionHandlerRegistry();
88
James Cookae086262018-12-15 00:28:4889 // Get or create a ax tree id keyed on |handler|.
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3090 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);
dtsengad8ae0f2014-11-04 19:56:2495
dtsengad8ae0f2014-11-04 19:56:2496 // 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 Cookae086262018-12-15 00:28:48102 // Maps an id to its handler.
Mario Sanchez Prada1b559ffe2020-07-14 12:58:30103 std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_;
dtseng32ea17362017-02-25 00:52:27104
Erik Chen280765ec2021-03-18 23:24:02105 // Tracks all observers.
106 base::ObserverList<AXActionHandlerObserver> observers_;
dtsengad8ae0f2014-11-04 19:56:24107};
108
dtsengbe423432017-02-22 14:05:43109} // namespace ui
dmazzoni1efe8792015-08-07 01:02:15110
Erik Chen2ccece52021-03-18 01:59:15111#endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_