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"
16 // These are enums from android.text.InputType in Java:
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:
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:
41 ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
= 1
49 BrowserAccessibility
* BrowserAccessibility::Create() {
50 return new BrowserAccessibilityAndroid();
53 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
57 bool BrowserAccessibilityAndroid::IsNative() const {
61 void BrowserAccessibilityAndroid::OnLocationChanged() {
62 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_LOCATION_CHANGED
, this);
65 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
66 if (InternalChildCount() == 0)
69 // Iframes are always allowed to contain children.
71 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA
||
72 GetRole() == ui::AX_ROLE_WEB_AREA
) {
76 // If it has a focusable child, we definitely can't leave out children.
77 if (HasFocusableChild())
80 // Date and time controls should drop their children.
81 if (GetRole() == ui::AX_ROLE_DATE
|| GetRole() == ui::AX_ROLE_INPUT_TIME
)
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())
92 // Focusable nodes with text can drop their children.
93 if (HasState(ui::AX_STATE_FOCUSABLE
) && !name
.empty())
96 // Nodes with only static text as children can drop their children.
97 if (HasOnlyStaticTextChildren())
101 return BrowserAccessibility::PlatformIsLeaf();
104 bool BrowserAccessibilityAndroid::IsCheckable() const {
105 bool checkable
= false;
106 bool is_aria_pressed_defined
;
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
) {
116 if (HasState(ui::AX_STATE_CHECKED
))
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())
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 {
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
);
171 GetRole() == ui::AX_ROLE_WEB_AREA
) {
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())
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
;
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";
248 case ui::AX_ROLE_SLIDER
:
249 class_name
= "android.widget.SeekBar";
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";
258 case ui::AX_ROLE_BUTTON
:
259 case ui::AX_ROLE_MENU_BUTTON
:
260 class_name
= "android.widget.Button";
262 case ui::AX_ROLE_CHECK_BOX
:
263 case ui::AX_ROLE_SWITCH
:
264 class_name
= "android.widget.CheckBox";
266 case ui::AX_ROLE_RADIO_BUTTON
:
267 class_name
= "android.widget.RadioButton";
269 case ui::AX_ROLE_TOGGLE_BUTTON
:
270 class_name
= "android.widget.ToggleButton";
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";
277 case ui::AX_ROLE_METER
:
278 case ui::AX_ROLE_PROGRESS_INDICATOR
:
279 class_name
= "android.widget.ProgressBar";
281 case ui::AX_ROLE_TAB_LIST
:
282 class_name
= "android.widget.TabWidget";
284 case ui::AX_ROLE_GRID
:
285 case ui::AX_ROLE_TABLE
:
286 class_name
= "android.widget.GridView";
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";
293 case ui::AX_ROLE_DIALOG
:
294 class_name
= "android.app.Dialog";
296 case ui::AX_ROLE_ROOT_WEB_AREA
:
297 class_name
= "android.webkit.WebView";
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";
305 class_name
= "android.view.View";
312 base::string16
BrowserAccessibilityAndroid::GetText() const {
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
324 base::string16 value
= GetString16Attribute(ui::AX_ATTR_VALUE
);
325 if (!value
.empty()) {
326 if (HasState(ui::AX_STATE_EDITABLE
))
330 case ui::AX_ROLE_COMBO_BOX
:
331 case ui::AX_ROLE_POP_UP_BUTTON
:
332 case ui::AX_ROLE_TEXT_FIELD
:
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
)
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
;
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
);
376 if (!description
.empty())
378 else if (title_elem_id
&& !name
.empty())
380 else if (!help
.empty())
382 else if (!name
.empty())
384 else if (!placeholder
.empty())
386 else if (!value
.empty())
388 else if (title_elem_id
) {
389 BrowserAccessibility
* title_elem
=
390 manager()->GetFromID(title_elem_id
);
392 text
= static_cast<BrowserAccessibilityAndroid
*>(title_elem
)->GetText();
395 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
398 (HasOnlyStaticTextChildren() ||
399 (IsFocusable() && HasOnlyTextAndImageChildren()))) {
400 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
401 BrowserAccessibility
* child
= InternalGetChild(i
);
402 text
+= static_cast<BrowserAccessibilityAndroid
*>(child
)->GetText();
406 if (text
.empty() && (IsLink() || GetRole() == ui::AX_ROLE_IMAGE
)) {
407 base::string16 url
= GetString16Attribute(ui::AX_ATTR_URL
);
408 // Given a url like http://foo.com/bar/baz.png, just return the
409 // base name, e.g., "baz".
410 int trailing_slashes
= 0;
411 while (url
.size() - trailing_slashes
> 0 &&
412 url
[url
.size() - trailing_slashes
- 1] == '/') {
415 if (trailing_slashes
)
416 url
= url
.substr(0, url
.size() - trailing_slashes
);
417 size_t slash_index
= url
.rfind('/');
418 if (slash_index
!= std::string::npos
)
419 url
= url
.substr(slash_index
+ 1);
420 size_t dot_index
= url
.rfind('.');
421 if (dot_index
!= std::string::npos
)
422 url
= url
.substr(0, dot_index
);
429 int BrowserAccessibilityAndroid::GetItemIndex() const {
432 case ui::AX_ROLE_LIST_ITEM
:
433 case ui::AX_ROLE_LIST_BOX_OPTION
:
434 case ui::AX_ROLE_TREE_ITEM
:
435 index
= GetIntAttribute(ui::AX_ATTR_POS_IN_SET
) - 1;
437 case ui::AX_ROLE_SLIDER
:
438 case ui::AX_ROLE_PROGRESS_INDICATOR
: {
439 // Return a percentage here for live feedback in an AccessibilityEvent.
440 // The exact value is returned in RangeCurrentValue.
441 float min
= GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
442 float max
= GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
443 float value
= GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
444 if (max
> min
&& value
>= min
&& value
<= max
)
445 index
= static_cast<int>(((value
- min
)) * 100 / (max
- min
));
452 int BrowserAccessibilityAndroid::GetItemCount() const {
455 case ui::AX_ROLE_LIST
:
456 case ui::AX_ROLE_LIST_BOX
:
457 case ui::AX_ROLE_DESCRIPTION_LIST
:
458 count
= PlatformChildCount();
460 case ui::AX_ROLE_SLIDER
:
461 case ui::AX_ROLE_PROGRESS_INDICATOR
:
462 // An AccessibilityEvent can only return integer information about a
463 // seek control, so we return a percentage. The real range is returned
464 // in RangeMin and RangeMax.
471 bool BrowserAccessibilityAndroid::CanScrollForward() const {
473 float value
= GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
474 float max
= GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
477 return GetScrollX() < GetMaxScrollX() ||
478 GetScrollY() < GetMaxScrollY();
482 bool BrowserAccessibilityAndroid::CanScrollBackward() const {
484 float value
= GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
485 float min
= GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
488 return GetScrollX() > GetMinScrollX() ||
489 GetScrollY() > GetMinScrollY();
493 bool BrowserAccessibilityAndroid::CanScrollUp() const {
494 return GetScrollY() > GetMinScrollY();
497 bool BrowserAccessibilityAndroid::CanScrollDown() const {
498 return GetScrollY() < GetMaxScrollY();
501 bool BrowserAccessibilityAndroid::CanScrollLeft() const {
502 return GetScrollX() > GetMinScrollX();
505 bool BrowserAccessibilityAndroid::CanScrollRight() const {
506 return GetScrollX() < GetMaxScrollX();
509 int BrowserAccessibilityAndroid::GetScrollX() const {
511 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
515 int BrowserAccessibilityAndroid::GetScrollY() const {
517 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &value
);
521 int BrowserAccessibilityAndroid::GetMinScrollX() const {
522 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MIN
);
525 int BrowserAccessibilityAndroid::GetMinScrollY() const {
526 return GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MIN
);
529 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
530 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
);
533 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
534 return GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
);
537 bool BrowserAccessibilityAndroid::Scroll(int direction
) const {
538 int x
= GetIntAttribute(ui::AX_ATTR_SCROLL_X
);
539 int x_min
= GetIntAttribute(ui::AX_ATTR_SCROLL_X_MIN
);
540 int x_max
= GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX
);
541 int y
= GetIntAttribute(ui::AX_ATTR_SCROLL_Y
);
542 int y_min
= GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MIN
);
543 int y_max
= GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX
);
545 // Figure out the bounding box of the visible portion of this scrollable
546 // view so we know how much to scroll by.
548 if (GetRole() == ui::AX_ROLE_ROOT_WEB_AREA
) {
549 // If this is the root web area, use the bounds of the view to determine
550 // how big one page is.
551 if (!manager()->delegate())
553 bounds
= manager()->delegate()->AccessibilityGetViewBounds();
554 } else if (GetRole() == ui::AX_ROLE_WEB_AREA
) {
555 // If this is a web area inside of an iframe, try to use the bounds of
556 // the containing element.
557 BrowserAccessibility
* parent
= GetParent();
558 while (parent
&& (parent
->GetLocation().width() == 0 ||
559 parent
->GetLocation().height() == 0)) {
560 parent
= parent
->GetParent();
563 bounds
= parent
->GetLocation();
565 bounds
= GetLocation();
567 // Otherwise this is something like a scrollable div, just use the
568 // bounds of this object itself.
569 bounds
= GetLocation();
572 // Scroll by 80% of one page.
573 int page_x
= std::max(bounds
.width() * 4 / 5, 1);
574 int page_y
= std::max(bounds
.height() * 4 / 5, 1);
576 if (direction
== FORWARD
)
577 direction
= y_max
> y_min
? DOWN
: RIGHT
;
578 if (direction
== BACKWARD
)
579 direction
= y_max
> y_min
? UP
: LEFT
;
583 y
= std::min(std::max(y
- page_y
, y_min
), y_max
);
586 y
= std::min(std::max(y
+ page_y
, y_min
), y_max
);
589 x
= std::min(std::max(x
- page_x
, x_min
), x_max
);
592 x
= std::min(std::max(x
+ page_x
, x_min
), x_max
);
598 manager()->SetScrollOffset(*this, gfx::Point(x
, y
));
602 // Given arbitrary old_value_ and new_value_, we must come up with reasonable
603 // edit metrics. Although edits like "apple" > "apples" are typical, anything
604 // is possible, such as "apple" > "applesauce", "apple" > "boot", or "" >
605 // "supercalifragilisticexpialidocious". So we consider old_value_ to be of the
606 // form AXB and new_value_ to be of the form AYB, where X and Y are the pieces
607 // that don't match. We take the X to be the "removed" characters and Y to be
608 // the "added" characters.
610 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
612 return CommonPrefixLength(old_value_
, new_value_
);
615 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
616 // This is len(AYB) - (len(A) + len(B)), or len(Y), the added characters.
617 return new_value_
.length() - CommonEndLengths(old_value_
, new_value_
);
620 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
621 // This is len(AXB) - (len(A) + len(B)), or len(X), the removed characters.
622 return old_value_
.length() - CommonEndLengths(old_value_
, new_value_
);
626 size_t BrowserAccessibilityAndroid::CommonPrefixLength(
627 const base::string16 a
,
628 const base::string16 b
) {
629 size_t a_len
= a
.length();
630 size_t b_len
= b
.length();
641 size_t BrowserAccessibilityAndroid::CommonSuffixLength(
642 const base::string16 a
,
643 const base::string16 b
) {
644 size_t a_len
= a
.length();
645 size_t b_len
= b
.length();
649 a
[a_len
- i
- 1] == b
[b_len
- i
- 1]) {
656 size_t BrowserAccessibilityAndroid::CommonEndLengths(
657 const base::string16 a
,
658 const base::string16 b
) {
659 size_t prefix_len
= CommonPrefixLength(a
, b
);
660 // Remove the matching prefix before finding the suffix. Otherwise, if
661 // old_value_ is "a" and new_value_ is "aa", "a" will be double-counted as
662 // both a prefix and a suffix of "aa".
663 base::string16 a_body
= a
.substr(prefix_len
, std::string::npos
);
664 base::string16 b_body
= b
.substr(prefix_len
, std::string::npos
);
665 size_t suffix_len
= CommonSuffixLength(a_body
, b_body
);
666 return prefix_len
+ suffix_len
;
669 base::string16
BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
673 int BrowserAccessibilityAndroid::GetSelectionStart() const {
675 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
679 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
681 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &sel_end
);
685 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
686 base::string16 value
= GetString16Attribute(ui::AX_ATTR_VALUE
);
687 return value
.length();
690 int BrowserAccessibilityAndroid::AndroidInputType() const {
691 std::string html_tag
= GetStringAttribute(
692 ui::AX_ATTR_HTML_TAG
);
693 if (html_tag
!= "input")
694 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
697 if (!GetHtmlAttribute("type", &type
))
698 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
700 if (type
.empty() || type
== "text" || type
== "search")
701 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT
;
702 else if (type
== "date")
703 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
704 else if (type
== "datetime" || type
== "datetime-local")
705 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
706 else if (type
== "email")
707 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL
;
708 else if (type
== "month")
709 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE
;
710 else if (type
== "number")
711 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER
;
712 else if (type
== "password")
713 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD
;
714 else if (type
== "tel")
715 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE
;
716 else if (type
== "time")
717 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME
;
718 else if (type
== "url")
719 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI
;
720 else if (type
== "week")
721 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME
;
723 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL
;
726 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
727 std::string live
= GetStringAttribute(
728 ui::AX_ATTR_LIVE_STATUS
);
729 if (live
== "polite")
730 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE
;
731 else if (live
== "assertive")
732 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE
;
733 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE
;
736 int BrowserAccessibilityAndroid::AndroidRangeType() const {
737 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT
;
740 int BrowserAccessibilityAndroid::RowCount() const {
741 if (GetRole() == ui::AX_ROLE_GRID
||
742 GetRole() == ui::AX_ROLE_TABLE
) {
743 return CountChildrenWithRole(ui::AX_ROLE_ROW
);
746 if (GetRole() == ui::AX_ROLE_LIST
||
747 GetRole() == ui::AX_ROLE_LIST_BOX
||
748 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST
||
749 GetRole() == ui::AX_ROLE_TREE
) {
750 return PlatformChildCount();
756 int BrowserAccessibilityAndroid::ColumnCount() const {
757 if (GetRole() == ui::AX_ROLE_GRID
||
758 GetRole() == ui::AX_ROLE_TABLE
) {
759 return CountChildrenWithRole(ui::AX_ROLE_COLUMN
);
764 int BrowserAccessibilityAndroid::RowIndex() const {
765 if (GetRole() == ui::AX_ROLE_LIST_ITEM
||
766 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION
||
767 GetRole() == ui::AX_ROLE_TREE_ITEM
) {
768 return GetIndexInParent();
771 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX
);
774 int BrowserAccessibilityAndroid::RowSpan() const {
775 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN
);
778 int BrowserAccessibilityAndroid::ColumnIndex() const {
779 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX
);
782 int BrowserAccessibilityAndroid::ColumnSpan() const {
783 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN
);
786 float BrowserAccessibilityAndroid::RangeMin() const {
787 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
790 float BrowserAccessibilityAndroid::RangeMax() const {
791 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
794 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
795 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
798 void BrowserAccessibilityAndroid::GetGranularityBoundaries(
800 std::vector
<int32
>* starts
,
801 std::vector
<int32
>* ends
,
803 switch (granularity
) {
804 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE
:
805 GetLineBoundaries(starts
, ends
, offset
);
807 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD
:
808 GetWordBoundaries(starts
, ends
, offset
);
815 void BrowserAccessibilityAndroid::GetLineBoundaries(
816 std::vector
<int32
>* line_starts
,
817 std::vector
<int32
>* line_ends
,
819 // If this node has no children, treat it as all one line.
820 if (GetText().size() > 0 && !InternalChildCount()) {
821 line_starts
->push_back(offset
);
822 line_ends
->push_back(offset
+ GetText().size());
825 // If this is a static text node, get the line boundaries from the
826 // inline text boxes if possible.
827 if (GetRole() == ui::AX_ROLE_STATIC_TEXT
) {
829 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
830 BrowserAccessibilityAndroid
* child
=
831 static_cast<BrowserAccessibilityAndroid
*>(InternalGetChild(i
));
832 CHECK_EQ(ui::AX_ROLE_INLINE_TEXT_BOX
, child
->GetRole());
833 // TODO(dmazzoni): replace this with a proper API to determine
834 // if two inline text boxes are on the same line. http://crbug.com/421771
835 int y
= child
->GetLocation().y();
837 line_starts
->push_back(offset
);
838 } else if (y
!= last_y
) {
839 line_ends
->push_back(offset
);
840 line_starts
->push_back(offset
);
842 offset
+= child
->GetText().size();
845 line_ends
->push_back(offset
);
849 // Otherwise, call GetLineBoundaries recursively on the children.
850 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
851 BrowserAccessibilityAndroid
* child
=
852 static_cast<BrowserAccessibilityAndroid
*>(InternalGetChild(i
));
853 child
->GetLineBoundaries(line_starts
, line_ends
, offset
);
854 offset
+= child
->GetText().size();
858 void BrowserAccessibilityAndroid::GetWordBoundaries(
859 std::vector
<int32
>* word_starts
,
860 std::vector
<int32
>* word_ends
,
862 if (GetRole() == ui::AX_ROLE_INLINE_TEXT_BOX
) {
863 const std::vector
<int32
>& starts
= GetIntListAttribute(
864 ui::AX_ATTR_WORD_STARTS
);
865 const std::vector
<int32
>& ends
= GetIntListAttribute(
866 ui::AX_ATTR_WORD_ENDS
);
867 for (size_t i
= 0; i
< starts
.size(); ++i
) {
868 word_starts
->push_back(offset
+ starts
[i
]);
869 word_ends
->push_back(offset
+ ends
[i
]);
874 base::string16 concatenated_text
;
875 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
876 BrowserAccessibilityAndroid
* child
=
877 static_cast<BrowserAccessibilityAndroid
*>(InternalGetChild(i
));
878 base::string16 child_text
= child
->GetText();
879 concatenated_text
+= child
->GetText();
882 base::string16 text
= GetText();
883 if (text
.empty() || concatenated_text
== text
) {
884 // Great - this node is just the concatenation of its children, so
885 // we can get the word boundaries recursively.
886 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
887 BrowserAccessibilityAndroid
* child
=
888 static_cast<BrowserAccessibilityAndroid
*>(InternalGetChild(i
));
889 child
->GetWordBoundaries(word_starts
, word_ends
, offset
);
890 offset
+= child
->GetText().size();
893 // This node has its own accessible text that doesn't match its
894 // visible text - like alt text for an image or something with an
895 // aria-label, so split the text into words locally.
896 base::i18n::BreakIterator
iter(text
, base::i18n::BreakIterator::BREAK_WORD
);
899 while (iter
.Advance()) {
901 word_starts
->push_back(iter
.prev());
902 word_ends
->push_back(iter
.pos());
908 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
909 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
911 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
912 BrowserAccessibility
* child
= InternalGetChild(i
);
913 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
915 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
921 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
922 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
924 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
925 BrowserAccessibility
* child
= InternalGetChild(i
);
926 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
)
932 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
933 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
935 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
936 BrowserAccessibility
* child
= InternalGetChild(i
);
937 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
&&
938 child
->GetRole() != ui::AX_ROLE_IMAGE
) {
945 bool BrowserAccessibilityAndroid::IsIframe() const {
946 base::string16 html_tag
= GetString16Attribute(
947 ui::AX_ATTR_HTML_TAG
);
948 return html_tag
== base::ASCIIToUTF16("iframe");
951 void BrowserAccessibilityAndroid::OnDataChanged() {
952 BrowserAccessibility::OnDataChanged();
954 if (IsEditableText()) {
955 base::string16 value
= GetString16Attribute(ui::AX_ATTR_VALUE
);
956 if (value
!= new_value_
) {
957 old_value_
= new_value_
;
962 if (GetRole() == ui::AX_ROLE_ALERT
&& first_time_
)
963 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
966 if (GetString16Attribute(
967 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
968 NotifyLiveRegionUpdate(live
);
974 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
975 base::string16
& aria_live
) {
976 if (!base::EqualsASCII(aria_live
, aria_strings::kAriaLivePolite
) &&
977 !base::EqualsASCII(aria_live
, aria_strings::kAriaLiveAssertive
))
980 base::string16 text
= GetText();
981 if (cached_text_
!= text
) {
983 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
990 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
992 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
993 if (PlatformGetChild(i
)->GetRole() == role
)
999 } // namespace content