Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blob9bfd68a343175d8505db9081a250d982a5d68013
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_FIELD;
178 bool BrowserAccessibilityAndroid::IsEnabled() const {
179 return HasState(ui::AX_STATE_ENABLED);
182 bool BrowserAccessibilityAndroid::IsFocusable() const {
183 bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
184 if (IsIframe() ||
185 GetRole() == ui::AX_ROLE_WEB_AREA) {
186 focusable = false;
188 return focusable;
191 bool BrowserAccessibilityAndroid::IsFocused() const {
192 return manager()->GetFocus(manager()->GetRoot()) == this;
195 bool BrowserAccessibilityAndroid::IsHeading() const {
196 BrowserAccessibilityAndroid* parent =
197 static_cast<BrowserAccessibilityAndroid*>(GetParent());
198 if (parent && parent->IsHeading())
199 return true;
201 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
202 GetRole() == ui::AX_ROLE_HEADING ||
203 GetRole() == ui::AX_ROLE_ROW_HEADER);
206 bool BrowserAccessibilityAndroid::IsHierarchical() const {
207 return (GetRole() == ui::AX_ROLE_LIST ||
208 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
209 GetRole() == ui::AX_ROLE_TREE);
212 bool BrowserAccessibilityAndroid::IsLink() const {
213 return (GetRole() == ui::AX_ROLE_LINK ||
214 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK);
217 bool BrowserAccessibilityAndroid::IsMultiLine() const {
218 return HasState(ui::AX_STATE_MULTILINE);
221 bool BrowserAccessibilityAndroid::IsPassword() const {
222 return HasState(ui::AX_STATE_PROTECTED);
225 bool BrowserAccessibilityAndroid::IsRangeType() const {
226 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR ||
227 GetRole() == ui::AX_ROLE_METER ||
228 GetRole() == ui::AX_ROLE_SCROLL_BAR ||
229 GetRole() == ui::AX_ROLE_SLIDER);
232 bool BrowserAccessibilityAndroid::IsScrollable() const {
233 int dummy;
234 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
237 bool BrowserAccessibilityAndroid::IsSelected() const {
238 return HasState(ui::AX_STATE_SELECTED);
241 bool BrowserAccessibilityAndroid::IsSlider() const {
242 return GetRole() == ui::AX_ROLE_SLIDER;
245 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
246 return !HasState(ui::AX_STATE_INVISIBLE);
249 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
250 return HasState(ui::AX_STATE_HASPOPUP);
253 const char* BrowserAccessibilityAndroid::GetClassName() const {
254 const char* class_name = NULL;
256 switch (GetRole()) {
257 case ui::AX_ROLE_SEARCH_BOX:
258 case ui::AX_ROLE_SPIN_BUTTON:
259 case ui::AX_ROLE_TEXT_FIELD:
260 class_name = "android.widget.EditText";
261 break;
262 case ui::AX_ROLE_SLIDER:
263 class_name = "android.widget.SeekBar";
264 break;
265 case ui::AX_ROLE_COLOR_WELL:
266 case ui::AX_ROLE_COMBO_BOX:
267 case ui::AX_ROLE_DATE:
268 case ui::AX_ROLE_POP_UP_BUTTON:
269 case ui::AX_ROLE_TIME:
270 class_name = "android.widget.Spinner";
271 break;
272 case ui::AX_ROLE_BUTTON:
273 case ui::AX_ROLE_MENU_BUTTON:
274 class_name = "android.widget.Button";
275 break;
276 case ui::AX_ROLE_CHECK_BOX:
277 case ui::AX_ROLE_SWITCH:
278 class_name = "android.widget.CheckBox";
279 break;
280 case ui::AX_ROLE_RADIO_BUTTON:
281 class_name = "android.widget.RadioButton";
282 break;
283 case ui::AX_ROLE_TOGGLE_BUTTON:
284 class_name = "android.widget.ToggleButton";
285 break;
286 case ui::AX_ROLE_CANVAS:
287 case ui::AX_ROLE_IMAGE:
288 case ui::AX_ROLE_SVG_ROOT:
289 class_name = "android.widget.Image";
290 break;
291 case ui::AX_ROLE_METER:
292 case ui::AX_ROLE_PROGRESS_INDICATOR:
293 class_name = "android.widget.ProgressBar";
294 break;
295 case ui::AX_ROLE_TAB_LIST:
296 class_name = "android.widget.TabWidget";
297 break;
298 case ui::AX_ROLE_GRID:
299 case ui::AX_ROLE_TABLE:
300 class_name = "android.widget.GridView";
301 break;
302 case ui::AX_ROLE_LIST:
303 case ui::AX_ROLE_LIST_BOX:
304 case ui::AX_ROLE_DESCRIPTION_LIST:
305 class_name = "android.widget.ListView";
306 break;
307 case ui::AX_ROLE_DIALOG:
308 class_name = "android.app.Dialog";
309 break;
310 case ui::AX_ROLE_ROOT_WEB_AREA:
311 class_name = "android.webkit.WebView";
312 break;
313 case ui::AX_ROLE_MENU_ITEM:
314 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
315 case ui::AX_ROLE_MENU_ITEM_RADIO:
316 class_name = "android.view.MenuItem";
317 break;
318 default:
319 class_name = "android.view.View";
320 break;
323 return class_name;
326 base::string16 BrowserAccessibilityAndroid::GetText() const {
327 if (IsIframe() ||
328 GetRole() == ui::AX_ROLE_WEB_AREA) {
329 return base::string16();
332 // See comment in browser_accessibility_win.cc for details.
333 // The difference here is that we can only expose one accessible
334 // name on Android, not 2 or 3 like on Windows or Mac.
336 // First, always return the |value| attribute if this is an
337 // input field.
338 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
339 if (!value.empty()) {
340 if (HasState(ui::AX_STATE_EDITABLE))
341 return value;
343 switch (GetRole()) {
344 case ui::AX_ROLE_COMBO_BOX:
345 case ui::AX_ROLE_POP_UP_BUTTON:
346 case ui::AX_ROLE_TEXT_FIELD:
347 return value;
351 // For color wells, the color is stored in separate attributes.
352 // Perhaps we could return color names in the future?
353 if (GetRole() == ui::AX_ROLE_COLOR_WELL) {
354 int red = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_RED);
355 int green = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_GREEN);
356 int blue = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_BLUE);
357 return base::UTF8ToUTF16(
358 base::StringPrintf("#%02X%02X%02X", red, green, blue));
361 // Always prefer visible text if this is a link. Sites sometimes add
362 // a "title" attribute to a link with more information, but we can't
363 // lose the link text.
364 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
365 if (!name.empty() && GetRole() == ui::AX_ROLE_LINK)
366 return name;
368 // If there's no text value, the basic rule is: prefer description
369 // (aria-labelledby or aria-label), then help (title), then name
370 // (inner text), then value (control value). However, if
371 // title_elem_id is set, that means there's a label element
372 // supplying the name and then name takes precedence over help.
373 // TODO(dmazzoni): clean this up by providing more granular labels in
374 // Blink, making the platform-specific mapping to accessible text simpler.
375 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
376 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
378 base::string16 placeholder;
379 switch (GetRole()) {
380 case ui::AX_ROLE_DATE:
381 case ui::AX_ROLE_TEXT_FIELD:
382 case ui::AX_ROLE_TIME:
383 GetHtmlAttribute("placeholder", &placeholder);
386 int title_elem_id = GetIntAttribute(
387 ui::AX_ATTR_TITLE_UI_ELEMENT);
388 base::string16 text;
389 if (!description.empty())
390 text = description;
391 else if (title_elem_id && !name.empty())
392 text = name;
393 else if (!help.empty())
394 text = help;
395 else if (!name.empty())
396 text = name;
397 else if (!placeholder.empty())
398 text = placeholder;
399 else if (!value.empty())
400 text = value;
401 else if (title_elem_id) {
402 BrowserAccessibility* title_elem =
403 manager()->GetFromID(title_elem_id);
404 if (title_elem)
405 text = static_cast<BrowserAccessibilityAndroid*>(title_elem)->GetText();
408 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
409 // from within this!
410 if (text.empty() &&
411 (HasOnlyStaticTextChildren() ||
412 (IsFocusable() && HasOnlyTextAndImageChildren()))) {
413 for (uint32 i = 0; i < InternalChildCount(); i++) {
414 BrowserAccessibility* child = InternalGetChild(i);
415 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
419 if (text.empty() && (IsLink() || GetRole() == ui::AX_ROLE_IMAGE)) {
420 base::string16 url = GetString16Attribute(ui::AX_ATTR_URL);
421 // Given a url like http://foo.com/bar/baz.png, just return the
422 // base name, e.g., "baz".
423 int trailing_slashes = 0;
424 while (url.size() - trailing_slashes > 0 &&
425 url[url.size() - trailing_slashes - 1] == '/') {
426 trailing_slashes++;
428 if (trailing_slashes)
429 url = url.substr(0, url.size() - trailing_slashes);
430 size_t slash_index = url.rfind('/');
431 if (slash_index != std::string::npos)
432 url = url.substr(slash_index + 1);
433 size_t dot_index = url.rfind('.');
434 if (dot_index != std::string::npos)
435 url = url.substr(0, dot_index);
436 text = url;
439 return text;
442 int BrowserAccessibilityAndroid::GetItemIndex() const {
443 int index = 0;
444 switch (GetRole()) {
445 case ui::AX_ROLE_LIST_ITEM:
446 case ui::AX_ROLE_LIST_BOX_OPTION:
447 case ui::AX_ROLE_TREE_ITEM:
448 index = GetIndexInParent();
449 break;
450 case ui::AX_ROLE_SLIDER:
451 case ui::AX_ROLE_PROGRESS_INDICATOR: {
452 // Return a percentage here for live feedback in an AccessibilityEvent.
453 // The exact value is returned in RangeCurrentValue.
454 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
455 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
456 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
457 if (max > min && value >= min && value <= max)
458 index = static_cast<int>(((value - min)) * 100 / (max - min));
459 break;
462 return index;
465 int BrowserAccessibilityAndroid::GetItemCount() const {
466 int count = 0;
467 switch (GetRole()) {
468 case ui::AX_ROLE_LIST:
469 case ui::AX_ROLE_LIST_BOX:
470 case ui::AX_ROLE_DESCRIPTION_LIST:
471 count = PlatformChildCount();
472 break;
473 case ui::AX_ROLE_SLIDER:
474 case ui::AX_ROLE_PROGRESS_INDICATOR:
475 // An AccessibilityEvent can only return integer information about a
476 // seek control, so we return a percentage. The real range is returned
477 // in RangeMin and RangeMax.
478 count = 100;
479 break;
481 return count;
484 int BrowserAccessibilityAndroid::GetScrollX() const {
485 int value = 0;
486 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
487 return value;
490 int BrowserAccessibilityAndroid::GetScrollY() const {
491 int value = 0;
492 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
493 return value;
496 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
497 int value = 0;
498 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
499 return value;
502 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
503 int value = 0;
504 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
505 return value;
508 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
509 size_t index = 0;
510 while (index < old_value_.length() &&
511 index < new_value_.length() &&
512 old_value_[index] == new_value_[index]) {
513 index++;
515 return index;
518 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
519 size_t old_len = old_value_.length();
520 size_t new_len = new_value_.length();
521 size_t left = 0;
522 while (left < old_len &&
523 left < new_len &&
524 old_value_[left] == new_value_[left]) {
525 left++;
527 size_t right = 0;
528 while (right < old_len &&
529 right < new_len &&
530 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
531 right++;
533 return (new_len - left - right);
536 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
537 size_t old_len = old_value_.length();
538 size_t new_len = new_value_.length();
539 size_t left = 0;
540 while (left < old_len &&
541 left < new_len &&
542 old_value_[left] == new_value_[left]) {
543 left++;
545 size_t right = 0;
546 while (right < old_len &&
547 right < new_len &&
548 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
549 right++;
551 return (old_len - left - right);
554 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
555 return old_value_;
558 int BrowserAccessibilityAndroid::GetSelectionStart() const {
559 int sel_start = 0;
560 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
561 return sel_start;
564 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
565 int sel_end = 0;
566 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
567 return sel_end;
570 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
571 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
572 return value.length();
575 int BrowserAccessibilityAndroid::AndroidInputType() const {
576 std::string html_tag = GetStringAttribute(
577 ui::AX_ATTR_HTML_TAG);
578 if (html_tag != "input")
579 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
581 std::string type;
582 if (!GetHtmlAttribute("type", &type))
583 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
585 if (type.empty() || type == "text" || type == "search")
586 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
587 else if (type == "date")
588 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
589 else if (type == "datetime" || type == "datetime-local")
590 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
591 else if (type == "email")
592 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
593 else if (type == "month")
594 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
595 else if (type == "number")
596 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
597 else if (type == "password")
598 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
599 else if (type == "tel")
600 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
601 else if (type == "time")
602 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
603 else if (type == "url")
604 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
605 else if (type == "week")
606 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
608 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
611 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
612 std::string live = GetStringAttribute(
613 ui::AX_ATTR_LIVE_STATUS);
614 if (live == "polite")
615 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
616 else if (live == "assertive")
617 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
618 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
621 int BrowserAccessibilityAndroid::AndroidRangeType() const {
622 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
625 int BrowserAccessibilityAndroid::RowCount() const {
626 if (GetRole() == ui::AX_ROLE_GRID ||
627 GetRole() == ui::AX_ROLE_TABLE) {
628 return CountChildrenWithRole(ui::AX_ROLE_ROW);
631 if (GetRole() == ui::AX_ROLE_LIST ||
632 GetRole() == ui::AX_ROLE_LIST_BOX ||
633 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
634 GetRole() == ui::AX_ROLE_TREE) {
635 return PlatformChildCount();
638 return 0;
641 int BrowserAccessibilityAndroid::ColumnCount() const {
642 if (GetRole() == ui::AX_ROLE_GRID ||
643 GetRole() == ui::AX_ROLE_TABLE) {
644 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
646 return 0;
649 int BrowserAccessibilityAndroid::RowIndex() const {
650 if (GetRole() == ui::AX_ROLE_LIST_ITEM ||
651 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
652 GetRole() == ui::AX_ROLE_TREE_ITEM) {
653 return GetIndexInParent();
656 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
659 int BrowserAccessibilityAndroid::RowSpan() const {
660 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
663 int BrowserAccessibilityAndroid::ColumnIndex() const {
664 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
667 int BrowserAccessibilityAndroid::ColumnSpan() const {
668 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
671 float BrowserAccessibilityAndroid::RangeMin() const {
672 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
675 float BrowserAccessibilityAndroid::RangeMax() const {
676 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
679 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
680 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
683 void BrowserAccessibilityAndroid::GetGranularityBoundaries(
684 int granularity,
685 std::vector<int32>* starts,
686 std::vector<int32>* ends,
687 int offset) {
688 switch (granularity) {
689 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE:
690 GetLineBoundaries(starts, ends, offset);
691 break;
692 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
693 GetWordBoundaries(starts, ends, offset);
694 break;
695 default:
696 NOTREACHED();
700 void BrowserAccessibilityAndroid::GetLineBoundaries(
701 std::vector<int32>* line_starts,
702 std::vector<int32>* line_ends,
703 int offset) {
704 // If this node has no children, treat it as all one line.
705 if (GetText().size() > 0 && !InternalChildCount()) {
706 line_starts->push_back(offset);
707 line_ends->push_back(offset + GetText().size());
710 // If this is a static text node, get the line boundaries from the
711 // inline text boxes if possible.
712 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) {
713 int last_y = 0;
714 for (uint32 i = 0; i < InternalChildCount(); i++) {
715 BrowserAccessibilityAndroid* child =
716 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
717 CHECK_EQ(ui::AX_ROLE_INLINE_TEXT_BOX, child->GetRole());
718 // TODO(dmazzoni): replace this with a proper API to determine
719 // if two inline text boxes are on the same line. http://crbug.com/421771
720 int y = child->GetLocation().y();
721 if (i == 0) {
722 line_starts->push_back(offset);
723 } else if (y != last_y) {
724 line_ends->push_back(offset);
725 line_starts->push_back(offset);
727 offset += child->GetText().size();
728 last_y = y;
730 line_ends->push_back(offset);
731 return;
734 // Otherwise, call GetLineBoundaries recursively on the children.
735 for (uint32 i = 0; i < InternalChildCount(); i++) {
736 BrowserAccessibilityAndroid* child =
737 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
738 child->GetLineBoundaries(line_starts, line_ends, offset);
739 offset += child->GetText().size();
743 void BrowserAccessibilityAndroid::GetWordBoundaries(
744 std::vector<int32>* word_starts,
745 std::vector<int32>* word_ends,
746 int offset) {
747 if (GetRole() == ui::AX_ROLE_INLINE_TEXT_BOX) {
748 const std::vector<int32>& starts = GetIntListAttribute(
749 ui::AX_ATTR_WORD_STARTS);
750 const std::vector<int32>& ends = GetIntListAttribute(
751 ui::AX_ATTR_WORD_ENDS);
752 for (size_t i = 0; i < starts.size(); ++i) {
753 word_starts->push_back(offset + starts[i]);
754 word_ends->push_back(offset + ends[i]);
756 return;
759 base::string16 concatenated_text;
760 for (uint32 i = 0; i < InternalChildCount(); i++) {
761 BrowserAccessibilityAndroid* child =
762 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
763 base::string16 child_text = child->GetText();
764 concatenated_text += child->GetText();
767 base::string16 text = GetText();
768 if (text.empty() || concatenated_text == text) {
769 // Great - this node is just the concatenation of its children, so
770 // we can get the word boundaries recursively.
771 for (uint32 i = 0; i < InternalChildCount(); i++) {
772 BrowserAccessibilityAndroid* child =
773 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
774 child->GetWordBoundaries(word_starts, word_ends, offset);
775 offset += child->GetText().size();
777 } else {
778 // This node has its own accessible text that doesn't match its
779 // visible text - like alt text for an image or something with an
780 // aria-label, so split the text into words locally.
781 base::i18n::BreakIterator iter(text, base::i18n::BreakIterator::BREAK_WORD);
782 if (!iter.Init())
783 return;
784 while (iter.Advance()) {
785 if (iter.IsWord()) {
786 word_starts->push_back(iter.prev());
787 word_ends->push_back(iter.pos());
793 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
794 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
795 // from within this!
796 for (uint32 i = 0; i < InternalChildCount(); i++) {
797 BrowserAccessibility* child = InternalGetChild(i);
798 if (child->HasState(ui::AX_STATE_FOCUSABLE))
799 return true;
800 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
801 return true;
803 return false;
806 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
807 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
808 // from within this!
809 for (uint32 i = 0; i < InternalChildCount(); i++) {
810 BrowserAccessibility* child = InternalGetChild(i);
811 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT)
812 return false;
814 return true;
817 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
818 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
819 // from within this!
820 for (uint32 i = 0; i < InternalChildCount(); i++) {
821 BrowserAccessibility* child = InternalGetChild(i);
822 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT &&
823 child->GetRole() != ui::AX_ROLE_IMAGE) {
824 return false;
827 return true;
830 bool BrowserAccessibilityAndroid::IsIframe() const {
831 base::string16 html_tag = GetString16Attribute(
832 ui::AX_ATTR_HTML_TAG);
833 return html_tag == base::ASCIIToUTF16("iframe");
836 void BrowserAccessibilityAndroid::OnDataChanged() {
837 BrowserAccessibility::OnDataChanged();
839 if (IsEditableText()) {
840 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
841 if (value != new_value_) {
842 old_value_ = new_value_;
843 new_value_ = value;
847 if (GetRole() == ui::AX_ROLE_ALERT && first_time_)
848 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
850 base::string16 live;
851 if (GetString16Attribute(
852 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
853 NotifyLiveRegionUpdate(live);
856 first_time_ = false;
859 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
860 base::string16& aria_live) {
861 if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
862 !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
863 return;
865 base::string16 text = GetText();
866 if (cached_text_ != text) {
867 if (!text.empty()) {
868 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
869 this);
871 cached_text_ = text;
875 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
876 int count = 0;
877 for (uint32 i = 0; i < PlatformChildCount(); i++) {
878 if (PlatformGetChild(i)->GetRole() == role)
879 count++;
881 return count;
884 } // namespace content