Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_manager_android.h
blob6ab9ae83f9ff56e1af90f95b4c141ae9fc534719
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"
12 namespace content {
14 namespace aria_strings {
15 extern const char kAriaLivePolite[];
16 extern const char kAriaLiveAssertive[];
19 // From android.view.accessibility.AccessibilityNodeInfo in Java:
20 enum AndroidMovementGranularity {
21 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_CHARACTER = 1,
22 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_WORD = 2,
23 ANDROID_ACCESSIBILITY_NODE_INFO_MOVEMENT_GRANULARITY_LINE = 4
26 // From android.view.accessibility.AccessibilityEvent in Java:
27 enum {
28 ANDROID_ACCESSIBILITY_EVENT_TEXT_CHANGED = 16,
29 ANDROID_ACCESSIBILITY_EVENT_TEXT_SELECTION_CHANGED = 8192,
30 ANDROID_ACCESSIBILITY_EVENT_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY = 131072
33 class BrowserAccessibilityAndroid;
35 class CONTENT_EXPORT BrowserAccessibilityManagerAndroid
36 : public BrowserAccessibilityManager {
37 public:
38 BrowserAccessibilityManagerAndroid(
39 base::android::ScopedJavaLocalRef<jobject> content_view_core,
40 const ui::AXTreeUpdate& initial_tree,
41 BrowserAccessibilityDelegate* delegate,
42 BrowserAccessibilityFactory* factory = new BrowserAccessibilityFactory());
44 ~BrowserAccessibilityManagerAndroid() override;
46 static ui::AXTreeUpdate GetEmptyDocument();
48 void SetContentViewCore(
49 base::android::ScopedJavaLocalRef<jobject> content_view_core);
51 // Implementation of BrowserAccessibilityManager.
52 void NotifyAccessibilityEvent(ui::AXEvent event_type,
53 BrowserAccessibility* node) override;
55 // --------------------------------------------------------------------------
56 // Methods called from Java via JNI
57 // --------------------------------------------------------------------------
59 // Tree methods.
60 jint GetRootId(JNIEnv* env, jobject obj);
61 jboolean IsNodeValid(JNIEnv* env, jobject obj, jint id);
62 void HitTest(JNIEnv* env, jobject obj, jint x, jint y);
64 // Methods to get information about a specific node.
65 jboolean IsEditableText(JNIEnv* env, jobject obj, jint id);
66 jint GetEditableTextSelectionStart(JNIEnv* env, jobject obj, jint id);
67 jint GetEditableTextSelectionEnd(JNIEnv* env, jobject obj, jint id);
69 // Populate Java accessibility data structures with info about a node.
70 jboolean PopulateAccessibilityNodeInfo(
71 JNIEnv* env, jobject obj, jobject info, jint id);
72 jboolean PopulateAccessibilityEvent(
73 JNIEnv* env, jobject obj, jobject event, jint id, jint event_type);
75 // Perform actions.
76 void Click(JNIEnv* env, jobject obj, jint id);
77 void Focus(JNIEnv* env, jobject obj, jint id);
78 void Blur(JNIEnv* env, jobject obj);
79 void ScrollToMakeNodeVisible(JNIEnv* env, jobject obj, jint id);
80 void SetTextFieldValue(JNIEnv* env, jobject obj, jint id, jstring value);
81 void SetSelection(JNIEnv* env, jobject obj, jint id, jint start, jint end);
82 jboolean AdjustSlider(JNIEnv* env, jobject obj, jint id, jboolean increment);
84 // Return the id of the next node in tree order in the direction given by
85 // |forwards|, starting with |start_id|, that matches |element_type|,
86 // where |element_type| is a special uppercase string from TalkBack or
87 // BrailleBack indicating general categories of web content like
88 // "SECTION" or "CONTROL". Return 0 if not found.
89 jint FindElementType(JNIEnv* env, jobject obj, jint start_id,
90 jstring element_type, jboolean forwards);
92 // Respond to a ACTION_[NEXT/PREVIOUS]_AT_MOVEMENT_GRANULARITY action
93 // and move the cursor/selection within the given node id. We keep track
94 // of our own selection in BrowserAccessibilityManager.java for static
95 // text, but if this is an editable text node, updates the selected text
96 // in Blink, too, and either way calls
97 // Java_BrowserAccessibilityManager_finishGranularityMove with the
98 // result.
99 jboolean NextAtGranularity(JNIEnv* env, jobject obj,
100 jint granularity, jboolean extend_selection,
101 jint id, jint cursor_index);
102 jboolean PreviousAtGranularity(JNIEnv* env, jobject obj,
103 jint granularity, jboolean extend_selection,
104 jint id, jint cursor_index);
106 // Helper functions to compute the next start and end index when moving
107 // forwards or backwards by character, word, or line. This part is
108 // unit-tested; the Java interfaces above are just wrappers. Both of these
109 // take a single cursor index as input and return the boundaries surrounding
110 // the next word or line. If moving by character, the output start and
111 // end index will be the same.
112 bool NextAtGranularity(
113 int32 granularity, int cursor_index,
114 BrowserAccessibilityAndroid* node, int32* start_index, int32* end_index);
115 bool PreviousAtGranularity(
116 int32 granularity, int cursor_index,
117 BrowserAccessibilityAndroid* node, int32* start_index, int32* end_index);
119 // Set accessibility focus. This sends a message to the renderer to
120 // asynchronously load inline text boxes for this node only, enabling more
121 // accurate movement by granularities on this node.
122 void SetAccessibilityFocus(JNIEnv* env, jobject obj, jint id);
124 protected:
125 // AXTreeDelegate overrides.
126 void OnAtomicUpdateFinished(
127 bool root_changed,
128 const std::vector<ui::AXTreeDelegate::Change>& changes) override;
130 bool UseRootScrollOffsetsWhenComputingBounds() override;
132 private:
133 // This gives BrowserAccessibilityManager::Create access to the class
134 // constructor.
135 friend class BrowserAccessibilityManager;
137 // A weak reference to the Java BrowserAccessibilityManager object.
138 // This avoids adding another reference to BrowserAccessibilityManager and
139 // preventing garbage collection.
140 // Premature garbage collection is prevented by the long-lived reference in
141 // ContentViewCore.
142 JavaObjectWeakGlobalRef java_ref_;
144 // Handle a hover event from the renderer process.
145 void HandleHoverEvent(BrowserAccessibility* node);
147 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManagerAndroid);
150 bool RegisterBrowserAccessibilityManager(JNIEnv* env);
154 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_ANDROID_H_