blob: eeb940e793bdd8021a305dcfc5d4cda15d65ac83 [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]f3112a52011-09-30 23:47:4915#include "content/common/content_export.h"
[email protected]10760e4a2013-09-04 23:32:2016#include "third_party/WebKit/public/web/WebAXEnums.h"
[email protected]d96f3842014-04-21 18:07:2917#include "ui/accessibility/ax_node.h"
[email protected]5eec2f52014-01-06 22:30:5418#include "ui/accessibility/ax_node_data.h"
[email protected]f27e81c2010-10-07 05:20:2319
[email protected]95b3f5442012-05-06 17:10:0720#if defined(OS_MACOSX) && __OBJC__
21@class BrowserAccessibilityCocoa;
[email protected]e6b34872012-10-24 20:51:3222#endif
23
24namespace content {
25class BrowserAccessibilityManager;
26#if defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2327class BrowserAccessibilityWin;
28#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]d96f3842014-04-21 18:07:2950 // Called only once, immediately after construction. The constructor doesn't
51 // take any arguments because in the Windows subclass we use a special
52 // function to construct a COM object.
53 virtual void Init(BrowserAccessibilityManager* manager, ui::AXNode* node);
[email protected]b05cd542011-06-08 01:38:0254
[email protected]d96f3842014-04-21 18:07:2955 // Called after the object is first initialized and again every time
56 // its data changes.
57 virtual void OnDataChanged();
58
59 // Called after an atomic update to the tree finished and this object
60 // was created or changed in this update.
61 virtual void OnUpdateFinished() {}
[email protected]ee845122011-09-01 08:44:1662
[email protected]cd3e2d902012-05-09 07:05:2063 // Returns true if this is a native platform-specific object, vs a
64 // cross-platform generic object.
65 virtual bool IsNative() const;
66
[email protected]d96f3842014-04-21 18:07:2967 // Called when the location changed.
dmazzoni487bac02014-08-27 20:54:1168 virtual void OnLocationChanged() {}
[email protected]a10a5f82013-05-06 05:24:5669
[email protected]f27e81c2010-10-07 05:20:2370 // Return true if this object is equal to or a descendant of |ancestor|.
71 bool IsDescendantOf(BrowserAccessibility* ancestor);
72
[email protected]933af8c2013-11-01 17:59:4073 // Returns true if this is a leaf node on this platform, meaning any
74 // children should not be exposed to this platform's native accessibility
75 // layer. Each platform subclass should implement this itself.
76 // The definition of a leaf may vary depending on the platform,
77 // but a leaf node should never have children that are focusable or
78 // that might send notifications.
79 virtual bool PlatformIsLeaf() const;
80
81 // Returns the number of children of this object, or 0 if PlatformIsLeaf()
82 // returns true.
83 uint32 PlatformChildCount() const;
84
85 // Return a pointer to the child at the given index, or NULL for an
86 // invalid index. Returns NULL if PlatformIsLeaf() returns true.
87 BrowserAccessibility* PlatformGetChild(uint32 child_index) const;
[email protected]f27e81c2010-10-07 05:20:2388
89 // Return the previous sibling of this object, or NULL if it's the first
90 // child of its parent.
91 BrowserAccessibility* GetPreviousSibling();
92
93 // Return the next sibling of this object, or NULL if it's the last child
94 // of its parent.
95 BrowserAccessibility* GetNextSibling();
96
[email protected]9052cca2011-11-30 23:59:3197 // Returns the bounds of this object in coordinates relative to the
98 // top-left corner of the overall web area.
[email protected]7f2d9f6b2013-06-13 22:57:0199 gfx::Rect GetLocalBoundsRect() const;
[email protected]02747d4e2010-11-03 19:10:00100
[email protected]9052cca2011-11-30 23:59:31101 // Returns the bounds of this object in screen coordinates.
[email protected]7f2d9f6b2013-06-13 22:57:01102 gfx::Rect GetGlobalBoundsRect() const;
[email protected]9052cca2011-11-30 23:59:31103
[email protected]516c16a2013-11-04 21:32:20104 // Returns the bounds of the given range in coordinates relative to the
105 // top-left corner of the overall web area. Only valid when the
106 // role is WebAXRoleStaticText.
107 gfx::Rect GetLocalBoundsForRange(int start, int len) const;
108
109 // Same as GetLocalBoundsForRange, in screen coordinates. Only valid when
110 // the role is WebAXRoleStaticText.
111 gfx::Rect GetGlobalBoundsForRange(int start, int len) const;
112
[email protected]9052cca2011-11-30 23:59:31113 // Returns the deepest descendant that contains the specified point
114 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00115 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
116
[email protected]c477540962013-03-07 00:43:10117 // Marks this object for deletion, releases our reference to it, and
[email protected]d96f3842014-04-21 18:07:29118 // nulls out the pointer to the underlying AXNode. May not delete
119 // the object immediately due to reference counting.
[email protected]8368e3c2011-03-08 19:26:24120 //
[email protected]c477540962013-03-07 00:43:10121 // Reference counting is used on some platforms because the
[email protected]8368e3c2011-03-08 19:26:24122 // operating system may hold onto a reference to a BrowserAccessibility
[email protected]c477540962013-03-07 00:43:10123 // object even after we're through with it. When a BrowserAccessibility
124 // has had Destroy() called but its reference count is not yet zero,
[email protected]d96f3842014-04-21 18:07:29125 // instance_active() returns false and queries on this object return failure.
[email protected]c477540962013-03-07 00:43:10126 virtual void Destroy();
[email protected]8368e3c2011-03-08 19:26:24127
128 // Subclasses should override this to support platform reference counting.
129 virtual void NativeAddReference() { }
130
131 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41132 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24133
134 //
[email protected]f27e81c2010-10-07 05:20:23135 // Accessors
[email protected]8368e3c2011-03-08 19:26:24136 //
137
[email protected]0d7dad62010-10-19 21:18:50138 BrowserAccessibilityManager* manager() const { return manager_; }
[email protected]d96f3842014-04-21 18:07:29139 bool instance_active() const { return node_ != NULL; }
140 ui::AXNode* node() const { return node_; }
[email protected]eecf89f02013-08-20 23:41:51141 const std::string& name() const { return name_; }
[email protected]99d408f2013-12-04 07:19:43142 const std::string& value() const { return value_; }
[email protected]99d408f2013-12-04 07:19:43143 void set_name(const std::string& name) { name_ = name; }
144 void set_value(const std::string& value) { value_ = value; }
145
[email protected]c1f9be742014-04-10 13:15:46146 // These access the internal accessibility tree, which doesn't necessarily
147 // reflect the accessibility tree that should be exposed on each platform.
148 // Use PlatformChildCount and PlatformGetChild to implement platform
149 // accessibility APIs.
[email protected]d96f3842014-04-21 18:07:29150 uint32 InternalChildCount() const;
151 BrowserAccessibility* InternalGetChild(uint32 child_index) const;
[email protected]c1f9be742014-04-10 13:15:46152
[email protected]d96f3842014-04-21 18:07:29153 BrowserAccessibility* GetParent() const;
154 int32 GetIndexInParent() const;
[email protected]c1f9be742014-04-10 13:15:46155
[email protected]d96f3842014-04-21 18:07:29156 int32 GetId() const;
157 const ui::AXNodeData& GetData() const;
158 gfx::Rect GetLocation() const;
159 int32 GetRole() const;
160 int32 GetState() const;
161
162 typedef std::vector<std::pair<std::string, std::string> > HtmlAttributes;
163 const HtmlAttributes& GetHtmlAttributes() const;
[email protected]c1f9be742014-04-10 13:15:46164
[email protected]95b3f5442012-05-06 17:10:07165#if defined(OS_MACOSX) && __OBJC__
[email protected]cd3e2d902012-05-09 07:05:20166 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10167#elif defined(OS_WIN)
[email protected]cd3e2d902012-05-09 07:05:20168 BrowserAccessibilityWin* ToBrowserAccessibilityWin();
[email protected]f27e81c2010-10-07 05:20:23169#endif
170
[email protected]eecf89f02013-08-20 23:41:51171 // Accessing accessibility attributes:
172 //
173 // There are dozens of possible attributes for an accessibility node,
174 // but only a few tend to apply to any one object, so we store them
175 // in sparse arrays of <attribute id, attribute value> pairs, organized
176 // by type (bool, int, float, string, int list).
177 //
178 // There are three accessors for each type of attribute: one that returns
179 // true if the attribute is present and false if not, one that takes a
180 // pointer argument and returns true if the attribute is present (if you
181 // need to distinguish between the default value and a missing attribute),
182 // and another that returns the default value for that type if the
183 // attribute is not present. In addition, strings can be returned as
[email protected]fcf75d42013-12-03 20:11:26184 // either std::string or base::string16, for convenience.
[email protected]ee845122011-09-01 08:44:16185
[email protected]5eec2f52014-01-06 22:30:54186 bool HasBoolAttribute(ui::AXBoolAttribute attr) const;
187 bool GetBoolAttribute(ui::AXBoolAttribute attr) const;
188 bool GetBoolAttribute(ui::AXBoolAttribute attr, bool* value) const;
[email protected]eecf89f02013-08-20 23:41:51189
[email protected]5eec2f52014-01-06 22:30:54190 bool HasFloatAttribute(ui::AXFloatAttribute attr) const;
191 float GetFloatAttribute(ui::AXFloatAttribute attr) const;
192 bool GetFloatAttribute(ui::AXFloatAttribute attr, float* value) const;
[email protected]02747d4e2010-11-03 19:10:00193
[email protected]5eec2f52014-01-06 22:30:54194 bool HasIntAttribute(ui::AXIntAttribute attribute) const;
195 int GetIntAttribute(ui::AXIntAttribute attribute) const;
196 bool GetIntAttribute(ui::AXIntAttribute attribute, int* value) const;
[email protected]ee845122011-09-01 08:44:16197
[email protected]eecf89f02013-08-20 23:41:51198 bool HasStringAttribute(
[email protected]5eec2f52014-01-06 22:30:54199 ui::AXStringAttribute attribute) const;
200 const std::string& GetStringAttribute(ui::AXStringAttribute attribute) const;
201 bool GetStringAttribute(ui::AXStringAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51202 std::string* value) const;
203
[email protected]5eec2f52014-01-06 22:30:54204 bool GetString16Attribute(ui::AXStringAttribute attribute,
[email protected]fcf75d42013-12-03 20:11:26205 base::string16* value) const;
206 base::string16 GetString16Attribute(
[email protected]5eec2f52014-01-06 22:30:54207 ui::AXStringAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51208
[email protected]5eec2f52014-01-06 22:30:54209 bool HasIntListAttribute(ui::AXIntListAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51210 const std::vector<int32>& GetIntListAttribute(
[email protected]5eec2f52014-01-06 22:30:54211 ui::AXIntListAttribute attribute) const;
212 bool GetIntListAttribute(ui::AXIntListAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51213 std::vector<int32>* value) const;
214
[email protected]5eec2f52014-01-06 22:30:54215 void SetStringAttribute(ui::AXStringAttribute attribute,
216 const std::string& value);
[email protected]ee845122011-09-01 08:44:16217
218 // Retrieve the value of a html attribute from the attribute map and
219 // returns true if found.
[email protected]fcf75d42013-12-03 20:11:26220 bool GetHtmlAttribute(const char* attr, base::string16* value) const;
[email protected]eecf89f02013-08-20 23:41:51221 bool GetHtmlAttribute(const char* attr, std::string* value) const;
[email protected]ee845122011-09-01 08:44:16222
[email protected]38f1e3b052012-02-10 21:46:07223 // Utility method to handle special cases for ARIA booleans, tristates and
224 // booleans which have a "mixed" state.
225 //
226 // Warning: the term "Tristate" is used loosely by the spec and here,
227 // as some attributes support a 4th state.
228 //
229 // The following attributes are appropriate to use with this method:
230 // aria-selected (selectable)
231 // aria-grabbed (grabbable)
232 // aria-expanded (expandable)
233 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state
234 // aria-checked (checkable) -- supports 4th "mixed state"
235 bool GetAriaTristate(const char* attr_name,
236 bool* is_defined,
237 bool* is_mixed) const;
238
[email protected]c43b0dc2011-12-03 04:31:13239 // Returns true if the bit corresponding to the given state enum is 1.
[email protected]5eec2f52014-01-06 22:30:54240 bool HasState(ui::AXState state_enum) const;
[email protected]c43b0dc2011-12-03 04:31:13241
[email protected]ee845122011-09-01 08:44:16242 // Returns true if this node is an editable text field of any kind.
243 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00244
[email protected]d1029c52011-10-21 21:59:51245 // Append the text from this node and its children.
[email protected]eecf89f02013-08-20 23:41:51246 std::string GetTextRecursive() const;
[email protected]d1029c52011-10-21 21:59:51247
[email protected]aa50cea82010-11-05 23:02:38248 protected:
249 BrowserAccessibility();
250
[email protected]d96f3842014-04-21 18:07:29251 // The manager of this tree of accessibility objects.
[email protected]f27e81c2010-10-07 05:20:23252 BrowserAccessibilityManager* manager_;
253
[email protected]d96f3842014-04-21 18:07:29254 // The underlying node.
255 ui::AXNode* node_;
[email protected]f27e81c2010-10-07 05:20:23256
[email protected]99d408f2013-12-04 07:19:43257 private:
[email protected]3dba3ed2013-12-17 21:24:01258 // Return the sum of the lengths of all static text descendants,
259 // including this object if it's static text.
260 int GetStaticTextLenRecursive() const;
261
[email protected]eecf89f02013-08-20 23:41:51262 std::string name_;
263 std::string value_;
[email protected]f27e81c2010-10-07 05:20:23264
[email protected]c5c2a672010-10-01 23:28:04265 private:
266 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
267};
268
[email protected]e6b34872012-10-24 20:51:32269} // namespace content
270
[email protected]ba1fa652011-06-25 05:16:22271#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_