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

Unified Diff: content/browser/accessibility/accessibility_tree_formatter.cc

Issue 13479003: Modify AccessibilityTreeFormatter to build up an internal representation of the tree (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 7 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/accessibility/accessibility_tree_formatter.cc
diff --git a/content/browser/accessibility/accessibility_tree_formatter.cc b/content/browser/accessibility/accessibility_tree_formatter.cc
index 3904bc0d83bbec92675e22b5aaf50bb2a93422da..84f200aeed536ee9fbf881abae459500bd66e1b9 100644
--- a/content/browser/accessibility/accessibility_tree_formatter.cc
+++ b/content/browser/accessibility/accessibility_tree_formatter.cc
@@ -7,6 +7,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_util.h"
+#include "base/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "content/browser/accessibility/browser_accessibility_manager.h"
@@ -18,11 +19,12 @@ namespace content {
namespace {
const int kIndentSpaces = 4;
const char* kSkipString = "@NO_DUMP";
+const char* kChildrenDictAttr = "children";
}
AccessibilityTreeFormatter::AccessibilityTreeFormatter(
- BrowserAccessibility* node)
- : node_(node) {
+ BrowserAccessibility* root)
+ : root_(root) {
Initialize();
}
@@ -45,33 +47,59 @@ AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create(
AccessibilityTreeFormatter::~AccessibilityTreeFormatter() {
}
+scoped_ptr<DictionaryValue>
+AccessibilityTreeFormatter::BuildAccessibilityTree() {
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue);
+ RecursiveBuildAccessibilityTree(*root_, dict.get());
+ return dict.Pass();
+}
+
void AccessibilityTreeFormatter::FormatAccessibilityTree(
string16* contents) {
- RecursiveFormatAccessibilityTree(node_, contents, 0);
+ scoped_ptr<DictionaryValue> dict = BuildAccessibilityTree();
+ RecursiveFormatAccessibilityTree(*(dict.get()), contents);
+}
+
+void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree(
+ const BrowserAccessibility& node, DictionaryValue* dict) {
+ AddProperties(node, dict);
+ ListValue* children = new ListValue;
+ dict->Set(kChildrenDictAttr, children);
+ for (size_t i = 0; i < node.children().size(); ++i) {
+ BrowserAccessibility* child_node = node.children()[i];
+ DictionaryValue* child_dict = new DictionaryValue;
+ children->Append(child_dict);
+ RecursiveBuildAccessibilityTree(*child_node, child_dict);
+ }
}
void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree(
- BrowserAccessibility* node, string16* contents, int indent) {
- scoped_ptr<char[]> prefix(new char[indent + 1]);
- for (int i = 0; i < indent; ++i)
- prefix[i] = ' ';
- prefix[indent] = '\0';
-
- string16 line = ToString(node, prefix.get());
+ const DictionaryValue& dict, string16* contents, int depth) {
+ string16 line = ToString(dict, string16(depth * kIndentSpaces, ' '));
if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos)
return;
*contents += line;
- for (size_t i = 0; i < node->children().size(); ++i) {
- RecursiveFormatAccessibilityTree(node->children()[i], contents,
- indent + kIndentSpaces);
+ const ListValue* children;
+ dict.GetList(kChildrenDictAttr, &children);
+ const DictionaryValue* child_dict;
+ for (size_t i = 0; i < children->GetSize(); i++) {
+ children->GetDictionary(i, &child_dict);
+ RecursiveFormatAccessibilityTree(*child_dict, contents, depth + 1);
}
}
#if (!defined(OS_WIN) && !defined(OS_MACOSX))
-string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node,
- char* prefix) {
- return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) +
+void AccessibilityTreeFormatter::AddProperties(const BrowserAccessibility& node,
+ DictionaryValue* dict) {
+ dict->SetInteger("id", node.renderer_id());
+}
+
+string16 AccessibilityTreeFormatter::ToString(const DictionaryValue& node,
+ const string16& indent) {
+ int id_value;
+ node.GetInteger("id", &id_value);
+ return indent + base::IntToString16(id_value) +
ASCIIToUTF16("\n");
}
@@ -127,23 +155,31 @@ bool AccessibilityTreeFormatter::MatchesFilters(
return allow;
}
-void AccessibilityTreeFormatter::StartLine() {
- line_.clear();
+string16 AccessibilityTreeFormatter::FormatCoordinates(
+ const char* name, const char* x_name, const char* y_name,
+ const DictionaryValue& value) {
+ int x, y;
+ value.GetInteger(x_name, &x);
+ value.GetInteger(y_name, &y);
+ std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y));
+
+ return UTF8ToUTF16(xy_str);
+}
+
+void AccessibilityTreeFormatter::WriteAttribute(
+ bool include_by_default, const std::string& attr, string16* line) {
+ WriteAttribute(include_by_default, UTF8ToUTF16(attr), line);
}
-void AccessibilityTreeFormatter::Add(
- bool include_by_default, const string16& attr) {
+void AccessibilityTreeFormatter::WriteAttribute(
+ bool include_by_default, const string16& attr, string16* line) {
if (attr.empty())
return;
if (!MatchesFilters(attr, include_by_default))
return;
- if (!line_.empty())
- line_ += ASCIIToUTF16(" ");
- line_ += attr;
-}
-
-string16 AccessibilityTreeFormatter::FinishLine() {
- return line_;
+ if (!line->empty())
+ *line += ASCIIToUTF16(" ");
+ *line += attr;
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698