blob: 33eb60d40ca9f63be8368e8bfe9815aa4434a302 [file] [log] [blame]
[email protected]eaf8a3422012-01-24 23:35:311// Copyright (c) 2012 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
[email protected]f27e81c2010-10-07 05:20:238#include <map>
9#include <utility>
10#include <vector>
11
[email protected]c5c2a672010-10-01 23:28:0412#include "base/basictypes.h"
[email protected]eecf89f02013-08-20 23:41:5113#include "base/strings/string16.h"
[email protected]f27e81c2010-10-07 05:20:2314#include "build/build_config.h"
[email protected]0a935a02012-06-12 22:55:1515#include "content/common/accessibility_node_data.h"
[email protected]f3112a52011-09-30 23:47:4916#include "content/common/content_export.h"
[email protected]f27e81c2010-10-07 05:20:2317
[email protected]95b3f5442012-05-06 17:10:0718#if defined(OS_MACOSX) && __OBJC__
19@class BrowserAccessibilityCocoa;
[email protected]e6b34872012-10-24 20:51:3220#endif
21
22namespace content {
23class BrowserAccessibilityManager;
24#if defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2325class BrowserAccessibilityWin;
[email protected]cd3e2d902012-05-09 07:05:2026#elif defined(TOOLKIT_GTK)
27class BrowserAccessibilityGtk;
[email protected]f27e81c2010-10-07 05:20:2328#endif
29
[email protected]c5c2a672010-10-01 23:28:0430////////////////////////////////////////////////////////////////////////////////
31//
32// BrowserAccessibility
33//
34// Class implementing the cross platform interface for the Browser-Renderer
35// communication of accessibility information, providing accessibility
36// to be used by screen readers and other assistive technology (AT).
37//
38// An implementation for each platform handles platform specific accessibility
39// APIs.
40//
41////////////////////////////////////////////////////////////////////////////////
[email protected]f3112a52011-09-30 23:47:4942class CONTENT_EXPORT BrowserAccessibility {
[email protected]c5c2a672010-10-01 23:28:0443 public:
[email protected]f27e81c2010-10-07 05:20:2344 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0445 // caller.
[email protected]f27e81c2010-10-07 05:20:2346 static BrowserAccessibility* Create();
47
[email protected]c5c2a672010-10-01 23:28:0448 virtual ~BrowserAccessibility();
49
[email protected]b05cd542011-06-08 01:38:0250 // Detach all descendants of this subtree and push all of the node pointers,
51 // including this node, onto the end of |nodes|.
52 virtual void DetachTree(std::vector<BrowserAccessibility*>* nodes);
53
[email protected]c477540962013-03-07 00:43:1054 // Perform platform-specific initialization. This can be called multiple times
[email protected]f27e81c2010-10-07 05:20:2355 // during the lifetime of this instance after the members of this base object
56 // have been reset with new values from the renderer process.
[email protected]a52aa412011-12-14 19:03:3057 // Child dependent initialization can be done here.
58 virtual void PostInitialize() {}
[email protected]ee845122011-09-01 08:44:1659
[email protected]cd3e2d902012-05-09 07:05:2060 // Returns true if this is a native platform-specific object, vs a
61 // cross-platform generic object.
62 virtual bool IsNative() const;
63
[email protected]c477540962013-03-07 00:43:1064 // Initialize the tree structure of this object.
65 void InitializeTreeStructure(
66 BrowserAccessibilityManager* manager,
[email protected]a52aa412011-12-14 19:03:3067 BrowserAccessibility* parent,
[email protected]c477540962013-03-07 00:43:1068 int32 renderer_id,
69 int32 index_in_parent);
70
71 // Initialize this object's data.
72 void InitializeData(const AccessibilityNodeData& src);
[email protected]f27e81c2010-10-07 05:20:2373
[email protected]26aef302013-03-14 22:43:5174 virtual void SwapChildren(std::vector<BrowserAccessibility*>& children);
[email protected]c477540962013-03-07 00:43:1075
[email protected]8368e3c2011-03-08 19:26:2476 // Update the parent and index in parent if this node has been moved.
77 void UpdateParent(BrowserAccessibility* parent, int index_in_parent);
78
[email protected]a10a5f82013-05-06 05:24:5679 // Update this node's location, leaving everything else the same.
80 virtual void SetLocation(const gfx::Rect& new_location);
81
[email protected]f27e81c2010-10-07 05:20:2382 // Return true if this object is equal to or a descendant of |ancestor|.
83 bool IsDescendantOf(BrowserAccessibility* ancestor);
84
85 // Returns the parent of this object, or NULL if it's the root.
[email protected]7f2d9f6b2013-06-13 22:57:0186 BrowserAccessibility* parent() const { return parent_; }
[email protected]f27e81c2010-10-07 05:20:2387
88 // Returns the number of children of this object.
[email protected]9c1a75a2011-03-10 02:38:1289 uint32 child_count() const { return children_.size(); }
[email protected]f27e81c2010-10-07 05:20:2390
91 // Return a pointer to the child with the given index.
[email protected]7f2d9f6b2013-06-13 22:57:0192 BrowserAccessibility* GetChild(uint32 child_index) const;
[email protected]f27e81c2010-10-07 05:20:2393
94 // Return the previous sibling of this object, or NULL if it's the first
95 // child of its parent.
96 BrowserAccessibility* GetPreviousSibling();
97
98 // Return the next sibling of this object, or NULL if it's the last child
99 // of its parent.
100 BrowserAccessibility* GetNextSibling();
101
[email protected]9052cca2011-11-30 23:59:31102 // Returns the bounds of this object in coordinates relative to the
103 // top-left corner of the overall web area.
[email protected]7f2d9f6b2013-06-13 22:57:01104 gfx::Rect GetLocalBoundsRect() const;
[email protected]02747d4e2010-11-03 19:10:00105
[email protected]9052cca2011-11-30 23:59:31106 // Returns the bounds of this object in screen coordinates.
[email protected]7f2d9f6b2013-06-13 22:57:01107 gfx::Rect GetGlobalBoundsRect() const;
[email protected]9052cca2011-11-30 23:59:31108
109 // Returns the deepest descendant that contains the specified point
110 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00111 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
112
[email protected]c477540962013-03-07 00:43:10113 // Marks this object for deletion, releases our reference to it, and
114 // recursively calls Destroy() on its children. May not delete
115 // immediately due to reference counting.
[email protected]8368e3c2011-03-08 19:26:24116 //
[email protected]c477540962013-03-07 00:43:10117 // Reference counting is used on some platforms because the
[email protected]8368e3c2011-03-08 19:26:24118 // operating system may hold onto a reference to a BrowserAccessibility
[email protected]c477540962013-03-07 00:43:10119 // object even after we're through with it. When a BrowserAccessibility
120 // has had Destroy() called but its reference count is not yet zero,
121 // queries on this object return failure
122 virtual void Destroy();
[email protected]8368e3c2011-03-08 19:26:24123
124 // Subclasses should override this to support platform reference counting.
125 virtual void NativeAddReference() { }
126
127 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41128 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24129
130 //
[email protected]f27e81c2010-10-07 05:20:23131 // Accessors
[email protected]8368e3c2011-03-08 19:26:24132 //
133
[email protected]f27e81c2010-10-07 05:20:23134 const std::vector<BrowserAccessibility*>& children() const {
135 return children_;
136 }
[email protected]eecf89f02013-08-20 23:41:51137 const std::vector<std::pair<std::string, std::string> >&
138 html_attributes() const {
[email protected]0d7dad62010-10-19 21:18:50139 return html_attributes_;
140 }
[email protected]f27e81c2010-10-07 05:20:23141 int32 index_in_parent() const { return index_in_parent_; }
[email protected]0aed2f52011-03-23 18:06:36142 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50143 BrowserAccessibilityManager* manager() const { return manager_; }
[email protected]eecf89f02013-08-20 23:41:51144 const std::string& name() const { return name_; }
[email protected]0d7dad62010-10-19 21:18:50145 int32 renderer_id() const { return renderer_id_; }
146 int32 role() const { return role_; }
[email protected]0d7dad62010-10-19 21:18:50147 int32 state() const { return state_; }
[email protected]eecf89f02013-08-20 23:41:51148 const std::string& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24149 bool instance_active() const { return instance_active_; }
[email protected]f27e81c2010-10-07 05:20:23150
[email protected]95b3f5442012-05-06 17:10:07151#if defined(OS_MACOSX) && __OBJC__
[email protected]cd3e2d902012-05-09 07:05:20152 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10153#elif defined(OS_WIN)
[email protected]cd3e2d902012-05-09 07:05:20154 BrowserAccessibilityWin* ToBrowserAccessibilityWin();
155#elif defined(TOOLKIT_GTK)
156 BrowserAccessibilityGtk* ToBrowserAccessibilityGtk();
[email protected]f27e81c2010-10-07 05:20:23157#endif
158
[email protected]eecf89f02013-08-20 23:41:51159 // Accessing accessibility attributes:
160 //
161 // There are dozens of possible attributes for an accessibility node,
162 // but only a few tend to apply to any one object, so we store them
163 // in sparse arrays of <attribute id, attribute value> pairs, organized
164 // by type (bool, int, float, string, int list).
165 //
166 // There are three accessors for each type of attribute: one that returns
167 // true if the attribute is present and false if not, one that takes a
168 // pointer argument and returns true if the attribute is present (if you
169 // need to distinguish between the default value and a missing attribute),
170 // and another that returns the default value for that type if the
171 // attribute is not present. In addition, strings can be returned as
172 // either std::string or string16, for convenience.
[email protected]ee845122011-09-01 08:44:16173
[email protected]eecf89f02013-08-20 23:41:51174 bool HasBoolAttribute(AccessibilityNodeData::BoolAttribute attr) const;
175 bool GetBoolAttribute(AccessibilityNodeData::BoolAttribute attr) const;
176 bool GetBoolAttribute(AccessibilityNodeData::BoolAttribute attr,
177 bool* value) const;
178
179 bool HasFloatAttribute(AccessibilityNodeData::FloatAttribute attr) const;
180 float GetFloatAttribute(AccessibilityNodeData::FloatAttribute attr) const;
[email protected]e6b34872012-10-24 20:51:32181 bool GetFloatAttribute(AccessibilityNodeData::FloatAttribute attr,
[email protected]0a935a02012-06-12 22:55:15182 float* value) const;
[email protected]02747d4e2010-11-03 19:10:00183
[email protected]eecf89f02013-08-20 23:41:51184 bool HasIntAttribute(AccessibilityNodeData::IntAttribute attribute) const;
185 int GetIntAttribute(AccessibilityNodeData::IntAttribute attribute) const;
[email protected]e6b34872012-10-24 20:51:32186 bool GetIntAttribute(AccessibilityNodeData::IntAttribute attribute,
[email protected]0a935a02012-06-12 22:55:15187 int* value) const;
[email protected]ee845122011-09-01 08:44:16188
[email protected]eecf89f02013-08-20 23:41:51189 bool HasStringAttribute(
190 AccessibilityNodeData::StringAttribute attribute) const;
191 const std::string& GetStringAttribute(
192 AccessibilityNodeData::StringAttribute attribute) const;
193 bool GetStringAttribute(AccessibilityNodeData::StringAttribute attribute,
194 std::string* value) const;
195
196 bool GetString16Attribute(AccessibilityNodeData::StringAttribute attribute,
197 string16* value) const;
198 string16 GetString16Attribute(
199 AccessibilityNodeData::StringAttribute attribute) const;
200
201 bool HasIntListAttribute(
202 AccessibilityNodeData::IntListAttribute attribute) const;
203 const std::vector<int32>& GetIntListAttribute(
204 AccessibilityNodeData::IntListAttribute attribute) const;
205 bool GetIntListAttribute(AccessibilityNodeData::IntListAttribute attribute,
206 std::vector<int32>* value) const;
207
208 void SetStringAttribute(
209 AccessibilityNodeData::StringAttribute attribute,
210 const std::string& value);
[email protected]ee845122011-09-01 08:44:16211
212 // Retrieve the value of a html attribute from the attribute map and
213 // returns true if found.
214 bool GetHtmlAttribute(const char* attr, string16* value) const;
[email protected]eecf89f02013-08-20 23:41:51215 bool GetHtmlAttribute(const char* attr, std::string* value) const;
[email protected]ee845122011-09-01 08:44:16216
[email protected]38f1e3b052012-02-10 21:46:07217 // Utility method to handle special cases for ARIA booleans, tristates and
218 // booleans which have a "mixed" state.
219 //
220 // Warning: the term "Tristate" is used loosely by the spec and here,
221 // as some attributes support a 4th state.
222 //
223 // The following attributes are appropriate to use with this method:
224 // aria-selected (selectable)
225 // aria-grabbed (grabbable)
226 // aria-expanded (expandable)
227 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state
228 // aria-checked (checkable) -- supports 4th "mixed state"
229 bool GetAriaTristate(const char* attr_name,
230 bool* is_defined,
231 bool* is_mixed) const;
232
[email protected]c43b0dc2011-12-03 04:31:13233 // Returns true if the bit corresponding to the given state enum is 1.
[email protected]e6b34872012-10-24 20:51:32234 bool HasState(AccessibilityNodeData::State state_enum) const;
[email protected]c43b0dc2011-12-03 04:31:13235
[email protected]ee845122011-09-01 08:44:16236 // Returns true if this node is an editable text field of any kind.
237 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00238
[email protected]d1029c52011-10-21 21:59:51239 // Append the text from this node and its children.
[email protected]eecf89f02013-08-20 23:41:51240 std::string GetTextRecursive() const;
[email protected]d1029c52011-10-21 21:59:51241
[email protected]aa50cea82010-11-05 23:02:38242 protected:
[email protected]a52aa412011-12-14 19:03:30243 // Perform platform specific initialization. This can be called multiple times
244 // during the lifetime of this instance after the members of this base object
245 // have been reset with new values from the renderer process.
246 // Perform child independent initialization in this method.
[email protected]c477540962013-03-07 00:43:10247 virtual void PreInitialize() {}
[email protected]a52aa412011-12-14 19:03:30248
[email protected]aa50cea82010-11-05 23:02:38249 BrowserAccessibility();
250
[email protected]f27e81c2010-10-07 05:20:23251 // The manager of this tree of accessibility objects; needed for
252 // global operations like focus tracking.
253 BrowserAccessibilityManager* manager_;
254
255 // The parent of this object, may be NULL if we're the root object.
256 BrowserAccessibility* parent_;
257
[email protected]f27e81c2010-10-07 05:20:23258 // The index of this within its parent object.
259 int32 index_in_parent_;
260
261 // The ID of this object in the renderer process.
262 int32 renderer_id_;
263
264 // The children of this object.
265 std::vector<BrowserAccessibility*> children_;
266
267 // Accessibility metadata from the renderer
[email protected]eecf89f02013-08-20 23:41:51268 std::string name_;
269 std::string value_;
270 std::vector<std::pair<
271 AccessibilityNodeData::BoolAttribute, bool> > bool_attributes_;
272 std::vector<std::pair<
273 AccessibilityNodeData::FloatAttribute, float> > float_attributes_;
274 std::vector<std::pair<
275 AccessibilityNodeData::IntAttribute, int> > int_attributes_;
276 std::vector<std::pair<
277 AccessibilityNodeData::StringAttribute, std::string> > string_attributes_;
278 std::vector<std::pair<
279 AccessibilityNodeData::IntListAttribute, std::vector<int32> > >
280 intlist_attributes_;
281 std::vector<std::pair<std::string, std::string> > html_attributes_;
[email protected]f27e81c2010-10-07 05:20:23282 int32 role_;
283 int32 state_;
[email protected]0aed2f52011-03-23 18:06:36284 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24285
286 // BrowserAccessibility objects are reference-counted on some platforms.
287 // When we're done with this object and it's removed from our accessibility
288 // tree, a client may still be holding onto a pointer to this object, so
289 // we mark it as inactive so that calls to any of this object's methods
290 // immediately return failure.
291 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23292
[email protected]c5c2a672010-10-01 23:28:04293 private:
294 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
295};
296
[email protected]e6b34872012-10-24 20:51:32297} // namespace content
298
[email protected]ba1fa652011-06-25 05:16:22299#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_