blob: a52075128a519077ae5ef2a2e1e7a81bc1261e58 [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]10760e4a2013-09-04 23:32:2017#include "third_party/WebKit/public/web/WebAXEnums.h"
[email protected]f27e81c2010-10-07 05:20:2318
[email protected]95b3f5442012-05-06 17:10:0719#if defined(OS_MACOSX) && __OBJC__
20@class BrowserAccessibilityCocoa;
[email protected]e6b34872012-10-24 20:51:3221#endif
22
23namespace content {
24class BrowserAccessibilityManager;
25#if defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2326class BrowserAccessibilityWin;
[email protected]cd3e2d902012-05-09 07:05:2027#elif defined(TOOLKIT_GTK)
28class BrowserAccessibilityGtk;
[email protected]f27e81c2010-10-07 05:20:2329#endif
30
[email protected]c5c2a672010-10-01 23:28:0431////////////////////////////////////////////////////////////////////////////////
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]c477540962013-03-07 00:43:1055 // Perform platform-specific initialization. This can be called multiple times
[email protected]f27e81c2010-10-07 05:20:2356 // 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]cd3e2d902012-05-09 07:05:2061 // Returns true if this is a native platform-specific object, vs a
62 // cross-platform generic object.
63 virtual bool IsNative() const;
64
[email protected]c477540962013-03-07 00:43:1065 // Initialize the tree structure of this object.
66 void InitializeTreeStructure(
67 BrowserAccessibilityManager* manager,
[email protected]a52aa412011-12-14 19:03:3068 BrowserAccessibility* parent,
[email protected]c477540962013-03-07 00:43:1069 int32 renderer_id,
70 int32 index_in_parent);
71
72 // Initialize this object's data.
73 void InitializeData(const AccessibilityNodeData& src);
[email protected]f27e81c2010-10-07 05:20:2374
[email protected]26aef302013-03-14 22:43:5175 virtual void SwapChildren(std::vector<BrowserAccessibility*>& children);
[email protected]c477540962013-03-07 00:43:1076
[email protected]8368e3c2011-03-08 19:26:2477 // Update the parent and index in parent if this node has been moved.
78 void UpdateParent(BrowserAccessibility* parent, int index_in_parent);
79
[email protected]a10a5f82013-05-06 05:24:5680 // Update this node's location, leaving everything else the same.
81 virtual void SetLocation(const gfx::Rect& new_location);
82
[email protected]f27e81c2010-10-07 05:20:2383 // Return true if this object is equal to or a descendant of |ancestor|.
84 bool IsDescendantOf(BrowserAccessibility* ancestor);
85
86 // Returns the parent of this object, or NULL if it's the root.
[email protected]7f2d9f6b2013-06-13 22:57:0187 BrowserAccessibility* parent() const { return parent_; }
[email protected]f27e81c2010-10-07 05:20:2388
89 // Returns the number of children of this object.
[email protected]9c1a75a2011-03-10 02:38:1290 uint32 child_count() const { return children_.size(); }
[email protected]f27e81c2010-10-07 05:20:2391
92 // Return a pointer to the child with the given index.
[email protected]7f2d9f6b2013-06-13 22:57:0193 BrowserAccessibility* GetChild(uint32 child_index) const;
[email protected]f27e81c2010-10-07 05:20:2394
95 // Return the previous sibling of this object, or NULL if it's the first
96 // child of its parent.
97 BrowserAccessibility* GetPreviousSibling();
98
99 // Return the next sibling of this object, or NULL if it's the last child
100 // of its parent.
101 BrowserAccessibility* GetNextSibling();
102
[email protected]9052cca2011-11-30 23:59:31103 // Returns the bounds of this object in coordinates relative to the
104 // top-left corner of the overall web area.
[email protected]7f2d9f6b2013-06-13 22:57:01105 gfx::Rect GetLocalBoundsRect() const;
[email protected]02747d4e2010-11-03 19:10:00106
[email protected]9052cca2011-11-30 23:59:31107 // Returns the bounds of this object in screen coordinates.
[email protected]7f2d9f6b2013-06-13 22:57:01108 gfx::Rect GetGlobalBoundsRect() const;
[email protected]9052cca2011-11-30 23:59:31109
110 // Returns the deepest descendant that contains the specified point
111 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00112 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
113
[email protected]c477540962013-03-07 00:43:10114 // Marks this object for deletion, releases our reference to it, and
115 // recursively calls Destroy() on its children. May not delete
116 // immediately due to reference counting.
[email protected]8368e3c2011-03-08 19:26:24117 //
[email protected]c477540962013-03-07 00:43:10118 // Reference counting is used on some platforms because the
[email protected]8368e3c2011-03-08 19:26:24119 // operating system may hold onto a reference to a BrowserAccessibility
[email protected]c477540962013-03-07 00:43:10120 // object even after we're through with it. When a BrowserAccessibility
121 // has had Destroy() called but its reference count is not yet zero,
122 // queries on this object return failure
123 virtual void Destroy();
[email protected]8368e3c2011-03-08 19:26:24124
125 // Subclasses should override this to support platform reference counting.
126 virtual void NativeAddReference() { }
127
128 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41129 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24130
131 //
[email protected]f27e81c2010-10-07 05:20:23132 // Accessors
[email protected]8368e3c2011-03-08 19:26:24133 //
134
[email protected]f27e81c2010-10-07 05:20:23135 const std::vector<BrowserAccessibility*>& children() const {
136 return children_;
137 }
[email protected]eecf89f02013-08-20 23:41:51138 const std::vector<std::pair<std::string, std::string> >&
139 html_attributes() const {
[email protected]0d7dad62010-10-19 21:18:50140 return html_attributes_;
141 }
[email protected]f27e81c2010-10-07 05:20:23142 int32 index_in_parent() const { return index_in_parent_; }
[email protected]0aed2f52011-03-23 18:06:36143 gfx::Rect location() const { return location_; }
[email protected]0d7dad62010-10-19 21:18:50144 BrowserAccessibilityManager* manager() const { return manager_; }
[email protected]eecf89f02013-08-20 23:41:51145 const std::string& name() const { return name_; }
[email protected]0d7dad62010-10-19 21:18:50146 int32 renderer_id() const { return renderer_id_; }
147 int32 role() const { return role_; }
[email protected]0d7dad62010-10-19 21:18:50148 int32 state() const { return state_; }
[email protected]eecf89f02013-08-20 23:41:51149 const std::string& value() const { return value_; }
[email protected]8368e3c2011-03-08 19:26:24150 bool instance_active() const { return instance_active_; }
[email protected]f27e81c2010-10-07 05:20:23151
[email protected]95b3f5442012-05-06 17:10:07152#if defined(OS_MACOSX) && __OBJC__
[email protected]cd3e2d902012-05-09 07:05:20153 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10154#elif defined(OS_WIN)
[email protected]cd3e2d902012-05-09 07:05:20155 BrowserAccessibilityWin* ToBrowserAccessibilityWin();
156#elif defined(TOOLKIT_GTK)
157 BrowserAccessibilityGtk* ToBrowserAccessibilityGtk();
[email protected]f27e81c2010-10-07 05:20:23158#endif
159
[email protected]eecf89f02013-08-20 23:41:51160 // Accessing accessibility attributes:
161 //
162 // There are dozens of possible attributes for an accessibility node,
163 // but only a few tend to apply to any one object, so we store them
164 // in sparse arrays of <attribute id, attribute value> pairs, organized
165 // by type (bool, int, float, string, int list).
166 //
167 // There are three accessors for each type of attribute: one that returns
168 // true if the attribute is present and false if not, one that takes a
169 // pointer argument and returns true if the attribute is present (if you
170 // need to distinguish between the default value and a missing attribute),
171 // and another that returns the default value for that type if the
172 // attribute is not present. In addition, strings can be returned as
173 // either std::string or string16, for convenience.
[email protected]ee845122011-09-01 08:44:16174
[email protected]eecf89f02013-08-20 23:41:51175 bool HasBoolAttribute(AccessibilityNodeData::BoolAttribute attr) const;
176 bool GetBoolAttribute(AccessibilityNodeData::BoolAttribute attr) const;
177 bool GetBoolAttribute(AccessibilityNodeData::BoolAttribute attr,
178 bool* value) const;
179
180 bool HasFloatAttribute(AccessibilityNodeData::FloatAttribute attr) const;
181 float GetFloatAttribute(AccessibilityNodeData::FloatAttribute attr) const;
[email protected]e6b34872012-10-24 20:51:32182 bool GetFloatAttribute(AccessibilityNodeData::FloatAttribute attr,
[email protected]0a935a02012-06-12 22:55:15183 float* value) const;
[email protected]02747d4e2010-11-03 19:10:00184
[email protected]eecf89f02013-08-20 23:41:51185 bool HasIntAttribute(AccessibilityNodeData::IntAttribute attribute) const;
186 int GetIntAttribute(AccessibilityNodeData::IntAttribute attribute) const;
[email protected]e6b34872012-10-24 20:51:32187 bool GetIntAttribute(AccessibilityNodeData::IntAttribute attribute,
[email protected]0a935a02012-06-12 22:55:15188 int* value) const;
[email protected]ee845122011-09-01 08:44:16189
[email protected]eecf89f02013-08-20 23:41:51190 bool HasStringAttribute(
191 AccessibilityNodeData::StringAttribute attribute) const;
192 const std::string& GetStringAttribute(
193 AccessibilityNodeData::StringAttribute attribute) const;
194 bool GetStringAttribute(AccessibilityNodeData::StringAttribute attribute,
195 std::string* value) const;
196
197 bool GetString16Attribute(AccessibilityNodeData::StringAttribute attribute,
198 string16* value) const;
199 string16 GetString16Attribute(
200 AccessibilityNodeData::StringAttribute attribute) const;
201
202 bool HasIntListAttribute(
203 AccessibilityNodeData::IntListAttribute attribute) const;
204 const std::vector<int32>& GetIntListAttribute(
205 AccessibilityNodeData::IntListAttribute attribute) const;
206 bool GetIntListAttribute(AccessibilityNodeData::IntListAttribute attribute,
207 std::vector<int32>* value) const;
208
209 void SetStringAttribute(
210 AccessibilityNodeData::StringAttribute attribute,
211 const std::string& value);
[email protected]ee845122011-09-01 08:44:16212
213 // Retrieve the value of a html attribute from the attribute map and
214 // returns true if found.
215 bool GetHtmlAttribute(const char* attr, string16* value) const;
[email protected]eecf89f02013-08-20 23:41:51216 bool GetHtmlAttribute(const char* attr, std::string* value) const;
[email protected]ee845122011-09-01 08:44:16217
[email protected]38f1e3b052012-02-10 21:46:07218 // Utility method to handle special cases for ARIA booleans, tristates and
219 // booleans which have a "mixed" state.
220 //
221 // Warning: the term "Tristate" is used loosely by the spec and here,
222 // as some attributes support a 4th state.
223 //
224 // The following attributes are appropriate to use with this method:
225 // aria-selected (selectable)
226 // aria-grabbed (grabbable)
227 // aria-expanded (expandable)
228 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state
229 // aria-checked (checkable) -- supports 4th "mixed state"
230 bool GetAriaTristate(const char* attr_name,
231 bool* is_defined,
232 bool* is_mixed) const;
233
[email protected]c43b0dc2011-12-03 04:31:13234 // Returns true if the bit corresponding to the given state enum is 1.
[email protected]10760e4a2013-09-04 23:32:20235 bool HasState(WebKit::WebAXState state_enum) const;
[email protected]c43b0dc2011-12-03 04:31:13236
[email protected]ee845122011-09-01 08:44:16237 // Returns true if this node is an editable text field of any kind.
238 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00239
[email protected]d1029c52011-10-21 21:59:51240 // Append the text from this node and its children.
[email protected]eecf89f02013-08-20 23:41:51241 std::string GetTextRecursive() const;
[email protected]d1029c52011-10-21 21:59:51242
[email protected]aa50cea82010-11-05 23:02:38243 protected:
[email protected]a52aa412011-12-14 19:03:30244 // Perform platform specific initialization. This can be called multiple times
245 // during the lifetime of this instance after the members of this base object
246 // have been reset with new values from the renderer process.
247 // Perform child independent initialization in this method.
[email protected]c477540962013-03-07 00:43:10248 virtual void PreInitialize() {}
[email protected]a52aa412011-12-14 19:03:30249
[email protected]aa50cea82010-11-05 23:02:38250 BrowserAccessibility();
251
[email protected]f27e81c2010-10-07 05:20:23252 // The manager of this tree of accessibility objects; needed for
253 // global operations like focus tracking.
254 BrowserAccessibilityManager* manager_;
255
256 // The parent of this object, may be NULL if we're the root object.
257 BrowserAccessibility* parent_;
258
[email protected]f27e81c2010-10-07 05:20:23259 // The index of this within its parent object.
260 int32 index_in_parent_;
261
262 // The ID of this object in the renderer process.
263 int32 renderer_id_;
264
265 // The children of this object.
266 std::vector<BrowserAccessibility*> children_;
267
268 // Accessibility metadata from the renderer
[email protected]eecf89f02013-08-20 23:41:51269 std::string name_;
270 std::string value_;
271 std::vector<std::pair<
272 AccessibilityNodeData::BoolAttribute, bool> > bool_attributes_;
273 std::vector<std::pair<
274 AccessibilityNodeData::FloatAttribute, float> > float_attributes_;
275 std::vector<std::pair<
276 AccessibilityNodeData::IntAttribute, int> > int_attributes_;
277 std::vector<std::pair<
278 AccessibilityNodeData::StringAttribute, std::string> > string_attributes_;
279 std::vector<std::pair<
280 AccessibilityNodeData::IntListAttribute, std::vector<int32> > >
281 intlist_attributes_;
282 std::vector<std::pair<std::string, std::string> > html_attributes_;
[email protected]f27e81c2010-10-07 05:20:23283 int32 role_;
284 int32 state_;
[email protected]0aed2f52011-03-23 18:06:36285 gfx::Rect location_;
[email protected]8368e3c2011-03-08 19:26:24286
287 // BrowserAccessibility objects are reference-counted on some platforms.
288 // When we're done with this object and it's removed from our accessibility
289 // tree, a client may still be holding onto a pointer to this object, so
290 // we mark it as inactive so that calls to any of this object's methods
291 // immediately return failure.
292 bool instance_active_;
[email protected]f27e81c2010-10-07 05:20:23293
[email protected]c5c2a672010-10-01 23:28:04294 private:
295 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
296};
297
[email protected]e6b34872012-10-24 20:51:32298} // namespace content
299
[email protected]ba1fa652011-06-25 05:16:22300#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_