| 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/accessibility_tree_formatter.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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 } | 48 } |
| 49 | 49 |
| 50 scoped_ptr<base::DictionaryValue> | 50 scoped_ptr<base::DictionaryValue> |
| 51 AccessibilityTreeFormatter::BuildAccessibilityTree() { | 51 AccessibilityTreeFormatter::BuildAccessibilityTree() { |
| 52 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 52 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 53 RecursiveBuildAccessibilityTree(*root_, dict.get()); | 53 RecursiveBuildAccessibilityTree(*root_, dict.get()); |
| 54 return dict.Pass(); | 54 return dict.Pass(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void AccessibilityTreeFormatter::FormatAccessibilityTree( | 57 void AccessibilityTreeFormatter::FormatAccessibilityTree( |
| 58 string16* contents) { | 58 base::string16* contents) { |
| 59 scoped_ptr<base::DictionaryValue> dict = BuildAccessibilityTree(); | 59 scoped_ptr<base::DictionaryValue> dict = BuildAccessibilityTree(); |
| 60 RecursiveFormatAccessibilityTree(*(dict.get()), contents); | 60 RecursiveFormatAccessibilityTree(*(dict.get()), contents); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree( | 63 void AccessibilityTreeFormatter::RecursiveBuildAccessibilityTree( |
| 64 const BrowserAccessibility& node, base::DictionaryValue* dict) { | 64 const BrowserAccessibility& node, base::DictionaryValue* dict) { |
| 65 AddProperties(node, dict); | 65 AddProperties(node, dict); |
| 66 | 66 |
| 67 base::ListValue* children = new base::ListValue; | 67 base::ListValue* children = new base::ListValue; |
| 68 dict->Set(kChildrenDictAttr, children); | 68 dict->Set(kChildrenDictAttr, children); |
| 69 | 69 |
| 70 for (size_t i = 0; i < node.PlatformChildCount(); ++i) { | 70 for (size_t i = 0; i < node.PlatformChildCount(); ++i) { |
| 71 BrowserAccessibility* child_node = node.children()[i]; | 71 BrowserAccessibility* child_node = node.children()[i]; |
| 72 base::DictionaryValue* child_dict = new base::DictionaryValue; | 72 base::DictionaryValue* child_dict = new base::DictionaryValue; |
| 73 children->Append(child_dict); | 73 children->Append(child_dict); |
| 74 RecursiveBuildAccessibilityTree(*child_node, child_dict); | 74 RecursiveBuildAccessibilityTree(*child_node, child_dict); |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( | 78 void AccessibilityTreeFormatter::RecursiveFormatAccessibilityTree( |
| 79 const base::DictionaryValue& dict, string16* contents, int depth) { | 79 const base::DictionaryValue& dict, base::string16* contents, int depth) { |
| 80 string16 line = ToString(dict, string16(depth * kIndentSpaces, ' ')); | 80 base::string16 line = |
| 81 if (line.find(ASCIIToUTF16(kSkipString)) != string16::npos) | 81 ToString(dict, base::string16(depth * kIndentSpaces, ' ')); |
| 82 if (line.find(ASCIIToUTF16(kSkipString)) != base::string16::npos) |
| 82 return; | 83 return; |
| 83 | 84 |
| 84 *contents += line; | 85 *contents += line; |
| 85 const base::ListValue* children; | 86 const base::ListValue* children; |
| 86 dict.GetList(kChildrenDictAttr, &children); | 87 dict.GetList(kChildrenDictAttr, &children); |
| 87 const base::DictionaryValue* child_dict; | 88 const base::DictionaryValue* child_dict; |
| 88 for (size_t i = 0; i < children->GetSize(); i++) { | 89 for (size_t i = 0; i < children->GetSize(); i++) { |
| 89 children->GetDictionary(i, &child_dict); | 90 children->GetDictionary(i, &child_dict); |
| 90 RecursiveFormatAccessibilityTree(*child_dict, contents, depth + 1); | 91 RecursiveFormatAccessibilityTree(*child_dict, contents, depth + 1); |
| 91 } | 92 } |
| 92 } | 93 } |
| 93 | 94 |
| 94 #if (!defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ | 95 #if (!defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
| 95 !defined(TOOLKIT_GTK)) | 96 !defined(TOOLKIT_GTK)) |
| 96 void AccessibilityTreeFormatter::AddProperties(const BrowserAccessibility& node, | 97 void AccessibilityTreeFormatter::AddProperties(const BrowserAccessibility& node, |
| 97 base::DictionaryValue* dict) { | 98 base::DictionaryValue* dict) { |
| 98 dict->SetInteger("id", node.renderer_id()); | 99 dict->SetInteger("id", node.renderer_id()); |
| 99 } | 100 } |
| 100 | 101 |
| 101 string16 AccessibilityTreeFormatter::ToString(const base::DictionaryValue& node, | 102 base::string16 AccessibilityTreeFormatter::ToString( |
| 102 const string16& indent) { | 103 const base::DictionaryValue& node, |
| 104 const base::string16& indent) { |
| 103 int id_value; | 105 int id_value; |
| 104 node.GetInteger("id", &id_value); | 106 node.GetInteger("id", &id_value); |
| 105 return indent + base::IntToString16(id_value) + | 107 return indent + base::IntToString16(id_value) + |
| 106 ASCIIToUTF16("\n"); | 108 ASCIIToUTF16("\n"); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void AccessibilityTreeFormatter::Initialize() {} | 111 void AccessibilityTreeFormatter::Initialize() {} |
| 110 | 112 |
| 111 // static | 113 // static |
| 112 const base::FilePath::StringType | 114 const base::FilePath::StringType |
| (...skipping 22 matching lines...) Expand all Loading... |
| 135 return std::string(); | 137 return std::string(); |
| 136 } | 138 } |
| 137 #endif | 139 #endif |
| 138 | 140 |
| 139 void AccessibilityTreeFormatter::SetFilters( | 141 void AccessibilityTreeFormatter::SetFilters( |
| 140 const std::vector<Filter>& filters) { | 142 const std::vector<Filter>& filters) { |
| 141 filters_ = filters; | 143 filters_ = filters; |
| 142 } | 144 } |
| 143 | 145 |
| 144 bool AccessibilityTreeFormatter::MatchesFilters( | 146 bool AccessibilityTreeFormatter::MatchesFilters( |
| 145 const string16& text, bool default_result) const { | 147 const base::string16& text, bool default_result) const { |
| 146 std::vector<Filter>::const_iterator iter = filters_.begin(); | 148 std::vector<Filter>::const_iterator iter = filters_.begin(); |
| 147 bool allow = default_result; | 149 bool allow = default_result; |
| 148 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { | 150 for (iter = filters_.begin(); iter != filters_.end(); ++iter) { |
| 149 if (MatchPattern(text, iter->match_str)) { | 151 if (MatchPattern(text, iter->match_str)) { |
| 150 if (iter->type == Filter::ALLOW_EMPTY) | 152 if (iter->type == Filter::ALLOW_EMPTY) |
| 151 allow = true; | 153 allow = true; |
| 152 else if (iter->type == Filter::ALLOW) | 154 else if (iter->type == Filter::ALLOW) |
| 153 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); | 155 allow = (!MatchPattern(text, UTF8ToUTF16("*=''"))); |
| 154 else | 156 else |
| 155 allow = false; | 157 allow = false; |
| 156 } | 158 } |
| 157 } | 159 } |
| 158 return allow; | 160 return allow; |
| 159 } | 161 } |
| 160 | 162 |
| 161 string16 AccessibilityTreeFormatter::FormatCoordinates( | 163 base::string16 AccessibilityTreeFormatter::FormatCoordinates( |
| 162 const char* name, const char* x_name, const char* y_name, | 164 const char* name, const char* x_name, const char* y_name, |
| 163 const base::DictionaryValue& value) { | 165 const base::DictionaryValue& value) { |
| 164 int x, y; | 166 int x, y; |
| 165 value.GetInteger(x_name, &x); | 167 value.GetInteger(x_name, &x); |
| 166 value.GetInteger(y_name, &y); | 168 value.GetInteger(y_name, &y); |
| 167 std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y)); | 169 std::string xy_str(base::StringPrintf("%s=(%d, %d)", name, x, y)); |
| 168 | 170 |
| 169 return UTF8ToUTF16(xy_str); | 171 return UTF8ToUTF16(xy_str); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void AccessibilityTreeFormatter::WriteAttribute( | 174 void AccessibilityTreeFormatter::WriteAttribute( |
| 173 bool include_by_default, const std::string& attr, string16* line) { | 175 bool include_by_default, const std::string& attr, base::string16* line) { |
| 174 WriteAttribute(include_by_default, UTF8ToUTF16(attr), line); | 176 WriteAttribute(include_by_default, UTF8ToUTF16(attr), line); |
| 175 } | 177 } |
| 176 | 178 |
| 177 void AccessibilityTreeFormatter::WriteAttribute( | 179 void AccessibilityTreeFormatter::WriteAttribute( |
| 178 bool include_by_default, const string16& attr, string16* line) { | 180 bool include_by_default, const base::string16& attr, base::string16* line) { |
| 179 if (attr.empty()) | 181 if (attr.empty()) |
| 180 return; | 182 return; |
| 181 if (!MatchesFilters(attr, include_by_default)) | 183 if (!MatchesFilters(attr, include_by_default)) |
| 182 return; | 184 return; |
| 183 if (!line->empty()) | 185 if (!line->empty()) |
| 184 *line += ASCIIToUTF16(" "); | 186 *line += ASCIIToUTF16(" "); |
| 185 *line += attr; | 187 *line += attr; |
| 186 } | 188 } |
| 187 | 189 |
| 188 } // namespace content | 190 } // namespace content |
| OLD | NEW |