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_
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
;
24 class BrowserAccessibility
;
25 class BrowserAccessibilityManager
;
26 #if defined(OS_ANDROID)
27 class BrowserAccessibilityManagerAndroid
;
29 class BrowserAccessibilityManagerWin
;
30 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_X11)
31 class BrowserAccessibilityManagerAuraLinux
;
35 CONTENT_EXPORT
ui::AXTreeUpdate
MakeAXTreeUpdate(
36 const ui::AXNodeData
& node
,
37 const ui::AXNodeData
& node2
= ui::AXNodeData(),
38 const ui::AXNodeData
& node3
= ui::AXNodeData(),
39 const ui::AXNodeData
& node4
= ui::AXNodeData(),
40 const ui::AXNodeData
& node5
= ui::AXNodeData(),
41 const ui::AXNodeData
& node6
= ui::AXNodeData(),
42 const ui::AXNodeData
& node7
= ui::AXNodeData(),
43 const ui::AXNodeData
& node8
= ui::AXNodeData(),
44 const ui::AXNodeData
& node9
= ui::AXNodeData());
46 // Class that can perform actions on behalf of the BrowserAccessibilityManager.
47 // Note: BrowserAccessibilityManager should never cache any of the return
48 // values from any of these interfaces, especially those that return pointers.
49 // They may only be valid within this call stack. That policy eliminates any
50 // concerns about ownership and lifecycle issues; none of these interfaces
51 // transfer ownership and no return values are guaranteed to be valid outside
52 // of the current call stack.
53 class CONTENT_EXPORT BrowserAccessibilityDelegate
{
55 virtual ~BrowserAccessibilityDelegate() {}
56 virtual void AccessibilitySetFocus(int acc_obj_id
) = 0;
57 virtual void AccessibilityDoDefaultAction(int acc_obj_id
) = 0;
58 virtual void AccessibilityShowContextMenu(int acc_obj_id
) = 0;
59 virtual void AccessibilityScrollToMakeVisible(
60 int acc_obj_id
, const gfx::Rect
& subfocus
) = 0;
61 virtual void AccessibilityScrollToPoint(
62 int acc_obj_id
, const gfx::Point
& point
) = 0;
63 virtual void AccessibilitySetScrollOffset(
64 int acc_obj_id
, const gfx::Point
& offset
) = 0;
65 virtual void AccessibilitySetTextSelection(
66 int acc_obj_id
, int start_offset
, int end_offset
) = 0;
67 virtual void AccessibilitySetValue(
68 int acc_obj_id
, const base::string16
& value
) = 0;
69 virtual bool AccessibilityViewHasFocus() const = 0;
70 virtual gfx::Rect
AccessibilityGetViewBounds() const = 0;
71 virtual gfx::Point
AccessibilityOriginInScreen(
72 const gfx::Rect
& bounds
) const = 0;
73 virtual void AccessibilityHitTest(
74 const gfx::Point
& point
) = 0;
75 virtual void AccessibilitySetAccessibilityFocus(int acc_obj_id
) = 0;
76 virtual void AccessibilityFatalError() = 0;
77 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget() = 0;
78 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible() = 0;
79 virtual BrowserAccessibilityManager
* AccessibilityGetChildFrame(
80 int accessibility_node_id
) = 0;
81 virtual void AccessibilityGetAllChildFrames(
82 std::vector
<BrowserAccessibilityManager
*>* child_frames
) = 0;
83 virtual BrowserAccessibility
* AccessibilityGetParentFrame() = 0;
86 class CONTENT_EXPORT BrowserAccessibilityFactory
{
88 virtual ~BrowserAccessibilityFactory() {}
90 // Create an instance of BrowserAccessibility and return a new
92 virtual BrowserAccessibility
* Create();
95 // This is all of the information about the current find in page result,
96 // so we can activate it if requested.
97 struct BrowserAccessibilityFindInPageInfo
{
98 BrowserAccessibilityFindInPageInfo();
100 // This data about find in text results is updated as the user types.
108 // The active request id indicates that the user committed to a find query,
109 // e.g. by pressing enter or pressing the next or previous buttons. If
110 // |active_request_id| == |request_id|, we fire an accessibility event
111 // to move screen reader focus to that event.
112 int active_request_id
;
115 // Manages a tree of BrowserAccessibility objects.
116 class CONTENT_EXPORT BrowserAccessibilityManager
: public ui::AXTreeDelegate
{
118 // Creates the platform-specific BrowserAccessibilityManager, but
119 // with no parent window pointer. Only useful for unit tests.
120 static BrowserAccessibilityManager
* Create(
121 const ui::AXTreeUpdate
& initial_tree
,
122 BrowserAccessibilityDelegate
* delegate
,
123 BrowserAccessibilityFactory
* factory
= new BrowserAccessibilityFactory());
125 ~BrowserAccessibilityManager() override
;
127 void Initialize(const ui::AXTreeUpdate
& initial_tree
);
129 static ui::AXTreeUpdate
GetEmptyDocument();
131 virtual void NotifyAccessibilityEvent(
132 ui::AXEvent event_type
, BrowserAccessibility
* node
) { }
134 // Return a pointer to the root of the tree, does not make a new reference.
135 BrowserAccessibility
* GetRoot();
137 // Returns a pointer to the BrowserAccessibility object for a given AXNode.
138 BrowserAccessibility
* GetFromAXNode(ui::AXNode
* node
);
140 // Return a pointer to the object corresponding to the given id,
141 // does not make a new reference.
142 BrowserAccessibility
* GetFromID(int32 id
);
144 // Called to notify the accessibility manager that its associated native
146 virtual void OnWindowFocused();
148 // Called to notify the accessibility manager that its associated native
150 virtual void OnWindowBlurred();
152 // Notify the accessibility manager about page navigation.
153 void UserIsNavigatingAway();
154 virtual void UserIsReloading();
155 void NavigationSucceeded();
156 void NavigationFailed();
158 // Called to notify the accessibility manager that a mouse down event
159 // occurred in the tab.
162 // Update the focused node to |node|, which may be null.
163 // If |notify| is true, send a message to the renderer to set focus
165 void SetFocus(ui::AXNode
* node
, bool notify
);
166 void SetFocus(BrowserAccessibility
* node
, bool notify
);
168 // Tell the renderer to do the default action for this node.
169 void DoDefaultAction(const BrowserAccessibility
& node
);
171 // Tell the renderer to scroll to make |node| visible.
172 // In addition, if it's not possible to make the entire object visible,
173 // scroll so that the |subfocus| rect is visible at least. The subfocus
174 // rect is in local coordinates of the object itself.
175 void ScrollToMakeVisible(
176 const BrowserAccessibility
& node
, gfx::Rect subfocus
);
178 // Tell the renderer to scroll such that |node| is at |point|,
179 // where |point| is in global coordinates of the WebContents.
181 const BrowserAccessibility
& node
, gfx::Point point
);
183 // If |node| is itself a scrollable container, set its scroll
184 // offset to |offset|.
185 void SetScrollOffset(const BrowserAccessibility
& node
, gfx::Point offset
);
187 // Tell the renderer to set the value of an editable text node.
189 const BrowserAccessibility
& node
, const base::string16
& value
);
191 // Tell the renderer to set the text selection on a node.
192 void SetTextSelection(
193 const BrowserAccessibility
& node
, int start_offset
, int end_offset
);
195 // Retrieve the bounds of the parent View in screen coordinates.
196 gfx::Rect
GetViewBounds();
198 // Fire an event telling native assistive technology to move focus to the
199 // given find in page result.
200 void ActivateFindInPageResult(int request_id
, int match_index
);
202 // Called when the renderer process has notified us of about tree changes.
203 void OnAccessibilityEvents(
204 const std::vector
<AccessibilityHostMsg_EventParams
>& params
);
206 // Called when the renderer process updates the location of accessibility
208 void OnLocationChanges(
209 const std::vector
<AccessibilityHostMsg_LocationChangeParams
>& params
);
211 // Called when a new find in page result is received. We hold on to this
212 // information and don't activate it until the user requests it.
213 void OnFindInPageResult(
214 int request_id
, int match_index
, int start_id
, int start_offset
,
215 int end_id
, int end_offset
);
217 // This is called when the user has committed to a find in page query,
218 // e.g. by pressing enter or tapping on the next / previous result buttons.
219 // If a match has already been received for this request id,
220 // activate the result now by firing an accessibility event. If a match
221 // has not been received, we hold onto this request id and update it
222 // when OnFindInPageResult is called.
223 void ActivateFindInPageResult(int request_id
);
226 BrowserAccessibilityManagerWin
* ToBrowserAccessibilityManagerWin();
229 #if defined(OS_ANDROID)
230 BrowserAccessibilityManagerAndroid
* ToBrowserAccessibilityManagerAndroid();
233 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_X11)
234 BrowserAccessibilityManagerAuraLinux
*
235 ToBrowserAccessibilityManagerAuraLinux();
238 // Return the object that has focus, if it's a descandant of the
239 // given root (inclusive). Does not make a new reference.
240 virtual BrowserAccessibility
* GetFocus(BrowserAccessibility
* root
);
242 // Return the descentant of the given root that has focus, or that object's
243 // active descendant if it has one.
244 BrowserAccessibility
* GetActiveDescendantFocus(BrowserAccessibility
* root
);
246 // True by default, but some platforms want to treat the root
247 // scroll offsets separately.
248 virtual bool UseRootScrollOffsetsWhenComputingBounds();
251 BrowserAccessibility
* NextInTreeOrder(BrowserAccessibility
* node
);
252 BrowserAccessibility
* PreviousInTreeOrder(BrowserAccessibility
* node
);
254 // AXTreeDelegate implementation.
255 void OnNodeWillBeDeleted(ui::AXTree
* tree
, ui::AXNode
* node
) override
;
256 void OnSubtreeWillBeDeleted(ui::AXTree
* tree
, ui::AXNode
* node
) override
;
257 void OnNodeCreated(ui::AXTree
* tree
, ui::AXNode
* node
) override
;
258 void OnNodeChanged(ui::AXTree
* tree
, ui::AXNode
* node
) override
;
259 void OnAtomicUpdateFinished(
262 const std::vector
<ui::AXTreeDelegate::Change
>& changes
) override
;
264 BrowserAccessibilityDelegate
* delegate() const { return delegate_
; }
265 void set_delegate(BrowserAccessibilityDelegate
* delegate
) {
266 delegate_
= delegate
;
269 // If this BrowserAccessibilityManager is a child frame or guest frame,
270 // return the BrowserAccessibilityDelegate from the highest ancestor frame
271 // in the frame tree.
272 BrowserAccessibilityDelegate
* GetDelegateFromRootManager();
274 // Get a snapshot of the current tree as an AXTreeUpdate.
275 ui::AXTreeUpdate
SnapshotAXTreeForTesting();
278 BrowserAccessibilityManager(
279 BrowserAccessibilityDelegate
* delegate
,
280 BrowserAccessibilityFactory
* factory
);
282 BrowserAccessibilityManager(
283 const ui::AXTreeUpdate
& initial_tree
,
284 BrowserAccessibilityDelegate
* delegate
,
285 BrowserAccessibilityFactory
* factory
);
288 // The following states keep track of whether or not the
289 // on-screen keyboard is allowed to be shown.
290 enum OnScreenKeyboardState
{
291 // Never show the on-screen keyboard because this tab is hidden.
292 OSK_DISALLOWED_BECAUSE_TAB_HIDDEN
,
294 // This tab was just shown, so don't pop-up the on-screen keyboard if a
295 // text field gets focus that wasn't the result of an explicit touch.
296 OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED
,
298 // A touch event has occurred within the window, but focus has not
299 // explicitly changed. Allow the on-screen keyboard to be shown if the
300 // touch event was within the bounds of the currently focused object.
301 // Otherwise we'll just wait to see if focus changes.
302 OSK_ALLOWED_WITHIN_FOCUSED_OBJECT
,
304 // Focus has changed within a tab that's already visible. Allow the
305 // on-screen keyboard to show anytime that a touch event leads to an
306 // editable text control getting focus.
311 // The object that can perform actions on our behalf.
312 BrowserAccessibilityDelegate
* delegate_
;
314 // Factory to create BrowserAccessibility objects (for dependency injection).
315 scoped_ptr
<BrowserAccessibilityFactory
> factory_
;
317 // The underlying tree of accessibility objects.
318 scoped_ptr
<ui::AXSerializableTree
> tree_
;
320 // The node that currently has focus.
323 // A mapping from a node id to its wrapper of type BrowserAccessibility.
324 base::hash_map
<int32
, BrowserAccessibility
*> id_wrapper_map_
;
326 // True if the user has initiated a navigation to another page.
327 bool user_is_navigating_away_
;
329 // The on-screen keyboard state.
330 OnScreenKeyboardState osk_state_
;
332 BrowserAccessibilityFindInPageInfo find_in_page_info_
;
334 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManager
);
337 } // namespace content
339 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_