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 <stddef.h> |
| 8 |
7 #include <set> | 9 #include <set> |
8 | 10 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
11 #include "ui/accessibility/ax_node.h" | 13 #include "ui/accessibility/ax_node.h" |
12 | 14 |
13 namespace ui { | 15 namespace ui { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 67 |
66 AXTree::~AXTree() { | 68 AXTree::~AXTree() { |
67 if (root_) | 69 if (root_) |
68 DestroyNodeAndSubtree(root_, nullptr); | 70 DestroyNodeAndSubtree(root_, nullptr); |
69 } | 71 } |
70 | 72 |
71 void AXTree::SetDelegate(AXTreeDelegate* delegate) { | 73 void AXTree::SetDelegate(AXTreeDelegate* delegate) { |
72 delegate_ = delegate; | 74 delegate_ = delegate; |
73 } | 75 } |
74 | 76 |
75 AXNode* AXTree::GetFromId(int32 id) const { | 77 AXNode* AXTree::GetFromId(int32_t id) const { |
76 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id); | 78 base::hash_map<int32_t, AXNode*>::const_iterator iter = id_map_.find(id); |
77 return iter != id_map_.end() ? iter->second : NULL; | 79 return iter != id_map_.end() ? iter->second : NULL; |
78 } | 80 } |
79 | 81 |
80 void AXTree::UpdateData(const AXTreeData& data) { | 82 void AXTree::UpdateData(const AXTreeData& data) { |
81 data_ = data; | 83 data_ = data; |
82 if (delegate_) | 84 if (delegate_) |
83 delegate_->OnTreeDataChanged(this); | 85 delegate_->OnTreeDataChanged(this); |
84 } | 86 } |
85 | 87 |
86 bool AXTree::Unserialize(const AXTreeUpdate& update) { | 88 bool AXTree::Unserialize(const AXTreeUpdate& update) { |
87 AXTreeUpdateState update_state; | 89 AXTreeUpdateState update_state; |
88 int32 old_root_id = root_ ? root_->id() : 0; | 90 int32_t old_root_id = root_ ? root_->id() : 0; |
89 | 91 |
90 if (update.has_tree_data) | 92 if (update.has_tree_data) |
91 UpdateData(update.tree_data); | 93 UpdateData(update.tree_data); |
92 | 94 |
93 if (update.node_id_to_clear != 0) { | 95 if (update.node_id_to_clear != 0) { |
94 AXNode* node = GetFromId(update.node_id_to_clear); | 96 AXNode* node = GetFromId(update.node_id_to_clear); |
95 if (!node) { | 97 if (!node) { |
96 error_ = base::StringPrintf("Bad node_id_to_clear: %d", | 98 error_ = base::StringPrintf("Bad node_id_to_clear: %d", |
97 update.node_id_to_clear); | 99 update.node_id_to_clear); |
98 return false; | 100 return false; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 this, root_->id() != old_root_id, changes); | 151 this, root_->id() != old_root_id, changes); |
150 } | 152 } |
151 | 153 |
152 return true; | 154 return true; |
153 } | 155 } |
154 | 156 |
155 std::string AXTree::ToString() const { | 157 std::string AXTree::ToString() const { |
156 return "AXTree" + data_.ToString() + "\n" + TreeToStringHelper(root_, 0); | 158 return "AXTree" + data_.ToString() + "\n" + TreeToStringHelper(root_, 0); |
157 } | 159 } |
158 | 160 |
159 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) { | 161 AXNode* AXTree::CreateNode(AXNode* parent, |
| 162 int32_t id, |
| 163 int32_t index_in_parent) { |
160 AXNode* new_node = new AXNode(parent, id, index_in_parent); | 164 AXNode* new_node = new AXNode(parent, id, index_in_parent); |
161 id_map_[new_node->id()] = new_node; | 165 id_map_[new_node->id()] = new_node; |
162 if (delegate_) | 166 if (delegate_) |
163 delegate_->OnNodeCreated(this, new_node); | 167 delegate_->OnNodeCreated(this, new_node); |
164 return new_node; | 168 return new_node; |
165 } | 169 } |
166 | 170 |
167 bool AXTree::UpdateNode(const AXNodeData& src, | 171 bool AXTree::UpdateNode(const AXNodeData& src, |
168 AXTreeUpdateState* update_state) { | 172 AXTreeUpdateState* update_state) { |
169 // This method updates one node in the tree based on serialized data | 173 // This method updates one node in the tree based on serialized data |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 id_map_.erase(node->id()); | 245 id_map_.erase(node->id()); |
242 for (int i = 0; i < node->child_count(); ++i) | 246 for (int i = 0; i < node->child_count(); ++i) |
243 DestroyNodeAndSubtree(node->ChildAtIndex(i), update_state); | 247 DestroyNodeAndSubtree(node->ChildAtIndex(i), update_state); |
244 if (update_state) { | 248 if (update_state) { |
245 update_state->pending_nodes.erase(node); | 249 update_state->pending_nodes.erase(node); |
246 } | 250 } |
247 node->Destroy(); | 251 node->Destroy(); |
248 } | 252 } |
249 | 253 |
250 bool AXTree::DeleteOldChildren(AXNode* node, | 254 bool AXTree::DeleteOldChildren(AXNode* node, |
251 const std::vector<int32>& new_child_ids, | 255 const std::vector<int32_t>& new_child_ids, |
252 AXTreeUpdateState* update_state) { | 256 AXTreeUpdateState* update_state) { |
253 // Create a set of child ids in |src| for fast lookup, and return false | 257 // Create a set of child ids in |src| for fast lookup, and return false |
254 // if a duplicate is found; | 258 // if a duplicate is found; |
255 std::set<int32> new_child_id_set; | 259 std::set<int32_t> new_child_id_set; |
256 for (size_t i = 0; i < new_child_ids.size(); ++i) { | 260 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
257 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) { | 261 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) { |
258 error_ = base::StringPrintf("Node %d has duplicate child id %d", | 262 error_ = base::StringPrintf("Node %d has duplicate child id %d", |
259 node->id(), new_child_ids[i]); | 263 node->id(), new_child_ids[i]); |
260 return false; | 264 return false; |
261 } | 265 } |
262 new_child_id_set.insert(new_child_ids[i]); | 266 new_child_id_set.insert(new_child_ids[i]); |
263 } | 267 } |
264 | 268 |
265 // Delete the old children. | 269 // Delete the old children. |
266 const std::vector<AXNode*>& old_children = node->children(); | 270 const std::vector<AXNode*>& old_children = node->children(); |
267 for (size_t i = 0; i < old_children.size(); ++i) { | 271 for (size_t i = 0; i < old_children.size(); ++i) { |
268 int old_id = old_children[i]->id(); | 272 int old_id = old_children[i]->id(); |
269 if (new_child_id_set.find(old_id) == new_child_id_set.end()) | 273 if (new_child_id_set.find(old_id) == new_child_id_set.end()) |
270 DestroySubtree(old_children[i], update_state); | 274 DestroySubtree(old_children[i], update_state); |
271 } | 275 } |
272 | 276 |
273 return true; | 277 return true; |
274 } | 278 } |
275 | 279 |
276 bool AXTree::CreateNewChildVector(AXNode* node, | 280 bool AXTree::CreateNewChildVector(AXNode* node, |
277 const std::vector<int32>& new_child_ids, | 281 const std::vector<int32_t>& new_child_ids, |
278 std::vector<AXNode*>* new_children, | 282 std::vector<AXNode*>* new_children, |
279 AXTreeUpdateState* update_state) { | 283 AXTreeUpdateState* update_state) { |
280 bool success = true; | 284 bool success = true; |
281 for (size_t i = 0; i < new_child_ids.size(); ++i) { | 285 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
282 int32 child_id = new_child_ids[i]; | 286 int32_t child_id = new_child_ids[i]; |
283 int32 index_in_parent = static_cast<int32>(i); | 287 int32_t index_in_parent = static_cast<int32_t>(i); |
284 AXNode* child = GetFromId(child_id); | 288 AXNode* child = GetFromId(child_id); |
285 if (child) { | 289 if (child) { |
286 if (child->parent() != node) { | 290 if (child->parent() != node) { |
287 // This is a serious error - nodes should never be reparented. | 291 // This is a serious error - nodes should never be reparented. |
288 // If this case occurs, continue so this node isn't left in an | 292 // If this case occurs, continue so this node isn't left in an |
289 // inconsistent state, but return failure at the end. | 293 // inconsistent state, but return failure at the end. |
290 error_ = base::StringPrintf( | 294 error_ = base::StringPrintf( |
291 "Node %d reparented from %d to %d", | 295 "Node %d reparented from %d to %d", |
292 child->id(), | 296 child->id(), |
293 child->parent() ? child->parent()->id() : 0, | 297 child->parent() ? child->parent()->id() : 0, |
294 node->id()); | 298 node->id()); |
295 success = false; | 299 success = false; |
296 continue; | 300 continue; |
297 } | 301 } |
298 child->SetIndexInParent(index_in_parent); | 302 child->SetIndexInParent(index_in_parent); |
299 } else { | 303 } else { |
300 child = CreateNode(node, child_id, index_in_parent); | 304 child = CreateNode(node, child_id, index_in_parent); |
301 update_state->pending_nodes.insert(child); | 305 update_state->pending_nodes.insert(child); |
302 update_state->new_nodes.insert(child); | 306 update_state->new_nodes.insert(child); |
303 } | 307 } |
304 new_children->push_back(child); | 308 new_children->push_back(child); |
305 } | 309 } |
306 | 310 |
307 return success; | 311 return success; |
308 } | 312 } |
309 | 313 |
310 } // namespace ui | 314 } // namespace ui |
OLD | NEW |