Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: ui/accessibility/ax_tree.cc

Issue 67283004: First step to move common accessibility code out of content. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Get rid of content change Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // 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 UI_EXPORT AXTreeSourceAdapter : public AXTreeSource<AXNode> {
20 public:
21 AXTreeSourceAdapter(AXTree* tree) : tree_(tree) {}
22 virtual ~AXTreeSourceAdapter() {}
23
24 // AXTreeSource implementation.
25 virtual AXNode* GetFromId(int32 id) const OVERRIDE {
26 return tree_->GetFromId(id);
27 }
28
29 virtual int32 GetId(const AXNode* node) const OVERRIDE {
30 return node->id();
31 }
32
33 virtual int GetChildCount(const AXNode* node) const OVERRIDE {
34 return node->child_count();
35 }
36
37 virtual AXNode* GetChildAtIndex(const AXNode* node, int index)
38 const OVERRIDE {
39 return node->ChildAtIndex(index);
40 }
41
42 virtual int32 GetParentId(const AXNode* node) const OVERRIDE {
43 if (node->parent())
44 return node->parent()->id();
45 else
46 return 0;
47 }
48
49 virtual void SerializeNode(
50 const AXNode* node, AXNodeData* out_data) const OVERRIDE {
51 *out_data = node->data();
52 }
53
54 private:
55 AXTree* tree_;
56 };
57
58 AXTree::AXTree()
59 : root_(NULL) {
60 AXNodeData root;
61 root.id = 0;
62 root.role = AX_ROLE_ROOT_WEB_AREA;
63
64 AXTreeUpdate initial_state;
65 initial_state.nodes.push_back(root);
66 Unserialize(initial_state);
67 }
68
69 AXTree::AXTree(const AXTreeUpdate& initial_state)
70 : root_(NULL) {
71 Unserialize(initial_state);
72 }
73
74 AXTree::~AXTree() {
75 if (root_)
76 DestroyNodeAndSubtree(root_);
77 }
78
79 AXNode* AXTree::GetRoot() const {
80 return root_;
81 }
82
83 AXNode* AXTree::GetFromId(int32 id) const {
84 base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id);
85 return iter != id_map_.end() ? (iter->second) : NULL;
86 }
87
88 bool AXTree::Unserialize(const AXTreeUpdate& update) {
89 for (size_t i = 0; i < update.nodes.size(); ++i) {
90 if (!UpdateNode(update.nodes[i]))
91 return false;
92 }
93
94 return true;
95 }
96
97 AXTreeSource<AXNode>* AXTree::CreateTreeSource() {
98 return new AXTreeSourceAdapter(this);
99 }
100
101 AXNode* AXTree::CreateNode() {
102 return new AXNode();
103 }
104
105 bool AXTree::UpdateNode(const AXNodeData& src) {
106 // This method updates one node in the tree based on serialized data
107 // received in an AXTreeUpdate. See AXTreeUpdate for pre and post
108 // conditions.
109
110 // Look up the node by id. If it's not found, then either the root
111 // of the tree is being swapped, or we're out of sync with the source
112 // and this is a serious error.
113 AXNode* node = static_cast<AXNode*>(GetFromId(src.id));
114 if (!node) {
115 if (src.role != AX_ROLE_ROOT_WEB_AREA)
116 return false;
117 node = CreateAndInitializeNode(NULL, src.id, 0);
118 }
119
120 // Set the node's data.
121 node->SetData(src);
122
123 // First, delete nodes that used to be children of this node but aren't
124 // anymore.
125 if (!DeleteOldChildren(node, src.child_ids))
126 return false;
127
128 // Now build a new children vector, reusing nodes when possible,
129 // and swap it in.
130 std::vector<AXNode*> new_children;
131 bool success = CreateNewChildVector(node, src.child_ids, &new_children);
132 node->SwapChildren(new_children);
133
134 // Update the root of the tree if needed.
135 if (src.role == AX_ROLE_ROOT_WEB_AREA &&
136 (!root_ || root_->id() != src.id)) {
137 if (root_)
138 DestroyNodeAndSubtree(root_);
139 root_ = node;
140 OnRootChanged();
141 }
142
143 return success;
144 }
145
146 void AXTree::OnRootChanged() {
147 }
148
149 AXNode* AXTree::CreateAndInitializeNode(
150 AXNode* parent, int32 id, int32 index_in_parent) {
151 AXNode* node = CreateNode();
152 node->Init(parent, id, index_in_parent);
153 id_map_[node->id()] = node;
154 return node;
155 }
156
157 void AXTree::DestroyNodeAndSubtree(AXNode* node) {
158 id_map_.erase(node->id());
159 for (int i = 0; i < node->child_count(); ++i) {
160 AXNode* child = static_cast<AXNode*>(node->ChildAtIndex(i));
161 child->Destroy();
162 }
163 node->Destroy();
164 }
165
166 bool AXTree::DeleteOldChildren(AXNode* node,
167 const std::vector<int32> new_child_ids) {
168 // Create a set of child ids in |src| for fast lookup, and return false
169 // if a duplicate is found;
170 std::set<int32> new_child_id_set;
171 for (size_t i = 0; i < new_child_ids.size(); ++i) {
172 if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end())
173 return false;
174 new_child_id_set.insert(new_child_ids[i]);
175 }
176
177 // Delete the old children.
178 const std::vector<AXNode*>& old_children = node->children();
179 for (size_t i = 0; i < old_children.size(); ++i) {
180 int old_id = old_children[i]->id();
181 if (new_child_id_set.find(old_id) == new_child_id_set.end())
182 DestroyNodeAndSubtree(old_children[i]);
183 }
184
185 return true;
186 }
187
188 bool AXTree::CreateNewChildVector(AXNode* node,
189 const std::vector<int32> new_child_ids,
190 std::vector<AXNode*>* new_children) {
191 bool success = true;
192 for (size_t i = 0; i < new_child_ids.size(); ++i) {
193 int32 child_id = new_child_ids[i];
194 int32 index_in_parent = static_cast<int32>(i);
195 AXNode* child = static_cast<AXNode*>(GetFromId(child_id));
196 if (child) {
197 if (child->parent() != node) {
198 // This is a serious error - nodes should never be reparented.
199 // If this case occurs, continue so this node isn't left in an
200 // inconsistent state, but return failure at the end.
201 success = false;
202 continue;
203 }
204 child->UpdateIndexInParent(index_in_parent);
205 } else {
206 child = CreateAndInitializeNode(node, child_id, index_in_parent);
207 }
208 new_children->push_back(child);
209 }
210
211 return success;
212 }
213
214 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698