| 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 <oleacc.h> | 7 #include <oleacc.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h
" | 15 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h
" |
| 16 #include "content/browser/accessibility/browser_accessibility_manager.h" | 16 #include "content/browser/accessibility/browser_accessibility_manager.h" |
| 17 #include "content/browser/accessibility/browser_accessibility_win.h" | 17 #include "content/browser/accessibility/browser_accessibility_win.h" |
| 18 #include "content/common/accessibility_node_data.h" | 18 #include "content/common/accessibility_node_data.h" |
| 19 #include "third_party/iaccessible2/ia2_api_all.h" | 19 #include "third_party/iaccessible2/ia2_api_all.h" |
| 20 #include "ui/base/win/atl_module.h" | 20 #include "ui/base/win/atl_module.h" |
| 21 | 21 |
| 22 using base::StringPrintf; | |
| 23 | |
| 24 namespace content { | 22 namespace content { |
| 25 | 23 |
| 26 const char* ALL_ATTRIBUTES[] = { | |
| 27 "name", | |
| 28 "value", | |
| 29 "states", | |
| 30 "attributes", | |
| 31 "role_name", | |
| 32 "currentValue", | |
| 33 "minimumValue", | |
| 34 "maximumValue", | |
| 35 "description", | |
| 36 "default_action", | |
| 37 "keyboard_shortcut", | |
| 38 "location", | |
| 39 "size", | |
| 40 "index_in_parent", | |
| 41 "n_relations", | |
| 42 "group_level", | |
| 43 "similar_items_in_group", | |
| 44 "position_in_group", | |
| 45 "table_rows", | |
| 46 "table_columns", | |
| 47 "row_index", | |
| 48 "column_index", | |
| 49 "n_characters", | |
| 50 "caret_offset", | |
| 51 "n_selections", | |
| 52 "selection_start", | |
| 53 "selection_end" | |
| 54 }; | |
| 55 | |
| 56 void AccessibilityTreeFormatter::Initialize() { | 24 void AccessibilityTreeFormatter::Initialize() { |
| 57 ui::win::CreateATLModuleIfNeeded(); | 25 ui::win::CreateATLModuleIfNeeded(); |
| 58 } | 26 } |
| 59 | 27 |
| 60 void AccessibilityTreeFormatter::AddProperties( | 28 string16 AccessibilityTreeFormatter::ToString( |
| 61 const BrowserAccessibility& node, DictionaryValue* dict) { | 29 BrowserAccessibility* node, char* prefix) { |
| 62 BrowserAccessibilityWin* acc_obj = | 30 BrowserAccessibilityWin* acc_obj = node->ToBrowserAccessibilityWin(); |
| 63 const_cast<BrowserAccessibility*>(&node)->ToBrowserAccessibilityWin(); | |
| 64 | 31 |
| 32 // Get the computed name. |
| 65 VARIANT variant_self; | 33 VARIANT variant_self; |
| 66 variant_self.vt = VT_I4; | 34 variant_self.vt = VT_I4; |
| 67 variant_self.lVal = CHILDID_SELF; | 35 variant_self.lVal = CHILDID_SELF; |
| 68 | |
| 69 dict->SetString("role", IAccessible2RoleToString(acc_obj->ia2_role())); | |
| 70 | |
| 71 CComBSTR msaa_variant; | 36 CComBSTR msaa_variant; |
| 72 HRESULT hresult = acc_obj->get_accName(variant_self, &msaa_variant); | 37 HRESULT hresult = acc_obj->get_accName(variant_self, &msaa_variant); |
| 73 if (hresult == S_OK) | 38 string16 name; |
| 74 dict->SetString("name", msaa_variant.m_str); | 39 if (S_OK == hresult) |
| 40 name = msaa_variant.m_str; |
| 41 |
| 75 hresult = acc_obj->get_accValue(variant_self, &msaa_variant); | 42 hresult = acc_obj->get_accValue(variant_self, &msaa_variant); |
| 76 if (hresult == S_OK) | 43 string16 value; |
| 77 dict->SetString("value", msaa_variant.m_str); | 44 if (S_OK == hresult) |
| 45 value = msaa_variant.m_str; |
| 78 | 46 |
| 47 hresult = acc_obj->get_accDescription(variant_self, &msaa_variant); |
| 48 string16 description; |
| 49 if (S_OK == hresult) |
| 50 description = msaa_variant.m_str; |
| 51 |
| 52 hresult = acc_obj->get_accHelp(variant_self, &msaa_variant); |
| 53 string16 help; |
| 54 if (S_OK == hresult) |
| 55 help = msaa_variant.m_str; |
| 56 |
| 57 hresult = acc_obj->get_accDefaultAction(variant_self, &msaa_variant); |
| 58 string16 default_action; |
| 59 if (S_OK == hresult) |
| 60 default_action = msaa_variant.m_str; |
| 61 |
| 62 hresult = acc_obj->get_accKeyboardShortcut(variant_self, &msaa_variant); |
| 63 string16 keyboard_shortcut; |
| 64 if (S_OK == hresult) |
| 65 keyboard_shortcut = msaa_variant.m_str; |
| 66 |
| 67 // Get state strings. |
| 79 std::vector<string16> state_strings; | 68 std::vector<string16> state_strings; |
| 80 int32 ia_state = acc_obj->ia_state(); | 69 int32 ia_state = acc_obj->ia_state(); |
| 81 | 70 |
| 82 // Avoid flakiness: these states depend on whether the window is focused | 71 // Avoid flakiness: these states depend on whether the window is focused |
| 83 // and the position of the mouse cursor. | 72 // and the position of the mouse cursor. |
| 84 ia_state &= ~STATE_SYSTEM_HOTTRACKED; | 73 ia_state &= ~STATE_SYSTEM_HOTTRACKED; |
| 85 ia_state &= ~STATE_SYSTEM_OFFSCREEN; | 74 ia_state &= ~STATE_SYSTEM_OFFSCREEN; |
| 86 | 75 |
| 87 IAccessibleStateToStringVector(ia_state, &state_strings); | 76 IAccessibleStateToStringVector(ia_state, &state_strings); |
| 88 IAccessible2StateToStringVector(acc_obj->ia2_state(), &state_strings); | 77 IAccessible2StateToStringVector(acc_obj->ia2_state(), &state_strings); |
| 89 ListValue* states = new ListValue; | 78 |
| 79 // Get the attributes. |
| 80 const std::vector<string16>& ia2_attributes = acc_obj->ia2_attributes(); |
| 81 |
| 82 // Build the line. |
| 83 StartLine(); |
| 84 Add(true, IAccessible2RoleToString(acc_obj->ia2_role())); |
| 85 Add(true, L"name='" + name + L"'"); |
| 86 Add(false, L"value='" + value + L"'"); |
| 90 for (std::vector<string16>::const_iterator it = state_strings.begin(); | 87 for (std::vector<string16>::const_iterator it = state_strings.begin(); |
| 91 it != state_strings.end(); | 88 it != state_strings.end(); |
| 92 ++it) { | 89 ++it) { |
| 93 states->AppendString(UTF16ToUTF8(*it)); | 90 Add(false, *it); |
| 94 } | 91 } |
| 95 dict->Set("states", states); | |
| 96 | |
| 97 const std::vector<string16>& ia2_attributes = acc_obj->ia2_attributes(); | |
| 98 ListValue* attributes = new ListValue; | |
| 99 for (std::vector<string16>::const_iterator it = ia2_attributes.begin(); | 92 for (std::vector<string16>::const_iterator it = ia2_attributes.begin(); |
| 100 it != ia2_attributes.end(); | 93 it != ia2_attributes.end(); |
| 101 ++it) { | 94 ++it) { |
| 102 attributes->AppendString(UTF16ToUTF8(*it)); | 95 Add(false, *it); |
| 103 } | 96 } |
| 104 dict->Set("attributes", attributes); | 97 Add(false, L"role_name='" + acc_obj->role_name() + L"'"); |
| 105 | |
| 106 dict->SetString("role_name", acc_obj->role_name()); | |
| 107 | |
| 108 VARIANT currentValue; | 98 VARIANT currentValue; |
| 109 if (acc_obj->get_currentValue(¤tValue) == S_OK) | 99 if (acc_obj->get_currentValue(¤tValue) == S_OK) |
| 110 dict->SetDouble("currentValue", V_R8(¤tValue)); | 100 Add(false, base::StringPrintf(L"currentValue=%.2f", V_R8(¤tValue))); |
| 111 | |
| 112 VARIANT minimumValue; | 101 VARIANT minimumValue; |
| 113 if (acc_obj->get_minimumValue(&minimumValue) == S_OK) | 102 if (acc_obj->get_minimumValue(&minimumValue) == S_OK) |
| 114 dict->SetDouble("minimumValue", V_R8(&minimumValue)); | 103 Add(false, base::StringPrintf(L"minimumValue=%.2f", V_R8(&minimumValue))); |
| 115 | |
| 116 VARIANT maximumValue; | 104 VARIANT maximumValue; |
| 117 if (acc_obj->get_maximumValue(&maximumValue) == S_OK) | 105 if (acc_obj->get_maximumValue(&maximumValue) == S_OK) |
| 118 dict->SetDouble("maximumValue", V_R8(&maximumValue)); | 106 Add(false, base::StringPrintf(L"maximumValue=%.2f", V_R8(&maximumValue))); |
| 119 | 107 Add(false, L"description='" + description + L"'"); |
| 120 hresult = acc_obj->get_accDescription(variant_self, &msaa_variant); | 108 Add(false, L"default_action='" + default_action + L"'"); |
| 121 if (hresult == S_OK) | 109 Add(false, L"keyboard_shortcut='" + keyboard_shortcut + L"'"); |
| 122 dict->SetString("description", msaa_variant.m_str); | 110 BrowserAccessibility* root = node->manager()->GetRoot(); |
| 123 | |
| 124 hresult = acc_obj->get_accDefaultAction(variant_self, &msaa_variant); | |
| 125 if (hresult == S_OK) | |
| 126 dict->SetString("default_action", msaa_variant.m_str); | |
| 127 | |
| 128 hresult = acc_obj->get_accKeyboardShortcut(variant_self, &msaa_variant); | |
| 129 if (hresult == S_OK) | |
| 130 dict->SetString("keyboard_shortcut", msaa_variant.m_str); | |
| 131 | |
| 132 hresult = acc_obj->get_accHelp(variant_self, &msaa_variant); | |
| 133 if (S_OK == hresult) | |
| 134 dict->SetString("help", msaa_variant.m_str); | |
| 135 | |
| 136 BrowserAccessibility* root = node.manager()->GetRoot(); | |
| 137 LONG left, top, width, height; | 111 LONG left, top, width, height; |
| 138 LONG root_left, root_top, root_width, root_height; | 112 LONG root_left, root_top, root_width, root_height; |
| 139 if (acc_obj->accLocation(&left, &top, &width, &height, variant_self) | 113 if (S_FALSE != acc_obj->accLocation( |
| 140 != S_FALSE | 114 &left, &top, &width, &height, variant_self) && |
| 141 && root->ToBrowserAccessibilityWin()->accLocation( | 115 S_FALSE != root->ToBrowserAccessibilityWin()->accLocation( |
| 142 &root_left, &root_top, &root_width, &root_height, variant_self) | 116 &root_left, &root_top, &root_width, &root_height, variant_self)) { |
| 143 != S_FALSE) { | 117 Add(false, base::StringPrintf(L"location=(%d, %d)", |
| 144 DictionaryValue* location = new DictionaryValue; | 118 left - root_left, top - root_top)); |
| 145 location->SetInteger("x", left - root_left); | 119 Add(false, base::StringPrintf(L"size=(%d, %d)", width, height)); |
| 146 location->SetInteger("y", top - root_top); | |
| 147 dict->Set("location", location); | |
| 148 | |
| 149 DictionaryValue* size = new DictionaryValue; | |
| 150 size->SetInteger("width", width); | |
| 151 size->SetInteger("height", height); | |
| 152 dict->Set("size", size); | |
| 153 } | 120 } |
| 154 | |
| 155 LONG index_in_parent; | 121 LONG index_in_parent; |
| 156 if (acc_obj->get_indexInParent(&index_in_parent) == S_OK) | 122 if (acc_obj->get_indexInParent(&index_in_parent) == S_OK) |
| 157 dict->SetInteger("index_in_parent", index_in_parent); | 123 Add(false, base::StringPrintf(L"index_in_parent=%d", index_in_parent)); |
| 158 | |
| 159 LONG n_relations; | 124 LONG n_relations; |
| 160 if (acc_obj->get_nRelations(&n_relations) == S_OK) | 125 if (acc_obj->get_nRelations(&n_relations) == S_OK) |
| 161 dict->SetInteger("n_relations", n_relations); | 126 Add(false, base::StringPrintf(L"n_relations=%d", n_relations)); |
| 162 | |
| 163 LONG group_level, similar_items_in_group, position_in_group; | 127 LONG group_level, similar_items_in_group, position_in_group; |
| 164 if (acc_obj->get_groupPosition(&group_level, | 128 if (acc_obj->get_groupPosition(&group_level, |
| 165 &similar_items_in_group, | 129 &similar_items_in_group, |
| 166 &position_in_group) == S_OK) { | 130 &position_in_group) == S_OK) { |
| 167 dict->SetInteger("group_level", group_level); | 131 Add(false, base::StringPrintf(L"group_level=%d", group_level)); |
| 168 dict->SetInteger("similar_items_in_group", similar_items_in_group); | 132 Add(false, base::StringPrintf(L"similar_items_in_group=%d", |
| 169 dict->SetInteger("position_in_group", position_in_group); | 133 similar_items_in_group)); |
| 134 Add(false, base::StringPrintf(L"position_in_group=%d", position_in_group)); |
| 170 } | 135 } |
| 171 LONG table_rows; | 136 LONG table_rows; |
| 172 if (acc_obj->get_nRows(&table_rows) == S_OK) | 137 if (acc_obj->get_nRows(&table_rows) == S_OK) |
| 173 dict->SetInteger("table_rows", table_rows); | 138 Add(false, base::StringPrintf(L"table_rows=%d", table_rows)); |
| 174 LONG table_columns; | 139 LONG table_columns; |
| 175 if (acc_obj->get_nRows(&table_columns) == S_OK) | 140 if (acc_obj->get_nRows(&table_columns) == S_OK) |
| 176 dict->SetInteger("table_columns", table_columns); | 141 Add(false, base::StringPrintf(L"table_columns=%d", table_columns)); |
| 177 LONG row_index; | 142 LONG row_index; |
| 178 if (acc_obj->get_rowIndex(&row_index) == S_OK) | 143 if (acc_obj->get_rowIndex(&row_index) == S_OK) |
| 179 dict->SetInteger("row_index", row_index); | 144 Add(false, base::StringPrintf(L"row_index=%d", row_index)); |
| 180 LONG column_index; | 145 LONG column_index; |
| 181 if (acc_obj->get_columnIndex(&column_index) == S_OK) | 146 if (acc_obj->get_columnIndex(&column_index) == S_OK) |
| 182 dict->SetInteger("column_index", column_index); | 147 Add(false, base::StringPrintf(L"column_index=%d", column_index)); |
| 183 LONG n_characters; | 148 LONG n_characters; |
| 184 if (acc_obj->get_nCharacters(&n_characters) == S_OK) | 149 if (acc_obj->get_nCharacters(&n_characters) == S_OK) |
| 185 dict->SetInteger("n_characters", n_characters); | 150 Add(false, base::StringPrintf(L"n_characters=%d", n_characters)); |
| 186 LONG caret_offset; | 151 LONG caret_offset; |
| 187 if (acc_obj->get_caretOffset(&caret_offset) == S_OK) | 152 if (acc_obj->get_caretOffset(&caret_offset) == S_OK) |
| 188 dict->SetInteger("caret_offset", caret_offset); | 153 Add(false, base::StringPrintf(L"caret_offset=%d", caret_offset)); |
| 189 LONG n_selections; | 154 LONG n_selections; |
| 190 if (acc_obj->get_nSelections(&n_selections) == S_OK) { | 155 if (acc_obj->get_nSelections(&n_selections) == S_OK) { |
| 191 dict->SetInteger("n_selections", n_selections); | 156 Add(false, base::StringPrintf(L"n_selections=%d", n_selections)); |
| 192 if (n_selections > 0) { | 157 if (n_selections > 0) { |
| 193 LONG start, end; | 158 LONG start, end; |
| 194 if (acc_obj->get_selection(0, &start, &end) == S_OK) { | 159 if (acc_obj->get_selection(0, &start, &end) == S_OK) { |
| 195 dict->SetInteger("selection_start", start); | 160 Add(false, base::StringPrintf(L"selection_start=%d", start)); |
| 196 dict->SetInteger("selection_end", end); | 161 Add(false, base::StringPrintf(L"selection_end=%d", end)); |
| 197 } | 162 } |
| 198 } | 163 } |
| 199 } | 164 } |
| 200 } | |
| 201 | 165 |
| 202 string16 AccessibilityTreeFormatter::ToString(const DictionaryValue& dict, | 166 return UTF8ToUTF16(prefix) + FinishLine() + ASCIIToUTF16("\n"); |
| 203 const string16& indent) { | |
| 204 string16 line; | |
| 205 | |
| 206 string16 role_value; | |
| 207 dict.GetString("role", &role_value); | |
| 208 WriteAttribute(true, UTF16ToUTF8(role_value), &line); | |
| 209 | |
| 210 string16 name_value; | |
| 211 dict.GetString("name", &name_value); | |
| 212 WriteAttribute(true, base::StringPrintf(L"name='%ls'", name_value.c_str()), | |
| 213 &line); | |
| 214 | |
| 215 for (int i = 0; i < arraysize(ALL_ATTRIBUTES); i++) { | |
| 216 const char* attribute_name = ALL_ATTRIBUTES[i]; | |
| 217 const Value* value; | |
| 218 if (!dict.Get(attribute_name, &value)) | |
| 219 continue; | |
| 220 | |
| 221 switch (value->GetType()) { | |
| 222 case Value::TYPE_STRING: { | |
| 223 string16 string_value; | |
| 224 value->GetAsString(&string_value); | |
| 225 WriteAttribute(false, | |
| 226 StringPrintf(L"%ls='%ls'", | |
| 227 UTF8ToUTF16(attribute_name).c_str(), | |
| 228 string_value.c_str()), | |
| 229 &line); | |
| 230 break; | |
| 231 } | |
| 232 case Value::TYPE_INTEGER: { | |
| 233 int int_value; | |
| 234 value->GetAsInteger(&int_value); | |
| 235 WriteAttribute(false, | |
| 236 base::StringPrintf(L"%ls=%d", | |
| 237 UTF8ToUTF16(attribute_name).c_str(), | |
| 238 int_value), | |
| 239 &line); | |
| 240 break; | |
| 241 } | |
| 242 case Value::TYPE_DOUBLE: { | |
| 243 double double_value; | |
| 244 value->GetAsDouble(&double_value); | |
| 245 WriteAttribute(false, | |
| 246 base::StringPrintf(L"%ls=%.2f", | |
| 247 UTF8ToUTF16(attribute_name).c_str(), | |
| 248 double_value), | |
| 249 &line); | |
| 250 break; | |
| 251 } | |
| 252 case Value::TYPE_LIST: { | |
| 253 // Currently all list values are string and are written without | |
| 254 // attribute names. | |
| 255 const ListValue* list_value; | |
| 256 value->GetAsList(&list_value); | |
| 257 for (ListValue::const_iterator it = list_value->begin(); | |
| 258 it != list_value->end(); | |
| 259 ++it) { | |
| 260 string16 string_value; | |
| 261 if ((*it)->GetAsString(&string_value)) | |
| 262 WriteAttribute(false, string_value, &line); | |
| 263 } | |
| 264 break; | |
| 265 } | |
| 266 case Value::TYPE_DICTIONARY: { | |
| 267 // Currently all dictionary values are coordinates. | |
| 268 // Revisit this if that changes. | |
| 269 const DictionaryValue* dict_value; | |
| 270 value->GetAsDictionary(&dict_value); | |
| 271 if (strcmp(attribute_name, "size") == 0) { | |
| 272 WriteAttribute(false, | |
| 273 FormatCoordinates("size", "width", "height", | |
| 274 *dict_value), | |
| 275 &line); | |
| 276 } else if (strcmp(attribute_name, "location") == 0) { | |
| 277 WriteAttribute(false, | |
| 278 FormatCoordinates("location", "x", "y", *dict_value), | |
| 279 &line); | |
| 280 } | |
| 281 break; | |
| 282 } | |
| 283 default: | |
| 284 NOTREACHED(); | |
| 285 break; | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 return indent + line + ASCIIToUTF16("\n"); | |
| 290 } | 167 } |
| 291 | 168 |
| 292 // static | 169 // static |
| 293 const base::FilePath::StringType | 170 const base::FilePath::StringType |
| 294 AccessibilityTreeFormatter::GetActualFileSuffix() { | 171 AccessibilityTreeFormatter::GetActualFileSuffix() { |
| 295 return FILE_PATH_LITERAL("-actual-win.txt"); | 172 return FILE_PATH_LITERAL("-actual-win.txt"); |
| 296 } | 173 } |
| 297 | 174 |
| 298 // static | 175 // static |
| 299 const base::FilePath::StringType | 176 const base::FilePath::StringType |
| (...skipping 10 matching lines...) Expand all Loading... |
| 310 const std::string AccessibilityTreeFormatter::GetAllowString() { | 187 const std::string AccessibilityTreeFormatter::GetAllowString() { |
| 311 return "@WIN-ALLOW:"; | 188 return "@WIN-ALLOW:"; |
| 312 } | 189 } |
| 313 | 190 |
| 314 // static | 191 // static |
| 315 const std::string AccessibilityTreeFormatter::GetDenyString() { | 192 const std::string AccessibilityTreeFormatter::GetDenyString() { |
| 316 return "@WIN-DENY:"; | 193 return "@WIN-DENY:"; |
| 317 } | 194 } |
| 318 | 195 |
| 319 } // namespace content | 196 } // namespace content |
| OLD | NEW |