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 "content/common/ax_tree_impl.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "content/common/ax_node_impl.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 // This class is an implementation of the AXTreeSource interface with |
| 14 // AXNode as the node type, that just delegates to an AXTree. The purpose |
| 15 // of this is so that AXTreeSerializer only needs to work with the |
| 16 // AXTreeSource abstraction and doesn't need to actually know about |
| 17 // AXTree directly. Another AXTreeSource is used to abstract the Blink |
| 18 // accessibility tree. |
| 19 class CONTENT_EXPORT AXTreeSourceAdapter : public AXTreeSource<AXNode> { |
| 20 public: |
| 21 AXTreeSourceAdapter(AXTree* tree) : tree_(tree) {} |
| 22 virtual ~AXTreeSourceAdapter() {} |
| 23 |
| 24 // AXTreeSource implementation. |
| 25 virtual AXNode* GetRoot() const OVERRIDE { |
| 26 return tree_->GetRoot(); |
| 27 } |
| 28 |
| 29 virtual int32 GetRootId() const OVERRIDE { |
| 30 return tree_->GetRoot()->GetId(); |
| 31 } |
| 32 |
| 33 virtual AXNode* GetFromId(int32 id) const OVERRIDE { |
| 34 return tree_->GetFromId(id); |
| 35 } |
| 36 |
| 37 virtual int32 GetId(const AXNode* node) const OVERRIDE { |
| 38 return node->GetId(); |
| 39 } |
| 40 |
| 41 virtual int GetChildCount(const AXNode* node) const OVERRIDE { |
| 42 return node->GetChildCount(); |
| 43 } |
| 44 |
| 45 virtual AXNode* GetChildAtIndex(const AXNode* node, int index) |
| 46 const OVERRIDE { |
| 47 return node->ChildAtIndex(index); |
| 48 } |
| 49 |
| 50 virtual int32 GetParentId(const AXNode* node) const OVERRIDE { |
| 51 if (node->GetParent()) |
| 52 return node->GetParent()->GetId(); |
| 53 else |
| 54 return 0; |
| 55 } |
| 56 |
| 57 virtual void SerializeNode( |
| 58 const AXNode* node, AXNodeData* out_data) const OVERRIDE { |
| 59 *out_data = node->data(); |
| 60 } |
| 61 |
| 62 private: |
| 63 AXTree* tree_; |
| 64 }; |
| 65 |
| 66 // static |
| 67 AXTree* AXTree::Create(const AXTreeUpdate& initial_state) { |
| 68 return new AXTreeImpl(initial_state); |
| 69 } |
| 70 |
| 71 AXTreeImpl::AXTreeImpl() |
| 72 : root_(NULL) { |
| 73 AXNodeData root; |
| 74 root.id = 0; |
| 75 root.role = WebKit::WebAXRoleRootWebArea; |
| 76 |
| 77 AXTreeUpdate initial_state; |
| 78 initial_state.nodes.push_back(root); |
| 79 Unserialize(initial_state); |
| 80 } |
| 81 |
| 82 AXTreeImpl::AXTreeImpl(const AXTreeUpdate& initial_state) |
| 83 : root_(NULL) { |
| 84 Unserialize(initial_state); |
| 85 } |
| 86 |
| 87 AXTreeImpl::~AXTreeImpl() { |
| 88 if (root_) |
| 89 DestroyNodeAndSubtree(root_); |
| 90 } |
| 91 |
| 92 AXNode* AXTreeImpl::GetRoot() const { |
| 93 return root_; |
| 94 } |
| 95 |
| 96 AXNode* AXTreeImpl::GetFromId(int32 id) const { |
| 97 base::hash_map<int32, AXNodeImpl*>::const_iterator iter = id_map_.find(id); |
| 98 return iter != id_map_.end() ? (iter->second) : NULL; |
| 99 } |
| 100 |
| 101 bool AXTreeImpl::Unserialize(const AXTreeUpdate& update) { |
| 102 for (size_t i = 0; i < update.nodes.size(); ++i) { |
| 103 if (!UpdateNode(update.nodes[i])) |
| 104 return false; |
| 105 } |
| 106 |
| 107 return true; |
| 108 } |
| 109 |
| 110 AXTreeSource<AXNode>* AXTreeImpl::CreateTreeSource() { |
| 111 return new AXTreeSourceAdapter(this); |
| 112 } |
| 113 |
| 114 AXNodeImpl* AXTreeImpl::CreateNode() { |
| 115 return new AXNodeImpl(); |
| 116 } |
| 117 |
| 118 bool AXTreeImpl::UpdateNode(const AXNodeData& src) { |
| 119 // This method updates one node in the tree based on serialized data |
| 120 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post |
| 121 // conditions. |
| 122 |
| 123 // Look up the node by id. If it's not found, then either the root |
| 124 // of the tree is being swapped, or we're out of sync with the source |
| 125 // and this is a serious error. |
| 126 AXNodeImpl* node = static_cast<AXNodeImpl*>(GetFromId(src.id)); |
| 127 if (!node) { |
| 128 if (src.role != WebKit::WebAXRoleRootWebArea) |
| 129 return false; |
| 130 node = CreateAndInitializeNode(NULL, src.id, 0); |
| 131 } |
| 132 |
| 133 // Set the node's data. |
| 134 node->SetData(src); |
| 135 |
| 136 // First, delete nodes that used to be children of this node but aren't |
| 137 // anymore. |
| 138 if (!DeleteOldChildren(node, src.child_ids)) |
| 139 return false; |
| 140 |
| 141 // Now build a new children vector, reusing nodes when possible, |
| 142 // and swap it in. |
| 143 std::vector<AXNodeImpl*> new_children; |
| 144 bool success = CreateNewChildVector(node, src.child_ids, &new_children); |
| 145 node->SwapChildren(new_children); |
| 146 |
| 147 // Update the root of the tree if needed. |
| 148 if (src.role == WebKit::WebAXRoleRootWebArea && |
| 149 (!root_ || root_->GetId() != src.id)) { |
| 150 if (root_) |
| 151 DestroyNodeAndSubtree(root_); |
| 152 root_ = node; |
| 153 OnRootChanged(); |
| 154 } |
| 155 |
| 156 return success; |
| 157 } |
| 158 |
| 159 void AXTreeImpl::OnRootChanged() { |
| 160 } |
| 161 |
| 162 AXNodeImpl* AXTreeImpl::CreateAndInitializeNode( |
| 163 AXNodeImpl* parent, int32 id, int32 index_in_parent) { |
| 164 AXNodeImpl* node = CreateNode(); |
| 165 node->Init(parent, id, index_in_parent); |
| 166 id_map_[node->GetId()] = node; |
| 167 return node; |
| 168 } |
| 169 |
| 170 void AXTreeImpl::DestroyNodeAndSubtree(AXNodeImpl* node) { |
| 171 id_map_.erase(node->GetId()); |
| 172 for (int i = 0; i < node->GetChildCount(); ++i) { |
| 173 AXNodeImpl* child = static_cast<AXNodeImpl*>(node->ChildAtIndex(i)); |
| 174 child->Destroy(); |
| 175 } |
| 176 node->Destroy(); |
| 177 } |
| 178 |
| 179 bool AXTreeImpl::DeleteOldChildren(AXNodeImpl* node, |
| 180 const std::vector<int32> new_child_ids) { |
| 181 // Create a set of child ids in |src| for fast lookup, and return false |
| 182 // if a duplicate is found; |
| 183 std::set<int32> new_child_id_set; |
| 184 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
| 185 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) |
| 186 return false; |
| 187 new_child_id_set.insert(new_child_ids[i]); |
| 188 } |
| 189 |
| 190 // Delete the old children. |
| 191 const std::vector<AXNodeImpl*>& old_children = node->children(); |
| 192 for (size_t i = 0; i < old_children.size(); ++i) { |
| 193 int old_id = old_children[i]->GetId(); |
| 194 if (new_child_id_set.find(old_id) == new_child_id_set.end()) |
| 195 DestroyNodeAndSubtree(old_children[i]); |
| 196 } |
| 197 |
| 198 return true; |
| 199 } |
| 200 |
| 201 bool AXTreeImpl::CreateNewChildVector(AXNodeImpl* node, |
| 202 const std::vector<int32> new_child_ids, |
| 203 std::vector<AXNodeImpl*>* new_children) { |
| 204 bool success = true; |
| 205 for (size_t i = 0; i < new_child_ids.size(); ++i) { |
| 206 int32 child_id = new_child_ids[i]; |
| 207 int32 index_in_parent = static_cast<int32>(i); |
| 208 AXNodeImpl* child = static_cast<AXNodeImpl*>(GetFromId(child_id)); |
| 209 if (child) { |
| 210 if (child->GetParent() != node) { |
| 211 // This is a serious error - nodes should never be reparented. |
| 212 // If this case occurs, continue so this node isn't left in an |
| 213 // inconsistent state, but return failure at the end. |
| 214 success = false; |
| 215 continue; |
| 216 } |
| 217 child->UpdateIndexInParent(index_in_parent); |
| 218 } else { |
| 219 child = CreateAndInitializeNode(node, child_id, index_in_parent); |
| 220 } |
| 221 new_children->push_back(child); |
| 222 } |
| 223 |
| 224 return success; |
| 225 } |
| 226 |
| 227 } // namespace content |
OLD | NEW |