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 #ifndef CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_ANDROID_H_
6 #define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_ANDROID_H_
8 #include "base/android/scoped_java_ref.h"
9 #include "content/browser/accessibility/browser_accessibility_manager.h"
10 #include "content/browser/android/content_view_core_impl.h"
14 namespace aria_strings
{
15 extern const char kAriaLivePolite
[];
16 extern const char kAriaLiveAssertive
[];
19 // A Java counterpart will be generated for this enum.
20 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.content.browser.accessibility
21 enum ScrollDirection
{
30 // From android.view.accessibility.AccessibilityNodeInfo in Java:
31 enum AndroidMovementGranularity
{
32 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER
= 1,
33 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD
= 2,
34 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE
= 4
37 // From android.view.accessibility.AccessibilityEvent in Java:
39 ANDROID_ACCESSIBILITY_EVENT_TEXT_CHANGED
= 16,
40 ANDROID_ACCESSIBILITY_EVENT_TEXT_SELECTION_CHANGED
= 8192,
41 ANDROID_ACCESSIBILITY_EVENT_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
= 131072
44 class BrowserAccessibilityAndroid
;
46 class CONTENT_EXPORT BrowserAccessibilityManagerAndroid
47 : public BrowserAccessibilityManager
{
49 BrowserAccessibilityManagerAndroid(
50 base::android::ScopedJavaLocalRef
<jobject
> content_view_core
,
51 const SimpleAXTreeUpdate
& initial_tree
,
52 BrowserAccessibilityDelegate
* delegate
,
53 BrowserAccessibilityFactory
* factory
= new BrowserAccessibilityFactory());
55 ~BrowserAccessibilityManagerAndroid() override
;
57 static SimpleAXTreeUpdate
GetEmptyDocument();
59 void SetContentViewCore(
60 base::android::ScopedJavaLocalRef
<jobject
> content_view_core
);
62 // By default, the tree is pruned for a better screen reading experience,
64 // * If the node has only static text children
65 // * If the node is focusable and has no focusable children
66 // * If the node is a heading
67 // This can be turned off to generate a tree that more accurately reflects
68 // the DOM and includes style changes within these nodes.
69 void set_prune_tree_for_screen_reader(bool prune
) {
70 prune_tree_for_screen_reader_
= prune
;
72 bool prune_tree_for_screen_reader() { return prune_tree_for_screen_reader_
; }
74 // Implementation of BrowserAccessibilityManager.
75 void NotifyAccessibilityEvent(ui::AXEvent event_type
,
76 BrowserAccessibility
* node
) override
;
78 // --------------------------------------------------------------------------
79 // Methods called from Java via JNI
80 // --------------------------------------------------------------------------
83 jint
GetRootId(JNIEnv
* env
, jobject obj
);
84 jboolean
IsNodeValid(JNIEnv
* env
, jobject obj
, jint id
);
85 void HitTest(JNIEnv
* env
, jobject obj
, jint x
, jint y
);
87 // Methods to get information about a specific node.
88 jboolean
IsEditableText(JNIEnv
* env
, jobject obj
, jint id
);
89 jint
GetEditableTextSelectionStart(JNIEnv
* env
, jobject obj
, jint id
);
90 jint
GetEditableTextSelectionEnd(JNIEnv
* env
, jobject obj
, jint id
);
92 // Populate Java accessibility data structures with info about a node.
93 jboolean
PopulateAccessibilityNodeInfo(
94 JNIEnv
* env
, jobject obj
, jobject info
, jint id
);
95 jboolean
PopulateAccessibilityEvent(
96 JNIEnv
* env
, jobject obj
, jobject event
, jint id
, jint event_type
);
99 void Click(JNIEnv
* env
, jobject obj
, jint id
);
100 void Focus(JNIEnv
* env
, jobject obj
, jint id
);
101 void Blur(JNIEnv
* env
, jobject obj
);
102 void ScrollToMakeNodeVisible(JNIEnv
* env
, jobject obj
, jint id
);
103 void SetTextFieldValue(JNIEnv
* env
, jobject obj
, jint id
, jstring value
);
104 void SetSelection(JNIEnv
* env
, jobject obj
, jint id
, jint start
, jint end
);
105 jboolean
AdjustSlider(JNIEnv
* env
, jobject obj
, jint id
, jboolean increment
);
107 // Return the id of the next node in tree order in the direction given by
108 // |forwards|, starting with |start_id|, that matches |element_type|,
109 // where |element_type| is a special uppercase string from TalkBack or
110 // BrailleBack indicating general categories of web content like
111 // "SECTION" or "CONTROL". Return 0 if not found.
112 jint
FindElementType(JNIEnv
* env
, jobject obj
, jint start_id
,
113 jstring element_type
, jboolean forwards
);
115 // Respond to a ACTION_[NEXT/PREVIOUS]_AT_MOVEMENT_GRANULARITY action
116 // and move the cursor/selection within the given node id. We keep track
117 // of our own selection in BrowserAccessibilityManager.java for static
118 // text, but if this is an editable text node, updates the selected text
119 // in Blink, too, and either way calls
120 // Java_BrowserAccessibilityManager_finishGranularityMove with the
122 jboolean
NextAtGranularity(JNIEnv
* env
, jobject obj
,
123 jint granularity
, jboolean extend_selection
,
124 jint id
, jint cursor_index
);
125 jboolean
PreviousAtGranularity(JNIEnv
* env
, jobject obj
,
126 jint granularity
, jboolean extend_selection
,
127 jint id
, jint cursor_index
);
129 // Helper functions to compute the next start and end index when moving
130 // forwards or backwards by character, word, or line. This part is
131 // unit-tested; the Java interfaces above are just wrappers. Both of these
132 // take a single cursor index as input and return the boundaries surrounding
133 // the next word or line. If moving by character, the output start and
134 // end index will be the same.
135 bool NextAtGranularity(
136 int32 granularity
, int cursor_index
,
137 BrowserAccessibilityAndroid
* node
, int32
* start_index
, int32
* end_index
);
138 bool PreviousAtGranularity(
139 int32 granularity
, int cursor_index
,
140 BrowserAccessibilityAndroid
* node
, int32
* start_index
, int32
* end_index
);
142 // Set accessibility focus. This sends a message to the renderer to
143 // asynchronously load inline text boxes for this node only, enabling more
144 // accurate movement by granularities on this node.
145 void SetAccessibilityFocus(JNIEnv
* env
, jobject obj
, jint id
);
147 // Returns true if the object is a slider.
148 bool IsSlider(JNIEnv
* env
, jobject obj
, jint id
);
150 // Scrolls any scrollable container by about 80% of one page in the
152 bool Scroll(JNIEnv
* env
, jobject obj
, jint id
, int direction
);
155 // AXTreeDelegate overrides.
156 void OnAtomicUpdateFinished(
159 const std::vector
<ui::AXTreeDelegate::Change
>& changes
) override
;
161 bool UseRootScrollOffsetsWhenComputingBounds() override
;
164 // This gives BrowserAccessibilityManager::Create access to the class
166 friend class BrowserAccessibilityManager
;
168 // A weak reference to the Java BrowserAccessibilityManager object.
169 // This avoids adding another reference to BrowserAccessibilityManager and
170 // preventing garbage collection.
171 // Premature garbage collection is prevented by the long-lived reference in
173 JavaObjectWeakGlobalRef java_ref_
;
175 // Handle a hover event from the renderer process.
176 void HandleHoverEvent(BrowserAccessibility
* node
);
178 // See docs for set_prune_tree_for_screen_reader, above.
179 bool prune_tree_for_screen_reader_
;
181 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManagerAndroid
);
184 bool RegisterBrowserAccessibilityManager(JNIEnv
* env
);
188 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_ANDROID_H_