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/dump_accessibility_tree_helper.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/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "content/browser/accessibility/browser_accessibility_manager.h" | |
12 #include "content/port/browser/render_widget_host_view_port.h" | |
13 #include "content/public/browser/render_view_host.h" | |
14 #include "content/public/browser/web_contents.h" | |
11 | 15 |
12 namespace content { | 16 namespace content { |
13 namespace { | 17 namespace { |
14 const int kIndentSpaces = 4; | 18 const int kIndentSpaces = 4; |
15 const char* kSkipString = "@NO_DUMP"; | 19 const char* kSkipString = "@NO_DUMP"; |
16 } | 20 } |
17 | 21 |
18 DumpAccessibilityTreeHelper::DumpAccessibilityTreeHelper() { | 22 AccessibilityTreeFormatter::AccessibilityTreeFormatter( |
23 BrowserAccessibility* node) | |
24 : node_(node) { | |
19 Initialize(); | 25 Initialize(); |
20 } | 26 } |
21 | 27 |
22 DumpAccessibilityTreeHelper::~DumpAccessibilityTreeHelper() { | 28 // static |
29 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create( | |
30 RenderViewHost* rvh) { | |
31 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( | |
32 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView()); | |
33 | |
34 content::BrowserAccessibilityManager* manager = | |
jam
2013/03/04 04:03:39
nit: here and below, no "content::"
aboxhall
2013/03/04 17:36:11
Done.
| |
35 host_view->GetBrowserAccessibilityManager(); | |
36 if (!manager) | |
37 return NULL; | |
38 | |
39 content::BrowserAccessibility* root = manager->GetRoot(); | |
40 return new AccessibilityTreeFormatter(root); | |
23 } | 41 } |
24 | 42 |
25 void DumpAccessibilityTreeHelper::DumpAccessibilityTree( | 43 |
26 BrowserAccessibility* node, string16* contents) { | 44 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() { |
27 RecursiveDumpAccessibilityTree(node, contents, 0); | |
28 } | 45 } |
29 | 46 |
30 void DumpAccessibilityTreeHelper::RecursiveDumpAccessibilityTree( | 47 void AccessibilityTreeFormatter::FormatAccessibilityTree( |
48 string16* contents) { | |
49 RecursiveFormatAccessibilityTree(node_, contents, 0); | |
50 } | |
51 | |
52 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( | |
31 BrowserAccessibility* node, string16* contents, int indent) { | 53 BrowserAccessibility* node, string16* contents, int indent) { |
32 scoped_array<char> prefix(new char[indent + 1]); | 54 scoped_array<char> prefix(new char[indent + 1]); |
33 for (int i = 0; i < indent; ++i) | 55 for (int i = 0; i < indent; ++i) |
34 prefix[i] = ' '; | 56 prefix[i] = ' '; |
35 prefix[indent] = '\0'; | 57 prefix[indent] = '\0'; |
36 | 58 |
37 string16 line = ToString(node, prefix.get()); | 59 string16 line = ToString(node, prefix.get()); |
38 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) | 60 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) |
39 return; | 61 return; |
40 | 62 |
41 *contents += line; | 63 *contents += line; |
42 for (size_t i = 0; i < node->children().size(); ++i) { | 64 for (size_t i = 0; i < node->children().size(); ++i) { |
43 RecursiveDumpAccessibilityTree(node->children()[i], contents, | 65 RecursiveFormatAccessibilityTree(node->children()[i], contents, |
44 indent + kIndentSpaces); | 66 indent + kIndentSpaces); |
45 } | 67 } |
46 } | 68 } |
47 | 69 |
48 void DumpAccessibilityTreeHelper::SetFilters( | 70 void AccessibilityTreeFormatter::SetFilters( |
49 const std::vector<Filter>& filters) { | 71 const std::vector<Filter>& filters) { |
50 filters_ = filters; | 72 filters_ = filters; |
51 } | 73 } |
52 | 74 |
53 bool DumpAccessibilityTreeHelper::MatchesFilters( | 75 bool AccessibilityTreeFormatter::MatchesFilters( |
54 const string16& text, bool default_result) { | 76 const string16& text, bool default_result) const { |
55 std::vector<Filter>::const_iterator iter = filters_.begin(); | 77 std::vector<Filter>::const_iterator iter = filters_.begin(); |
56 bool allow = default_result; | 78 bool allow = default_result; |
57 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { | 79 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { |
58 if (MatchPattern(text, iter->match_str)) { | 80 if (MatchPattern(text, iter->match_str)) { |
59 if (iter->type == Filter::ALLOW_EMPTY) | 81 if (iter->type == Filter::ALLOW_EMPTY) |
60 allow = true; | 82 allow = true; |
61 else if (iter->type == Filter::ALLOW) | 83 else if (iter->type == Filter::ALLOW) |
62 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); | 84 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); |
63 else | 85 else |
64 allow = false; | 86 allow = false; |
65 } | 87 } |
66 } | 88 } |
67 return allow; | 89 return allow; |
68 } | 90 } |
69 | 91 |
70 void DumpAccessibilityTreeHelper::StartLine() { | 92 void AccessibilityTreeFormatter::StartLine() { |
71 line_.clear(); | 93 line_.clear(); |
72 } | 94 } |
73 | 95 |
74 void DumpAccessibilityTreeHelper::Add( | 96 void AccessibilityTreeFormatter::Add( |
75 bool include_by_default, const string16& attr) { | 97 bool include_by_default, const string16& attr) { |
76 if (attr.empty()) | 98 if (attr.empty()) |
77 return; | 99 return; |
78 if (!MatchesFilters(attr, include_by_default)) | 100 if (!MatchesFilters(attr, include_by_default)) |
79 return; | 101 return; |
80 if (!line_.empty()) | 102 if (!line_.empty()) |
81 line_ += ASCIIToUTF16(" "); | 103 line_ += ASCIIToUTF16(" "); |
82 line_ += attr; | 104 line_ += attr; |
83 } | 105 } |
84 | 106 |
85 string16 DumpAccessibilityTreeHelper::FinishLine() { | 107 string16 AccessibilityTreeFormatter::FinishLine() { |
86 return line_; | 108 return line_; |
87 } | 109 } |
88 | 110 |
89 } // namespace content | 111 } // namespace content |
OLD | NEW |