Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blob8ea3c5639b5e3899fa780113d30748b41f0d9744
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/accessibility/browser_accessibility_android.h"
7 #include "base/i18n/break_iterator.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
12 #include "content/common/accessibility_messages.h"
14 namespace {
16 // These are enums from android.text.InputType in Java:
17 enum {
18 ANDROID_TEXT_INPUTTYPE_TYPE_NULL = 0,
19 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME = 0x4,
20 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE = 0x14,
21 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME = 0x24,
22 ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER = 0x2,
23 ANDROID_TEXT_INPUTTYPE_TYPE_PHONE = 0x3,
24 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT = 0x1,
25 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI = 0x11,
26 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EDIT_TEXT = 0xa1,
27 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL = 0xd1,
28 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD = 0xe1
31 // These are enums from android.view.View in Java:
32 enum {
33 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE = 0,
34 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE = 1,
35 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE = 2
38 // These are enums from
39 // android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
40 enum {
41 ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT = 1
44 } // namespace
46 namespace content {
48 // static
49 BrowserAccessibility* BrowserAccessibility::Create() {
50 return new BrowserAccessibilityAndroid();
53 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
54 first_time_ = true;
57 bool BrowserAccessibilityAndroid::IsNative() const {
58 return true;
61 void BrowserAccessibilityAndroid::OnLocationChanged() {
62 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_LOCATION_CHANGED, this);
65 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
66 if (InternalChildCount() == 0)
67 return true;
69 // Iframes are always allowed to contain children.
70 if (IsIframe() ||
71 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
72 GetRole() == ui::AX_ROLE_WEB_AREA) {
73 return false;
76 // If it has a focusable child, we definitely can't leave out children.
77 if (HasFocusableChild())
78 return false;
80 // Date and time controls should drop their children.
81 if (GetRole() == ui::AX_ROLE_DATE || GetRole() == ui::AX_ROLE_TIME)
82 return true;
84 // Headings with text can drop their children.
85 base::string16 name = GetText();
86 if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
87 return true;
89 // Focusable nodes with text can drop their children.
90 if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
91 return true;
93 // Nodes with only static text as children can drop their children.
94 if (HasOnlyStaticTextChildren())
95 return true;
97 return BrowserAccessibility::PlatformIsLeaf();
100 bool BrowserAccessibilityAndroid::CanScrollForward() const {
101 if (!IsSlider())
102 return false;
104 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
105 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
106 return value < max;
109 bool BrowserAccessibilityAndroid::CanScrollBackward() const {
110 if (!IsSlider())
111 return false;
113 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
114 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
115 return value > min;
118 bool BrowserAccessibilityAndroid::IsCheckable() const {
119 bool checkable = false;
120 bool is_aria_pressed_defined;
121 bool is_mixed;
122 GetAriaTristate("aria-pressed", &is_aria_pressed_defined, &is_mixed);
123 if (GetRole() == ui::AX_ROLE_CHECK_BOX ||
124 GetRole() == ui::AX_ROLE_RADIO_BUTTON ||
125 GetRole() == ui::AX_ROLE_MENU_ITEM_CHECK_BOX ||
126 GetRole() == ui::AX_ROLE_MENU_ITEM_RADIO ||
127 is_aria_pressed_defined) {
128 checkable = true;
130 if (HasState(ui::AX_STATE_CHECKED))
131 checkable = true;
132 return checkable;
135 bool BrowserAccessibilityAndroid::IsChecked() const {
136 return (HasState(ui::AX_STATE_CHECKED) || HasState(ui::AX_STATE_PRESSED));
139 bool BrowserAccessibilityAndroid::IsClickable() const {
140 if (!PlatformIsLeaf())
141 return false;
143 return IsFocusable() || !GetText().empty();
146 bool BrowserAccessibilityAndroid::IsCollection() const {
147 return (GetRole() == ui::AX_ROLE_GRID ||
148 GetRole() == ui::AX_ROLE_LIST ||
149 GetRole() == ui::AX_ROLE_LIST_BOX ||
150 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
151 GetRole() == ui::AX_ROLE_TABLE ||
152 GetRole() == ui::AX_ROLE_TREE);
155 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
156 return (GetRole() == ui::AX_ROLE_CELL ||
157 GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
158 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM ||
159 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
160 GetRole() == ui::AX_ROLE_LIST_ITEM ||
161 GetRole() == ui::AX_ROLE_ROW_HEADER ||
162 GetRole() == ui::AX_ROLE_TREE_ITEM);
165 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
166 std::string invalid;
167 return GetHtmlAttribute("aria-invalid", &invalid);
170 bool BrowserAccessibilityAndroid::IsDismissable() const {
171 return false; // No concept of "dismissable" on the web currently.
174 bool BrowserAccessibilityAndroid::IsEditableText() const {
175 return (GetRole() == ui::AX_ROLE_TEXT_AREA ||
176 GetRole() == ui::AX_ROLE_TEXT_FIELD);
179 bool BrowserAccessibilityAndroid::IsEnabled() const {
180 return HasState(ui::AX_STATE_ENABLED);
183 bool BrowserAccessibilityAndroid::IsFocusable() const {
184 bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
185 if (IsIframe() ||
186 GetRole() == ui::AX_ROLE_WEB_AREA) {
187 focusable = false;
189 return focusable;
192 bool BrowserAccessibilityAndroid::IsFocused() const {
193 return manager()->GetFocus(manager()->GetRoot()) == this;
196 bool BrowserAccessibilityAndroid::IsHeading() const {
197 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
198 GetRole() == ui::AX_ROLE_HEADING ||
199 GetRole() == ui::AX_ROLE_ROW_HEADER);
202 bool BrowserAccessibilityAndroid::IsHierarchical() const {
203 return (GetRole() == ui::AX_ROLE_LIST ||
204 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
205 GetRole() == ui::AX_ROLE_TREE);
208 bool BrowserAccessibilityAndroid::IsLink() const {
209 return (GetRole() == ui::AX_ROLE_LINK ||
210 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK);
213 bool BrowserAccessibilityAndroid::IsMultiLine() const {
214 return GetRole() == ui::AX_ROLE_TEXT_AREA;
217 bool BrowserAccessibilityAndroid::IsPassword() const {
218 return HasState(ui::AX_STATE_PROTECTED);
221 bool BrowserAccessibilityAndroid::IsRangeType() const {
222 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR ||
223 GetRole() == ui::AX_ROLE_METER ||
224 GetRole() == ui::AX_ROLE_SCROLL_BAR ||
225 GetRole() == ui::AX_ROLE_SLIDER);
228 bool BrowserAccessibilityAndroid::IsScrollable() const {
229 int dummy;
230 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
233 bool BrowserAccessibilityAndroid::IsSelected() const {
234 return HasState(ui::AX_STATE_SELECTED);
237 bool BrowserAccessibilityAndroid::IsSlider() const {
238 return GetRole() == ui::AX_ROLE_SLIDER;
241 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
242 return !HasState(ui::AX_STATE_INVISIBLE);
245 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
246 return HasState(ui::AX_STATE_HASPOPUP);
249 const char* BrowserAccessibilityAndroid::GetClassName() const {
250 const char* class_name = NULL;
252 switch (GetRole()) {
253 case ui::AX_ROLE_SPIN_BUTTON:
254 case ui::AX_ROLE_TEXT_AREA:
255 case ui::AX_ROLE_TEXT_FIELD:
256 class_name = "android.widget.EditText";
257 break;
258 case ui::AX_ROLE_SLIDER:
259 class_name = "android.widget.SeekBar";
260 break;
261 case ui::AX_ROLE_COLOR_WELL:
262 case ui::AX_ROLE_COMBO_BOX:
263 case ui::AX_ROLE_DATE:
264 case ui::AX_ROLE_POP_UP_BUTTON:
265 case ui::AX_ROLE_TIME:
266 class_name = "android.widget.Spinner";
267 break;
268 case ui::AX_ROLE_BUTTON:
269 case ui::AX_ROLE_MENU_BUTTON:
270 class_name = "android.widget.Button";
271 break;
272 case ui::AX_ROLE_CHECK_BOX:
273 class_name = "android.widget.CheckBox";
274 break;
275 case ui::AX_ROLE_RADIO_BUTTON:
276 class_name = "android.widget.RadioButton";
277 break;
278 case ui::AX_ROLE_TOGGLE_BUTTON:
279 class_name = "android.widget.ToggleButton";
280 break;
281 case ui::AX_ROLE_CANVAS:
282 case ui::AX_ROLE_IMAGE:
283 case ui::AX_ROLE_SVG_ROOT:
284 class_name = "android.widget.Image";
285 break;
286 case ui::AX_ROLE_METER:
287 case ui::AX_ROLE_PROGRESS_INDICATOR:
288 class_name = "android.widget.ProgressBar";
289 break;
290 case ui::AX_ROLE_TAB_LIST:
291 class_name = "android.widget.TabWidget";
292 break;
293 case ui::AX_ROLE_GRID:
294 case ui::AX_ROLE_TABLE:
295 class_name = "android.widget.GridView";
296 break;
297 case ui::AX_ROLE_LIST:
298 case ui::AX_ROLE_LIST_BOX:
299 case ui::AX_ROLE_DESCRIPTION_LIST:
300 class_name = "android.widget.ListView";
301 break;
302 case ui::AX_ROLE_DIALOG:
303 class_name = "android.app.Dialog";
304 break;
305 case ui::AX_ROLE_ROOT_WEB_AREA:
306 class_name = "android.webkit.WebView";
307 break;
308 case ui::AX_ROLE_MENU_ITEM:
309 case ui::AX_ROLE_MENU_ITEM_CHECK_BOX:
310 case ui::AX_ROLE_MENU_ITEM_RADIO:
311 class_name = "android.view.MenuItem";
312 break;
313 default:
314 class_name = "android.view.View";
315 break;
318 return class_name;
321 base::string16 BrowserAccessibilityAndroid::GetText() const {
322 if (IsIframe() ||
323 GetRole() == ui::AX_ROLE_WEB_AREA) {
324 return base::string16();
327 // See comment in browser_accessibility_win.cc for details.
328 // The difference here is that we can only expose one accessible
329 // name on Android, not 2 or 3 like on Windows or Mac.
331 // First, always return the |value| attribute if this is an
332 // input field.
333 if (!value().empty()) {
334 if (HasState(ui::AX_STATE_EDITABLE))
335 return base::UTF8ToUTF16(value());
337 switch (GetRole()) {
338 case ui::AX_ROLE_COMBO_BOX:
339 case ui::AX_ROLE_POP_UP_BUTTON:
340 case ui::AX_ROLE_TEXT_AREA:
341 case ui::AX_ROLE_TEXT_FIELD:
342 return base::UTF8ToUTF16(value());
346 // For color wells, the color is stored in separate attributes.
347 // Perhaps we could return color names in the future?
348 if (GetRole() == ui::AX_ROLE_COLOR_WELL) {
349 int red = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_RED);
350 int green = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_GREEN);
351 int blue = GetIntAttribute(ui::AX_ATTR_COLOR_VALUE_BLUE);
352 return base::UTF8ToUTF16(
353 base::StringPrintf("#%02X%02X%02X", red, green, blue));
356 // Always prefer visible text if this is a link. Sites sometimes add
357 // a "title" attribute to a link with more information, but we can't
358 // lose the link text.
359 if (!name().empty() && GetRole() == ui::AX_ROLE_LINK)
360 return base::UTF8ToUTF16(name());
362 // If there's no text value, the basic rule is: prefer description
363 // (aria-labelledby or aria-label), then help (title), then name
364 // (inner text), then value (control value). However, if
365 // title_elem_id is set, that means there's a label element
366 // supplying the name and then name takes precedence over help.
367 // TODO(dmazzoni): clean this up by providing more granular labels in
368 // Blink, making the platform-specific mapping to accessible text simpler.
369 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
370 base::string16 help = GetString16Attribute(ui::AX_ATTR_HELP);
372 base::string16 placeholder;
373 switch (GetRole()) {
374 case ui::AX_ROLE_DATE:
375 case ui::AX_ROLE_TEXT_AREA:
376 case ui::AX_ROLE_TEXT_FIELD:
377 case ui::AX_ROLE_TIME:
378 GetHtmlAttribute("placeholder", &placeholder);
381 int title_elem_id = GetIntAttribute(
382 ui::AX_ATTR_TITLE_UI_ELEMENT);
383 base::string16 text;
384 if (!description.empty())
385 text = description;
386 else if (title_elem_id && !name().empty())
387 text = base::UTF8ToUTF16(name());
388 else if (!help.empty())
389 text = help;
390 else if (!name().empty())
391 text = base::UTF8ToUTF16(name());
392 else if (!placeholder.empty())
393 text = placeholder;
394 else if (!value().empty())
395 text = base::UTF8ToUTF16(value());
396 else if (title_elem_id) {
397 BrowserAccessibility* title_elem =
398 manager()->GetFromID(title_elem_id);
399 if (title_elem)
400 text = static_cast<BrowserAccessibilityAndroid*>(title_elem)->GetText();
403 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
404 // from within this!
405 if (text.empty() &&
406 (HasOnlyStaticTextChildren() ||
407 (IsFocusable() && HasOnlyTextAndImageChildren()))) {
408 for (uint32 i = 0; i < InternalChildCount(); i++) {
409 BrowserAccessibility* child = InternalGetChild(i);
410 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
414 if (text.empty() && (IsLink() || GetRole() == ui::AX_ROLE_IMAGE)) {
415 base::string16 url = GetString16Attribute(ui::AX_ATTR_URL);
416 // Given a url like http://foo.com/bar/baz.png, just return the
417 // base name, e.g., "baz".
418 int trailing_slashes = 0;
419 while (url.size() - trailing_slashes > 0 &&
420 url[url.size() - trailing_slashes - 1] == '/') {
421 trailing_slashes++;
423 if (trailing_slashes)
424 url = url.substr(0, url.size() - trailing_slashes);
425 size_t slash_index = url.rfind('/');
426 if (slash_index != std::string::npos)
427 url = url.substr(slash_index + 1);
428 size_t dot_index = url.rfind('.');
429 if (dot_index != std::string::npos)
430 url = url.substr(0, dot_index);
431 text = url;
434 return text;
437 int BrowserAccessibilityAndroid::GetItemIndex() const {
438 int index = 0;
439 switch (GetRole()) {
440 case ui::AX_ROLE_LIST_ITEM:
441 case ui::AX_ROLE_LIST_BOX_OPTION:
442 case ui::AX_ROLE_TREE_ITEM:
443 index = GetIndexInParent();
444 break;
445 case ui::AX_ROLE_SLIDER:
446 case ui::AX_ROLE_PROGRESS_INDICATOR: {
447 // Return a percentage here for live feedback in an AccessibilityEvent.
448 // The exact value is returned in RangeCurrentValue.
449 float min = GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
450 float max = GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
451 float value = GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
452 if (max > min && value >= min && value <= max)
453 index = static_cast<int>(((value - min)) * 100 / (max - min));
454 break;
457 return index;
460 int BrowserAccessibilityAndroid::GetItemCount() const {
461 int count = 0;
462 switch (GetRole()) {
463 case ui::AX_ROLE_LIST:
464 case ui::AX_ROLE_LIST_BOX:
465 case ui::AX_ROLE_DESCRIPTION_LIST:
466 count = PlatformChildCount();
467 break;
468 case ui::AX_ROLE_SLIDER:
469 case ui::AX_ROLE_PROGRESS_INDICATOR:
470 // An AccessibilityEvent can only return integer information about a
471 // seek control, so we return a percentage. The real range is returned
472 // in RangeMin and RangeMax.
473 count = 100;
474 break;
476 return count;
479 int BrowserAccessibilityAndroid::GetScrollX() const {
480 int value = 0;
481 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
482 return value;
485 int BrowserAccessibilityAndroid::GetScrollY() const {
486 int value = 0;
487 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
488 return value;
491 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
492 int value = 0;
493 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
494 return value;
497 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
498 int value = 0;
499 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
500 return value;
503 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
504 size_t index = 0;
505 while (index < old_value_.length() &&
506 index < new_value_.length() &&
507 old_value_[index] == new_value_[index]) {
508 index++;
510 return index;
513 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
514 size_t old_len = old_value_.length();
515 size_t new_len = new_value_.length();
516 size_t left = 0;
517 while (left < old_len &&
518 left < new_len &&
519 old_value_[left] == new_value_[left]) {
520 left++;
522 size_t right = 0;
523 while (right < old_len &&
524 right < new_len &&
525 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
526 right++;
528 return (new_len - left - right);
531 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
532 size_t old_len = old_value_.length();
533 size_t new_len = new_value_.length();
534 size_t left = 0;
535 while (left < old_len &&
536 left < new_len &&
537 old_value_[left] == new_value_[left]) {
538 left++;
540 size_t right = 0;
541 while (right < old_len &&
542 right < new_len &&
543 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
544 right++;
546 return (old_len - left - right);
549 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
550 return old_value_;
553 int BrowserAccessibilityAndroid::GetSelectionStart() const {
554 int sel_start = 0;
555 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
556 return sel_start;
559 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
560 int sel_end = 0;
561 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
562 return sel_end;
565 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
566 return value().length();
569 int BrowserAccessibilityAndroid::AndroidInputType() const {
570 std::string html_tag = GetStringAttribute(
571 ui::AX_ATTR_HTML_TAG);
572 if (html_tag != "input")
573 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
575 std::string type;
576 if (!GetHtmlAttribute("type", &type))
577 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
579 if (type.empty() || type == "text" || type == "search")
580 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
581 else if (type == "date")
582 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
583 else if (type == "datetime" || type == "datetime-local")
584 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
585 else if (type == "email")
586 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
587 else if (type == "month")
588 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
589 else if (type == "number")
590 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
591 else if (type == "password")
592 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
593 else if (type == "tel")
594 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
595 else if (type == "time")
596 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
597 else if (type == "url")
598 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
599 else if (type == "week")
600 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
602 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
605 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
606 std::string live = GetStringAttribute(
607 ui::AX_ATTR_LIVE_STATUS);
608 if (live == "polite")
609 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
610 else if (live == "assertive")
611 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
612 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
615 int BrowserAccessibilityAndroid::AndroidRangeType() const {
616 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
619 int BrowserAccessibilityAndroid::RowCount() const {
620 if (GetRole() == ui::AX_ROLE_GRID ||
621 GetRole() == ui::AX_ROLE_TABLE) {
622 return CountChildrenWithRole(ui::AX_ROLE_ROW);
625 if (GetRole() == ui::AX_ROLE_LIST ||
626 GetRole() == ui::AX_ROLE_LIST_BOX ||
627 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST ||
628 GetRole() == ui::AX_ROLE_TREE) {
629 return PlatformChildCount();
632 return 0;
635 int BrowserAccessibilityAndroid::ColumnCount() const {
636 if (GetRole() == ui::AX_ROLE_GRID ||
637 GetRole() == ui::AX_ROLE_TABLE) {
638 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
640 return 0;
643 int BrowserAccessibilityAndroid::RowIndex() const {
644 if (GetRole() == ui::AX_ROLE_LIST_ITEM ||
645 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
646 GetRole() == ui::AX_ROLE_TREE_ITEM) {
647 return GetIndexInParent();
650 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
653 int BrowserAccessibilityAndroid::RowSpan() const {
654 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
657 int BrowserAccessibilityAndroid::ColumnIndex() const {
658 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
661 int BrowserAccessibilityAndroid::ColumnSpan() const {
662 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
665 float BrowserAccessibilityAndroid::RangeMin() const {
666 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
669 float BrowserAccessibilityAndroid::RangeMax() const {
670 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
673 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
674 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
677 void BrowserAccessibilityAndroid::GetGranularityBoundaries(
678 int granularity,
679 std::vector<int32>* starts,
680 std::vector<int32>* ends,
681 int offset) {
682 switch (granularity) {
683 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE:
684 GetLineBoundaries(starts, ends, offset);
685 break;
686 case ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD:
687 GetWordBoundaries(starts, ends, offset);
688 break;
689 default:
690 NOTREACHED();
694 void BrowserAccessibilityAndroid::GetLineBoundaries(
695 std::vector<int32>* line_starts,
696 std::vector<int32>* line_ends,
697 int offset) {
698 // If this node has no children, treat it as all one line.
699 if (GetText().size() > 0 && !InternalChildCount()) {
700 line_starts->push_back(offset);
701 line_ends->push_back(offset + GetText().size());
704 // If this is a static text node, get the line boundaries from the
705 // inline text boxes if possible.
706 if (GetRole() == ui::AX_ROLE_STATIC_TEXT) {
707 int last_y = 0;
708 for (uint32 i = 0; i < InternalChildCount(); i++) {
709 BrowserAccessibilityAndroid* child =
710 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
711 CHECK_EQ(ui::AX_ROLE_INLINE_TEXT_BOX, child->GetRole());
712 // TODO(dmazzoni): replace this with a proper API to determine
713 // if two inline text boxes are on the same line. http://crbug.com/421771
714 int y = child->GetLocation().y();
715 if (i == 0) {
716 line_starts->push_back(offset);
717 } else if (y != last_y) {
718 line_ends->push_back(offset);
719 line_starts->push_back(offset);
721 offset += child->GetText().size();
722 last_y = y;
724 line_ends->push_back(offset);
725 return;
728 // Otherwise, call GetLineBoundaries recursively on the children.
729 for (uint32 i = 0; i < InternalChildCount(); i++) {
730 BrowserAccessibilityAndroid* child =
731 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
732 child->GetLineBoundaries(line_starts, line_ends, offset);
733 offset += child->GetText().size();
737 void BrowserAccessibilityAndroid::GetWordBoundaries(
738 std::vector<int32>* word_starts,
739 std::vector<int32>* word_ends,
740 int offset) {
741 if (GetRole() == ui::AX_ROLE_INLINE_TEXT_BOX) {
742 const std::vector<int32>& starts = GetIntListAttribute(
743 ui::AX_ATTR_WORD_STARTS);
744 const std::vector<int32>& ends = GetIntListAttribute(
745 ui::AX_ATTR_WORD_ENDS);
746 for (size_t i = 0; i < starts.size(); ++i) {
747 word_starts->push_back(offset + starts[i]);
748 word_ends->push_back(offset + ends[i]);
750 return;
753 base::string16 concatenated_text;
754 for (uint32 i = 0; i < InternalChildCount(); i++) {
755 BrowserAccessibilityAndroid* child =
756 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
757 base::string16 child_text = child->GetText();
758 concatenated_text += child->GetText();
761 base::string16 text = GetText();
762 if (text.empty() || concatenated_text == text) {
763 // Great - this node is just the concatenation of its children, so
764 // we can get the word boundaries recursively.
765 for (uint32 i = 0; i < InternalChildCount(); i++) {
766 BrowserAccessibilityAndroid* child =
767 static_cast<BrowserAccessibilityAndroid*>(InternalGetChild(i));
768 child->GetWordBoundaries(word_starts, word_ends, offset);
769 offset += child->GetText().size();
771 } else {
772 // This node has its own accessible text that doesn't match its
773 // visible text - like alt text for an image or something with an
774 // aria-label, so split the text into words locally.
775 base::i18n::BreakIterator iter(text, base::i18n::BreakIterator::BREAK_WORD);
776 if (!iter.Init())
777 return;
778 while (iter.Advance()) {
779 if (iter.IsWord()) {
780 word_starts->push_back(iter.prev());
781 word_ends->push_back(iter.pos());
787 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
788 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
789 // from within this!
790 for (uint32 i = 0; i < InternalChildCount(); i++) {
791 BrowserAccessibility* child = InternalGetChild(i);
792 if (child->HasState(ui::AX_STATE_FOCUSABLE))
793 return true;
794 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
795 return true;
797 return false;
800 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
801 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
802 // from within this!
803 for (uint32 i = 0; i < InternalChildCount(); i++) {
804 BrowserAccessibility* child = InternalGetChild(i);
805 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT)
806 return false;
808 return true;
811 bool BrowserAccessibilityAndroid::HasOnlyTextAndImageChildren() const {
812 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
813 // from within this!
814 for (uint32 i = 0; i < InternalChildCount(); i++) {
815 BrowserAccessibility* child = InternalGetChild(i);
816 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT &&
817 child->GetRole() != ui::AX_ROLE_IMAGE) {
818 return false;
821 return true;
824 bool BrowserAccessibilityAndroid::IsIframe() const {
825 base::string16 html_tag = GetString16Attribute(
826 ui::AX_ATTR_HTML_TAG);
827 return html_tag == base::ASCIIToUTF16("iframe");
830 void BrowserAccessibilityAndroid::OnDataChanged() {
831 BrowserAccessibility::OnDataChanged();
833 if (IsEditableText()) {
834 if (base::UTF8ToUTF16(value()) != new_value_) {
835 old_value_ = new_value_;
836 new_value_ = base::UTF8ToUTF16(value());
840 if (GetRole() == ui::AX_ROLE_ALERT && first_time_)
841 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
843 base::string16 live;
844 if (GetString16Attribute(
845 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
846 NotifyLiveRegionUpdate(live);
849 first_time_ = false;
852 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
853 base::string16& aria_live) {
854 if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
855 !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
856 return;
858 base::string16 text = GetText();
859 if (cached_text_ != text) {
860 if (!text.empty()) {
861 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
862 this);
864 cached_text_ = text;
868 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
869 int count = 0;
870 for (uint32 i = 0; i < PlatformChildCount(); i++) {
871 if (PlatformGetChild(i)->GetRole() == role)
872 count++;
874 return count;
877 } // namespace content