blob: e399b8604f59bc6ffd97bb004b2d65ea0e85fdd4 [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"
mohan.reddy0079fba2014-09-25 07:25:2314#include "base/strings/string_split.h"
[email protected]f27e81c2010-10-07 05:20:2315#include "build/build_config.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]d96f3842014-04-21 18:07:2918#include "ui/accessibility/ax_node.h"
[email protected]5eec2f52014-01-06 22:30:5419#include "ui/accessibility/ax_node_data.h"
[email protected]f27e81c2010-10-07 05:20:2320
[email protected]95b3f5442012-05-06 17:10:0721#if defined(OS_MACOSX) && __OBJC__
22@class BrowserAccessibilityCocoa;
[email protected]e6b34872012-10-24 20:51:3223#endif
24
25namespace content {
26class BrowserAccessibilityManager;
27#if defined(OS_WIN)
[email protected]f27e81c2010-10-07 05:20:2328class BrowserAccessibilityWin;
29#endif
30
[email protected]c5c2a672010-10-01 23:28:0431////////////////////////////////////////////////////////////////////////////////
32//
33// BrowserAccessibility
34//
dmazzoni0b5d2482014-09-10 19:45:5735// A BrowserAccessibility object represents one node in the accessibility
36// tree on the browser side. It exactly corresponds to one WebAXObject from
37// Blink. It's owned by a BrowserAccessibilityManager.
[email protected]c5c2a672010-10-01 23:28:0438//
dmazzoni0b5d2482014-09-10 19:45:5739// There are subclasses of BrowserAccessibility for each platform where
40// we implement native accessibility APIs. This base class is used occasionally
41// for tests.
[email protected]c5c2a672010-10-01 23:28:0442//
43////////////////////////////////////////////////////////////////////////////////
[email protected]f3112a52011-09-30 23:47:4944class CONTENT_EXPORT BrowserAccessibility {
[email protected]c5c2a672010-10-01 23:28:0445 public:
[email protected]f27e81c2010-10-07 05:20:2346 // Creates a platform specific BrowserAccessibility. Ownership passes to the
[email protected]c5c2a672010-10-01 23:28:0447 // caller.
[email protected]f27e81c2010-10-07 05:20:2348 static BrowserAccessibility* Create();
49
[email protected]c5c2a672010-10-01 23:28:0450 virtual ~BrowserAccessibility();
51
[email protected]d96f3842014-04-21 18:07:2952 // Called only once, immediately after construction. The constructor doesn't
53 // take any arguments because in the Windows subclass we use a special
54 // function to construct a COM object.
55 virtual void Init(BrowserAccessibilityManager* manager, ui::AXNode* node);
[email protected]b05cd542011-06-08 01:38:0256
[email protected]d96f3842014-04-21 18:07:2957 // Called after the object is first initialized and again every time
58 // its data changes.
59 virtual void OnDataChanged();
60
61 // Called after an atomic update to the tree finished and this object
62 // was created or changed in this update.
63 virtual void OnUpdateFinished() {}
[email protected]ee845122011-09-01 08:44:1664
[email protected]cd3e2d902012-05-09 07:05:2065 // Returns true if this is a native platform-specific object, vs a
66 // cross-platform generic object.
67 virtual bool IsNative() const;
68
[email protected]d96f3842014-04-21 18:07:2969 // Called when the location changed.
dmazzoni487bac02014-08-27 20:54:1170 virtual void OnLocationChanged() {}
[email protected]a10a5f82013-05-06 05:24:5671
[email protected]f27e81c2010-10-07 05:20:2372 // Return true if this object is equal to or a descendant of |ancestor|.
73 bool IsDescendantOf(BrowserAccessibility* ancestor);
74
[email protected]933af8c2013-11-01 17:59:4075 // Returns true if this is a leaf node on this platform, meaning any
76 // children should not be exposed to this platform's native accessibility
77 // layer. Each platform subclass should implement this itself.
78 // The definition of a leaf may vary depending on the platform,
79 // but a leaf node should never have children that are focusable or
80 // that might send notifications.
81 virtual bool PlatformIsLeaf() const;
82
83 // Returns the number of children of this object, or 0 if PlatformIsLeaf()
84 // returns true.
85 uint32 PlatformChildCount() const;
86
87 // Return a pointer to the child at the given index, or NULL for an
88 // invalid index. Returns NULL if PlatformIsLeaf() returns true.
89 BrowserAccessibility* PlatformGetChild(uint32 child_index) const;
[email protected]f27e81c2010-10-07 05:20:2390
91 // Return the previous sibling of this object, or NULL if it's the first
92 // child of its parent.
93 BrowserAccessibility* GetPreviousSibling();
94
95 // Return the next sibling of this object, or NULL if it's the last child
96 // of its parent.
97 BrowserAccessibility* GetNextSibling();
98
[email protected]9052cca2011-11-30 23:59:3199 // Returns the bounds of this object in coordinates relative to the
100 // top-left corner of the overall web area.
[email protected]7f2d9f6b2013-06-13 22:57:01101 gfx::Rect GetLocalBoundsRect() const;
[email protected]02747d4e2010-11-03 19:10:00102
[email protected]9052cca2011-11-30 23:59:31103 // Returns the bounds of this object in screen coordinates.
[email protected]7f2d9f6b2013-06-13 22:57:01104 gfx::Rect GetGlobalBoundsRect() const;
[email protected]9052cca2011-11-30 23:59:31105
[email protected]516c16a2013-11-04 21:32:20106 // Returns the bounds of the given range in coordinates relative to the
107 // top-left corner of the overall web area. Only valid when the
108 // role is WebAXRoleStaticText.
109 gfx::Rect GetLocalBoundsForRange(int start, int len) const;
110
111 // Same as GetLocalBoundsForRange, in screen coordinates. Only valid when
112 // the role is WebAXRoleStaticText.
113 gfx::Rect GetGlobalBoundsForRange(int start, int len) const;
114
[email protected]9052cca2011-11-30 23:59:31115 // Returns the deepest descendant that contains the specified point
116 // (in global screen coordinates).
[email protected]02747d4e2010-11-03 19:10:00117 BrowserAccessibility* BrowserAccessibilityForPoint(const gfx::Point& point);
118
[email protected]c477540962013-03-07 00:43:10119 // Marks this object for deletion, releases our reference to it, and
[email protected]d96f3842014-04-21 18:07:29120 // nulls out the pointer to the underlying AXNode. May not delete
121 // the object immediately due to reference counting.
[email protected]8368e3c2011-03-08 19:26:24122 //
[email protected]c477540962013-03-07 00:43:10123 // Reference counting is used on some platforms because the
[email protected]8368e3c2011-03-08 19:26:24124 // operating system may hold onto a reference to a BrowserAccessibility
[email protected]c477540962013-03-07 00:43:10125 // object even after we're through with it. When a BrowserAccessibility
126 // has had Destroy() called but its reference count is not yet zero,
[email protected]d96f3842014-04-21 18:07:29127 // instance_active() returns false and queries on this object return failure.
[email protected]c477540962013-03-07 00:43:10128 virtual void Destroy();
[email protected]8368e3c2011-03-08 19:26:24129
130 // Subclasses should override this to support platform reference counting.
131 virtual void NativeAddReference() { }
132
133 // Subclasses should override this to support platform reference counting.
[email protected]402da9f2011-03-08 19:45:41134 virtual void NativeReleaseReference();
[email protected]8368e3c2011-03-08 19:26:24135
136 //
[email protected]f27e81c2010-10-07 05:20:23137 // Accessors
[email protected]8368e3c2011-03-08 19:26:24138 //
139
[email protected]0d7dad62010-10-19 21:18:50140 BrowserAccessibilityManager* manager() const { return manager_; }
[email protected]d96f3842014-04-21 18:07:29141 bool instance_active() const { return node_ != NULL; }
142 ui::AXNode* node() const { return node_; }
[email protected]eecf89f02013-08-20 23:41:51143 const std::string& name() const { return name_; }
[email protected]99d408f2013-12-04 07:19:43144 const std::string& value() const { return value_; }
[email protected]99d408f2013-12-04 07:19:43145 void set_name(const std::string& name) { name_ = name; }
146 void set_value(const std::string& value) { value_ = value; }
147
[email protected]c1f9be742014-04-10 13:15:46148 // These access the internal accessibility tree, which doesn't necessarily
149 // reflect the accessibility tree that should be exposed on each platform.
150 // Use PlatformChildCount and PlatformGetChild to implement platform
151 // accessibility APIs.
[email protected]d96f3842014-04-21 18:07:29152 uint32 InternalChildCount() const;
153 BrowserAccessibility* InternalGetChild(uint32 child_index) const;
[email protected]c1f9be742014-04-10 13:15:46154
[email protected]d96f3842014-04-21 18:07:29155 BrowserAccessibility* GetParent() const;
156 int32 GetIndexInParent() const;
[email protected]c1f9be742014-04-10 13:15:46157
[email protected]d96f3842014-04-21 18:07:29158 int32 GetId() const;
159 const ui::AXNodeData& GetData() const;
160 gfx::Rect GetLocation() const;
161 int32 GetRole() const;
162 int32 GetState() const;
163
mohan.reddy0079fba2014-09-25 07:25:23164 typedef base::StringPairs HtmlAttributes;
[email protected]d96f3842014-04-21 18:07:29165 const HtmlAttributes& GetHtmlAttributes() const;
[email protected]c1f9be742014-04-10 13:15:46166
[email protected]95b3f5442012-05-06 17:10:07167#if defined(OS_MACOSX) && __OBJC__
[email protected]cd3e2d902012-05-09 07:05:20168 BrowserAccessibilityCocoa* ToBrowserAccessibilityCocoa();
[email protected]1dbadbd2010-10-13 18:50:10169#elif defined(OS_WIN)
[email protected]cd3e2d902012-05-09 07:05:20170 BrowserAccessibilityWin* ToBrowserAccessibilityWin();
[email protected]f27e81c2010-10-07 05:20:23171#endif
172
[email protected]eecf89f02013-08-20 23:41:51173 // Accessing accessibility attributes:
174 //
175 // There are dozens of possible attributes for an accessibility node,
176 // but only a few tend to apply to any one object, so we store them
177 // in sparse arrays of <attribute id, attribute value> pairs, organized
178 // by type (bool, int, float, string, int list).
179 //
180 // There are three accessors for each type of attribute: one that returns
181 // true if the attribute is present and false if not, one that takes a
182 // pointer argument and returns true if the attribute is present (if you
183 // need to distinguish between the default value and a missing attribute),
184 // and another that returns the default value for that type if the
185 // attribute is not present. In addition, strings can be returned as
[email protected]fcf75d42013-12-03 20:11:26186 // either std::string or base::string16, for convenience.
[email protected]ee845122011-09-01 08:44:16187
[email protected]5eec2f52014-01-06 22:30:54188 bool HasBoolAttribute(ui::AXBoolAttribute attr) const;
189 bool GetBoolAttribute(ui::AXBoolAttribute attr) const;
190 bool GetBoolAttribute(ui::AXBoolAttribute attr, bool* value) const;
[email protected]eecf89f02013-08-20 23:41:51191
[email protected]5eec2f52014-01-06 22:30:54192 bool HasFloatAttribute(ui::AXFloatAttribute attr) const;
193 float GetFloatAttribute(ui::AXFloatAttribute attr) const;
194 bool GetFloatAttribute(ui::AXFloatAttribute attr, float* value) const;
[email protected]02747d4e2010-11-03 19:10:00195
[email protected]5eec2f52014-01-06 22:30:54196 bool HasIntAttribute(ui::AXIntAttribute attribute) const;
197 int GetIntAttribute(ui::AXIntAttribute attribute) const;
198 bool GetIntAttribute(ui::AXIntAttribute attribute, int* value) const;
[email protected]ee845122011-09-01 08:44:16199
[email protected]eecf89f02013-08-20 23:41:51200 bool HasStringAttribute(
[email protected]5eec2f52014-01-06 22:30:54201 ui::AXStringAttribute attribute) const;
202 const std::string& GetStringAttribute(ui::AXStringAttribute attribute) const;
203 bool GetStringAttribute(ui::AXStringAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51204 std::string* value) const;
205
[email protected]5eec2f52014-01-06 22:30:54206 bool GetString16Attribute(ui::AXStringAttribute attribute,
[email protected]fcf75d42013-12-03 20:11:26207 base::string16* value) const;
208 base::string16 GetString16Attribute(
[email protected]5eec2f52014-01-06 22:30:54209 ui::AXStringAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51210
[email protected]5eec2f52014-01-06 22:30:54211 bool HasIntListAttribute(ui::AXIntListAttribute attribute) const;
[email protected]eecf89f02013-08-20 23:41:51212 const std::vector<int32>& GetIntListAttribute(
[email protected]5eec2f52014-01-06 22:30:54213 ui::AXIntListAttribute attribute) const;
214 bool GetIntListAttribute(ui::AXIntListAttribute attribute,
[email protected]eecf89f02013-08-20 23:41:51215 std::vector<int32>* value) const;
216
[email protected]5eec2f52014-01-06 22:30:54217 void SetStringAttribute(ui::AXStringAttribute attribute,
218 const std::string& value);
[email protected]ee845122011-09-01 08:44:16219
220 // Retrieve the value of a html attribute from the attribute map and
221 // returns true if found.
[email protected]fcf75d42013-12-03 20:11:26222 bool GetHtmlAttribute(const char* attr, base::string16* value) const;
[email protected]eecf89f02013-08-20 23:41:51223 bool GetHtmlAttribute(const char* attr, std::string* value) const;
[email protected]ee845122011-09-01 08:44:16224
[email protected]38f1e3b052012-02-10 21:46:07225 // Utility method to handle special cases for ARIA booleans, tristates and
226 // booleans which have a "mixed" state.
227 //
228 // Warning: the term "Tristate" is used loosely by the spec and here,
229 // as some attributes support a 4th state.
230 //
231 // The following attributes are appropriate to use with this method:
232 // aria-selected (selectable)
233 // aria-grabbed (grabbable)
234 // aria-expanded (expandable)
235 // aria-pressed (toggleable/pressable) -- supports 4th "mixed" state
236 // aria-checked (checkable) -- supports 4th "mixed state"
237 bool GetAriaTristate(const char* attr_name,
238 bool* is_defined,
239 bool* is_mixed) const;
240
[email protected]c43b0dc2011-12-03 04:31:13241 // Returns true if the bit corresponding to the given state enum is 1.
[email protected]5eec2f52014-01-06 22:30:54242 bool HasState(ui::AXState state_enum) const;
[email protected]c43b0dc2011-12-03 04:31:13243
je_julie.kimff9c8d32015-01-12 11:14:46244 // Returns true if this node is an cell or an table header.
245 bool IsCellOrTableHeaderRole() const;
246
[email protected]ee845122011-09-01 08:44:16247 // Returns true if this node is an editable text field of any kind.
248 bool IsEditableText() const;
[email protected]02747d4e2010-11-03 19:10:00249
dmazzoni8b43c602014-12-17 21:23:35250 // True if this is a web area, and its grandparent is a presentational iframe.
251 bool IsWebAreaForPresentationalIframe() const;
252
[email protected]d1029c52011-10-21 21:59:51253 // Append the text from this node and its children.
[email protected]eecf89f02013-08-20 23:41:51254 std::string GetTextRecursive() const;
[email protected]d1029c52011-10-21 21:59:51255
[email protected]aa50cea82010-11-05 23:02:38256 protected:
257 BrowserAccessibility();
258
[email protected]d96f3842014-04-21 18:07:29259 // The manager of this tree of accessibility objects.
[email protected]f27e81c2010-10-07 05:20:23260 BrowserAccessibilityManager* manager_;
261
[email protected]d96f3842014-04-21 18:07:29262 // The underlying node.
263 ui::AXNode* node_;
[email protected]f27e81c2010-10-07 05:20:23264
[email protected]99d408f2013-12-04 07:19:43265 private:
[email protected]3dba3ed2013-12-17 21:24:01266 // Return the sum of the lengths of all static text descendants,
267 // including this object if it's static text.
268 int GetStaticTextLenRecursive() const;
269
[email protected]eecf89f02013-08-20 23:41:51270 std::string name_;
271 std::string value_;
[email protected]f27e81c2010-10-07 05:20:23272
[email protected]c5c2a672010-10-01 23:28:04273 DISALLOW_COPY_AND_ASSIGN(BrowserAccessibility);
274};
275
[email protected]e6b34872012-10-24 20:51:32276} // namespace content
277
[email protected]ba1fa652011-06-25 05:16:22278#endif // CONTENT_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_H_