Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: content/browser/accessibility/accessibility_tree_formatter_win.cc

Issue 13479003: Modify AccessibilityTreeFormatter to build up an internal representation of the tree (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix syntax errors Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
22 namespace content { 24 namespace content {
23 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
24 void AccessibilityTreeFormatter::Initialize() { 56 void AccessibilityTreeFormatter::Initialize() {
25 ui::win::CreateATLModuleIfNeeded(); 57 ui::win::CreateATLModuleIfNeeded();
26 } 58 }
27 59
28 string16 AccessibilityTreeFormatter::ToString( 60 void AccessibilityTreeFormatter::AddProperties(
29 BrowserAccessibility* node, char* prefix) { 61 const BrowserAccessibility& node, DictionaryValue* dict) {
30 BrowserAccessibilityWin* acc_obj = node->ToBrowserAccessibilityWin(); 62 BrowserAccessibilityWin* acc_obj =
63 const_cast<BrowserAccessibility*>(&node)->ToBrowserAccessibilityWin();
31 64
32 // Get the computed name.
33 VARIANT variant_self; 65 VARIANT variant_self;
34 variant_self.vt = VT_I4; 66 variant_self.vt = VT_I4;
35 variant_self.lVal = CHILDID_SELF; 67 variant_self.lVal = CHILDID_SELF;
68
69 dict->SetString("role", IAccessible2RoleToString(acc_obj->ia2_role()));
70
36 CComBSTR msaa_variant; 71 CComBSTR msaa_variant;
37 HRESULT hresult = acc_obj->get_accName(variant_self, &msaa_variant); 72 HRESULT hresult = acc_obj->get_accName(variant_self, &msaa_variant);
38 string16 name; 73 if (hresult == S_OK)
39 if (S_OK == hresult) 74 dict->SetString("name", msaa_variant.m_str);
40 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);
41 78
42 hresult = acc_obj->get_accValue(variant_self, &msaa_variant);
43 string16 value;
44 if (S_OK == hresult)
45 value = msaa_variant.m_str;
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.
68 std::vector<string16> state_strings; 79 std::vector<string16> state_strings;
69 int32 ia_state = acc_obj->ia_state(); 80 int32 ia_state = acc_obj->ia_state();
70 81
71 // Avoid flakiness: these states depend on whether the window is focused 82 // Avoid flakiness: these states depend on whether the window is focused
72 // and the position of the mouse cursor. 83 // and the position of the mouse cursor.
73 ia_state &= ~STATE_SYSTEM_HOTTRACKED; 84 ia_state &= ~STATE_SYSTEM_HOTTRACKED;
74 ia_state &= ~STATE_SYSTEM_OFFSCREEN; 85 ia_state &= ~STATE_SYSTEM_OFFSCREEN;
75 86
76 IAccessibleStateToStringVector(ia_state, &state_strings); 87 IAccessibleStateToStringVector(ia_state, &state_strings);
77 IAccessible2StateToStringVector(acc_obj->ia2_state(), &state_strings); 88 IAccessible2StateToStringVector(acc_obj->ia2_state(), &state_strings);
78 89 ListValue* states = new ListValue;
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"'");
87 for (std::vector<string16>::const_iterator it = state_strings.begin(); 90 for (std::vector<string16>::const_iterator it = state_strings.begin();
88 it != state_strings.end(); 91 it != state_strings.end();
89 ++it) { 92 ++it) {
90 Add(false, *it); 93 states->AppendString(UTF16ToUTF8(*it));
91 } 94 }
95 dict->Set("states", states);
96
97 const std::vector<string16>& ia2_attributes = acc_obj->ia2_attributes();
98 ListValue* attributes = new ListValue;
92 for (std::vector<string16>::const_iterator it = ia2_attributes.begin(); 99 for (std::vector<string16>::const_iterator it = ia2_attributes.begin();
93 it != ia2_attributes.end(); 100 it != ia2_attributes.end();
94 ++it) { 101 ++it) {
95 Add(false, *it); 102 attributes->AppendString(UTF16ToUTF8(*it));
96 } 103 }
97 Add(false, L"role_name='" + acc_obj->role_name() + L"'"); 104 dict->Set("attributes", attributes);
105
106 dict->SetString("role_name", acc_obj->role_name());
107
98 VARIANT currentValue; 108 VARIANT currentValue;
99 if (acc_obj->get_currentValue(&currentValue) == S_OK) 109 if (acc_obj->get_currentValue(&currentValue) == S_OK)
100 Add(false, base::StringPrintf(L"currentValue=%.2f", V_R8(&currentValue))); 110 dict->SetDouble("currentValue", V_R8(&currentValue));
111
101 VARIANT minimumValue; 112 VARIANT minimumValue;
102 if (acc_obj->get_minimumValue(&minimumValue) == S_OK) 113 if (acc_obj->get_minimumValue(&minimumValue) == S_OK)
103 Add(false, base::StringPrintf(L"minimumValue=%.2f", V_R8(&minimumValue))); 114 dict->SetDouble("minimumValue", V_R8(&minimumValue));
115
104 VARIANT maximumValue; 116 VARIANT maximumValue;
105 if (acc_obj->get_maximumValue(&maximumValue) == S_OK) 117 if (acc_obj->get_maximumValue(&maximumValue) == S_OK)
106 Add(false, base::StringPrintf(L"maximumValue=%.2f", V_R8(&maximumValue))); 118 dict->SetDouble("maximumValue", V_R8(&maximumValue));
107 Add(false, L"description='" + description + L"'"); 119
108 Add(false, L"default_action='" + default_action + L"'"); 120 hresult = acc_obj->get_accDescription(variant_self, &msaa_variant);
109 Add(false, L"keyboard_shortcut='" + keyboard_shortcut + L"'"); 121 if (hresult == S_OK)
110 BrowserAccessibility* root = node->manager()->GetRoot(); 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();
111 LONG left, top, width, height; 137 LONG left, top, width, height;
112 LONG root_left, root_top, root_width, root_height; 138 LONG root_left, root_top, root_width, root_height;
113 if (S_FALSE != acc_obj->accLocation( 139 if (acc_obj->accLocation(&left, &top, &width, &height, variant_self)
114 &left, &top, &width, &height, variant_self) && 140 != S_FALSE
115 S_FALSE != root->ToBrowserAccessibilityWin()->accLocation( 141 && root->ToBrowserAccessibilityWin()->accLocation(
116 &root_left, &root_top, &root_width, &root_height, variant_self)) { 142 &root_left, &root_top, &root_width, &root_height, variant_self)
117 Add(false, base::StringPrintf(L"location=(%d, %d)", 143 != S_FALSE) {
118 left - root_left, top - root_top)); 144 DictionaryValue* location = new DictionaryValue;
119 Add(false, base::StringPrintf(L"size=(%d, %d)", width, height)); 145 location->SetInteger("x", left - root_left);
120 } 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 }
154
121 LONG index_in_parent; 155 LONG index_in_parent;
122 if (acc_obj->get_indexInParent(&index_in_parent) == S_OK) 156 if (acc_obj->get_indexInParent(&index_in_parent) == S_OK)
123 Add(false, base::StringPrintf(L"index_in_parent=%d", index_in_parent)); 157 dict->SetInteger("index_in_parent", index_in_parent);
158
124 LONG n_relations; 159 LONG n_relations;
125 if (acc_obj->get_nRelations(&n_relations) == S_OK) 160 if (acc_obj->get_nRelations(&n_relations) == S_OK)
126 Add(false, base::StringPrintf(L"n_relations=%d", n_relations)); 161 dict->SetInteger("n_relations", n_relations);
162
127 LONG group_level, similar_items_in_group, position_in_group; 163 LONG group_level, similar_items_in_group, position_in_group;
128 if (acc_obj->get_groupPosition(&group_level, 164 if (acc_obj->get_groupPosition(&group_level,
129 &similar_items_in_group, 165 &similar_items_in_group,
130 &position_in_group) == S_OK) { 166 &position_in_group) == S_OK) {
131 Add(false, base::StringPrintf(L"group_level=%d", group_level)); 167 dict->SetInteger("group_level", group_level);
132 Add(false, base::StringPrintf(L"similar_items_in_group=%d", 168 dict->SetInteger("similar_items_in_group", similar_items_in_group);
133 similar_items_in_group)); 169 dict->SetInteger("position_in_group", position_in_group);
134 Add(false, base::StringPrintf(L"position_in_group=%d", position_in_group));
135 } 170 }
136 LONG table_rows; 171 LONG table_rows;
137 if (acc_obj->get_nRows(&table_rows) == S_OK) 172 if (acc_obj->get_nRows(&table_rows) == S_OK)
138 Add(false, base::StringPrintf(L"table_rows=%d", table_rows)); 173 dict->SetInteger("table_rows", table_rows);
139 LONG table_columns; 174 LONG table_columns;
140 if (acc_obj->get_nRows(&table_columns) == S_OK) 175 if (acc_obj->get_nRows(&table_columns) == S_OK)
141 Add(false, base::StringPrintf(L"table_columns=%d", table_columns)); 176 dict->SetInteger("table_columns", table_columns);
142 LONG row_index; 177 LONG row_index;
143 if (acc_obj->get_rowIndex(&row_index) == S_OK) 178 if (acc_obj->get_rowIndex(&row_index) == S_OK)
144 Add(false, base::StringPrintf(L"row_index=%d", row_index)); 179 dict->SetInteger("row_index", row_index);
145 LONG column_index; 180 LONG column_index;
146 if (acc_obj->get_columnIndex(&column_index) == S_OK) 181 if (acc_obj->get_columnIndex(&column_index) == S_OK)
147 Add(false, base::StringPrintf(L"column_index=%d", column_index)); 182 dict->SetInteger("column_index", column_index);
148 LONG n_characters; 183 LONG n_characters;
149 if (acc_obj->get_nCharacters(&n_characters) == S_OK) 184 if (acc_obj->get_nCharacters(&n_characters) == S_OK)
150 Add(false, base::StringPrintf(L"n_characters=%d", n_characters)); 185 dict->SetInteger("n_characters", n_characters);
151 LONG caret_offset; 186 LONG caret_offset;
152 if (acc_obj->get_caretOffset(&caret_offset) == S_OK) 187 if (acc_obj->get_caretOffset(&caret_offset) == S_OK)
153 Add(false, base::StringPrintf(L"caret_offset=%d", caret_offset)); 188 dict->SetInteger("caret_offset", caret_offset);
154 LONG n_selections; 189 LONG n_selections;
155 if (acc_obj->get_nSelections(&n_selections) == S_OK) { 190 if (acc_obj->get_nSelections(&n_selections) == S_OK) {
156 Add(false, base::StringPrintf(L"n_selections=%d", n_selections)); 191 dict->SetInteger("n_selections", n_selections);
157 if (n_selections > 0) { 192 if (n_selections > 0) {
158 LONG start, end; 193 LONG start, end;
159 if (acc_obj->get_selection(0, &start, &end) == S_OK) { 194 if (acc_obj->get_selection(0, &start, &end) == S_OK) {
160 Add(false, base::StringPrintf(L"selection_start=%d", start)); 195 dict->SetInteger("selection_start", start);
161 Add(false, base::StringPrintf(L"selection_end=%d", end)); 196 dict->SetInteger("selection_end", end);
162 } 197 }
163 } 198 }
164 } 199 }
165
166 return UTF8ToUTF16(prefix) + FinishLine() + ASCIIToUTF16("\n");
167 } 200 }
168 201
202 string16 AccessibilityTreeFormatter::ToString(const 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 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 }
291
169 // static 292 // static
170 const base::FilePath::StringType 293 const base::FilePath::StringType
171 AccessibilityTreeFormatter::GetActualFileSuffix() { 294 AccessibilityTreeFormatter::GetActualFileSuffix() {
172 return FILE_PATH_LITERAL("-actual-win.txt"); 295 return FILE_PATH_LITERAL("-actual-win.txt");
173 } 296 }
174 297
175 // static 298 // static
176 const base::FilePath::StringType 299 const base::FilePath::StringType
177 AccessibilityTreeFormatter::GetExpectedFileSuffix() { 300 AccessibilityTreeFormatter::GetExpectedFileSuffix() {
178 return FILE_PATH_LITERAL("-expected-win.txt"); 301 return FILE_PATH_LITERAL("-expected-win.txt");
179 } 302 }
180 303
181 // static 304 // static
182 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() { 305 const std::string AccessibilityTreeFormatter::GetAllowEmptyString() {
183 return "@WIN-ALLOW-EMPTY:"; 306 return "@WIN-ALLOW-EMPTY:";
184 } 307 }
185 308
186 // static 309 // static
187 const std::string AccessibilityTreeFormatter::GetAllowString() { 310 const std::string AccessibilityTreeFormatter::GetAllowString() {
188 return "@WIN-ALLOW:"; 311 return "@WIN-ALLOW:";
189 } 312 }
190 313
191 // static 314 // static
192 const std::string AccessibilityTreeFormatter::GetDenyString() { 315 const std::string AccessibilityTreeFormatter::GetDenyString() {
193 return "@WIN-DENY:"; 316 return "@WIN-DENY:";
194 } 317 }
195 318
196 } // namespace content 319 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698