[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 1 | // Copyright 2013 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 UI_ACCESSIBILITY_AX_TREE_H_ |
| 6 | #define UI_ACCESSIBILITY_AX_TREE_H_ |
| 7 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 8 | #include <set> |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 9 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 10 | #include "base/containers/hash_tables.h" |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 11 | #include "ui/accessibility/ax_export.h" |
| 12 | #include "ui/accessibility/ax_tree.h" |
| 13 | #include "ui/accessibility/ax_tree_update.h" |
| 14 | |
| 15 | namespace ui { |
| 16 | |
| 17 | class AXNode; |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 18 | struct AXTreeUpdateState; |
| 19 | |
| 20 | // Used when you want to be notified when changes happen to the tree. |
| 21 | class AX_EXPORT AXTreeDelegate { |
| 22 | public: |
| 23 | AXTreeDelegate(); |
| 24 | virtual ~AXTreeDelegate(); |
| 25 | |
| 26 | // Called just before a node is deleted. Its id and data will be valid, |
| 27 | // but its links to parents and children are invalid. This is called |
| 28 | // in the middle of an update, the tree may be in an invalid state! |
| 29 | virtual void OnNodeWillBeDeleted(AXNode* node) = 0; |
| 30 | |
| 31 | // Called after a new node is created. It's guaranteed to be called |
| 32 | // after it's been fully initialized, so you can rely on its data and |
| 33 | // links to parents and children being valid. This will be called on |
| 34 | // parents before it's called on their children. |
| 35 | virtual void OnNodeCreated(AXNode* node) = 0; |
| 36 | |
| 37 | // Called when a node changes its data or children. |
| 38 | virtual void OnNodeChanged(AXNode* node) = 0; |
| 39 | |
| 40 | // Called when the root node changes. |
| 41 | virtual void OnRootChanged(AXNode* new_root) = 0; |
| 42 | }; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 43 | |
| 44 | // AXTree is a live, managed tree of AXNode objects that can receive |
| 45 | // updates from another AXTreeSource via AXTreeUpdates, and it can be |
| 46 | // used as a source for sending updates to another client tree. |
| 47 | // It's designed to be subclassed to implement support for native |
| 48 | // accessibility APIs on a specific platform. |
| 49 | class AX_EXPORT AXTree { |
| 50 | public: |
| 51 | AXTree(); |
| 52 | explicit AXTree(const AXTreeUpdate& initial_state); |
| 53 | virtual ~AXTree(); |
| 54 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 55 | virtual void SetDelegate(AXTreeDelegate* delegate); |
| 56 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 57 | virtual AXNode* GetRoot() const; |
| 58 | virtual AXNode* GetFromId(int32 id) const; |
| 59 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 60 | // Returns true on success. If it returns false, it's a fatal error |
| 61 | // and this tree should be destroyed, and the source of the tree update |
| 62 | // should not be trusted any longer. |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 63 | virtual bool Unserialize(const AXTreeUpdate& update); |
| 64 | |
[email protected] | 5eec2f5 | 2014-01-06 22:30:54 | [diff] [blame] | 65 | // Return a multi-line indented string representation, for logging. |
| 66 | std::string ToString() const; |
| 67 | |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 68 | // A string describing the error from an unsuccessful Unserialize, |
| 69 | // for testing and debugging. |
| 70 | const std::string& error() { return error_; } |
| 71 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 72 | private: |
| 73 | AXNode* CreateNode(AXNode* parent, int32 id, int32 index_in_parent); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 74 | |
| 75 | // This is called from within Unserialize(), it returns true on success. |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 76 | bool UpdateNode(const AXNodeData& src, AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 77 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 78 | void OnRootChanged(); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 79 | |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 80 | // Convenience function to create a node and call Initialize on it. |
| 81 | AXNode* CreateAndInitializeNode( |
| 82 | AXNode* parent, int32 id, int32 index_in_parent); |
| 83 | |
| 84 | // Call Destroy() on |node|, and delete it from the id map, and then |
| 85 | // call recursively on all nodes in its subtree. |
| 86 | void DestroyNodeAndSubtree(AXNode* node); |
| 87 | |
| 88 | // Iterate over the children of |node| and for each child, destroy the |
| 89 | // child and its subtree if its id is not in |new_child_ids|. Returns |
| 90 | // true on success, false on fatal error. |
| 91 | bool DeleteOldChildren(AXNode* node, |
| 92 | const std::vector<int32> new_child_ids); |
| 93 | |
| 94 | // Iterate over |new_child_ids| and populate |new_children| with |
| 95 | // pointers to child nodes, reusing existing nodes already in the tree |
| 96 | // if they exist, and creating otherwise. Reparenting is disallowed, so |
| 97 | // if the id already exists as the child of another node, that's an |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 98 | // error. Returns true on success, false on fatal error. |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 99 | bool CreateNewChildVector(AXNode* node, |
| 100 | const std::vector<int32> new_child_ids, |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 101 | std::vector<AXNode*>* new_children, |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 102 | AXTreeUpdateState* update_state); |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 103 | |
[email protected] | e736e81b | 2014-02-24 07:15:58 | [diff] [blame^] | 104 | AXTreeDelegate* delegate_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 105 | AXNode* root_; |
| 106 | base::hash_map<int32, AXNode*> id_map_; |
[email protected] | d4e27346 | 2013-12-04 04:37:58 | [diff] [blame] | 107 | std::string error_; |
[email protected] | 4b02bbca | 2013-11-22 08:59:03 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace ui |
| 111 | |
| 112 | #endif // UI_ACCESSIBILITY_AX_TREE_H_ |