Move MatchPattern to its own header and the base namespace.
[chromium-blink-merge.git] / content / browser / accessibility / accessibility_tree_formatter.cc
blobeeb2f0afecab9a733602ce14ffe7a5b4595a68ba
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.
5 #include "content/browser/accessibility/accessibility_tree_formatter.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/pattern.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/browser/accessibility/browser_accessibility_manager.h"
15 #include "content/browser/renderer_host/render_widget_host_view_base.h"
16 #include "content/browser/web_contents/web_contents_impl.h"
17 #include "content/public/browser/web_contents.h"
19 namespace content {
20 namespace {
21 const char kIndentSymbol = '+';
22 const int kIndentSymbolCount = 2;
23 const char* kSkipString = "@NO_DUMP";
24 const char* kSkipChildren = "@NO_CHILDREN_DUMP";
25 const char* kChildrenDictAttr = "children";
28 AccessibilityTreeFormatter::AccessibilityTreeFormatter(
29 BrowserAccessibility* root)
30 : root_(root),
31 show_ids_(false) {
32 Initialize();
35 // static
36 AccessibilityTreeFormatter* AccessibilityTreeFormatter::Create(
37 WebContents* web_contents) {
38 BrowserAccessibilityManager* manager =
39 static_cast<WebContentsImpl*>(web_contents)->
40 GetRootBrowserAccessibilityManager();
41 if (!manager)
42 return NULL;
44 BrowserAccessibility* root = manager->GetRoot();
45 return new AccessibilityTreeFormatter(root);
49 AccessibilityTreeFormatter::~AccessibilityTreeFormatter() {
52 scoped_ptr<base::DictionaryValue>
53 AccessibilityTreeFormatter::BuildAccessibilityTree() {
54 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
55 RecursiveBuildAccessibilityTree(*root_, dict.get());
56 return dict.Pass();
59 void AccessibilityTreeFormatter::FormatAccessibilityTree(
60 base::string16* contents) {
61 scoped_ptr<base::DictionaryValue> dict = BuildAccessibilityTree();
62 RecursiveFormatAccessibilityTree(*(dict.get()), contents);
65 void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree(
66 const BrowserAccessibility& node, base::DictionaryValue* dict) {
67 AddProperties(node, dict);
69 base::ListValue* children = new base::ListValue;
70 dict->Set(kChildrenDictAttr, children);
72 for (size_t i = 0; i < node.PlatformChildCount(); ++i) {
73 BrowserAccessibility* child_node = node.PlatformGetChild(i);
74 base::DictionaryValue* child_dict = new base::DictionaryValue;
75 children->Append(child_dict);
76 RecursiveBuildAccessibilityTree(*child_node, child_dict);
80 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree(
81 const base::DictionaryValue& dict, base::string16* contents, int depth) {
82 base::string16 indent = base::string16(depth * kIndentSymbolCount,
83 kIndentSymbol);
84 base::string16 line = indent + ToString(dict);
85 if (line.find(base::ASCIIToUTF16(kSkipString)) != base::string16::npos)
86 return;
88 *contents += line + base::ASCIIToUTF16("\n");
89 if (line.find(base::ASCIIToUTF16(kSkipChildren)) != base::string16::npos)
90 return;
92 const base::ListValue* children;
93 dict.GetList(kChildrenDictAttr, &children);
94 const base::DictionaryValue* child_dict;
95 for (size_t i = 0; i < children->GetSize(); i++) {
96 children->GetDictionary(i, &child_dict);
97 RecursiveFormatAccessibilityTree(*child_dict, contents, depth + 1);
101 #if (!defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_ANDROID))
102 void AccessibilityTreeFormatter::AddProperties(const BrowserAccessibility& node,
103 base::DictionaryValue* dict) {
104 dict->SetInteger("id", node.GetId());
107 base::string16 AccessibilityTreeFormatter::ToString(
108 const base::DictionaryValue& node) {
109 int id_value;
110 node.GetInteger("id", &id_value);
111 return base::IntToString16(id_value);
114 void AccessibilityTreeFormatter::Initialize() {}
116 // static
117 const base::FilePath::StringType
118 AccessibilityTreeFormatter::GetActualFileSuffix() {
119 return base::FilePath::StringType();
122 // static
123 const base::FilePath::StringType
124 AccessibilityTreeFormatter::GetExpectedFileSuffix() {
125 return base::FilePath::StringType();
128 // static
129 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() {
130 return std::string();
133 // static
134 const std::string AccessibilityTreeFormatter::GetAllowString() {
135 return std::string();
138 // static
139 const std::string AccessibilityTreeFormatter::GetDenyString() {
140 return std::string();
142 #endif
144 void AccessibilityTreeFormatter::SetFilters(
145 const std::vector<Filter>& filters) {
146 filters_ = filters;
149 // static
150 bool AccessibilityTreeFormatter::MatchesFilters(
151 const std::vector<Filter>& filters,
152 const base::string16& text,
153 bool default_result) {
154 std::vector<Filter>::const_iterator iter = filters.begin();
155 bool allow = default_result;
156 for (iter = filters.begin(); iter != filters.end(); ++iter) {
157 if (base::MatchPattern(text, iter->match_str)) {
158 if (iter->type == Filter::ALLOW_EMPTY)
159 allow = true;
160 else if (iter->type == Filter::ALLOW)
161 allow = (!base::MatchPattern(text, base::UTF8ToUTF16("*=''")));
162 else
163 allow = false;
166 return allow;
169 bool AccessibilityTreeFormatter::MatchesFilters(
170 const base::string16& text, bool default_result) const {
171 return MatchesFilters(filters_, text, default_result);
174 base::string16 AccessibilityTreeFormatter::FormatCoordinates(
175 const char* name, const char* x_name, const char* y_name,
176 const base::DictionaryValue& value) {
177 int x, y;
178 value.GetInteger(x_name, &x);
179 value.GetInteger(y_name, &y);
180 std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y));
182 return base::UTF8ToUTF16(xy_str);
185 void AccessibilityTreeFormatter::WriteAttribute(
186 bool include_by_default, const std::string& attr, base::string16* line) {
187 WriteAttribute(include_by_default, base::UTF8ToUTF16(attr), line);
190 void AccessibilityTreeFormatter::WriteAttribute(
191 bool include_by_default, const base::string16& attr, base::string16* line) {
192 if (attr.empty())
193 return;
194 if (!MatchesFilters(attr, include_by_default))
195 return;
196 if (!line->empty())
197 *line += base::ASCIIToUTF16(" ");
198 *line += attr;
201 } // namespace content