Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_manager.h
blobf60bd025eb27125108508712a85204224f5b91b6
1 // Copyright (c) 2012 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_H_
6 #define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_
8 #include <vector>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "build/build_config.h"
13 #include "content/common/content_export.h"
14 #include "third_party/WebKit/public/web/WebAXEnums.h"
15 #include "ui/accessibility/ax_node_data.h"
16 #include "ui/accessibility/ax_serializable_tree.h"
17 #include "ui/accessibility/ax_tree_update.h"
18 #include "ui/gfx/native_widget_types.h"
20 struct AccessibilityHostMsg_EventParams;
21 struct AccessibilityHostMsg_LocationChangeParams;
23 namespace content {
24 class BrowserAccessibility;
25 class BrowserAccessibilityManager;
26 #if defined(OS_ANDROID)
27 class BrowserAccessibilityManagerAndroid;
28 #endif
29 #if defined(OS_WIN)
30 class BrowserAccessibilityManagerWin;
31 #endif
33 // For testing.
34 CONTENT_EXPORT ui::AXTreeUpdate MakeAXTreeUpdate(
35 const ui::AXNodeData& node,
36 const ui::AXNodeData& node2 = ui::AXNodeData(),
37 const ui::AXNodeData& node3 = ui::AXNodeData(),
38 const ui::AXNodeData& node4 = ui::AXNodeData(),
39 const ui::AXNodeData& node5 = ui::AXNodeData(),
40 const ui::AXNodeData& node6 = ui::AXNodeData(),
41 const ui::AXNodeData& node7 = ui::AXNodeData(),
42 const ui::AXNodeData& node8 = ui::AXNodeData(),
43 const ui::AXNodeData& node9 = ui::AXNodeData());
45 // Class that can perform actions on behalf of the BrowserAccessibilityManager.
46 // Note: BrowserAccessibilityManager should never cache any of the return
47 // values from any of these interfaces, especially those that return pointers.
48 // They may only be valid within this call stack. That policy eliminates any
49 // concerns about ownership and lifecycle issues; none of these interfaces
50 // transfer ownership and no return values are guaranteed to be valid outside
51 // of the current call stack.
52 class CONTENT_EXPORT BrowserAccessibilityDelegate {
53 public:
54 virtual ~BrowserAccessibilityDelegate() {}
55 virtual void AccessibilitySetFocus(int acc_obj_id) = 0;
56 virtual void AccessibilityDoDefaultAction(int acc_obj_id) = 0;
57 virtual void AccessibilityShowMenu(const gfx::Point& global_point) = 0;
58 virtual void AccessibilityScrollToMakeVisible(
59 int acc_obj_id, const gfx::Rect& subfocus) = 0;
60 virtual void AccessibilityScrollToPoint(
61 int acc_obj_id, const gfx::Point& point) = 0;
62 virtual void AccessibilitySetTextSelection(
63 int acc_obj_id, int start_offset, int end_offset) = 0;
64 virtual void AccessibilitySetValue(
65 int acc_obj_id, const base::string16& value) = 0;
66 virtual bool AccessibilityViewHasFocus() const = 0;
67 virtual gfx::Rect AccessibilityGetViewBounds() const = 0;
68 virtual gfx::Point AccessibilityOriginInScreen(
69 const gfx::Rect& bounds) const = 0;
70 virtual void AccessibilityHitTest(
71 const gfx::Point& point) = 0;
72 virtual void AccessibilitySetAccessibilityFocus(int acc_obj_id) = 0;
73 virtual void AccessibilityFatalError() = 0;
74 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget() = 0;
75 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible() = 0;
76 virtual BrowserAccessibilityManager* AccessibilityGetChildFrame(
77 int accessibility_node_id) = 0;
78 virtual void AccessibilityGetAllChildFrames(
79 std::vector<BrowserAccessibilityManager*>* child_frames) = 0;
80 virtual BrowserAccessibility* AccessibilityGetParentFrame() = 0;
83 class CONTENT_EXPORT BrowserAccessibilityFactory {
84 public:
85 virtual ~BrowserAccessibilityFactory() {}
87 // Create an instance of BrowserAccessibility and return a new
88 // reference to it.
89 virtual BrowserAccessibility* Create();
92 // This is all of the information about the current find in page result,
93 // so we can activate it if requested.
94 struct BrowserAccessibilityFindInPageInfo {
95 BrowserAccessibilityFindInPageInfo();
97 // This data about find in text results is updated as the user types.
98 int request_id;
99 int match_index;
100 int start_id;
101 int start_offset;
102 int end_id;
103 int end_offset;
105 // The active request id indicates that the user committed to a find query,
106 // e.g. by pressing enter or pressing the next or previous buttons. If
107 // |active_request_id| == |request_id|, we fire an accessibility event
108 // to move screen reader focus to that event.
109 int active_request_id;
112 // Manages a tree of BrowserAccessibility objects.
113 class CONTENT_EXPORT BrowserAccessibilityManager : public ui::AXTreeDelegate {
114 public:
115 // Creates the platform-specific BrowserAccessibilityManager, but
116 // with no parent window pointer. Only useful for unit tests.
117 static BrowserAccessibilityManager* Create(
118 const ui::AXTreeUpdate& initial_tree,
119 BrowserAccessibilityDelegate* delegate,
120 BrowserAccessibilityFactory* factory = new BrowserAccessibilityFactory());
122 ~BrowserAccessibilityManager() override;
124 void Initialize(const ui::AXTreeUpdate& initial_tree);
126 static ui::AXTreeUpdate GetEmptyDocument();
128 virtual void NotifyAccessibilityEvent(
129 ui::AXEvent event_type, BrowserAccessibility* node) { }
131 // Return a pointer to the root of the tree, does not make a new reference.
132 BrowserAccessibility* GetRoot();
134 // Returns a pointer to the BrowserAccessibility object for a given AXNode.
135 BrowserAccessibility* GetFromAXNode(ui::AXNode* node);
137 // Return a pointer to the object corresponding to the given id,
138 // does not make a new reference.
139 BrowserAccessibility* GetFromID(int32 id);
141 // Called to notify the accessibility manager that its associated native
142 // view got focused.
143 virtual void OnWindowFocused();
145 // Called to notify the accessibility manager that its associated native
146 // view lost focus.
147 virtual void OnWindowBlurred();
149 // Notify the accessibility manager about page navigation.
150 void UserIsNavigatingAway();
151 virtual void UserIsReloading();
152 void NavigationSucceeded();
153 void NavigationFailed();
155 // Called to notify the accessibility manager that a mouse down event
156 // occurred in the tab.
157 void GotMouseDown();
159 // Update the focused node to |node|, which may be null.
160 // If |notify| is true, send a message to the renderer to set focus
161 // to this node.
162 void SetFocus(ui::AXNode* node, bool notify);
163 void SetFocus(BrowserAccessibility* node, bool notify);
165 // Tell the renderer to do the default action for this node.
166 void DoDefaultAction(const BrowserAccessibility& node);
168 // Tell the renderer to scroll to make |node| visible.
169 // In addition, if it's not possible to make the entire object visible,
170 // scroll so that the |subfocus| rect is visible at least. The subfocus
171 // rect is in local coordinates of the object itself.
172 void ScrollToMakeVisible(
173 const BrowserAccessibility& node, gfx::Rect subfocus);
175 // Tell the renderer to scroll such that |node| is at |point|,
176 // where |point| is in global coordinates of the WebContents.
177 void ScrollToPoint(
178 const BrowserAccessibility& node, gfx::Point point);
180 // Tell the renderer to set the value of an editable text node.
181 void SetValue(
182 const BrowserAccessibility& node, const base::string16& value);
184 // Tell the renderer to set the text selection on a node.
185 void SetTextSelection(
186 const BrowserAccessibility& node, int start_offset, int end_offset);
188 // Retrieve the bounds of the parent View in screen coordinates.
189 gfx::Rect GetViewBounds();
191 // Fire an event telling native assistive technology to move focus to the
192 // given find in page result.
193 void ActivateFindInPageResult(int request_id, int match_index);
195 // Called when the renderer process has notified us of about tree changes.
196 void OnAccessibilityEvents(
197 const std::vector<AccessibilityHostMsg_EventParams>& params);
199 // Called when the renderer process updates the location of accessibility
200 // objects.
201 void OnLocationChanges(
202 const std::vector<AccessibilityHostMsg_LocationChangeParams>& params);
204 // Called when a new find in page result is received. We hold on to this
205 // information and don't activate it until the user requests it.
206 void OnFindInPageResult(
207 int request_id, int match_index, int start_id, int start_offset,
208 int end_id, int end_offset);
210 // This is called when the user has committed to a find in page query,
211 // e.g. by pressing enter or tapping on the next / previous result buttons.
212 // If a match has already been received for this request id,
213 // activate the result now by firing an accessibility event. If a match
214 // has not been received, we hold onto this request id and update it
215 // when OnFindInPageResult is called.
216 void ActivateFindInPageResult(int request_id);
218 #if defined(OS_WIN)
219 BrowserAccessibilityManagerWin* ToBrowserAccessibilityManagerWin();
220 #endif
222 #if defined(OS_ANDROID)
223 BrowserAccessibilityManagerAndroid* ToBrowserAccessibilityManagerAndroid();
224 #endif
226 // Return the object that has focus, if it's a descandant of the
227 // given root (inclusive). Does not make a new reference.
228 virtual BrowserAccessibility* GetFocus(BrowserAccessibility* root);
230 // Return the descentant of the given root that has focus, or that object's
231 // active descendant if it has one.
232 BrowserAccessibility* GetActiveDescendantFocus(BrowserAccessibility* root);
234 // True by default, but some platforms want to treat the root
235 // scroll offsets separately.
236 virtual bool UseRootScrollOffsetsWhenComputingBounds();
238 // Walk the tree.
239 BrowserAccessibility* NextInTreeOrder(BrowserAccessibility* node);
240 BrowserAccessibility* PreviousInTreeOrder(BrowserAccessibility* node);
242 // AXTreeDelegate implementation.
243 void OnNodeWillBeDeleted(ui::AXNode* node) override;
244 void OnSubtreeWillBeDeleted(ui::AXNode* node) override;
245 void OnNodeCreated(ui::AXNode* node) override;
246 void OnNodeChanged(ui::AXNode* node) override;
247 void OnAtomicUpdateFinished(
248 bool root_changed,
249 const std::vector<ui::AXTreeDelegate::Change>& changes) override;
251 BrowserAccessibilityDelegate* delegate() const { return delegate_; }
252 void set_delegate(BrowserAccessibilityDelegate* delegate) {
253 delegate_ = delegate;
256 // If this BrowserAccessibilityManager is a child frame or guest frame,
257 // return the BrowserAccessibilityDelegate from the highest ancestor frame
258 // in the frame tree.
259 BrowserAccessibilityDelegate* GetDelegateFromRootManager();
261 // Get a snapshot of the current tree as an AXTreeUpdate.
262 ui::AXTreeUpdate SnapshotAXTreeForTesting();
264 protected:
265 BrowserAccessibilityManager(
266 BrowserAccessibilityDelegate* delegate,
267 BrowserAccessibilityFactory* factory);
269 BrowserAccessibilityManager(
270 const ui::AXTreeUpdate& initial_tree,
271 BrowserAccessibilityDelegate* delegate,
272 BrowserAccessibilityFactory* factory);
274 private:
275 // The following states keep track of whether or not the
276 // on-screen keyboard is allowed to be shown.
277 enum OnScreenKeyboardState {
278 // Never show the on-screen keyboard because this tab is hidden.
279 OSK_DISALLOWED_BECAUSE_TAB_HIDDEN,
281 // This tab was just shown, so don't pop-up the on-screen keyboard if a
282 // text field gets focus that wasn't the result of an explicit touch.
283 OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED,
285 // A touch event has occurred within the window, but focus has not
286 // explicitly changed. Allow the on-screen keyboard to be shown if the
287 // touch event was within the bounds of the currently focused object.
288 // Otherwise we'll just wait to see if focus changes.
289 OSK_ALLOWED_WITHIN_FOCUSED_OBJECT,
291 // Focus has changed within a tab that's already visible. Allow the
292 // on-screen keyboard to show anytime that a touch event leads to an
293 // editable text control getting focus.
294 OSK_ALLOWED
297 protected:
298 // The object that can perform actions on our behalf.
299 BrowserAccessibilityDelegate* delegate_;
301 // Factory to create BrowserAccessibility objects (for dependency injection).
302 scoped_ptr<BrowserAccessibilityFactory> factory_;
304 // The underlying tree of accessibility objects.
305 scoped_ptr<ui::AXSerializableTree> tree_;
307 // The node that currently has focus.
308 ui::AXNode* focus_;
310 // A mapping from a node id to its wrapper of type BrowserAccessibility.
311 base::hash_map<int32, BrowserAccessibility*> id_wrapper_map_;
313 // True if the user has initiated a navigation to another page.
314 bool user_is_navigating_away_;
316 // The on-screen keyboard state.
317 OnScreenKeyboardState osk_state_;
319 BrowserAccessibilityFindInPageInfo find_in_page_info_;
321 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManager);
324 } // namespace content
326 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_