Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blob3cd374e7ea1f4626a310a9afd9122291c9e558d4
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 "content/browser/accessibility/browser_accessibility_android.h"
7 #include "base/i18n/break_iterator.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
12 #include "content/common/accessibility_messages.h"
14 namespace {
16 // These are enums from android.text.InputType in Java:
17 enum {
18 ANDROID_TEXT_INPUTTYPE_TYPE_NULL = 0,
19 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME = 0x4,
20 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE = 0x14,
21 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME = 0x24,
22 ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER = 0x2,
23 ANDROID_TEXT_INPUTTYPE_TYPE_PHONE = 0x3,
24 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT = 0x1,
25 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI = 0x11,
26 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EDIT_TEXT = 0xa1,
27 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL = 0xd1,
28 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD = 0xe1
31 // These are enums from android.view.View in Java:
32 enum {
33 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE = 0,
34 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE = 1,
35 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE = 2
38 // These are enums from
39 // android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
40 enum {
41 ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT = 1
44 } // namespace
46 namespace content {
48 // static
49 BrowserAccessibility* BrowserAccessibility::Create() {
50 return new BrowserAccessibilityAndroid();
53 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
54 first_time_ = true;
57 bool BrowserAccessibilityAndroid::IsNative() const {
58 return true;
61 void BrowserAccessibilityAndroid::OnLocationChanged() {
62 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_LOCATION_CHANGED, this);
65 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
66 if (InternalChildCount() == 0)
67 return true;
69 // Iframes are always allowed to contain children.
70 if (IsIframe() ||
71 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
72 GetRole() == ui::AX_ROLE_WEB_AREA) {
73 return false;
76 // If it has a focusable child, we definitely can't leave out children.
77 if (HasFocusableChild())
78 return false;
80 // Date and time controls should drop their children.
81 if (GetRole() == ui::AX_ROLE_DATE || GetRole() == ui::AX_ROLE_TIME)
82 return true;
84 // Headings with text can drop their children.
85 base::string16 name = GetText();
86 if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
87 return true;
89 // Focusable nodes with text can drop their children.
90 if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
91 return true;
93 // Nodes with only static text as children can drop their children.
94 if (HasOnlyStaticTextChildren())
95 return true;
97 return BrowserAccessibility::PlatformIsLeaf();
100 bool BrowserAccessibilityAndroid::CanScrollForward() const {
101 if (!IsSlider())
102 return false;
104 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
105 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
106 return value < max;
109 bool BrowserAccessibilityAndroid::CanScrollBackward() const {
110 if (!IsSlider())
111 return false;
113 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
114 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
115 return value > min;
118 bool BrowserAccessibilityAndroid::IsCheckable() const {
119 bool checkable = false;
120 bool is_aria_pressed_defined;
121 bool is_mixed;
122 GetAriaTristate("aria-pressed", &is_aria_pressed_defined, &is_mixed);
123 if (GetRole() == ui::AX_ROLE_CHECK_BOX ||
124 GetRole() == ui::AX_ROLE_RADIO_BUTTON ||
125 GetRole() == ui::AX_ROLE_MENU_ITEM_CHECK_BOX ||
126 GetRole() == ui::AX_ROLE_MENU_ITEM_RADIO ||
127 is_aria_pressed_defined) {
128 checkable = true;
130 if (HasState(ui::AX_STATE_CHECKED))
131 checkable = true;
132 return checkable;
135 bool BrowserAccessibilityAndroid::IsChecked() const {
136 return (HasState(ui::AX_STATE_CHECKED) || HasState(ui::AX_STATE_PRESSED));
139 bool BrowserAccessibilityAndroid::IsClickable() const {
140 if (!PlatformIsLeaf())
141 return false;
143 return IsFocusable() || !GetText().empty();
146 bool BrowserAccessibilityAndroid::IsCollection() const {
147 return (GetRole() == ui::AX_ROLE_GRID ||
148 GetRole() == ui::AX_ROLE_LIST ||
149 GetRole() == ui::AX_ROLE_LIST_BOX ||
150 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
151 GetRole() == ui::AX_ROLE_TABLE ||
152 GetRole() == ui::AX_ROLE_TREE);
155 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
156 return (GetRole() == ui::AX_ROLE_CELL ||
157 GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
158 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM ||
159 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
160 GetRole() == ui::AX_ROLE_LIST_ITEM ||
161 GetRole() == ui::AX_ROLE_ROW_HEADER ||
162 GetRole() == ui::AX_ROLE_TREE_ITEM);
165 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
166 std::string invalid;
167 return GetHtmlAttribute("aria-invalid", &invalid);
170 bool BrowserAccessibilityAndroid::IsDismissable() const {
171 return false; // No concept of "dismissable" on the web currently.
174 bool BrowserAccessibilityAndroid::IsEditableText() const {
175 return (GetRole() == ui::AX_ROLE_TEXT_AREA ||
176 GetRole() == ui::AX_ROLE_TEXT_FIELD);
179 bool BrowserAccessibilityAndroid::IsEnabled() const {
180 return HasState(ui::AX_STATE_ENABLED);
183 bool BrowserAccessibilityAndroid::IsFocusable() const {
184 bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
185 if (IsIframe() ||
186 GetRole() == ui::AX_ROLE_WEB_AREA) {
187 focusable = false;
189 return focusable;
192 bool BrowserAccessibilityAndroid::IsFocused() const {
193 return manager()->GetFocus(manager()->GetRoot()) == this;
196 bool BrowserAccessibilityAndroid::IsHeading() const {
197 BrowserAccessibilityAndroid* parent =
198 static_cast<BrowserAccessibilityAndroid*>(GetParent());
199 if (parent && parent->IsHeading())
200 return true;
202 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
203 GetRole() == ui::AX_ROLE_HEADING ||
204 GetRole() == ui::AX_ROLE_ROW_HEADER);
207 bool BrowserAccessibilityAndroid::IsHierarchical() const {
208 return (GetRole() == ui::AX_ROLE_LIST ||
209 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
210 GetRole() == ui::AX_ROLE_TREE);
213 bool BrowserAccessibilityAndroid::IsLink() const {
214 return (GetRole() == ui::AX_ROLE_LINK ||
215 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK);
218 bool BrowserAccessibilityAndroid::IsMultiLine() const {
219 return GetRole() == ui::AX_ROLE_TEXT_AREA;
222 bool BrowserAccessibilityAndroid::IsPassword() const {
223 return HasState(ui::AX_STATE_PROTECTED);
226 bool BrowserAccessibilityAndroid::IsRangeType() const {
227 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR ||
228 GetRole() == ui::AX_ROLE_METER ||
229 GetRole() == ui::AX_ROLE_SCROLL_BAR ||
230 GetRole() == ui::AX_ROLE_SLIDER);
233 bool BrowserAccessibilityAndroid::IsScrollable() const {
234 int dummy;
235 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
238 bool BrowserAccessibilityAndroid::IsSelected() const {
239 return HasState(ui::AX_STATE_SELECTED);
242 bool BrowserAccessibilityAndroid::IsSlider() const {
243 return GetRole() == ui::AX_ROLE_SLIDER;
246 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
247 return !HasState(ui::AX_STATE_INVISIBLE);
250 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
251 return HasState(ui::AX_STATE_HASPOPUP);
254 const char* BrowserAccessibilityAndroid::GetClassName() const {
255 const char* class_name = NULL;
257 switch (GetRole()) {
258 case ui::AX_ROLE_SEARCH_BOX:
259 case ui::AX_ROLE_SPIN_BUTTON:
260 case ui::AX_ROLE_TEXT_AREA:
261 case ui::AX_ROLE_TEXT_FIELD:
262 class_name = "android.widget.EditText";
263 break;
264 case ui::AX_ROLE_SLIDER:
265 class_name = "android.widget.SeekBar";
266 break;
267 case ui::AX_ROLE_COLOR_WELL:
268 case ui::AX_ROLE_COMBO_BOX:
269 case ui::AX_ROLE_DATE:
270 case ui::AX_ROLE_POP_UP_BUTTON:
271 case ui::AX_ROLE_TIME:
272 class_name = "android.widget.Spinner";
273 break;
274 case ui::AX_ROLE_BUTTON:
275 case ui::AX_ROLE_MENU_BUTTON:
276 class_name = "android.widget.Button";
277 break;
278 case ui::AX_ROLE_CHECK_BOX:
279 case ui::AX_ROLE_SWITCH:
280 class_name = "android.widget.CheckBox";
281 break;
282 case ui::AX_ROLE_RADIO_BUTTON:
283 class_name = "android.widget.RadioButton";
284 break;
285 case ui::AX_ROLE_TOGGLE_BUTTON:
286 class_name = "android.widget.ToggleButton";
287 break;
288 case ui::AX_ROLE_CANVAS:
289 case ui::AX_ROLE_IMAGE:
290 case ui::AX_ROLE_SVG_ROOT:
291 class_name = "android.widget.Image";
292 break;
293 case ui::AX_ROLE_METER:
294 case ui::AX_ROLE_PROGRESS_INDICATOR:
295 class_name = "android.widget.ProgressBar";
296 break;
297 case ui::AX_ROLE_TAB_LIST:
298 class_name = "android.widget.TabWidget";
299 break;
300 case ui::AX_ROLE_GRID:
301 case ui::AX_ROLE_TABLE:
302 class_name = "android.widget.GridView";
303 break;
304 case ui::AX_ROLE_LIST:
305 case ui::AX_ROLE_LIST_BOX:
306 case ui::AX_ROLE_DESCRIPTION_LIST:
307 class_name = "android.widget.ListView";
308 break;
309 case ui::AX_ROLE_DIALOG:
310 class_name = "android.app.Dialog";
311 break;
312 case ui::AX_ROLE_ROOT_WEB_AREA:
313 class_name = "android.webkit.WebView";
314 break;
315 case ui::AX_ROLE_MENU_ITEM:
316 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
317 case ui::AX_ROLE_MENU_ITEM_RADIO:
318 class_name = "android.view.MenuItem";
319 break;
320 default:
321 class_name = "android.view.View";
322 break;
325 return class_name;
328 base::string16 BrowserAccessibilityAndroid::GetText() const {
329 if (IsIframe() ||
330 GetRole() == ui::AX_ROLE_WEB_AREA) {
331 return base::string16();
334 // See comment in browser_accessibility_win.cc for details.
335 // The difference here is that we can only expose one accessible
336 // name on Android, not 2 or 3 like on Windows or Mac.
338 // First, always return the |value| attribute if this is an
339 // input field.
340 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
341 if (!value.empty()) {
342 if (HasState(ui::AX_STATE_EDITABLE))
343 return value;
345 switch (GetRole()) {
346 case ui::AX_ROLE_COMBO_BOX:
347 case ui::AX_ROLE_POP_UP_BUTTON:
348 case ui::AX_ROLE_TEXT_AREA:
349 case ui::AX_ROLE_TEXT_FIELD:
350 return value;
354 // For color wells, the color is stored in separate attributes.
355 // Perhaps we could return color names in the future?
356 if (GetRole() == ui::AX_ROLE_COLOR_WELL) {
357 int red = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_RED);
358 int green = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_GREEN);
359 int blue = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_BLUE);
360 return base::UTF8ToUTF16(
361 base::StringPrintf("#%02X%02X%02X", red, green, blue));
364 // Always prefer visible text if this is a link. Sites sometimes add
365 // a "title" attribute to a link with more information, but we can't
366 // lose the link text.
367 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
368 if (!name.empty() && GetRole() == ui::AX_ROLE_LINK)
369 return name;
371 // If there's no text value, the basic rule is: prefer description
372 // (aria-labelledby or aria-label), then help (title), then name
373 // (inner text), then value (control value). However, if
374 // title_elem_id is set, that means there's a label element
375 // supplying the name and then name takes precedence over help.
376 // TODO(dmazzoni): clean this up by providing more granular labels in
377 // Blink, making the platform-specific mapping to accessible text simpler.
378 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
379 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
381 base::string16 placeholder;
382 switch (GetRole()) {
383 case ui::AX_ROLE_DATE:
384 case ui::AX_ROLE_TEXT_AREA:
385 case ui::AX_ROLE_TEXT_FIELD:
386 case ui::AX_ROLE_TIME:
387 GetHtmlAttribute("placeholder", &placeholder);
390 int title_elem_id = GetIntAttribute(
391 ui::AX_ATTR_TITLE_UI_ELEMENT);
392 base::string16 text;
393 if (!description.empty())
394 text = description;
395 else if (title_elem_id && !name.empty())
396 text = name;
397 else if (!help.empty())
398 text = help;
399 else if (!name.empty())
400 text = name;
401 else if (!placeholder.empty())
402 text = placeholder;
403 else if (!value.empty())
404 text = value;
405 else if (title_elem_id) {
406 BrowserAccessibility* title_elem =
407 manager()->GetFromID(title_elem_id);
408 if (title_elem)
409 text = static_cast<BrowserAccessibilityAndroid*>(title_elem)->GetText();
412 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
413 // from within this!
414 if (text.empty() &&
415 (HasOnlyStaticTextChildren() ||
416 (IsFocusable() && HasOnlyTextAndImageChildren()))) {
417 for (uint32 i = 0; i < InternalChildCount(); i++) {
418 BrowserAccessibility* child = InternalGetChild(i);
419 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
423 if (text.empty() && (IsLink() || GetRole() == ui::AX_ROLE_IMAGE)) {
424 base::string16 url = GetString16Attribute(ui::AX_ATTR_URL);
425 // Given a url like http://foo.com/bar/baz.png, just return the
426 // base name, e.g., "baz".
427 int trailing_slashes = 0;
428 while (url.size() - trailing_slashes > 0 &&
429 url[url.size() - trailing_slashes - 1] == '/') {
430 trailing_slashes++;
432 if (trailing_slashes)
433 url = url.substr(0, url.size() - trailing_slashes);
434 size_t slash_index = url.rfind('/');
435 if (slash_index != std::string::npos)
436 url = url.substr(slash_index + 1);
437 size_t dot_index = url.rfind('.');
438 if (dot_index != std::string::npos)
439 url = url.substr(0, dot_index);
440 text = url;
443 return text;
446 int BrowserAccessibilityAndroid::GetItemIndex() const {
447 int index = 0;
448 switch (GetRole()) {
449 case ui::AX_ROLE_LIST_ITEM:
450 case ui::AX_ROLE_LIST_BOX_OPTION:
451 case ui::AX_ROLE_TREE_ITEM:
452 index = GetIndexInParent();
453 break;
454 case ui::AX_ROLE_SLIDER:
455 case ui::AX_ROLE_PROGRESS_INDICATOR: {
456 // Return a percentage here for live feedback in an AccessibilityEvent.
457 // The exact value is returned in RangeCurrentValue.
458 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
459 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
460 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
461 if (max > min && value >= min && value <= max)
462 index = static_cast<int>(((value - min)) * 100 / (max - min));
463 break;
466 return index;
469 int BrowserAccessibilityAndroid::GetItemCount() const {
470 int count = 0;
471 switch (GetRole()) {
472 case ui::AX_ROLE_LIST:
473 case ui::AX_ROLE_LIST_BOX:
474 case ui::AX_ROLE_DESCRIPTION_LIST:
475 count = PlatformChildCount();
476 break;
477 case ui::AX_ROLE_SLIDER:
478 case ui::AX_ROLE_PROGRESS_INDICATOR:
479 // An AccessibilityEvent can only return integer information about a
480 // seek control, so we return a percentage. The real range is returned
481 // in RangeMin and RangeMax.
482 count = 100;
483 break;
485 return count;
488 int BrowserAccessibilityAndroid::GetScrollX() const {
489 int value = 0;
490 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
491 return value;
494 int BrowserAccessibilityAndroid::GetScrollY() const {
495 int value = 0;
496 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
497 return value;
500 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
501 int value = 0;
502 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
503 return value;
506 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
507 int value = 0;
508 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
509 return value;
512 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
513 size_t index = 0;
514 while (index < old_value_.length() &&
515 index < new_value_.length() &&
516 old_value_[index] == new_value_[index]) {
517 index++;
519 return index;
522 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
523 size_t old_len = old_value_.length();
524 size_t new_len = new_value_.length();
525 size_t left = 0;
526 while (left < old_len &&
527 left < new_len &&
528 old_value_[left] == new_value_[left]) {
529 left++;
531 size_t right = 0;
532 while (right < old_len &&
533 right < new_len &&
534 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
535 right++;
537 return (new_len - left - right);
540 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
541 size_t old_len = old_value_.length();
542 size_t new_len = new_value_.length();
543 size_t left = 0;
544 while (left < old_len &&
545 left < new_len &&
546 old_value_[left] == new_value_[left]) {
547 left++;
549 size_t right = 0;
550 while (right < old_len &&
551 right < new_len &&
552 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
553 right++;
555 return (old_len - left - right);
558 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
559 return old_value_;
562 int BrowserAccessibilityAndroid::GetSelectionStart() const {
563 int sel_start = 0;
564 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
565 return sel_start;
568 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
569 int sel_end = 0;
570 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
571 return sel_end;
574 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
575 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
576 return value.length();
579 int BrowserAccessibilityAndroid::AndroidInputType() const {
580 std::string html_tag = GetStringAttribute(
581 ui::AX_ATTR_HTML_TAG);
582 if (html_tag != "input")
583 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
585 std::string type;
586 if (!GetHtmlAttribute("type", &type))
587 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
589 if (type.empty() || type == "text" || type == "search")
590 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
591 else if (type == "date")
592 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
593 else if (type == "datetime" || type == "datetime-local")
594 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
595 else if (type == "email")
596 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
597 else if (type == "month")
598 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
599 else if (type == "number")
600 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
601 else if (type == "password")
602 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
603 else if (type == "tel")
604 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
605 else if (type == "time")
606 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
607 else if (type == "url")
608 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
609 else if (type == "week")
610 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
612 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
615 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
616 std::string live = GetStringAttribute(
617 ui::AX_ATTR_LIVE_STATUS);
618 if (live == "polite")
619 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
620 else if (live == "assertive")
621 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
622 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
625 int BrowserAccessibilityAndroid::AndroidRangeType() const {
626 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
629 int BrowserAccessibilityAndroid::RowCount() const {
630 if (GetRole() == ui::AX_ROLE_GRID ||
631 GetRole() == ui::AX_ROLE_TABLE) {
632 return CountChildrenWithRole(ui::AX_ROLE_ROW);
635 if (GetRole() == ui::AX_ROLE_LIST ||
636 GetRole() == ui::AX_ROLE_LIST_BOX ||
637 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
638 GetRole() == ui::AX_ROLE_TREE) {
639 return PlatformChildCount();
642 return 0;
645 int BrowserAccessibilityAndroid::ColumnCount() const {
646 if (GetRole() == ui::AX_ROLE_GRID ||
647 GetRole() == ui::AX_ROLE_TABLE) {
648 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
650 return 0;
653 int BrowserAccessibilityAndroid::RowIndex() const {
654 if (GetRole() == ui::AX_ROLE_LIST_ITEM ||
655 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
656 GetRole() == ui::AX_ROLE_TREE_ITEM) {
657 return GetIndexInParent();
660 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
663 int BrowserAccessibilityAndroid::RowSpan() const {
664 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
667 int BrowserAccessibilityAndroid::ColumnIndex() const {
668 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
671 int BrowserAccessibilityAndroid::ColumnSpan() const {
672 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
675 float BrowserAccessibilityAndroid::RangeMin() const {
676 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
679 float BrowserAccessibilityAndroid::RangeMax() const {
680 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
683 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
684 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
687 void BrowserAccessibilityAndroid::GetGranularityBoundaries(
688 int granularity,
689 std::vector<int32>* starts,
690 std::vector<int32>* ends,
691 int offset) {
692 switch (granularity) {
693 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE:
694 GetLineBoundaries(starts, ends, offset);
695 break;
696 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
697 GetWordBoundaries(starts, ends, offset);
698 break;
699 default:
700 NOTREACHED();
704 void BrowserAccessibilityAndroid::GetLineBoundaries(
705 std::vector<int32>* line_starts,
706 std::vector<int32>* line_ends,
707 int offset) {
708 // If this node has no children, treat it as all one line.
709 if (GetText().size() > 0 && !InternalChildCount()) {
710 line_starts->push_back(offset);
711 line_ends->push_back(offset + GetText().size());
714 // If this is a static text node, get the line boundaries from the
715 // inline text boxes if possible.
716 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) {
717 int last_y = 0;
718 for (uint32 i = 0; i < InternalChildCount(); i++) {
719 BrowserAccessibilityAndroid* child =
720 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
721 CHECK_EQ(ui::AX_ROLE_INLINE_TEXT_BOX, child->GetRole());
722 // TODO(dmazzoni): replace this with a proper API to determine
723 // if two inline text boxes are on the same line. http://crbug.com/421771
724 int y = child->GetLocation().y();
725 if (i == 0) {
726 line_starts->push_back(offset);
727 } else if (y != last_y) {
728 line_ends->push_back(offset);
729 line_starts->push_back(offset);
731 offset += child->GetText().size();
732 last_y = y;
734 line_ends->push_back(offset);
735 return;
738 // Otherwise, call GetLineBoundaries recursively on the children.
739 for (uint32 i = 0; i < InternalChildCount(); i++) {
740 BrowserAccessibilityAndroid* child =
741 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
742 child->GetLineBoundaries(line_starts, line_ends, offset);
743 offset += child->GetText().size();
747 void BrowserAccessibilityAndroid::GetWordBoundaries(
748 std::vector<int32>* word_starts,
749 std::vector<int32>* word_ends,
750 int offset) {
751 if (GetRole() == ui::AX_ROLE_INLINE_TEXT_BOX) {
752 const std::vector<int32>& starts = GetIntListAttribute(
753 ui::AX_ATTR_WORD_STARTS);
754 const std::vector<int32>& ends = GetIntListAttribute(
755 ui::AX_ATTR_WORD_ENDS);
756 for (size_t i = 0; i < starts.size(); ++i) {
757 word_starts->push_back(offset + starts[i]);
758 word_ends->push_back(offset + ends[i]);
760 return;
763 base::string16 concatenated_text;
764 for (uint32 i = 0; i < InternalChildCount(); i++) {
765 BrowserAccessibilityAndroid* child =
766 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
767 base::string16 child_text = child->GetText();
768 concatenated_text += child->GetText();
771 base::string16 text = GetText();
772 if (text.empty() || concatenated_text == text) {
773 // Great - this node is just the concatenation of its children, so
774 // we can get the word boundaries recursively.
775 for (uint32 i = 0; i < InternalChildCount(); i++) {
776 BrowserAccessibilityAndroid* child =
777 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
778 child->GetWordBoundaries(word_starts, word_ends, offset);
779 offset += child->GetText().size();
781 } else {
782 // This node has its own accessible text that doesn't match its
783 // visible text - like alt text for an image or something with an
784 // aria-label, so split the text into words locally.
785 base::i18n::BreakIterator iter(text, base::i18n::BreakIterator::BREAK_WORD);
786 if (!iter.Init())
787 return;
788 while (iter.Advance()) {
789 if (iter.IsWord()) {
790 word_starts->push_back(iter.prev());
791 word_ends->push_back(iter.pos());
797 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
798 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
799 // from within this!
800 for (uint32 i = 0; i < InternalChildCount(); i++) {
801 BrowserAccessibility* child = InternalGetChild(i);
802 if (child->HasState(ui::AX_STATE_FOCUSABLE))
803 return true;
804 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
805 return true;
807 return false;
810 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
811 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
812 // from within this!
813 for (uint32 i = 0; i < InternalChildCount(); i++) {
814 BrowserAccessibility* child = InternalGetChild(i);
815 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT)
816 return false;
818 return true;
821 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
822 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
823 // from within this!
824 for (uint32 i = 0; i < InternalChildCount(); i++) {
825 BrowserAccessibility* child = InternalGetChild(i);
826 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT &&
827 child->GetRole() != ui::AX_ROLE_IMAGE) {
828 return false;
831 return true;
834 bool BrowserAccessibilityAndroid::IsIframe() const {
835 base::string16 html_tag = GetString16Attribute(
836 ui::AX_ATTR_HTML_TAG);
837 return html_tag == base::ASCIIToUTF16("iframe");
840 void BrowserAccessibilityAndroid::OnDataChanged() {
841 BrowserAccessibility::OnDataChanged();
843 if (IsEditableText()) {
844 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
845 if (value != new_value_) {
846 old_value_ = new_value_;
847 new_value_ = value;
851 if (GetRole() == ui::AX_ROLE_ALERT && first_time_)
852 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
854 base::string16 live;
855 if (GetString16Attribute(
856 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
857 NotifyLiveRegionUpdate(live);
860 first_time_ = false;
863 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
864 base::string16& aria_live) {
865 if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
866 !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
867 return;
869 base::string16 text = GetText();
870 if (cached_text_ != text) {
871 if (!text.empty()) {
872 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
873 this);
875 cached_text_ = text;
879 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
880 int count = 0;
881 for (uint32 i = 0; i < PlatformChildCount(); i++) {
882 if (PlatformGetChild(i)->GetRole() == role)
883 count++;
885 return count;
888 } // namespace content