OLD | NEW |
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 string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node, |
73 char* prefix) { | 94 char* prefix) { |
74 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) + | 95 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) + |
75 ASCIIToUTF16("\n"); | 96 ASCIIToUTF16("\n"); |
76 } | 97 } |
77 | 98 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 allow = true; | 141 allow = true; |
121 else if (iter->type == Filter::ALLOW) | 142 else if (iter->type == Filter::ALLOW) |
122 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); | 143 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); |
123 else | 144 else |
124 allow = false; | 145 allow = false; |
125 } | 146 } |
126 } | 147 } |
127 return allow; | 148 return allow; |
128 } | 149 } |
129 | 150 |
130 void AccessibilityTreeFormatter::StartLine() { | 151 string16 AccessibilityTreeFormatter::FormatCoordinates( |
131 line_.clear(); | 152 const char* name, const char* x_name, const char* y_name, |
| 153 const DictionaryValue& value) { |
| 154 int x, y; |
| 155 value.GetInteger(x_name, &x); |
| 156 value.GetInteger(y_name, &y); |
| 157 std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y)); |
| 158 |
| 159 return UTF8ToUTF16(xy_str); |
132 } | 160 } |
133 | 161 |
134 void AccessibilityTreeFormatter::Add( | 162 void AccessibilityTreeFormatter::WriteAttribute( |
135 bool include_by_default, const string16& attr) { | 163 bool include_by_default, const std::string& attr, string16* line) { |
| 164 WriteAttribute(include_by_default, UTF8ToUTF16(attr), line); |
| 165 } |
| 166 |
| 167 void AccessibilityTreeFormatter::WriteAttribute( |
| 168 bool include_by_default, const string16& attr, string16* line) { |
136 if (attr.empty()) | 169 if (attr.empty()) |
137 return; | 170 return; |
138 if (!MatchesFilters(attr, include_by_default)) | 171 if (!MatchesFilters(attr, include_by_default)) |
139 return; | 172 return; |
140 if (!line_.empty()) | 173 if (!line->empty()) |
141 line_ += ASCIIToUTF16(" "); | 174 *line += ASCIIToUTF16(" "); |
142 line_ += attr; | 175 *line += attr; |
143 } | |
144 | |
145 string16 AccessibilityTreeFormatter::FinishLine() { | |
146 return line_; | |
147 } | 176 } |
148 | 177 |
149 } // namespace content | 178 } // namespace content |
OLD | NEW |