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