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 (!name
.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(" ");
386 } else if (!help
.empty())
388 else if (!placeholder
.empty())
390 else if (!value
.empty())
392 else if (title_elem_id
) {
393 BrowserAccessibility
* title_elem
=
394 manager()->GetFromID(title_elem_id
);
396 text
= static_cast<BrowserAccessibilityAndroid
*>(title_elem
)->GetText();
399 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
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] == '/') {
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
);
433 int BrowserAccessibilityAndroid::GetItemIndex() const {
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;
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
));
456 int BrowserAccessibilityAndroid::GetItemCount() const {
459 case ui::AX_ROLE_LIST
:
460 case ui::AX_ROLE_LIST_BOX
:
461 case ui::AX_ROLE_DESCRIPTION_LIST
:
462 count
= PlatformChildCount();
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.
475 bool BrowserAccessibilityAndroid::CanScrollForward() const {
477 float value
= GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
478 float max
= GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE
);
481 return GetScrollX() < GetMaxScrollX() ||
482 GetScrollY() < GetMaxScrollY();
486 bool BrowserAccessibilityAndroid::CanScrollBackward() const {
488 float value
= GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE
);
489 float min
= GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE
);
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 {
515 GetIntAttribute(ui::AX_ATTR_SCROLL_X
, &value
);
519 int BrowserAccessibilityAndroid::GetScrollY() const {
521 GetIntAttribute(ui::AX_ATTR_SCROLL_Y
, &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.
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())
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();
567 bounds
= parent
->GetLocation();
569 bounds
= GetLocation();
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
;
587 y
= std::min(std::max(y
- page_y
, y_min
), y_max
);
590 y
= std::min(std::max(y
+ page_y
, y_min
), y_max
);
593 x
= std::min(std::max(x
- page_x
, x_min
), x_max
);
596 x
= std::min(std::max(x
+ page_x
, x_min
), x_max
);
602 manager()->SetScrollOffset(*this, gfx::Point(x
, y
));
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 {
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_
);
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();
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();
653 a
[a_len
- i
- 1] == b
[b_len
- i
- 1]) {
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 {
677 int BrowserAccessibilityAndroid::GetSelectionStart() const {
679 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START
, &sel_start
);
683 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
685 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END
, &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
;
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();
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
);
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(
804 std::vector
<int32
>* starts
,
805 std::vector
<int32
>* ends
,
807 switch (granularity
) {
808 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE
:
809 GetLineBoundaries(starts
, ends
, offset
);
811 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD
:
812 GetWordBoundaries(starts
, ends
, offset
);
819 void BrowserAccessibilityAndroid::GetLineBoundaries(
820 std::vector
<int32
>* line_starts
,
821 std::vector
<int32
>* line_ends
,
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
) {
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();
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();
849 line_ends
->push_back(offset
);
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
,
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
]);
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();
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
);
903 while (iter
.Advance()) {
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
915 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
916 BrowserAccessibility
* child
= InternalGetChild(i
);
917 if (child
->HasState(ui::AX_STATE_FOCUSABLE
))
919 if (static_cast<BrowserAccessibilityAndroid
*>(child
)->HasFocusableChild())
925 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
926 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
928 for (uint32 i
= 0; i
< InternalChildCount(); i
++) {
929 BrowserAccessibility
* child
= InternalGetChild(i
);
930 if (child
->GetRole() != ui::AX_ROLE_STATIC_TEXT
)
936 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
937 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
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
) {
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_
;
966 if (GetRole() == ui::AX_ROLE_ALERT
&& first_time_
)
967 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT
, this);
970 if (GetString16Attribute(
971 ui::AX_ATTR_CONTAINER_LIVE_STATUS
, &live
)) {
972 NotifyLiveRegionUpdate(live
);
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
))
984 base::string16 text
= GetText();
985 if (cached_text_
!= text
) {
987 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW
,
994 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role
) const {
996 for (uint32 i
= 0; i
< PlatformChildCount(); i
++) {
997 if (PlatformGetChild(i
)->GetRole() == role
)
1003 } // namespace content