This source file includes following definitions.
- TreeToStringHelper
- root_
- root_
- SetDelegate
- GetRoot
- GetFromId
- Unserialize
- ToString
- CreateNode
- UpdateNode
- CreateAndInitializeNode
- DestroyNodeAndSubtree
- DeleteOldChildren
- CreateNewChildVector
#include "ui/accessibility/ax_tree.h"
#include <set>
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "ui/accessibility/ax_node.h"
namespace ui {
namespace {
std::string TreeToStringHelper(AXNode* node, int indent) {
std::string result;
for (int i = 0; i < indent; i++)
result += " ";
result += node->data().ToString() + "\n";
for (int i = 0; i < node->child_count(); ++i)
result += TreeToStringHelper(node->ChildAtIndex(i), indent + 1);
return result;
}
}
struct AXTreeUpdateState {
std::set<AXNode*> pending_nodes;
std::set<AXNode*> new_nodes;
};
AXTreeDelegate::AXTreeDelegate() {
}
AXTreeDelegate::~AXTreeDelegate() {
}
AXTree::AXTree()
: delegate_(NULL), root_(NULL) {
AXNodeData root;
root.id = 0;
root.role = AX_ROLE_ROOT_WEB_AREA;
AXTreeUpdate initial_state;
initial_state.nodes.push_back(root);
CHECK(Unserialize(initial_state)) << error();
}
AXTree::AXTree(const AXTreeUpdate& initial_state)
: delegate_(NULL), root_(NULL) {
CHECK(Unserialize(initial_state)) << error();
}
AXTree::~AXTree() {
if (root_)
DestroyNodeAndSubtree(root_);
}
void AXTree::SetDelegate(AXTreeDelegate* delegate) {
delegate_ = delegate;
}
AXNode* AXTree::GetRoot() const {
return root_;
}
AXNode* AXTree::GetFromId(int32 id) const {
base::hash_map<int32, AXNode*>::const_iterator iter = id_map_.find(id);
return iter != id_map_.end() ? (iter->second) : NULL;
}
bool AXTree::Unserialize(const AXTreeUpdate& update) {
AXTreeUpdateState update_state;
int32 old_root_id = root_ ? root_->id() : 0;
if (update.node_id_to_clear != 0) {
AXNode* node = GetFromId(update.node_id_to_clear);
if (!node) {
error_ = base::StringPrintf("Bad node_id_to_clear: %d",
update.node_id_to_clear);
return false;
}
if (node == root_) {
DestroyNodeAndSubtree(root_);
root_ = NULL;
} else {
for (int i = 0; i < node->child_count(); ++i)
DestroyNodeAndSubtree(node->ChildAtIndex(i));
std::vector<AXNode*> children;
node->SwapChildren(children);
update_state.pending_nodes.insert(node);
}
}
for (size_t i = 0; i < update.nodes.size(); ++i) {
if (!UpdateNode(update.nodes[i], &update_state))
return false;
}
if (!update_state.pending_nodes.empty()) {
error_ = "Nodes left pending by the update:";
for (std::set<AXNode*>::iterator iter = update_state.pending_nodes.begin();
iter != update_state.pending_nodes.end(); ++iter) {
error_ += base::StringPrintf(" %d", (*iter)->id());
}
return false;
}
if (delegate_) {
for (size_t i = 0; i < update.nodes.size(); ++i) {
AXNode* node = GetFromId(update.nodes[i].id);
if (update_state.new_nodes.find(node) != update_state.new_nodes.end()) {
delegate_->OnNodeCreated(node);
update_state.new_nodes.erase(node);
} else {
delegate_->OnNodeChanged(node);
}
}
if (root_->id() != old_root_id)
delegate_->OnRootChanged(root_);
}
return true;
}
std::string AXTree::ToString() const {
return TreeToStringHelper(root_, 0);
}
AXNode* AXTree::CreateNode(AXNode* parent, int32 id, int32 index_in_parent) {
return new AXNode(parent, id, index_in_parent);
}
bool AXTree::UpdateNode(
const AXNodeData& src, AXTreeUpdateState* update_state) {
AXNode* node = GetFromId(src.id);
if (node) {
update_state->pending_nodes.erase(node);
} else {
if (src.role != AX_ROLE_ROOT_WEB_AREA) {
error_ = base::StringPrintf(
"%d is not in the tree and not the new root", src.id);
return false;
}
node = CreateAndInitializeNode(NULL, src.id, 0);
update_state->new_nodes.insert(node);
}
node->SetData(src);
if (!DeleteOldChildren(node, src.child_ids))
return false;
std::vector<AXNode*> new_children;
bool success = CreateNewChildVector(
node, src.child_ids, &new_children, update_state);
node->SwapChildren(new_children);
if (src.role == AX_ROLE_ROOT_WEB_AREA &&
(!root_ || root_->id() != src.id)) {
if (root_)
DestroyNodeAndSubtree(root_);
root_ = node;
}
return success;
}
AXNode* AXTree::CreateAndInitializeNode(
AXNode* parent, int32 id, int32 index_in_parent) {
AXNode* node = CreateNode(parent, id, index_in_parent);
id_map_[node->id()] = node;
return node;
}
void AXTree::DestroyNodeAndSubtree(AXNode* node) {
id_map_.erase(node->id());
for (int i = 0; i < node->child_count(); ++i)
DestroyNodeAndSubtree(node->ChildAtIndex(i));
if (delegate_)
delegate_->OnNodeWillBeDeleted(node);
node->Destroy();
}
bool AXTree::DeleteOldChildren(AXNode* node,
const std::vector<int32> new_child_ids) {
std::set<int32> new_child_id_set;
for (size_t i = 0; i < new_child_ids.size(); ++i) {
if (new_child_id_set.find(new_child_ids[i]) != new_child_id_set.end()) {
error_ = base::StringPrintf("Node %d has duplicate child id %d",
node->id(), new_child_ids[i]);
return false;
}
new_child_id_set.insert(new_child_ids[i]);
}
const std::vector<AXNode*>& old_children = node->children();
for (size_t i = 0; i < old_children.size(); ++i) {
int old_id = old_children[i]->id();
if (new_child_id_set.find(old_id) == new_child_id_set.end())
DestroyNodeAndSubtree(old_children[i]);
}
return true;
}
bool AXTree::CreateNewChildVector(AXNode* node,
const std::vector<int32> new_child_ids,
std::vector<AXNode*>* new_children,
AXTreeUpdateState* update_state) {
bool success = true;
for (size_t i = 0; i < new_child_ids.size(); ++i) {
int32 child_id = new_child_ids[i];
int32 index_in_parent = static_cast<int32>(i);
AXNode* child = GetFromId(child_id);
if (child) {
if (child->parent() != node) {
error_ = base::StringPrintf(
"Node %d reparented from %d to %d",
child->id(),
child->parent() ? child->parent()->id() : 0,
node->id());
success = false;
continue;
}
child->SetIndexInParent(index_in_parent);
} else {
child = CreateAndInitializeNode(node, child_id, index_in_parent);
update_state->pending_nodes.insert(child);
update_state->new_nodes.insert(child);
}
new_children->push_back(child);
}
return success;
}
}