Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blobcec0a664cdab41d4359171680289c0390aaeb6fe
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/strings/utf_string_conversions.h"
8 #include "content/browser/accessibility/browser_accessibility_manager_android.h"
9 #include "content/common/accessibility_messages.h"
11 namespace {
13 // These are enums from android.text.InputType in Java:
14 enum {
15 ANDROID_TEXT_INPUTTYPE_TYPE_NULL = 0,
16 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME = 0x4,
17 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE = 0x14,
18 ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME = 0x24,
19 ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER = 0x2,
20 ANDROID_TEXT_INPUTTYPE_TYPE_PHONE = 0x3,
21 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT = 0x1,
22 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI = 0x11,
23 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EDIT_TEXT = 0xa1,
24 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL = 0xd1,
25 ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD = 0xe1
28 // These are enums from android.view.View in Java:
29 enum {
30 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE = 0,
31 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE = 1,
32 ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE = 2
35 // These are enums from
36 // android.view.accessibility.AccessibilityNodeInfo.RangeInfo in Java:
37 enum {
38 ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT = 1
41 } // namespace
43 namespace content {
45 // static
46 BrowserAccessibility* BrowserAccessibility::Create() {
47 return new BrowserAccessibilityAndroid();
50 BrowserAccessibilityAndroid::BrowserAccessibilityAndroid() {
51 first_time_ = true;
54 bool BrowserAccessibilityAndroid::IsNative() const {
55 return true;
58 bool BrowserAccessibilityAndroid::PlatformIsLeaf() const {
59 if (InternalChildCount() == 0)
60 return true;
62 // Iframes are always allowed to contain children.
63 if (IsIframe() ||
64 GetRole() == ui::AX_ROLE_ROOT_WEB_AREA ||
65 GetRole() == ui::AX_ROLE_WEB_AREA) {
66 return false;
69 // If it has a focusable child, we definitely can't leave out children.
70 if (HasFocusableChild())
71 return false;
73 // Headings with text can drop their children.
74 base::string16 name = GetText();
75 if (GetRole() == ui::AX_ROLE_HEADING && !name.empty())
76 return true;
78 // Focusable nodes with text can drop their children.
79 if (HasState(ui::AX_STATE_FOCUSABLE) && !name.empty())
80 return true;
82 // Nodes with only static text as children can drop their children.
83 if (HasOnlyStaticTextChildren())
84 return true;
86 return BrowserAccessibility::PlatformIsLeaf();
89 bool BrowserAccessibilityAndroid::IsCheckable() const {
90 bool checkable = false;
91 bool is_aria_pressed_defined;
92 bool is_mixed;
93 GetAriaTristate("aria-pressed", &is_aria_pressed_defined, &is_mixed);
94 if (GetRole() == ui::AX_ROLE_CHECK_BOX ||
95 GetRole() == ui::AX_ROLE_RADIO_BUTTON ||
96 is_aria_pressed_defined) {
97 checkable = true;
99 if (HasState(ui::AX_STATE_CHECKED))
100 checkable = true;
101 return checkable;
104 bool BrowserAccessibilityAndroid::IsChecked() const {
105 return HasState(ui::AX_STATE_CHECKED);
108 bool BrowserAccessibilityAndroid::IsClickable() const {
109 return (PlatformIsLeaf() && !GetText().empty());
112 bool BrowserAccessibilityAndroid::IsCollection() const {
113 return (GetRole() == ui::AX_ROLE_GRID ||
114 GetRole() == ui::AX_ROLE_LIST ||
115 GetRole() == ui::AX_ROLE_LIST_BOX ||
116 GetRole() == ui::AX_ROLE_TABLE ||
117 GetRole() == ui::AX_ROLE_TREE);
120 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
121 return (GetRole() == ui::AX_ROLE_CELL ||
122 GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
123 GetRole() == ui::AX_ROLE_DESCRIPTION_LIST_TERM ||
124 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
125 GetRole() == ui::AX_ROLE_LIST_ITEM ||
126 GetRole() == ui::AX_ROLE_ROW_HEADER ||
127 GetRole() == ui::AX_ROLE_TREE_ITEM);
130 bool BrowserAccessibilityAndroid::IsContentInvalid() const {
131 std::string invalid;
132 return GetHtmlAttribute("aria-invalid", &invalid);
135 bool BrowserAccessibilityAndroid::IsDismissable() const {
136 return false; // No concept of "dismissable" on the web currently.
139 bool BrowserAccessibilityAndroid::IsEnabled() const {
140 return HasState(ui::AX_STATE_ENABLED);
143 bool BrowserAccessibilityAndroid::IsFocusable() const {
144 bool focusable = HasState(ui::AX_STATE_FOCUSABLE);
145 if (IsIframe() ||
146 GetRole() == ui::AX_ROLE_WEB_AREA) {
147 focusable = false;
149 return focusable;
152 bool BrowserAccessibilityAndroid::IsFocused() const {
153 return manager()->GetFocus(manager()->GetRoot()) == this;
156 bool BrowserAccessibilityAndroid::IsHeading() const {
157 return (GetRole() == ui::AX_ROLE_COLUMN_HEADER ||
158 GetRole() == ui::AX_ROLE_HEADING ||
159 GetRole() == ui::AX_ROLE_ROW_HEADER);
162 bool BrowserAccessibilityAndroid::IsHierarchical() const {
163 return (GetRole() == ui::AX_ROLE_LIST ||
164 GetRole() == ui::AX_ROLE_TREE);
167 bool BrowserAccessibilityAndroid::IsLink() const {
168 return GetRole() == ui::AX_ROLE_LINK ||
169 GetRole() == ui::AX_ROLE_IMAGE_MAP_LINK;
172 bool BrowserAccessibilityAndroid::IsMultiLine() const {
173 return GetRole() == ui::AX_ROLE_TEXT_AREA;
176 bool BrowserAccessibilityAndroid::IsPassword() const {
177 return HasState(ui::AX_STATE_PROTECTED);
180 bool BrowserAccessibilityAndroid::IsRangeType() const {
181 return (GetRole() == ui::AX_ROLE_PROGRESS_INDICATOR ||
182 GetRole() == ui::AX_ROLE_SCROLL_BAR ||
183 GetRole() == ui::AX_ROLE_SLIDER);
186 bool BrowserAccessibilityAndroid::IsScrollable() const {
187 int dummy;
188 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
191 bool BrowserAccessibilityAndroid::IsSelected() const {
192 return HasState(ui::AX_STATE_SELECTED);
195 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
196 return !HasState(ui::AX_STATE_INVISIBLE);
199 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
200 return HasState(ui::AX_STATE_HASPOPUP);
203 const char* BrowserAccessibilityAndroid::GetClassName() const {
204 const char* class_name = NULL;
206 switch(GetRole()) {
207 case ui::AX_ROLE_EDITABLE_TEXT:
208 case ui::AX_ROLE_SPIN_BUTTON:
209 case ui::AX_ROLE_TEXT_AREA:
210 case ui::AX_ROLE_TEXT_FIELD:
211 class_name = "android.widget.EditText";
212 break;
213 case ui::AX_ROLE_SLIDER:
214 class_name = "android.widget.SeekBar";
215 break;
216 case ui::AX_ROLE_COMBO_BOX:
217 class_name = "android.widget.Spinner";
218 break;
219 case ui::AX_ROLE_BUTTON:
220 case ui::AX_ROLE_MENU_BUTTON:
221 case ui::AX_ROLE_POP_UP_BUTTON:
222 class_name = "android.widget.Button";
223 break;
224 case ui::AX_ROLE_CHECK_BOX:
225 class_name = "android.widget.CheckBox";
226 break;
227 case ui::AX_ROLE_RADIO_BUTTON:
228 class_name = "android.widget.RadioButton";
229 break;
230 case ui::AX_ROLE_TOGGLE_BUTTON:
231 class_name = "android.widget.ToggleButton";
232 break;
233 case ui::AX_ROLE_CANVAS:
234 case ui::AX_ROLE_IMAGE:
235 class_name = "android.widget.Image";
236 break;
237 case ui::AX_ROLE_PROGRESS_INDICATOR:
238 class_name = "android.widget.ProgressBar";
239 break;
240 case ui::AX_ROLE_TAB_LIST:
241 class_name = "android.widget.TabWidget";
242 break;
243 case ui::AX_ROLE_GRID:
244 case ui::AX_ROLE_TABLE:
245 class_name = "android.widget.GridView";
246 break;
247 case ui::AX_ROLE_LIST:
248 case ui::AX_ROLE_LIST_BOX:
249 class_name = "android.widget.ListView";
250 break;
251 case ui::AX_ROLE_DIALOG:
252 class_name = "android.app.Dialog";
253 break;
254 case ui::AX_ROLE_ROOT_WEB_AREA:
255 class_name = "android.webkit.WebView";
256 break;
257 default:
258 class_name = "android.view.View";
259 break;
262 return class_name;
265 base::string16 BrowserAccessibilityAndroid::GetText() const {
266 if (IsIframe() ||
267 GetRole() == ui::AX_ROLE_WEB_AREA) {
268 return base::string16();
271 base::string16 description = GetString16Attribute(ui::AX_ATTR_DESCRIPTION);
272 base::string16 text;
273 if (!name().empty())
274 text = base::UTF8ToUTF16(name());
275 else if (!description.empty())
276 text = description;
277 else if (!value().empty())
278 text = base::UTF8ToUTF16(value());
280 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
281 // from within this!
282 if (text.empty() && HasOnlyStaticTextChildren()) {
283 for (uint32 i = 0; i < InternalChildCount(); i++) {
284 BrowserAccessibility* child = InternalGetChild(i);
285 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
289 switch(GetRole()) {
290 case ui::AX_ROLE_HEADING:
291 // Only append "heading" if this node already has text.
292 if (!text.empty())
293 text += base::ASCIIToUTF16(" Heading");
294 break;
297 if (text.empty() && IsLink()) {
298 base::string16 url = GetString16Attribute(ui::AX_ATTR_URL);
299 // Given a url like http://foo.com/bar/baz.png, just return the
300 // base name, e.g., "baz".
301 int trailing_slashes = 0;
302 while (url.size() - trailing_slashes > 0 &&
303 url[url.size() - trailing_slashes - 1] == '/') {
304 trailing_slashes++;
306 if (trailing_slashes)
307 url = url.substr(0, url.size() - trailing_slashes);
308 size_t slash_index = url.rfind('/');
309 if (slash_index != std::string::npos)
310 url = url.substr(slash_index + 1);
311 size_t dot_index = url.rfind('.');
312 if (dot_index != std::string::npos)
313 url = url.substr(0, dot_index);
314 text = url;
317 return text;
320 int BrowserAccessibilityAndroid::GetItemIndex() const {
321 int index = 0;
322 switch(GetRole()) {
323 case ui::AX_ROLE_LIST_ITEM:
324 case ui::AX_ROLE_LIST_BOX_OPTION:
325 case ui::AX_ROLE_TREE_ITEM:
326 index = GetIndexInParent();
327 break;
328 case ui::AX_ROLE_SLIDER:
329 case ui::AX_ROLE_PROGRESS_INDICATOR: {
330 float value_for_range;
331 if (GetFloatAttribute(
332 ui::AX_ATTR_VALUE_FOR_RANGE, &value_for_range)) {
333 index = static_cast<int>(value_for_range);
335 break;
338 return index;
341 int BrowserAccessibilityAndroid::GetItemCount() const {
342 int count = 0;
343 switch(GetRole()) {
344 case ui::AX_ROLE_LIST:
345 case ui::AX_ROLE_LIST_BOX:
346 count = PlatformChildCount();
347 break;
348 case ui::AX_ROLE_SLIDER:
349 case ui::AX_ROLE_PROGRESS_INDICATOR: {
350 float max_value_for_range;
351 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
352 &max_value_for_range)) {
353 count = static_cast<int>(max_value_for_range);
355 break;
358 return count;
361 int BrowserAccessibilityAndroid::GetScrollX() const {
362 int value = 0;
363 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
364 return value;
367 int BrowserAccessibilityAndroid::GetScrollY() const {
368 int value = 0;
369 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
370 return value;
373 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
374 int value = 0;
375 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
376 return value;
379 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
380 int value = 0;
381 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
382 return value;
385 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
386 size_t index = 0;
387 while (index < old_value_.length() &&
388 index < new_value_.length() &&
389 old_value_[index] == new_value_[index]) {
390 index++;
392 return index;
395 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
396 size_t old_len = old_value_.length();
397 size_t new_len = new_value_.length();
398 size_t left = 0;
399 while (left < old_len &&
400 left < new_len &&
401 old_value_[left] == new_value_[left]) {
402 left++;
404 size_t right = 0;
405 while (right < old_len &&
406 right < new_len &&
407 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
408 right++;
410 return (new_len - left - right);
413 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
414 size_t old_len = old_value_.length();
415 size_t new_len = new_value_.length();
416 size_t left = 0;
417 while (left < old_len &&
418 left < new_len &&
419 old_value_[left] == new_value_[left]) {
420 left++;
422 size_t right = 0;
423 while (right < old_len &&
424 right < new_len &&
425 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
426 right++;
428 return (old_len - left - right);
431 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
432 return old_value_;
435 int BrowserAccessibilityAndroid::GetSelectionStart() const {
436 int sel_start = 0;
437 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
438 return sel_start;
441 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
442 int sel_end = 0;
443 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
444 return sel_end;
447 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
448 return value().length();
451 int BrowserAccessibilityAndroid::AndroidInputType() const {
452 std::string html_tag = GetStringAttribute(
453 ui::AX_ATTR_HTML_TAG);
454 if (html_tag != "input")
455 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
457 std::string type;
458 if (!GetHtmlAttribute("type", &type))
459 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
461 if (type == "" || type == "text" || type == "search")
462 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
463 else if (type == "date")
464 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
465 else if (type == "datetime" || type == "datetime-local")
466 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
467 else if (type == "email")
468 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
469 else if (type == "month")
470 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
471 else if (type == "number")
472 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
473 else if (type == "password")
474 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
475 else if (type == "tel")
476 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
477 else if (type == "time")
478 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
479 else if (type == "url")
480 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
481 else if (type == "week")
482 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
484 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
487 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
488 std::string live = GetStringAttribute(
489 ui::AX_ATTR_LIVE_STATUS);
490 if (live == "polite")
491 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
492 else if (live == "assertive")
493 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
494 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
497 int BrowserAccessibilityAndroid::AndroidRangeType() const {
498 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
501 int BrowserAccessibilityAndroid::RowCount() const {
502 if (GetRole() == ui::AX_ROLE_GRID ||
503 GetRole() == ui::AX_ROLE_TABLE) {
504 return CountChildrenWithRole(ui::AX_ROLE_ROW);
507 if (GetRole() == ui::AX_ROLE_LIST ||
508 GetRole() == ui::AX_ROLE_LIST_BOX ||
509 GetRole() == ui::AX_ROLE_TREE) {
510 return PlatformChildCount();
513 return 0;
516 int BrowserAccessibilityAndroid::ColumnCount() const {
517 if (GetRole() == ui::AX_ROLE_GRID ||
518 GetRole() == ui::AX_ROLE_TABLE) {
519 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
521 return 0;
524 int BrowserAccessibilityAndroid::RowIndex() const {
525 if (GetRole() == ui::AX_ROLE_LIST_ITEM ||
526 GetRole() == ui::AX_ROLE_LIST_BOX_OPTION ||
527 GetRole() == ui::AX_ROLE_TREE_ITEM) {
528 return GetIndexInParent();
531 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
534 int BrowserAccessibilityAndroid::RowSpan() const {
535 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
538 int BrowserAccessibilityAndroid::ColumnIndex() const {
539 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
542 int BrowserAccessibilityAndroid::ColumnSpan() const {
543 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
546 float BrowserAccessibilityAndroid::RangeMin() const {
547 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
550 float BrowserAccessibilityAndroid::RangeMax() const {
551 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
554 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
555 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
558 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
559 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
560 // from within this!
561 for (uint32 i = 0; i < InternalChildCount(); i++) {
562 BrowserAccessibility* child = InternalGetChild(i);
563 if (child->HasState(ui::AX_STATE_FOCUSABLE))
564 return true;
565 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
566 return true;
568 return false;
571 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
572 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
573 // from within this!
574 for (uint32 i = 0; i < InternalChildCount(); i++) {
575 BrowserAccessibility* child = InternalGetChild(i);
576 if (child->GetRole() != ui::AX_ROLE_STATIC_TEXT)
577 return false;
579 return true;
582 bool BrowserAccessibilityAndroid::IsIframe() const {
583 base::string16 html_tag = GetString16Attribute(
584 ui::AX_ATTR_HTML_TAG);
585 return html_tag == base::ASCIIToUTF16("iframe");
588 void BrowserAccessibilityAndroid::OnDataChanged() {
589 BrowserAccessibility::OnDataChanged();
591 if (IsEditableText()) {
592 if (base::UTF8ToUTF16(value()) != new_value_) {
593 old_value_ = new_value_;
594 new_value_ = base::UTF8ToUTF16(value());
598 if (GetRole() == ui::AX_ROLE_ALERT && first_time_)
599 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
601 base::string16 live;
602 if (GetString16Attribute(
603 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
604 NotifyLiveRegionUpdate(live);
607 first_time_ = false;
610 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
611 base::string16& aria_live) {
612 if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
613 !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
614 return;
616 base::string16 text = GetText();
617 if (cached_text_ != text) {
618 if (!text.empty()) {
619 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
620 this);
622 cached_text_ = text;
626 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
627 int count = 0;
628 for (uint32 i = 0; i < PlatformChildCount(); i++) {
629 if (PlatformGetChild(i)->GetRole() == role)
630 count++;
632 return count;
635 } // namespace content