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

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: Code cleanup 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/json/json_writer.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "base/string_util.h" 10 #include "base/string_util.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";
21 } 22 }
22 23
23 AccessibilityTreeFormatter::AccessibilityTreeFormatter( 24 AccessibilityTreeFormatter::AccessibilityTreeFormatter(
24 BrowserAccessibility* node) 25 BrowserAccessibility* root)
25 : node_(node) { 26 : root_(root) {
26 Initialize(); 27 Initialize();
27 } 28 }
28 29
29 // static 30 // static
30 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create( 31 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create(
31 RenderViewHost* rvh) { 32 RenderViewHost* rvh) {
32 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( 33 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>(
33 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView()); 34 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView());
34 35
35 BrowserAccessibilityManager* manager = 36 BrowserAccessibilityManager* manager =
36 host_view->GetBrowserAccessibilityManager(); 37 host_view->GetBrowserAccessibilityManager();
37 if (!manager) 38 if (!manager)
38 return NULL; 39 return NULL;
39 40
40 BrowserAccessibility* root = manager->GetRoot(); 41 BrowserAccessibility* root = manager->GetRoot();
41 return new AccessibilityTreeFormatter(root); 42 return new AccessibilityTreeFormatter(root);
42 } 43 }
43 44
44 45
45 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() { 46 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() {
46 } 47 }
47 48
49 void AccessibilityTreeFormatter::BuildAccessibilityTree(
50 DictionaryValue* dict) {
51 RecursiveBuildAccessibilityTree(root_, dict);
52 }
53
48 void AccessibilityTreeFormatter::FormatAccessibilityTree( 54 void AccessibilityTreeFormatter::FormatAccessibilityTree(
49 string16* contents) { 55 string16* contents) {
50 RecursiveFormatAccessibilityTree(node_, contents, 0); 56 DictionaryValue dict;
57 BuildAccessibilityTree(&dict);
58 RecursiveFormatAccessibilityTree(&dict, contents);
59 }
60
61 void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree(
62 BrowserAccessibility* node, DictionaryValue* dict) {
63 AddProperties(node, dict);
64 ListValue* children = new ListValue;
65 dict->Set("children", children);
dmazzoni 2013/04/08 05:46:35 Put dict field names in a constant at the top of t
aboxhall 2013/04/10 03:09:34 Done (there was actually only this one).
66 for (size_t i = 0; i < node->children().size(); ++i) {
67 BrowserAccessibility* child_node = node->children()[i];
68 DictionaryValue* child_dict = new DictionaryValue;
69 children->Append(child_dict);
70 RecursiveBuildAccessibilityTree(child_node, child_dict);
71 }
51 } 72 }
52 73
53 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( 74 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree(
54 BrowserAccessibility* node, string16* contents, int indent) { 75 DictionaryValue* dict, string16* contents, int depth) {
55 scoped_array<char> prefix(new char[indent + 1]); 76 string16 line = ToString(dict, string16(depth * kIndentSpaces, ' '));
dmazzoni 2013/04/08 05:46:35 Thanks - I didn't realize you could construct a st
aboxhall 2013/04/10 03:09:34 Hehe - I was thinking "surely there's a more effic
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) 77 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos)
62 return; 78 return;
63 79
64 *contents += line; 80 *contents += line;
65 for (size_t i = 0; i < node->children().size(); ++i) { 81 ListValue* children;
66 RecursiveFormatAccessibilityTree(node->children()[i], contents, 82 dict->GetList("children", &children);
dmazzoni 2013/04/08 05:46:35 Make sure this uses the constant!
aboxhall 2013/04/10 03:09:34 Done.
aboxhall 2013/04/10 03:09:34 Done.
67 indent + kIndentSpaces); 83 DictionaryValue* child_dict;
84 for (size_t i = 0; i < children->GetSize(); i++) {
85 children->GetDictionary(i, &child_dict);
86 RecursiveFormatAccessibilityTree(child_dict, contents, depth + 1);
68 } 87 }
69 } 88 }
70 89
71 #if (!defined(OS_WIN) && !defined(OS_MACOSX)) 90 #if (!defined(OS_WIN) && !defined(OS_MACOSX))
72 string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node, 91 string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node,
73 char* prefix) { 92 char* prefix) {
74 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) + 93 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) +
75 ASCIIToUTF16("\n"); 94 ASCIIToUTF16("\n");
76 } 95 }
77 96
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 allow = true; 139 allow = true;
121 else if (iter->type == Filter::ALLOW) 140 else if (iter->type == Filter::ALLOW)
122 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); 141 allow = (!MatchPattern(text, UTF8ToUTF16("*=''")));
123 else 142 else
124 allow = false; 143 allow = false;
125 } 144 }
126 } 145 }
127 return allow; 146 return allow;
128 } 147 }
129 148
130 void AccessibilityTreeFormatter::StartLine() { 149 void AccessibilityTreeFormatter::WriteAttribute(
131 line_.clear(); 150 bool include_by_default, const string16& attr, string16* line) {
132 }
133
134 void AccessibilityTreeFormatter::Add(
135 bool include_by_default, const string16& attr) {
136 if (attr.empty()) 151 if (attr.empty())
137 return; 152 return;
138 if (!MatchesFilters(attr, include_by_default)) 153 if (!MatchesFilters(attr, include_by_default))
139 return; 154 return;
140 if (!line_.empty()) 155 if (!line->empty())
141 line_ += ASCIIToUTF16(" "); 156 *line += ASCIIToUTF16(" ");
142 line_ += attr; 157 *line += attr;
143 }
144
145 string16 AccessibilityTreeFormatter::FinishLine() {
146 return line_;
147 } 158 }
148 159
149 } // namespace content 160 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698