Record MTU discovery packets in net-internals log.
[chromium-blink-merge.git] / ui / accessibility / ax_node_data.cc
blob44394edaa7ce04f353068fc905c1ebc8ba7d96cd
1 // Copyright 2013 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 "ui/accessibility/ax_node_data.h"
7 #include <set>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
14 using base::DoubleToString;
15 using base::IntToString;
17 namespace ui {
19 namespace {
21 std::string IntVectorToString(const std::vector<int>& items) {
22 std::string str;
23 for (size_t i = 0; i < items.size(); ++i) {
24 if (i > 0)
25 str += ",";
26 str += IntToString(items[i]);
28 return str;
31 // Predicate that returns true if the first value of a pair is |first|.
32 template<typename FirstType, typename SecondType>
33 struct FirstIs {
34 FirstIs(FirstType first)
35 : first_(first) {}
36 bool operator()(std::pair<FirstType, SecondType> const& p) {
37 return p.first == first_;
39 FirstType first_;
42 // Helper function that finds a key in a vector of pairs by matching on the
43 // first value, and returns an iterator.
44 template<typename FirstType, typename SecondType>
45 typename std::vector<std::pair<FirstType, SecondType>>::const_iterator
46 FindInVectorOfPairs(
47 FirstType first,
48 const std::vector<std::pair<FirstType, SecondType>>& vector) {
49 return std::find_if(vector.begin(),
50 vector.end(),
51 FirstIs<FirstType, SecondType>(first));
54 } // namespace
56 AXNodeData::AXNodeData()
57 : id(-1),
58 role(AX_ROLE_UNKNOWN),
59 state(0xFFFFFFFF) {
62 AXNodeData::~AXNodeData() {
65 bool AXNodeData::HasBoolAttribute(AXBoolAttribute attribute) const {
66 auto iter = FindInVectorOfPairs(attribute, bool_attributes);
67 return iter != bool_attributes.end();
70 bool AXNodeData::GetBoolAttribute(AXBoolAttribute attribute) const {
71 bool result;
72 if (GetBoolAttribute(attribute, &result))
73 return result;
74 return false;
77 bool AXNodeData::GetBoolAttribute(
78 AXBoolAttribute attribute, bool* value) const {
79 auto iter = FindInVectorOfPairs(attribute, bool_attributes);
80 if (iter != bool_attributes.end()) {
81 *value = iter->second;
82 return true;
85 return false;
88 bool AXNodeData::HasFloatAttribute(AXFloatAttribute attribute) const {
89 auto iter = FindInVectorOfPairs(attribute, float_attributes);
90 return iter != float_attributes.end();
93 float AXNodeData::GetFloatAttribute(AXFloatAttribute attribute) const {
94 float result;
95 if (GetFloatAttribute(attribute, &result))
96 return result;
97 return 0.0;
100 bool AXNodeData::GetFloatAttribute(
101 AXFloatAttribute attribute, float* value) const {
102 auto iter = FindInVectorOfPairs(attribute, float_attributes);
103 if (iter != float_attributes.end()) {
104 *value = iter->second;
105 return true;
108 return false;
111 bool AXNodeData::HasIntAttribute(AXIntAttribute attribute) const {
112 auto iter = FindInVectorOfPairs(attribute, int_attributes);
113 return iter != int_attributes.end();
116 int AXNodeData::GetIntAttribute(AXIntAttribute attribute) const {
117 int result;
118 if (GetIntAttribute(attribute, &result))
119 return result;
120 return 0;
123 bool AXNodeData::GetIntAttribute(
124 AXIntAttribute attribute, int* value) const {
125 auto iter = FindInVectorOfPairs(attribute, int_attributes);
126 if (iter != int_attributes.end()) {
127 *value = iter->second;
128 return true;
131 return false;
134 bool AXNodeData::HasStringAttribute(AXStringAttribute attribute) const {
135 auto iter = FindInVectorOfPairs(attribute, string_attributes);
136 return iter != string_attributes.end();
139 const std::string& AXNodeData::GetStringAttribute(
140 AXStringAttribute attribute) const {
141 CR_DEFINE_STATIC_LOCAL(std::string, empty_string, ());
142 auto iter = FindInVectorOfPairs(attribute, string_attributes);
143 return iter != string_attributes.end() ? iter->second : empty_string;
146 bool AXNodeData::GetStringAttribute(
147 AXStringAttribute attribute, std::string* value) const {
148 auto iter = FindInVectorOfPairs(attribute, string_attributes);
149 if (iter != string_attributes.end()) {
150 *value = iter->second;
151 return true;
154 return false;
157 base::string16 AXNodeData::GetString16Attribute(
158 AXStringAttribute attribute) const {
159 std::string value_utf8;
160 if (!GetStringAttribute(attribute, &value_utf8))
161 return base::string16();
162 return base::UTF8ToUTF16(value_utf8);
165 bool AXNodeData::GetString16Attribute(
166 AXStringAttribute attribute,
167 base::string16* value) const {
168 std::string value_utf8;
169 if (!GetStringAttribute(attribute, &value_utf8))
170 return false;
171 *value = base::UTF8ToUTF16(value_utf8);
172 return true;
175 bool AXNodeData::HasIntListAttribute(AXIntListAttribute attribute) const {
176 auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
177 return iter != intlist_attributes.end();
180 const std::vector<int32>& AXNodeData::GetIntListAttribute(
181 AXIntListAttribute attribute) const {
182 CR_DEFINE_STATIC_LOCAL(std::vector<int32>, empty_vector, ());
183 auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
184 if (iter != intlist_attributes.end())
185 return iter->second;
186 return empty_vector;
189 bool AXNodeData::GetIntListAttribute(
190 AXIntListAttribute attribute, std::vector<int32>* value) const {
191 auto iter = FindInVectorOfPairs(attribute, intlist_attributes);
192 if (iter != intlist_attributes.end()) {
193 *value = iter->second;
194 return true;
197 return false;
200 bool AXNodeData::GetHtmlAttribute(
201 const char* html_attr, std::string* value) const {
202 for (size_t i = 0; i < html_attributes.size(); ++i) {
203 const std::string& attr = html_attributes[i].first;
204 if (base::LowerCaseEqualsASCII(attr, html_attr)) {
205 *value = html_attributes[i].second;
206 return true;
210 return false;
213 bool AXNodeData::GetHtmlAttribute(
214 const char* html_attr, base::string16* value) const {
215 std::string value_utf8;
216 if (!GetHtmlAttribute(html_attr, &value_utf8))
217 return false;
218 *value = base::UTF8ToUTF16(value_utf8);
219 return true;
222 void AXNodeData::AddStringAttribute(
223 AXStringAttribute attribute, const std::string& value) {
224 string_attributes.push_back(std::make_pair(attribute, value));
227 void AXNodeData::AddIntAttribute(
228 AXIntAttribute attribute, int value) {
229 int_attributes.push_back(std::make_pair(attribute, value));
232 void AXNodeData::AddFloatAttribute(
233 AXFloatAttribute attribute, float value) {
234 float_attributes.push_back(std::make_pair(attribute, value));
237 void AXNodeData::AddBoolAttribute(
238 AXBoolAttribute attribute, bool value) {
239 bool_attributes.push_back(std::make_pair(attribute, value));
242 void AXNodeData::AddIntListAttribute(
243 AXIntListAttribute attribute, const std::vector<int32>& value) {
244 intlist_attributes.push_back(std::make_pair(attribute, value));
247 void AXNodeData::SetName(std::string name) {
248 string_attributes.push_back(std::make_pair(AX_ATTR_NAME, name));
251 void AXNodeData::SetValue(std::string value) {
252 string_attributes.push_back(std::make_pair(AX_ATTR_VALUE, value));
255 std::string AXNodeData::ToString() const {
256 std::string result;
258 result += "id=" + IntToString(id);
259 result += " " + ui::ToString(role);
261 if (state & (1 << AX_STATE_BUSY))
262 result += " BUSY";
263 if (state & (1 << AX_STATE_CHECKED))
264 result += " CHECKED";
265 if (state & (1 << AX_STATE_COLLAPSED))
266 result += " COLLAPSED";
267 if (state & (1 << AX_STATE_EXPANDED))
268 result += " EXPANDED";
269 if (state & (1 << AX_STATE_FOCUSABLE))
270 result += " FOCUSABLE";
271 if (state & (1 << AX_STATE_FOCUSED))
272 result += " FOCUSED";
273 if (state & (1 << AX_STATE_HASPOPUP))
274 result += " HASPOPUP";
275 if (state & (1 << AX_STATE_HOVERED))
276 result += " HOVERED";
277 if (state & (1 << AX_STATE_INDETERMINATE))
278 result += " INDETERMINATE";
279 if (state & (1 << AX_STATE_INVISIBLE))
280 result += " INVISIBLE";
281 if (state & (1 << AX_STATE_LINKED))
282 result += " LINKED";
283 if (state & (1 << AX_STATE_MULTISELECTABLE))
284 result += " MULTISELECTABLE";
285 if (state & (1 << AX_STATE_OFFSCREEN))
286 result += " OFFSCREEN";
287 if (state & (1 << AX_STATE_PRESSED))
288 result += " PRESSED";
289 if (state & (1 << AX_STATE_PROTECTED))
290 result += " PROTECTED";
291 if (state & (1 << AX_STATE_READ_ONLY))
292 result += " READONLY";
293 if (state & (1 << AX_STATE_REQUIRED))
294 result += " REQUIRED";
295 if (state & (1 << AX_STATE_SELECTABLE))
296 result += " SELECTABLE";
297 if (state & (1 << AX_STATE_SELECTED))
298 result += " SELECTED";
299 if (state & (1 << AX_STATE_VERTICAL))
300 result += " VERTICAL";
301 if (state & (1 << AX_STATE_VISITED))
302 result += " VISITED";
304 result += " (" + IntToString(location.x()) + ", " +
305 IntToString(location.y()) + ")-(" +
306 IntToString(location.width()) + ", " +
307 IntToString(location.height()) + ")";
309 for (size_t i = 0; i < int_attributes.size(); ++i) {
310 std::string value = IntToString(int_attributes[i].second);
311 switch (int_attributes[i].first) {
312 case AX_ATTR_SCROLL_X:
313 result += " scroll_x=" + value;
314 break;
315 case AX_ATTR_SCROLL_X_MIN:
316 result += " scroll_x_min=" + value;
317 break;
318 case AX_ATTR_SCROLL_X_MAX:
319 result += " scroll_x_max=" + value;
320 break;
321 case AX_ATTR_SCROLL_Y:
322 result += " scroll_y=" + value;
323 break;
324 case AX_ATTR_SCROLL_Y_MIN:
325 result += " scroll_y_min=" + value;
326 break;
327 case AX_ATTR_SCROLL_Y_MAX:
328 result += " scroll_y_max=" + value;
329 break;
330 case AX_ATTR_HIERARCHICAL_LEVEL:
331 result += " level=" + value;
332 break;
333 case AX_ATTR_TEXT_SEL_START:
334 result += " sel_start=" + value;
335 break;
336 case AX_ATTR_TEXT_SEL_END:
337 result += " sel_end=" + value;
338 break;
339 case AX_ATTR_TABLE_ROW_COUNT:
340 result += " rows=" + value;
341 break;
342 case AX_ATTR_TABLE_COLUMN_COUNT:
343 result += " cols=" + value;
344 break;
345 case AX_ATTR_TABLE_CELL_COLUMN_INDEX:
346 result += " col=" + value;
347 break;
348 case AX_ATTR_TABLE_CELL_ROW_INDEX:
349 result += " row=" + value;
350 break;
351 case AX_ATTR_TABLE_CELL_COLUMN_SPAN:
352 result += " colspan=" + value;
353 break;
354 case AX_ATTR_TABLE_CELL_ROW_SPAN:
355 result += " rowspan=" + value;
356 break;
357 case AX_ATTR_TABLE_COLUMN_HEADER_ID:
358 result += " column_header_id=" + value;
359 break;
360 case AX_ATTR_TABLE_COLUMN_INDEX:
361 result += " column_index=" + value;
362 break;
363 case AX_ATTR_TABLE_HEADER_ID:
364 result += " header_id=" + value;
365 break;
366 case AX_ATTR_TABLE_ROW_HEADER_ID:
367 result += " row_header_id=" + value;
368 break;
369 case AX_ATTR_TABLE_ROW_INDEX:
370 result += " row_index=" + value;
371 break;
372 case AX_ATTR_SORT_DIRECTION:
373 switch (int_attributes[i].second) {
374 case AX_SORT_DIRECTION_UNSORTED:
375 result += " sort_direction=none";
376 break;
377 case AX_SORT_DIRECTION_ASCENDING:
378 result += " sort_direction=ascending";
379 break;
380 case AX_SORT_DIRECTION_DESCENDING:
381 result += " sort_direction=descending";
382 break;
383 case AX_SORT_DIRECTION_OTHER:
384 result += " sort_direction=other";
385 break;
387 break;
388 case AX_ATTR_TITLE_UI_ELEMENT:
389 result += " title_elem=" + value;
390 break;
391 case AX_ATTR_ACTIVEDESCENDANT_ID:
392 result += " activedescendant=" + value;
393 break;
394 case AX_ATTR_TREE_ID:
395 result += " tree_id=" + value;
396 break;
397 case AX_ATTR_CHILD_TREE_ID:
398 result += " child_tree_id=" + value;
399 break;
400 case AX_ATTR_COLOR_VALUE:
401 result += base::StringPrintf(" color_value=&%X",
402 int_attributes[i].second);
403 break;
404 case AX_ATTR_BACKGROUND_COLOR:
405 result += base::StringPrintf(" background_color=&%X",
406 int_attributes[i].second);
407 break;
408 case AX_ATTR_COLOR:
409 result += base::StringPrintf(" color=&%X", int_attributes[i].second);
410 break;
411 case AX_ATTR_TEXT_DIRECTION:
412 switch (int_attributes[i].second) {
413 case AX_TEXT_DIRECTION_LTR:
414 result += " text_direction=ltr";
415 break;
416 case AX_TEXT_DIRECTION_RTL:
417 result += " text_direction=rtl";
418 break;
419 case AX_TEXT_DIRECTION_TTB:
420 result += " text_direction=ttb";
421 break;
422 case AX_TEXT_DIRECTION_BTT:
423 result += " text_direction=btt";
424 break;
426 case AX_ATTR_TEXT_STYLE: {
427 unsigned int text_style = int_attributes[i].second;
428 if (text_style == AX_TEXT_STYLE_NONE)
429 break;
430 std::string text_style_value(" text_style=");
431 if (text_style & AX_TEXT_STYLE_BOLD)
432 text_style_value += "bold,";
433 if (text_style & AX_TEXT_STYLE_ITALIC)
434 text_style_value += "italic,";
435 if (text_style & AX_TEXT_STYLE_UNDERLINE)
436 text_style_value += "underline,";
437 if (text_style & AX_TEXT_STYLE_LINE_THROUGH)
438 text_style_value += "line-through,";
439 result += text_style_value.substr(0, text_style_value.size() - 1);;
441 break;
442 case AX_ATTR_SET_SIZE:
443 result += " setsize=" + value;
444 break;
445 case AX_ATTR_POS_IN_SET:
446 result += " posinset=" + value;
447 break;
448 case AX_ATTR_INVALID_STATE:
449 switch (int_attributes[i].second) {
450 case AX_INVALID_STATE_FALSE:
451 result += " invalid_state=false";
452 break;
453 case AX_INVALID_STATE_TRUE:
454 result += " invalid_state=true";
455 break;
456 case AX_INVALID_STATE_SPELLING:
457 result += " invalid_state=spelling";
458 break;
459 case AX_INVALID_STATE_GRAMMAR:
460 result += " invalid_state=grammar";
461 break;
462 case AX_INVALID_STATE_OTHER:
463 result += " invalid_state=other";
464 break;
466 break;
467 case AX_INT_ATTRIBUTE_NONE:
468 break;
472 for (size_t i = 0; i < string_attributes.size(); ++i) {
473 std::string value = string_attributes[i].second;
474 switch (string_attributes[i].first) {
475 case AX_ATTR_DOC_URL:
476 result += " doc_url=" + value;
477 break;
478 case AX_ATTR_DOC_TITLE:
479 result += " doc_title=" + value;
480 break;
481 case AX_ATTR_DOC_MIMETYPE:
482 result += " doc_mimetype=" + value;
483 break;
484 case AX_ATTR_DOC_DOCTYPE:
485 result += " doc_doctype=" + value;
486 break;
487 case AX_ATTR_ACCESS_KEY:
488 result += " access_key=" + value;
489 break;
490 case AX_ATTR_ACTION:
491 result += " action=" + value;
492 break;
493 case AX_ATTR_AUTO_COMPLETE:
494 result += " autocomplete=" + value;
495 break;
496 case AX_ATTR_DESCRIPTION:
497 result += " description=" + value;
498 break;
499 case AX_ATTR_DISPLAY:
500 result += " display=" + value;
501 break;
502 case AX_ATTR_HELP:
503 result += " help=" + value;
504 break;
505 case AX_ATTR_HTML_TAG:
506 result += " html_tag=" + value;
507 break;
508 case AX_ATTR_ARIA_INVALID_VALUE:
509 result += " aria_invalid_value=" + value;
510 break;
511 case AX_ATTR_LIVE_RELEVANT:
512 result += " relevant=" + value;
513 break;
514 case AX_ATTR_LIVE_STATUS:
515 result += " live=" + value;
516 break;
517 case AX_ATTR_CONTAINER_LIVE_RELEVANT:
518 result += " container_relevant=" + value;
519 break;
520 case AX_ATTR_CONTAINER_LIVE_STATUS:
521 result += " container_live=" + value;
522 break;
523 case AX_ATTR_PLACEHOLDER:
524 result += "placeholder" + value;
525 break;
526 case AX_ATTR_ROLE:
527 result += " role=" + value;
528 break;
529 case AX_ATTR_SHORTCUT:
530 result += " shortcut=" + value;
531 break;
532 case AX_ATTR_URL:
533 result += " url=" + value;
534 break;
535 case AX_ATTR_NAME:
536 result += " name=" + value;
537 break;
538 case AX_ATTR_VALUE:
539 result += " value=" + value;
540 break;
541 case AX_STRING_ATTRIBUTE_NONE:
542 break;
546 for (size_t i = 0; i < float_attributes.size(); ++i) {
547 std::string value = DoubleToString(float_attributes[i].second);
548 switch (float_attributes[i].first) {
549 case AX_ATTR_DOC_LOADING_PROGRESS:
550 result += " doc_progress=" + value;
551 break;
552 case AX_ATTR_VALUE_FOR_RANGE:
553 result += " value_for_range=" + value;
554 break;
555 case AX_ATTR_MAX_VALUE_FOR_RANGE:
556 result += " max_value=" + value;
557 break;
558 case AX_ATTR_MIN_VALUE_FOR_RANGE:
559 result += " min_value=" + value;
560 break;
561 case AX_ATTR_FONT_SIZE:
562 result += " font_size=" + value;
563 break;
564 case AX_FLOAT_ATTRIBUTE_NONE:
565 break;
569 for (size_t i = 0; i < bool_attributes.size(); ++i) {
570 std::string value = bool_attributes[i].second ? "true" : "false";
571 switch (bool_attributes[i].first) {
572 case AX_ATTR_DOC_LOADED:
573 result += " doc_loaded=" + value;
574 break;
575 case AX_ATTR_BUTTON_MIXED:
576 result += " mixed=" + value;
577 break;
578 case AX_ATTR_LIVE_ATOMIC:
579 result += " atomic=" + value;
580 break;
581 case AX_ATTR_LIVE_BUSY:
582 result += " busy=" + value;
583 break;
584 case AX_ATTR_CONTAINER_LIVE_ATOMIC:
585 result += " container_atomic=" + value;
586 break;
587 case AX_ATTR_CONTAINER_LIVE_BUSY:
588 result += " container_busy=" + value;
589 break;
590 case AX_ATTR_ARIA_READONLY:
591 result += " aria_readonly=" + value;
592 break;
593 case AX_ATTR_CAN_SET_VALUE:
594 result += " can_set_value=" + value;
595 break;
596 case AX_ATTR_UPDATE_LOCATION_ONLY:
597 result += " update_location_only=" + value;
598 break;
599 case AX_ATTR_CANVAS_HAS_FALLBACK:
600 result += " has_fallback=" + value;
601 break;
602 case AX_ATTR_IS_AX_TREE_HOST:
603 result += " is_ax_tree_host=" + value;
604 break;
605 case AX_BOOL_ATTRIBUTE_NONE:
606 break;
610 for (size_t i = 0; i < intlist_attributes.size(); ++i) {
611 const std::vector<int32>& values = intlist_attributes[i].second;
612 switch (intlist_attributes[i].first) {
613 case AX_ATTR_INDIRECT_CHILD_IDS:
614 result += " indirect_child_ids=" + IntVectorToString(values);
615 break;
616 case AX_ATTR_CONTROLS_IDS:
617 result += " controls_ids=" + IntVectorToString(values);
618 break;
619 case AX_ATTR_DESCRIBEDBY_IDS:
620 result += " describedby_ids=" + IntVectorToString(values);
621 break;
622 case AX_ATTR_FLOWTO_IDS:
623 result += " flowto_ids=" + IntVectorToString(values);
624 break;
625 case AX_ATTR_LABELLEDBY_IDS:
626 result += " labelledby_ids=" + IntVectorToString(values);
627 break;
628 case AX_ATTR_OWNS_IDS:
629 result += " owns_ids=" + IntVectorToString(values);
630 break;
631 case AX_ATTR_LINE_BREAKS:
632 result += " line_breaks=" + IntVectorToString(values);
633 break;
634 case AX_ATTR_CELL_IDS:
635 result += " cell_ids=" + IntVectorToString(values);
636 break;
637 case AX_ATTR_UNIQUE_CELL_IDS:
638 result += " unique_cell_ids=" + IntVectorToString(values);
639 break;
640 case AX_ATTR_CHARACTER_OFFSETS:
641 result += " character_offsets=" + IntVectorToString(values);
642 break;
643 case AX_ATTR_WORD_STARTS:
644 result += " word_starts=" + IntVectorToString(values);
645 break;
646 case AX_ATTR_WORD_ENDS:
647 result += " word_ends=" + IntVectorToString(values);
648 break;
649 case AX_INT_LIST_ATTRIBUTE_NONE:
650 break;
654 if (!child_ids.empty())
655 result += " child_ids=" + IntVectorToString(child_ids);
657 return result;
660 } // namespace ui