OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 #include "ui/accessibility/ax_tree.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "ui/accessibility/ax_node.h" |
| 10 |
| 11 namespace ui { |
| 12 |
| 13 AXTree::AXTree() |
| 14 : root_(NULL) { |
| 15 AXNodeData root; |
| 16 root.id = 0; |
| 17 root.role = AX_ROLE_ROOT_WEB_AREA; |
| 18 |
| 19 AXTreeUpdate initial_state; |
| 20 initial_state.nodes.push_back(root); |
| 21 Unserialize(initial_state); |
| 22 } |
| 23 |
| 24 AXTree::AXTree(const AXTreeUpdate& initial_state) |
| 25 : root_(NULL) { |
| 26 Unserialize(initial_state); |
| 27 } |
| 28 |
| 29 AXTree::~AXTree() { |
| 30 if (root_) |
| 31 DestroyNodeAndSubtree(root_); |
| 32 } |
| 33 |
| 34 AXNode* AXTree::GetRoot() const { |
| 35 return root_; |
| 36 } |
| 37 |
| 38 AXNode* AXTree::GetFromId(int32 id) const { |
| 39 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id); |
| 40 return iter != id_map_.end() ? (iter->second) : NULL; |
| 41 } |
| 42 |
| 43 bool AXTree::Unserialize(const AXTreeUpdate& update) { |
| 44 for (size_t i = 0; i < update.nodes.size(); ++i) { |
| 45 if (!UpdateNode(update.nodes[i])) |
| 46 return false; |
| 47 } |
| 48 |
| 49 return true; |
| 50 } |
| 51 |
| 52 AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) { |
| 53 return new AXNode(parent, id, index_in_parent); |
| 54 } |
| 55 |
| 56 bool AXTree::UpdateNode(const AXNodeData& src) { |
| 57 // This method updates one node in the tree based on serialized data |
| 58 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post |
| 59 // conditions. |
| 60 |
| 61 // 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 |
| 63 // and this is a serious error. |
| 64 AXNode* node = static_cast<AXNode*>(GetFromId(src.id)); |
| 65 if (!node) { |
| 66 if (src.role != AX_ROLE_ROOT_WEB_AREA) |
| 67 return false; |
| 68 node = CreateAndInitializeNode(NULL, src.id, 0); |
| 69 } |
| 70 |
| 71 // Set the node's data. |
| 72 node->SetData(src); |
| 73 |
| 74 // First, delete nodes that used to be children of this node but aren't |
| 75 // anymore. |
| 76 if (!DeleteOldChildren(node, src.child_ids)) |
| 77 return false; |
| 78 |
| 79 // Now build a new children vector, reusing nodes when possible, |
| 80 // and swap it in. |
| 81 std::vector<AXNode*> new_children; |
| 82 bool success = CreateNewChildVector(node, src.child_ids, &new_children); |
| 83 node->SwapChildren(new_children); |
| 84 |
| 85 // Update the root of the tree if needed. |
| 86 if (src.role == AX_ROLE_ROOT_WEB_AREA && |
| 87 (!root_ || root_->id() != src.id)) { |
| 88 if (root_) |
| 89 DestroyNodeAndSubtree(root_); |
| 90 root_ = node; |
| 91 OnRootChanged(); |
| 92 } |
| 93 |
| 94 return success; |
| 95 } |
| 96 |
| 97 void AXTree::OnRootChanged() { |
| 98 } |
| 99 |
| 100 AXNode* AXTree::CreateAndInitializeNode( |
| 101 AXNode* parent, int32 id, int32 index_in_parent) { |
| 102 AXNode* node = CreateNode(parent, id, index_in_parent); |
| 103 id_map_[node->id()] = node; |
| 104 return node; |
| 105 } |
| 106 |
| 107 void AXTree::DestroyNodeAndSubtree(AXNode* node) { |
| 108 id_map_.erase(node->id()); |
| 109 for (int i = 0; i < node->child_count(); ++i) { |
| 110 AXNode* child = static_cast<AXNode*>(node->ChildAtIndex(i)); |
| 111 child->Destroy(); |
| 112 } |
| 113 node->Destroy(); |
| 114 } |
| 115 |
| 116 bool AXTree::DeleteOldChildren(AXNode* node, |
| 117 const std::vector<int32> new_child_ids) { |
| 118 // Create a set of child ids in |src| for fast lookup, and return false |
| 119 // if a duplicate is found; |
| 120 std::set<int32> new_child_id_set; |
| 121 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()) |
| 123 return false; |
| 124 new_child_id_set.insert(new_child_ids[i]); |
| 125 } |
| 126 |
| 127 // Delete the old children. |
| 128 const std::vector<AXNode*>& old_children = node->children(); |
| 129 for (size_t i = 0; i < old_children.size(); ++i) { |
| 130 int old_id = old_children[i]->id(); |
| 131 if (new_child_id_set.find(old_id) == new_child_id_set.end()) |
| 132 DestroyNodeAndSubtree(old_children[i]); |
| 133 } |
| 134 |
| 135 return true; |
| 136 } |
| 137 |
| 138 bool AXTree::CreateNewChildVector(AXNode* node, |
| 139 const std::vector<int32> new_child_ids, |
| 140 std::vector<AXNode*>* new_children) { |
| 141 bool success = true; |
| 142 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
| 143 int32 child_id = new_child_ids[i]; |
| 144 int32 index_in_parent = static_cast<int32>(i); |
| 145 AXNode* child = static_cast<AXNode*>(GetFromId(child_id)); |
| 146 if (child) { |
| 147 if (child->parent() != node) { |
| 148 // This is a serious error - nodes should never be reparented. |
| 149 // If this case occurs, continue so this node isn't left in an |
| 150 // inconsistent state, but return failure at the end. |
| 151 success = false; |
| 152 continue; |
| 153 } |
| 154 child->SetIndexInParent(index_in_parent); |
| 155 } else { |
| 156 child = CreateAndInitializeNode(node, child_id, index_in_parent); |
| 157 } |
| 158 new_children->push_back(child); |
| 159 } |
| 160 |
| 161 return success; |
| 162 } |
| 163 |
| 164 } // namespace ui |
OLD | NEW |