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