OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/accessibility/accessibility_tree_formatter_impl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_util.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" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 const int kIndentSpaces = 4; |
| 19 const char* kSkipString = "@NO_DUMP"; |
| 20 } |
| 21 |
| 22 AccessibilityTreeFormatterImpl::AccessibilityTreeFormatterImpl( |
| 23 BrowserAccessibility* node) |
| 24 : node_(node) { |
| 25 Initialize(); |
| 26 } |
| 27 |
| 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 = |
| 35 host_view->GetBrowserAccessibilityManager(); |
| 36 if (!manager) |
| 37 return NULL; |
| 38 |
| 39 content::BrowserAccessibility* root = manager->GetRoot(); |
| 40 return new AccessibilityTreeFormatterImpl(root); |
| 41 } |
| 42 |
| 43 |
| 44 AccessibilityTreeFormatterImpl::~AccessibilityTreeFormatterImpl() { |
| 45 } |
| 46 |
| 47 void AccessibilityTreeFormatterImpl::FormatAccessibilityTree( |
| 48 string16* contents) { |
| 49 RecursiveFormatAccessibilityTree(node_, contents, 0); |
| 50 } |
| 51 |
| 52 void AccessibilityTreeFormatterImpl::RecursiveFormatAccessibilityTree( |
| 53 BrowserAccessibility* node, string16* contents, int indent) { |
| 54 scoped_array<char> prefix(new char[indent + 1]); |
| 55 for (int i = 0; i < indent; ++i) |
| 56 prefix[i] = ' '; |
| 57 prefix[indent] = '\0'; |
| 58 |
| 59 string16 line = ToString(node, prefix.get()); |
| 60 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) |
| 61 return; |
| 62 |
| 63 *contents += line; |
| 64 for (size_t i = 0; i < node->children().size(); ++i) { |
| 65 RecursiveFormatAccessibilityTree(node->children()[i], contents, |
| 66 indent + kIndentSpaces); |
| 67 } |
| 68 } |
| 69 |
| 70 void AccessibilityTreeFormatterImpl::SetFilters( |
| 71 const std::vector<Filter>& filters) { |
| 72 filters_ = filters; |
| 73 } |
| 74 |
| 75 bool AccessibilityTreeFormatterImpl::MatchesFilters( |
| 76 const string16& text, bool default_result) const { |
| 77 std::vector<Filter>::const_iterator iter = filters_.begin(); |
| 78 bool allow = default_result; |
| 79 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { |
| 80 if (MatchPattern(text, iter->match_str)) { |
| 81 if (iter->type == Filter::ALLOW_EMPTY) |
| 82 allow = true; |
| 83 else if (iter->type == Filter::ALLOW) |
| 84 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); |
| 85 else |
| 86 allow = false; |
| 87 } |
| 88 } |
| 89 return allow; |
| 90 } |
| 91 |
| 92 void AccessibilityTreeFormatterImpl::StartLine() { |
| 93 line_.clear(); |
| 94 } |
| 95 |
| 96 void AccessibilityTreeFormatterImpl::Add( |
| 97 bool include_by_default, const string16& attr) { |
| 98 if (attr.empty()) |
| 99 return; |
| 100 if (!MatchesFilters(attr, include_by_default)) |
| 101 return; |
| 102 if (!line_.empty()) |
| 103 line_ += ASCIIToUTF16(" "); |
| 104 line_ += attr; |
| 105 } |
| 106 |
| 107 string16 AccessibilityTreeFormatterImpl::FinishLine() { |
| 108 return line_; |
| 109 } |
| 110 |
| 111 } // namespace content |
OLD | NEW |