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 #include "ui/accessibility/ax_tree.h" | 5 #include "ui/accessibility/ax_tree.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/logging.h" | |
10 #include "base/strings/stringprintf.h" | |
9 #include "ui/accessibility/ax_node.h" | 11 #include "ui/accessibility/ax_node.h" |
10 | 12 |
11 namespace ui { | 13 namespace ui { |
12 | 14 |
13 AXTree::AXTree() | 15 AXTree::AXTree() |
14 : root_(NULL) { | 16 : root_(NULL) { |
15 AXNodeData root; | 17 AXNodeData root; |
16 root.id = 0; | 18 root.id = 0; |
17 root.role = AX_ROLE_ROOT_WEB_AREA; | 19 root.role = AX_ROLE_ROOT_WEB_AREA; |
18 | 20 |
19 AXTreeUpdate initial_state; | 21 AXTreeUpdate initial_state; |
20 initial_state.nodes.push_back(root); | 22 initial_state.nodes.push_back(root); |
21 Unserialize(initial_state); | 23 CHECK(Unserialize(initial_state)); |
aboxhall
2013/12/02 17:18:08
<< error() ? (and below)
dmazzoni
2013/12/03 08:35:04
Good catch! I thought of that when I added error()
| |
22 } | 24 } |
23 | 25 |
24 AXTree::AXTree(const AXTreeUpdate& initial_state) | 26 AXTree::AXTree(const AXTreeUpdate& initial_state) |
25 : root_(NULL) { | 27 : root_(NULL) { |
26 Unserialize(initial_state); | 28 CHECK(Unserialize(initial_state)); |
27 } | 29 } |
28 | 30 |
29 AXTree::~AXTree() { | 31 AXTree::~AXTree() { |
30 if (root_) | 32 if (root_) |
31 DestroyNodeAndSubtree(root_); | 33 DestroyNodeAndSubtree(root_); |
32 } | 34 } |
33 | 35 |
34 AXNode* AXTree::GetRoot() const { | 36 AXNode* AXTree::GetRoot() const { |
35 return root_; | 37 return root_; |
36 } | 38 } |
37 | 39 |
38 AXNode* AXTree::GetFromId(int32 id) const { | 40 AXNode* AXTree::GetFromId(int32 id) const { |
39 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id); | 41 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id); |
40 return iter != id_map_.end() ? (iter->second) : NULL; | 42 return iter != id_map_.end() ? (iter->second) : NULL; |
41 } | 43 } |
42 | 44 |
43 bool AXTree::Unserialize(const AXTreeUpdate& update) { | 45 bool AXTree::Unserialize(const AXTreeUpdate& update) { |
46 std::set<AXNode*> pending_nodes; | |
47 | |
48 if (update.node_id_to_clear != 0) { | |
49 AXNode* node = GetFromId(update.node_id_to_clear); | |
50 if (!node) { | |
51 error_ = base::StringPrintf("Bad node_id_to_clear: %d", | |
aboxhall
2013/12/02 17:18:08
Should this be behind an #ifdef DEBUG ?
dmazzoni
2013/12/03 08:35:04
I think these are useful for release mode, and it'
| |
52 update.node_id_to_clear); | |
53 return false; | |
54 } | |
55 if (node == root_) { | |
56 DestroyNodeAndSubtree(root_); | |
57 root_ = NULL; | |
58 } else { | |
59 for (int i = 0; i < node->child_count(); ++i) | |
60 DestroyNodeAndSubtree(node->ChildAtIndex(i)); | |
61 std::vector<AXNode*> children; | |
62 node->SwapChildren(children); | |
63 pending_nodes.insert(node); | |
64 } | |
65 } | |
66 | |
44 for (size_t i = 0; i < update.nodes.size(); ++i) { | 67 for (size_t i = 0; i < update.nodes.size(); ++i) { |
45 if (!UpdateNode(update.nodes[i])) | 68 if (!UpdateNode(update.nodes[i], &pending_nodes)) |
46 return false; | 69 return false; |
47 } | 70 } |
48 | 71 |
72 if (!pending_nodes.empty()) { | |
73 error_ = "Nodes left pending by the update:"; | |
74 for (std::set<AXNode*>::iterator iter = pending_nodes.begin(); | |
75 iter != pending_nodes.end(); ++iter) { | |
76 error_ += base::StringPrintf(" %d", (*iter)->id()); | |
77 } | |
78 return false; | |
79 } | |
80 | |
49 return true; | 81 return true; |
50 } | 82 } |
51 | 83 |
52 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) { | 84 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) { |
53 return new AXNode(parent, id, index_in_parent); | 85 return new AXNode(parent, id, index_in_parent); |
54 } | 86 } |
55 | 87 |
56 bool AXTree::UpdateNode(const AXNodeData& src) { | 88 bool AXTree::UpdateNode( |
89 const AXNodeData& src, std::set<AXNode*>* pending_nodes) { | |
aboxhall
2013/12/02 17:18:08
This is called orphaned_nodes in the header.
dmazzoni
2013/12/03 08:35:04
Done.
| |
57 // This method updates one node in the tree based on serialized data | 90 // This method updates one node in the tree based on serialized data |
58 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post | 91 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post |
59 // conditions. | 92 // conditions. |
60 | 93 |
61 // Look up the node by id. If it's not found, then either the root | 94 // Look up the node by id. If it's not found, then either the root |
62 // of the tree is being swapped, or we're out of sync with the source | 95 // of the tree is being swapped, or we're out of sync with the source |
63 // and this is a serious error. | 96 // and this is a serious error. |
64 AXNode* node = static_cast<AXNode*>(GetFromId(src.id)); | 97 AXNode* node = GetFromId(src.id); |
65 if (!node) { | 98 if (node) { |
66 if (src.role != AX_ROLE_ROOT_WEB_AREA) | 99 pending_nodes->erase(node); |
100 } else { | |
101 if (src.role != AX_ROLE_ROOT_WEB_AREA) { | |
102 error_ = base::StringPrintf( | |
103 "%d is not in the tree and not the new root", src.id); | |
67 return false; | 104 return false; |
105 } | |
68 node = CreateAndInitializeNode(NULL, src.id, 0); | 106 node = CreateAndInitializeNode(NULL, src.id, 0); |
69 } | 107 } |
70 | 108 |
71 // Set the node's data. | 109 // Set the node's data. |
72 node->SetData(src); | 110 node->SetData(src); |
73 | 111 |
74 // First, delete nodes that used to be children of this node but aren't | 112 // First, delete nodes that used to be children of this node but aren't |
75 // anymore. | 113 // anymore. |
76 if (!DeleteOldChildren(node, src.child_ids)) | 114 if (!DeleteOldChildren(node, src.child_ids)) |
77 return false; | 115 return false; |
78 | 116 |
79 // Now build a new children vector, reusing nodes when possible, | 117 // Now build a new children vector, reusing nodes when possible, |
80 // and swap it in. | 118 // and swap it in. |
81 std::vector<AXNode*> new_children; | 119 std::vector<AXNode*> new_children; |
82 bool success = CreateNewChildVector(node, src.child_ids, &new_children); | 120 bool success = CreateNewChildVector( |
121 node, src.child_ids, &new_children, pending_nodes); | |
83 node->SwapChildren(new_children); | 122 node->SwapChildren(new_children); |
84 | 123 |
85 // Update the root of the tree if needed. | 124 // Update the root of the tree if needed. |
86 if (src.role == AX_ROLE_ROOT_WEB_AREA && | 125 if (src.role == AX_ROLE_ROOT_WEB_AREA && |
87 (!root_ || root_->id() != src.id)) { | 126 (!root_ || root_->id() != src.id)) { |
88 if (root_) | 127 if (root_) |
89 DestroyNodeAndSubtree(root_); | 128 DestroyNodeAndSubtree(root_); |
90 root_ = node; | 129 root_ = node; |
91 OnRootChanged(); | 130 OnRootChanged(); |
92 } | 131 } |
93 | 132 |
94 return success; | 133 return success; |
95 } | 134 } |
96 | 135 |
97 void AXTree::OnRootChanged() { | 136 void AXTree::OnRootChanged() { |
98 } | 137 } |
99 | 138 |
100 AXNode* AXTree::CreateAndInitializeNode( | 139 AXNode* AXTree::CreateAndInitializeNode( |
101 AXNode* parent, int32 id, int32 index_in_parent) { | 140 AXNode* parent, int32 id, int32 index_in_parent) { |
102 AXNode* node = CreateNode(parent, id, index_in_parent); | 141 AXNode* node = CreateNode(parent, id, index_in_parent); |
103 id_map_[node->id()] = node; | 142 id_map_[node->id()] = node; |
104 return node; | 143 return node; |
105 } | 144 } |
106 | 145 |
107 void AXTree::DestroyNodeAndSubtree(AXNode* node) { | 146 void AXTree::DestroyNodeAndSubtree(AXNode* node) { |
108 id_map_.erase(node->id()); | 147 id_map_.erase(node->id()); |
109 for (int i = 0; i < node->child_count(); ++i) { | 148 for (int i = 0; i < node->child_count(); ++i) |
110 AXNode* child = static_cast<AXNode*>(node->ChildAtIndex(i)); | 149 DestroyNodeAndSubtree(node->ChildAtIndex(i)); |
111 child->Destroy(); | |
112 } | |
113 node->Destroy(); | 150 node->Destroy(); |
114 } | 151 } |
115 | 152 |
116 bool AXTree::DeleteOldChildren(AXNode* node, | 153 bool AXTree::DeleteOldChildren(AXNode* node, |
117 const std::vector<int32> new_child_ids) { | 154 const std::vector<int32> new_child_ids) { |
118 // Create a set of child ids in |src| for fast lookup, and return false | 155 // Create a set of child ids in |src| for fast lookup, and return false |
119 // if a duplicate is found; | 156 // if a duplicate is found; |
120 std::set<int32> new_child_id_set; | 157 std::set<int32> new_child_id_set; |
121 for (size_t i = 0; i < new_child_ids.size(); ++i) { | 158 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
122 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) | 159 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) { |
160 error_ = base::StringPrintf("Node %d has duplicate child id %d", | |
161 node->id(), new_child_ids[i]); | |
123 return false; | 162 return false; |
163 } | |
124 new_child_id_set.insert(new_child_ids[i]); | 164 new_child_id_set.insert(new_child_ids[i]); |
125 } | 165 } |
126 | 166 |
127 // Delete the old children. | 167 // Delete the old children. |
128 const std::vector<AXNode*>& old_children = node->children(); | 168 const std::vector<AXNode*>& old_children = node->children(); |
129 for (size_t i = 0; i < old_children.size(); ++i) { | 169 for (size_t i = 0; i < old_children.size(); ++i) { |
130 int old_id = old_children[i]->id(); | 170 int old_id = old_children[i]->id(); |
131 if (new_child_id_set.find(old_id) == new_child_id_set.end()) | 171 if (new_child_id_set.find(old_id) == new_child_id_set.end()) |
132 DestroyNodeAndSubtree(old_children[i]); | 172 DestroyNodeAndSubtree(old_children[i]); |
133 } | 173 } |
134 | 174 |
135 return true; | 175 return true; |
136 } | 176 } |
137 | 177 |
138 bool AXTree::CreateNewChildVector(AXNode* node, | 178 bool AXTree::CreateNewChildVector(AXNode* node, |
139 const std::vector<int32> new_child_ids, | 179 const std::vector<int32> new_child_ids, |
140 std::vector<AXNode*>* new_children) { | 180 std::vector<AXNode*>* new_children, |
181 std::set<AXNode*>* pending_nodes) { | |
141 bool success = true; | 182 bool success = true; |
142 for (size_t i = 0; i < new_child_ids.size(); ++i) { | 183 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
143 int32 child_id = new_child_ids[i]; | 184 int32 child_id = new_child_ids[i]; |
144 int32 index_in_parent = static_cast<int32>(i); | 185 int32 index_in_parent = static_cast<int32>(i); |
145 AXNode* child = static_cast<AXNode*>(GetFromId(child_id)); | 186 AXNode* child = GetFromId(child_id); |
146 if (child) { | 187 if (child) { |
147 if (child->parent() != node) { | 188 if (child->parent() != node) { |
148 // This is a serious error - nodes should never be reparented. | 189 // This is a serious error - nodes should never be reparented. |
149 // If this case occurs, continue so this node isn't left in an | 190 // If this case occurs, continue so this node isn't left in an |
150 // inconsistent state, but return failure at the end. | 191 // inconsistent state, but return failure at the end. |
192 error_ = base::StringPrintf( | |
193 "Node %d reparented from %d to %d", | |
194 child->id(), | |
195 child->parent() ? child->parent()->id() : 0, | |
196 node->id()); | |
151 success = false; | 197 success = false; |
152 continue; | 198 continue; |
153 } | 199 } |
154 child->SetIndexInParent(index_in_parent); | 200 child->SetIndexInParent(index_in_parent); |
155 } else { | 201 } else { |
156 child = CreateAndInitializeNode(node, child_id, index_in_parent); | 202 child = CreateAndInitializeNode(node, child_id, index_in_parent); |
203 pending_nodes->insert(child); | |
157 } | 204 } |
158 new_children->push_back(child); | 205 new_children->push_back(child); |
159 } | 206 } |
160 | 207 |
161 return success; | 208 return success; |
162 } | 209 } |
163 | 210 |
164 } // namespace ui | 211 } // namespace ui |
OLD | NEW |