summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/ax_action_handler_registry.h
blob: 3bc2dd0e8b14acdb28fb355eedc2bcb8aebe4e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_
#define UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_

#include <cstdint>
#include <map>
#include <utility>

#include "base/observer_list.h"
#include "ui/accessibility/ax_action_handler.h"
#include "ui/accessibility/ax_base_export.h"
#include "ui/accessibility/ax_tree_id.h"

namespace base {
template <typename T>
class NoDestructor;
}  // namespace base

namespace ui {

class AXActionHandlerBase;

// An observer is informed of all automation actions.
class AXActionHandlerObserver : public base::CheckedObserver {
 public:
  // This method is intended to route actions to their final destinations. The
  // routing is asynchronous and we do not know which observers intend to
  // respond to which actions -- so we forward all actions to all observers.
  // Only the observer that owns the unique |tree_id| will perform the action.
  virtual void PerformAction(const ui::AXActionData& action_data) = 0;
};

// This class generates and saves a runtime id for an accessibility tree.
// It provides a few distinct forms of generating an id:
//     - from a frame id (which consists of a process and routing id)
//     - from a backing |AXActionHandlerBase| object
//
// The first form allows underlying instances to change but refer to the same
// frame.
// The second form allows this registry to track the object for later retrieval.
class AX_BASE_EXPORT AXActionHandlerRegistry final {
 public:
  using FrameID = std::pair<int, int>;

  // Get the single instance of this class.
  static AXActionHandlerRegistry* GetInstance();

  virtual ~AXActionHandlerRegistry();
  AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete;
  AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete;

  // Gets the frame id based on an ax tree id.
  FrameID GetFrameID(const AXTreeID& ax_tree_id);

  // Gets an ax tree id from a frame id.
  AXTreeID GetAXTreeID(FrameID frame_id);

  // Retrieve an |AXActionHandlerBase| based on an ax tree id.
  AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id);

  // Removes an ax tree id, and its associated delegate and frame id (if it
  // exists).
  void RemoveAXTreeID(AXTreeID ax_tree_id);

  // Associate a frame id with an ax tree id.
  void SetFrameIDForAXTreeID(const FrameID& frame_id,
                             const AXTreeID& ax_tree_id);

  void AddObserver(AXActionHandlerObserver* observer);
  void RemoveObserver(AXActionHandlerObserver* observer);

  // Calls PerformAction on all observers.
  void PerformAction(const ui::AXActionData& action_data);

 private:
  friend base::NoDestructor<AXActionHandlerRegistry>;

  // Allows registration of tree ids meant to be internally by AXActionHandler*.
  // These typically involve the creation of a new tree id.
  friend AXActionHandler;
  friend AXActionHandlerBase;

  AXActionHandlerRegistry();

  // Get or create a ax tree id keyed on |handler|.
  AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler);

  // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly.
  void SetAXTreeID(const AXTreeID& ax_tree_id,
                   AXActionHandlerBase* action_handler);

  // Maps an accessibility tree to its frame via ids.
  std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_;

  // Maps frames to an accessibility tree via ids.
  std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_;

  // Maps an id to its handler.
  std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_;

  // Tracks all observers.
  base::ObserverList<AXActionHandlerObserver> observers_;
};

}  // namespace ui

#endif  // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_