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_manager_android.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_string.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/browser/accessibility/browser_accessibility_android.h"
15 #include "content/common/accessibility_messages.h"
16 #include "jni/BrowserAccessibilityManager_jni.h"
18 using base::android::AttachCurrentThread
;
19 using base::android::ScopedJavaLocalRef
;
23 // These are enums from android.view.accessibility.AccessibilityEvent in Java:
25 ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_CHANGED
= 16,
26 ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_SELECTION_CHANGED
= 8192
29 enum AndroidHtmlElementType
{
30 HTML_ELEMENT_TYPE_SECTION
,
31 HTML_ELEMENT_TYPE_LIST
,
32 HTML_ELEMENT_TYPE_CONTROL
,
36 // These are special unofficial strings sent from TalkBack/BrailleBack
37 // to jump to certain categories of web elements.
38 AndroidHtmlElementType
HtmlElementTypeFromString(base::string16 element_type
) {
39 if (element_type
== base::ASCIIToUTF16("SECTION"))
40 return HTML_ELEMENT_TYPE_SECTION
;
41 else if (element_type
== base::ASCIIToUTF16("LIST"))
42 return HTML_ELEMENT_TYPE_LIST
;
43 else if (element_type
== base::ASCIIToUTF16("CONTROL"))
44 return HTML_ELEMENT_TYPE_CONTROL
;
46 return HTML_ELEMENT_TYPE_ANY
;
49 } // anonymous namespace
53 namespace aria_strings
{
54 const char kAriaLivePolite
[] = "polite";
55 const char kAriaLiveAssertive
[] = "assertive";
59 BrowserAccessibilityManager
* BrowserAccessibilityManager::Create(
60 const ui::AXTreeUpdate
& initial_tree
,
61 BrowserAccessibilityDelegate
* delegate
,
62 BrowserAccessibilityFactory
* factory
) {
63 return new BrowserAccessibilityManagerAndroid(
64 ScopedJavaLocalRef
<jobject
>(), initial_tree
, delegate
, factory
);
67 BrowserAccessibilityManagerAndroid
*
68 BrowserAccessibilityManager::ToBrowserAccessibilityManagerAndroid() {
69 return static_cast<BrowserAccessibilityManagerAndroid
*>(this);
72 BrowserAccessibilityManagerAndroid::BrowserAccessibilityManagerAndroid(
73 ScopedJavaLocalRef
<jobject
> content_view_core
,
74 const ui::AXTreeUpdate
& initial_tree
,
75 BrowserAccessibilityDelegate
* delegate
,
76 BrowserAccessibilityFactory
* factory
)
77 : BrowserAccessibilityManager(initial_tree
, delegate
, factory
) {
78 SetContentViewCore(content_view_core
);
81 BrowserAccessibilityManagerAndroid::~BrowserAccessibilityManagerAndroid() {
82 JNIEnv
* env
= AttachCurrentThread();
83 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
87 Java_BrowserAccessibilityManager_onNativeObjectDestroyed(env
, obj
.obj());
91 ui::AXTreeUpdate
BrowserAccessibilityManagerAndroid::GetEmptyDocument() {
92 ui::AXNodeData empty_document
;
93 empty_document
.id
= 0;
94 empty_document
.role
= ui::AX_ROLE_ROOT_WEB_AREA
;
95 empty_document
.state
= 1 << ui::AX_STATE_READ_ONLY
;
97 ui::AXTreeUpdate update
;
98 update
.nodes
.push_back(empty_document
);
102 void BrowserAccessibilityManagerAndroid::SetContentViewCore(
103 ScopedJavaLocalRef
<jobject
> content_view_core
) {
104 if (content_view_core
.is_null())
107 JNIEnv
* env
= AttachCurrentThread();
108 java_ref_
= JavaObjectWeakGlobalRef(
109 env
, Java_BrowserAccessibilityManager_create(
110 env
, reinterpret_cast<intptr_t>(this),
111 content_view_core
.obj()).obj());
114 void BrowserAccessibilityManagerAndroid::NotifyAccessibilityEvent(
115 ui::AXEvent event_type
,
116 BrowserAccessibility
* node
) {
117 JNIEnv
* env
= AttachCurrentThread();
118 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
122 if (event_type
== ui::AX_EVENT_HIDE
)
125 if (event_type
== ui::AX_EVENT_HOVER
) {
126 HandleHoverEvent(node
);
130 // Always send AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED to notify
131 // the Android system that the accessibility hierarchy rooted at this
133 Java_BrowserAccessibilityManager_handleContentChanged(
134 env
, obj
.obj(), node
->GetId());
136 switch (event_type
) {
137 case ui::AX_EVENT_LOAD_COMPLETE
:
138 Java_BrowserAccessibilityManager_handlePageLoaded(
139 env
, obj
.obj(), focus_
->id());
141 case ui::AX_EVENT_FOCUS
:
142 Java_BrowserAccessibilityManager_handleFocusChanged(
143 env
, obj
.obj(), node
->GetId());
145 case ui::AX_EVENT_CHECKED_STATE_CHANGED
:
146 Java_BrowserAccessibilityManager_handleCheckStateChanged(
147 env
, obj
.obj(), node
->GetId());
149 case ui::AX_EVENT_SCROLL_POSITION_CHANGED
:
150 Java_BrowserAccessibilityManager_handleScrollPositionChanged(
151 env
, obj
.obj(), node
->GetId());
153 case ui::AX_EVENT_SCROLLED_TO_ANCHOR
:
154 Java_BrowserAccessibilityManager_handleScrolledToAnchor(
155 env
, obj
.obj(), node
->GetId());
157 case ui::AX_EVENT_ALERT
:
158 // An alert is a special case of live region. Fall through to the
159 // next case to handle it.
160 case ui::AX_EVENT_SHOW
: {
161 // This event is fired when an object appears in a live region.
163 BrowserAccessibilityAndroid
* android_node
=
164 static_cast<BrowserAccessibilityAndroid
*>(node
);
165 Java_BrowserAccessibilityManager_announceLiveRegionText(
167 base::android::ConvertUTF16ToJavaString(
168 env
, android_node
->GetText()).obj());
171 case ui::AX_EVENT_TEXT_SELECTION_CHANGED
:
172 Java_BrowserAccessibilityManager_handleTextSelectionChanged(
173 env
, obj
.obj(), node
->GetId());
175 case ui::AX_EVENT_CHILDREN_CHANGED
:
176 case ui::AX_EVENT_TEXT_CHANGED
:
177 case ui::AX_EVENT_VALUE_CHANGED
:
178 if (node
->IsEditableText()) {
179 Java_BrowserAccessibilityManager_handleEditableTextChanged(
180 env
, obj
.obj(), node
->GetId());
184 // There are some notifications that aren't meaningful on Android.
185 // It's okay to skip them.
190 jint
BrowserAccessibilityManagerAndroid::GetRootId(JNIEnv
* env
, jobject obj
) {
191 return static_cast<jint
>(GetRoot()->GetId());
194 jboolean
BrowserAccessibilityManagerAndroid::IsNodeValid(
195 JNIEnv
* env
, jobject obj
, jint id
) {
196 return GetFromID(id
) != NULL
;
199 void BrowserAccessibilityManagerAndroid::HitTest(
200 JNIEnv
* env
, jobject obj
, jint x
, jint y
) {
202 delegate()->AccessibilityHitTest(gfx::Point(x
, y
));
205 jboolean
BrowserAccessibilityManagerAndroid::PopulateAccessibilityNodeInfo(
206 JNIEnv
* env
, jobject obj
, jobject info
, jint id
) {
207 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
212 if (node
->GetParent()) {
213 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoParent(
214 env
, obj
, info
, node
->GetParent()->GetId());
216 for (unsigned i
= 0; i
< node
->PlatformChildCount(); ++i
) {
217 Java_BrowserAccessibilityManager_addAccessibilityNodeInfoChild(
218 env
, obj
, info
, node
->InternalGetChild(i
)->GetId());
220 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoBooleanAttributes(
230 node
->IsScrollable(),
232 node
->IsVisibleToUser());
233 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoClassName(
235 base::android::ConvertUTF8ToJavaString(env
, node
->GetClassName()).obj());
236 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoContentDescription(
238 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj(),
241 gfx::Rect absolute_rect
= node
->GetLocalBoundsRect();
242 gfx::Rect parent_relative_rect
= absolute_rect
;
243 if (node
->GetParent()) {
244 gfx::Rect parent_rect
= node
->GetParent()->GetLocalBoundsRect();
245 parent_relative_rect
.Offset(-parent_rect
.OffsetFromOrigin());
247 bool is_root
= node
->GetParent() == NULL
;
248 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoLocation(
250 absolute_rect
.x(), absolute_rect
.y(),
251 parent_relative_rect
.x(), parent_relative_rect
.y(),
252 absolute_rect
.width(), absolute_rect
.height(),
256 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoKitKatAttributes(
258 node
->CanOpenPopup(),
259 node
->IsContentInvalid(),
260 node
->IsDismissable(),
262 node
->AndroidInputType(),
263 node
->AndroidLiveRegionType());
264 if (node
->IsCollection()) {
265 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionInfo(
269 node
->IsHierarchical());
271 if (node
->IsCollectionItem() || node
->IsHeading()) {
272 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionItemInfo(
280 if (node
->IsRangeType()) {
281 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoRangeInfo(
283 node
->AndroidRangeType(),
286 node
->RangeCurrentValue());
292 jboolean
BrowserAccessibilityManagerAndroid::PopulateAccessibilityEvent(
293 JNIEnv
* env
, jobject obj
, jobject event
, jint id
, jint event_type
) {
294 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
299 Java_BrowserAccessibilityManager_setAccessibilityEventBooleanAttributes(
304 node
->IsScrollable());
305 Java_BrowserAccessibilityManager_setAccessibilityEventClassName(
307 base::android::ConvertUTF8ToJavaString(env
, node
->GetClassName()).obj());
308 Java_BrowserAccessibilityManager_setAccessibilityEventListAttributes(
310 node
->GetItemIndex(),
311 node
->GetItemCount());
312 Java_BrowserAccessibilityManager_setAccessibilityEventScrollAttributes(
316 node
->GetMaxScrollX(),
317 node
->GetMaxScrollY());
319 switch (event_type
) {
320 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_CHANGED
:
321 Java_BrowserAccessibilityManager_setAccessibilityEventTextChangedAttrs(
323 node
->GetTextChangeFromIndex(),
324 node
->GetTextChangeAddedCount(),
325 node
->GetTextChangeRemovedCount(),
326 base::android::ConvertUTF16ToJavaString(
327 env
, node
->GetTextChangeBeforeText()).obj(),
328 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj());
330 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_SELECTION_CHANGED
:
331 Java_BrowserAccessibilityManager_setAccessibilityEventSelectionAttrs(
333 node
->GetSelectionStart(),
334 node
->GetSelectionEnd(),
335 node
->GetEditableTextLength(),
336 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj());
342 // Backwards-compatible fallback for new KitKat APIs.
343 Java_BrowserAccessibilityManager_setAccessibilityEventKitKatAttributes(
345 node
->CanOpenPopup(),
346 node
->IsContentInvalid(),
347 node
->IsDismissable(),
349 node
->AndroidInputType(),
350 node
->AndroidLiveRegionType());
351 if (node
->IsCollection()) {
352 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionInfo(
356 node
->IsHierarchical());
358 if (node
->IsCollectionItem() || node
->IsHeading()) {
359 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionItemInfo(
367 if (node
->IsRangeType()) {
368 Java_BrowserAccessibilityManager_setAccessibilityEventRangeInfo(
370 node
->AndroidRangeType(),
373 node
->RangeCurrentValue());
379 void BrowserAccessibilityManagerAndroid::Click(
380 JNIEnv
* env
, jobject obj
, jint id
) {
381 BrowserAccessibility
* node
= GetFromID(id
);
383 DoDefaultAction(*node
);
386 void BrowserAccessibilityManagerAndroid::Focus(
387 JNIEnv
* env
, jobject obj
, jint id
) {
388 BrowserAccessibility
* node
= GetFromID(id
);
390 SetFocus(node
, true);
393 void BrowserAccessibilityManagerAndroid::Blur(JNIEnv
* env
, jobject obj
) {
394 SetFocus(GetRoot(), true);
397 void BrowserAccessibilityManagerAndroid::ScrollToMakeNodeVisible(
398 JNIEnv
* env
, jobject obj
, jint id
) {
399 BrowserAccessibility
* node
= GetFromID(id
);
401 ScrollToMakeVisible(*node
, gfx::Rect(node
->GetLocation().size()));
404 void BrowserAccessibilityManagerAndroid::HandleHoverEvent(
405 BrowserAccessibility
* node
) {
406 JNIEnv
* env
= AttachCurrentThread();
407 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
411 BrowserAccessibilityAndroid
* ancestor
=
412 static_cast<BrowserAccessibilityAndroid
*>(node
->GetParent());
414 if (ancestor
->PlatformIsLeaf() ||
415 (ancestor
->IsFocusable() && !ancestor
->HasFocusableChild())) {
417 // Don't break - we want the highest ancestor that's focusable or a
420 ancestor
= static_cast<BrowserAccessibilityAndroid
*>(ancestor
->GetParent());
423 Java_BrowserAccessibilityManager_handleHover(
424 env
, obj
.obj(), node
->GetId());
427 jint
BrowserAccessibilityManagerAndroid::FindElementType(
428 JNIEnv
* env
, jobject obj
, jint start_id
, jstring element_type_str
,
430 BrowserAccessibility
* node
= GetFromID(start_id
);
434 AndroidHtmlElementType element_type
= HtmlElementTypeFromString(
435 base::android::ConvertJavaStringToUTF16(env
, element_type_str
));
437 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
439 switch(element_type
) {
440 case HTML_ELEMENT_TYPE_SECTION
:
441 if (node
->GetRole() == ui::AX_ROLE_ARTICLE
||
442 node
->GetRole() == ui::AX_ROLE_APPLICATION
||
443 node
->GetRole() == ui::AX_ROLE_BANNER
||
444 node
->GetRole() == ui::AX_ROLE_COMPLEMENTARY
||
445 node
->GetRole() == ui::AX_ROLE_CONTENT_INFO
||
446 node
->GetRole() == ui::AX_ROLE_HEADING
||
447 node
->GetRole() == ui::AX_ROLE_MAIN
||
448 node
->GetRole() == ui::AX_ROLE_NAVIGATION
||
449 node
->GetRole() == ui::AX_ROLE_SEARCH
||
450 node
->GetRole() == ui::AX_ROLE_REGION
) {
451 return node
->GetId();
454 case HTML_ELEMENT_TYPE_LIST
:
455 if (node
->GetRole() == ui::AX_ROLE_LIST
||
456 node
->GetRole() == ui::AX_ROLE_GRID
||
457 node
->GetRole() == ui::AX_ROLE_TABLE
||
458 node
->GetRole() == ui::AX_ROLE_TREE
) {
459 return node
->GetId();
462 case HTML_ELEMENT_TYPE_CONTROL
:
463 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsFocusable())
464 return node
->GetId();
466 case HTML_ELEMENT_TYPE_ANY
:
467 // In theory, the API says that an accessibility service could
468 // jump to an element by element name, like 'H1' or 'P'. This isn't
469 // currently used by any accessibility service, and we think it's
470 // better to keep them high-level like 'SECTION' or 'CONTROL', so we
471 // just fall back on linear navigation when we don't recognize the
473 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsClickable())
474 return node
->GetId();
478 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
484 void BrowserAccessibilityManagerAndroid::OnRootChanged(ui::AXNode
* new_root
) {
485 JNIEnv
* env
= AttachCurrentThread();
486 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
490 Java_BrowserAccessibilityManager_handleNavigate(env
, obj
.obj());
494 BrowserAccessibilityManagerAndroid::UseRootScrollOffsetsWhenComputingBounds() {
495 // The Java layer handles the root scroll offset.
499 bool RegisterBrowserAccessibilityManager(JNIEnv
* env
) {
500 return RegisterNativesImpl(env
);
503 } // namespace content