IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_android.cc
blobb86374a02ec354ff79f3e20180ccf48b64ba865e
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 (child_count() == 0)
60 return true;
62 // Iframes are always allowed to contain children.
63 if (IsIframe() ||
64 role() == ui::AX_ROLE_ROOT_WEB_AREA ||
65 role() == 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 (role() == 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 (role() == ui::AX_ROLE_CHECK_BOX ||
95 role() == 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 (role() == ui::AX_ROLE_GRID ||
114 role() == ui::AX_ROLE_LIST ||
115 role() == ui::AX_ROLE_LIST_BOX ||
116 role() == ui::AX_ROLE_TABLE ||
117 role() == ui::AX_ROLE_TREE);
120 bool BrowserAccessibilityAndroid::IsCollectionItem() const {
121 return (role() == ui::AX_ROLE_CELL ||
122 role() == ui::AX_ROLE_COLUMN_HEADER ||
123 role() == ui::AX_ROLE_DESCRIPTION_LIST_TERM ||
124 role() == ui::AX_ROLE_LIST_BOX_OPTION ||
125 role() == ui::AX_ROLE_LIST_ITEM ||
126 role() == ui::AX_ROLE_ROW_HEADER ||
127 role() == 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 role() == 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 (role() == ui::AX_ROLE_COLUMN_HEADER ||
158 role() == ui::AX_ROLE_HEADING ||
159 role() == ui::AX_ROLE_ROW_HEADER);
162 bool BrowserAccessibilityAndroid::IsHierarchical() const {
163 return (role() == ui::AX_ROLE_LIST ||
164 role() == ui::AX_ROLE_TREE);
167 bool BrowserAccessibilityAndroid::IsMultiLine() const {
168 return role() == ui::AX_ROLE_TEXT_AREA;
171 bool BrowserAccessibilityAndroid::IsPassword() const {
172 return HasState(ui::AX_STATE_PROTECTED);
175 bool BrowserAccessibilityAndroid::IsRangeType() const {
176 return (role() == ui::AX_ROLE_PROGRESS_INDICATOR ||
177 role() == ui::AX_ROLE_SCROLL_BAR ||
178 role() == ui::AX_ROLE_SLIDER);
181 bool BrowserAccessibilityAndroid::IsScrollable() const {
182 int dummy;
183 return GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &dummy);
186 bool BrowserAccessibilityAndroid::IsSelected() const {
187 return HasState(ui::AX_STATE_SELECTED);
190 bool BrowserAccessibilityAndroid::IsVisibleToUser() const {
191 return !HasState(ui::AX_STATE_INVISIBLE);
194 bool BrowserAccessibilityAndroid::CanOpenPopup() const {
195 return HasState(ui::AX_STATE_HASPOPUP);
198 const char* BrowserAccessibilityAndroid::GetClassName() const {
199 const char* class_name = NULL;
201 switch(role()) {
202 case ui::AX_ROLE_EDITABLE_TEXT:
203 case ui::AX_ROLE_SPIN_BUTTON:
204 case ui::AX_ROLE_TEXT_AREA:
205 case ui::AX_ROLE_TEXT_FIELD:
206 class_name = "android.widget.EditText";
207 break;
208 case ui::AX_ROLE_SLIDER:
209 class_name = "android.widget.SeekBar";
210 break;
211 case ui::AX_ROLE_COMBO_BOX:
212 class_name = "android.widget.Spinner";
213 break;
214 case ui::AX_ROLE_BUTTON:
215 case ui::AX_ROLE_MENU_BUTTON:
216 case ui::AX_ROLE_POP_UP_BUTTON:
217 class_name = "android.widget.Button";
218 break;
219 case ui::AX_ROLE_CHECK_BOX:
220 class_name = "android.widget.CheckBox";
221 break;
222 case ui::AX_ROLE_RADIO_BUTTON:
223 class_name = "android.widget.RadioButton";
224 break;
225 case ui::AX_ROLE_TOGGLE_BUTTON:
226 class_name = "android.widget.ToggleButton";
227 break;
228 case ui::AX_ROLE_CANVAS:
229 case ui::AX_ROLE_IMAGE:
230 class_name = "android.widget.Image";
231 break;
232 case ui::AX_ROLE_PROGRESS_INDICATOR:
233 class_name = "android.widget.ProgressBar";
234 break;
235 case ui::AX_ROLE_TAB_LIST:
236 class_name = "android.widget.TabWidget";
237 break;
238 case ui::AX_ROLE_GRID:
239 case ui::AX_ROLE_TABLE:
240 class_name = "android.widget.GridView";
241 break;
242 case ui::AX_ROLE_LIST:
243 case ui::AX_ROLE_LIST_BOX:
244 class_name = "android.widget.ListView";
245 break;
246 case ui::AX_ROLE_DIALOG:
247 class_name = "android.app.Dialog";
248 break;
249 default:
250 class_name = "android.view.View";
251 break;
254 return class_name;
257 base::string16 BrowserAccessibilityAndroid::GetText() const {
258 if (IsIframe() ||
259 role() == ui::AX_ROLE_WEB_AREA) {
260 return base::string16();
263 base::string16 description = GetString16Attribute(
264 ui::AX_ATTR_DESCRIPTION);
265 base::string16 text;
266 if (!name().empty())
267 text = base::UTF8ToUTF16(name());
268 else if (!description.empty())
269 text = description;
270 else if (!value().empty())
271 text = base::UTF8ToUTF16(value());
273 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
274 // from within this!
275 if (text.empty() && HasOnlyStaticTextChildren()) {
276 for (uint32 i = 0; i < child_count(); i++) {
277 BrowserAccessibility* child = children()[i];
278 text += static_cast<BrowserAccessibilityAndroid*>(child)->GetText();
282 switch(role()) {
283 case ui::AX_ROLE_IMAGE_MAP_LINK:
284 case ui::AX_ROLE_LINK:
285 if (!text.empty())
286 text += base::ASCIIToUTF16(" ");
287 text += base::ASCIIToUTF16("Link");
288 break;
289 case ui::AX_ROLE_HEADING:
290 // Only append "heading" if this node already has text.
291 if (!text.empty())
292 text += base::ASCIIToUTF16(" Heading");
293 break;
296 return text;
299 int BrowserAccessibilityAndroid::GetItemIndex() const {
300 int index = 0;
301 switch(role()) {
302 case ui::AX_ROLE_LIST_ITEM:
303 case ui::AX_ROLE_LIST_BOX_OPTION:
304 case ui::AX_ROLE_TREE_ITEM:
305 index = index_in_parent();
306 break;
307 case ui::AX_ROLE_SLIDER:
308 case ui::AX_ROLE_PROGRESS_INDICATOR: {
309 float value_for_range;
310 if (GetFloatAttribute(
311 ui::AX_ATTR_VALUE_FOR_RANGE, &value_for_range)) {
312 index = static_cast<int>(value_for_range);
314 break;
317 return index;
320 int BrowserAccessibilityAndroid::GetItemCount() const {
321 int count = 0;
322 switch(role()) {
323 case ui::AX_ROLE_LIST:
324 case ui::AX_ROLE_LIST_BOX:
325 count = PlatformChildCount();
326 break;
327 case ui::AX_ROLE_SLIDER:
328 case ui::AX_ROLE_PROGRESS_INDICATOR: {
329 float max_value_for_range;
330 if (GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE,
331 &max_value_for_range)) {
332 count = static_cast<int>(max_value_for_range);
334 break;
337 return count;
340 int BrowserAccessibilityAndroid::GetScrollX() const {
341 int value = 0;
342 GetIntAttribute(ui::AX_ATTR_SCROLL_X, &value);
343 return value;
346 int BrowserAccessibilityAndroid::GetScrollY() const {
347 int value = 0;
348 GetIntAttribute(ui::AX_ATTR_SCROLL_Y, &value);
349 return value;
352 int BrowserAccessibilityAndroid::GetMaxScrollX() const {
353 int value = 0;
354 GetIntAttribute(ui::AX_ATTR_SCROLL_X_MAX, &value);
355 return value;
358 int BrowserAccessibilityAndroid::GetMaxScrollY() const {
359 int value = 0;
360 GetIntAttribute(ui::AX_ATTR_SCROLL_Y_MAX, &value);
361 return value;
364 int BrowserAccessibilityAndroid::GetTextChangeFromIndex() const {
365 size_t index = 0;
366 while (index < old_value_.length() &&
367 index < new_value_.length() &&
368 old_value_[index] == new_value_[index]) {
369 index++;
371 return index;
374 int BrowserAccessibilityAndroid::GetTextChangeAddedCount() const {
375 size_t old_len = old_value_.length();
376 size_t new_len = new_value_.length();
377 size_t left = 0;
378 while (left < old_len &&
379 left < new_len &&
380 old_value_[left] == new_value_[left]) {
381 left++;
383 size_t right = 0;
384 while (right < old_len &&
385 right < new_len &&
386 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
387 right++;
389 return (new_len - left - right);
392 int BrowserAccessibilityAndroid::GetTextChangeRemovedCount() const {
393 size_t old_len = old_value_.length();
394 size_t new_len = new_value_.length();
395 size_t left = 0;
396 while (left < old_len &&
397 left < new_len &&
398 old_value_[left] == new_value_[left]) {
399 left++;
401 size_t right = 0;
402 while (right < old_len &&
403 right < new_len &&
404 old_value_[old_len - right - 1] == new_value_[new_len - right - 1]) {
405 right++;
407 return (old_len - left - right);
410 base::string16 BrowserAccessibilityAndroid::GetTextChangeBeforeText() const {
411 return old_value_;
414 int BrowserAccessibilityAndroid::GetSelectionStart() const {
415 int sel_start = 0;
416 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_START, &sel_start);
417 return sel_start;
420 int BrowserAccessibilityAndroid::GetSelectionEnd() const {
421 int sel_end = 0;
422 GetIntAttribute(ui::AX_ATTR_TEXT_SEL_END, &sel_end);
423 return sel_end;
426 int BrowserAccessibilityAndroid::GetEditableTextLength() const {
427 return value().length();
430 int BrowserAccessibilityAndroid::AndroidInputType() const {
431 std::string html_tag = GetStringAttribute(
432 ui::AX_ATTR_HTML_TAG);
433 if (html_tag != "input")
434 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
436 std::string type;
437 if (!GetHtmlAttribute("type", &type))
438 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
440 if (type == "" || type == "text" || type == "search")
441 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT;
442 else if (type == "date")
443 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
444 else if (type == "datetime" || type == "datetime-local")
445 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
446 else if (type == "email")
447 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_EMAIL;
448 else if (type == "month")
449 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_DATE;
450 else if (type == "number")
451 return ANDROID_TEXT_INPUTTYPE_TYPE_NUMBER;
452 else if (type == "password")
453 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_WEB_PASSWORD;
454 else if (type == "tel")
455 return ANDROID_TEXT_INPUTTYPE_TYPE_PHONE;
456 else if (type == "time")
457 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME_TIME;
458 else if (type == "url")
459 return ANDROID_TEXT_INPUTTYPE_TYPE_TEXT_URI;
460 else if (type == "week")
461 return ANDROID_TEXT_INPUTTYPE_TYPE_DATETIME;
463 return ANDROID_TEXT_INPUTTYPE_TYPE_NULL;
466 int BrowserAccessibilityAndroid::AndroidLiveRegionType() const {
467 std::string live = GetStringAttribute(
468 ui::AX_ATTR_LIVE_STATUS);
469 if (live == "polite")
470 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_POLITE;
471 else if (live == "assertive")
472 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_ASSERTIVE;
473 return ANDROID_VIEW_VIEW_ACCESSIBILITY_LIVE_REGION_NONE;
476 int BrowserAccessibilityAndroid::AndroidRangeType() const {
477 return ANDROID_VIEW_ACCESSIBILITY_RANGE_TYPE_FLOAT;
480 int BrowserAccessibilityAndroid::RowCount() const {
481 if (role() == ui::AX_ROLE_GRID ||
482 role() == ui::AX_ROLE_TABLE) {
483 return CountChildrenWithRole(ui::AX_ROLE_ROW);
486 if (role() == ui::AX_ROLE_LIST ||
487 role() == ui::AX_ROLE_LIST_BOX ||
488 role() == ui::AX_ROLE_TREE) {
489 return PlatformChildCount();
492 return 0;
495 int BrowserAccessibilityAndroid::ColumnCount() const {
496 if (role() == ui::AX_ROLE_GRID ||
497 role() == ui::AX_ROLE_TABLE) {
498 return CountChildrenWithRole(ui::AX_ROLE_COLUMN);
500 return 0;
503 int BrowserAccessibilityAndroid::RowIndex() const {
504 if (role() == ui::AX_ROLE_LIST_ITEM ||
505 role() == ui::AX_ROLE_LIST_BOX_OPTION ||
506 role() == ui::AX_ROLE_TREE_ITEM) {
507 return index_in_parent();
510 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_INDEX);
513 int BrowserAccessibilityAndroid::RowSpan() const {
514 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_ROW_SPAN);
517 int BrowserAccessibilityAndroid::ColumnIndex() const {
518 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_INDEX);
521 int BrowserAccessibilityAndroid::ColumnSpan() const {
522 return GetIntAttribute(ui::AX_ATTR_TABLE_CELL_COLUMN_SPAN);
525 float BrowserAccessibilityAndroid::RangeMin() const {
526 return GetFloatAttribute(ui::AX_ATTR_MIN_VALUE_FOR_RANGE);
529 float BrowserAccessibilityAndroid::RangeMax() const {
530 return GetFloatAttribute(ui::AX_ATTR_MAX_VALUE_FOR_RANGE);
533 float BrowserAccessibilityAndroid::RangeCurrentValue() const {
534 return GetFloatAttribute(ui::AX_ATTR_VALUE_FOR_RANGE);
537 bool BrowserAccessibilityAndroid::HasFocusableChild() const {
538 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
539 // from within this!
540 for (uint32 i = 0; i < child_count(); i++) {
541 BrowserAccessibility* child = children()[i];
542 if (child->HasState(ui::AX_STATE_FOCUSABLE))
543 return true;
544 if (static_cast<BrowserAccessibilityAndroid*>(child)->HasFocusableChild())
545 return true;
547 return false;
550 bool BrowserAccessibilityAndroid::HasOnlyStaticTextChildren() const {
551 // This is called from PlatformIsLeaf, so don't call PlatformChildCount
552 // from within this!
553 for (uint32 i = 0; i < child_count(); i++) {
554 BrowserAccessibility* child = children()[i];
555 if (child->role() != ui::AX_ROLE_STATIC_TEXT)
556 return false;
558 return true;
561 bool BrowserAccessibilityAndroid::IsIframe() const {
562 base::string16 html_tag = GetString16Attribute(
563 ui::AX_ATTR_HTML_TAG);
564 return html_tag == base::ASCIIToUTF16("iframe");
567 void BrowserAccessibilityAndroid::PostInitialize() {
568 BrowserAccessibility::PostInitialize();
570 if (IsEditableText()) {
571 if (base::UTF8ToUTF16(value()) != new_value_) {
572 old_value_ = new_value_;
573 new_value_ = base::UTF8ToUTF16(value());
577 if (role() == ui::AX_ROLE_ALERT && first_time_)
578 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, this);
580 base::string16 live;
581 if (GetString16Attribute(
582 ui::AX_ATTR_CONTAINER_LIVE_STATUS, &live)) {
583 NotifyLiveRegionUpdate(live);
586 first_time_ = false;
589 void BrowserAccessibilityAndroid::NotifyLiveRegionUpdate(
590 base::string16& aria_live) {
591 if (!EqualsASCII(aria_live, aria_strings::kAriaLivePolite) &&
592 !EqualsASCII(aria_live, aria_strings::kAriaLiveAssertive))
593 return;
595 base::string16 text = GetText();
596 if (cached_text_ != text) {
597 if (!text.empty()) {
598 manager()->NotifyAccessibilityEvent(ui::AX_EVENT_SHOW,
599 this);
601 cached_text_ = text;
605 int BrowserAccessibilityAndroid::CountChildrenWithRole(ui::AXRole role) const {
606 int count = 0;
607 for (uint32 i = 0; i < PlatformChildCount(); i++) {
608 if (PlatformGetChild(i)->role() == role)
609 count++;
611 return count;
614 } // namespace content