blob: 8b53037438e63a69883d73f61588b2e8468e498b [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>
Dominic Mazzoni336bc0062018-09-23 16:46:4310#include <string>
thestigcf9519fa2016-08-30 05:50:5411#include <utility>
dtsengad8ae0f2014-11-04 19:56:2412
avib7348942015-12-25 20:57:1013#include "base/macros.h"
Erik Chen280765ec2021-03-18 23:24:0214#include "base/observer_list.h"
15#include "base/values.h"
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3016#include "ui/accessibility/ax_action_handler.h"
Dominic Mazzonic39830f2021-02-02 05:58:2617#include "ui/accessibility/ax_base_export.h"
Dominic Mazzoni336bc0062018-09-23 16:46:4318#include "ui/accessibility/ax_tree_id.h"
avib7348942015-12-25 20:57:1019
olli.raula36aa8be2015-09-10 11:14:2220namespace base {
dtsengad8ae0f2014-11-04 19:56:2421template <typename T>
22struct DefaultSingletonTraits;
olli.raula36aa8be2015-09-10 11:14:2223} // namespace base
dtsengad8ae0f2014-11-04 19:56:2424
dtsengbe423432017-02-22 14:05:4325namespace ui {
dmazzoni1efe8792015-08-07 01:02:1526
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3027class AXActionHandlerBase;
dtseng32ea17362017-02-25 00:52:2728
Erik Chen280765ec2021-03-18 23:24:0229// An observer is informed of all automation actions.
30class 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 Tseng6d5814b82021-05-22 17:10:4436 virtual void PerformAction(const ui::AXActionData& action_data) = 0;
Erik Chen280765ec2021-03-18 23:24:0237};
38
dtseng32ea17362017-02-25 00:52:2739// 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 Prada1b559ffe2020-07-14 12:58:3042// - from a backing |AXActionHandlerBase| object
dtseng32ea17362017-02-25 00:52:2743//
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 Chen2ccece52021-03-18 01:59:1547class AX_BASE_EXPORT AXActionHandlerRegistry {
dtsengad8ae0f2014-11-04 19:56:2448 public:
thestigcf9519fa2016-08-30 05:50:5449 using FrameID = std::pair<int, int>;
dtsengad8ae0f2014-11-04 19:56:2450
51 // Get the single instance of this class.
Erik Chen2ccece52021-03-18 01:59:1552 static AXActionHandlerRegistry* GetInstance();
dtsengad8ae0f2014-11-04 19:56:2453
David Tsengefd9d9c2018-12-12 16:23:0054 // Gets the frame id based on an ax tree id.
55 FrameID GetFrameID(const AXTreeID& ax_tree_id);
dtsengad8ae0f2014-11-04 19:56:2456
David Tsengefd9d9c2018-12-12 16:23:0057 // Gets an ax tree id from a frame id.
58 AXTreeID GetAXTreeID(FrameID frame_id);
59
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3060 // Retrieve an |AXActionHandlerBase| based on an ax tree id.
61 AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id);
dtseng32ea17362017-02-25 00:52:2762
David Tsengefd9d9c2018-12-12 16:23:0063 // Removes an ax tree id, and its associated delegate and frame id (if it
64 // exists).
dtseng32ea17362017-02-25 00:52:2765 void RemoveAXTreeID(AXTreeID ax_tree_id);
dtsengbe423432017-02-22 14:05:4366
David Tsengefd9d9c2018-12-12 16:23:0067 // Associate a frame id with an ax tree id.
68 void SetFrameIDForAXTreeID(const FrameID& frame_id,
69 const AXTreeID& ax_tree_id);
70
Erik Chen280765ec2021-03-18 23:24:0271 void AddObserver(AXActionHandlerObserver* observer);
72 void RemoveObserver(AXActionHandlerObserver* observer);
73
74 // Calls PerformAction on all observers.
David Tseng6d5814b82021-05-22 17:10:4475 void PerformAction(const ui::AXActionData& action_data);
Erik Chen280765ec2021-03-18 23:24:0276
dtsengad8ae0f2014-11-04 19:56:2477 private:
Erik Chen2ccece52021-03-18 01:59:1578 friend struct base::DefaultSingletonTraits<AXActionHandlerRegistry>;
James Cookae086262018-12-15 00:28:4879 friend AXActionHandler;
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3080 friend AXActionHandlerBase;
dtseng32ea17362017-02-25 00:52:2781
James Cookae086262018-12-15 00:28:4882 // Get or create a ax tree id keyed on |handler|.
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3083 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);
dtsengad8ae0f2014-11-04 19:56:2488
Erik Chen2ccece52021-03-18 01:59:1589 AXActionHandlerRegistry();
90 virtual ~AXActionHandlerRegistry();
dtsengad8ae0f2014-11-04 19:56:2491
dtsengad8ae0f2014-11-04 19:56:2492 // 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 Cookae086262018-12-15 00:28:4898 // Maps an id to its handler.
Mario Sanchez Prada1b559ffe2020-07-14 12:58:3099 std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_;
dtseng32ea17362017-02-25 00:52:27100
Erik Chen280765ec2021-03-18 23:24:02101 // Tracks all observers.
102 base::ObserverList<AXActionHandlerObserver> observers_;
103
Erik Chen2ccece52021-03-18 01:59:15104 DISALLOW_COPY_AND_ASSIGN(AXActionHandlerRegistry);
dtsengad8ae0f2014-11-04 19:56:24105};
106
dtsengbe423432017-02-22 14:05:43107} // namespace ui
dmazzoni1efe8792015-08-07 01:02:15108
Erik Chen2ccece52021-03-18 01:59:15109#endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_