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

Side by Side 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: Lazy initialize ALL_ATTRIBUTES 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "content/browser/accessibility/accessibility_tree_formatter.h" 5 #include "content/browser/accessibility/accessibility_tree_formatter.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
12 #include "content/browser/accessibility/browser_accessibility_manager.h" 13 #include "content/browser/accessibility/browser_accessibility_manager.h"
13 #include "content/port/browser/render_widget_host_view_port.h" 14 #include "content/port/browser/render_widget_host_view_port.h"
14 #include "content/public/browser/render_view_host.h" 15 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h" 16 #include "content/public/browser/web_contents.h"
16 17
17 namespace content { 18 namespace content {
18 namespace { 19 namespace {
19 const int kIndentSpaces = 4; 20 const int kIndentSpaces = 4;
20 const char* kSkipString = "@NO_DUMP"; 21 const char* kSkipString = "@NO_DUMP";
22 const char* kChildrenDictAttr = "children";
21 } 23 }
22 24
23 AccessibilityTreeFormatter::AccessibilityTreeFormatter( 25 AccessibilityTreeFormatter::AccessibilityTreeFormatter(
24 BrowserAccessibility* node) 26 BrowserAccessibility* root)
25 : node_(node) { 27 : root_(root) {
26 Initialize(); 28 Initialize();
27 } 29 }
28 30
29 // static 31 // static
30 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create( 32 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create(
31 RenderViewHost* rvh) { 33 RenderViewHost* rvh) {
32 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( 34 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>(
33 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView()); 35 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView());
34 36
35 BrowserAccessibilityManager* manager = 37 BrowserAccessibilityManager* manager =
36 host_view->GetBrowserAccessibilityManager(); 38 host_view->GetBrowserAccessibilityManager();
37 if (!manager) 39 if (!manager)
38 return NULL; 40 return NULL;
39 41
40 BrowserAccessibility* root = manager->GetRoot(); 42 BrowserAccessibility* root = manager->GetRoot();
41 return new AccessibilityTreeFormatter(root); 43 return new AccessibilityTreeFormatter(root);
42 } 44 }
43 45
44 46
45 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() { 47 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() {
46 } 48 }
47 49
50 scoped_ptr<DictionaryValue>
51 AccessibilityTreeFormatter::BuildAccessibilityTree() {
52 scoped_ptr<DictionaryValue> dict(new DictionaryValue);
53 RecursiveBuildAccessibilityTree(*root_, dict.get());
54 return dict.Pass();
55 }
56
48 void AccessibilityTreeFormatter::FormatAccessibilityTree( 57 void AccessibilityTreeFormatter::FormatAccessibilityTree(
49 string16* contents) { 58 string16* contents) {
50 RecursiveFormatAccessibilityTree(node_, contents, 0); 59 scoped_ptr<DictionaryValue> dict = BuildAccessibilityTree();
60 RecursiveFormatAccessibilityTree(*(dict.get()), contents);
61 }
62
63 void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree(
64 const BrowserAccessibility& node, DictionaryValue* dict) {
65 AddProperties(node, dict);
66 ListValue* children = new ListValue;
67 dict->Set(kChildrenDictAttr, children);
68 for (size_t i = 0; i < node.children().size(); ++i) {
69 BrowserAccessibility* child_node = node.children()[i];
70 DictionaryValue* child_dict = new DictionaryValue;
71 children->Append(child_dict);
72 RecursiveBuildAccessibilityTree(*child_node, child_dict);
73 }
51 } 74 }
52 75
53 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( 76 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree(
54 BrowserAccessibility* node, string16* contents, int indent) { 77 const DictionaryValue& dict, string16* contents, int depth) {
55 scoped_ptr<char[]> prefix(new char[indent + 1]); 78 string16 line = ToString(dict, string16(depth * kIndentSpaces, ' '));
56 for (int i = 0; i < indent; ++i)
57 prefix[i] = ' ';
58 prefix[indent] = '\0';
59
60 string16 line = ToString(node, prefix.get());
61 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) 79 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos)
62 return; 80 return;
63 81
64 *contents += line; 82 *contents += line;
65 for (size_t i = 0; i < node->children().size(); ++i) { 83 const ListValue* children;
66 RecursiveFormatAccessibilityTree(node->children()[i], contents, 84 dict.GetList(kChildrenDictAttr, &children);
67 indent + kIndentSpaces); 85 const DictionaryValue* child_dict;
86 for (size_t i = 0; i < children->GetSize(); i++) {
87 children->GetDictionary(i, &child_dict);
88 RecursiveFormatAccessibilityTree(*child_dict, contents, depth + 1);
68 } 89 }
69 } 90 }
70 91
71 #if (!defined(OS_WIN) && !defined(OS_MACOSX)) 92 #if (!defined(OS_WIN) && !defined(OS_MACOSX))
72 string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node, 93 void AccessibilityTreeFormatter::AddProperties(const BrowserAccessibility& node,
73 char* prefix) { 94 DictionaryValue* dict) {
74 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) + 95 dict->SetInteger("id", node.renderer_id());
96 }
97
98 string16 AccessibilityTreeFormatter::ToString(const DictionaryValue& node,
99 const string16& indent) {
100 int id_value;
101 node.GetInteger("id", &id_value);
102 return indent + base::IntToString16(id_value) +
75 ASCIIToUTF16("\n"); 103 ASCIIToUTF16("\n");
76 } 104 }
77 105
78 void AccessibilityTreeFormatter::Initialize() {} 106 void AccessibilityTreeFormatter::Initialize() {}
79 107
80 // static 108 // static
81 const base::FilePath::StringType 109 const base::FilePath::StringType
82 AccessibilityTreeFormatter::GetActualFileSuffix() { 110 AccessibilityTreeFormatter::GetActualFileSuffix() {
83 return base::FilePath::StringType(); 111 return base::FilePath::StringType();
84 } 112 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 allow = true; 148 allow = true;
121 else if (iter->type == Filter::ALLOW) 149 else if (iter->type == Filter::ALLOW)
122 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); 150 allow = (!MatchPattern(text, UTF8ToUTF16("*=''")));
123 else 151 else
124 allow = false; 152 allow = false;
125 } 153 }
126 } 154 }
127 return allow; 155 return allow;
128 } 156 }
129 157
130 void AccessibilityTreeFormatter::StartLine() { 158 string16 AccessibilityTreeFormatter::FormatCoordinates(
131 line_.clear(); 159 const char* name, const char* x_name, const char* y_name,
160 const DictionaryValue& value) {
161 int x, y;
162 value.GetInteger(x_name, &x);
163 value.GetInteger(y_name, &y);
164 std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y));
165
166 return UTF8ToUTF16(xy_str);
132 } 167 }
133 168
134 void AccessibilityTreeFormatter::Add( 169 void AccessibilityTreeFormatter::WriteAttribute(
135 bool include_by_default, const string16& attr) { 170 bool include_by_default, const std::string& attr, string16* line) {
171 WriteAttribute(include_by_default, UTF8ToUTF16(attr), line);
172 }
173
174 void AccessibilityTreeFormatter::WriteAttribute(
175 bool include_by_default, const string16& attr, string16* line) {
136 if (attr.empty()) 176 if (attr.empty())
137 return; 177 return;
138 if (!MatchesFilters(attr, include_by_default)) 178 if (!MatchesFilters(attr, include_by_default))
139 return; 179 return;
140 if (!line_.empty()) 180 if (!line->empty())
141 line_ += ASCIIToUTF16(" "); 181 *line += ASCIIToUTF16(" ");
142 line_ += attr; 182 *line += attr;
143 }
144
145 string16 AccessibilityTreeFormatter::FinishLine() {
146 return line_;
147 } 183 }
148 184
149 } // namespace content 185 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698