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 <oleacc.h> 8 9 #include <string> 10 11 #include "base/files/file_path.h" 12 #include "base/strings/string_util.h" 13 #include "base/strings/stringprintf.h" 14 #include "base/strings/utf_string_conversions.h" 15 #include "content/browser/accessibility/accessibility_tree_formatter_utils_win.h" 16 #include "content/browser/accessibility/browser_accessibility_manager.h" 17 #include "content/browser/accessibility/browser_accessibility_win.h" 18 #include "content/common/accessibility_node_data.h" 19 #include "third_party/iaccessible2/ia2_api_all.h" 20 #include "ui/base/win/atl_module.h" 21 22 using base::StringPrintf; 23 24 namespace content { 25 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() { 57 ui::win::CreateATLModuleIfNeeded(); 58 } 59 60 void AccessibilityTreeFormatter::AddProperties( 61 const BrowserAccessibility& node, base::DictionaryValue* dict) { 62 BrowserAccessibilityWin* acc_obj = 63 const_cast<BrowserAccessibility*>(&node)->ToBrowserAccessibilityWin(); 64 65 VARIANT variant_self; 66 variant_self.vt = VT_I4; 67 variant_self.lVal = CHILDID_SELF; 68 69 dict->SetString("role", IAccessible2RoleToString(acc_obj->ia2_role())); 70 71 CComBSTR msaa_variant; 72 HRESULT hresult = acc_obj->get_accName(variant_self, &msaa_variant); 73 if (hresult == S_OK) 74 dict->SetString("name", msaa_variant.m_str); 75 hresult = acc_obj->get_accValue(variant_self, &msaa_variant); 76 if (hresult == S_OK) 77 dict->SetString("value", msaa_variant.m_str); 78 79 std::vector<string16> state_strings; 80 int32 ia_state = acc_obj->ia_state(); 81 82 // Avoid flakiness: these states depend on whether the window is focused 83 // and the position of the mouse cursor. 84 ia_state &= ~STATE_SYSTEM_HOTTRACKED; 85 ia_state &= ~STATE_SYSTEM_OFFSCREEN; 86 87 IAccessibleStateToStringVector(ia_state, &state_strings); 88 IAccessible2StateToStringVector(acc_obj->ia2_state(), &state_strings); 89 base::ListValue* states = new base::ListValue; 90 for (std::vector<string16>::const_iterator it = state_strings.begin(); 91 it != state_strings.end(); 92 ++it) { 93 states->AppendString(UTF16ToUTF8(*it)); 94 } 95 dict->Set("states", states); 96 97 const std::vector<string16>& ia2_attributes = acc_obj->ia2_attributes(); 98 base::ListValue* attributes = new base::ListValue; 99 for (std::vector<string16>::const_iterator it = ia2_attributes.begin(); 100 it != ia2_attributes.end(); 101 ++it) { 102 attributes->AppendString(UTF16ToUTF8(*it)); 103 } 104 dict->Set("attributes", attributes); 105 106 dict->SetString("role_name", acc_obj->role_name()); 107 108 VARIANT currentValue; 109 if (acc_obj->get_currentValue(¤tValue) == S_OK) 110 dict->SetDouble("currentValue", V_R8(¤tValue)); 111 112 VARIANT minimumValue; 113 if (acc_obj->get_minimumValue(&minimumValue) == S_OK) 114 dict->SetDouble("minimumValue", V_R8(&minimumValue)); 115 116 VARIANT maximumValue; 117 if (acc_obj->get_maximumValue(&maximumValue) == S_OK) 118 dict->SetDouble("maximumValue", V_R8(&maximumValue)); 119 120 hresult = acc_obj->get_accDescription(variant_self, &msaa_variant); 121 if (hresult == S_OK) 122 dict->SetString("description", msaa_variant.m_str); 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; 138 LONG root_left, root_top, root_width, root_height; 139 if (acc_obj->accLocation(&left, &top, &width, &height, variant_self) 140 != S_FALSE 141 && root->ToBrowserAccessibilityWin()->accLocation( 142 &root_left, &root_top, &root_width, &root_height, variant_self) 143 != S_FALSE) { 144 base::DictionaryValue* location = new base::DictionaryValue; 145 location->SetInteger("x", left - root_left); 146 location->SetInteger("y", top - root_top); 147 dict->Set("location", location); 148 149 base::DictionaryValue* size = new base::DictionaryValue; 150 size->SetInteger("width", width); 151 size->SetInteger("height", height); 152 dict->Set("size", size); 153 } 154 155 LONG index_in_parent; 156 if (acc_obj->get_indexInParent(&index_in_parent) == S_OK) 157 dict->SetInteger("index_in_parent", index_in_parent); 158 159 LONG n_relations; 160 if (acc_obj->get_nRelations(&n_relations) == S_OK) 161 dict->SetInteger("n_relations", n_relations); 162 163 LONG group_level, similar_items_in_group, position_in_group; 164 if (acc_obj->get_groupPosition(&group_level, 165 &similar_items_in_group, 166 &position_in_group) == S_OK) { 167 dict->SetInteger("group_level", group_level); 168 dict->SetInteger("similar_items_in_group", similar_items_in_group); 169 dict->SetInteger("position_in_group", position_in_group); 170 } 171 LONG table_rows; 172 if (acc_obj->get_nRows(&table_rows) == S_OK) 173 dict->SetInteger("table_rows", table_rows); 174 LONG table_columns; 175 if (acc_obj->get_nRows(&table_columns) == S_OK) 176 dict->SetInteger("table_columns", table_columns); 177 LONG row_index; 178 if (acc_obj->get_rowIndex(&row_index) == S_OK) 179 dict->SetInteger("row_index", row_index); 180 LONG column_index; 181 if (acc_obj->get_columnIndex(&column_index) == S_OK) 182 dict->SetInteger("column_index", column_index); 183 LONG n_characters; 184 if (acc_obj->get_nCharacters(&n_characters) == S_OK) 185 dict->SetInteger("n_characters", n_characters); 186 LONG caret_offset; 187 if (acc_obj->get_caretOffset(&caret_offset) == S_OK) 188 dict->SetInteger("caret_offset", caret_offset); 189 LONG n_selections; 190 if (acc_obj->get_nSelections(&n_selections) == S_OK) { 191 dict->SetInteger("n_selections", n_selections); 192 if (n_selections > 0) { 193 LONG start, end; 194 if (acc_obj->get_selection(0, &start, &end) == S_OK) { 195 dict->SetInteger("selection_start", start); 196 dict->SetInteger("selection_end", end); 197 } 198 } 199 } 200 } 201 202 string16 AccessibilityTreeFormatter::ToString(const base::DictionaryValue& dict, 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 base::Value* value; 218 if (!dict.Get(attribute_name, &value)) 219 continue; 220 221 switch (value->GetType()) { 222 case base::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 base::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 base::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 base::Value::TYPE_LIST: { 253 // Currently all list values are string and are written without 254 // attribute names. 255 const base::ListValue* list_value; 256 value->GetAsList(&list_value); 257 for (base::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 base::Value::TYPE_DICTIONARY: { 267 // Currently all dictionary values are coordinates. 268 // Revisit this if that changes. 269 const base::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 } 291 292 // static 293 const base::FilePath::StringType 294 AccessibilityTreeFormatter::GetActualFileSuffix() { 295 return FILE_PATH_LITERAL("-actual-win.txt"); 296 } 297 298 // static 299 const base::FilePath::StringType 300 AccessibilityTreeFormatter::GetExpectedFileSuffix() { 301 return FILE_PATH_LITERAL("-expected-win.txt"); 302 } 303 304 // static 305 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() { 306 return "@WIN-ALLOW-EMPTY:"; 307 } 308 309 // static 310 const std::string AccessibilityTreeFormatter::GetAllowString() { 311 return "@WIN-ALLOW:"; 312 } 313 314 // static 315 const std::string AccessibilityTreeFormatter::GetDenyString() { 316 return "@WIN-DENY:"; 317 } 318 319 } // namespace content 320