Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_manager.h
blob3dd3e8705dfc3558a94784b103488e366862a9e8
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 #elif defined(OS_WIN)
29 class BrowserAccessibilityManagerWin;
30 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_X11)
31 class BrowserAccessibilityManagerAuraLinux;
32 #endif
34 // For testing.
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 {
54 public:
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 {
87 public:
88 virtual ~BrowserAccessibilityFactory() {}
90 // Create an instance of BrowserAccessibility and return a new
91 // reference to it.
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.
101 int request_id;
102 int match_index;
103 int start_id;
104 int start_offset;
105 int end_id;
106 int end_offset;
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 {
117 public:
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
145 // view got focused.
146 virtual void OnWindowFocused();
148 // Called to notify the accessibility manager that its associated native
149 // view lost focus.
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.
160 void GotMouseDown();
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
164 // to this node.
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.
180 void ScrollToPoint(
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.
188 void SetValue(
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
207 // objects.
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);
225 #if defined(OS_WIN)
226 BrowserAccessibilityManagerWin* ToBrowserAccessibilityManagerWin();
227 #endif
229 #if defined(OS_ANDROID)
230 BrowserAccessibilityManagerAndroid* ToBrowserAccessibilityManagerAndroid();
231 #endif
233 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_X11)
234 BrowserAccessibilityManagerAuraLinux*
235 ToBrowserAccessibilityManagerAuraLinux();
236 #endif
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();
250 // Walk the tree.
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(
260 ui::AXTree* tree,
261 bool root_changed,
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();
277 protected:
278 BrowserAccessibilityManager(
279 BrowserAccessibilityDelegate* delegate,
280 BrowserAccessibilityFactory* factory);
282 BrowserAccessibilityManager(
283 const ui::AXTreeUpdate& initial_tree,
284 BrowserAccessibilityDelegate* delegate,
285 BrowserAccessibilityFactory* factory);
287 private:
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.
307 OSK_ALLOWED
310 protected:
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.
321 ui::AXNode* 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_