ax_action_handler_registry.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_
  5. #define UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include <utility>
  9. #include "base/observer_list.h"
  10. #include "ui/accessibility/ax_action_handler.h"
  11. #include "ui/accessibility/ax_base_export.h"
  12. #include "ui/accessibility/ax_tree_id.h"
  13. namespace base {
  14. template <typename T, typename O>
  15. class NoDestructor;
  16. } // namespace base
  17. namespace ui {
  18. class AXActionHandlerBase;
  19. // An observer is informed of all automation actions.
  20. class AXActionHandlerObserver : public base::CheckedObserver {
  21. public:
  22. // This method is intended to route actions to their final destinations. The
  23. // routing is asynchronous and we do not know which observers intend to
  24. // respond to which actions -- so we forward all actions to all observers.
  25. // Only the observer that owns the unique |tree_id| will perform the action.
  26. virtual void PerformAction(const ui::AXActionData& action_data) = 0;
  27. };
  28. // This class generates and saves a runtime id for an accessibility tree.
  29. // It provides a few distinct forms of generating an id:
  30. // - from a frame id (which consists of a process and routing id)
  31. // - from a backing |AXActionHandlerBase| object
  32. //
  33. // The first form allows underlying instances to change but refer to the same
  34. // frame.
  35. // The second form allows this registry to track the object for later retrieval.
  36. class AX_BASE_EXPORT AXActionHandlerRegistry final {
  37. public:
  38. using FrameID = std::pair<int, int>;
  39. // Get the single instance of this class.
  40. static AXActionHandlerRegistry* GetInstance();
  41. virtual ~AXActionHandlerRegistry();
  42. AXActionHandlerRegistry(const AXActionHandlerRegistry&) = delete;
  43. AXActionHandlerRegistry& operator=(const AXActionHandlerRegistry&) = delete;
  44. // Gets the frame id based on an ax tree id.
  45. FrameID GetFrameID(const AXTreeID& ax_tree_id);
  46. // Gets an ax tree id from a frame id.
  47. AXTreeID GetAXTreeID(FrameID frame_id);
  48. // Retrieve an |AXActionHandlerBase| based on an ax tree id.
  49. AXActionHandlerBase* GetActionHandler(AXTreeID ax_tree_id);
  50. // Removes an ax tree id, and its associated delegate and frame id (if it
  51. // exists).
  52. void RemoveAXTreeID(AXTreeID ax_tree_id);
  53. // Associate a frame id with an ax tree id.
  54. void SetFrameIDForAXTreeID(const FrameID& frame_id,
  55. const AXTreeID& ax_tree_id);
  56. void AddObserver(AXActionHandlerObserver* observer);
  57. void RemoveObserver(AXActionHandlerObserver* observer);
  58. // Calls PerformAction on all observers.
  59. void PerformAction(const ui::AXActionData& action_data);
  60. private:
  61. friend base::NoDestructor<AXActionHandlerRegistry, std::nullptr_t>;
  62. // Allows registration of tree ids meant to be internally by AXActionHandler*.
  63. // These typically involve the creation of a new tree id.
  64. friend AXActionHandler;
  65. friend AXActionHandlerBase;
  66. AXActionHandlerRegistry();
  67. // Get or create a ax tree id keyed on |handler|.
  68. AXTreeID GetOrCreateAXTreeID(AXActionHandlerBase* handler);
  69. // Set a mapping between an AXTreeID and AXActionHandlerBase explicitly.
  70. void SetAXTreeID(const AXTreeID& ax_tree_id,
  71. AXActionHandlerBase* action_handler);
  72. // Maps an accessibility tree to its frame via ids.
  73. std::map<AXTreeID, FrameID> ax_tree_to_frame_id_map_;
  74. // Maps frames to an accessibility tree via ids.
  75. std::map<FrameID, AXTreeID> frame_to_ax_tree_id_map_;
  76. // Maps an id to its handler.
  77. std::map<AXTreeID, AXActionHandlerBase*> id_to_action_handler_;
  78. // Tracks all observers.
  79. base::ObserverList<AXActionHandlerObserver> observers_;
  80. };
  81. } // namespace ui
  82. #endif // UI_ACCESSIBILITY_AX_ACTION_HANDLER_REGISTRY_H_