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(
251 absolute_rect
.x(), absolute_rect
.y(),
252 parent_relative_rect
.x(), parent_relative_rect
.y(),
253 absolute_rect
.width(), absolute_rect
.height(),
257 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoKitKatAttributes(
259 node
->CanOpenPopup(),
260 node
->IsContentInvalid(),
261 node
->IsDismissable(),
263 node
->AndroidInputType(),
264 node
->AndroidLiveRegionType());
265 if (node
->IsCollection()) {
266 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionInfo(
270 node
->IsHierarchical());
272 if (node
->IsCollectionItem() || node
->IsHeading()) {
273 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionItemInfo(
281 if (node
->IsRangeType()) {
282 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoRangeInfo(
284 node
->AndroidRangeType(),
287 node
->RangeCurrentValue());
293 jboolean
BrowserAccessibilityManagerAndroid::PopulateAccessibilityEvent(
294 JNIEnv
* env
, jobject obj
, jobject event
, jint id
, jint event_type
) {
295 BrowserAccessibilityAndroid
* node
= static_cast<BrowserAccessibilityAndroid
*>(
300 Java_BrowserAccessibilityManager_setAccessibilityEventBooleanAttributes(
305 node
->IsScrollable());
306 Java_BrowserAccessibilityManager_setAccessibilityEventClassName(
308 base::android::ConvertUTF8ToJavaString(env
, node
->GetClassName()).obj());
309 Java_BrowserAccessibilityManager_setAccessibilityEventListAttributes(
311 node
->GetItemIndex(),
312 node
->GetItemCount());
313 Java_BrowserAccessibilityManager_setAccessibilityEventScrollAttributes(
317 node
->GetMaxScrollX(),
318 node
->GetMaxScrollY());
320 switch (event_type
) {
321 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_CHANGED
:
322 Java_BrowserAccessibilityManager_setAccessibilityEventTextChangedAttrs(
324 node
->GetTextChangeFromIndex(),
325 node
->GetTextChangeAddedCount(),
326 node
->GetTextChangeRemovedCount(),
327 base::android::ConvertUTF16ToJavaString(
328 env
, node
->GetTextChangeBeforeText()).obj(),
329 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj());
331 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_SELECTION_CHANGED
:
332 Java_BrowserAccessibilityManager_setAccessibilityEventSelectionAttrs(
334 node
->GetSelectionStart(),
335 node
->GetSelectionEnd(),
336 node
->GetEditableTextLength(),
337 base::android::ConvertUTF16ToJavaString(env
, node
->GetText()).obj());
343 // Backwards-compatible fallback for new KitKat APIs.
344 Java_BrowserAccessibilityManager_setAccessibilityEventKitKatAttributes(
346 node
->CanOpenPopup(),
347 node
->IsContentInvalid(),
348 node
->IsDismissable(),
350 node
->AndroidInputType(),
351 node
->AndroidLiveRegionType());
352 if (node
->IsCollection()) {
353 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionInfo(
357 node
->IsHierarchical());
359 if (node
->IsCollectionItem() || node
->IsHeading()) {
360 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionItemInfo(
368 if (node
->IsRangeType()) {
369 Java_BrowserAccessibilityManager_setAccessibilityEventRangeInfo(
371 node
->AndroidRangeType(),
374 node
->RangeCurrentValue());
380 void BrowserAccessibilityManagerAndroid::Click(
381 JNIEnv
* env
, jobject obj
, jint id
) {
382 BrowserAccessibility
* node
= GetFromID(id
);
384 DoDefaultAction(*node
);
387 void BrowserAccessibilityManagerAndroid::Focus(
388 JNIEnv
* env
, jobject obj
, jint id
) {
389 BrowserAccessibility
* node
= GetFromID(id
);
391 SetFocus(node
, true);
394 void BrowserAccessibilityManagerAndroid::Blur(JNIEnv
* env
, jobject obj
) {
395 SetFocus(GetRoot(), true);
398 void BrowserAccessibilityManagerAndroid::ScrollToMakeNodeVisible(
399 JNIEnv
* env
, jobject obj
, jint id
) {
400 BrowserAccessibility
* node
= GetFromID(id
);
402 ScrollToMakeVisible(*node
, gfx::Rect(node
->GetLocation().size()));
405 void BrowserAccessibilityManagerAndroid::HandleHoverEvent(
406 BrowserAccessibility
* node
) {
407 JNIEnv
* env
= AttachCurrentThread();
408 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
412 BrowserAccessibilityAndroid
* ancestor
=
413 static_cast<BrowserAccessibilityAndroid
*>(node
->GetParent());
415 if (ancestor
->PlatformIsLeaf() ||
416 (ancestor
->IsFocusable() && !ancestor
->HasFocusableChild())) {
418 // Don't break - we want the highest ancestor that's focusable or a
421 ancestor
= static_cast<BrowserAccessibilityAndroid
*>(ancestor
->GetParent());
424 Java_BrowserAccessibilityManager_handleHover(
425 env
, obj
.obj(), node
->GetId());
428 jint
BrowserAccessibilityManagerAndroid::FindElementType(
429 JNIEnv
* env
, jobject obj
, jint start_id
, jstring element_type_str
,
431 BrowserAccessibility
* node
= GetFromID(start_id
);
435 AndroidHtmlElementType element_type
= HtmlElementTypeFromString(
436 base::android::ConvertJavaStringToUTF16(env
, element_type_str
));
438 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
440 switch(element_type
) {
441 case HTML_ELEMENT_TYPE_SECTION
:
442 if (node
->GetRole() == ui::AX_ROLE_ARTICLE
||
443 node
->GetRole() == ui::AX_ROLE_APPLICATION
||
444 node
->GetRole() == ui::AX_ROLE_BANNER
||
445 node
->GetRole() == ui::AX_ROLE_COMPLEMENTARY
||
446 node
->GetRole() == ui::AX_ROLE_CONTENT_INFO
||
447 node
->GetRole() == ui::AX_ROLE_HEADING
||
448 node
->GetRole() == ui::AX_ROLE_MAIN
||
449 node
->GetRole() == ui::AX_ROLE_NAVIGATION
||
450 node
->GetRole() == ui::AX_ROLE_SEARCH
||
451 node
->GetRole() == ui::AX_ROLE_REGION
) {
452 return node
->GetId();
455 case HTML_ELEMENT_TYPE_LIST
:
456 if (node
->GetRole() == ui::AX_ROLE_LIST
||
457 node
->GetRole() == ui::AX_ROLE_GRID
||
458 node
->GetRole() == ui::AX_ROLE_TABLE
||
459 node
->GetRole() == ui::AX_ROLE_TREE
) {
460 return node
->GetId();
463 case HTML_ELEMENT_TYPE_CONTROL
:
464 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsFocusable())
465 return node
->GetId();
467 case HTML_ELEMENT_TYPE_ANY
:
468 // In theory, the API says that an accessibility service could
469 // jump to an element by element name, like 'H1' or 'P'. This isn't
470 // currently used by any accessibility service, and we think it's
471 // better to keep them high-level like 'SECTION' or 'CONTROL', so we
472 // just fall back on linear navigation when we don't recognize the
474 if (static_cast<BrowserAccessibilityAndroid
*>(node
)->IsClickable())
475 return node
->GetId();
479 node
= forwards
? NextInTreeOrder(node
) : PreviousInTreeOrder(node
);
485 void BrowserAccessibilityManagerAndroid::OnRootChanged(ui::AXNode
* new_root
) {
486 JNIEnv
* env
= AttachCurrentThread();
487 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
491 Java_BrowserAccessibilityManager_handleNavigate(env
, obj
.obj());
495 BrowserAccessibilityManagerAndroid::UseRootScrollOffsetsWhenComputingBounds() {
496 // The Java layer handles the root scroll offset.
500 bool RegisterBrowserAccessibilityManager(JNIEnv
* env
) {
501 return RegisterNativesImpl(env
);
504 } // namespace content