blob: 10276e279d375774c9ea17742b57be3f4cabad1d [file] [log] [blame]
[email protected]9c1a75a2011-03-10 02:38:121// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c5c2a672010-10-01 23:28:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ba1fa652011-06-25 05:16:225#ifndef CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
6#define CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_
[email protected]c5c2a672010-10-01 23:28:047#pragma once
8
[email protected]f27e81c2010-10-07 05:20:239#include <map>
10#include <utility>
11#include <vector>
12
[email protected]c5c2a672010-10-01 23:28:0413#include "base/basictypes.h"
[email protected]f27e81c2010-10-07 05:20:2314#include "build/build_config.h"
[email protected]f3112a52011-09-30 23:47:4915#include "content/common/content_export.h"
[email protected]f27e81c2010-10-07 05:20:2316#include "webkit/glue/webaccessibility.h"
17
18class BrowserAccessibilityManager;
[email protected]0d7dad62010-10-19 21:18:5019#if defined(OS_MACOSX) && __OBJC__
20@class BrowserAccessibilityCocoa;
[email protected]1dbadbd2010-10-13 18:50:1021#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2322class BrowserAccessibilityWin;
23#endif
24
25using webkit_glue::WebAccessibility;
[email protected]ee845122011-09-01 08:44:1626typedef std::map<WebAccessibility::BoolAttribute, bool> BoolAttrMap;
27typedef std::map<WebAccessibility::FloatAttribute, float> FloatAttrMap;
28typedef std::map<WebAccessibility::IntAttribute, int> IntAttrMap;
29typedef std::map<WebAccessibility::StringAttribute, string16> StringAttrMap;
[email protected]c5c2a672010-10-01 23:28:0430
31////////////////////////////////////////////////////////////////////////////////
32//
33// BrowserAccessibility
34//
35// Class implementing the cross platform interface for the Browser-Renderer
36// communication of accessibility information, providing accessibility
37// to be used by screen readers and other assistive technology (AT).
38//
39// An implementation for each platform handles platform specific accessibility
40// APIs.
41//
42////////////////////////////////////////////////////////////////////////////////
[email protected]f3112a52011-09-30 23:47:4943class CONTENT_EXPORT BrowserAccessibility {
[email protected]c5c2a672010-10-01 23:28:0444 public:
[email protected]f27e81c2010-10-07 05:20:2345 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0446 // caller.
[email protected]f27e81c2010-10-07 05:20:2347 static BrowserAccessibility* Create();
48
[email protected]c5c2a672010-10-01 23:28:0449 virtual ~BrowserAccessibility();
50
[email protected]b05cd542011-06-08 01:38:0251 // Detach all descendants of this subtree and push all of the node pointers,
52 // including this node, onto the end of |nodes|.
53 virtual void DetachTree(std::vector<BrowserAccessibility*>* nodes);
54
[email protected]f27e81c2010-10-07 05:20:2355 // Perform platform specific initialization. This can be called multiple times
56 // during the lifetime of this instance after the members of this base object
57 // have been reset with new values from the renderer process.
[email protected]a52aa412011-12-14 19:03:3058 // Child dependent initialization can be done here.
59 virtual void PostInitialize() {}
[email protected]ee845122011-09-01 08:44:1660
[email protected]8368e3c2011-03-08 19:26:2461 // Initialize this object, reading attributes from |src|. Does not
62 // recurse into children of |src| and build the whole subtree.
[email protected]a52aa412011-12-14 19:03:3063 void PreInitialize(BrowserAccessibilityManager* manager,
64 BrowserAccessibility* parent,
65 int32 child_id,
66 int32 index_in_parent,
67 const WebAccessibility& src);
[email protected]f27e81c2010-10-07 05:20:2368
69 // Add a child of this object.
70 void AddChild(BrowserAccessibility* child);
71
[email protected]8368e3c2011-03-08 19:26:2472 // Update the parent and index in parent if this node has been moved.
73 void UpdateParent(BrowserAccessibility* parent, int index_in_parent);
74
[email protected]f27e81c2010-10-07 05:20:2375 // Return true if this object is equal to or a descendant of |ancestor|.
76 bool IsDescendantOf(BrowserAccessibility* ancestor);
77
78 // Returns the parent of this object, or NULL if it's the root.
[email protected]9c1a75a2011-03-10 02:38:1279 BrowserAccessibility* parent() { return parent_; }
[email protected]f27e81c2010-10-07 05:20:2380
81 // Returns the number of children of this object.
[email protected]9c1a75a2011-03-10 02:38:1282 uint32 child_count() const { return children_.size(); }
[email protected]f27e81c2010-10-07 05:20:2383
84 // Return a pointer to the child with the given index.
85 BrowserAccessibility* GetChild(uint32 child_index);
86
87 // Return the previous sibling of this object, or NULL if it's the first
88 // child of its parent.
89 BrowserAccessibility* GetPreviousSibling();
90
91 // Return the next sibling of this object, or NULL if it's the last child
92 // of its parent.
93 BrowserAccessibility* GetNextSibling();
94
[email protected]9052cca2011-11-30 23:59:3195 // Returns the bounds of this object in coordinates relative to the
96 // top-left corner of the overall web area.
97 gfx::Rect GetLocalBoundsRect();
[email protected]02747d4e2010-11-03 19:10:0098
[email protected]9052cca2011-11-30 23:59:3199 // Returns the bounds of this object in screen coordinates.
100 gfx::Rect GetGlobalBoundsRect();
101
102 // Returns the deepest descendant that contains the specified point
103 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00104 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
105
[email protected]72f3f552011-12-03 07:36:33106 // Scrolls this element so that the |focus| rect (in local coordinates
107 // relative to this element) is scrolled to fit within the given
108 // |viewport| (which is clipped to the actual bounds of this
109 // object if you specify something larger). If the whole focus area
110 // doesn't fit, you can specify |subfocus|, which will prioritize
111 // a smaller area within |focus|.
112 //
113 // Note that "focus" doesn't necessarily mean the rectangle must correspond
114 // to a focused element on the page; assistive technology might request
115 // that any object be made visible.
116 void ScrollToMakeVisible(const gfx::Rect& subfocus,
117 const gfx::Rect& focus,
118 const gfx::Rect& viewport);
119
120 // This is a 1-dimensional scroll offset helper function that's applied
121 // separately in the horizontal and vertical directions, because the
122 // logic is the same. The goal is to compute the best scroll offset
123 // in order to make a focused item visible within a viewport.
124 //
125 // In case the whole focused item cannot fit, you can specify a
126 // subfocus - a smaller region within the focus that should
127 // be prioritized. If the whole focused item can fit, the subfocus is
128 // ignored.
129 //
130 // Example: the viewport is scrolled to the right just enough
131 // that the focus is in view.
132 // Before:
133 // +----------Viewport---------+
134 // +----Focus---+
135 // +--SubFocus--+
136 //
137 // After:
138 // +----------Viewport---------+
139 // +----Focus---+
140 // +--SubFocus--+
141 //
142 // When constraints cannot be fully satisfied, the min
143 // (left/top) position takes precedence over the max (right/bottom).
144 //
145 // Note that the return value represents the ideal new scroll offset.
146 // This may be out of range - the calling function should clip this
147 // to the available range.
148 static int ComputeBestScrollOffset(int current_scroll_offset,
149 int subfocus_min, int subfocus_max,
150 int focus_min, int focus_max,
151 int viewport_min, int viewport_max);
152
[email protected]8368e3c2011-03-08 19:26:24153 //
154 // Reference counting
155 //
156 // Each object has an internal reference count and many platform
157 // implementations may also use native reference counting.
158 //
159 // The internal reference counting is used because sometimes
160 // multiple references to the same object exist temporarily during
161 // an update. When the internal reference count reaches zero,
162 // NativeReleaseReference is called.
163 //
164 // Native reference counting is used on some platforms because the
165 // operating system may hold onto a reference to a BrowserAccessibility
166 // object even after we're through with it. On these platforms, when
167 // the internal reference count reaches zero, instance_active is set
168 // to zero, and all queries on this object should return failure.
169 // The object isn't actually deleted until the operating system releases
170 // all of its references.
171 //
172
173 // Increment this node's internal reference count.
174 virtual void InternalAddReference();
175
176 // Decrement this node's internal reference count. If the reference count
177 // reaches zero, call NativeReleaseReference().
178 virtual void InternalReleaseReference(bool recursive);
179
180 // Subclasses should override this to support platform reference counting.
181 virtual void NativeAddReference() { }
182
183 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41184 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24185
186 //
[email protected]f27e81c2010-10-07 05:20:23187 // Accessors
[email protected]8368e3c2011-03-08 19:26:24188 //
189
[email protected]ee845122011-09-01 08:44:16190 const BoolAttrMap& bool_attributes() const {
191 return bool_attributes_;
[email protected]1b12b232011-07-20 21:16:34192 }
193
[email protected]ee845122011-09-01 08:44:16194 const FloatAttrMap& float_attributes() const {
195 return float_attributes_;
196 }
197
198 const IntAttrMap& int_attributes() const {
[email protected]1b12b232011-07-20 21:16:34199 return int_attributes_;
200 }
201
[email protected]ee845122011-09-01 08:44:16202 const StringAttrMap& string_attributes() const {
203 return string_attributes_;
204 }
205
[email protected]f27e81c2010-10-07 05:20:23206 int32 child_id() const { return child_id_; }
207 const std::vector<BrowserAccessibility*>& children() const {
208 return children_;
209 }
[email protected]0d7dad62010-10-19 21:18:50210 const std::vector<std::pair<string16, string16> >& html_attributes() const {
211 return html_attributes_;
212 }
[email protected]f27e81c2010-10-07 05:20:23213 int32 index_in_parent() const { return index_in_parent_; }
[email protected]457af8d2011-05-14 01:02:47214 const std::vector<int32>& indirect_child_ids() const {
215 return indirect_child_ids_;
216 }
[email protected]80d647532011-06-17 22:53:49217 const std::vector<int32>& line_breaks() const {
218 return line_breaks_;
219 }
[email protected]1b12b232011-07-20 21:16:34220 const std::vector<int32>& cell_ids() const {
221 return cell_ids_;
222 }
[email protected]ee845122011-09-01 08:44:16223 const std::vector<int32>& unique_cell_ids() const {
224 return unique_cell_ids_;
225 }
[email protected]0aed2f52011-03-23 18:06:36226 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50227 BrowserAccessibilityManager* manager() const { return manager_; }
228 const string16& name() const { return name_; }
229 int32 renderer_id() const { return renderer_id_; }
230 int32 role() const { return role_; }
231 const string16& role_name() const { return role_name_; }
232 int32 state() const { return state_; }
233 const string16& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24234 bool instance_active() const { return instance_active_; }
235 int32 ref_count() const { return ref_count_; }
[email protected]f27e81c2010-10-07 05:20:23236
[email protected]0d7dad62010-10-19 21:18:50237#if defined(OS_MACOSX) && __OBJC__
238 BrowserAccessibilityCocoa* toBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10239#elif defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:23240 BrowserAccessibilityWin* toBrowserAccessibilityWin();
241#endif
242
[email protected]ee845122011-09-01 08:44:16243 // Retrieve the value of a bool attribute from the bool attribute
244 // map and returns true if found.
245 bool GetBoolAttribute(WebAccessibility::BoolAttribute attr, bool* value)
246 const;
247
248 // Retrieve the value of a float attribute from the float attribute
249 // map and returns true if found.
250 bool GetFloatAttribute(WebAccessibility::FloatAttribute attr, float* value)
251 const;
[email protected]02747d4e2010-11-03 19:10:00252
[email protected]1b12b232011-07-20 21:16:34253 // Retrieve the value of an integer attribute from the integer attribute
254 // map and returns true if found.
[email protected]ee845122011-09-01 08:44:16255 bool GetIntAttribute(WebAccessibility::IntAttribute attribute, int* value)
256 const;
257
258 // Retrieve the value of a string attribute from the attribute map and
259 // returns true if found.
260 bool GetStringAttribute(WebAccessibility::StringAttribute attribute,
261 string16* value) const;
262
263 // Retrieve the value of a html attribute from the attribute map and
264 // returns true if found.
265 bool GetHtmlAttribute(const char* attr, string16* value) const;
266
[email protected]c43b0dc2011-12-03 04:31:13267 // Returns true if the bit corresponding to the given state enum is 1.
268 bool HasState(WebAccessibility::State state_enum) const;
269
[email protected]ee845122011-09-01 08:44:16270 // Returns true if this node is an editable text field of any kind.
271 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00272
[email protected]d1029c52011-10-21 21:59:51273 // Append the text from this node and its children.
274 string16 GetTextRecursive() const;
275
[email protected]aa50cea82010-11-05 23:02:38276 protected:
[email protected]a52aa412011-12-14 19:03:30277 // Perform platform specific initialization. This can be called multiple times
278 // during the lifetime of this instance after the members of this base object
279 // have been reset with new values from the renderer process.
280 // Perform child independent initialization in this method.
281 virtual void PreInitialize();
282
[email protected]aa50cea82010-11-05 23:02:38283 BrowserAccessibility();
284
[email protected]f27e81c2010-10-07 05:20:23285 // The manager of this tree of accessibility objects; needed for
286 // global operations like focus tracking.
287 BrowserAccessibilityManager* manager_;
288
289 // The parent of this object, may be NULL if we're the root object.
290 BrowserAccessibility* parent_;
291
292 // The ID of this object; globally unique within the browser process.
293 int32 child_id_;
294
295 // The index of this within its parent object.
296 int32 index_in_parent_;
297
298 // The ID of this object in the renderer process.
299 int32 renderer_id_;
300
301 // The children of this object.
302 std::vector<BrowserAccessibility*> children_;
303
[email protected]8368e3c2011-03-08 19:26:24304 // The number of internal references to this object.
305 int32 ref_count_;
306
[email protected]f27e81c2010-10-07 05:20:23307 // Accessibility metadata from the renderer
308 string16 name_;
309 string16 value_;
[email protected]ee845122011-09-01 08:44:16310 BoolAttrMap bool_attributes_;
311 IntAttrMap int_attributes_;
312 FloatAttrMap float_attributes_;
313 StringAttrMap string_attributes_;
[email protected]f27e81c2010-10-07 05:20:23314 std::vector<std::pair<string16, string16> > html_attributes_;
315 int32 role_;
316 int32 state_;
317 string16 role_name_;
[email protected]0aed2f52011-03-23 18:06:36318 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24319 std::vector<int32> indirect_child_ids_;
[email protected]be4eeb0b2011-06-15 22:15:06320 std::vector<int32> line_breaks_;
[email protected]1b12b232011-07-20 21:16:34321 std::vector<int32> cell_ids_;
[email protected]ee845122011-09-01 08:44:16322 std::vector<int32> unique_cell_ids_;
[email protected]8368e3c2011-03-08 19:26:24323
324 // BrowserAccessibility objects are reference-counted on some platforms.
325 // When we're done with this object and it's removed from our accessibility
326 // tree, a client may still be holding onto a pointer to this object, so
327 // we mark it as inactive so that calls to any of this object's methods
328 // immediately return failure.
329 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23330
[email protected]c5c2a672010-10-01 23:28:04331 private:
332 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
333};
334
[email protected]ba1fa652011-06-25 05:16:22335#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_