[Storage] Blob Storage Refactoring pt 1:
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_manager.h
blob765ac4d971f3e418b9de01ca2669deedbb134b90
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 // Called to notify the accessibility manager that a mouse down event
150 // occurred in the tab.
151 void GotMouseDown();
153 // Update the focused node to |node|, which may be null.
154 // If |notify| is true, send a message to the renderer to set focus
155 // to this node.
156 void SetFocus(ui::AXNode* node, bool notify);
157 void SetFocus(BrowserAccessibility* node, bool notify);
159 // Tell the renderer to do the default action for this node.
160 void DoDefaultAction(const BrowserAccessibility& node);
162 // Tell the renderer to scroll to make |node| visible.
163 // In addition, if it's not possible to make the entire object visible,
164 // scroll so that the |subfocus| rect is visible at least. The subfocus
165 // rect is in local coordinates of the object itself.
166 void ScrollToMakeVisible(
167 const BrowserAccessibility& node, gfx::Rect subfocus);
169 // Tell the renderer to scroll such that |node| is at |point|,
170 // where |point| is in global coordinates of the WebContents.
171 void ScrollToPoint(
172 const BrowserAccessibility& node, gfx::Point point);
174 // Tell the renderer to set the value of an editable text node.
175 void SetValue(
176 const BrowserAccessibility& node, const base::string16& value);
178 // Tell the renderer to set the text selection on a node.
179 void SetTextSelection(
180 const BrowserAccessibility& node, int start_offset, int end_offset);
182 // Retrieve the bounds of the parent View in screen coordinates.
183 gfx::Rect GetViewBounds();
185 // Fire an event telling native assistive technology to move focus to the
186 // given find in page result.
187 void ActivateFindInPageResult(int request_id, int match_index);
189 // Called when the renderer process has notified us of about tree changes.
190 void OnAccessibilityEvents(
191 const std::vector<AccessibilityHostMsg_EventParams>& params);
193 // Called when the renderer process updates the location of accessibility
194 // objects.
195 void OnLocationChanges(
196 const std::vector<AccessibilityHostMsg_LocationChangeParams>& params);
198 // Called when a new find in page result is received. We hold on to this
199 // information and don't activate it until the user requests it.
200 void OnFindInPageResult(
201 int request_id, int match_index, int start_id, int start_offset,
202 int end_id, int end_offset);
204 // This is called when the user has committed to a find in page query,
205 // e.g. by pressing enter or tapping on the next / previous result buttons.
206 // If a match has already been received for this request id,
207 // activate the result now by firing an accessibility event. If a match
208 // has not been received, we hold onto this request id and update it
209 // when OnFindInPageResult is called.
210 void ActivateFindInPageResult(int request_id);
212 #if defined(OS_WIN)
213 BrowserAccessibilityManagerWin* ToBrowserAccessibilityManagerWin();
214 #endif
216 #if defined(OS_ANDROID)
217 BrowserAccessibilityManagerAndroid* ToBrowserAccessibilityManagerAndroid();
218 #endif
220 // Return the object that has focus, if it's a descandant of the
221 // given root (inclusive). Does not make a new reference.
222 virtual BrowserAccessibility* GetFocus(BrowserAccessibility* root);
224 // Return the descentant of the given root that has focus, or that object's
225 // active descendant if it has one.
226 BrowserAccessibility* GetActiveDescendantFocus(BrowserAccessibility* root);
228 // True by default, but some platforms want to treat the root
229 // scroll offsets separately.
230 virtual bool UseRootScrollOffsetsWhenComputingBounds();
232 // Walk the tree.
233 BrowserAccessibility* NextInTreeOrder(BrowserAccessibility* node);
234 BrowserAccessibility* PreviousInTreeOrder(BrowserAccessibility* node);
236 // AXTreeDelegate implementation.
237 void OnNodeWillBeDeleted(ui::AXNode* node) override;
238 void OnNodeCreated(ui::AXNode* node) override;
239 void OnNodeChanged(ui::AXNode* node) override;
240 void OnNodeCreationFinished(ui::AXNode* node) override;
241 void OnNodeChangeFinished(ui::AXNode* node) override;
242 void OnRootChanged(ui::AXNode* new_root) override {}
244 BrowserAccessibilityDelegate* delegate() const { return delegate_; }
245 void set_delegate(BrowserAccessibilityDelegate* delegate) {
246 delegate_ = delegate;
249 // If this BrowserAccessibilityManager is a child frame or guest frame,
250 // return the BrowserAccessibilityDelegate from the highest ancestor frame
251 // in the frame tree.
252 BrowserAccessibilityDelegate* GetDelegateFromRootManager();
254 // Get a snapshot of the current tree as an AXTreeUpdate.
255 ui::AXTreeUpdate SnapshotAXTreeForTesting();
257 protected:
258 BrowserAccessibilityManager(
259 BrowserAccessibilityDelegate* delegate,
260 BrowserAccessibilityFactory* factory);
262 BrowserAccessibilityManager(
263 const ui::AXTreeUpdate& initial_tree,
264 BrowserAccessibilityDelegate* delegate,
265 BrowserAccessibilityFactory* factory);
267 // Called at the end of updating the tree.
268 virtual void OnTreeUpdateFinished() {}
270 private:
271 // The following states keep track of whether or not the
272 // on-screen keyboard is allowed to be shown.
273 enum OnScreenKeyboardState {
274 // Never show the on-screen keyboard because this tab is hidden.
275 OSK_DISALLOWED_BECAUSE_TAB_HIDDEN,
277 // This tab was just shown, so don't pop-up the on-screen keyboard if a
278 // text field gets focus that wasn't the result of an explicit touch.
279 OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED,
281 // A touch event has occurred within the window, but focus has not
282 // explicitly changed. Allow the on-screen keyboard to be shown if the
283 // touch event was within the bounds of the currently focused object.
284 // Otherwise we'll just wait to see if focus changes.
285 OSK_ALLOWED_WITHIN_FOCUSED_OBJECT,
287 // Focus has changed within a tab that's already visible. Allow the
288 // on-screen keyboard to show anytime that a touch event leads to an
289 // editable text control getting focus.
290 OSK_ALLOWED
293 protected:
294 // The object that can perform actions on our behalf.
295 BrowserAccessibilityDelegate* delegate_;
297 // Factory to create BrowserAccessibility objects (for dependency injection).
298 scoped_ptr<BrowserAccessibilityFactory> factory_;
300 // The underlying tree of accessibility objects.
301 scoped_ptr<ui::AXSerializableTree> tree_;
303 // The node that currently has focus.
304 ui::AXNode* focus_;
306 // A mapping from a node id to its wrapper of type BrowserAccessibility.
307 base::hash_map<int32, BrowserAccessibility*> id_wrapper_map_;
309 // The on-screen keyboard state.
310 OnScreenKeyboardState osk_state_;
312 BrowserAccessibilityFindInPageInfo find_in_page_info_;
314 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManager);
317 } // namespace content
319 #endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_