ax_tree_converter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2019 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 FUCHSIA_WEB_WEBENGINE_BROWSER_AX_TREE_CONVERTER_H_
  5. #define FUCHSIA_WEB_WEBENGINE_BROWSER_AX_TREE_CONVERTER_H_
  6. #include <fuchsia/accessibility/semantics/cpp/fidl.h>
  7. #include <unordered_map>
  8. #include "base/containers/flat_map.h"
  9. #include "content/public/browser/ax_event_notification_details.h"
  10. #include "fuchsia_web/webengine/web_engine_export.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "ui/accessibility/ax_node.h"
  13. // Maps AXNode IDs to Fuchsia Node IDs.
  14. // This class saves the remapped values.
  15. class WEB_ENGINE_EXPORT NodeIDMapper {
  16. public:
  17. NodeIDMapper();
  18. virtual ~NodeIDMapper();
  19. // Maps |ax_tree_id| and the signed |ax_node_id| to a unique unsigned
  20. // |fuchsia_node_id|, with special handling of root IDs. A Fuchsia Node ID of
  21. // 0 indicates the root, and is always returned if |is_tree_root| is true.
  22. // No value is returned if this mapper can't produce more unique IDs.
  23. virtual uint32_t ToFuchsiaNodeID(const ui::AXTreeID& ax_tree_id,
  24. int32_t ax_node_id,
  25. bool is_tree_root);
  26. // From a Fuchsia Node ID, returns the pair of the AXTreeID and the AXNode ID
  27. // that maps to it. If the Fuchsia Node ID is not in the map, returns no
  28. // value.
  29. virtual absl::optional<std::pair<ui::AXTreeID, int32_t>> ToAXNodeID(
  30. uint32_t fuchsia_node_id);
  31. // Updates the AXNode IDs to point to the new |ax_tree_id|. This method
  32. // must be called whenever an AXTree changes its AXTreeID, so that stored
  33. // values here will point to the correct tree.
  34. // Returns true if the update was applied successfully.
  35. virtual bool UpdateAXTreeIDForCachedNodeIDs(
  36. const ui::AXTreeID& old_ax_tree_id,
  37. const ui::AXTreeID& new_ax_tree_id);
  38. private:
  39. // Keeps track of the next unused, unique node ID.
  40. uint32_t next_fuchsia_id_ = 1;
  41. // Pair that represents the current root.
  42. std::pair<ui::AXTreeID, int32_t> root_;
  43. // Stores the node ID mappings. Note that because converting from AXNode IDs
  44. // to Fuchsia Node IDs is more common, the map makes access in this
  45. // direction O(1). The storage representation is chosen here to use a map to
  46. // hold the AXTreeIDs (which are just a few), but an unordered_map to hold the
  47. // IDs (which can be thousands).
  48. base::flat_map<ui::AXTreeID, std::unordered_map<int32_t, uint32_t>> id_map_;
  49. };
  50. // Converts an AXNode to a Fuchsia Semantic Node.
  51. // Both data types represent a single node, and no additional state is needed.
  52. // AXNode is used to convey partial updates, so not all fields may be
  53. // present. Those that are will be converted. The Fuchsia SemanticsManager
  54. // accepts partial updates, so |node| does not require all fields to be set.
  55. WEB_ENGINE_EXPORT fuchsia::accessibility::semantics::Node
  56. AXNodeDataToSemanticNode(const ui::AXNode& ax_node,
  57. const ui::AXNode& container_node,
  58. const ui::AXTreeID& tree_id,
  59. bool is_root,
  60. float scale_factor,
  61. NodeIDMapper* id_mapper);
  62. // Converts Fuchsia action of type |fuchsia_action| to an ax::mojom::Action of
  63. // type |mojom_action|. Function will return true if |fuchsia_action| is
  64. // supported in Chromium.
  65. bool ConvertAction(fuchsia::accessibility::semantics::Action fuchsia_action,
  66. ax::mojom::Action* mojom_action);
  67. #endif // FUCHSIA_WEB_WEBENGINE_BROWSER_AX_TREE_CONVERTER_H_