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