Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_manager_android.cc
blob09c6ec46d64f02e887cafe75892a6a76ea672e55
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"
7 #include <cmath>
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;
21 namespace {
23 // These are enums from android.view.accessibility.AccessibilityEvent in Java:
24 enum {
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,
33 HTML_ELEMENT_TYPE_ANY
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;
45 else
46 return HTML_ELEMENT_TYPE_ANY;
49 } // anonymous namespace
51 namespace content {
53 namespace aria_strings {
54 const char kAriaLivePolite[] = "polite";
55 const char kAriaLiveAssertive[] = "assertive";
58 // static
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);
84 if (obj.is_null())
85 return;
87 Java_BrowserAccessibilityManager_onNativeObjectDestroyed(env, obj.obj());
90 // static
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);
99 return update;
102 void BrowserAccessibilityManagerAndroid::SetContentViewCore(
103 ScopedJavaLocalRef<jobject> content_view_core) {
104 if (content_view_core.is_null())
105 return;
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);
119 if (obj.is_null())
120 return;
122 if (event_type == ui::AX_EVENT_HIDE)
123 return;
125 if (event_type == ui::AX_EVENT_HOVER) {
126 HandleHoverEvent(node);
127 return;
130 // Always send AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED to notify
131 // the Android system that the accessibility hierarchy rooted at this
132 // node has changed.
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());
140 break;
141 case ui::AX_EVENT_FOCUS:
142 Java_BrowserAccessibilityManager_handleFocusChanged(
143 env, obj.obj(), node->GetId());
144 break;
145 case ui::AX_EVENT_CHECKED_STATE_CHANGED:
146 Java_BrowserAccessibilityManager_handleCheckStateChanged(
147 env, obj.obj(), node->GetId());
148 break;
149 case ui::AX_EVENT_SCROLL_POSITION_CHANGED:
150 Java_BrowserAccessibilityManager_handleScrollPositionChanged(
151 env, obj.obj(), node->GetId());
152 break;
153 case ui::AX_EVENT_SCROLLED_TO_ANCHOR:
154 Java_BrowserAccessibilityManager_handleScrolledToAnchor(
155 env, obj.obj(), node->GetId());
156 break;
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.
162 // Speak its text.
163 BrowserAccessibilityAndroid* android_node =
164 static_cast<BrowserAccessibilityAndroid*>(node);
165 Java_BrowserAccessibilityManager_announceLiveRegionText(
166 env, obj.obj(),
167 base::android::ConvertUTF16ToJavaString(
168 env, android_node->GetText()).obj());
169 break;
171 case ui::AX_EVENT_TEXT_SELECTION_CHANGED:
172 Java_BrowserAccessibilityManager_handleTextSelectionChanged(
173 env, obj.obj(), node->GetId());
174 break;
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());
182 break;
183 default:
184 // There are some notifications that aren't meaningful on Android.
185 // It's okay to skip them.
186 break;
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) {
201 if (delegate())
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*>(
208 GetFromID(id));
209 if (!node)
210 return false;
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(
221 env, obj, info,
223 node->IsCheckable(),
224 node->IsChecked(),
225 node->IsClickable(),
226 node->IsEnabled(),
227 node->IsFocusable(),
228 node->IsFocused(),
229 node->IsPassword(),
230 node->IsScrollable(),
231 node->IsSelected(),
232 node->IsVisibleToUser());
233 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoClassName(
234 env, obj, info,
235 base::android::ConvertUTF8ToJavaString(env, node->GetClassName()).obj());
236 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoContentDescription(
237 env, obj, info,
238 base::android::ConvertUTF16ToJavaString(env, node->GetText()).obj(),
239 node->IsLink());
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(
249 env, obj, info,
250 absolute_rect.x(), absolute_rect.y(),
251 parent_relative_rect.x(), parent_relative_rect.y(),
252 absolute_rect.width(), absolute_rect.height(),
253 is_root);
255 // New KitKat APIs
256 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoKitKatAttributes(
257 env, obj, info,
258 node->CanOpenPopup(),
259 node->IsContentInvalid(),
260 node->IsDismissable(),
261 node->IsMultiLine(),
262 node->AndroidInputType(),
263 node->AndroidLiveRegionType());
264 if (node->IsCollection()) {
265 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionInfo(
266 env, obj, info,
267 node->RowCount(),
268 node->ColumnCount(),
269 node->IsHierarchical());
271 if (node->IsCollectionItem() || node->IsHeading()) {
272 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoCollectionItemInfo(
273 env, obj, info,
274 node->RowIndex(),
275 node->RowSpan(),
276 node->ColumnIndex(),
277 node->ColumnSpan(),
278 node->IsHeading());
280 if (node->IsRangeType()) {
281 Java_BrowserAccessibilityManager_setAccessibilityNodeInfoRangeInfo(
282 env, obj, info,
283 node->AndroidRangeType(),
284 node->RangeMin(),
285 node->RangeMax(),
286 node->RangeCurrentValue());
289 return true;
292 jboolean BrowserAccessibilityManagerAndroid::PopulateAccessibilityEvent(
293 JNIEnv* env, jobject obj, jobject event, jint id, jint event_type) {
294 BrowserAccessibilityAndroid* node = static_cast<BrowserAccessibilityAndroid*>(
295 GetFromID(id));
296 if (!node)
297 return false;
299 Java_BrowserAccessibilityManager_setAccessibilityEventBooleanAttributes(
300 env, obj, event,
301 node->IsChecked(),
302 node->IsEnabled(),
303 node->IsPassword(),
304 node->IsScrollable());
305 Java_BrowserAccessibilityManager_setAccessibilityEventClassName(
306 env, obj, event,
307 base::android::ConvertUTF8ToJavaString(env, node->GetClassName()).obj());
308 Java_BrowserAccessibilityManager_setAccessibilityEventListAttributes(
309 env, obj, event,
310 node->GetItemIndex(),
311 node->GetItemCount());
312 Java_BrowserAccessibilityManager_setAccessibilityEventScrollAttributes(
313 env, obj, event,
314 node->GetScrollX(),
315 node->GetScrollY(),
316 node->GetMaxScrollX(),
317 node->GetMaxScrollY());
319 switch (event_type) {
320 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_CHANGED:
321 Java_BrowserAccessibilityManager_setAccessibilityEventTextChangedAttrs(
322 env, obj, event,
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());
329 break;
330 case ANDROID_ACCESSIBILITY_EVENT_TYPE_VIEW_TEXT_SELECTION_CHANGED:
331 Java_BrowserAccessibilityManager_setAccessibilityEventSelectionAttrs(
332 env, obj, event,
333 node->GetSelectionStart(),
334 node->GetSelectionEnd(),
335 node->GetEditableTextLength(),
336 base::android::ConvertUTF16ToJavaString(env, node->GetText()).obj());
337 break;
338 default:
339 break;
342 // Backwards-compatible fallback for new KitKat APIs.
343 Java_BrowserAccessibilityManager_setAccessibilityEventKitKatAttributes(
344 env, obj, event,
345 node->CanOpenPopup(),
346 node->IsContentInvalid(),
347 node->IsDismissable(),
348 node->IsMultiLine(),
349 node->AndroidInputType(),
350 node->AndroidLiveRegionType());
351 if (node->IsCollection()) {
352 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionInfo(
353 env, obj, event,
354 node->RowCount(),
355 node->ColumnCount(),
356 node->IsHierarchical());
358 if (node->IsCollectionItem() || node->IsHeading()) {
359 Java_BrowserAccessibilityManager_setAccessibilityEventCollectionItemInfo(
360 env, obj, event,
361 node->RowIndex(),
362 node->RowSpan(),
363 node->ColumnIndex(),
364 node->ColumnSpan(),
365 node->IsHeading());
367 if (node->IsRangeType()) {
368 Java_BrowserAccessibilityManager_setAccessibilityEventRangeInfo(
369 env, obj, event,
370 node->AndroidRangeType(),
371 node->RangeMin(),
372 node->RangeMax(),
373 node->RangeCurrentValue());
376 return true;
379 void BrowserAccessibilityManagerAndroid::Click(
380 JNIEnv* env, jobject obj, jint id) {
381 BrowserAccessibility* node = GetFromID(id);
382 if (node)
383 DoDefaultAction(*node);
386 void BrowserAccessibilityManagerAndroid::Focus(
387 JNIEnv* env, jobject obj, jint id) {
388 BrowserAccessibility* node = GetFromID(id);
389 if (node)
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);
400 if (node)
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);
408 if (obj.is_null())
409 return;
411 BrowserAccessibilityAndroid* ancestor =
412 static_cast<BrowserAccessibilityAndroid*>(node->GetParent());
413 while (ancestor) {
414 if (ancestor->PlatformIsLeaf() ||
415 (ancestor->IsFocusable() && !ancestor->HasFocusableChild())) {
416 node = ancestor;
417 // Don't break - we want the highest ancestor that's focusable or a
418 // leaf node.
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,
429 jboolean forwards) {
430 BrowserAccessibility* node = GetFromID(start_id);
431 if (!node)
432 return 0;
434 AndroidHtmlElementType element_type = HtmlElementTypeFromString(
435 base::android::ConvertJavaStringToUTF16(env, element_type_str));
437 node = forwards ? NextInTreeOrder(node) : PreviousInTreeOrder(node);
438 while (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();
453 break;
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();
461 break;
462 case HTML_ELEMENT_TYPE_CONTROL:
463 if (static_cast<BrowserAccessibilityAndroid*>(node)->IsFocusable())
464 return node->GetId();
465 break;
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
472 // element type.
473 if (static_cast<BrowserAccessibilityAndroid*>(node)->IsClickable())
474 return node->GetId();
475 break;
478 node = forwards ? NextInTreeOrder(node) : PreviousInTreeOrder(node);
481 return 0;
484 void BrowserAccessibilityManagerAndroid::OnRootChanged(ui::AXNode* new_root) {
485 JNIEnv* env = AttachCurrentThread();
486 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
487 if (obj.is_null())
488 return;
490 Java_BrowserAccessibilityManager_handleNavigate(env, obj.obj());
493 bool
494 BrowserAccessibilityManagerAndroid::UseRootScrollOffsetsWhenComputingBounds() {
495 // The Java layer handles the root scroll offset.
496 return false;
499 bool RegisterBrowserAccessibilityManager(JNIEnv* env) {
500 return RegisterNativesImpl(env);
503 } // namespace content