Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blobc16eb8d7a1345da9450cd45af1bd77c72705231b
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_INPUT_TIME)
82 return true;
84 BrowserAccessibilityManagerAndroid* manager_android =
85 static_cast<BrowserAccessibilityManagerAndroid*>(manager());
86 if (manager_android->prune_tree_for_screen_reader()) {
87 // Headings with text can drop their children.
88 base::string16 name = GetText();
89 if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
90 return true;
92 // Focusable nodes with text can drop their children.
93 if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
94 return true;
96 // Nodes with only static text as children can drop their children.
97 if (HasOnlyStaticTextChildren())
98 return true;
101 return BrowserAccessibility::PlatformIsLeaf();
104 bool BrowserAccessibilityAndroid::IsCheckable() const {
105 bool checkable = false;
106 bool is_aria_pressed_defined;
107 bool is_mixed;
108 GetAriaTristate("aria-pressed", &is_aria_pressed_defined, &is_mixed);
109 if (GetRole() == ui::AX_ROLE_CHECK_BOX ||
110 GetRole() == ui::AX_ROLE_RADIO_BUTTON ||
111 GetRole() == ui::AX_ROLE_MENU_ITEM_CHECK_BOX ||
112 GetRole() == ui::AX_ROLE_MENU_ITEM_RADIO ||
113 is_aria_pressed_defined) {
114 checkable = true;
116 if (HasState(ui::AX_STATE_CHECKED))
117 checkable = true;
118 return checkable;
121 bool BrowserAccessibilityAndroid::IsChecked() const {
122 return (HasState(ui::AX_STATE_CHECKED) || HasState(ui::AX_STATE_PRESSED));
125 bool BrowserAccessibilityAndroid::IsClickable() const {
126 if (!PlatformIsLeaf())
127 return false;
129 return IsFocusable() || !GetText().empty();
132 bool BrowserAccessibilityAndroid::IsCollection() const {
133 return (GetRole() == ui::AX_ROLE_GRID ||
134 GetRole() == ui::AX_ROLE_LIST ||
135 GetRole() == ui::AX_ROLE_LIST_BOX ||
136 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
137 GetRole() == ui::AX_ROLE_TABLE ||
138 GetRole() == ui::AX_ROLE_TREE);
141 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
142 return (GetRole() == ui::AX_ROLE_CELL ||
143 GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
144 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM ||
145 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
146 GetRole() == ui::AX_ROLE_LIST_ITEM ||
147 GetRole() == ui::AX_ROLE_ROW_HEADER ||
148 GetRole() == ui::AX_ROLE_TREE_ITEM);
151 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
152 std::string invalid;
153 return GetHtmlAttribute("aria-invalid", &invalid);
156 bool BrowserAccessibilityAndroid::IsDismissable() const {
157 return false; // No concept of "dismissable" on the web currently.
160 bool BrowserAccessibilityAndroid::IsEditableText() const {
161 return GetRole() == ui::AX_ROLE_TEXT_FIELD;
164 bool BrowserAccessibilityAndroid::IsEnabled() const {
165 return HasState(ui::AX_STATE_ENABLED);
168 bool BrowserAccessibilityAndroid::IsFocusable() const {
169 bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
170 if (IsIframe() ||
171 GetRole() == ui::AX_ROLE_WEB_AREA) {
172 focusable = false;
174 return focusable;
177 bool BrowserAccessibilityAndroid::IsFocused() const {
178 return manager()->GetFocus(manager()->GetRoot()) == this;
181 bool BrowserAccessibilityAndroid::IsHeading() const {
182 BrowserAccessibilityAndroid* parent =
183 static_cast<BrowserAccessibilityAndroid*>(GetParent());
184 if (parent && parent->IsHeading())
185 return true;
187 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
188 GetRole() == ui::AX_ROLE_HEADING ||
189 GetRole() == ui::AX_ROLE_ROW_HEADER);
192 bool BrowserAccessibilityAndroid::IsHierarchical() const {
193 return (GetRole() == ui::AX_ROLE_LIST ||
194 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
195 GetRole() == ui::AX_ROLE_TREE);
198 bool BrowserAccessibilityAndroid::IsLink() const {
199 return (GetRole() == ui::AX_ROLE_LINK ||
200 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK);
203 bool BrowserAccessibilityAndroid::IsMultiLine() const {
204 return HasState(ui::AX_STATE_MULTILINE);
207 bool BrowserAccessibilityAndroid::IsPassword() const {
208 return HasState(ui::AX_STATE_PROTECTED);
211 bool BrowserAccessibilityAndroid::IsRangeType() const {
212 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR ||
213 GetRole() == ui::AX_ROLE_METER ||
214 GetRole() == ui::AX_ROLE_SCROLL_BAR ||
215 GetRole() == ui::AX_ROLE_SLIDER);
218 bool BrowserAccessibilityAndroid::IsScrollable() const {
219 return (HasIntAttribute(ui::AX_ATTR_SCROLL_X_MAX) &&
220 GetRole() != ui::AX_ROLE_SCROLL_AREA);
223 bool BrowserAccessibilityAndroid::IsSelected() const {
224 return HasState(ui::AX_STATE_SELECTED);
227 bool BrowserAccessibilityAndroid::IsSlider() const {
228 return GetRole() == ui::AX_ROLE_SLIDER;
231 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
232 return !HasState(ui::AX_STATE_INVISIBLE);
235 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
236 return HasState(ui::AX_STATE_HASPOPUP);
239 const char* BrowserAccessibilityAndroid::GetClassName() const {
240 const char* class_name = NULL;
242 switch (GetRole()) {
243 case ui::AX_ROLE_SEARCH_BOX:
244 case ui::AX_ROLE_SPIN_BUTTON:
245 case ui::AX_ROLE_TEXT_FIELD:
246 class_name = "android.widget.EditText";
247 break;
248 case ui::AX_ROLE_SLIDER:
249 class_name = "android.widget.SeekBar";
250 break;
251 case ui::AX_ROLE_COLOR_WELL:
252 case ui::AX_ROLE_COMBO_BOX:
253 case ui::AX_ROLE_DATE:
254 case ui::AX_ROLE_POP_UP_BUTTON:
255 case ui::AX_ROLE_INPUT_TIME:
256 class_name = "android.widget.Spinner";
257 break;
258 case ui::AX_ROLE_BUTTON:
259 case ui::AX_ROLE_MENU_BUTTON:
260 class_name = "android.widget.Button";
261 break;
262 case ui::AX_ROLE_CHECK_BOX:
263 case ui::AX_ROLE_SWITCH:
264 class_name = "android.widget.CheckBox";
265 break;
266 case ui::AX_ROLE_RADIO_BUTTON:
267 class_name = "android.widget.RadioButton";
268 break;
269 case ui::AX_ROLE_TOGGLE_BUTTON:
270 class_name = "android.widget.ToggleButton";
271 break;
272 case ui::AX_ROLE_CANVAS:
273 case ui::AX_ROLE_IMAGE:
274 case ui::AX_ROLE_SVG_ROOT:
275 class_name = "android.widget.Image";
276 break;
277 case ui::AX_ROLE_METER:
278 case ui::AX_ROLE_PROGRESS_INDICATOR:
279 class_name = "android.widget.ProgressBar";
280 break;
281 case ui::AX_ROLE_TAB_LIST:
282 class_name = "android.widget.TabWidget";
283 break;
284 case ui::AX_ROLE_GRID:
285 case ui::AX_ROLE_TABLE:
286 class_name = "android.widget.GridView";
287 break;
288 case ui::AX_ROLE_LIST:
289 case ui::AX_ROLE_LIST_BOX:
290 case ui::AX_ROLE_DESCRIPTION_LIST:
291 class_name = "android.widget.ListView";
292 break;
293 case ui::AX_ROLE_DIALOG:
294 class_name = "android.app.Dialog";
295 break;
296 case ui::AX_ROLE_ROOT_WEB_AREA:
297 class_name = "android.webkit.WebView";
298 break;
299 case ui::AX_ROLE_MENU_ITEM:
300 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
301 case ui::AX_ROLE_MENU_ITEM_RADIO:
302 class_name = "android.view.MenuItem";
303 break;
304 default:
305 class_name = "android.view.View";
306 break;
309 return class_name;
312 base::string16 BrowserAccessibilityAndroid::GetText() const {
313 if (IsIframe() ||
314 GetRole() == ui::AX_ROLE_WEB_AREA) {
315 return base::string16();
318 // See comment in browser_accessibility_win.cc for details.
319 // The difference here is that we can only expose one accessible
320 // name on Android, not 2 or 3 like on Windows or Mac.
322 // First, always return the |value| attribute if this is an
323 // input field.
324 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
325 if (!value.empty()) {
326 if (HasState(ui::AX_STATE_EDITABLE))
327 return value;
329 switch (GetRole()) {
330 case ui::AX_ROLE_COMBO_BOX:
331 case ui::AX_ROLE_POP_UP_BUTTON:
332 case ui::AX_ROLE_TEXT_FIELD:
333 return value;
337 // For color wells, the color is stored in separate attributes.
338 // Perhaps we could return color names in the future?
339 if (GetRole() == ui::AX_ROLE_COLOR_WELL) {
340 int color = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE);
341 int red = (color >> 16) & 0xFF;
342 int green = (color >> 8) & 0xFF;
343 int blue = color & 0xFF;
344 return base::UTF8ToUTF16(
345 base::StringPrintf("#%02X%02X%02X", red, green, blue));
348 // Always prefer visible text if this is a link. Sites sometimes add
349 // a "title" attribute to a link with more information, but we can't
350 // lose the link text.
351 base::string16 name = GetString16Attribute(ui::AX_ATTR_NAME);
352 if (!name.empty() && GetRole() == ui::AX_ROLE_LINK)
353 return name;
355 // If there's no text value, the basic rule is: prefer description
356 // (aria-labelledby or aria-label), then help (title), then name
357 // (inner text), then value (control value). However, if
358 // title_elem_id is set, that means there's a label element
359 // supplying the name and then name takes precedence over help.
360 // TODO(dmazzoni): clean this up by providing more granular labels in
361 // Blink, making the platform-specific mapping to accessible text simpler.
362 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
363 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
365 base::string16 placeholder;
366 switch (GetRole()) {
367 case ui::AX_ROLE_DATE:
368 case ui::AX_ROLE_INPUT_TIME:
369 case ui::AX_ROLE_TEXT_FIELD:
370 GetHtmlAttribute("placeholder", &placeholder);
373 int title_elem_id = GetIntAttribute(
374 ui::AX_ATTR_TITLE_UI_ELEMENT);
375 base::string16 text;
376 if (!description.empty())
377 text = description;
378 else if (!name.empty()) {
379 text = name;
380 if (!help.empty()) {
381 // TODO(vkuzkokov): This is not the best way to pass 2 texts but this is
382 // how Blink seems to be doing it.
383 text += base::ASCIIToUTF16(" ");
384 text += help;
386 } else if (!help.empty())
387 text = help;
388 else if (!placeholder.empty())
389 text = placeholder;
390 else if (!value.empty())
391 text = value;
392 else if (title_elem_id) {
393 BrowserAccessibility* title_elem =
394 manager()->GetFromID(title_elem_id);
395 if (title_elem)
396 text = static_cast<BrowserAccessibilityAndroid*>(title_elem)->GetText();
399 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
400 // from within this!
401 if (text.empty() &&
402 (HasOnlyStaticTextChildren() ||
403 (IsFocusable() && HasOnlyTextAndImageChildren()))) {
404 for (uint32 i = 0; i < InternalChildCount(); i++) {
405 BrowserAccessibility* child = InternalGetChild(i);
406 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
410 if (text.empty() && (IsLink() || GetRole() == ui::AX_ROLE_IMAGE)) {
411 base::string16 url = GetString16Attribute(ui::AX_ATTR_URL);
412 // Given a url like http://foo.com/bar/baz.png, just return the
413 // base name, e.g., "baz".
414 int trailing_slashes = 0;
415 while (url.size() - trailing_slashes > 0 &&
416 url[url.size() - trailing_slashes - 1] == '/') {
417 trailing_slashes++;
419 if (trailing_slashes)
420 url = url.substr(0, url.size() - trailing_slashes);
421 size_t slash_index = url.rfind('/');
422 if (slash_index != std::string::npos)
423 url = url.substr(slash_index + 1);
424 size_t dot_index = url.rfind('.');
425 if (dot_index != std::string::npos)
426 url = url.substr(0, dot_index);
427 text = url;
430 return text;
433 int BrowserAccessibilityAndroid::GetItemIndex() const {
434 int index = 0;
435 switch (GetRole()) {
436 case ui::AX_ROLE_LIST_ITEM:
437 case ui::AX_ROLE_LIST_BOX_OPTION:
438 case ui::AX_ROLE_TREE_ITEM:
439 index = GetIntAttribute(ui::AX_ATTR_POS_IN_SET) - 1;
440 break;
441 case ui::AX_ROLE_SLIDER:
442 case ui::AX_ROLE_PROGRESS_INDICATOR: {
443 // Return a percentage here for live feedback in an AccessibilityEvent.
444 // The exact value is returned in RangeCurrentValue.
445 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
446 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
447 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
448 if (max > min && value >= min && value <= max)
449 index = static_cast<int>(((value - min)) * 100 / (max - min));
450 break;
453 return index;
456 int BrowserAccessibilityAndroid::GetItemCount() const {
457 int count = 0;
458 switch (GetRole()) {
459 case ui::AX_ROLE_LIST:
460 case ui::AX_ROLE_LIST_BOX:
461 case ui::AX_ROLE_DESCRIPTION_LIST:
462 count = PlatformChildCount();
463 break;
464 case ui::AX_ROLE_SLIDER:
465 case ui::AX_ROLE_PROGRESS_INDICATOR:
466 // An AccessibilityEvent can only return integer information about a
467 // seek control, so we return a percentage. The real range is returned
468 // in RangeMin and RangeMax.
469 count = 100;
470 break;
472 return count;
475 bool BrowserAccessibilityAndroid::CanScrollForward() const {
476 if (IsSlider()) {
477 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
478 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
479 return value < max;
480 } else {
481 return GetScrollX() < GetMaxScrollX() ||
482 GetScrollY() < GetMaxScrollY();
486 bool BrowserAccessibilityAndroid::CanScrollBackward() const {
487 if (IsSlider()) {
488 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
489 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
490 return value > min;
491 } else {
492 return GetScrollX() > GetMinScrollX() ||
493 GetScrollY() > GetMinScrollY();
497 bool BrowserAccessibilityAndroid::CanScrollUp() const {
498 return GetScrollY() > GetMinScrollY();
501 bool BrowserAccessibilityAndroid::CanScrollDown() const {
502 return GetScrollY() < GetMaxScrollY();
505 bool BrowserAccessibilityAndroid::CanScrollLeft() const {
506 return GetScrollX() > GetMinScrollX();
509 bool BrowserAccessibilityAndroid::CanScrollRight() const {
510 return GetScrollX() < GetMaxScrollX();
513 int BrowserAccessibilityAndroid::GetScrollX() const {
514 int value = 0;
515 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
516 return value;
519 int BrowserAccessibilityAndroid::GetScrollY() const {
520 int value = 0;
521 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
522 return value;
525 int BrowserAccessibilityAndroid::GetMinScrollX() const {
526 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MIN);
529 int BrowserAccessibilityAndroid::GetMinScrollY() const {
530 return GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MIN);
533 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
534 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX);
537 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
538 return GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX);
541 bool BrowserAccessibilityAndroid::Scroll(int direction) const {
542 int x = GetIntAttribute(ui::AX_ATTR_SCROLL_X);
543 int x_min = GetIntAttribute(ui::AX_ATTR_SCROLL_X_MIN);
544 int x_max = GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX);
545 int y = GetIntAttribute(ui::AX_ATTR_SCROLL_Y);
546 int y_min = GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MIN);
547 int y_max = GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX);
549 // Figure out the bounding box of the visible portion of this scrollable
550 // view so we know how much to scroll by.
551 gfx::Rect bounds;
552 if (GetRole() == ui::AX_ROLE_ROOT_WEB_AREA) {
553 // If this is the root web area, use the bounds of the view to determine
554 // how big one page is.
555 if (!manager()->delegate())
556 return false;
557 bounds = manager()->delegate()->AccessibilityGetViewBounds();
558 } else if (GetRole() == ui::AX_ROLE_WEB_AREA) {
559 // If this is a web area inside of an iframe, try to use the bounds of
560 // the containing element.
561 BrowserAccessibility* parent = GetParent();
562 while (parent && (parent->GetLocation().width() == 0 ||
563 parent->GetLocation().height() == 0)) {
564 parent = parent->GetParent();
566 if (parent)
567 bounds = parent->GetLocation();
568 else
569 bounds = GetLocation();
570 } else {
571 // Otherwise this is something like a scrollable div, just use the
572 // bounds of this object itself.
573 bounds = GetLocation();
576 // Scroll by 80% of one page.
577 int page_x = std::max(bounds.width() * 4 / 5, 1);
578 int page_y = std::max(bounds.height() * 4 / 5, 1);
580 if (direction == FORWARD)
581 direction = y_max > y_min ? DOWN : RIGHT;
582 if (direction == BACKWARD)
583 direction = y_max > y_min ? UP : LEFT;
585 switch (direction) {
586 case UP:
587 y = std::min(std::max(y - page_y, y_min), y_max);
588 break;
589 case DOWN:
590 y = std::min(std::max(y + page_y, y_min), y_max);
591 break;
592 case LEFT:
593 x = std::min(std::max(x - page_x, x_min), x_max);
594 break;
595 case RIGHT:
596 x = std::min(std::max(x + page_x, x_min), x_max);
597 break;
598 default:
599 NOTREACHED();
602 manager()->SetScrollOffset(*this, gfx::Point(x, y));
603 return true;
606 // Given arbitrary old_value_ and new_value_, we must come up with reasonable
607 // edit metrics. Although edits like "apple" > "apples" are typical, anything
608 // is possible, such as "apple" > "applesauce", "apple" > "boot", or "" >
609 // "supercalifragilisticexpialidocious". So we consider old_value_ to be of the
610 // form AXB and new_value_ to be of the form AYB, where X and Y are the pieces
611 // that don't match. We take the X to be the "removed" characters and Y to be
612 // the "added" characters.
614 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
615 // This is len(A)
616 return CommonPrefixLength(old_value_, new_value_);
619 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
620 // This is len(AYB) - (len(A) + len(B)), or len(Y), the added characters.
621 return new_value_.length() - CommonEndLengths(old_value_, new_value_);
624 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
625 // This is len(AXB) - (len(A) + len(B)), or len(X), the removed characters.
626 return old_value_.length() - CommonEndLengths(old_value_, new_value_);
629 // static
630 size_t BrowserAccessibilityAndroid::CommonPrefixLength(
631 const base::string16 a,
632 const base::string16 b) {
633 size_t a_len = a.length();
634 size_t b_len = b.length();
635 size_t i = 0;
636 while (i < a_len &&
637 i < b_len &&
638 a[i] == b[i]) {
639 i++;
641 return i;
644 // static
645 size_t BrowserAccessibilityAndroid::CommonSuffixLength(
646 const base::string16 a,
647 const base::string16 b) {
648 size_t a_len = a.length();
649 size_t b_len = b.length();
650 size_t i = 0;
651 while (i < a_len &&
652 i < b_len &&
653 a[a_len - i - 1] == b[b_len - i - 1]) {
654 i++;
656 return i;
659 // static
660 size_t BrowserAccessibilityAndroid::CommonEndLengths(
661 const base::string16 a,
662 const base::string16 b) {
663 size_t prefix_len = CommonPrefixLength(a, b);
664 // Remove the matching prefix before finding the suffix. Otherwise, if
665 // old_value_ is "a" and new_value_ is "aa", "a" will be double-counted as
666 // both a prefix and a suffix of "aa".
667 base::string16 a_body = a.substr(prefix_len, std::string::npos);
668 base::string16 b_body = b.substr(prefix_len, std::string::npos);
669 size_t suffix_len = CommonSuffixLength(a_body, b_body);
670 return prefix_len + suffix_len;
673 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
674 return old_value_;
677 int BrowserAccessibilityAndroid::GetSelectionStart() const {
678 int sel_start = 0;
679 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
680 return sel_start;
683 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
684 int sel_end = 0;
685 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
686 return sel_end;
689 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
690 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
691 return value.length();
694 int BrowserAccessibilityAndroid::AndroidInputType() const {
695 std::string html_tag = GetStringAttribute(
696 ui::AX_ATTR_HTML_TAG);
697 if (html_tag != "input")
698 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
700 std::string type;
701 if (!GetHtmlAttribute("type", &type))
702 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
704 if (type.empty() || type == "text" || type == "search")
705 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
706 else if (type == "date")
707 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
708 else if (type == "datetime" || type == "datetime-local")
709 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
710 else if (type == "email")
711 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
712 else if (type == "month")
713 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
714 else if (type == "number")
715 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
716 else if (type == "password")
717 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
718 else if (type == "tel")
719 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
720 else if (type == "time")
721 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
722 else if (type == "url")
723 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
724 else if (type == "week")
725 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
727 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
730 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
731 std::string live = GetStringAttribute(
732 ui::AX_ATTR_LIVE_STATUS);
733 if (live == "polite")
734 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
735 else if (live == "assertive")
736 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
737 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
740 int BrowserAccessibilityAndroid::AndroidRangeType() const {
741 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
744 int BrowserAccessibilityAndroid::RowCount() const {
745 if (GetRole() == ui::AX_ROLE_GRID ||
746 GetRole() == ui::AX_ROLE_TABLE) {
747 return CountChildrenWithRole(ui::AX_ROLE_ROW);
750 if (GetRole() == ui::AX_ROLE_LIST ||
751 GetRole() == ui::AX_ROLE_LIST_BOX ||
752 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
753 GetRole() == ui::AX_ROLE_TREE) {
754 return PlatformChildCount();
757 return 0;
760 int BrowserAccessibilityAndroid::ColumnCount() const {
761 if (GetRole() == ui::AX_ROLE_GRID ||
762 GetRole() == ui::AX_ROLE_TABLE) {
763 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
765 return 0;
768 int BrowserAccessibilityAndroid::RowIndex() const {
769 if (GetRole() == ui::AX_ROLE_LIST_ITEM ||
770 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
771 GetRole() == ui::AX_ROLE_TREE_ITEM) {
772 return GetIndexInParent();
775 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
778 int BrowserAccessibilityAndroid::RowSpan() const {
779 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
782 int BrowserAccessibilityAndroid::ColumnIndex() const {
783 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
786 int BrowserAccessibilityAndroid::ColumnSpan() const {
787 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
790 float BrowserAccessibilityAndroid::RangeMin() const {
791 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
794 float BrowserAccessibilityAndroid::RangeMax() const {
795 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
798 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
799 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
802 void BrowserAccessibilityAndroid::GetGranularityBoundaries(
803 int granularity,
804 std::vector<int32>* starts,
805 std::vector<int32>* ends,
806 int offset) {
807 switch (granularity) {
808 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE:
809 GetLineBoundaries(starts, ends, offset);
810 break;
811 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
812 GetWordBoundaries(starts, ends, offset);
813 break;
814 default:
815 NOTREACHED();
819 void BrowserAccessibilityAndroid::GetLineBoundaries(
820 std::vector<int32>* line_starts,
821 std::vector<int32>* line_ends,
822 int offset) {
823 // If this node has no children, treat it as all one line.
824 if (GetText().size() > 0 && !InternalChildCount()) {
825 line_starts->push_back(offset);
826 line_ends->push_back(offset + GetText().size());
829 // If this is a static text node, get the line boundaries from the
830 // inline text boxes if possible.
831 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) {
832 int last_y = 0;
833 for (uint32 i = 0; i < InternalChildCount(); i++) {
834 BrowserAccessibilityAndroid* child =
835 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
836 CHECK_EQ(ui::AX_ROLE_INLINE_TEXT_BOX, child->GetRole());
837 // TODO(dmazzoni): replace this with a proper API to determine
838 // if two inline text boxes are on the same line. http://crbug.com/421771
839 int y = child->GetLocation().y();
840 if (i == 0) {
841 line_starts->push_back(offset);
842 } else if (y != last_y) {
843 line_ends->push_back(offset);
844 line_starts->push_back(offset);
846 offset += child->GetText().size();
847 last_y = y;
849 line_ends->push_back(offset);
850 return;
853 // Otherwise, call GetLineBoundaries recursively on the children.
854 for (uint32 i = 0; i < InternalChildCount(); i++) {
855 BrowserAccessibilityAndroid* child =
856 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
857 child->GetLineBoundaries(line_starts, line_ends, offset);
858 offset += child->GetText().size();
862 void BrowserAccessibilityAndroid::GetWordBoundaries(
863 std::vector<int32>* word_starts,
864 std::vector<int32>* word_ends,
865 int offset) {
866 if (GetRole() == ui::AX_ROLE_INLINE_TEXT_BOX) {
867 const std::vector<int32>& starts = GetIntListAttribute(
868 ui::AX_ATTR_WORD_STARTS);
869 const std::vector<int32>& ends = GetIntListAttribute(
870 ui::AX_ATTR_WORD_ENDS);
871 for (size_t i = 0; i < starts.size(); ++i) {
872 word_starts->push_back(offset + starts[i]);
873 word_ends->push_back(offset + ends[i]);
875 return;
878 base::string16 concatenated_text;
879 for (uint32 i = 0; i < InternalChildCount(); i++) {
880 BrowserAccessibilityAndroid* child =
881 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
882 base::string16 child_text = child->GetText();
883 concatenated_text += child->GetText();
886 base::string16 text = GetText();
887 if (text.empty() || concatenated_text == text) {
888 // Great - this node is just the concatenation of its children, so
889 // we can get the word boundaries recursively.
890 for (uint32 i = 0; i < InternalChildCount(); i++) {
891 BrowserAccessibilityAndroid* child =
892 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
893 child->GetWordBoundaries(word_starts, word_ends, offset);
894 offset += child->GetText().size();
896 } else {
897 // This node has its own accessible text that doesn't match its
898 // visible text - like alt text for an image or something with an
899 // aria-label, so split the text into words locally.
900 base::i18n::BreakIterator iter(text, base::i18n::BreakIterator::BREAK_WORD);
901 if (!iter.Init())
902 return;
903 while (iter.Advance()) {
904 if (iter.IsWord()) {
905 word_starts->push_back(iter.prev());
906 word_ends->push_back(iter.pos());
912 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
913 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
914 // from within this!
915 for (uint32 i = 0; i < InternalChildCount(); i++) {
916 BrowserAccessibility* child = InternalGetChild(i);
917 if (child->HasState(ui::AX_STATE_FOCUSABLE))
918 return true;
919 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
920 return true;
922 return false;
925 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
926 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
927 // from within this!
928 for (uint32 i = 0; i < InternalChildCount(); i++) {
929 BrowserAccessibility* child = InternalGetChild(i);
930 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT)
931 return false;
933 return true;
936 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
937 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
938 // from within this!
939 for (uint32 i = 0; i < InternalChildCount(); i++) {
940 BrowserAccessibility* child = InternalGetChild(i);
941 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT &&
942 child->GetRole() != ui::AX_ROLE_IMAGE) {
943 return false;
946 return true;
949 bool BrowserAccessibilityAndroid::IsIframe() const {
950 base::string16 html_tag = GetString16Attribute(
951 ui::AX_ATTR_HTML_TAG);
952 return html_tag == base::ASCIIToUTF16("iframe");
955 void BrowserAccessibilityAndroid::OnDataChanged() {
956 BrowserAccessibility::OnDataChanged();
958 if (IsEditableText()) {
959 base::string16 value = GetString16Attribute(ui::AX_ATTR_VALUE);
960 if (value != new_value_) {
961 old_value_ = new_value_;
962 new_value_ = value;
966 if (GetRole() == ui::AX_ROLE_ALERT && first_time_)
967 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
969 base::string16 live;
970 if (GetString16Attribute(
971 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
972 NotifyLiveRegionUpdate(live);
975 first_time_ = false;
978 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
979 base::string16& aria_live) {
980 if (!base::EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
981 !base::EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
982 return;
984 base::string16 text = GetText();
985 if (cached_text_ != text) {
986 if (!text.empty()) {
987 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
988 this);
990 cached_text_ = text;
994 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
995 int count = 0;
996 for (uint32 i = 0; i < PlatformChildCount(); i++) {
997 if (PlatformGetChild(i)->GetRole() == role)
998 count++;
1000 return count;
1003 } // namespace content