OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_ACCESSIBILITY_AX_TREE_H_ | 5 #ifndef UI_ACCESSIBILITY_AX_TREE_H_ |
6 #define UI_ACCESSIBILITY_AX_TREE_H_ | 6 #define UI_ACCESSIBILITY_AX_TREE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "ui/accessibility/ax_export.h" | 11 #include "ui/accessibility/ax_export.h" |
12 #include "ui/accessibility/ax_tree.h" | 12 #include "ui/accessibility/ax_tree.h" |
13 #include "ui/accessibility/ax_tree_update.h" | 13 #include "ui/accessibility/ax_tree_update.h" |
14 | 14 |
15 namespace ui { | 15 namespace ui { |
16 | 16 |
17 class AXNode; | 17 class AXNode; |
| 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 }; |
18 | 43 |
19 // AXTree is a live, managed tree of AXNode objects that can receive | 44 // AXTree is a live, managed tree of AXNode objects that can receive |
20 // updates from another AXTreeSource via AXTreeUpdates, and it can be | 45 // updates from another AXTreeSource via AXTreeUpdates, and it can be |
21 // used as a source for sending updates to another client tree. | 46 // used as a source for sending updates to another client tree. |
22 // It's designed to be subclassed to implement support for native | 47 // It's designed to be subclassed to implement support for native |
23 // accessibility APIs on a specific platform. | 48 // accessibility APIs on a specific platform. |
24 class AX_EXPORT AXTree { | 49 class AX_EXPORT AXTree { |
25 public: | 50 public: |
26 AXTree(); | 51 AXTree(); |
27 explicit AXTree(const AXTreeUpdate& initial_state); | 52 explicit AXTree(const AXTreeUpdate& initial_state); |
28 virtual ~AXTree(); | 53 virtual ~AXTree(); |
29 | 54 |
| 55 virtual void SetDelegate(AXTreeDelegate* delegate); |
| 56 |
30 virtual AXNode* GetRoot() const; | 57 virtual AXNode* GetRoot() const; |
31 virtual AXNode* GetFromId(int32 id) const; | 58 virtual AXNode* GetFromId(int32 id) const; |
32 | 59 |
33 // Returns true on success. If it returns false, it's a fatal error | 60 // Returns true on success. If it returns false, it's a fatal error |
34 // and this tree should be destroyed, and the source of the tree update | 61 // and this tree should be destroyed, and the source of the tree update |
35 // should not be trusted any longer. | 62 // should not be trusted any longer. |
36 virtual bool Unserialize(const AXTreeUpdate& update); | 63 virtual bool Unserialize(const AXTreeUpdate& update); |
37 | 64 |
38 // Return a multi-line indented string representation, for logging. | 65 // Return a multi-line indented string representation, for logging. |
39 std::string ToString() const; | 66 std::string ToString() const; |
40 | 67 |
41 // A string describing the error from an unsuccessful Unserialize, | 68 // A string describing the error from an unsuccessful Unserialize, |
42 // for testing and debugging. | 69 // for testing and debugging. |
43 const std::string& error() { return error_; } | 70 const std::string& error() { return error_; } |
44 | 71 |
45 protected: | 72 private: |
46 // Subclasses can override this to use a subclass of AXNode. | 73 AXNode* CreateNode(AXNode* parent, int32 id, int32 index_in_parent); |
47 virtual AXNode* CreateNode(AXNode* parent, int32 id, int32 index_in_parent); | |
48 | 74 |
49 // This is called from within Unserialize(), it returns true on success. | 75 // This is called from within Unserialize(), it returns true on success. |
50 // Subclasses can override this to do additional processing. |pending_nodes| | 76 bool UpdateNode(const AXNodeData& src, AXTreeUpdateState* update_state); |
51 // is updated to contain all nodes that have been implicitly referenced | |
52 // as part of this update, but haven't been updated yet. It's an error if | |
53 // there are any pending nodes at the end of Unserialize. | |
54 virtual bool UpdateNode(const AXNodeData& src, | |
55 std::set<AXNode*>* pending_nodes); | |
56 | 77 |
57 // Subclasses can override this to do special behavior when the root changes. | 78 void OnRootChanged(); |
58 virtual void OnRootChanged(); | |
59 | 79 |
60 private: | |
61 // Convenience function to create a node and call Initialize on it. | 80 // Convenience function to create a node and call Initialize on it. |
62 AXNode* CreateAndInitializeNode( | 81 AXNode* CreateAndInitializeNode( |
63 AXNode* parent, int32 id, int32 index_in_parent); | 82 AXNode* parent, int32 id, int32 index_in_parent); |
64 | 83 |
65 // Call Destroy() on |node|, and delete it from the id map, and then | 84 // Call Destroy() on |node|, and delete it from the id map, and then |
66 // call recursively on all nodes in its subtree. | 85 // call recursively on all nodes in its subtree. |
67 void DestroyNodeAndSubtree(AXNode* node); | 86 void DestroyNodeAndSubtree(AXNode* node); |
68 | 87 |
69 // Iterate over the children of |node| and for each child, destroy the | 88 // Iterate over the children of |node| and for each child, destroy the |
70 // child and its subtree if its id is not in |new_child_ids|. Returns | 89 // child and its subtree if its id is not in |new_child_ids|. Returns |
71 // true on success, false on fatal error. | 90 // true on success, false on fatal error. |
72 bool DeleteOldChildren(AXNode* node, | 91 bool DeleteOldChildren(AXNode* node, |
73 const std::vector<int32> new_child_ids); | 92 const std::vector<int32> new_child_ids); |
74 | 93 |
75 // Iterate over |new_child_ids| and populate |new_children| with | 94 // Iterate over |new_child_ids| and populate |new_children| with |
76 // pointers to child nodes, reusing existing nodes already in the tree | 95 // pointers to child nodes, reusing existing nodes already in the tree |
77 // if they exist, and creating otherwise. Reparenting is disallowed, so | 96 // if they exist, and creating otherwise. Reparenting is disallowed, so |
78 // if the id already exists as the child of another node, that's an | 97 // if the id already exists as the child of another node, that's an |
79 // error. Returns true on success, false on fatal error. See | 98 // error. Returns true on success, false on fatal error. |
80 // UpdateNode, above, for an explanation of |pending_nodes|. | |
81 bool CreateNewChildVector(AXNode* node, | 99 bool CreateNewChildVector(AXNode* node, |
82 const std::vector<int32> new_child_ids, | 100 const std::vector<int32> new_child_ids, |
83 std::vector<AXNode*>* new_children, | 101 std::vector<AXNode*>* new_children, |
84 std::set<AXNode*>* pending_nodes); | 102 AXTreeUpdateState* update_state); |
85 | 103 |
| 104 AXTreeDelegate* delegate_; |
86 AXNode* root_; | 105 AXNode* root_; |
87 base::hash_map<int32, AXNode*> id_map_; | 106 base::hash_map<int32, AXNode*> id_map_; |
88 std::string error_; | 107 std::string error_; |
89 }; | 108 }; |
90 | 109 |
91 } // namespace ui | 110 } // namespace ui |
92 | 111 |
93 #endif // UI_ACCESSIBILITY_AX_TREE_H_ | 112 #endif // UI_ACCESSIBILITY_AX_TREE_H_ |
OLD | NEW |