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.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "content/browser/accessibility/browser_accessibility_manager.h" |
| 13 #include "content/port/browser/render_widget_host_view_port.h" |
| 14 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 |
| 17 namespace content { |
| 18 namespace { |
| 19 const int kIndentSpaces = 4; |
| 20 const char* kSkipString = "@NO_DUMP"; |
| 21 } |
| 22 |
| 23 AccessibilityTreeFormatter::AccessibilityTreeFormatter( |
| 24 BrowserAccessibility* node) |
| 25 : node_(node) { |
| 26 Initialize(); |
| 27 } |
| 28 |
| 29 // static |
| 30 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create( |
| 31 RenderViewHost* rvh) { |
| 32 RenderWidgetHostViewPort* host_view = static_cast<RenderWidgetHostViewPort*>( |
| 33 WebContents::FromRenderViewHost(rvh)->GetRenderWidgetHostView()); |
| 34 |
| 35 BrowserAccessibilityManager* manager = |
| 36 host_view->GetBrowserAccessibilityManager(); |
| 37 if (!manager) |
| 38 return NULL; |
| 39 |
| 40 BrowserAccessibility* root = manager->GetRoot(); |
| 41 return new AccessibilityTreeFormatter(root); |
| 42 } |
| 43 |
| 44 |
| 45 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() { |
| 46 } |
| 47 |
| 48 void AccessibilityTreeFormatter::FormatAccessibilityTree( |
| 49 string16* contents) { |
| 50 RecursiveFormatAccessibilityTree(node_, contents, 0); |
| 51 } |
| 52 |
| 53 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( |
| 54 BrowserAccessibility* node, string16* contents, int indent) { |
| 55 scoped_array<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()); |
| 61 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) |
| 62 return; |
| 63 |
| 64 *contents += line; |
| 65 for (size_t i = 0; i < node->children().size(); ++i) { |
| 66 RecursiveFormatAccessibilityTree(node->children()[i], contents, |
| 67 indent + kIndentSpaces); |
| 68 } |
| 69 } |
| 70 |
| 71 #if (!defined(OS_WIN) && !defined(OS_MACOSX)) || defined(USE_AURA) |
| 72 string16 AccessibilityTreeFormatter::ToString(BrowserAccessibility* node, |
| 73 char* prefix) { |
| 74 return UTF8ToUTF16(prefix) + base::IntToString16(node->renderer_id()) + |
| 75 ASCIIToUTF16("\n"); |
| 76 } |
| 77 |
| 78 void AccessibilityTreeFormatter::Initialize() {} |
| 79 |
| 80 // static |
| 81 const base::FilePath::StringType |
| 82 AccessibilityTreeFormatter::GetActualFileSuffix() { |
| 83 return FILE_PATH_LITERAL(""); |
| 84 } |
| 85 |
| 86 // static |
| 87 const base::FilePath::StringType |
| 88 AccessibilityTreeFormatter::GetExpectedFileSuffix() { |
| 89 return FILE_PATH_LITERAL(""); |
| 90 } |
| 91 |
| 92 // static |
| 93 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() { |
| 94 return ""; |
| 95 } |
| 96 |
| 97 // static |
| 98 const std::string AccessibilityTreeFormatter::GetAllowString() { |
| 99 return ""; |
| 100 } |
| 101 |
| 102 // static |
| 103 const std::string AccessibilityTreeFormatter::GetDenyString() { |
| 104 return ""; |
| 105 } |
| 106 #endif |
| 107 |
| 108 void AccessibilityTreeFormatter::SetFilters( |
| 109 const std::vector<Filter>& filters) { |
| 110 filters_ = filters; |
| 111 } |
| 112 |
| 113 bool AccessibilityTreeFormatter::MatchesFilters( |
| 114 const string16& text, bool default_result) const { |
| 115 std::vector<Filter>::const_iterator iter = filters_.begin(); |
| 116 bool allow = default_result; |
| 117 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { |
| 118 if (MatchPattern(text, iter->match_str)) { |
| 119 if (iter->type == Filter::ALLOW_EMPTY) |
| 120 allow = true; |
| 121 else if (iter->type == Filter::ALLOW) |
| 122 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); |
| 123 else |
| 124 allow = false; |
| 125 } |
| 126 } |
| 127 return allow; |
| 128 } |
| 129 |
| 130 void AccessibilityTreeFormatter::StartLine() { |
| 131 line_.clear(); |
| 132 } |
| 133 |
| 134 void AccessibilityTreeFormatter::Add( |
| 135 bool include_by_default, const string16& attr) { |
| 136 if (attr.empty()) |
| 137 return; |
| 138 if (!MatchesFilters(attr, include_by_default)) |
| 139 return; |
| 140 if (!line_.empty()) |
| 141 line_ += ASCIIToUTF16(" "); |
| 142 line_ += attr; |
| 143 } |
| 144 |
| 145 string16 AccessibilityTreeFormatter::FinishLine() { |
| 146 return line_; |
| 147 } |
| 148 |
| 149 } // namespace content |
OLD | NEW |