diff options
Diffstat (limited to 'chromium/ui/accessibility/ax_tree.h')
-rw-r--r-- | chromium/ui/accessibility/ax_tree.h | 195 |
1 files changed, 58 insertions, 137 deletions
diff --git a/chromium/ui/accessibility/ax_tree.h b/chromium/ui/accessibility/ax_tree.h index fb8f2b74d68..66be06f7937 100644 --- a/chromium/ui/accessibility/ax_tree.h +++ b/chromium/ui/accessibility/ax_tree.h @@ -8,8 +8,9 @@ #include <stdint.h> #include <set> +#include <unordered_map> -#include "base/containers/hash_tables.h" +#include "base/observer_list.h" #include "ui/accessibility/ax_export.h" #include "ui/accessibility/ax_node.h" #include "ui/accessibility/ax_node_data.h" @@ -20,138 +21,9 @@ namespace ui { class AXTableInfo; class AXTree; +class AXTreeObserver; struct AXTreeUpdateState; -// Used when you want to be notified when changes happen to the tree. -// -// Some of the notifications are called in the middle of an update operation. -// Be careful, as the tree may be in an inconsistent state at this time; -// don't walk the parents and children at this time: -// OnNodeWillBeDeleted -// OnSubtreeWillBeDeleted -// OnNodeWillBeReparented -// OnSubtreeWillBeReparented -// OnNodeCreated -// OnNodeReparented -// OnNodeChanged -// -// In addition, one additional notification is fired at the end of an -// atomic update, and it provides a vector of nodes that were added or -// changed, for final postprocessing: -// OnAtomicUpdateFinished -// -class AX_EXPORT AXTreeDelegate { - public: - AXTreeDelegate(); - virtual ~AXTreeDelegate(); - - // Called before a node's data gets updated. - virtual void OnNodeDataWillChange(AXTree* tree, - const AXNodeData& old_node_data, - const AXNodeData& new_node_data) = 0; - - // Individual callbacks for every attribute of AXNodeData that can change. - virtual void OnRoleChanged(AXTree* tree, - AXNode* node, - ax::mojom::Role old_role, - ax::mojom::Role new_role) {} - virtual void OnStateChanged(AXTree* tree, - AXNode* node, - ax::mojom::State state, - bool new_value) {} - virtual void OnStringAttributeChanged(AXTree* tree, - AXNode* node, - ax::mojom::StringAttribute attr, - const std::string& old_value, - const std::string& new_value) {} - virtual void OnIntAttributeChanged(AXTree* tree, - AXNode* node, - ax::mojom::IntAttribute attr, - int32_t old_value, - int32_t new_value) {} - virtual void OnFloatAttributeChanged(AXTree* tree, - AXNode* node, - ax::mojom::FloatAttribute attr, - float old_value, - float new_value) {} - virtual void OnBoolAttributeChanged(AXTree* tree, - AXNode* node, - ax::mojom::BoolAttribute attr, - bool new_value) {} - virtual void OnIntListAttributeChanged( - AXTree* tree, - AXNode* node, - ax::mojom::IntListAttribute attr, - const std::vector<int32_t>& old_value, - const std::vector<int32_t>& new_value) {} - virtual void OnStringListAttributeChanged( - AXTree* tree, - AXNode* node, - ax::mojom::StringListAttribute attr, - const std::vector<std::string>& old_value, - const std::vector<std::string>& new_value) {} - - // Called when tree data changes. - virtual void OnTreeDataChanged(AXTree* tree, - const ui::AXTreeData& old_data, - const ui::AXTreeData& new_data) = 0; - // Called just before a node is deleted. Its id and data will be valid, - // but its links to parents and children are invalid. This is called - // in the middle of an update, the tree may be in an invalid state! - virtual void OnNodeWillBeDeleted(AXTree* tree, AXNode* node) = 0; - - // Same as OnNodeWillBeDeleted, but only called once for an entire subtree. - // This is called in the middle of an update, the tree may be in an - // invalid state! - virtual void OnSubtreeWillBeDeleted(AXTree* tree, AXNode* node) = 0; - - // Called just before a node is deleted for reparenting. See - // |OnNodeWillBeDeleted| for additional information. - virtual void OnNodeWillBeReparented(AXTree* tree, AXNode* node) = 0; - - // Called just before a subtree is deleted for reparenting. See - // |OnSubtreeWillBeDeleted| for additional information. - virtual void OnSubtreeWillBeReparented(AXTree* tree, AXNode* node) = 0; - - // Called immediately after a new node is created. The tree may be in - // the middle of an update, don't walk the parents and children now. - virtual void OnNodeCreated(AXTree* tree, AXNode* node) = 0; - - // Called immediately after a node is reparented. The tree may be in the - // middle of an update, don't walk the parents and children now. - virtual void OnNodeReparented(AXTree* tree, AXNode* node) = 0; - - // Called when a node changes its data or children. The tree may be in - // the middle of an update, don't walk the parents and children now. - virtual void OnNodeChanged(AXTree* tree, AXNode* node) = 0; - - enum ChangeType { - NODE_CREATED, - SUBTREE_CREATED, - NODE_CHANGED, - NODE_REPARENTED, - SUBTREE_REPARENTED - }; - - struct Change { - Change(AXNode* node, ChangeType type) { - this->node = node; - this->type = type; - } - AXNode* node; - ChangeType type; - }; - - // Called at the end of the update operation. Every node that was added - // or changed will be included in |changes|, along with an enum indicating - // the type of change - either (1) a node was created, (2) a node was created - // and it's the root of a new subtree, or (3) a node was changed. Finally, - // a bool indicates if the root of the tree was changed or not. - virtual void OnAtomicUpdateFinished(AXTree* tree, - bool root_changed, - const std::vector<Change>& changes) = 0; -}; - // AXTree is a live, managed tree of AXNode objects that can receive // updates from another AXTreeSource via AXTreeUpdates, and it can be // used as a source for sending updates to another client tree. @@ -170,8 +42,11 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { explicit AXTree(const AXTreeUpdate& initial_state); virtual ~AXTree(); - virtual void SetDelegate(AXTreeDelegate* delegate); - AXTreeDelegate* delegate() const { return delegate_; } + void AddObserver(AXTreeObserver* observer); + bool HasObserver(AXTreeObserver* observer); + void RemoveObserver(const AXTreeObserver* observer); + + base::ObserverList<AXTreeObserver>& observers() { return observers_; } AXNode* root() const { return root_; } @@ -257,6 +132,19 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { // conflict with positive-numbered node IDs from tree sources. int32_t GetNextNegativeInternalNodeId(); + // Returns the pos_in_set of node. Looks in ordered_set_info_map_ for cached + // value. Calculates pos_in_set and set_size for node (and all other nodes in + // the same ordered set) if no value is present in the cache. + // This function is guaranteed to be only called on nodes that can hold + // pos_in_set values, minimizing the size of the cache. + int32_t GetPosInSet(const AXNode& node, const AXNode* ordered_set) override; + // Returns the set_size of node. Looks in ordered_set_info_map_ for cached + // value. Calculates pos_inset_set and set_size for node (and all other nodes + // in the same ordered set) if no value is present in the cache. + // This function is guaranteed to be only called on nodes that can hold + // set_size values, minimizing the size of the cache. + int32_t GetSetSize(const AXNode& node, const AXNode* ordered_set) override; + private: friend class AXTableInfoTest; @@ -282,7 +170,9 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { bool is_new_root, AXTreeUpdateState* update_state); - void CallNodeChangeCallbacks(AXNode* node, const AXNodeData& new_data); + void CallNodeChangeCallbacks(AXNode* node, + const AXNodeData& old_data, + const AXNodeData& new_data); void UpdateReverseRelations(AXNode* node, const AXNodeData& new_data); @@ -313,9 +203,9 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { std::vector<AXNode*>* new_children, AXTreeUpdateState* update_state); - AXTreeDelegate* delegate_ = nullptr; + base::ObserverList<AXTreeObserver> observers_; AXNode* root_ = nullptr; - base::hash_map<int32_t, AXNode*> id_map_; + std::unordered_map<int32_t, AXNode*> id_map_; std::string error_; AXTreeData data_; @@ -330,7 +220,7 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { // Map from node ID to cached table info, if the given node is a table. // Invalidated every time the tree is updated. - mutable base::hash_map<int32_t, AXTableInfo*> table_info_map_; + mutable std::unordered_map<int32_t, AXTableInfo*> table_info_map_; // The next negative node ID to use for internal nodes. int32_t next_negative_internal_node_id_ = -1; @@ -340,6 +230,37 @@ class AX_EXPORT AXTree : public AXNode::OwnerTree { // this code to be unit-tested on other platforms (for example, more // code sanitizers run on Linux). bool enable_extra_mac_nodes_ = false; + + // Contains pos_in_set and set_size data for an AXNode. + struct OrderedSetInfo { + int32_t pos_in_set; + int32_t set_size; + OrderedSetInfo() : pos_in_set(0), set_size(0) {} + ~OrderedSetInfo() {} + }; + + // Populates items vector with all items within ordered_set. + // Will only add items whose roles match the role of the + // ordered_set. + void PopulateOrderedSetItems(const AXNode* ordered_set, + const AXNode* local_parent, + std::vector<const AXNode*>& items, + bool node_is_radio_button) const; + + // Helper for GetPosInSet and GetSetSize. Computes the pos_in_set and set_size + // values of all items in ordered_set and caches those values. + void ComputeSetSizePosInSetAndCache(const AXNode& node, + const AXNode* ordered_set); + + // Map from node ID to OrderedSetInfo. + // Item-like and ordered-set-like objects will map to populated OrderedSetInfo + // objects. + // All other objects will map to default-constructed OrderedSetInfo objects. + // Invalidated every time the tree is updated. + mutable std::unordered_map<int32_t, OrderedSetInfo> ordered_set_info_map_; + + // AXTree owns pointers so copying is non-trivial. + DISALLOW_COPY_AND_ASSIGN(AXTree); }; } // namespace ui |