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

Side by Side Diff: ui/accessibility/ax_tree_serializer.h

Issue 90853002: Make tree serialization more robust and add more unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rename update0 to unused_update Created 7 years 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
« no previous file with comments | « ui/accessibility/ax_tree.cc ('k') | ui/accessibility/ax_tree_serializer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_ 5 #ifndef UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_
6 #define UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_ 6 #define UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_
7 7
8 #include <set> 8 #include <set>
9 9
10 #include "base/containers/hash_tables.h" 10 #include "base/containers/hash_tables.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/stl_util.h"
12 #include "ui/accessibility/ax_tree_source.h" 13 #include "ui/accessibility/ax_tree_source.h"
13 #include "ui/accessibility/ax_tree_update.h" 14 #include "ui/accessibility/ax_tree_update.h"
14 15
15 namespace ui { 16 namespace ui {
16 17
17 struct ClientTreeNode; 18 struct ClientTreeNode;
18 19
19 // AXTreeSerializer is a helper class that serializes incremental 20 // AXTreeSerializer is a helper class that serializes incremental
20 // updates to an AXTreeSource as a vector of AXNodeData structs. 21 // updates to an AXTreeSource as a AXTreeUpdate struct.
21 // These structs can be unserialized by an AXTree. An AXTreeSerializer 22 // These structs can be unserialized by a client object such as an
22 // keeps track of the tree of node ids that its client is aware of, so 23 // AXTree. An AXTreeSerializer keeps track of the tree of node ids that its
23 // it will automatically include, as part of any update, any additional nodes 24 // client is aware of so that it will never generate an AXTreeUpdate that
24 // that the client is not aware of yet. 25 // results in an invalid tree.
25 // 26 //
26 // When the AXTreeSource changes, call SerializeChanges to serialize the 27 // Every node in the source tree must have an id that's a unique positive
27 // changes to the tree as an AXTreeUpdate. If a single node has changed, 28 // integer, the same node must not appear twice.
28 // pass that node to SerializeChanges. If a node has been added or removed,
29 // pass the parent of that node to SerializeChanges and it will automatically
30 // handle changes to its set of children.
31 // 29 //
32 // TODO(dmazzoni): add sample usage code. 30 // Usage:
31 //
32 // You must call SerializeChanges() every time a node in the tree changes,
33 // and send the generated AXTreeUpdate to the client.
34 //
35 // If a node is added, call SerializeChanges on its parent.
36 // If a node is removed, call SerializeChanges on its parent.
37 // If a whole new subtree is added, just call SerializeChanges on its root.
38 // If the root of the tree changes, call SerializeChanges on the new root.
39 //
40 // AXTreeSerializer will avoid re-serializing nodes that do not change.
41 // For example, if node 1 has children 2, 3, 4, 5 and then child 2 is
42 // removed and a new child 6 is added, the AXTreeSerializer will only
43 // update nodes 1 and 6 (and any children of node 6 recursively). It will
44 // assume that nodes 3, 4, and 5 are not modified unless you explicitly
45 // call SerializeChanges() on them.
46 //
47 // As long as the source tree has unique ids for every node and no loops,
48 // and as long as every update is applied to the client tree, AXTreeSerializer
49 // will continue to work. If the source tree makes a change but fails to
50 // call SerializeChanges properly, the trees may get out of sync - but
51 // because AXTreeSerializer always keeps track of what updates it's sent,
52 // it will never send an invalid update and the client tree will not break,
53 // it just may not contain all of the changes.
33 template<class AXSourceNode> 54 template<class AXSourceNode>
34 class AXTreeSerializer { 55 class AXTreeSerializer {
35 public: 56 public:
36 explicit AXTreeSerializer(AXTreeSource<AXSourceNode>* tree); 57 explicit AXTreeSerializer(AXTreeSource<AXSourceNode>* tree);
58 ~AXTreeSerializer();
37 59
38 // Throw out the internal state that keeps track of the nodes the client 60 // Throw out the internal state that keeps track of the nodes the client
39 // knows about. This has the effect that the next update will send the 61 // knows about. This has the effect that the next update will send the
40 // entire tree over because it assumes the client knows nothing. 62 // entire tree over because it assumes the client knows nothing.
41 void Reset(); 63 void Reset();
42 64
43 // Serialize all changes to |node| and append them to |out_update|. 65 // Serialize all changes to |node| and append them to |out_update|.
44 void SerializeChanges(const AXSourceNode* node, 66 void SerializeChanges(const AXSourceNode* node,
45 AXTreeUpdate* out_update); 67 AXTreeUpdate* out_update);
46 68
69 // Only for unit testing. Normally this class relies on getting a call
70 // to SerializeChanges() every time the source tree changes. For unit
71 // testing, it's convenient to create a static AXTree for the initial
72 // state and then call ChangeTreeSourceForTesting and then SerializeChanges
73 // to simulate the changes you'd get if a tree changed from the initial
74 // state to the second tree's state.
75 void ChangeTreeSourceForTesting(AXTreeSource<AXSourceNode>* new_tree);
76
47 private: 77 private:
78 // Return the least common ancestor of a node in the source tree
79 // and a node in the client tree, or NULL if there is no such node.
80 // The least common ancestor is the closest ancestor to |node| (which
81 // may be |node| itself) that's in both the source tree and client tree,
82 // and for which both the source and client tree agree on their ancestor
83 // chain up to the root.
84 //
85 // Example 1:
86 //
87 // Client Tree Source tree
88 // 1 1
89 // / \ / \
90 // 2 3 2 4
91 //
92 // LCA(source node 2, client node 2) is node 2.
93 // LCA(source node 3, client node 4) is node 1.
94 //
95 // Example 2:
96 //
97 // Client Tree Source tree
98 // 1 1
99 // / \ / \
100 // 2 3 2 3
101 // / \ / /
102 // 4 7 8 4
103 // / \ / \
104 // 5 6 5 6
105 //
106 // LCA(source node 8, client node 7) is node 2.
107 // LCA(source node 5, client node 5) is node 1.
108 // It's not node 5, because the two trees disagree on the parent of
109 // node 4, so the LCA is the first ancestor both trees agree on.
110 const AXSourceNode* LeastCommonAncestor(const AXSourceNode* node,
111 ClientTreeNode* client_node);
112
113 // Return the least common ancestor of |node| that's in the client tree.
114 // This just walks up the ancestors of |node| until it finds a node that's
115 // also in the client tree, and then calls LeastCommonAncestor on the
116 // source node and client node.
117 const AXSourceNode* LeastCommonAncestor(const AXSourceNode* node);
118
119 // Walk the subtree rooted at |node| and return true if any nodes that
120 // would be updated are being reparented. If so, update |lca| to point
121 // to the least common ancestor of the previous LCA and the previous
122 // parent of the node being reparented.
123 bool AnyDescendantWasReparented(const AXSourceNode* node,
124 const AXSourceNode** lca);
125
126 ClientTreeNode* ClientTreeNodeById(int32 id);
127
48 // Delete the given client tree node and recursively delete all of its 128 // Delete the given client tree node and recursively delete all of its
49 // descendants. 129 // descendants.
50 void DeleteClientSubtree(ClientTreeNode* client_node); 130 void DeleteClientSubtree(ClientTreeNode* client_node);
51 131
132 // Helper function, called recursively with each new node to serialize.
52 void SerializeChangedNodes(const AXSourceNode* node, 133 void SerializeChangedNodes(const AXSourceNode* node,
53 std::set<int>* ids_serialized,
54 AXTreeUpdate* out_update); 134 AXTreeUpdate* out_update);
55 135
56 // The tree source. 136 // The tree source.
57 AXTreeSource<AXSourceNode>* tree_; 137 AXTreeSource<AXSourceNode>* tree_;
58 138
59 // Our representation of the client tree. 139 // Our representation of the client tree.
60 ClientTreeNode* client_root_; 140 ClientTreeNode* client_root_;
61 141
62 // A map from IDs to nodes in the client tree. 142 // A map from IDs to nodes in the client tree.
63 base::hash_map<int32, ClientTreeNode*> client_id_map_; 143 base::hash_map<int32, ClientTreeNode*> client_id_map_;
(...skipping 11 matching lines...) Expand all
75 }; 155 };
76 156
77 template<class AXSourceNode> 157 template<class AXSourceNode>
78 AXTreeSerializer<AXSourceNode>::AXTreeSerializer( 158 AXTreeSerializer<AXSourceNode>::AXTreeSerializer(
79 AXTreeSource<AXSourceNode>* tree) 159 AXTreeSource<AXSourceNode>* tree)
80 : tree_(tree), 160 : tree_(tree),
81 client_root_(NULL) { 161 client_root_(NULL) {
82 } 162 }
83 163
84 template<class AXSourceNode> 164 template<class AXSourceNode>
165 AXTreeSerializer<AXSourceNode>::~AXTreeSerializer() {
166 Reset();
167 }
168
169 template<class AXSourceNode>
85 void AXTreeSerializer<AXSourceNode>::Reset() { 170 void AXTreeSerializer<AXSourceNode>::Reset() {
86 if (client_root_) { 171 if (client_root_) {
87 DeleteClientSubtree(client_root_); 172 DeleteClientSubtree(client_root_);
88 client_root_ = NULL; 173 client_root_ = NULL;
89 } 174 }
90 } 175 }
91 176
92 template<class AXSourceNode> 177 template<class AXSourceNode>
178 void AXTreeSerializer<AXSourceNode>::ChangeTreeSourceForTesting(
179 AXTreeSource<AXSourceNode>* new_tree) {
180 tree_ = new_tree;
181 }
182
183 template<class AXSourceNode>
184 const AXSourceNode* AXTreeSerializer<AXSourceNode>::LeastCommonAncestor(
185 const AXSourceNode* node, ClientTreeNode* client_node) {
186 if (node == NULL || client_node == NULL)
187 return NULL;
188
189 std::vector<const AXSourceNode*> ancestors;
190 while (node) {
191 ancestors.push_back(node);
192 node = tree_->GetParent(node);
193 }
194
195 std::vector<ClientTreeNode*> client_ancestors;
196 while (client_node) {
197 client_ancestors.push_back(client_node);
198 client_node = client_node->parent;
199 }
200
201 // Start at the root. Keep going until the source ancestor chain and
202 // client ancestor chain disagree. The last node before they disagree
203 // is the LCA.
204 const AXSourceNode* lca = NULL;
205 int source_index = static_cast<int>(ancestors.size() - 1);
206 int client_index = static_cast<int>(client_ancestors.size() - 1);
207 while (source_index >= 0 && client_index >= 0) {
208 if (tree_->GetId(ancestors[source_index]) !=
209 client_ancestors[client_index]->id) {
210 return lca;
211 }
212 lca = ancestors[source_index];
213 source_index--;
214 client_index--;
215 }
216 return lca;
217 }
218
219 template<class AXSourceNode>
220 const AXSourceNode* AXTreeSerializer<AXSourceNode>::LeastCommonAncestor(
221 const AXSourceNode* node) {
222 // Walk up the tree until the source node's id also exists in the
223 // client tree, then call LeastCommonAncestor on those two nodes.
224 ClientTreeNode* client_node = ClientTreeNodeById(tree_->GetId(node));
225 while (node && !client_node) {
226 node = tree_->GetParent(node);
227 if (node)
228 client_node = ClientTreeNodeById(tree_->GetId(node));
229 }
230 return LeastCommonAncestor(node, client_node);
231 }
232
233 template<class AXSourceNode>
234 bool AXTreeSerializer<AXSourceNode>::AnyDescendantWasReparented(
235 const AXSourceNode* node, const AXSourceNode** lca) {
236 bool result = false;
237 int id = tree_->GetId(node);
238 int child_count = tree_->GetChildCount(node);
239 for (int i = 0; i < child_count; ++i) {
240 const AXSourceNode* child = tree_->GetChildAtIndex(node, i);
241 int child_id = tree_->GetId(child);
242 ClientTreeNode* client_child = ClientTreeNodeById(child_id);
243 if (client_child) {
244 if (!client_child->parent) {
245 // If the client child has no parent, it must have been the
246 // previous root node, so there is no LCA and we can exit early.
247 *lca = NULL;
248 return true;
249 } else if (client_child->parent->id != id) {
250 // If the client child's parent is not this node, update the LCA
251 // and return true (reparenting was found).
252 *lca = LeastCommonAncestor(*lca, client_child);
253 result = true;
254 } else {
255 // This child is already in the client tree, we won't
256 // recursively serialize it so we don't need to check this
257 // subtree recursively for reparenting.
258 continue;
259 }
260 }
261
262 // This is a new child or reparented child, check it recursively.
263 if (AnyDescendantWasReparented(child, lca))
264 result = true;
265 }
266 return result;
267 }
268
269 template<class AXSourceNode>
270 ClientTreeNode* AXTreeSerializer<AXSourceNode>::ClientTreeNodeById(int32 id) {
271 base::hash_map<int32, ClientTreeNode*>::iterator iter =
272 client_id_map_.find(id);
273 if (iter != client_id_map_.end())
274 return iter->second;
275 else
276 return NULL;
277 }
278
279 template<class AXSourceNode>
93 void AXTreeSerializer<AXSourceNode>::SerializeChanges( 280 void AXTreeSerializer<AXSourceNode>::SerializeChanges(
94 const AXSourceNode* node, 281 const AXSourceNode* node,
95 AXTreeUpdate* out_update) { 282 AXTreeUpdate* out_update) {
96 std::set<int> ids_serialized; 283 // If the node isn't in the client tree, we need to serialize starting
97 SerializeChangedNodes(node, &ids_serialized, out_update); 284 // with the LCA.
285 const AXSourceNode* lca = LeastCommonAncestor(node);
286
287 if (client_root_) {
288 // If the LCA is anything other than the node itself, tell the
289 // client to first delete the subtree rooted at the LCA.
290 bool need_delete = (lca != node);
291 if (lca) {
292 // Check for any reparenting within this subtree - if there is
293 // any, we need to delete and reserialize the whole subtree
294 // that contains the old and new parents of the reparented node.
295 if (AnyDescendantWasReparented(lca, &lca))
296 need_delete = true;
297 }
298
299 if (lca == NULL) {
300 // If there's no LCA, just tell the client to destroy the whole
301 // tree and then we'll serialize everything from the new root.
302 out_update->node_id_to_clear = client_root_->id;
303 DeleteClientSubtree(client_root_);
304 client_id_map_.erase(client_root_->id);
305 client_root_ = NULL;
306 } else if (need_delete) {
307 // Otherwise, if we need to reserialize a subtree, first we need
308 // to delete those nodes in our client tree so that
309 // SerializeChangedNodes() will be sure to send them again.
310 out_update->node_id_to_clear = tree_->GetId(lca);
311 ClientTreeNode* client_lca = ClientTreeNodeById(tree_->GetId(lca));
312 CHECK(client_lca);
313 for (size_t i = 0; i < client_lca->children.size(); ++i) {
314 client_id_map_.erase(client_lca->children[i]->id);
315 DeleteClientSubtree(client_lca->children[i]);
316 }
317 client_lca->children.clear();
318 }
319 }
320
321 // Serialize from the LCA, or from the root if there isn't one.
322 if (!lca)
323 lca = tree_->GetRoot();
324 SerializeChangedNodes(lca, out_update);
98 } 325 }
99 326
100 template<class AXSourceNode> 327 template<class AXSourceNode>
101 void AXTreeSerializer<AXSourceNode>::DeleteClientSubtree( 328 void AXTreeSerializer<AXSourceNode>::DeleteClientSubtree(
102 ClientTreeNode* client_node) { 329 ClientTreeNode* client_node) {
103 for (size_t i = 0; i < client_node->children.size(); ++i) { 330 for (size_t i = 0; i < client_node->children.size(); ++i) {
104 client_id_map_.erase(client_node->children[i]->id); 331 client_id_map_.erase(client_node->children[i]->id);
105 DeleteClientSubtree(client_node->children[i]); 332 DeleteClientSubtree(client_node->children[i]);
106 } 333 }
107 client_node->children.clear(); 334 client_node->children.clear();
108 } 335 }
109 336
110 template<class AXSourceNode> 337 template<class AXSourceNode>
111 void AXTreeSerializer<AXSourceNode>::SerializeChangedNodes( 338 void AXTreeSerializer<AXSourceNode>::SerializeChangedNodes(
112 const AXSourceNode* node, 339 const AXSourceNode* node,
113 std::set<int>* ids_serialized,
114 AXTreeUpdate* out_update) { 340 AXTreeUpdate* out_update) {
115 int id = tree_->GetId(node);
116 if (ids_serialized->find(id) != ids_serialized->end())
117 return;
118 ids_serialized->insert(id);
119
120 // This method has three responsibilities: 341 // This method has three responsibilities:
121 // 1. Serialize |node| into an AXNodeData, and append it to 342 // 1. Serialize |node| into an AXNodeData, and append it to
122 // the AXTreeUpdate to be sent to the client. 343 // the AXTreeUpdate to be sent to the client.
123 // 2. Determine if |node| has any new children that the client doesn't 344 // 2. Determine if |node| has any new children that the client doesn't
124 // know about yet, and call SerializeChangedNodes recursively on those. 345 // know about yet, and call SerializeChangedNodes recursively on those.
125 // 3. Update our internal data structure that keeps track of what nodes 346 // 3. Update our internal data structure that keeps track of what nodes
126 // the client knows about. 347 // the client knows about.
127 348
128 // First, find the ClientTreeNode for this id in our data structure where 349 // First, find the ClientTreeNode for this id in our data structure where
129 // we keep track of what accessibility objects the client already knows 350 // we keep track of what accessibility objects the client already knows
130 // about. If we don't find it, then this must be the new root of the 351 // about. If we don't find it, then this must be the new root of the
131 // accessibility tree. 352 // accessibility tree.
132 // 353 int id = tree_->GetId(node);
133 // TODO(dmazzoni): handle the case where the root changes. 354 ClientTreeNode* client_node = ClientTreeNodeById(id);
134 ClientTreeNode* client_node = NULL; 355 if (!client_node) {
135 base::hash_map<int32, ClientTreeNode*>::iterator iter =
136 client_id_map_.find(id);
137 if (iter != client_id_map_.end()) {
138 client_node = iter->second;
139 } else {
140 if (client_root_) { 356 if (client_root_) {
141 client_id_map_.erase(client_root_->id); 357 client_id_map_.erase(client_root_->id);
142 DeleteClientSubtree(client_root_); 358 DeleteClientSubtree(client_root_);
143 } 359 }
144 client_root_ = new ClientTreeNode(); 360 client_root_ = new ClientTreeNode();
145 client_node = client_root_; 361 client_node = client_root_;
146 client_node->id = id; 362 client_node->id = id;
147 client_node->parent = NULL; 363 client_node->parent = NULL;
148 client_id_map_[client_node->id] = client_node; 364 client_id_map_[client_node->id] = client_node;
149 } 365 }
150 366
151 // Iterate over the ids of the children of |node|. 367 // Iterate over the ids of the children of |node|.
152 // Create a set of the child ids so we can quickly look 368 // Create a set of the child ids so we can quickly look
153 // up which children are new and which ones were there before. 369 // up which children are new and which ones were there before.
154 // Also catch the case where a child is already in the client tree
155 // data structure with a different parent, and make sure the old parent
156 // clears this node first.
157 base::hash_set<int32> new_child_ids; 370 base::hash_set<int32> new_child_ids;
158 int child_count = tree_->GetChildCount(node); 371 int child_count = tree_->GetChildCount(node);
159 for (int i = 0; i < child_count; ++i) { 372 for (int i = 0; i < child_count; ++i) {
160 AXSourceNode* child = tree_->GetChildAtIndex(node, i); 373 AXSourceNode* child = tree_->GetChildAtIndex(node, i);
161 int new_child_id = tree_->GetId(child); 374 int new_child_id = tree_->GetId(child);
162 new_child_ids.insert(new_child_id); 375 new_child_ids.insert(new_child_id);
163 376
377 // This is a sanity check - there shouldn't be any reparenting
378 // because we've already handled it above.
164 ClientTreeNode* client_child = client_id_map_[new_child_id]; 379 ClientTreeNode* client_child = client_id_map_[new_child_id];
165 if (client_child && client_child->parent != client_node) { 380 CHECK(!client_child || client_child->parent == client_node);
166 // The child is being reparented. Find the source tree node
167 // corresponding to the old parent, or the closest ancestor
168 // still in the tree.
169 ClientTreeNode* client_parent = client_child->parent;
170 AXSourceNode* parent = NULL;
171 while (client_parent) {
172 parent = tree_->GetFromId(client_parent->id);
173 if (parent)
174 break;
175 client_parent = client_parent->parent;
176 }
177 CHECK(parent);
178
179 // Call SerializeChangedNodes recursively on the old parent,
180 // so that the update that clears |child| from its old parent
181 // occurs stricly before the update that adds |child| to its
182 // new parent.
183 SerializeChangedNodes(parent, ids_serialized, out_update);
184 }
185 } 381 }
186 382
187 // Go through the old children and delete subtrees for child 383 // Go through the old children and delete subtrees for child
188 // ids that are no longer present, and create a map from 384 // ids that are no longer present, and create a map from
189 // id to ClientTreeNode for the rest. It's important to delete 385 // id to ClientTreeNode for the rest. It's important to delete
190 // first in a separate pass so that nodes that are reparented 386 // first in a separate pass so that nodes that are reparented
191 // don't end up children of two different parents in the middle 387 // don't end up children of two different parents in the middle
192 // of an update, which can lead to a double-free. 388 // of an update, which can lead to a double-free.
193 base::hash_map<int32, ClientTreeNode*> client_child_id_map; 389 base::hash_map<int32, ClientTreeNode*> client_child_id_map;
194 std::vector<ClientTreeNode*> old_children; 390 std::vector<ClientTreeNode*> old_children;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 new_child->id = child_id; 433 new_child->id = child_id;
238 new_child->parent = client_node; 434 new_child->parent = client_node;
239 client_node->children.push_back(new_child); 435 client_node->children.push_back(new_child);
240 client_id_map_[child_id] = new_child; 436 client_id_map_[child_id] = new_child;
241 children_to_serialize.push_back(child); 437 children_to_serialize.push_back(child);
242 } 438 }
243 } 439 }
244 440
245 // Serialize all of the new children, recursively. 441 // Serialize all of the new children, recursively.
246 for (size_t i = 0; i < children_to_serialize.size(); ++i) 442 for (size_t i = 0; i < children_to_serialize.size(); ++i)
247 SerializeChangedNodes(children_to_serialize[i], ids_serialized, out_update); 443 SerializeChangedNodes(children_to_serialize[i], out_update);
248 } 444 }
249 445
250 } // namespace ui 446 } // namespace ui
251 447
252 #endif // UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_ 448 #endif // UI_ACCESSIBILITY_AX_TREE_SERIALIZER_H_
OLDNEW
« no previous file with comments | « ui/accessibility/ax_tree.cc ('k') | ui/accessibility/ax_tree_serializer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698